words = "one two three four five six seven eight nine ten".split()Practice II
login to https://traininghub.vikrant.dev and create a notebook of name students-practice2.ipynb
iteration patterns
words['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
for w in words:
print(w.upper())ONE
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
for i,w in enumerate(words):
print(i, w)0 one
1 two
2 three
3 four
4 five
5 six
6 seven
7 eight
8 nine
9 ten
import thisThe 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!
poem = """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!"""poem"The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!"
for line in poem.split("\n"):
print(line)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!
for i, line in enumerate(poem.split("\n")):
print(i, line)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!
test--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 1 ----> 1 test NameError: name 'test' is not defined
"test"'test'
"""one
two
three"""'one\ntwo\nthree'
"test"'test'
'test''test'
print(p)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[16], line 1 ----> 1 print(p) NameError: name 'p' is not defined
variable and literal
what ever you type after # will be ignored by python
5 # this is litearal integer with value 55
"test" # this is literal text with value "test"'test'
"x" # litearal text with char "x"'x'
x # this is a variable with name x--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[20], line 1 ----> 1 x # this is a variable with name x NameError: name 'x' is not defined
"poem" # this is litearal text 'poem'
poem # it is a variable"The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!"
lines = poem.split("\n")
for i, line in enumerate(lines):
print(i, line)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!
lines = poem.split("\n")
for i, p in enumerate(lines):
print(i, p)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!
lines = poem.split("\n")
for i, sentence in enumerate(lines):
print(i, sentence)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!
lines = poem.split("\n")
for linenum, sentence in enumerate(lines):
print(linenum, sentence)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", "Alex", "Albert"]
lastname = ["Wondergirl", "Lion", "Elis"]for first, last in zip(firstname, lastname):
print(first, last)Alice Wondergirl
Alex Lion
Albert Elis
nums = [1, 2, 3, 4, 5, 6, 7, 8]nums = [1, 2, 3, 4, 5, 6, 7, 8]
lines = poem.split("\n")
for i, line in zip(nums, lines):
print(i, line)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.
lines = poem.split("\n")
for i, sentence in enumerate(lines, start=1):
print(i, sentence)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!
vars().keys()dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__builtin__', '__builtins__', '_ih', '_oh', '_dh', 'In', 'Out', 'get_ipython', 'exit', 'quit', 'open', '_', '__', '___', '__session__', '_i', '_ii', '_iii', '_i1', 'words', '_i2', '_2', '_i3', 'w', '_i4', 'i', '_i5', 'this', '_i6', 'poem', '_i7', '_7', '_i8', 'line', '_i9', '_i10', '_i11', '_i12', '_12', '_i13', '_13', '_i14', '_14', '_i15', '_15', '_i16', '_i17', '_17', '_i18', '_18', '_i19', '_19', '_i20', '_i21', '_21', '_i22', '_22', '_i23', 'lines', '_i24', 'p', '_i25', 'sentence', '_i26', 'linenum', '_i27', 'firstname', 'lastname', '_i28', 'first', 'last', '_i29', 'nums', '_i30', '_i31', '_i32', '_i33', '_33', '_i34'])
lines['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!"]
vars()['lines']['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!"]
def add(x , y):
return x + yadd(4 , 5)9
for i in nums:
print(i)1
2
3
4
5
6
7
8
for i in reversed(nums):
print(i)8
7
6
5
4
3
2
1
for i in reversed(reversed(nums)):
print(i)--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[43], line 1 ----> 1 for i in reversed(reversed(nums)): 2 print(i) TypeError: 'list_reverseiterator' object is not reversible
for line in reversed(lines):
print(line)Namespaces are one honking great idea -- let's do more of those!
If the implementation is easy to explain, it may be a good idea.
If the implementation is hard to explain, it's a bad idea.
Although never is often better than *right* now.
Now is better than never.
Although that way may not be obvious at first unless you're Dutch.
There should be one-- and preferably only one --obvious way to do it.
In the face of ambiguity, refuse the temptation to guess.
Unless explicitly silenced.
Errors should never pass silently.
Although practicality beats purity.
Special cases aren't special enough to break the rules.
Readability counts.
Sparse is better than dense.
Flat is better than nested.
Complex is better than complicated.
Simple is better than complex.
Explicit is better than implicit.
Beautiful is better than ugly.
The Zen of Python, by Tim Peters
modules
len(lines)21
len(poem)856
sum(nums)36
import mathmath.sin(0)0.0
math.exp(5) 148.4131591025766
math.pi3.141592653589793
math.exp(1)2.718281828459045
import randomrandom.random() # geenrates a random between 0 to 10.4808872377346419
random.choice(lines)'Flat is better than nested.'
random.choice(lines)'Sparse is better than dense.'
random.choice(lines)'Flat is better than nested.'
def greet_randomly(name):
greeting = ["Hello", "Namskar", "Good day", "Good night", "How do you do"]
g = random.choice(greeting)
print(g, name)greet_randomly("vikrant")Good day vikrant
greet_randomly("vikrant")Good day vikrant
greet_randomly("vikrant")Good night vikrant
greet_randomly("vikrant")Good day vikrant
greet_randomly("vikrant")Namskar vikrant
greet_randomly("vikrant")Good day vikrant
greet_randomly("vikrant")Hello vikrant
import random as ranran.choice(lines)'Simple is better than complex.'
import mathWriting our own Module
%%file hello.txt
hello
how are you
this is hello text!Writing hello.txt
%%file mymodule.py
def greet_randomly(name):
greeting = ["Hello", "Namskar", "Good day", "Good night", "How do you do"]
g = random.choice(greeting)
print(g, name)
def add(x, y):
return x + y
def square(x):
return x*x
def unique(items):
u = []
for item in items:
if item not in u:
u.append(item)
return u
Overwriting mymodule.py
import mymodulemymodule.greet_randomly("python")--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[75], line 1 ----> 1 mymodule.greet_randomly("python") File ~/programming/work/github/vikrant.dev/python-made-easy-science-math/mymodule.py:4, in greet_randomly(name) 2 def greet_randomly(name): 3 greeting = ["Hello", "Namskar", "Good day", "Good night", "How do you do"] ----> 4 g = random.choice(greeting) 5 print(g, name) NameError: name 'random' is not defined
%%file mymodule1.py
import random
def greet_randomly(name):
greeting = ["Hello", "Namskar", "Good day", "Good night", "How do you do"]
g = random.choice(greeting)
print(g, name)
def add(x, y):
return x + y
def square(x):
return x*x
def unique(items):
u = []
for item in items:
if item not in u:
u.append(item)
return u
Writing mymodule1.py
import mymodule1mymodule1.greet_randomly("python")Good day python
mymodule1.unique([1, 1, 1, 1, 2, 2, 3, 3, 21, 1])[1, 2, 3, 21]
nums = [11, 11, 23, 11, 2, 1, 2, 1, 2, 14, 5,]mymodule1.unique(nums)[11, 23, 2, 1, 14, 5]
list slicing
nums[0]11
nums[4]2
lines['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!"]
lines[0]'The Zen of Python, by Tim Peters'
lines[-1]"Namespaces are one honking great idea -- let's do more of those!"
nums[11, 11, 23, 11, 2, 1, 2, 1, 2, 14, 5]
nums[0]11
nums[1:5] # items from location 1 to 4[11, 23, 11, 2]
nums[3:8] # items starting from index 3 to index 7[11, 2, 1, 2, 1]
nums[:5] # start will be taken as default ...at zero[11, 11, 23, 11, 2]
nums[:3] # take first three[11, 11, 23]
nums[:4] # take first four[11, 11, 23, 11]
nums[:5] # takre first five[11, 11, 23, 11, 2]
nums[5:] # drop first five [1, 2, 1, 2, 14, 5]
nums[2:] # drop first two[23, 11, 2, 1, 2, 1, 2, 14, 5]
nums[11, 11, 23, 11, 2, 1, 2, 1, 2, 14, 5]
nums[:6][11, 11, 23, 11, 2, 1]
nums[3:][11, 2, 1, 2, 1, 2, 14, 5]
nums[:] # copy[11, 11, 23, 11, 2, 1, 2, 1, 2, 14, 5]
ones = [1, 1, 1, 1]anotherones = ones # this is not copy ... this is just another nameanotherones[1, 1, 1, 1]
ones.append(0)ones[1, 1, 1, 1, 0]
anotherones[1, 1, 1, 1, 0]
ones[1, 1, 1, 1, 0]
ones.pop() # will remove last item0
ones[1, 1, 1, 1]
copyones = ones[:]copyones[1, 1, 1, 1]
ones[1, 1, 1, 1]
ones.append(0)ones[1, 1, 1, 1, 0]
copyones[1, 1, 1, 1]
nums[11, 11, 23, 11, 2, 1, 2, 1, 2, 14, 5]
nums[1:8:2] # start at 1 end at 7 but alternate ..step 2[11, 11, 1, 1]
nums[::2][11, 23, 2, 2, 2, 5]
naturals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]naturals[::2][1, 3, 5, 7, 9]
naturals[1::2][2, 4, 6, 8, 10]
naturals[::-1] # reverse[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]