Module I - Day 4

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 22 Oct 2025

Click here for All Notes

© Vikrant Patil

Quick recap

  • basic datatype - integers, floats , str
  • collections - list, dictionary, tuple
  • str methods - split, join
  • list methods - append, pop, count, index
  • built in functions - print, len, sum, sorted, int, str
  • custom functions
"hello" + "python" # concatenation
'hellopython'
"hello"*5
'hellohellohellohellohello'
"*"*5
'*****'
print("*"*20)
********************
nums = [1, 7, 3, 5, 2, 8]
nums.index(7)
1
nums.index(8)
5
nums.count(1)
1
nums.append(1)
nums
[1, 7, 3, 5, 2, 8, 1]
nums.count(1)
2
def median(nums):
    sorted_nums = sorted(nums)
    c = (len(sorted_nums)-1)/2
    n = int(c) # this is integer
    return sorted_nums[n]
x = 4534546546
str(x)
'4534546546'
len(str(x))
10
def twice(x):
    return 2*x


def square(x):
    return x*x

def add(x, y):
    return x + y
square(twice(int("6")))

nested function call

int("6")
6
square(twice(6))
twice(6)
12
square(12)
144
square(twice(int("6")))
144
square(twice(len("hello")))
100

method chaining

There is a text statement. I want to find out index of a word “is” from list of words made from this statement

text = "Hello this is some statement"
text.split().index("is")
2
  text.split().index("is")
  ---->---->---->----->----->methods will get executed in this order

conditions

x 
4534546546
y = 343 # assignment operator
x == y # condition to check if both sides are equal
False
"hello" == "name"
False
"hello" == "hello"
True
nums
[1, 7, 3, 5, 2, 8, 1]
nums == [1,7, 3, 5, 2, 8, 1]
True
nums == [1, 2, 3]
False
x > y
True
x >= y
True
x < y
False
x <= y
False
x != y
True
x != x
False
statement = "lets make a statement which has some silly words in it"
"make" in statement
True
nums
[1, 7, 3, 5, 2, 8, 1]
7 in nums
True
"hello" in nums
False
7 in statement
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[49], line 1
----> 1 7 in statement

TypeError: 'in <string>' requires string as left operand, not int
"hello" not in statement
True
"make" not in statement
False
def say_hello(name, language):
    if language == "marathi": # code block
        print("नमस्कार", name)
    elif language == "english":
        print("hello", name)
    elif language == "tamil":
        print("வணக்கம்", name)
    else:
        print("Sorry", name, ", this language is not supported")
        
    
say_hello("vikrant", "marathi")
नमस्कार vikrant
say_hello("vikrant", "tamil")
வணக்கம் vikrant
say_hello("vikrant", "german")
Sorry vikrant , this language is not supported
def say_hello(name, language):
    if language == "marathi": # code block
        print("नमस्कार", name)
    elif language == "tamil":
        print("வணக்கம் wewe", name)
    elif language == "english":
        print("hello", name)
    elif language == "tamil":
        print("வணக்கம்", name)
    else:
        print("Sorry", name, ", this language is not supported")
        
say_hello("vikrant", "tamil")
வணக்கம் wewe vikrant
def say_hello1(name, language):
    if language == "marathi": # code block
        print("नमस्कार", name)      
    if language == "tamil":
        print("வணக்கம் wewe", name)
    if language == "english":
        print("hello", name)
    if language == "tamil":
        print("வணக்கம்", name)
    if language not in ["marathi", "english", "tamil"]:
        print("Sorry", name, ", this language is not supported")
        
say_hello1("vikrant", "tamil")
வணக்கம் wewe vikrant
வணக்கம் vikrant
say_hello("vikrant", "german")
Sorry vikrant , this language is not supported
say_hello("vikrant", "marathi")
नमस्कार vikrant
max(nums)
8

problem

  • Write a function max2 which takes two arguments x and y which are supposed to be numeric , it will return maximum number from x and y. Sample function call will look like this
>>> max2(5, 4)
5
>>> max2(4, 5)
5
x = 10
y = 30
def max2(x, y):
    if x > y:
        return x
    else:
        return y
max2(5, 6)
6
max2(7, 3)
7
def max3(x, y, z):
    a = max2(x, y)
    return max2(a, z)
max3(45, 67, 21)
67
def max3(x, y, z):
    return max2(max2(x, y), z)
def max3(x, y, z):
    if x > y and x > z:
        return x
    elif y > x and y > z:
        return y
    else:
        return z

loop

nums
[1, 7, 3, 5, 2, 8, 1]
for number in nums:
    print(number**2)
1
49
9
25
4
64
1
students = ["Alice", "Alex", "Ray", "Solo"]
for s in students:
    print("hello", s)
hello Alice
hello Alex
hello Ray
hello Solo
nums = [1, 7, 3, 5, 2, 8, 1]
sqrs = [] # empty list

for n in nums:
    s = n*n
    sqrs.append(s)
sqrs
[1, 49, 9, 25, 4, 64, 1]
students
['Alice', 'Alex', 'Ray', 'Solo']
caps_name = []
for student in students:
    caps_name.append(student.upper())
caps_name
['ALICE', 'ALEX', 'RAY', 'SOLO']
for student in students:
    print("hello")
hello
hello
hello
hello
for n in nums:
    print("hello")
hello
hello
hello
hello
hello
hello
hello
range(10)
range(0, 10)
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9
for i in range(10):
    print("hello")
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
for i in range(5):
    print("hello")
hello
hello
hello
hello
hello
for i in range(6):
    print("hello")
hello
hello
hello
hello
hello
hello

problem

Write a program which will print squares of first 10 natural numbers without creating a list

n = 1
for i in range(10):
    print(n*n)
    n = n + 1
1
4
9
16
25
36
49
64
81
100
for char in statement:
    print(char)
l
e
t
s
 
m
a
k
e
 
a
 
s
t
a
t
e
m
e
n
t
 
w
h
i
c
h
 
h
a
s
 
s
o
m
e
 
s
i
l
l
y
 
w
o
r
d
s
 
i
n
 
i
t
for char in statement:
    print(char, end=",")
l,e,t,s, ,m,a,k,e, ,a, ,s,t,a,t,e,m,e,n,t, ,w,h,i,c,h, ,h,a,s, ,s,o,m,e, ,s,i,l,l,y, ,w,o,r,d,s, ,i,n, ,i,t,
for n in nums:
    print(n, end=",")
1,7,3,5,2,8,1,
for n in nums:
    print(n, end=" ")
1 7 3 5 2 8 1 
stationarybox = ["pencil", "pen", "eraser", "geometry-box", "colors", "color-palet"]

def clean(item):
    print("Cleaned", item)

for item in stationarybox:
    clean(item)
Cleaned pencil
Cleaned pen
Cleaned eraser
Cleaned geometry-box
Cleaned colors
Cleaned color-palet