-
Notifications
You must be signed in to change notification settings - Fork 36
/
func-code.py
262 lines (193 loc) · 3.77 KB
/
func-code.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
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
def func(args):
print args
print 'hello world'
return args, 77
a = func('hello')
print a
'''
#default arguments
def newFunc(arg='abc',arg1):
print 'Hello, How are you ',arg, arg1
newFunc(44)
newFunc(55)
newFunc(66,'Awi')
def func(name,age,location):
print name,age,location
func(age=33,location='mumbai',name='awa')
'''
'''
def func(*vargs):
print vargs[1] * vargs[3]
func('abc',44,'def',90)
#func('xyz')
'''
#l = [1,2,3,4,5]
'''
def revpacking(name,age,location):
print name,age,location
l = ['awi',77,'blore']
revpacking(*l)
'''
'''
def fun(name,*subjects):
print 'hello %s' %name
res = ''
for i in subjects:
res = res + ' ' + str(i)
print 'thanks for attending subjects,', res
fun('abc',34,67,23)
fun('def',55,66)
'''
'''
def func(name,age,location):
print name,age,location
db = {'name':'abc','age':33, 'location':'bangalore'}
func(**db)
def kwargsfunc(**kwargs):
if 'age' in kwargs:
print kwargs['age']
kwargsfunc(name='awantik',age=20)
'''
'''
def kfunc(name,*marks,**location):
print name
print marks
print location
#kfunc('awantik', 33,44,55, comp='abc',age=44)
kfunc('awantik', 33,44,55,34,43,56)
'''
'''
print 'a' in 'abcd'
print 'a' in ['a','b','c']
db = {'a':'awantik', 'b':'bawa', 'c':'cat'}
if 'a' in db:
print 'yess'
else:
print 'not there'
'''
'''
l = range(10,100,5)
print len(l), max(l), sum(l)
for d in l[5:]:
print d
for x in range(len(l)):
l[x] -=2
print l
for idx,data in enumerate(l):
print idx,data
x = enumerate(l)
print x.next()
print x.next()
'''
'''
def func():
i = 0
while True:
i += 1
yield i
f = func()
print f
print f.next()
print f.next()
print f.next()
print f.next()
'''
#lambda - Annonymous functions
'''
f = lambda x: x + 5
g = lambda x,y: x + y
print f(8)
print g(9,19)
l = [ ['awa',33], ['bwa',11] ]
print sorted(l,cmp= lambda x,y: cmp(x[0],y[0]))
'''
#Function that generates function
def funGenerator(num):
return lambda x: x**num
f = funGenerator(2) # lambda x: x**2
g = funGenerator(3) # lambda x: x**3
h = funGenerator(4) #
print f(4)
print g(4)
print h(4)
#Functional Programming
'''
def fun(x):
return x*2
l = [1,2,3,3,4,4,5,56,9]
print map(float,l)
print map(fun,l)
print map(lambda x:x**2,l)
print filter(lambda x: x%2 ==0,l)
print reduce(lambda x,y: x+y,l)
'''
'''
class Base:
pass
b = Base()
b.name = 'awantik'
b.gender = 'M'
print b.__dict__
print b.name
'''
'''
class Base:
address = 'abc'
def __init__(self,name,gender):
self.__name = name
self.gender = gender
def someinfo(self,location):
#self.location
print self
@staticmethod
def staticSomeInfo(name,age):
Base.address = name
b = Base('awantik','male')
print b.__dict__
b.someinfo('bangalore')
print b.address
Base.staticSomeInfo('awantik',44)
print b._Base__name
class Derived(Base):
def Happy(self):
print 'happy time'
d = Derived('abc',87)
d.Happy()
d.someinfo('bangalore')
'''
'''
class Base:
def __init__(self,**kwargs):
for k,v in kwargs.items():
print k,v
if k == 'age':
self.age = v
if k == 'name':
self.name = v
b = Base(name='Awantik',age=40)
print b.__dict__
b = Base(name='newguy')
print b.__dict__
class Derived(Base):
pass
d = Derived()
a = []
if type(a) == list:
print 'List it is'
print isinstance(d,Base)
'''
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def __eq__(self,obj):
return self.name == obj.name and self.age == obj.age
def __le__()
p1 = Person('awantik',79)
p2 = Person('awantik',79)
#p2 = p1
print id(p1), id(p2)
if p1 == p2:
print 'same'
#print p1.__dict__
#print p2.__dict__