In [ ]:
In [9]:
name = "Chris"
print(name == "Chris")
print(2 == 5)
In [10]:
2 > 5
"a" < "b"
3.33 > 7
3.33 == 7
3.33 >= 7
400 <= 1000
400 < 1000
1000 == 1000
1000 <= 1000
1000 >= 1000
Out[10]:
If you want to know more about comparing strings like this, then look at this
In [11]:
print(name != "Bob")
In [12]:
print(2 is 2)
name = "Chris"
print(name is not name)
What? I hear you ask, how is this any different to ==
?
Hold your horses and let's find out.
In [13]:
a = "chris"
b = "chris"
print(a is b) # identity (True)
print(a == b) # equality (True)
In [14]:
a = [] # we will learn about these soon!
b = []
print(a is b) # identity (False)
print(a == b) # equality (True)
These are both comparing the same thing? Why is one True
and the other is False
?
Equality comparison is only concerned with the value
Identity comparison is only concerned with the object. If 2 objects have the same value but are not stored in the same memory address then they are not equal. This can be explained further by...
Immutable means that something cannot be changed after it is created
Mutable means that something can be changed after it is created
In [15]:
string_var = "fdjklhd" #immutable
print(string_var) #fdjklhd
s = string_var
s += "ueoureo"
print(string_var) #fdjklhd
print(s) #fdjklhdueoureo
list_var = [] # mutable and we are still learning about these later
print(list_var) #[]
l = list_var # l is a reference to list_var
l.append(1)
print(list_var) #[1]
print(l) #[1]
In [16]:
age = 21
age < 15
age > 25
Out[16]:
But there must be a better way! What do you think?
In [1]:
make = "Tesla"
capacity = 80000
year = 2016
zero_to_sixty = 3
print(make == "Tesla" or make == "tesla")
print(make == "Tesla" and make == "tesla")
print(capacity < 80000 and capacity > 60000)
print(capacity > 60000 or capactity < 100000 and year == 2014 or year == 2016)
In [18]:
15 < age < 25
Out[18]:
In [19]:
no_hours_slept = 3
if no_hours_slept < 5:
print("Good Lord, turn Netflix off")
print("This print statement does not care how many hours you slept")
Take note of the structure
:
at the end of the if statementnormal
running
### Exercise
What are the benefits of if statements?
What kind of control is this?
Write an if statement that prints hello
and the name if the name
variable is equal to Winston Churchill
In [2]:
name = "Homer"
age = 80
print("My name is " + name)
print("I am %d" % age)
print("I am over one hundred years old!") #is this true? With no flow control it will print anyway
In [ ]:
name = "Homer"
age = 80
print("My name is " + name)
print("I am " + age)
if age > 100:
print("I am over one hundred years old!") #is this true? With no flow control it will print anyway
print("this will print if Homer is over 100, too") #as long as you stay indented, this is all part of the if
print("this will print regardless")
3
EQUAL FOR ALL
if it is equal to 3 using equality comparison IDENTITY FOR ALL
if it is equal to 3 using identity comparison10
TEN AND THREE
if the first variable is equal to 3 AND the second is equal to 10
In [ ]:
first_var = 3
if first_var == 3:
print("EQUAL FOR ALL")
if first_var is 3:
print("IDENTITY FOR ALL")
if first_var:
print("I AM REAL")
if first_var is not None:
print("I AM NOT NONE")
second_var = 10
if first_var == 3 and second_var == 10:
print("TEN AND THREE")
In [ ]:
age = 12
if age >= 18:
print "come on in"
If the user is 18 or over then we print that they can come in. What about the users under 18? They get no output. How do we get around this?
In [ ]:
age = 12
if age >= 18:
print("come on in")
else:
print("I think this ID is fake")
Declare and instantiate variables to store a users details (name, height and year of birth)
Wow, just wow
if they are 7ft or overYOUNGUN
if they are under 30
In [ ]:
name = "Jill"
height = 8
yob = 1990
year = 2016
print(name)
if height < 7:
print(height)
else:
print("WOW, JUST WOW")
age = year - yob
if age >= 30:
print(age)
else:
print("YOUNGUN")
In [1]:
age = 800
if age < 13:
print("Child")
elif age > 13 and age < 20:
print("Teenager")
elif age >= 21 and age < 140:
print("Adult")
else:
print("HOW HAVE YOU CHEATED DEATH?")
print("YOU ARE A MACHINE")
In [2]:
pet = "cat"
if pet == "fish":
print("Aquatic amigo")
elif pet == "cat" or pet == "dog":
print("four legged friend")
elif pet == "lizard" or pet == "dragon":
print("reptilian rapport")
else:
print("what the smeg")
if(x > 10):
is the same as
if x > 10:
Then what are brackets for?
Precedence, or order of evalutation
, is exactly the same as BODMAS (Brackets, Order, Division, Multiplication, Addition and Subtraction) from GCSE Maths.
In [ ]:
print(5 + 6 * 9)
print((5 + 6) * 9) #see how the brackets change the order? This applies to if statements as well
In [ ]:
4 * (5 / (2 - 1))
(5 + 6) / (2 * 8)
4 / ((1 * 2) + 1)
In [4]:
print(1 + 1 == 1)
print(1 + (1 == 1)) #Why does this output what it does?
This is because adding boolean and integers adds the integer equiavalent of the boolean variable, which makes True
equal to 1.