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 (available here) 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".
x = 3
y = 3.14
name= "michal"
b = True
print(type(x))
print(type(y))
print(type(name))
print(type(b))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
type("4")
str
10/3
3.3333333333333335
10//3
3
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-4-c3d232229824> 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
x = 345
print (x%10)
print (x//10)
print (x%100)
print ((x//10)%10)
5 34 45 4
int(3.14)
3
int("3")
3
float("3")
3.0
float("3.14")
3.14
str(3)
'3'
str(3.14)
'3.14'
today = "Tuesday"
strike = "No"
my_recitation = "Tuesday"
if today == "Sunday":
print("Shvizut Yom Alef")
if strike == "Y":
print("Stay home")
else:
print("Lecture in intro to CS!")
elif today == "Wednesday":
print("Another lecture in intro to CS!")
elif today==my_recitation:
print("Go to recitation!")
elif today=="Monday" or today=="Tuesday" or today=="Friday" or today=="Saturday":
print("no intro to CS")
else:
print("Not a day")
no intro to CS
n = 100
if n%2 == 0:
print("hi")
elif n > 2:
print("bye")
else:
print("something")
print("out of if statement")
hi out of if statement
n = 100
if n%2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("out of if statement")
hi bye out of if statement
n = 101
if n%2 == 0:
print("hi")
if n > 2:
print("bye")
else:
print("something")
print("out of if statement")
bye out of if statement
s = input("Please enter an integer: ")
print(s, type(s))
Please enter an integer: 300 300 <class 'str'>
num = int(input("Please enter an integer: "))
print(num, type(num))
Please enter an integer: 300 300 <class 'int'>
num = int(input("Please enter an integer: "))
print(num, type(num))
Please enter an integer: 300txt
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-dedbc0dd1f23> in <module>() ----> 1 num = int(input("Please enter an integer: ")) 2 print(num, type(num)) ValueError: invalid literal for int() with base 10: '300txt'
a = 0
while a < 10:
print(a)
a+=1
print("value of a: " + str(a))
0 1 2 3 4 5 6 7 8 9 value of a: 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
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
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: 1001 1001 has 2 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 += 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: -10 -10 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: 1001 1001 has 2 0's