We have seen 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 had also solved an excercise of counting the number of zeros in a number inputted by the user.
x = 3
y = 3.14
c = "intro2CS"
b = True
type(x), type(y), type(c), type(b)
(int, float, str, bool)
x + y
6.140000000000001
c + "amir"
'intro2CSamir'
"4" + "5"3 < 4 and 3==4
'45'
c * "amir"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-30882b0d6373> in <module>() ----> 1 c * "amir" TypeError: can't multiply sequence by non-int of type 'str'
c * 2
'intro2CSintro2CS'
c * 2.5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-7cc6ba3a227d> in <module>() ----> 1 c * 2.5 TypeError: can't multiply sequence by non-int of type 'float'
c * 2.0
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-95d5d3faabfd> in <module>() ----> 1 c * 2.0 TypeError: can't multiply sequence by non-int of type 'float'
2 + 2.0
4.0
2**0.5
1.4142135623730951
2**2.0
4.0
2.0**2
4.0
10%2
0
109%10
9
9//2 # rounding down
4
3 < 4
True
3 == 4
False
3 != 4
True
True and False
False
3 < 4 and 3==4
False
-2%10
8
type(3 < 4)
bool
int("4")
4
int(4.0)
4
int("4.0")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-27-0b5266654709> in <module>() ----> 1 int("4.0") ValueError: invalid literal for int() with base 10: '4.0'
int(4.99) # int() always rounds down
4
float(4)
4.0
float("3.14")
3.14
float("3")
3.0
str(43)
'43'
str(True)
'True'
course = "intro" + str(2) + "cs"
course
'intro2cs'
today = "Monday"
strike = "No"
my_recitation = "Monday"
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")
Go to recitation!
Two conditional structures:
n=100
if n > 90 and n%2 == 0:
print("hi")
if n > 99:
print("bye")
else:
print("something")
hi bye
Now there is only one conditional structure:
n=100
if n > 90 and n%2 == 0:
print("hi")
elif n > 99:
print("bye")
else:
print("something")
hi
x = input(" please input an integer: ")
x
please input an integer: 3445
'3445'
x = input(" please input an integer: ")
int(x)
please input an integer: 3445
3445
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
num = int(input("please input an integer: "))
m = num
cnt = 0
while num > 0: # if we change to >= we get an infinite loop
if num % 10 == 0:
cnt += 1
num = num // 10
print(m, cnt)
please input an integer: 10003 10003 3
for c in "amir":
print(c)
a m i r
num = input("please input an integer: ")
cnt = 0
for digit in num:
if digit == "0":
cnt += 1
print(num, cnt)
please input an integer: 10003 10003 3
num = input("please input an integer: ")
cnt = str.count(num, "0")
print(num, cnt)
please input an integer: 10003 10003 3
def amir(a):
return a+1 # as opposed to print which will not return the value
x = amir(10)
x * 2
22