Skip to content

Commit 22c3c73

Browse files
committed
feat: add function and prev class codes
1 parent 69e10de commit 22c3c73

22 files changed

+360
-0
lines changed

forLoopsAndPatterns/challenge3.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
N = int(input())
2+
3+
for i in range(N):
4+
print('*', end='')

forLoopsAndPatterns/challenge4.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
N = int(input())
2+
3+
# loop within loop
4+
# nested loops
5+
for j in range(N):
6+
for i in range(N):
7+
print('*', end='')
8+
# after printed one row
9+
# we need to start a new line
10+
print()
11+
12+
13+
14+
15+

forLoopsAndPatterns/challenge5.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Challenge 5:
2+
# Given N = 3 print
3+
"""
4+
* => 1 star
5+
** => 2 stars
6+
*** => 3 stars
7+
8+
start = 1
9+
end = N + 1
10+
inc = 1
11+
12+
range(1, N + 1)
13+
"""
14+
15+
N = int(input())
16+
# how can I print numbers from 1 to N?
17+
18+
for i in range(1, N + 1):
19+
# print(i)
20+
# how can I print i stars?
21+
for j in range(i):
22+
print('*', end='')
23+
# after printing one row, we print a new line
24+
print()
25+

forLoopsAndPatterns5/break1.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# say we want to break at 50
2+
3+
for i in range(100):
4+
print(i, end=' ')
5+
if i == 50:
6+
break
7+
8+
9+
print()
10+
11+
# we can also break the loop directly
12+
13+
for i in range(100):
14+
print(i)
15+
break
16+

forLoopsAndPatterns5/challenge1.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# print all integers from 1 to n
2+
3+
n = int(input())
4+
5+
# this does not print the value N=n
6+
for i in range(1, n): # starting at 1, ending at n
7+
print(i, end=' ')
8+
9+
print()
10+
11+
12+
# this will print all values from 1 to n
13+
for i in range(1, n + 1):
14+
# end = n + 1, because we want n to included
15+
print(i, end=' ')
16+

forLoopsAndPatterns5/challenge2.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# print all odd integers from 1 to n
2+
3+
n = int(input())
4+
5+
# prints all numbers from 1 to n
6+
for i in range(1, n + 1):
7+
print(i, end=' ')
8+
9+
print()
10+
11+
# we can increment by 2 now
12+
for i in range(1, n + 1, 2):
13+
print(i, end=' ')
14+

forLoopsAndPatterns5/challenge3.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# print the number of factors of a number n
2+
3+
n = int(input())
4+
5+
for i in range(1, n + 1):
6+
if n % i == 0:
7+
print(i, end=' ')
8+

forLoopsAndPatterns5/challenge4.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# print a^b
2+
3+
a = int(input())
4+
b = int(input())
5+
6+
ans = 1
7+
for i in range(b): # range(1, b + 1) also works
8+
ans *= a
9+
print('ans now is: ', ans)
10+
11+
print('final ans is: ', ans)

forLoopsAndPatterns5/continue1.py

Whitespace-only changes.

forLoopsAndPatterns5/forLoop1.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
print(list(range(-1, 10, 2)))
2+
3+
# what if we want to pick these values one by one
4+
# we can simply use a for loop to iterate one by one
5+
6+
for i in range(-1, 10, 2):
7+
print(i, end=' ')
8+
9+
# now let us say we want to print their squares
10+
print() # prints a new line
11+
12+
for i in range(-1, 10, 2):
13+
print(i * i, end=' ')
14+
15+
16+
# let's say we want to find the sum of these numbers
17+
18+
sum = 0
19+
for i in range(-1, 10, 2):
20+
sum += i
21+
22+
print()
23+
print('sum of all these numbers', sum)
24+
25+
print()
26+
print('Now using while loop')
27+
28+
i = -1
29+
while i < 10:
30+
print(i * i, end=' ')
31+
i += 2

forLoopsAndPatterns5/range1.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
print(range(6)) # this is not helpful
2+
3+
print(list(range(6))) # converting this to a list
4+
# we will talk about list in the next classes
5+
6+
print(list(range(2, 8)))
7+
# [2, 3, 4, 5, 6, 7]
8+
9+
print(list(range(-1, 10, 2))) # values < 10 are included
10+
# [-1, 1, 3, 5, 7, 9]
11+
12+
print(list(range(10, 0, -2))) # values > 0 are included
13+
# [10, 8, 6, 4, 2]
14+
15+
print(list(range(10, 12, -2))) # values > 12 are included
16+
# 10 itself is not greater than 12
17+
# does not include anything
18+
19+
# []
20+
21+
print(list(range(5, 1))) # start = 5, end = 1, inc = 1
22+
# values < end, 5 itself is not less than end
23+
# []

functions/customRange.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# default argument
3+
def my_range(end, start = 3):
4+
print('start', start)
5+
print('end', end)
6+
7+
while start < end:
8+
print(start, end=' ')
9+
start += 1
10+
11+
# my_range(1, 10)
12+
13+
my_range(10) # default start = ?
14+
print()
15+
my_range(5)
16+
print()
17+
18+
my_range(1, 10)
19+
20+
print()
21+
22+
my_range(start=1, end=10)
23+
# keyword arguments, more explained in next class

functions/defaultArgsFailing.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# WHY are we not allowed to keep default arguments in
2+
# the starting
3+
4+
# ALL THE DEFAULT ARGUMENTS SHOULD COME AT THE END
5+
6+
def introduce(age = 22, name = 'Sahil'):
7+
print('Hello, I am', name)
8+
print('I am', age, 'years old')
9+
print()
10+
11+
introduce('Sahil')
12+
introduce(23)
13+
14+
# Does not allow default arguments in the start
15+
"""
16+
def add(n1 = 2, n2):
17+
return n1 + n2
18+
19+
print(add(3, 5))
20+
"""

functions/introduce.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def introduce(name, age = 22):
2+
print('Hello, I am', name)
3+
print('I am', age, 'years old')
4+
print()
5+
6+
introduce('Sahil', 22)
7+
8+
introduce('Kuldip', 25)
9+
10+
introduce('Sahil')
11+
12+
introduce('Rahul', 24)
13+
14+

functions/multipleInputs.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
t = int(input())
2+
3+
for i in range(t):
4+
x = int(input())
5+
print(i, 'th input is:', x)

functions/myPrintFunction.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# make my own function that prints some data
2+
3+
def my_print_fun(val):
4+
# we can use the library's print function
5+
print(val)
6+
7+
my_print_fun(5)
8+
my_print_fun("Hello world")
9+
my_print_fun(5.66)
10+
print()
11+
# write a function that adds 2 numbers
12+
13+
def add_2_numbers(num_1, num_2):
14+
print(num_1 + num_2)
15+
16+
add_2_numbers(5, 7)
17+
add_2_numbers(5.7, 9.6)
18+
add_2_numbers(-1, -2)
19+
20+
x = 1
21+
y = 2
22+
add_2_numbers(x, y)
23+

functions/noReturnCapture.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def add_2_nums_with_return(n1, n2):
2+
return n1 + n2
3+
4+
y = add_2_nums_with_return(5, 6)
5+
print(y)

functions/recipe.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# make a tea, print the recipe
2+
# define a function
3+
4+
# add_two_numbers
5+
6+
# def function_name():
7+
# # function code will start here
8+
9+
def tea():
10+
print('Light the stove')
11+
print('Boil the water in a tea pan')
12+
print('Add tea powder')
13+
print('Add milk')
14+
print('Add sugar')
15+
print('Use a sieve and filter in a cup')
16+
print()
17+
18+
19+
# call a function
20+
tea()
21+
22+
tea()
23+
24+
tea()
25+
26+
# 0, 1, 2, ..., 9
27+
for i in range(10):
28+
tea()
29+

functions/returnValues.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
def add_2_nums(n1, n2):
3+
print(n1 + n2)
4+
5+
6+
x = add_2_nums(1, 2)
7+
print(x)
8+
print()
9+
10+
# try to return some value now
11+
12+
def add_2_nums_with_return(n1, n2):
13+
return n1 + n2
14+
15+
y = add_2_nums_with_return(5, 6)
16+
print(y)
17+
18+
if y % 2 == 0:
19+
print('Result of addition is even')
20+
else:
21+
print('Result of addition is odd')
22+

functions/squareNum.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
def add(x, y):
3+
return x + y
4+
5+
def square(x):
6+
return x*x
7+
8+
n1 = square(3)
9+
print('n1 is', n1)
10+
11+
n2 = square(5)
12+
print('n2 is', n2)
13+
14+
print(add(n1, square(9)))
15+

patternPrinting/downwardTriangle.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n = int(input())
2+
3+
# loop over n times, say n = 5
4+
# r = 1, 2, 3, 4, 5
5+
for r in range(1, n + 1):
6+
# loop over (n - r + 1) times
7+
for c in range(n - r + 1):
8+
print('*', end='')
9+
print()
10+
11+

patternPrinting/hulkChallenge1.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
n = int(input())
3+
4+
for r in range(1, n):
5+
6+
# print (n - r + 1) *s
7+
for c in range(n - r + 1):
8+
print('*', end='')
9+
10+
# print 2*(r - 1) spaces
11+
for c in range(2*(r - 1)):
12+
print(' ', end='')
13+
14+
# print (n - r + 1) *s
15+
for c in range(n - r + 1):
16+
print('*', end='')
17+
18+
print()
19+
20+
# code for the HW (challenge 7)
21+
for r in range(1, n + 1):
22+
for c in range(r):
23+
print('*', end='')
24+
25+
for c in range(2*(n - r)):
26+
print(' ', end='')
27+
28+
for c in range(r):
29+
print('*', end='')
30+
print()

0 commit comments

Comments
 (0)