Week 1 - Welcome to 61A!
Table of Contents
Installing Python
- Follow the instructions over at Lab 00.
Intro to Python
A programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about computational processes. Programs serve to communicate those ideas among the members of a programming community. Thus, programs must be written for people to read, and only incidentally for machines to execute.
We will introduce expressions, which are statements that Python can execute. Then, we will go over how Python executes them, and how they can be stored in variables. Lastly, we will discuss the print
function.
A brief aside
Generally, in 61A your assignments (labs, homework, and projects) will be given in a Python file with the extension .py
. You can edit these files, and the Python interpreter will execute it line by line.
That being said, Python is a great introductory programming language. There are many features that make it friendly, one of which is the Python interpreter, which can be started with python3 -i
(try it out in your terminal!).
Throughout 61A, you will see expressions such as
>>> print("hi")
hi
The >>>
represents statements executed directly in a Python interpreter (ie. not from a file), and the line after is the output we get from executing the >>>
line.
Primitive Expressions
Primitive expressions represent the simplest building blocks that the language provides
Python defines many built in types of primitives, and the most commonly used types are int
, float
(floating point number), bool
(boolean, or True/False type), str
(string type), and other container types that we will get into later
>>> 3.14 # this is a float
3.14
>>> 'hello!' # this is a string
'hello!'
>>> "double quotes!" # this is another string
'double quotes!'
Call Expressions
Call expressions look like
>>> function_name(my, arguments, here)
and they apply a function to an argument, and give you a result back. Later in class, we will learn how to define our own functions to create more reusable building blocks.
>>> max(1, 3) # takes the max of 1 and 3
3
>>> pow(2, 3) # takes 2 to the 3rd power
8
>>> abs(-1) # returns the absolute value of -1
1
Names / Environment
Python also allows for assignment, meaning that you can store values inside of variables.
>>> a = 42
>>> a
42
>>> my_sum = a + 10
>>> my_sum
52
>>> ten = abs(-10)
>>> ten
10
The =
binds the left name to the right value inside the current frame of execution. In the above case, we are executing these lines in the default global frame, which we can see below.
Evaluation
When Python evaluates a call expressions, it uses this order:
- Evaluate operator and operand
- Apply the operator to the operand.
Example #1: Simple Evaluation
>>> abs(-10)
10
- Evaluate the operator and the operand
- Here, the operator is
abs
and the operand is-10
. - Looking up
abs
, we get that it’s the built-in functionabs
. -10
is a primitive, so we are good to go.
- Here, the operator is
- Apply the operator to the operand
- We apply the built-in function
abs
to-10
, and we get10
as our answer.
- We apply the built-in function
Example #2: Nested Evaluation
What happens when we have nested evaluation statements? (Example taken from composingprograms)
>>> mul(add(2, mul(4, 6)), add(3, 5)) # assume we have add and mul defined
208
When we have nested evaluation statements, Python will follow the same procedures. Let’s walk through a few steps:
- Evaluate the operator and the operand
- The operator in the outermost statement is
mul
. This is a valid name in our current environment. Then, our operands areadd(2, mul(4, 6))
andadd(3, 5)
. Since these are not primitive expressions, we must evaluate them to get a value. - For
add(2, mul(4, 6))
:- Evaluate the operator and operand of
add(2, mul(4, 6))
. The operator isadd
and the operands are2
andmul(4, 6)
.2
is a primitive expression, so we can continue here.mul(4, 6)
needs to be evaluated. Using the same process, we get 24. - Apply the operator to the operand. Applying
add
to2
and24
returns26
.
- Evaluate the operator and operand of
- For
add(3, 5)
: Using the same process above, we get8
.
- The operator in the outermost statement is
- Apply the operator to the operand. Our operator is
mul
, and our operands are26
and8
. Our final return value is208
.
Print is a function that
- Returns
None
(nothing), and - Prints a value out to the terminal.
>>> print("hi")
hi
>>> print(4.2)
4.2
>>> print(print("hi"))
hi
None
In the last example above, think about the evaluation order! (eval operator, operand, apply)
Explanation
- Eval operator and operand of
print(print("hi"))
- Operator is
print
- Operand is
print("hi")
- Eval operator and operand of
print("hi")
- Operator is
print
- Operand is
"hi"
- Operator is
- Apply operator to operand. Here,
hi
is printed out to the terminal, andNone
is returned.
- Eval operator and operand of
- Operand is
None
- Operator is
- Apply
print
toNone
.None
is printed out to the terminal, andNone
is returned.