IF ELSE
Code
##set 5 + 3 to correctAnswer
correctAnswer = 5 + 3
##set 35 to temperature
temperature = 35
##set name to bill
name = "Bill"
## check if correctAnswer is equal to 8,if correctanswer print out 8
##to test things if they are equal python use == sign
##note there is an indentation or what you call space after the if statement
##there is an : at the end of an if,else and elif statement
if correctAnswer ==8:
print("correctanswer")
## else if the correctAnswer is not equal to 8 ,print "wrong answer"
else:
print("wronganswer")
##check if temperature is equal to 40,if true print "correct answer"
if temperature == 40:
print("correct answer")
##else if temperature is not equal to 40,print "wrong answer"
else:
print("wrong answer")
##check if name is equal to Bill,if true print "correct answer"
if name == "Bill":
print("correct answer")
##else if name is not equal to bill print "wrong answer"
else:
print("wrong answer")
OutputcorrectAnswer = 5 + 3
##set 35 to temperature
temperature = 35
##set name to bill
name = "Bill"
## check if correctAnswer is equal to 8,if correctanswer print out 8
##to test things if they are equal python use == sign
##note there is an indentation or what you call space after the if statement
##there is an : at the end of an if,else and elif statement
if correctAnswer ==8:
print("correctanswer")
## else if the correctAnswer is not equal to 8 ,print "wrong answer"
else:
print("wronganswer")
##check if temperature is equal to 40,if true print "correct answer"
if temperature == 40:
print("correct answer")
##else if temperature is not equal to 40,print "wrong answer"
else:
print("wrong answer")
##check if name is equal to Bill,if true print "correct answer"
if name == "Bill":
print("correct answer")
##else if name is not equal to bill print "wrong answer"
else:
print("wrong answer")
Exactly like it said
Comparing Numbers
Now let's try to compare some numbers
##set num1 as the integer 4
num1= 4
##set num 2 as the integer 10
num2=10
##if num 1 is less than num 2 print out num1 is less than num2
if num1 < num2:
print(num1, "is less than", num2)
##if num 1 is more than num 2 ,print out num 1 is more than num 2
if num1 > num2:
print(num1, "is greater than", num2)
##if num 1 is equal to num 2 ,print out num1 is equal to num 2
if num1 == num2:
print(num1, "is equal to", num2)
##if num1 is not equal to num2
if num1 != num2:
print(num1, "is not equal to", num2)
num1= 4
##set num 2 as the integer 10
num2=10
##if num 1 is less than num 2 print out num1 is less than num2
if num1 < num2:
print(num1, "is less than", num2)
##if num 1 is more than num 2 ,print out num 1 is more than num 2
if num1 > num2:
print(num1, "is greater than", num2)
##if num 1 is equal to num 2 ,print out num1 is equal to num 2
if num1 == num2:
print(num1, "is equal to", num2)
##if num1 is not equal to num2
if num1 != num2:
print(num1, "is not equal to", num2)
Output
Let's try again by replacing the num with your own input
#all your input are string hence they need to be convert to int
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 < num2:
print(num1, "is less than", num2 )
if num1 > num2:
print(num1, "is greater than", num2 )
if num1 == num2:
print(num1, "is equal to", num2 )
if num1 != num2:
print (num1, "is not equal to", num2)
Output
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 < num2:
print(num1, "is less than", num2 )
if num1 > num2:
print(num1, "is greater than", num2 )
if num1 == num2:
print(num1, "is equal to", num2 )
if num1 != num2:
print (num1, "is not equal to", num2)
IF ELSE ELIF
Now,we are going to try something diff,if elif and else.
Elif allow you to modify the condition
##set answer as inputanswer=int (input("Enter the mark you got on your test"))
##if answer is greater or equal to 10,you got at least 10
if answer >= 10:
print("You got at least 10!")
##else if answer is greater or equal to 5,print you got at least 5
elif answer >= 5:
print ("You got at least 5!")
##else if answer is greater or equal to 3,print you got at least 3
elif answer >= 3:
print("You got at least 3!")
##else if it is not any above,print you got less than 3
else:
print("You got less than 3.")
Output
##if answer is greater or equal to 10,you got at least 10
if answer >= 10:
print("You got at least 10!")
##else if answer is greater or equal to 5,print you got at least 5
elif answer >= 5:
print ("You got at least 5!")
##else if answer is greater or equal to 3,print you got at least 3
elif answer >= 3:
print("You got at least 3!")
##else if it is not any above,print you got less than 3
else:
print("You got less than 3.")
Testing for more than one condition
Code
##input to enter your age
age = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##if age is more than or equal to 8
if age >= 8:
##if grade is more than or equal to 3,remember to indent
if grade >= 3:
##print "you can play this game "
print "You can play this game."
##else
else:
##print "sorry,you can't play the game"
print "Sorry, you can't play the game."
age = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##if age is more than or equal to 8
if age >= 8:
##if grade is more than or equal to 3,remember to indent
if grade >= 3:
##print "you can play this game "
print "You can play this game."
##else
else:
##print "sorry,you can't play the game"
print "Sorry, you can't play the game."
Output
Using and
That last example will work fine. But there is a shorter way to do the same thing. You can combine conditions like this:
##input to enter your age
age = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##if age is more than or equal to 8 and grade is more than or equal to 3
if age >= 8 and grade >= 3:
##print "you can play this game
print "You can play this game."
##else
else:
##print "sorry,you can't play the game
print "Sorry, you can't play the game.
Outputage = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##if age is more than or equal to 8 and grade is more than or equal to 3
if age >= 8 and grade >= 3:
##print "you can play this game
print "You can play this game."
##else
else:
##print "sorry,you can't play the game
print "Sorry, you can't play the game.
More and condition
You can put more than two conditions together with and:
Code
##input to enter your age
age = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##input to enter your favourite color
color = input("Enter your favorite color: ")
##if age is more than or equal to 8 and grade is more than or equal to 3 and color is equal to green,print" you are allowed to play this game"
if age >= 8 and grade >= 3 and color == "green":
print "You are allowed to play this game."
##else,print "sorry ,you can't play the game"
else:
print "Sorry, you can't play the game."
Output
age = float(input("Enter your age: "))
##input to enter your grade
grade = int(input("Enter your grade: "))
##input to enter your favourite color
color = input("Enter your favorite color: ")
##if age is more than or equal to 8 and grade is more than or equal to 3 and color is equal to green,print" you are allowed to play this game"
if age >= 8 and grade >= 3 and color == "green":
print "You are allowed to play this game."
##else,print "sorry ,you can't play the game"
else:
print "Sorry, you can't play the game."
If there are more than two conditions, all the conditions have to be true for the if
statement to be true. There are other ways of combining conditions too.
Using or
The or keyword is also used to put conditions together. If you use or, the block is executed
if any of the conditions are true
Code:
##input enter your favourite color
color = input("Enter your favorite color: ")
##if color is equal to red or color is equal to blue or color is equal to green,print"you are allowed to play this game
if color == "red" or color == "blue" or color == "green":
print "You are allowed to play this game."
else:
print "Sorry, you can't play the game."
Output





