template = """
Hi {user},
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: {username}
passwd: {passwd}
Thanks and Regards,
Authority
"""Module III - Day 2
Python Made Easy: Science and Math Edition
Sep-Dec 2025 batch, Vikrant Patil
Date: 8th Feb 2026
Live note are here https://vikrant.dev/python-made-easy-science-math/students-module3-day1.html
Please login to https://traininghub.vikrant.dev and create a notebook with name module3-day2.ipynb
© Vikrant Patil
String formating
print(template.format(user="Alex", username="alex234", passwd="%#$$90aLex"))
Hi Alex,
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: alex234
passwd: %#$$90aLex
Thanks and Regards,
Authority
%%file users.csv
Alex,alex23,%sdsdAlx,alex@xyz.com
Samiha,samiha24,$3iujhdi,samiha@xyz.com
Poorva,poorvaj23,56jhsdgyu,poorva@xyz.com
vikrant,vikrantp232,kljs89324,vikrant@xyz.comOverwriting users.csv
import csv
def userdetails(filename):
with open(filename) as f:
creader = csv.reader(f)
return [row for row in creader]
def send_email(email, contents):
print(f"sending email to {email}")
print(contents)
def create_email_contents(user, username, passwd):
return template.format(user=user, username=username, passwd=passwd)
def process_list_send_emails(filename):
users = userdetails(filename)
for name, username, passwd in users:
contents = create_email_contents(name, username, passwd)
userdetails("users.csv")[['Alex', 'alex23', '%sdsdAlx', 'alex@xyz.com'],
['Samiha', 'samiha24', '$3iujhdi', 'samiha@xyz.com'],
['Poorva', 'poorvaj23', '56jhsdgyu', 'poorva@xyz.com'],
['vikrant', 'vikrantp232', 'kljs89324', 'vikrant@xyz.com']]
ans = 42
f"Answer to question of life is {ans}"'Answer to question of life is 42'
s = "Answer to question of life is {ans}"s.format(ans=ans)'Answer to question of life is 42'
a, b = 10, 20f"{a} + {b} = {a+b}"'10 + 20 = 30'
name = "vikrant"f"{name} is my name!"'vikrant is my name!'
f"{name} is india's war ship""vikrant is india's war ship"
"{name} , how are you?"'{name} , how are you?'
import csv
def userdetails(filename):
with open(filename) as f:
creader = csv.reader(f)
return [row for row in creader]
def send_email(email, contents):
print(f"sending email to {email}")
print(contents)
def create_email_contents(user, username, passwd):
return template.format(user=user, username=username, passwd=passwd)
def process_list_send_emails(filename):
users = userdetails(filename)
for name, username, passwd, email in users:
contents = create_email_contents(name, username, passwd)
send_email(email, contents)
print("x"*30)
process_list_send_emails("users.csv")sending email to alex@xyz.com
Hi Alex,
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: alex23
passwd: %sdsdAlx
Thanks and Regards,
Authority
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sending email to samiha@xyz.com
Hi Samiha,
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: samiha24
passwd: $3iujhdi
Thanks and Regards,
Authority
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sending email to poorva@xyz.com
Hi Poorva,
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: poorvaj23
passwd: 56jhsdgyu
Thanks and Regards,
Authority
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sending email to vikrant@xyz.com
Hi vikrant,
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: vikrantp232
passwd: kljs89324
Thanks and Regards,
Authority
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
s 'Answer to question of life is {ans}'
s.format(ans=ans)'Answer to question of life is 42'
template.formatfunction arguments
def func(a, b):
return a + ba10
b20
func(30, 50)80
func(a=30, b=50)80
func(a=a, b=b) # it will make use of loca variable a and b30
template2 = """
Hi {name},
How are you doing? It is great pleasure to tell you that
your application has been shortlisted for the program.
Please send more details as given below.
...
..
You will get stipend of Rs 10000/- per month during the program
You will need user crendentials on the campus as given below
username: {user}
passwd: {password}
Thanks and Regards,
Authority
"""def create_email_contents(user, username, passwd):
return template2.format(name=user, user=username, password=passwd)name'vikrant'
name.center(100)' vikrant '
name.center(10)' vikrant '
name.center(20)' vikrant '
name.rjust(20)' vikrant'
name.ljust(20)'vikrant '
Riffle Shuffle
Remember the card game that we play. We will try to simulate riffle shuffle of a deck
A = ["A1", "A2", "A3", "A4"]
B = [f"B{i}" for i in range(1, 5)]
deck = A + Bdeck['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4']
deck[1:]['A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4']
n = len(deck)
mid = n//2 # ineteger
part1 = deck[:mid] # take first half
part2 = deck[mid:] # drop first half
s = []
for x, y in zip(part1, part2):
s.extend([x,y])
if len(part1) < len(part2):
s.extend([part[-1]])s['A1', 'B1', 'A2', 'B2', 'A3', 'B3', 'A4', 'B4']
part1['A1', 'A2', 'A3', 'A4']
part2['B1', 'B2', 'B3', 'B4']
def riffle_shuffle(deck):
n = len(deck)
mid = n//2 # ineteger
part1 = deck[:mid] # take first half
part2 = deck[mid:] # drop first half
s = []
for x, y in zip(part1, part2):
s.extend([x,y])
if len(part1) < len(part2):
s.extend([part2[-1]])
return sriffle_shuffle(deck)['A1', 'B1', 'A2', 'B2', 'A3', 'B3', 'A4', 'B4']
def sumofsquares(a, b):
return square(a) + square(b)
def square(x):
return x*x
def sumof(a, b, func):
return func(a) + func(b)sumofsquares(32, 42)2788
sumof(32, 42, square)2788
square(32)1024
def cube(x):
return x*x*xsumof(32, 42, cube)106856
f(x) = x**2 + 2*x + 1
f(f(f(f(f(4))))))
Here we are applying f on 4 five times!
def repeat(func, arg, n):
"""
applies function func to arg , n times
"""
for i in range(n):
arg = func(arg)
print(f"{i:2d}","".join(arg))
return argrepeat(riffle_shuffle, "hello world!", 15) 0 hweolrllod !
1 hlwleoodl r!
2 holdwll ero!
3 hlo ledrwol!
4 hdlrow olle!
5 h dollrloew!
6 hr ldooelwl!
7 hore llwdlo!
8 hlowrdel ol!
9 hello world!
10 hweolrllod !
11 hlwleoodl r!
12 holdwll ero!
13 hlo ledrwol!
14 hdlrow olle!
['h', 'd', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', '!']
deck['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4']
repeat(riffle_shuffle, deck, 10) 0 A1B1A2B2A3B3A4B4
1 A1A3B1B3A2A4B2B4
2 A1A2A3A4B1B2B3B4
3 A1B1A2B2A3B3A4B4
4 A1A3B1B3A2A4B2B4
5 A1A2A3A4B1B2B3B4
6 A1B1A2B2A3B3A4B4
7 A1A3B1B3A2A4B2B4
8 A1A2A3A4B1B2B3B4
9 A1B1A2B2A3B3A4B4
['A1', 'B1', 'A2', 'B2', 'A3', 'B3', 'A4', 'B4']
def repeat_count(func, arg):
newarg = arg[:]
count = 1
newarg = func(newarg)
while newarg != arg:
newarg = func(newarg)
count += 1
return count
repeat_count(riffle_shuffle, deck)3
repeat_count(riffle_shiffle, list("hello world!"))10
print("length", "shuffles")
for i in range(1, 53):
print("{i:6d} {s:8d}".format(i=i, s=repeat_count(riffle_shuffle, list(range(i)))))length shuffles
1 1
2 1
3 1
4 2
5 2
6 4
7 4
8 3
9 3
10 6
11 6
12 10
13 10
14 12
15 12
16 4
17 4
18 8
19 8
20 18
21 18
22 6
23 6
24 11
25 11
26 20
27 20
28 18
29 18
30 28
31 28
32 5
33 5
34 10
35 10
36 12
37 12
38 36
39 36
40 12
41 12
42 20
43 20
44 14
45 14
46 12
47 12
48 23
49 23
50 21
51 21
52 8