Topic2 Variables Expression Statements
Topic2 Variables Expression Statements
Topic 2
Variables, Expressions, and Statements
Fall 2021
Michael S. Brown
EECS Department
York University
Output of running
Python Interpreter program
lda 10 r1
lda 20 r2
add r1 r2 15
sto 21 20
out
dat 5 ..
data 10
Important: When you use True, False, or None that the first letter is capitalized.
For example, False and FALSE are not the same, only False is the Boolean literal
• For example, in the previous slide you saw A-z which is a fast way of
saying all characters representing letters (upper case and lower
case). A-Z means only uppercase, a-z only lowercase, A-z means
all.
Most programming languages (Python included) will scan the program before running it to detect
any syntax errors. If it finds an error it will not run the program. The example on the previous slide
is a syntax error.
Let's consider the simple program below. Notice that I've added line numbers. Even though you
don't have to put in line numbers when you program, your interpreter keeps track of the line numbers
of your of code. Knowing the line number is useful when the interpreter tell you an error occurred at
a particular line of code.
1: x = 5
2: y = 10 + x
3: z = x + y
4: print(z)
So, before the program even starts running, the interpreter has the following
information:
Output
20
20 (integer)
EECS1015 – York University -- Prof. Michael S. Brown 27
Thinking like the Python Interpreter (9/9)
Before the program started
1: x = 5
2: y = 10 + x List of variables Data/Objects stored in memory
3: z = x + y x 5 (integer)
4: print(z)
y
10 (integer)
• If you didn't watch it, please see the Lecture 1 on "How your computer
works". Step-by-step is how your computer works, even at the lowest
hardware level. As you learn to program, you need to learn to think about
solving problems and tasks in a step-by-step manner.
z=x+y
The entire line is considered a statement.
Statements generally do not return a value. x + y is an expression.
print( type(z) )
The entire line is considered a statement.
This line of code will call the function In this example, the call to the function type(z) is
type(z) first, the result of that function considered an expression, since the function returns
is passed to the function print( ). This a value.
will perform the action to print the result.
Remember: An expression is a combination
of values (literals), variables, operators, and
calls to functions.
Expressions need to be evaluated.
• When you first learn a programming language, you need to learn the
symbols it uses for math
(2) Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and
not 27. Can you explain why?
(3) Multiplication and both division operators have the same precedence, which is higher than
addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4,
and 5-2*2 is 1, not 6.
(4) Operators with the same precedence are evaluated from left-to-right. In algebra we say they are
left-associative. So in the expression 6-3+2, the subtraction happens first, yielding 3. We then add 2
to get the result 5. If the operations had been evaluated from right to left, the result would have been
6-(3+2), which is 1.
EECS1015 – York University -- Prof. Michael S. Brown See more details here: link 44
Evaluating an expression
• An expression can consist of multiple expressions, each evaluates to
a single value.
an expression values Evaluation of this expression. . .
40 / 10.0 * 5
Remember, you interpreter does everything step-by-step. (2)
It can only run one math operator at a time.
So an expression above will be broken into several
4.0 *5
small expression and performed one at a time, combining (3)
the results to get the final value. 20.0
EECS1015 – York University -- Prof. Michael S. Brown 45
Some more examples
Expression: 5 + 6 ** 2 - 10
5 + 6 ** 2 - 10 Exponent higher precedence than +/-
5 + 36 - 10 +- are equal, so evaluate left to right
41 - 10 last one
31 final result
Expression: (4 + 2) ** 2 / 3
(4 + 2) ** 2 / 3 Parenthesis has highest precedence
6 ** 2 / 3 Exponent has higher precendence than /
36 / 3 last one
12.0 final result – notice it is a float because of the divide
Expression: 10**(2+1)*2
10** (2+1) *2 Parenthesis has highest precedence
10**3*2 Exponent has higher precendence than /
1000 * 2 last one
a = a + b a += b
a = a - b a -= b
a = a * b a *= b
a = a / b shortcut syntax a /= b
a = a // b a //= b
a = a % b a %= b
10 (integer)
y
15 (integer)
Incorrect interpretation
List of variables Data/Objects stored in memory
x 5 (integer)
10 (integer)
y
15 (integer)
EECS1015 – York University -- Prof. Michael S. Brown 59
Reassigning variables (5/5)
Lines 5&6: The next two lines of code print the data
1: x = 5 bound to x and y. Note that this is the exact same data!
2: y = 10
Correct interpretation
3: x = x + y
4: y = x List of variables Data/Objects stored in memory
5: print(x) x
6: print(y) 5 (integer)
10 (integer)
y
15 (integer)
Note: After this code, the values of "5" and "10" are unbound. This means we no longer have a way
in our code to access the data 5 and 10 through variables.
line 1 – bind a to 5
a = 5 List of variables Data/Objects stored in memory
b = 10
a 5 (integer)
a = b b
b = a 10 (integer)
line 2 – bind b to 10
a = 5 List of variables Data/Objects stored in memory
b = 10
a 5 (integer)
a = b b
b = a 10 (integer)
"Hello World"
EECS1015 – York University -- Prof. Michael S. Brown 68
String vs other literals
• Even though strings can look like other literals, we need to keep in
mind they are different
• For example:
"1234" is the sequence of characters "1", "2", "3", 4"
It is not the number 1234.
HelloWorld
Hello World
"String1" + "String2"
"String1String2"
• When the "+" is used with Strings, it is not the math add operator, but
the concatentation.
step 1: Replace word1 with its value "EECS" + word2 + " is awesome"
step 2: Replace word2 with its value "EECS" + "1015" + " is awesome"
Line 3: Concat "Hello " and "World", create new string "Hello World" and bind it to x.
x = "Hello" List of variables Data/Objects stored in memory
x = x + " "
x "Hello" (string)
x = x + "World"
print(x) "World" (string)
" " (string)
"Hello " (string)
"Hello World" (string)
x = ""
Python knows that stuff between
"" is a string. In this case, there is
nothing there. That is fine, this is
still a valid string.
The result will be the string the The string here is optional. We call this
user types in. the "prompt".
x = "1234" x = "10.5"
y = int("1234") y = float("10.5")
print(x) print(x)
print(y) print(y)
print(type(x)) print(type(x))
print(type(y)) print(type(y))
1234 10.5
1234 10.5
<class 'str'> <class 'str'>
<class 'int'> <class 'float'>
EECS1015 – York University -- Prof. Michael S. Brown https://trinket.io/python/ea18acd09f 85
Input with string to numerical conversion
Python code
prompt = "Enter a number: " Think like the interpreter:
x = input(prompt) w = int(x) + int(y)
Let's examine this statement
y = input(prompt)
w = int(x) + int(y)
step 1: Replace x with its value w = int("10") + int(y)
print(w)
step 2: call int("10") and return integer 10 w = 10 + int(y)
https://trinket.io/python/ab318de516 Question: What are the values of x and y after the print(w).
Are they strings or integers?
y = int(" 20") OK . . result will be integer 20. Python will ignore the spaces.
u = int("1.2") Error – this will case a run-time error. 1.2 is not an integer.
y = float(" 20.0 ") OK . . result will be floate 20.0. Python will ignore the spaces.
https://trinket.io/python/4fc9488ca4
Try fixing the code yourself here.
EECS1015 – York University -- Prof. Michael S. Brown 95
Finally comments - comments
• You have noticed in my trinket code I put in comments.
• In Python, we can add comments to the code using the # symbol
• Anything after # will be considered a comment
• Comments are used by programmers to describe their code
• It is important to get into the habit of using comments
# Program – KM to Miles
# Author: You
# For: EECS1015
## You can use more than one #
kilometers = input("Enter a distance in km: ")
miles = float(kilometers) * 0.621371 # you can also comment here.
print("In miles:")
print(miles)
• You also learned to take the program and break it down step by step.
This allowed you to "think the interpreter"
• With this lecture you should be able to write your own simple
programs in Python