4. Logic, if/else
4.1. Conditionals
We’re pretty good at telling python exactly what to do now.
But there is no room for choice. It’s just: do these steps in this order
That doesn’t give us much flexibility (or computational power)
Activity
Using only the Python features/statements we’ve seen so far, can you write a program that will divide a number in half only if that number is a multiple of 2?
We need to work towards conditional execution
4.2. Logic First
Sometimes things are True or False
- I’m betting you actually are pretty familiar with some LOGIC
AND
OR
NOT
Activity
Write out the truth tables for the logical operations AND, OR and NOT.
Don’t know what a ‘logical operator’ or ‘truth table’ is? No problem, Ask:
Wikipedia
Google
Your neighbour
To make parts of the program conditionally executed, we need a formal way to describe conditions.
We need: logic.
- Let’s try some comparison:
>>> 19 == 87 False
>>> 5 == 5 True
- Note that
==
is comparison while=
is assignment. They are not the same! Python will punish you if you forget this! You’ll all mess this up eventually
Just be thankful this isn’t C/C++
- Note that
Activity
Figure out what the other comparison operators in Python are. Hint: 3
doesn’t equal 5
, it is ____ than 5.
- These operators can be applied to any two expressions (could be simply a value or variable, but can be more complex):
>>> a = 15 >>> b = 37 >>> (a+b)*9 > (b-a)*3 + 2 True
What is the type of the result of applying a comparison operator?
4.3. Conditional execution
Now we can test if a comparison statement is
True
orFalse
.We need a way to use that to control our program.
if
some condition isTrue
, do something:if grade < 50: print('should have gone to class more often.')
If the condition following the keyword
if
isTrue
, the code after the:
gets executed.If the condition is
False
, the code gets skipped over.As usual, the block that gets executed/skipped is denoted with indentation
The block can be as long as you want; no maximum size (though the minimum size is 1)
(like in a function definition)
NOTE: Remember that the thing in the if statement is either True or False!
Activity
Using only the Python features/statements we’ve seen so far, can you write a program that will divide a number in half only if that number is a multiple of 2?
HINT: You may want to look up the Python modulus operator: %
.
4.4. Compound conditions
We can use the logical operators
and
,or
andnot
to combine conditions.The combinations can be arbitrarily complex:
if (grade < 90 and personality_type == 'A' and desired_career == 'med school') or (grade < 100 and personality_type == 'AAA'): print('Time to ask for extra credit!')
4.5. Alternative execution
This pattern is very common:
if x > 10: do_something() if not(x > 10): do_something_else()
Programmers are lazy and don’t want to type the condition (here
x > 10
) twice (this also introduces the chance for more bugs)So
if
statements have a specialelse
statement that can go with them:if x > 10: do_something() else: do_something_else()
Does exactly the same thing as the preceding code… but…
Involves less typing and is easier to read and understand.
Activity
Write a Python function called hail
that takes an integer as its argument. If the integer is even, return the value of the integer divided by 2. If it’s odd, return the value of the integer multiplied by 3 and with 1 added.
That is: n
goes to 2/n
if even, 3*n+1
if odd.
HINT: You may want to look up the Python modulus operator: %
.
This is actually some neat math stuff
Isn’t it interesting that we’re writing a function that’s doing exactly what the math is saying?
4.6. Chains of alternatives
Sometimes a binary
if
/else
isn’t enough.What if I want several, mutually exclusive, alternatives?:
if year < 1960: print('Jazz') elif year < 1980: print('Rock') elif year < 1990: print('Synthpop') elif year < 2003: print('Alternative') else: print('Music died when I got my first real job')
elif
is a contraction ofelse if
NOTE: Only one of the
elifs
gets executed, that’s it. The remaining ones are completely ignored.- You can chain as many as you want
- If these were just
if
s, what would happen ifyear
was1980
? Let’s try it quick.
- If these were just
Always end with a plain
else
to catch any conditions not covered in the chain.
Activity
Write a Python function that takes an integer from 0-100 representing a course grade and returns a string representing the letter grade: A,B,C,D or F. You can pick the cutoffs.
4.7. Nested conditionals
Computer scientists love “nesting” things: putting things inside other things.
You can “nest” a conditional inside another conditional:
if x > 0: if y > 0: print('First Quadrant') else: print('Fourth Quadrant') else: if y > 0: print('Second Quadrant') else: print('Third Quadrant')
In the above example, we could have done it without nesting by using
and
and whatnot, but for fun we can nest it- Again, no limit to how deep you nest… but mind the readability of your code!
Actually, technically, Python has a limit of 20 nested statements, but other languages have much deeper limits
In a more mathematical/theoreticaltical sense, there is no limit
Do not go to anyone with this:
if a > 0: if b > 0: if c > 0: if d > 0: if e > 0: if f > 0: if g > 0: if h > 0: if i > 0: if j > 0: if k > 0: if l > 0: if m > 0: if n > 0: if o > 0: if p > 0: if q > 0: if r > 0: if s > 0: if t > 0: if u > 0: if v > 0: if w > 0: if x > 0: if y > 0: if z > 0: print('I want my TA to hate me') else: print("I'm making a huge mistake")
4.8. Libraries
Most of you are here because you are pragmatic people who want to get stuff done
The fastest way to get stuff done is by leveraging stuff that other people have done.
Remember functions? Wouldn’t it be awesome if there were huge collections of functions that already existed… and did a lot of the stuff you want to do?
Python has a huge variety of existing libraries/packages.
4.9. NumPy
The most important library for us is Numerical Python (“NumPy” for short).
For anyone working with real data in Python, NumPy is awesome
- Because it isn’t ‘built in’ to Python, we have to tell Python that we want to use NumPy:
>>> import numpy