Unit 2: Expressions and Variables For Loops
Unit 2: Expressions and Variables For Loops
>>> 1 + 1
2
>>> 1 + 3 * 4 - 2
11
>>> 7 / 2
3
>>> 7.0 / 2
3.5
>>> 10 ** 6
1000000
2
Variables
• Declaring
no type is written; same syntax as assignment
• Operators
no ++ or -- operators (must manually adjust by 1)
Java Python
int x = 2; x = 2
x++; x = x + 1
System.out.println(x) print(x)
;
x = x * 8
x = x * 8; print(x)
System.out.println(x)
; d = 3.2
d = d / 2
double d = 3.2;
print(d)
d = d / 2;
System.out.println(d)
; 3
Types
• Python is looser about types than Java
Variables' types do not need to be declared
Variables can change types as a program is running
4
String Multiplication
• Python strings can be multiplied by an integer.
The result is many copies of the string concatenated together.
>>> "hello" * 3
"hellohellohello"
5
String Concatenation
• Integers and strings cannot be concatenated in Python.
Workarounds:
str(value) - converts a value into a string
print(expr, expr) - prints two items on the same
line
>>> x = 4
>>> print(”You should not count to " + x + ".")
TypeError: cannot concatenate 'str' and 'int' objects
6
The for Loop
for name in range(max):
statements
7
for Loop Variations
for name in range(min, max):
statements
for name in range(min, max, step):
statements
Can specify a minimum other than 0, and a step other than 1
8
Nested Loops
• Nested loops are often replaced by string * and +
Java
....1
...2 1 for (int line = 1; line <= 5; line++) {
2 for (int j = 1; j <= (5 - line); j++) {
..3 3 System.out.print(".");
.4 4 }
5 System.out.println(line);
5 6 }
Python
1 for line in range(1, 6):
2 print((5 - line) * "." + str(line))
9
Exercise
• Rewrite the Mirror lecture program in Python. Its output:
#================#
| <><> |
| <>....<> |
| <>........<> |
|<>............<>|
|<>............<>|
| <>........<> |
| <>....<> |
| <><> |
#================#
10
Exercise Solution
def bar():
print "#" + 16 * "=" + "#"
def top():
for line in range(1, 5):
# split a long line by ending it with \
print "|" + (-2 * line + 8) * " " + \
"<>" + (4 * line - 4) * "." + "<>" + \
(-2 * line + 8) * " " + "|"
def bottom():
for line in range(4, 0, -1):
print "|" + (-2 * line + 8) * " " + \
"<>" + (4 * line - 4) * "." + "<>" + \
(-2 * line + 8) * " " + "|"
# main
bar()
top()
bottom()
bar()
11
Concatenating Ranges
• Ranges can be concatenated with +
Can be used to loop over a disjoint range of numbers
12
Exercise Solution 2
def bar():
print "#" + 16 * "=" + "#"
def mirror():
for line in range(1, 5) + range(4, 0, -1):
print "|" + (-2 * line + 8) * " " + \
"<>" + (4 * line - 4) * "." + "<>" + \
(-2 * line + 8) * " " + "|"
# main
bar()
mirror()
bar()
13
Constants
• Python doesn't really have constants.
Instead, declare a "global" variable at the top of your code.
All methods will be able to use this value.
constant.py
1 MAX_VALUE = 3
2
3 def printTop():
4 for i in range(MAX_VALUE):
5 for j in range(i):
6 print(j)
7 print()
8
9 def printBottom():
10 for i in range(MAX_VALUE, 0, -1):
11 for j in range(i, 0, -1):
12 print(MAX_VALUE)
13 print()
14
Exercise Solution 3
SIZE = 4
def bar():
print "#" + 4 * SIZE * "=" + "#"
def mirror():
for line in range(1, SIZE + 1) + range(SIZE, 0, -1):
print "|" + (-2 * line + 2 * SIZE) * " " + \
"<>" + (4 * line - 4) * "." + "<>" + \
(-2 * line + 2 * SIZE) * " " + "|"
# main
bar()
mirror()
bar()
15