-
Notifications
You must be signed in to change notification settings - Fork 1
/
11 CHAPTER 11 python.py
176 lines (123 loc) · 3.85 KB
/
11 CHAPTER 11 python.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
167
168
169
170
171
172
173
174
# ***********************************************************************************************************************************************************************
# ***********************************************************************************************************************************************************************
# *********************************************************CH - 11*******************************************************************************************************
# ***********************************************************************************************************************************************************************
# ***********************************************************************************************************************************************************************
# ***********************************************************************************************************************************************************************
# Intro to * age
# make flexible function
# * operator
# * args
# def total(a,b,c,d):
# return a+b+c+d
# def all_total(*args):
# print(args)
# print(type(args))
# all_total(1,2,3,4,42,4,232)
def all_total(*args):
# 1,2,3,4,42,4,232
total = 0
for num in args:
total += num
return total
print(all_total(1,2,3,4,42,4,232))
# *args with normal parameters
def multiply_nums(num1,num2,*args):
multiply = 1
print(args)
# print(num)
for i in args:
multiply *= i
return multiply
print(multiply_nums(1,2,3,3))
# * args as a argument
def multiply_nums(*args):
multiply = 1
print(args) 2
# print(num)
for i in args:
multiply *= i
return multiply
num = [2,3,4]
print(multiply_nums(*num)) # unpack
# print(type(num))
# exercise of ch - 11
# defin a function
# input
# num, iterable(tuple,list)containing numbera as args
# example
# nums = [1,2,3]
# to power (3 *multiply_nums)
# output
# list ---> [1**3,8,27]
# if user didn't pass any args then give a user a massage 'hey you did't pass args
# else
# return list
# Note - use list comprehension
# def cube_(num,*args):
# # multiply = 1
# # print(args)
# # print(list)
# # print(num)
# for i in args:
# if args:
# return[i**num]
# else:
# return "you did't pass args"
# nums = [1,2,3]
# print(cube_(2,*nums))
def to_power(num, *args):
if args:
return [i**num for i in args]
else:
return "you didn't pass any args"
nums = [2,3,4]
print(to_power(2,*[2,3]))
# ** kwargs
# kwargs (keyword argument)
# ** kwargs (dubble star operator)
# kwargs as a perameter
def func(name,**kwargs):
print(kwargs)
print(type(kwargs))
print(name)
for k,e in kwargs.items():
print(f'{k} : {e}')
func('mohit',first_name = 'Gaurav', last_name = 'Kumar')
# dictionary unpacking
d = {
'name' : 'Block',
'age' : 13,
'class' : 8,
}
func(**d)
# function with all type a parameters
# very important to understand
# PADK
# parameters
# * args
# Default parameters
# ** kwargs
# Default parameters
def func(name = 'unknown' , age = 13):
print(name)
print(age)
func('GAURAV',14)
# all we use
def func(name,*args,last_name = 'unknown',**kwargs):
print(name)
print(args)
print(last_name)
print(kwargs)
func('Gaurav',2,3,4,a = 1, b = 2)
# exercise2 of ch 11
def func(l,**kwargs):
if kwargs.get('reverse_str') == True:
return [name[::-1].title() for name in l]
else:
return[name.title()for name in l]
names = ['gaurav','mohit']
print(func(names,))
# name = ['Gaurav','Mohi t']
# print(func(name ,reversed_str = True))
# def func(name)