def cylinder_volume(radius, height):
return 3.14*radius**2*height
cylinder_volume(1, 2)
6.28
cylinder_volume(2, 1)# by mistake , arguments order swapped...inorrect order
12.56
cylinder_volume(radius=1, height=2)
6.28
cylinder_volume(height=2, radius=1) # order doen't matter if named parameters are given
6.28
def say_hello(name="friend"):
print("hello", name)
say_hello("vikrant")
hello vikrant
say_hello() # is the parameter defined with default value, if not passed ..default will be taken
hello friend
import random
def jitter(data, jitter=random.random()):
return data+jitter
jitter(45)
45.719416944816
for i in range(5):
print(jitter(i))
0.7194169448159936 1.7194169448159937 2.7194169448159937 3.7194169448159937 4.719416944815993
import random
def jitter(data, jitter=None):
if not jitter:
jitter = random.random()
return data+jitter
for i in range(5):
print(jitter(i))
0.8775489801271318 1.5138647086694264 2.6011478633148295 3.19229987623221 4.925170727877281
def create_data(basedata=[]): #not a goot way of creating empty list in default parameters
return [jitter(item) for item in basedata]
def appendzeros(n , basedata=[]):
basedata.extend([0 for i in range(n)])
return basedata
appendzeros(2, [1, 2, 3, 4])
[1, 2, 3, 4, 0, 0]
appendzeros(3, [1, 2, 3, 4])
[1, 2, 3, 4, 0, 0, 0]
appendzeros(2) # default value for basedata
[0, 0]
appendzeros(2) # this is surprise!
[0, 0, 0, 0, 0, 0]
def appendzeros(n , basedata=None):
if basedata==None:
basedata = []
basedata.extend([0 for i in range(n)])
return basedata
appendzeros(2)# basedata is now []
[0, 0]
appendzeros(3)# basedata is now []
[0, 0, 0]
max(1, 2, 3, 4)
4
max(1, 2, 3)
3
def mysum(*nums):
print(nums)
mysum(1, 2, 3)
(1, 2, 3)
mysum(1, 2)
(1, 2)
def mysum(*nums):
s = 0
for i in nums:
s += i
return s
mysum(1, 4, 5, 6)
16
mysum(1, 1, 1, 1, 1, 1, 1, 1, 1)
9
def mysum(*nums):
return sum(nums)
mysum(1, 2, 3, 4)
10
def foo(fixed, *args):
print(fixed)
print(args)
foo("hello", 2, 3, 41)
hello (2, 3, 41)
def foo(*args, fixed): # not allowed! variable number of arguments have to come last!
print(fixed)
print(args)
foo(1, 2, 3, "hello")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-45-cf5e6215f4d8> in <module> ----> 1 foo(1, 2, 3, "hello") TypeError: foo() missing 1 required keyword-only argument: 'fixed'
values = [1, 2, 3, 4, 4]
mysum(*values) # while calling * indicates individual values from the list
14
mysum(values)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-47-504e77c64afa> in <module> ----> 1 mysum(values) <ipython-input-40-7977d9df90be> in mysum(*nums) 1 def mysum(*nums): ----> 2 return sum(nums) TypeError: unsupported operand type(s) for +: 'int' and 'list'
tables = [[i*j for i in range(1, 11)] for j in range(1, 5)]
tables
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30], [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]]
tables[0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
tables[0][2]
3
tables[1][2]
6
tables[2][2]
9
tables[3][2]
12
def column(data2d, col):
rowcount = len(data2d)
return [data2d[r][col] for r in range(rowcount)]
column(tables, 2)
[3, 6, 9, 12]
column(tables, 3)
[4, 8, 12, 16]
def transpose(data2d):
colcount = len(data2d[0])
return [column(data2d, c) for c in range(colcount)]
transpose(tables)
[[1, 2, 3, 4], [2, 4, 6, 8], [3, 6, 9, 12], [4, 8, 12, 16], [5, 10, 15, 20], [6, 12, 18, 24], [7, 14, 21, 28], [8, 16, 24, 32], [9, 18, 27, 36], [10, 20, 30, 40]]
for item in tables:
print(item)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
for row in tables:
print(row)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
def column(data2d, col):
return [row[col] for row in data2d]
column(tables, 2)
[3, 6, 9, 12]
first = [1, 2, 3]
second = ("one", "two", "three")
for x, y in zip(first, second):
print(x, y)
1 one 2 two 3 three
list(zip(*tables))
[(1, 2, 3, 4), (2, 4, 6, 8), (3, 6, 9, 12), (4, 8, 12, 16), (5, 10, 15, 20), (6, 12, 18, 24), (7, 14, 21, 28), (8, 16, 24, 32), (9, 18, 27, 36), (10, 20, 30, 40)]
a = [1, 2, 3, 4, 5]
b = second = ("one", "two", "three")
list(zip(a, b))
[(1, 'one'), (2, 'two'), (3, 'three')]
a = [1, 2, 3, 4, 5]
b = second = ("one", "two", "three")
c = ["I","II","III","IV"]
list(zip(a, b, c))
[(1, 'one', 'I'), (2, 'two', 'II'), (3, 'three', 'III')]
list(zip(*tables))
[(1, 2, 3, 4), (2, 4, 6, 8), (3, 6, 9, 12), (4, 8, 12, 16), (5, 10, 15, 20), (6, 12, 18, 24), (7, 14, 21, 28), (8, 16, 24, 32), (9, 18, 27, 36), (10, 20, 30, 40)]
*[1, 2, 3, 4] --> 1, 2, 3, 4,
zip(*[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]])
zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
cylinder_volume(radius=1, height=4)
12.56
def foo(pos1, pos2,*args, **kwargs):
print(pos1, pos2)
print(args)
print(kwargs)
foo("a","b", 1, 2, 3,4 , key1="val1",key2="val2" , key3="val3")
a b
(1, 2, 3, 4)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
def make_contact(name, number, **customfields):
contact = {"name":name, "phone":number}
for key, value in customfields.items():
contact[key] = value
return contact
make_contact("vikrant", 9898540986, group="teacher", place="kudawale", profession="python trainer")
{'name': 'vikrant',
'phone': 9898540986,
'group': 'teacher',
'place': 'kudawale',
'profession': 'python trainer'}
make_contact("gunjan", 9898540986, group="friend", hostelblock="H", degree="B.E")
{'name': 'gunjan',
'phone': 9898540986,
'group': 'friend',
'hostelblock': 'H',
'degree': 'B.E'}
f = lambda x: x*x
f(5)
25
add3 = lambda x, y, z: x+y+z
add3(2, 3, 46)
51
records = [
("A", 34, 5.6),
("B", 30, 6.0),
("C", 36, 5.8)
]
sorted(records, key= lambda r: r[1])
[('B', 30, 6.0), ('A', 34, 5.6), ('C', 36, 5.8)]
sorted(records, key= lambda r: r[2])
[('A', 34, 5.6), ('C', 36, 5.8), ('B', 30, 6.0)]
problems
repeat which takeks function as argument and , initial value , n an integer. it repeated calling of function n times.>>> repeat(lambda x: 2*x, 1, 5)
32
twice(twice(twice(twice(twice(1)))))
compose which composed two functions>>> f = lambda x:2*x
>>> g = lambda x:x*x
>>> fg = compose(f, g)
>>> fg(2) ## f(g(2))
8