If ... Else w Pythonie

Warunki i instrukcje If w Pythonie

Python obsługuje powszechnie używane warunki logiczne z matematyki:

  • równa się:a == b
  • nie równa się:a != b
  • mniej niż:a < b
  • less than or equal to:a <= b
  • greater than:a > b
  • greater than or equal to:a >= b

These conditions can be used in many ways, the most common being "if statements" and loops.

if statements use if keywords to write.

Przykład

If statement:

a = 66
b = 200
jeśli b > a:
  print("b is greater than a")

Uruchom przykład

In this example, we used two variables,a and bis part of the if statement and is used to test if b is greater than a. Since a is 66 and b is 200, we know that 200 is greater than 66, so we print "b is greater than a" on the screen.

Indentation

Python depends on indentation to define the scope of code, using spaces. Other programming languages usually use braces for this purpose.

Przykład

No indentation if statement (will cause an error):

a = 66
b = 200
jeśli b > a:
print("b is greater than a") # Will cause an error

Uruchom przykład

Elif

elif is the Python way of expressing "if the previous condition is not correct, try this condition".

Przykład

a = 66
b = 66
jeśli b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

Uruchom przykład

In this example,a equal b, so the first condition is not met, but elif The condition is true, so we print "a and b are equal" on the screen.

Else

The else keyword captures any content not captured by the previous conditions.

Przykład

a = 200
b = 66
jeśli b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
inaczej:
  print("a is greater than b")

Uruchom przykład

In this example,a greater b, so the first condition is not met,elif conditions are also not met, so we move on to else conditions and print "a is greater than b" on the screen.

You can also use no elif of else:

Przykład

a = 200
b = 66
jeśli b > a:
  print("b is greater than a")
inaczej:
  print("b is not greater than a")

Uruchom przykład

Abbreviated If

If you only have one statement to execute, you can put it on the same line as the if statement.

Przykład

Single-line if statement:

a = 200
b = 66
if a > b: print("a is greater than b")

Uruchom przykład

Abbreviated If ... Else

If you only have two statements to execute, one for if and one for else, you can put them all on the same line:

Przykład

Single-line if-else statement:

a = 200
b = 66
print("A") if a > b else print("B")

Uruchom przykład

You can also use multiple else statements on the same line:

Przykład

Single-line if-else statement, with three conditions:

a = 200
b = 66
print("A") if a > b else print("=") if a == b else print("B")

Uruchom przykład

And

i Słowo kluczowe to operator logiczny, używany do łączenia warunków w zdaniach warunkowych:

Przykład

Testuj, czy a jest większe niż b, a c jest większe niż a:

a = 200
b = 66
c = 500
jeśli a > b i c > a:
  wypisz("Oba warunki są prawdą")

Uruchom przykład

Or

oraz Słowa kluczowe są również operatorami logicznymi, używanymi do łączenia warunków w zdaniach warunkowych:

Przykład

Testuj, czy a jest większe niż b, lub czy a jest większe niż c:

a = 200
b = 66
c = 500
jeśli a > b lub a > c:
  wypisz("Przynajmniej jedno z warunków jest prawdą")

Uruchom przykład

If wewnętrzny

Możesz zawrzeć if w if, co nazywa się if-embedowanym.

Przykład

x = 52
jeśli x > 10:
  wypisz("Ponad dziesięć,")
  jeśli x > 20:
    wypisz("i również wyższej niż 20!")
  inaczej:
    wypisz("ale nie wyższej niż 20.")

Uruchom przykład

Polecenie pass

Zakazane puste if-y, ale jeśli z jakiegoś powodu napisałeś pusty if, użyj polecenia pass, aby uniknąć błędów.

Przykład

a = 66
b = 200
jeśli b > a:
  przechodź

Uruchom przykład