-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser_defined_functions.py
More file actions
193 lines (100 loc) · 3.43 KB
/
Copy pathuser_defined_functions.py
File metadata and controls
193 lines (100 loc) · 3.43 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
def length_of_iterable(iterable):
count = 0
for _ in iterable:
count += 1
return count
# length = length_of_iterable("hello")
# print(length)
####################################################################
def display(name, age):
print(f"My name is {name}, I am {age} years old")
# positional arguments
display("John", 30)
display(30, "john")
# keyword arguments
display(age=30, name="John")
# combination of positional and keyword arguments
display("John", age=30)
###############################################################
# positional only arguments
def add(a, b, c, /):
print(a + b + c)
# add(1, 2, 3) # 6
# add(a=1, b=2, c=3) # TypeError
# add(1, b=2, c=3) # TypeError
def add(a, b, c, /, d):
print(a + b + c + d)
add(1, 2, 3, 4)
add(1, 2, 3, d=4)
####################################################################
# keyword only arguments
def add(*, a, b, c):
print(a + b + c)
# add(1, 2, 3) # TypeError
# add(a=1, b=2, c=3) # 6
# add(1, b=2, c=3) # TypeError
def add(a, *, b, c):
print(a + b + c)
add(1, b=2, c=3) # 6
add(a=1, b=2, c=3) # 6
##############################################################
# combination of keyword only and positional only parameters
def add(a, b, /, *, c, d):
print(a + b + c + d)
add(1, 2, c=3, d=4)
#############################################################
# default parameter
def add(a, b, c, d=0):
print(a + b + c + d)
add(1, 2, 3) # 6
add(1, 2, 3, 4) # 10
# default values cannot be initialized twice.
a = 7
def spam(x=a):
print(x)
a = 10
spam()
##################################################################
# *args and **kwargs
# variable positional arguments
def add(*args):
print(args)
print(sum(args))
add(1, 2, 3, 4, 5, 6, 7, 8)
add()
# variable keyword arguments
def display(**kwargs):
print(kwargs)
display(a=1, b=2, c=3)
# combination of variable positional and keyword arguments
def display(*args, **kwargs):
print(args, kwargs)
display(1, b=2, c=3)
#################################################################
# function annotations
def add(a: int, b: int) -> int:
print(a + b)
add("hai", "hello")
add(1, 2)
#########################################################################
""" Activity """
# count the number of positional and keyword arguments passed.
def display(*args, **kwargs):
print("Positional arguments:", len(args))
print("Keyword arguments:", len(kwargs))
# display(1, 2, 3, a=10, b=20)
###################################################################
# Write a function to print message “Hai Everyone” if the user input is not present and
# if the user input is present print the user input.
def greet(msg="hai everyone"):
print(msg)
# greet("hello")
####################################################################
# A function takes variable number of positional arguments as input.
# How to check if the arguments that are passed are more than 5
def check_pos(*args):
if len(args) > 5:
print("there are more than 5 positional arguments")
else:
print("there are less than 5 positional arguments")
# check_pos(1, 2, 3, 4, 5, 6, 6)