CS61A Week 1: Python Basics and Environment Setup, Study notes of Advanced Education

An introduction to the basics of python programming, specifically tailored for the cs61a course. It covers essential concepts such as expressions, values, data types (integers, floats, booleans, strings), and operators. The notes also explain call expressions, nested expressions, and the evaluation process in python. Furthermore, the document discusses the use of names and variables, assignment statements, environment diagrams, and functions, including function definitions, parameters, return values, and the concepts of global and local frames. It also touches on name lookup rules, providing a foundational understanding of python programming for beginners.

Typology: Study notes

2025/2026

Available from 12/22/2025

quenter-arielle
quenter-arielle 🇺🇸

28 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61A: WEEK 1
NOTES:
It is pretty simple - get used to your coding environment and a few of the basics of
Python.
Lab 0
While this is optional, it’s probably best to do it just to get used to the very basics of
Python as well as ok.py, the autograder used in CS61A.
Basics of Python
Expressions + Values
Programs work to manipulate values
Expressions in programs evaluate to values
Values can have different data types (string, float, boolean, integer, etc.)
Python evaluates these expressions, and then (potentially) displays its values
Data Types
Data
Type
Example
Values
Integers 2, 44, 25
Floats 3.14, 2.73, 6
pf3
pf4
pf5

Partial preview of the text

Download CS61A Week 1: Python Basics and Environment Setup and more Study notes Advanced Education in PDF only on Docsity!

CS61A: WEEK 1

NOTES :

It is pretty simple - get used to your coding environment and a few of the basics of Python. Lab 0 While this is optional, it’s probably best to do it just to get used to the very basics of Python as well as ok.py, the autograder used in CS61A. Basics of Python Expressions + Values  Programs work to manipulate valuesExpressions in programs evaluate to values  Values can have different data types (string, float, boolean, integer, etc.) Python evaluates these expressions , and then (potentially) displays its values Data Types Data Type Example Values Integers 2, 44, 25 Floats 3.14, 2.73, 6

Data Type Example Values

Boolean s True, False Strings Hi, ben Operators These are pretty self-explanatory Opera tor Example Expression What it does

  • 10 + 2 Adds two values together
  • 10 - 2 Subtracts values
  • 102 Multiplies values / 10/2 Divides values // 9// Takes the floor of the divided value (result on the left would evaluate to 4) % 9% Takes the remainder of the expression (result on the left would evaluate to 1) ** 2* Finds the value of the left value to the power of the right value Call Expressions Oftentimes, however, expressions use function calls rather than operators (and the operators above have call function equivalents!)
  1. Next, the leftmost argument for add will be read, which in this case is the large block add(3,add(3,2))
  2. As this is still another call function, the add will be evaluated, with the operands 3 and add(3,2)
  3. 3 is a ‘base case’, so that does not need to be further evaluated, but add(3,2) goes through the same procedure, which will then evaluate to 5.
  4. Now because the inner add function has been fully evaluated, the value is calculated in reverse order 1. 3 + 5 is calculated, which will return 8 for the first argument.
  5. Same thing happens for the other operand, which will then evaluate to 22
  6. 22 and 8 get summed, which will then result in 30 A visual representation (in the form of an expression tree ) of what was said above can be seen below. Names A name can be bound to a value. This does not necessarily need to be a variable - it could also be a function or an expression for instance. Names are often used because they can be reused in different parts of the code. A name that’s bound to a value is known as a variable.

For example: x = 2 y = 3 print(x + y) # Returns 5 print(x - y) # Returns - These values can also change: x = 2 print(x)

>>> 2

x = x + 5 print(x)

>>> 7

The equals sign used above is not similar to the one used in mathematics; it is used for assignment rather than equality, which means that you set a value to the variable. This assignment statement works by

  1. Evaluating the expression on the right of the =
  2. Binding the value of the expression to the name on the left side of the = Environment Diagrams These are very useful to visualize how the Python interpreter thinks (and also appear somewhat often in exam questions). PyTutor is a very good resource to generate these environment diagrams if you ever get confused about how assignment works. Functions Functions are very useful in programming languages in general because they

allow lines of code to be easily reused. Functions, however, are slightly more

complicated than variables due to the local and global frames (more on that later). What is a function?

value = example(3)

Running the above line will print 3 first, then assign 3*2 to value

print(value)

>>> 6

Frames There are different frames, which you can think of as different rooms in the same house.

The global frame is an environment that contains all the variables and functions

that were created in the main body of the program.

A function’s local frame is a child of the global frame, where it has its own set of

variables that can’t be accessed outside the function. For example: unhelpful_name = 0 # variable in the global frame def unhelpful_function(unhelpful_name): # variable in the local frame (even though it has the same name it isn't the same variable) return unhelpful_name # >>> 2

The above will return 2 as opposed to 0 because the unhelpful_name variable

called in the function is the one passed into the function. unhelpful_function(2) Name Lookup Rules Python looks up names in a user-defined function using the following logic:

  1. Look it up in the local frame
  2. If the name is not in the local frame, look for the name in the parent frame
  3. If the name is not in any searched frame, throw a NameError.