Module II - Day 3

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 07 Dec 2025

Click here for All Notes

Live note are here https://vikrant.dev/python-made-easy-science-math/students-module2-day3.html

Please login to https://traininghub.vikrant.dev and create a notebook with name module2-day3.ipynb

© Vikrant Patil

Iteration Patterns

nums = [1, 2, 3, 4, 5]

for n in nums: # we get item from the collection one by one
    print(n)
    
1
2
3
4
5
for i, n in enumerate(nums):
    print(i, n)
0 1
1 2
2 3
3 4
4 5
words = "This is some sentence which we will use to generate words".split()
words
['This',
 'is',
 'some',
 'sentence',
 'which',
 'we',
 'will',
 'use',
 'to',
 'generate',
 'words']
for i, word in enumerate(words):
    print(i, word)
0 This
1 is
2 some
3 sentence
4 which
5 we
6 will
7 use
8 to
9 generate
10 words
with open("zen.txt") as f:
    for linenum, line in enumerate(f):
        print(linenum, line, end="")
0 The Zen of Python, by Tim Peters
1 
2 Beautiful is better than ugly.
3 Explicit is better than implicit.
4 Simple is better than complex.
5 Complex is better than complicated.
6 Flat is better than nested.
7 Sparse is better than dense.
8 Readability counts.
9 Special cases aren't special enough to break the rules.
10 Although practicality beats purity.
11 Errors should never pass silently.
12 Unless explicitly silenced.
13 In the face of ambiguity, refuse the temptation to guess.
14 There should be one-- and preferably only one --obvious way to do it.
15 Although that way may not be obvious at first unless you're Dutch.
16 Now is better than never.
17 Although never is often better than *right* now.
18 If the implementation is hard to explain, it's a bad idea.
19 If the implementation is easy to explain, it may be a good idea.
20 Namespaces are one honking great idea -- let's do more of those!
firstname = ["Alice", "Shaharukh", "Madhuri", "Alex"]
lastname = ["Wondegirl", "Khan", "Dixit", "Lion"]
for i in range(len(firstname)):
    print(firstname[i], lastname[i])
Alice Wondegirl
Shaharukh Khan
Madhuri Dixit
Alex Lion
for first, last in zip(firstname, lastname):
    print(first, last)
Alice Wondegirl
Shaharukh Khan
Madhuri Dixit
Alex Lion
x = [1, 2, 3, 4]
y = [-1,-2, -3 , -4]
z = [11, 22, 33, 44]
for X,Y,Z in zip(x, y, z):
    print(X, Y, Z)
1 -1 11
2 -2 22
3 -3 33
4 -4 44
for a, b in zip(["A", "B", "C"], ["X","Y"]):
    print(a, b)
A X
B Y
zip(x, y, z)
<zip at 0x7f77e4e53500>
list(zip(x, y))
[(1, -1), (2, -2), (3, -3), (4, -4)]
with open("zen.txt") as f:
    for linenum, line in enumerate(f, start=1):
        print(linenum, line, end="")
1 The Zen of Python, by Tim Peters
2 
3 Beautiful is better than ugly.
4 Explicit is better than implicit.
5 Simple is better than complex.
6 Complex is better than complicated.
7 Flat is better than nested.
8 Sparse is better than dense.
9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
primes = [2, 3, 5, 7, 11, 13, 17, 19]
for p in reversed(primes):
    print(p)
19
17
13
11
7
5
3
2
for w in reversed(words):
    print(w)
words
generate
to
use
will
we
which
sentence
some
is
This

Iterators

rprimes = reversed(primes)
rprimes
<list_reverseiterator at 0x7f77e4f1df30>
next(rprimes)
19
next(rprimes)
17
next(rprimes)
13
next(rprimes)
11
next(rprimes)
7
next(rprimes)
5
next(rprimes)
3
next(rprimes)
2
next(rprimes)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Cell In[30], line 1
----> 1 next(rprimes)

StopIteration: 
for p in rprimes: # this will result in to empty loop
    print(p)
rprimes = reversed(primes)
for p in rprimes:
    print(p)
19
17
13
11
7
5
3
2
for p in rprimes:
    print(p)
zip(x, y)
<zip at 0x7f77e4312680>
iter(primes)
<list_iterator at 0x7f77e4be19c0>
with open("zen.txt") as f:
    print(next(f), end="")
    print("XX"*10)
    for line in f:
        print(line, end="")
    print("After for loop:", f.readline())
The Zen of Python, by Tim Peters
XXXXXXXXXXXXXXXXXXXX

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
After for loop: 
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
%%file zen.txt
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Overwriting zen.txt

problem - Write a function vector_add, which does vector addition of two vectors taken as lists - Write a function dot_product , which does dot product of two vectors given as list!

List comprehension

def squares(nums):
    sqrs = []
    for n in nums:
        sqrs.append(n*n)
    return sqrs
    
[ p*p for p in primes]
[4, 9, 25, 49, 121, 169, 289, 361]
[p**3 for p in primes]
[8, 27, 125, 343, 1331, 2197, 4913, 6859]
words
['This',
 'is',
 'some',
 'sentence',
 'which',
 'we',
 'will',
 'use',
 'to',
 'generate',
 'words']
[ w.upper() for w in words]
['THIS',
 'IS',
 'SOME',
 'SENTENCE',
 'WHICH',
 'WE',
 'WILL',
 'USE',
 'TO',
 'GENERATE',
 'WORDS']

[do(item) for item in collection]

def poly(x):
    return x**2 + x +1 
primes
[2, 3, 5, 7, 11, 13, 17, 19]
[poly(p) for p in primes]
[7, 13, 31, 57, 133, 183, 307, 381]
[p for p in primes]
[2, 3, 5, 7, 11, 13, 17, 19]
words
['This',
 'is',
 'some',
 'sentence',
 'which',
 'we',
 'will',
 'use',
 'to',
 'generate',
 'words']
[len(w) for w in words]
[4, 2, 4, 8, 5, 2, 4, 3, 2, 8, 5]
expected_words = []
for w in words:
    if len(w) == 2:
        expected_words.append(w)
expected_words
['is', 'we', 'to']
[w for w in words if len(w)==2]
['is', 'we', 'to']
primes
[2, 3, 5, 7, 11, 13, 17, 19]
[i for i in range(1, 21) if i not in primes]
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20]
import os
os.listdir()
['tail.py',
 'b.pdf',
 'students-module1-day4.ipynb',
 'about.qmd~',
 'index.qmd',
 'n.html',
 'say_hello.py',
 'l.html',
 'module2-day4.ipynb',
 'topics.qmd~',
 'maths_test_new.py',
 'm.html',
 'zen.txt',
 'notes.qmd',
 '_quarto.yml',
 'chaos.py',
 'students-module2-day2.ipynb',
 'notes.qmd~',
 'push',
 'c.pdf',
 'styles.css',
 'hello.py',
 'module1-day5.ipynb',
 'square.py',
 'trainer.qmd',
 'copyzen.txt',
 'maths_test.py',
 'test_prog.py',
 'power.py',
 'promotions',
 '.quarto',
 'Untitled1.ipynb',
 'index.qmd~',
 '__pycache__',
 'testargs.py',
 'head.py',
 'x.txt',
 'a.pdf',
 '_quarto.yml~',
 'welcome_multi_lingual.py',
 '_site',
 'Untitled.ipynb',
 '.gitignore',
 'module2-practice.ipynb',
 'students-module1-day2.ipynb',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'problems.ipynb',
 'topics.qmd',
 'module2-day3.ipynb',
 'args1.py',
 'cat.py',
 'module2-day1.ipynb',
 'trainer.qmd~',
 'test_args.py',
 'module1-day3.ipynb',
 'chaos1.py',
 'hello_person.py',
 'revision.ipynb',
 'Makefile~',
 'helloworld.py',
 'test_exception.py',
 'poem.txt',
 'stocks.csv',
 'y.txt',
 'students-module1-day3.ipynb',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day4.ipynb',
 'today.org',
 'students-module2-day3.ipynb',
 'z.txt']
[f for f in os.listdir() if f.endswith(".ipynb")]
['students-module1-day4.ipynb',
 'module2-day4.ipynb',
 'students-module2-day2.ipynb',
 'module1-day5.ipynb',
 'Untitled1.ipynb',
 'Untitled.ipynb',
 'module2-practice.ipynb',
 'students-module1-day2.ipynb',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'problems.ipynb',
 'module2-day3.ipynb',
 'module2-day1.ipynb',
 'module1-day3.ipynb',
 'revision.ipynb',
 'students-module1-day3.ipynb',
 'module1-day4.ipynb',
 'students-module2-day3.ipynb']
[f for f in os.listdir() if f.endswith(".py")]
['tail.py',
 'say_hello.py',
 'maths_test_new.py',
 'chaos.py',
 'hello.py',
 'square.py',
 'maths_test.py',
 'test_prog.py',
 'power.py',
 'testargs.py',
 'head.py',
 'welcome_multi_lingual.py',
 'args1.py',
 'cat.py',
 'test_args.py',
 'chaos1.py',
 'hello_person.py',
 'helloworld.py',
 'test_exception.py']
[do(item) for item in collection if cond(item)]
filename = "hello.py"
filename.endswith(".py")
True