You must also submit something to Gradescope for the points to count
This submission does not need to be finished!
Pre-lab Checkoffs
If you want to get lab checked off without attending lab, you must finish both the required and the optional questions of the lab, and then send it to me!
For now, use email, but by next discussion, I'll have something for submitting using a Google Form which I'll be a lot better at keeping track of.
Questions/comments from last section
Please fill out the attendance form with stuff you want to show up here!
(Put some fun stuff - I like answering fun questions)
Have had questions about games/music/etc. if that's the stuff you want to fill
Will also ask you to fill something for Lab so I have more material for this slide next time.
Temperature Check
Call Expressions (call expression order, what they look like)
This is Python specific! This table above doesn't necessarily apply to other languages (and later into the semester you'll see an example where the table above doesn't match the way the language works)
Boolean Operators
not
Returns the opposite truthy value of the expression.
For example, if you type not 0, Python will return True
and
Short circuits if it reaches a falsey value, and returns that value
This is not necessarily False
If all the values are truthy, the last value is returned
or
Short circuits if it reaches a truthy value, and returns that value
This is not necessarily True
If all the values are falsey, the last value is returned.
Short Circuiting
In call expressions, everything is evaluated from left to right, but this is not the case for when things short circuit.
In and and or statements, all statements are not necessarily evaluated.
and will keep on evaluating from left to right until it finds the first falsey value. If it finds a falsey value, it simply returns that value
Short Circuiting
The last value will always be returned as is if you reach it:
For or, it will short circuit when it reaches the first truthy value, so if it sees only falsey values until the end, or will simply just return the last value as is.
Let's take a look at a smaller example:
False, True ⇒ True
False, False ⇒ False
Notice how the last elements are the same
This is why the last element is returned as is (it's very similar for and, except everything before is True)
Short Circuiting
Examples
1 and True and 1/0
This will error
1 and True and 0 and 1/0
Returns 0
1 or True or 1/0
Returns 1
0 or 1 or True or 1/0
Returns 1
If Statements
if <condition>:
<block of statements>
[elif <condition>:] # optional; short for 'else if'
<block of statements>
[else:] # optional
<block of statements>
Don't forget the colons!
else does not need a conditional
You can chain together as many elif blocks as you want
Evaluate all ifs unless there's a return statement that ends the function
If you have a whole block of if/elif/else, you only evaluate at maximum 1 of the blocks.
Example (If Statements)
n = 0if n == 0:
print("hi")
else:
print("bye")
if n == n:
print("0")
In this case, the console will output
hi
0
return in Functions
defbox(x):return x + 2print(x + 3) # you will never reach this line
x = box(3)
x # 5
return will prematurely exit a function, and will make Python evaluate that function call to whatever gets returned
Worksheet!
While Loops
while <condition>:
<block of statements>
A while loop allows for a repeated execution of a certain block of code, allowing you to write just one thing that will end up being executed multiple times.
The condition is checked before the execution of each iteration.
To avoid an infinite loop, you must make sure your while loop changes the variable in the condition
While Loops Examples
Example 1
n = 0while n < 5:
print(n)
n = n + 1# Without this line you will have an infinite loop!print(n)
Output:
0
1
2
3
4
5
While Loops Examples
Example 2
n = 5while n < 5:
# Doesn't pass the condition on the initial loop# as a result, doesn't run any of the blocksprint(n)
print(n)