text = "hello"Module I - Day 5
Python Made Easy: Science and Math Edition
Sep-Dec 2025 batch, Vikrant Patil
Date: 25 Oct 2025
© Vikrant Patil
Programming Constructs
- conditions
if "hel" in text: # after : we go into new code block which has to have indentation more deep.
print("Hello!")
print("Welcome")
elif "o" in text:
print("o")
elif "del" in "text":
print("Del")
else:
print("Finally")
# this statement is outside if statement!
print("statement is over")Hello!
Welcome
statement is over
if "hel" in text: # after : we go into new code block which has to have indentation more deep.
print("Hello!")
print("Welcome")
else:
print("Finally")Hello!
Welcome
if "hel" in text: # after : we go into new code block which has to have indentation more deep.
print("Hello!")
print("Welcome")Hello!
Welcome
loop
for loop in python is used with respect to collection. If we want to do some operation on every item from any collection then we use for loop.
- list
- text
- tuple
- dictionary
nums = [4, 5, 2, 1, 6, 8]for number in nums: # code block!
print(number) # it usually prints on new line4
5
2
1
6
8
for number in nums:
print(number*number)16
25
4
1
36
64
statement = "this is a statement fromw hich I will make list of words"words = statement.split()for word in words:
print(word)this
is
a
statement
fromw
hich
I
will
make
list
of
words
for word in words:
print(word, end=",")this,is,a,statement,fromw,hich,I,will,make,list,of,words,
for n in nums: #[4, 5, 2, 1, 6, 8]
print(n, end=",")4,5,2,1,6,8,
the variables n, number, word which we used in above for loops are called loop variables. It is defined on fly while the for loop gets executed. Its value is updated in each iteration.
problems
- Write a function
productwhich finds product of all elements from a list.
>>> product([3, 2, 4])
24
- Write a function
factorialto find factorial of a number.
>>> factorial(5)
120
- Write a function
findlenswhich finds lengths of every word from a given list of words.
>>> findlens(["one", "two", "three"])
[3, 3, 5]
for p in nums:
print(nums*len(nums))[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
nums[4, 5, 2, 1, 6, 8]
nums*2[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
nums*3[4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8, 4, 5, 2, 1, 6, 8]
for p in nums:
print(p**2)16
25
4
1
36
64
"text"*3'texttexttext'
[1, 2, 3]*4[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
nums[4, 5, 2, 1, 6, 8]
squares = [] # empty list
for n in nums:
s = n*n
squares.append(s)squares[16, 25, 4, 1, 36, 64]
def mysum(numbers): # mysum is a function which takes numbers as argument
s = 0
for n in numbers:
s = s + n # accumulation... adding next number to previous sum
return smysum(nums)26
mysum([1, 1, 1, 1, 1,1, 1, 1, 1, 1])10
for loop necessarily needs a collection to operate on! It can not work without collection. If I want to do some task n times, then I need to have a collection of size n to put a for loop which will get executed n times!
range(5)range(0, 5)
for i in range(5):
print(i)0
1
2
3
4
for i in range(8):
print(i)0
1
2
3
4
5
6
7
range(10)range(0, 10)
problem
- Write a function
sum_naturalswhich will find out sum of firstnnatural numbers
def sum_naturals(n):
return mysum(range(1, n+1)) # this will have numbers from 1 to nsum_naturals(10)55
sum_naturals(100)5050
def sum_naturals(n):
return sum(range(1, n+1))sum_naturals(50)1275
problems
- Write a function
productwhich finds product of all elements from a list.
>>> product([3, 2, 4])
24
- Write a function
factorialto find factorial of a number.
>>> factorial(5)
120
- Write a function
findlenswhich finds lengths of every word from a given list of words.
>>> findlens(["one", "two", "three"])
[3, 3, 5]
def product(nums):
p = 1
for n in nums:
p = p * n
return pproduct([3,2,4,2])48
# lets have a lopp how the function and for loop is geting executed step by step
product([3,2,4,2])
# arguments nums = [3,2,4,2]
p = 1
# first step in for loop
n = 3 # loop variable
p = p * n # p has become 3
# second step in for loop
n = 2 # loop variable
p = p * n # p will becomes 3 * 2
# third step in for loop
n = 4 # loop variable
p = p * n # p will becomes 3 * 2 * 4
# fourth step in for loop
n = 2
p = p * n # p will becomes 3 * 2 * 4 * 2
# for loop is over
# return pp48
product([3,2,4,2])48
bonus problem
- Write a function
reverselistwhich will create a new list which is in reversed order of original list.
reverselist([3, 5, 6, 72, 4])
[4, 72, 6, 5, 3]
help(nums.insert)Help on built-in function insert:
insert(index, object, /) method of builtins.list instance
Insert object before index.
problem
I want to find sqaures if even number from given list!
nums[4, 5, 2, 1, 6, 8]
newlist = []
for n in nums:
if iseven(n):
newlist.append(n*n)
else:
newlist.append(n)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[49], line 4 1 newlist = [] 3 for n in nums: ----> 4 if iseven(n): 5 newlist.append(n*n) 6 else: NameError: name 'iseven' is not defined
def iseven(x):
return x%2 == 0
newlist = []
for n in nums:
if iseven(n):
newlist.append(n*n)
else:
newlist.append(n)
newlist[16, 5, 4, 1, 36, 64]
Home work
problems
- Write a function
find_words_of_lento find words of given length from given list.
>>> find_words_of_len(words, 3)
['one', 'two', 'six']
- Write a function
uniquewhich will remove duplicates from a list.
>>> unique([1, 1, 2, 3, 1, 2, 3, 2, 4])
[1, 2, 3, 4]
- List of urls is given. Some urls are from same domain, some are from different. Find unique domain names used in the urls.
urls = ['www.abrakadabra.com/dccEcB/EGdd',
'www.abrakadabra.com/gADFeD/bcAF',
'www.abra.com/AGadbb/eagE',
'www.dabra.com/cffdfD/FCAD',
'www.abra.com/GFGaBE/dcfc',
'www.abra.com/gaFegG/Bdaf',
'www.abrakadabra.com/aGabaf/EEfa',
'www.dabra.com/ceEgFD/bGgc',
'www.dabra.com/bDEffC/bcEA']