You are on page 1of 14

01_if_elif_else (3)

March 4, 2024

if case1: perform action1 elif case2: perfrom action2 else: perform action3

[8]: if 1==1:
print('Hey it was true')

Hey it was true

[9]: x = 1 > 2

if x:
print('x was True')
else:
print('i am being printed because x was not true')

i am being printed because x was not true

[12]: location = 'Park'

if location == 'Auto shop':


print('Welcome to autoshop')
elif location == 'Bank':
print('Welcome to the bank')
elif location == 'Park':
print('Hey you are in Park')
else:
print('Where are you?')

Hey you are in Park

Write a program that prints the integers from 1 to 100.

But for multiples of 3, print ‘Hello Shreyas’

for Multiples of 5 print ‘Will you be teaching Deep Learning as well?’.

For multiples of both 3 and 5 print ‘6th Feb’


[2]: for x in range(1,101):
if x%3 == 0 and x%5==0:
print('6th Feb')

1
elif x%5 == 0:
print('Will you be teaching Deep Learning as well?')
elif x%3==0:
print('Hello Shreyas')
else:
print(x)

1
2
Hello Shreyas
4
Will you be teaching Deep Learning as well?
Hello Shreyas
7
8
Hello Shreyas
Will you be teaching Deep Learning as well?
11
Hello Shreyas
13
14
6th Feb
16
17
Hello Shreyas
19
Will you be teaching Deep Learning as well?
Hello Shreyas
22
23
Hello Shreyas
Will you be teaching Deep Learning as well?
26
Hello Shreyas
28
29
6th Feb
31
32
Hello Shreyas
34
Will you be teaching Deep Learning as well?
Hello Shreyas
37
38
Hello Shreyas
Will you be teaching Deep Learning as well?
41

2
Hello Shreyas
43
44
6th Feb
46
47
Hello Shreyas
49
Will you be teaching Deep Learning as well?
Hello Shreyas
52
53
Hello Shreyas
Will you be teaching Deep Learning as well?
56
Hello Shreyas
58
59
6th Feb
61
62
Hello Shreyas
64
Will you be teaching Deep Learning as well?
Hello Shreyas
67
68
Hello Shreyas
Will you be teaching Deep Learning as well?
71
Hello Shreyas
73
74
6th Feb
76
77
Hello Shreyas
79
Will you be teaching Deep Learning as well?
Hello Shreyas
82
83
Hello Shreyas
Will you be teaching Deep Learning as well?
86
Hello Shreyas
88
89

3
6th Feb
91
92
Hello Shreyas
94
Will you be teaching Deep Learning as well?
Hello Shreyas
97
98
Hello Shreyas
Will you be teaching Deep Learning as well?

[ ]:

4
02_For_Loops (1)

March 4, 2024

[1]: #for item in object:


# statements to do stuff

[3]: #example1
lst1 = [1,2,3,4,5,6,7,8,9,10]
for x in lst1:
print(x**2)

1
4
9
16
25
36
49
64
81
100

[5]: #example2
for x in lst1:
if x%2 == 0:
print(x)
else: print('Odd number')

Odd number
2
Odd number
4
Odd number
6
Odd number
8
Odd number
10

[7]: lst1

1
[7]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[10]: #example3
sum1 = 0
for num in lst1:
sum1 = sum1+num

print(sum1)

55

[11]: #example4
tup = (1,2,3,4,5)
for t in tup:
print(t**3)

1
8
27
64
125

[12]: #example5
lst2 = [(2,4),(6,8),(4,5)]
for tup in lst2:
print(tup)

(2, 4)
(6, 8)
(4, 5)

[14]: #Tuple Unpacking


for (t1,t2) in lst2:
print(t1+t2)

6
14
9

[16]: #example6

d = {'k1':1,'k2':2,'k3':3}

for item in d:
print(item)

k1
k2
k3

2
[17]: d.items()

[17]: dict_items([('k1', 1), ('k2', 2), ('k3', 3)])

[18]: #dictionary unpacking

for k,v in d.items():


print(k)
print(v)

k1
1
k2
2
k3
3

[19]: st = 'Print every word in this sentence that has an even number of letters'
for word in st.split():
if len(word)%2 == 0:
print(word)

word
in
this
sentence
that
an
even
number
of

[ ]:

3
03_while_loop (1)

March 4, 2024

[3]: x = 0
while x < 10:
print('x is currently:',x)
print('x is still less than 10, so adding 1 to x')
x = x+ 1

x is currently: 0
2
x is still less than 10, so adding 1 to x
x is currently: 1
3
x is still less than 10, so adding 1 to x
x is currently: 2
4
x is still less than 10, so adding 1 to x
x is currently: 3
5
x is still less than 10, so adding 1 to x
x is currently: 4
6
x is still less than 10, so adding 1 to x
x is currently: 5
7
x is still less than 10, so adding 1 to x
x is currently: 6
8
x is still less than 10, so adding 1 to x
x is currently: 7
9
x is still less than 10, so adding 1 to x
x is currently: 8
10
x is still less than 10, so adding 1 to x
x is currently: 9
11
x is still less than 10, so adding 1 to x

1
[4]: x = 0
while x < 10:
print('x is currently:',x)
print('x is still less than 10, so adding 1 to x')
x = x+ 1
else:
print('x is now equal to 10')

x is currently: 0
x is still less than 10, so adding 1 to x
x is currently: 1
x is still less than 10, so adding 1 to x
x is currently: 2
x is still less than 10, so adding 1 to x
x is currently: 3
x is still less than 10, so adding 1 to x
x is currently: 4
x is still less than 10, so adding 1 to x
x is currently: 5
x is still less than 10, so adding 1 to x
x is currently: 6
x is still less than 10, so adding 1 to x
x is currently: 7
x is still less than 10, so adding 1 to x
x is currently: 8
x is still less than 10, so adding 1 to x
x is currently: 9
x is still less than 10, so adding 1 to x
x is now equal to 10

0.0.1 break, continue, pass


• break: breaks out of the current closest enclosing loop
• continue: goes to the top of closest enclosing loop
• pass: does nothing at all

[7]: x = 0
while x < 10:
print('x is currently:',x)
print('x is still less than 10, so adding 1 to x')
x = x+ 1
if x == 3:
print('x is equal to 3')
else:
print('continuing')
continue

x is currently: 0
x is still less than 10, so adding 1 to x

2
continuing
x is currently: 1
x is still less than 10, so adding 1 to x
continuing
x is currently: 2
x is still less than 10, so adding 1 to x
x is equal to 3
x is currently: 3
x is still less than 10, so adding 1 to x
continuing
x is currently: 4
x is still less than 10, so adding 1 to x
continuing
x is currently: 5
x is still less than 10, so adding 1 to x
continuing
x is currently: 6
x is still less than 10, so adding 1 to x
continuing
x is currently: 7
x is still less than 10, so adding 1 to x
continuing
x is currently: 8
x is still less than 10, so adding 1 to x
continuing
x is currently: 9
x is still less than 10, so adding 1 to x
continuing

[6]: x = 0
while x < 10:
print('x is currently:',x)
print('x is still less than 10, so adding 1 to x')
x = x+ 1
if x == 3:
print('breaking at x is equal to 3')
break
else:
print('continuing')
continue

x is currently: 0
x is still less than 10, so adding 1 to x
continuing
x is currently: 1
x is still less than 10, so adding 1 to x
continuing
x is currently: 2

3
x is still less than 10, so adding 1 to x
breaking at x is equal to 3

[11]: for i in range(1,11):


if i == 6:
print('this is number 6')
continue
else:
print(i)

1
2
3
4
5
this is number 6
7
8
9
10

[16]: nested_lst = [[1,2,3],[4,5,6],[7,8,9]]

for i in nested_lst:
for j in i:
if j == 5:
continue
print(j)

1
2
3
4
6
7
8
9

[15]: num = 0
while num < 10:
num = num + 1
if (num%2) == 0:
continue
print(num)

1
3
5
7

4
9

[ ]:

5
04_List_Comprehensions (1)

March 4, 2024

[4]: st = 'hello'

lst1 = []
for x in st:
lst1.append(x)

[5]: lst1

[5]: ['h', 'e', 'l', 'l', 'o']

[6]: [x for x in 'word']

[6]: ['w', 'o', 'r', 'd']

[8]: #square numbers in range and turn it into list


lst5 = [x**2 for x in range(0,11)]

[10]: lst5

[10]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[11]: celsius = [0,10,20,43.9]

Fahren = [((9/5*temp)+32) for temp in celsius]


Fahren

[11]: [32.0, 50.0, 68.0, 111.02]

[12]: [x for x in range(0,11) if x%2!=0]

[12]: [1, 3, 5, 7, 9]

[13]: st = 'Print every word in this sentence that has an even number of letters'

[x[-1] for x in st.split()]

[13]: ['t', 'y', 'd', 'n', 's', 'e', 't', 's', 'n', 'n', 'r', 'f', 's']

1
[ ]:

You might also like