%%file welcome_multi_lingual.pyimport syslang = sys.argv[1]name = sys.argv[2]if lang =='marathi':print("नमस्कार", name)elif lang =="english":print("hello", name)elif lang =="Tamil":print("Wannakam", name)else:print("Unknown langualge")
Writing welcome_multi_lingual.py
!python welcome_multi_lingual.py marathi vikrant
नमस्कार vikrant
!python welcome_multi_lingual.py Tamil vikrant
Wannakam vikrant
Assignment Problems
Write a python script chaos.py which prints first 10 iterations of x which is computed using follwing logic. To start with some value of x is given by user (from command line arguments). After that for each iteration new value of x is generated using the equation x = 3.9 * x * (1 - x) after computing new value of x print it to screen. See what happens to value of x at 10 itertion for different starting values of x i.e. 0.24, 0.25, 0.26, 0.3 etc.
%%file chaos.pyimport sysdef chaotic_function(x):return3.9*x*(1-x)def run_iteration(x, n):for i inrange(n): x = chaotic_function(x) print(x) x =float(sys.argv[1])run_iteration(x, 10)
Write a python script tail.py which will print last 5 lines of a file. this is how it should work.
$ python tail.py zen.txt
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!
nums = [1, 2, 3, 4, 5, 6, 7, 8, 10]nums[0]
1
len(nums)
9
l =len(nums)
nums[l -1]
10
nums[l -2]
8
nums[l -3]
7
nums[l -4]
6
%%file tail.pyimport sysdef print_last_n(items, n): l =len(items)for i inrange(1, n+1):print(items[l - i])def print_last_10lines(filename):withopen(filename) as f: lines = []for line in f: lines.append(line.strip()) print_last_n(lines, 10)if__name__=="__main__": filepath = sys.argv[1] print_last_10lines(filepath)
Overwriting tail.py
!python tail.py zen.txt
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.
%%file tail.pyimport sysdef print_last_10lines(filename):withopen(filename) as f: lines = []for line in f: lines.append(line.strip())iflen(lines) >10: lines.pop(0)for line in lines:print(line)if__name__=="__main__": filepath = sys.argv[1] print_last_10lines(filepath)
Overwriting tail.py
nums
[2, 3, 4, 5, 6, 7, 8, 10]
nums.pop(0)
2
nums
[3, 4, 5, 6, 7, 8, 10]
!python tail.py zen.txt
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 tail.pyimport sysdef tail(filename, n):withopen(filename) as f: lines = []for line in f: lines.append(line.strip())iflen(lines) > n: lines.pop(0)for line in lines:print(line)if__name__=="__main__": n =int(sys.argv[1]) filepath = sys.argv[2] tail(filepath, n)
Overwriting tail.py
!python tail.py 5 zen.txt
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!
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!
list slicing
nums = [2, 3, 1, 3, 1, 35, 3, 1, 8,0]
nums
[2, 3, 1, 3, 1, 35, 3, 1, 8, 0]
nums[0]
2
list(range(1, 5))
[1, 2, 3, 4]
nums[0:5] # start at index 0 and stop at index 4
[2, 3, 1, 3, 1]
nums
[2, 3, 1, 3, 1, 35, 3, 1, 8, 0]
nums[3:7] # start at index 3 and stop at index 6
[3, 1, 35, 3]
nums[2:] # drop first 2 items
[1, 3, 1, 35, 3, 1, 8, 0]
nums[:5] # take first 5 items
[2, 3, 1, 3, 1]
nums[len(nums)-3:] # last 3 items
[1, 8, 0]
def tail(filename, n):withopen(filename) as f: lines = f.readlines() last_n_lines = lines[len(lines)-n:] # this is adavantage is due to list slicingfor line in last_n_lines:print(line, end="")
tail("zen.txt", 5)
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!
iteration patterns
## accumulation ... you make one single item from given s =0for i in nums: s = s + iprint(s)
57
sqr = []for i in nums: sqr.append(i*i)print(sqr)
[4, 9, 1, 9, 1, 1225, 9, 1, 64, 0]
withopen("zen.txt") as f: linenum =1for line in f:print(linenum, line, end="") linenum +=1
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!
withopen("zen.txt") as f:for i, line inenumerate(f):print(i, 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!
for i, n inenumerate(nums):print(i, n)
0 2
1 3
2 1
3 3
4 1
5 35
6 3
7 1
8 8
9 0
students = ["Alice", "Alex", "Hans", "Liya", "Luke"]
for rollnum, name inenumerate(students, start=1):print(rollnum, name)
1 Alice
2 Alex
3 Hans
4 Liya
5 Luke
for student inreversed(students):print(student)
Luke
Liya
Hans
Alex
Alice
students
['Alice', 'Alex', 'Hans', 'Liya', 'Luke']
for i, student inreversed(enumerate(students)):print(i, student)
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
Cell In[108], line 1----> 1for i, student inreversed(enumerate(students)):
2print(i, student)
TypeError: 'enumerate' object is not reversible
enumerate?
Init signature: enumerate(iterable, start=0)Docstring:
Return an enumerate object.
iterable
an object supporting iteration
The enumerate object yields pairs containing a count (from start, which
defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), ...
Type: type
Subclasses: