If Statements

If statements can be used to evalute an expression. Example of checking if a variable called number is positive:
if number > 0:
  print(f"{number} is positive")
Make sure to indent each line in the body of the if statement. Let's also print something if the number is negative. To do that, add an elif statement, which is short for else if:
if number > 0:
  print(f"{number} is positive")
elif number < 0:
  print(f"{number} is negative")
The elif statement runs only if the previous if statement is false. Let's also print something if the number is 0. To do that, add an else statement:
if number > 0:
  print(f"{number} is positive")
elif number < 0:
  print(f"{number} is negative")
else:
  print("0 is not positive or negative")
A few notes about if statements:
  • An if statement must start with the if keyword, and there can be one only
  • You can have as many elif statements as you want after an if statement
  • There can be one else statement only, and it must be at the very end of the if and elif statements

Exercise 1 of 4

Write the following as an if statement: If age is greater than 30, print "Hello sir". Use two spaces to indent.

Your code

Exercise 2 of 4

Write the following as an if statement: If total is less than or equal to 12.5, print "Too low".

Your code

Exercise 3 of 4

Write the following as an if statement: If name is equal to "Lizz", print "Hello Lizz". Otherwise, print "I don't know you".

Your code

Exercise 4 of 4

Write the following as an if statement: If x is equal to 0, print "zero". But if x is greater than 0, print "positive". Otherwise, print "negative".

Your code