If-statement


You use an if-statement if you only want some code to run if a certain condition is met. The condition is a boolean that evaluates to either true or false.

 if condition: 
   body

Else

What happens if the condition is not met? You can use an else statement to specify what should happen only if the condition in the if-statement if False.

For example, here is a program that asks the user to enter a number. The program will print out whether the number is equal to 0 or not.

num = int(input("Enter a number: "))
if num == 0: 
   print("That number is 0")
else: 
   print("That number is not 0")

If Else Revisited

elif is short for else if.

Here's an example of how it's used:

num = int(input("Enter a number: "))
if num == 0: 
   print("Your number is 0")
elif num > 0:
   print("Your number is positive")
else:
   print("Your number is negative")