We discussed some basic types and operations, conversion functions, the function input and several structures that Python has to offer: if-else, while loops, and for loops. We also solved an excercise of counting the number of zeros in a number that is given as an input by the user.
Takeaways:
We saw a demonstration on how to use IDLE (needed for homework coding part). Don't forget to run the test() function that is already given in the skeleton file.
In order to open a .py file in Script mode, rightclick the file and select "Edit with IDLE".
Read the following instructions if you are interested in working with Jupyter notebooks.
Code for printing several outputs in one cell (not part of the recitation):
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
Types: int, float, string, bool
x = 3
y = 3.14
name = "michal"
b = True
print(type(x))
print(type(y))
print(type(name))
print(type(b))
type(x)
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
int
type("4")
str
10 / 3
3.3333333333333335
10 // 3
3
10 // 2.0
5.0
4 + 5.0
9.0
2 ** 0.5
1.4142135623730951
9 ** 0.5
3.0
"4" + "5"
'45'
"4" * 3
'444'
"4" * "5"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-ab0f866df984> in <module> ----> 1 "4" * "5" TypeError: can't multiply sequence by non-int of type 'str'
10 % 2
0
2 % 10
2
(-2) % 10
8
3 < 4
True
3 < 4 and 3 > 5
False
Task: find the rightmost digit in a given number, using the operators mentioned above:
x = 345
# rightmost digit
x % 10
# second rightmost digit
(x // 10) % 10
5
4
Operator precedence:
int(3.14)
3
int("3")
3
float("3")
3.0
float("3.14")
3.14
str(3)
'3'
str(3.14)
'3.14'
hungry = True
today = "Thursday"
tv_night = "Thursday" #"Sunday"
last_episode_seen = 5
if hungry:
print("Better find something to eat...")
if today == "Friday":
print("Go to a restaurant")
else:
print("Cook something")
elif today == tv_night:
print("Watch episode number", last_episode_seen + 1, "of your favorite show")
elif today == "Thursday":
print("Go out for drinks!")
elif today == "Monday" or today == "Wednesday":
print("Workout")
elif today == "Friday" or today == "Tuesday" or today == "Saturday":
print("Go to sleep early :(")
else:
print("That's not a real day")
Better find something to eat... Cook something
n = 100
if n % 2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("end of if structure")
hi bye end of if structure
n = 101
if n % 2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("end of if structure")
bye end of if structure
n = 100
if n % 2 == 0:
print("hi")
elif n > 2:
print("bye")
else:
print("something")
print("end of if structure")
hi end of if structure
s = input("Please enter an integer: ")
print(s, type(s))
Please enter an integer: 3 3 <class 'str'>
num = int(input("Please enter an integer: "))
print(num, type(num))
Please enter an integer: 3 3 <class 'int'>
num = int(input("Please enter an integer: "))
print(num, type(num))
Please enter an integer: 3.2
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-32-dedbc0dd1f23> in <module> ----> 1 num = int(input("Please enter an integer: ")) 2 print(num, type(num)) ValueError: invalid literal for int() with base 10: '3.2'
num = int(input("Please enter an integer: "))
print(num, type(num))
Please enter an integer: hi
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-dedbc0dd1f23> in <module> ----> 1 num = int(input("Please enter an integer: ")) 2 print(num, type(num)) ValueError: invalid literal for int() with base 10: 'hi'
i = 0
while i < 10:
print(i)
i = i + 1
print("out of loop, value of i is", i)
0 1 2 3 4 5 6 7 8 9 out of loop, value of i is 10
print("This program will tell you how many 0's are in an integer")
orig_num = int(input("Please enter an integer: "))
num = orig_num
cnt = 0
while num > 0:
if num % 10 == 0:
cnt = cnt + 1 #cnt += 1
num = num // 10 #num //= 10
print(orig_num, "has", cnt, "0-s")
This program will tell you how many 0's are in an integer Please enter an integer: 1203 1203 has 1 0-s
Are there any issues with the code above?
print("This program will tell you how many 0's are in an integer")
orig_num = int(input("Please enter an integer: "))
num = orig_num
cnt = 0
while num > 0:
if num % 10 == 0:
cnt = cnt + 1 #cnt += 1
num = num // 10 #num //= 10
print(orig_num, "has", cnt, "0-s")
This program will tell you how many 0's are in an integer Please enter an integer: 0 0 has 0 0-s
print("This program will tell you how many 0's are in an integer")
orig_num = int(input("Please enter an integer: "))
num = orig_num
cnt = 0
if num == 0:
cnt = 1
while num > 0:
if num%10 == 0:
cnt = cnt + 1 # cnt += 1
num = num // 10 #num //= 10
print (orig_num, "has", cnt , "0-s")
This program will tell you how many 0's are in an integer Please enter an integer: 0 0 has 1 0-s
What about negative numbers?
Supose that the user entered 1203. We will track the values of the variables using a table:
orig_num | num | cnt | Program output | |
Before the loop | 1203 | 1203 | 0 | |
After iteration # 1 | 1203 | 120 | 0 | |
After iteration # 2 | 1203 | 12 | 1 | |
After iteration # 3 | 1203 | 1 | 1 | |
After iteration # 4 | 1203 | 0 | 1 | |
After the loop | 1203 | 0 | 1 | "1203 has 1 0-s" |
for x in "michal":
print(x)
print(type(x))
print(x * 4)
m <class 'str'> mmmm i <class 'str'> iiii c <class 'str'> cccc h <class 'str'> hhhh a <class 'str'> aaaa l <class 'str'> llll
print("This program will tell you how many 0's are in an integer")
num_str = input("Please enter an integer: ")
cnt = 0
for digit in num_str:
if digit == "0":
cnt += 1
print (num_str, "has", cnt, "0-s")
This program will tell you how many 0's are in an integer Please enter an integer: 1203 1203 has 1 0-s
print("This program will tell you how many 0's are in an integer")
num_str = input("Please enter an integer: ")
cnt = 0
for digit in num_str:
if digit == "0":
cnt = cnt + 1
print(num_str, "has", cnt, "0-s")
This program will tell you how many 0's are in an integer Please enter an integer: 0 0 has 1 0-s
num_str = input("Please enter an integer: ")
cnt = str.count(num_str, "0")
#cnt = num_str.count("0")
print (num_str, "has", cnt, "0's")
Please enter an integer: 1203 1203 has 1 0's