Week 4 Refresher

Let’s refresh your memory! First, read the reminders about syntax. Then, complete the exercises!

Reminders

Basic Variables and Operations

There are four basic variable types

1
2
3
4
x = 5     # int
x = 5.0   # float
x = "5"   # str
x = True  # bool

You can convert between types

1
2
3
4
y = "5"       # a string variable
x = int(y)    # convert to integer
x = float(y)  # convert to float
z = str(x)    # convert the float to string. does this equal "5" or "5.0"

There are 7 math operator types

1
2
3
4
5
6
7
8
9
x = 10
y = 2
x + y
x - y
x * y
x / y
x // y
x ** y
x % y

There are shortcuts for math operators. The following pairs of statements have the same result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
x = 5
y = 3
# x = x + y
x += y

x = 5
y = 3
# x = x - y
x -= y

x = 5
y = 3
# x = x * y
x *= y

x = 5
y = 3
# x = x / y
x /= y

x = 5
y = 3
# x = x // y
x //= y

x = 5
y = 3
# x = x ** y
x **= y

x = 5
y = 3
# x = x % y
x %= y

Booleans and Comparisons

There are boolean variables

1
2
x = True
x = False

Variables can be compared to create boolean variables

1
2
3
4
5
6
7
8
x = 42
y = 41
x == y
x != y
x > y
x >= y
x < y
x <= y

Boolean variables can be combined using the special boolean keywords

1
2
3
4
5
6
x = True
y = False
z1 = x and y
z2 = x or y
z3 = (x and y) or (not x and not y)
z4 = (not x and y) or (x and not y)

Code blocks let you group code. In python, they are created with 4 spaces. In pycharm and most python editors, hitting tab will just add 4 spaces. if statements use code blocks.

1
2
3
4
x = 10
y = 9
if x > y:
    print("X is bigger!")

if statements can be expanded using elif. elif will only be used if the first if is false. (hidden question: What needs to be put into the placeholder to make the above code work?)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
x = "3"
# placeholder
if x == 1:
    print("x is 1")
elif x == 2:
    print("x is 2")
elif x == 3:
    print("x is 3")
else:
    print("I'm not sure what x is")

Getting Input from Users

You can use the input to get information from the user. Try the following. What is missing?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
print("My Menu: ")
print("\t 1. Option 1")
print("\t 2. Option 2")
print("\t 3. Option 3")
x = input("What option do you choose?")
# placeholder
if x == 1:
    print("x is 1")
elif x == 2:
    print("x is 2")
elif x == 3:
    print("x is 3")
else:
    print("I'm not sure what x is")