Your goal should be to learn and help others learn.
Even if everyone here has programming experience, there is still a wide
range of experience levels. All are welcome!
π§π»βπ»π§πΌβπ»π©π»βπ»π§π½βπ»π§πΎβπ»π©π½βπ»π§πΏβπ»π©πΎβπ»π¨π»βπ»π©πΏβπ»π¨πΌβπ»π¨π½βπ»π©πΌβπ»π¨πΎβπ»π¨πΏβπ»
There are no "stupid" questions. Ask all your questions and welcome everyone
else's questions.
ππ»ππΌππΏββοΈππ½ππΎββοΈππΎππΏππ½ββοΈππ»ββοΈππΌββοΈππΌββοΈππ½ββοΈππ»ββοΈππΎββοΈππΏββοΈππ»ββοΈ
'a' + 'hoy'
'ahoy'
Programs manipulate values.
Each value has a certain data type.
Data type | Example values |
---|---|
Integers | 2 44 -3 |
Floats | 3.14 4.5 -2.0 |
Booleans | True False |
Strings | 'Β‘hola!' 'its python time!' |
Try in a Python interpreter, like on code.cs61a.org.
An expression describes a computation and evaluates to a value.
Some expressions use operators:
18 + 69
6/23
2 * 100
2 ** 100
Try in a Python interpreter, like on code.cs61a.org.
Many expressions use function calls:
pow(2, 100)
max(50, 300)
min(-1, -300)
Expressions with operators can also be expressed with function call notation:
2 ** 100
pow(2, 100)
add | ( | 18 | , | 69 | ) |
Operator | Operand | Operand |
How Python evaluates a call expression:
Operators and operands are also expressions, so they must be evaluated to discover their values.
This is called an expression tree.
After the lecture, you can try out this exercise. (Not graded, just another way to engage with the material!)
A name can be bound to a value.
One way to bind a name is with an assignment statement:
x | = | 7 |
Name | Value |
The value can be any expression:
x | = | 1 + 2 * 3 - 4 // 5 |
Name | Expression |
A name can be referenced multiple times:
x = 10
y = 3
result1 = x * y
result2 = x + y
A name that's bound to a data value is also known as a variable.
A name can only be bound to a single value.
my_name = 'Pamela'
my_name = my_name + 'ela'
π¬ Will that code error? If not, what will my_name
store?
It will not error (similar code in other languages might, however). The
name my_name
is now bound to the value 'Pamelaela'.
Try this after the lecture...
What will be the value of the final expression in this sequence?
f = min
f = max
g = min
h = max
max = g
max(f(2, g(h(1, 5), 3)), 4)
Don't write like that. Names matter!
An environment diagram is a visualization of how Python interprets a program. Use the free website PythonTutor to generate diagrams. View example
Code (left) | Frames (right) |
---|---|
Arrows indicate the order of execution. Green = just executed, red = up next. |
Each name is bound to a value. Within a frame, each name cannot be repeated. |
How Python interprets an assignment statement:
=
.=
sign.
A function is a sequence of code that performs a particular task and can be easily reused. β»οΈ
We've already used functions:
add(18, 69)
mul(60, sub(5, 4))
A function takes inputs (the arguments) and returns an output (the return value).
18, 69 β add β 87
The most common way to define functions is Python is the
def
statement.
def <name>(<parameters>):
return <return expression>
Example:
def add(num1, num2):
return num1 + num2
Once defined, we can call it:
add(2, 2)
add(18, 69)
The first line is called the function signature, all lines after are considered the function body.
def <name>(<parameters>): # β Function signature
return <return expression> # β Function body
def add(num1, num2): # β Function signature
return num1 + num2 # β Function body
The function body can have multiple lines:
def add(num1, num2): # β Function signature
sum = num1 + num2 # β Function body
return sum # β Function body
We can pass in any expressions as arguments.
def add(num1, num2):
return num1 + num2
x = 1
y = 2
add(x, y)
x = 3
add(x * x, x + x)
The return keyword returns a value to whoever calls the function (and exits the function).
def add(num1, num2):
return num1 + num2
sum = add(2, 4)
Reminder: You can use function calls in expressions:
big_sum = add(200, 412) + add(312, 256)
...and nest function calls inside function calls:
huge_sum = add(add(200, 412), add(312, 256))
What's wrong with this code?
def add(num1, num2):
return sum
sum = num1 + num2
sum = add(2, 4)
The code after the return statement will not be executed, that line belongs before the return.
What's wrong with this code?
def add():
return num1 + num2
sum = add(2, 4)
The function body is referring to variables that don't seem to exist. Most likely, they should be parameters in the function signature.
What's wrong with this code?
def add(num1, num2):
sum = num1 + num2
sum = add(2, 4)
The function body does not return any value. However, the code that calls it tries to use the result of the expression. It should have a return statement that returns the sum.
How Python interprets a def statement:
name
and
parameters
How Python interprets a function call:
All Python code is evaluated in the context of an environment, which is a sequence of frames.
We've seen two possible environments:
Global frame | |
Function's local frame, child of Global frame |
How Python looks up names in a user-defined function:
*This is simplified since we haven't learned all the Python features that complicate the rules.
def exclamify(text):
start_exclaim = "Β‘"
end_exclaim = "!"
return start_exclaim + text + end_exclaim
exclamify("the snails are eating my lupines")
start_exclaim
found in? text
found in? exclamify
found in?
start_exclaim = "Β‘"
end_exclaim = "β£οΈ"
def exclamify(text):
return start_exclaim + text + end_exclaim
exclamify("the voles are digging such holes")
start_exclaim
found in? text
found in? exclamify
found in?
def exclamify(text):
end_exclaim = "βοΈοΈοΈ"
return start_exclaim + text + end_exclaim
exclamify("the voles are digging such holes")
NameError
?start_exclaim
name, since it was never assigned.
exclamify
is called and Python tries
to execute the return statement.
You can try these exercises after the lecture for some additional practice:
To run the doctests, press the red test tube in the upper right corner.