Conditions in Python

#Conditions
x = 2
print(x==2) #prints out True
print(x==3) #prints out False
print(x<3) #prints out True

#Using "and" + "or" keywords
name = "John"
age = 23
if name == "John" and age == 23:
    print("Your name is John, and you are also 23 years old.")

if name == "John" or name == "Rick":
    print("Your name is either John or Rick.")

#Using "in" keyword
name = "John"
if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")

#Using the "is" keyword.  "is" compares instances vs "==" which compares values

x = [1,2,3]
y = [1,2,3]
print(x == y) #Prints out True
print(x is y) #Prints out False

#using the "not" keyword
second_number = not 10
print(second_number) #Prints False
print(not second_number) #Prints True

Leave a comment

Design a site like this with WordPress.com
Get started