#Conditionals statements in python
#'=' conditionals statements
a = 123
b = 123
if(a==b):
print('True')
#<, > conditionals statements
a = 2
b = 45
if(a<b):
print('A is smaller than B')
answer = input(":")
if answer == "lol":
print("haha")
else:
print("not haha")
exit()
please note that the exit() command is optional and is not necessary.
# How conditions work
if condition:
# if condition met
# code here
elif condition2: # (else if) checked if the first condition is false
# if condition2 met
# code here
elif condition3: # elif can be chained
# if condition3 met
# code here
else:
# if none of the conditions are met
# code here
# example
if name == "bob": # ex. a == b (is a equal to b)
print("the wifi password is 1234")
elif name == "jake": # if the first condition is not met python checks this one
print("the secret formula is under the bed")
else: # if none of the conditions are met
print("sorry i don't know who you are")
# Other Example
if age >= 18:
print('You can go in')
else:
print('you cannot pass, you are underage')
can_do = True
can_do1 = True
if can_do:
print("we can do it")
elif can_do1:
print("we can do it but the second time")
else:
print("we cant do it")
#result should be (we can do it.)
variable_name = input("y/n? >> ")
if variable_name == "y":
print("That's good! :) ")
# note the double equal signs. only a single equal sign will receive a Syntax error blah blah blah message.
elif variable_name == "n":
print("That's bad! :( ")
else:
print("You blow up for not following my instructions. Detention forever and get lost!")
x = int(input("number: ")) # get number from user
if x < 0:
print(f"{x} is less than 0!") # if x is less than 0, print x is less than 0
elif x == 0:
print(f"{x} is equal to 0!") # if x is equal to 0 then print x is equal to 0
if x > 0:
print(f"{x} is more than 0!") # if x is greater than 0 print x is more than 0
# yeah its me somewhatoriginal
# If the number is positive, we print an appropriate message
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
user_info = input("How may I help you?: ")
if user_info == "on":
print("Light is Turned Om")
elif user_info == "off":
print("Light is turned Off")
elif user_info == "status":
input("All are Good.What do you need?: ")
print("I don't have it")
else:
print("Shutdown processing are being Ready")
#copy the code to your py script to have the results!!!
Num1=float
Num2=float
Ans=float
operator=int
print("These are the following operations available:
, 1 for addition.
2 for subtraction.
3 for multiplication.
4 for division.")
operator = int(input("Enter the operator you wish to use"))
Num1 = float(input("Please enter your first number:"))
Num2 = float(input("Please enter your second number:"))
if operator ==1:
Ans == Num1 + Num2
else:
if operator ==2:
Ans == Num1 - Num2
else:
if operator ==3:
Ans == Num1 * Num2
else:
if operator ==4:
Ans == Num1 / Num2
print("Your answer is: %f.2", Ans)
temperature = float(input('What is the temperature? '))
if temperature > 70:
print('Wear shorts.')
else:
print('Wear long pants.')
print('Get some exercise outside.')
def e(x):
if x == "Sunny" and x == "sunny":
print('Remember your sunglasses!')
elif x == "Rainy" and x == "rainy":
print('Do not forget your umbrella!')
elif x == 'Thunderstorm' or x == 'thunderstorm' or x =='Stormy' or x == 'stormy':
print('Stay Home!')
x = input('What is the weather?')
person = input("Nationality? ")
if person == "french" or person == "French":
print("Préférez-vous parler français?")
if person == "italian" or person == "Italian":
print("Preferisci parlare italiano?")
can_run = True
can_run2 = False
if can_run:
print("i can run the code because can_run is true")
elif can_run2:
print("i can run the code if can_run2 is true")
else:
print("no other bool found true")
#result should be (i can run the code because can_run is true
number = int(input("Enter a number from 1 to 3"))
# make the user write a random number from 1 to 3
if number == 1:
print("You choose number 1")
elif number == 2:
print("You choose number 2")
else:
print("You choose number 3")
water = input('Does your creature live underwater?')
if water == 'yes':
print('Your creature lives underwater')
else:
print('Your creature does not live underwater')
num = 1
if num == 1:
print("num is 1"))
elif num == 2:
print("num is 2")
else:
print('invalid number')
# output num is 1
# if the conditional is true the left side of side the expression is resolved else the right side is resolved
print("num is 2") if num == 2 else print("num is 1")
# output num is 1
usrinput = input('Type a number')
if usrinput = 5
print('Your number is equal to 5')
elif usrinput > 5
print('Your number is bigger than 5')
elif usrinput < 5
print('Your number is less than 5')
boolean1 = (1 != 1)
boolean2 = (2 + 2 == 5)
boolean3 = (1 == 1)
if (boolean1):
print("Boolean one is true")
elif (boolean2):
print("Boolean two is true")
else:
print("Boolean three may, or may not be true")