a = [1, 2, 3, 4, 5]
b = ['1','2','3','4']
for x,y in zip(a,b):
print(x, y)1 1
2 2
3 3
4 4
Nov-Jan 2025 batch, Vikrant Patil
Date: 20 Jan 2026
Click here for All Notes
login to https://traininghub.vikrant.dev/ with your username and create a notebook with name module2-day4.ipynb
© Vikrant Patil
problem - Write a function to generate pascal traingle in list format given the size of base - Write a function to pretty print pascal triangle print_pascal given the traingle
1
1 1
1 2 1
1 3 3 1
1 3 3 1 1 3 3 1 ——————– 1 4 6 4 1
[[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1]]
def format_line(row, rowsize, maxsize):
print(" ".join([f"{i:{maxsize}d}" for i in row]).center(rowsize))
def get_size_max_int(lastrow):
return len(str(max(lastrow)))
def get_maxsize(lastrow):
n = len(lastrow)
maxdigits = get_size_max_int(lastrow)
rowsize = n*maxdigits + n-1
return rowsize, maxdigits
def pretty_print(t):
lastrow = t[-1]
rowsize, maxsize = get_maxsize(lastrow)
for row in t:
format_line(row, rowsize, maxsize) 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29]
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[45], line 1 ----> 1 pretty_print(triangle(10)) Cell In[32], line 18, in pretty_print(t) 16 rowsize, maxsize = get_maxsize(lastrow) 17 for row in t: ---> 18 format_line(row, rowsize, maxsize) Cell In[32], line 2, in format_line(row, rowsize, maxsize) 1 def format_line(row, rowsize, maxsize): ----> 2 print(" ".join([f"{i:{maxsize}d}" for i in row]).center(rowsize)) ValueError: Unknown format code 'd' for object of type 'str'
def format_line(row, rowsize, maxsize):
print(" ".join([str(i).rjust(maxsize) for i in row]).center(rowsize))
def get_size_max_int(lastrow):
def mylen(x):
return len(str(x))
return mylen(max(lastrow, key=mylen))
def get_maxsize(lastrow):
n = len(lastrow)
maxdigits = get_size_max_int(lastrow)
rowsize = maxdigits*n + n-1
print(rowsize, maxdigits)
return rowsize, maxdigits
def pretty_print(t):
lastrow = t[-1]
rowsize, maxsize = get_maxsize(lastrow)
for row in t:
format_line(row, rowsize, maxsize)43 3
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
Bank Account - deposit - withdraw - check balance
Writing bank0.py
Writing bank1.py
class BankAccount:
def __init__(self, name, amount): # dunder init
self.name = name
self.balance = amount
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance < amount:
raise Exception(f"Balance is low, can not withdraw {amount}!")
self.balance -= amount
def get_balance(self):
return self.balance--------------------------------------------------------------------------- Exception Traceback (most recent call last) Cell In[122], line 1 ----> 1 acc1.withdraw(20000) Cell In[112], line 12, in BankAccount.withdraw(self, amount) 10 def withdraw(self, amount): 11 if self.balance < amount: ---> 12 raise Exception(f"Balance is low, can not withdraw {amount}!") 13 self.balance -= amount Exception: Balance is low, can not withdraw 20000!
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[111], line 1 ----> 1 a.foo() # python passed self as first argument here! TypeError: A.foo() takes 0 positional arguments but 1 was given