Skip to content

Commit 3b3e243

Browse files
committed
feat: all codes of last classes
1 parent 22c3c73 commit 3b3e243

16 files changed

+244
-2
lines changed

functions/defaultArgsFailing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ def introduce(age = 22, name = 'Sahil'):
88
print('I am', age, 'years old')
99
print()
1010

11-
introduce('Sahil')
12-
introduce(23)
11+
12+
introduce('Rishi')
13+
# introduce(23)
14+
# introduce(25, 'Rishi')
1315

1416
# Does not allow default arguments in the start
1517
"""

functions/pattern.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
n = int(input())
2+
3+
for r in range(1, n + 1):
4+
for c in range(1, n - r + 1):
5+
print(0, end='')
6+
7+
for c in range(r, 2*r):
8+
print(c, end='')
9+
10+
for c in range(2*(r - 1), r - 1, -1):
11+
print(c, end='')
12+
13+
for c in range(1, n - r + 1):
14+
print(0, end='')
15+
16+
# if it's the last row, then do no print a new line
17+
if r < n:
18+
print()

functions/patternNaveen.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
N = int(input())
2+
3+
4+
for i in range(1, N+1):
5+
for j in range(N+1-i):
6+
if j > 0:
7+
# skip printing for the 1st iteration,
8+
# print for all the remaining
9+
print(' ', end='')
10+
for k in range(j, N):
11+
print('*', end='')
12+
print()

functions/test.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
def f(a, b, c, d = 1, e = 2, f = 3):
3+
print('d', d)
4+
print('e', e)
5+
print('f', f)
6+
print('something')
7+
8+
# skip d
9+
# pass e
10+
f(1, 2, 3, e = 4)

functions2/custom_range.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def my_range(start=0, end=10):
3+
while start < end:
4+
print(start, end=' ')
5+
start += 1
6+
7+
# my_range(end=20)
8+
9+
# my_range(start=5)
10+
11+
# my_range(10)
12+
13+
my_range()

functions2/default_argument.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# default argument
2+
3+
def print_date(day, month, year, style=0):
4+
if style == 0: # American
5+
print(month, '/', day, '/', year)
6+
elif style == 1: # European
7+
print(day, '/', month, '/', year)
8+
else:
9+
print('Invalid Style')
10+
11+
print_date(30, 5, 2022)
12+
# print_date(5, 30, 2022)
13+
14+
# print_date("30th", "May", "2022")
15+
16+
# print_date(30, 5, 2022, 1)
17+
18+
19+
20+

functions2/keyword_arguments.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def print_book_description(title, author, publisher, year, version, num_pages):
2+
print('The book has the title', title)
3+
print('The book has the author', author)
4+
print('The book has the publisher', publisher)
5+
print('The book has the year', year)
6+
print('The book has the version', version)
7+
print('The book has the num_pages', num_pages)
8+
9+
10+
# print_book_description('The Lord of The Rings', 'J. R. R Tolkien', 'George Allen & Uwin',
11+
# 1954, 1.0, 456)
12+
13+
# This below function call can give incorrect output because we forgot the order
14+
# of the arguments
15+
16+
# print_book_description('J. R. R Tolkien', 'The Lord of The Rings', 'George Allen & Uwin',
17+
# 1954, 1.0, 456)
18+
19+
20+
# keyword argument
21+
22+
print_book_description(author='J. R. R Tolkien', title='The Lord of The Rings', publisher='George Allen & Uwin',
23+
year=1954, version=1.0, num_pages=456)
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+

functions2/mix_keyword_default.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def print_date(day=1, month=1, year=2022, style=0):
2+
if style == 0: # American
3+
print(month, '/', day, '/', year)
4+
elif style == 1: # European
5+
print(day, '/', month, '/', year)
6+
else:
7+
print('Invalid Style')
8+
9+
print_date(day=30, year=2022)
10+
11+
print_date(day=5, style=1, month=5)

list1/build_list_1.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
num_matches = int(input('How many matches? '))
2+
3+
idx = 0
4+
5+
# runs = []
6+
runs = list() # BOTH these ways create an empty list
7+
8+
while idx < num_matches:
9+
print('idx is', idx, 'and matches is: ', num_matches)
10+
x = int(input('Please enter the score: '))
11+
runs.append(x)
12+
print('The current runs list is:', runs)
13+
# get the next match data
14+
idx += 1
15+
16+
print(type(runs))

list1/doubts.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
a = list()
2+
print(a)
3+
print(type(a))
4+
5+
b = []
6+
print(b)
7+
print(type(b))
8+
9+
from math import pi
10+
print((4/3)* pi)
11+
12+
print(88/21)

list1/first_list.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# runs scored
2+
runs = [0, 100, 99, 122, 34, 89, 92, 34, 99, 12]
3+
4+
for x in runs:
5+
print(x)
6+
7+
matches = len(runs)
8+
print('The no. of matches played is:', matches)
9+
10+
print('The runs in the last match is:', runs[matches - 1])
11+
12+
# index is the position of an element in the array
13+
l = [5, 1, -2, 2, 3, 4]
14+
15+
print('The quiz output is:')
16+
print(l[2])

list1/list_intro.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
m1 = int(input())
2+
m2 = int(input())
3+
m3 = int(input())
4+
m4 = int(input())
5+
m5 = int(input())
6+
m6 = int(input())
7+
m7 = int(input())
8+
m8 = int(input())
9+
m9 = int(input())
10+
m10 = int(input())
11+
12+
sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10
13+
14+
print(sum)

list1/list_operations.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# runs scored
2+
runs = [0, 100, 99, 122, 34, 89, 92, 34, 99, 12]
3+
4+
print(runs)
5+
6+
x = int(input('Please enter the runs in the 11th match: '))
7+
8+
# add some value x to the end of the list
9+
runs.append(x)
10+
11+
print(runs)

list1/solution1.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
n = int(input())
3+
4+
A = []
5+
for i in range(n):
6+
x = int(input())
7+
A.append(x)
8+
9+
print(A)
10+
11+
B = int(input())
12+
13+
# add B to all the element in the list A
14+
15+
# let's update the existing list
16+
17+
# update the existing
18+
# A[0] = A[0] + B
19+
# A[1] = A[1] + B
20+
# A[2] = A[2] + B
21+
# print(A)
22+
23+
for idx in range(n):
24+
A[idx] = A[idx] + B
25+
26+
print(A)
27+

list1/solution1Functions.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# In the assignment, no need to take any input
2+
# Only write the function
3+
4+
# A is a list
5+
# B is an integer
6+
def solve(A, B):
7+
n = len(A)
8+
9+
for i in range(n):
10+
A[i] = A[i] + B
11+
12+
return A
13+
14+
res = solve([9, 1, 2, 5, 6], 4)
15+
print(res)

list1/update_a_value.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
B = 7
2+
x = 9
3+
x = x + B
4+
5+
print(x)

0 commit comments

Comments
 (0)