nums = [2, 4, 5, 6, 2, 4, 6]
for index, item in enumerate(nums):
print(index, item)0 2
1 4
2 5
3 6
4 2
5 4
6 6
Sep-Dec 2025 batch, Vikrant Patil
Date: 06 Dec 2025
Live note are here https://vikrant.dev/python-made-easy-science-math/module2-day4.html
Please login to https://traininghub.vikrant.dev and create a notebook with name module2-day4.ipynb
© Vikrant Patil
0 2
1 4
2 5
3 6
4 2
5 4
6 6
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!
Alice Wondergirl
Alex Lion
Hans Solo
Alice Wondergirl
Alex Lion
Hans Solo
Alice Wondergirl
Alex Lion
Hans Solo
Theses are iterator - iterators are kind of pointer which keeps moving as you consume - So once consumed they will behave like empty data
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) Cell In[32], line 1 ----> 1 next(rnums) StopIteration:
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!
After for loop:
Overwriting test_exception.py
Writing stocks.csv
symbol
INFY
TATA
SIMPLES
MM
indexdata = [('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)][111.71436961893693,
141.21220022208635,
112.40571010053796,
137.54133351926248,
140.25154281801224]
[('IBM', 111.71436961893693),
('IBM', 141.21220022208635),
('IBM', 112.40571010053796),
('IBM', 137.54133351926248),
('IBM', 140.25154281801224)]
[('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224)]
[('IBM', 'Monday', 111.71436961893693),
('MICROSOFT', 'Monday', 235.0403622499107),
('APPLE', 'Monday', 321.49182055844256)]
[['INFY', '2000', '5.0'],
['TATA', '567', '3.5'],
['SIMPLES', '1534', '20.0'],
['MM', '234', '-5']]
['j',
'a',
's',
'g',
'h',
'd',
'j',
'h',
'g',
'd',
's',
'f',
'a',
'g',
'a',
's',
'd',
'g',
'h',
'f']
5432323
5436282
def filesize(root, f):
path = os.path.join(root, f)
if os.path.isfile(path):
return os.path.getsize(path)
def foldersize(root, files):
"get folder size in KB"
return sum(filesize(root, f) for f in files)
def get_size(folder):
"get folder size in KB"
return sum([foldersize(root, files) for root, dirs, files in os.walk(folder)])
get_size("_site")5436282
12119736
('.',
['promotions', '.quarto', '__pycache__', '_site', '.ipynb_checkpoints'],
['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',
'Untitled1.ipynb',
'index.qmd~',
'testargs.py',
'head.py',
'x.txt',
'a.pdf',
'_quarto.yml~',
'welcome_multi_lingual.py',
'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',
'module1-day4.ipynb',
'today.org',
'z.txt'])
problem - write function listpy (just like os.listdir!) which uses list comprehension to identify py files in given directory
['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',
'z.txt']
['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']
['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']
['zen.txt', 'copyzen.txt', 'x.txt', 'poem.txt', 'y.txt', 'z.txt']
[do(i) for i in collection(i) if cond(i)]
before loop make use of loop variable to do something and after loop make use of loop variable to put conditon
problem
prime numbers
factors which finds all factors of given number (include 1 and self)is_prime which checks if given number is prime based on fact that prime number has only two factors 1 and self.['aB', 'rA', 'kA', 'dA', 'bR']