forked from sonumahajan/All_Program_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.py
More file actions
281 lines (219 loc) · 6.84 KB
/
func.py
File metadata and controls
281 lines (219 loc) · 6.84 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# demo is the function name
def demo(name, age):
# print value
print(name, age)
# call function
demo("Ben", 25)
"""def function_name(parameter: data_type) -> return_type:
"""Doctring"""
# body of the function
return expression"""
#define a function with parameter
def add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")
"""A default argument is a parameter that assumes a default value if a value
is not provided in the function call for that argument. """
# Python program to demonstrate
# default arguments
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
# Driver code (We call myFun() with only
# argument)
myFun(10)
"""The idea is to allow the caller to specify the argument name with
values so that caller does not need to remember the order of parameters."""
# Python program to demonstrate Keyword Arguments
def student(firstname, lastname):
print(firstname, lastname)
# Keyword arguments
student(firstname='Computer', lastname='Science')
student(lastname='Science', firstname='Computer')
"""In Python, we can pass a variable number of arguments to a function
using special symbols. There are two special symbols:
*args (Non-Keyword Arguments)
**kwargs (Keyword Arguments)"""
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print(arg)
myFun('Hello', 'Welcome', 'to', 'Python')
# Python program to illustrate
# *kwargs for variable number of keyword arguments
def myFun(**kwargs):
for key, value in kwargs.items():
print("%s == %s" % (key, value))
# Driver code
myFun(first='I', mid='love', last='India')
"""The first string after the function is called the Document string or
Docstring in short. This is used to describe the functionality of the function. The use of docstring in functions is optional but it is considered a good practice.
The below syntax can be used to print out the docstring of a function:
Syntax: print(function_name.__doc__)"""
# A simple Python function to check
# whether x is even or odd
def evenOdd(x):
"""Function to check if the number is even or odd"""
if (x % 2 == 0):
print("even")
else:
print("odd")
# Driver code to call the function
print(evenOdd.__doc__)
"""A function that is defined inside another function is known as the inner
function or nested function. Nested functions are able to access variables of
the enclosing scope. Inner functions are used so that they can be protected
from everything happening outside the function."""
# Python program to
# demonstrate accessing of
# variables of nested functions
def f1():
s = 'I love Python'
def f2():
print(s)
f2()
# Driver's code
f1()
"""In Python, an anonymous function means that a function is without a name.
As we already know the def keyword is used to define the normal functions
and the lambda keyword is used to create anonymous functions.
Syntax:
lambda argument_list:expression"""
l = [10, 5, 12, 78, 6, 1, 7, 9]
even_nos = list(filter(lambda x: x % 2 == 0, l))
print("Even numbers are: ", even_nos)
#without lambda function
def even_numbers(nums):
even_list = []
for n in nums:
if n % 2 == 0:
even_list.append(n)
return even_list
num_list = [10, 5, 12, 78, 6, 1, 7, 9]
ans = even_numbers(num_list)
print("Even numbers are:", ans)
"""A recursive function is a function that calls itself again and again."""
def addition(num):
if num:
# call same function by reducing number by 1
return num + addition(num - 1)
else:
return 0
res = addition(10)
print(res)
"""The function return statement is used to exit from a function and
go back to the function caller and return the specified value or data item
to the caller.
Syntax:return [expression_list]"""
def square_value(num):
"""This function returns the square
value of the entered number"""
return num**2
print(square_value(2))
print(square_value(-4))
"""Local variables are those which are initialized inside a function and belong
only to that particular function. It cannot be accessed anywhere outside the function.
Let’s see how to create a local variable."""
def f():
# local variable
s = "I love Python"
print(s)
# Driver code
f()
"""If we will try to use this local variable outside the function then let’s see what will happen."""
def f():
# local variable
s = "I love Python"
print("Inside Function:", s)
# Driver code
f()
print(s)
"""These are those which are defined outside any function and which are accessible throughout
the program, i.e.,inside and outside of every function. Let’s see how to create a global variable."""
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love Python"
f()
print("Outside Function", s)
"""Special Case"""
# This function has a variable with
# name same as s.
def f():
s = "Me too."
print(s)
# Global scope
s = "I love Python"
f()
print(s)
"""Using global keyword"""
# This function modifies the global variable 's'
def f():
global s
s += ' GREAT'
print(s)
s = "Look for Python"
print(s)
# Global Scope
s = "Python is great!"
f()
print(s)
"""using Global and Local variable"""
a = 1
# Uses global because there is no local 'a'
def f():
print('Inside f() : ', a)
# Variable 'a' is redefined as a local
def g():
a = 2
print('Inside g() : ', a)
# Uses global keyword to modify global 'a'
def h():
global a
a = 3
print('Inside h() : ', a)
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
"""Below is the function display_student(name, age).
Assign a new name show_tudent(name, age) to it and call it using the new name."""
def display_student(name, age):
print(name, age)
# call using original name
display_student("Emma", 26)
# assign new name
showStudent = display_student
# call using new name
showStudent("Emma", 26)
"""Note: Create a function in such a way that we can pass any number of arguments to this function,
and the function should process them and display each argument’s value."""
def func1(*args):
for i in args:
print(i)
func1(20, 40, 60)
func1(80, 100)
def calculation(a, b):
addition = a + b
subtraction = a - b
# return multiple values separated by comma
return addition, subtraction
# get result in tuple format
res = calculation(40, 10)
print(res)
# function with default argument
def show_employee(name, salary=9000):
print("Name:", name, "salary:", salary)
show_employee("Ben", 12000)
show_employee("Jessa")