if then else python code example
Example 1: python if statement
usrinput = input(">> ")
if usrinput == "Hello":
print("Hi")
elif usrinput == "Bye":
print("Bye")
else:
print("Okay...?")
Example 2: if else python
# IF ELSE ELIF
print('What is age?')
age = int(input('number:')) # user gives number as input
if age > 18:
print('go ahead drive')
elif age == 18:
print('come personaly for test')
else:
print('still underage')
Example 3: python if else
# The if,elif,else statements check if something is True
# example
if 10 > 5:
print("Ten is greater than five")
#but let's say we write
if 10 < 5:
print("Ten is less than five")
#then this condition wouldn't be true
#so we create an "else" statement
if 10 < 5:
print("Ten is less than five")
else:
print("Ten is greater than five")
#this will print "Ten is greater than five"
# because in the if statement it checks if something is true
# we wrote 'if 10 < 5' and it's not true
# so in case the 'if' condition evaluates to false
# it executes another command
#now, the 'elif' statement
# this will check for another thing after the if statement
#example
if 10 < 5:
print("Ten is less than five")
elif 10 > 5:
print("Ten is greater than five")
# here we check if 10 is less than five
# then the statement evaluated to false because 10 is greater than 5
# so the program checked with the 'elif' (that means 'Else if')
# if the statement was true, and it was so it executed the command
#we can add as many elif statements as we want in our program
# but they always need to start with an 'if' statement
# and we can make only ONE 'else' statement at the end
# let's add an 'else' statement to the previous program
if 10 < 5:
print("Ten is less than five")
elif 10 > 5:
print("Ten is greater than five")
else:
print("Ten isn't greater or less than five")
# in this case it will print 'Ten is greater than five'
# hope this helped
Example 4: else if python
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example 5: if statement python
x=1; y=0; z=0;
if 1 in {x,y,z}:
print('At least one variable is equal to 1')
Example 6: if then else python
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")