-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3-function_ & _Recursion.py
166 lines (120 loc) · 2.44 KB
/
day3-function_ & _Recursion.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Function and Recursion
block of statements that preform a specific task
EX:
def func_name(para 1,para 2)": #function definition
some work
return val
func_name(arg1,arg2...)"""#function call
def sum1(a,b):
sum =a+b
print(sum)
return sum
sum1(10,10)
def sum2(a,b):
sum =a+b
print(sum)
sum2(10,10)
#function definition
def cal(a,b): #fparamrters
return a + b
sum = cal(10,33)#arguments
print(sum)
def word():
print("hey")
word()
word()
word()
word()
def avg(a,b,c):
sum = a+b+c/3
print(sum)
avg(10,10,10)
#Built-in function
print()
len()
type()
range()
#practice
#1 print len of list
list1 = [1,2,3,4,5]
list2 = [1,2,3,4,5]
def l(lst):
a =len(list)
print(a)
l(list1)
l(list2)
name = ["sakshi","raj","sai"]
s_name =["yadav","yadav","yadav"]
print(name[0],end="")
print(name[1],end="")
print(name[2],end="")
def print_len(list):
for item in list:
print(item,end="")
print("hehe")
print_len(name)
print_len(s_name)
#cal_fact
def fact(n):
fact1= 1
for i in range(1,n+1):#range() function, the ending value is exclusive,
fact1 *= i
return fact1
f= fact(10)
print(f)
def converter(usd_val):
inr_val =usd_val*83
print(usd_val,"USD=",inr_val,"INR")
converter(6)
def num(a=9):
if (a%2==0):
print("EVEN str")
else:
print("ODD str")
return a
num()
#recursion
#when a function calls itself repeatdly
#ex
def show(n):
if(n==0):#base case
return
print(n)
show(n-1)
print("end")
show(3)
#call stack is used in recusion
#call(function call) stack (one upon other)
#whn we call one function on other
#fact! of n
def play(n):
if(n == 1 or n == 1):
return 1
return play(n-1)*n
a =play(4)
print(a)
#cal the sum of first n num
def cal(n):
if(n==0):
return 0
return cal(n-1)+n
"""
cal(3) calls cal(2) + 3.
cal(2) calls cal(1) + 2.
cal(1) calls cal(0) + 1.
cal(0) returns 0 (base case).
cal(0) returns 0.
cal(1) + 1 = 0 + 1 = 1.
cal(2) + 2 = 1 + 2 = 3.
cal(3) + 3 = 3 + 3 = 6
sum = cal(3)
print(sum)
"""
#recursive function to print all elements in list
def print_list(list, idx=0):
if idx == len(list):
return
print(list[idx])
print_list(list, idx + 1)
list4 = [1, 2, 3, 4, 5, 6]
print_list(list4)