Python Conditions


Python Conditions and if Statements
  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

if Statements

If is a keyword for conditional statements.

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

Output:
b is greater than a

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

Output:
Error
*** Indentation Error

In case of other programming language, there is a curly brackets, but in python it is different, i.e. white space.

Elif statements

If the previous statements were not true, try this condition.


Example:
a = 33
b = 33
if b > a:
  print("b is greater than a")   #this condition is not true.
elif a == b:
  print("a and b are equal")

Output:                                                                                                
a and b are equal

Else Statements

The else keyword catches anything which isn't caught by the preceding conditions

Example:
a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

Output:
a is greater than b

Example:
a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

Output:
B is not greater than a

*one line if statements
a = 200
b = 33
if a > b: print("a is greater than b")

Output:
a is greater than b


Comments

Post a Comment

Popular posts from this blog

Basic Operators in Python