def twice(x):
return 2*x
def twice_(x):
print(2*x)
twice(5)
10
twice_(5)
10
x = twice(5)
x
10
y = twice_(5)
10
print(y)
None
Any function that does not have return statement, it will return None
twice(twice(5))
20
twice_(twice_(5)) # twice_(None)
10
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-117f8e4344b4> in <module> ----> 1 twice_(twice_(5)) <ipython-input-1-e77584a89e94> in twice_(x) 3 4 def twice_(x): ----> 5 print(2*x) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
2*None
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-ebf0962da868> in <module> ----> 1 2*None TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
def table(n):
"""generates a table of n
"""
t = []
for i in range(1, 11):
t.append(i*n)
return t
table(3)
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
table(13)
[13, 26, 39, 52, 65, 78, 91, 104, 117, 130]
def table(n):
"""generates a table of n
"""
return [i*n for i in range(1, 11)]
table(14)
[14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
newitems = []
for item in items:
newitems.append(do(item))
this for loop can be transformed to following list comprehension
[do(item) for item in items]
def fatctors(n):
fac = []
for i in range(1, n+1):
if n%i==0:
fac.append(n)
return fac
def factors(n):
return [i for i in range(1, n+1) if n%i==0]
factors(10)
[1, 2, 5, 10]
newitems = []
for item in items:
if cond(item):
newitems.apppend(do(item))
[do(item) for item in items if cond(item)]
tables = [table(i) for i in range(1, 6)]
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], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]]
tables[0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
tables[1]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Bonus problem
From 2D list, extract nth column. ```
list2d = [[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], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]] column(list2d, 2) [3, 6, 9, 12, 15]
Make use above column function to write a function to transpose a 2D list ```
sum([n for n in range(1, 1000) if n%13==0 or n%17==0])
64915
def add(x, y):
return x+y
add
<function __main__.add(x, y)>
print(add)
<function add at 0x7f7bc5846af0>
def sumofsquares(a, b):
return square(a)+square(b)
def square(a):
return a*a
def cube(a):
return a**3
def sumofcubes(x, y):
return cube(x) + cube(y)
def sumof(x, y, func):
return func(x) + func(y)
sumof(3, 4, square)
25
sumofsquares(3, 4)
25
sumofcubes(3, 4)
91
sumof(3, 4, cube)
91
words = ["one", "two", "three", "four", "five"]
numbers = [34, 23, 43, 42, 32, 43]
max(numbers)
43
max(words) # will return last item by arranging the text in dictionary order
'two'
max(words, key=len)
'three'
len("one")
3
len("two")
3
len("three")
5
len("four")
4
len("five")
4
records = [
("Alice", 98),
("Alex", 97),
("Elsa", 100),
("Elvis", 95),
("David", 70),
("Donald", 75)
]
max(records)
('Elvis', 95)
def get_score(record):
return record[1]
max(records, key=get_score)
('Elsa', 100)
sorted(records, key=get_score)
[('David', 70),
('Donald', 75),
('Elvis', 95),
('Alex', 97),
('Alice', 98),
('Elsa', 100)]
min(records, key=get_score)
('David', 70)
import os
os.listdir() # all the files in current directory
['index.html', 'Lecture6-Comprehensions-1.ipynb', 'presenty5.txt', 'sample.html', 'presenty3.txt', 'Welcome.html', 'push', 'Lecture4-Working_With_Data.html', 'joy.py', 'presenty6.txt', 'Lecture2-Quick_Tour.ipynb', 'Lecture7-Recap.html', 'sample.ipynb', '#presenty4.txt#', '__pycache__', 'Untitled.html', 'Lecture4-Working_With_Data.ipynb', 'Lecture5-Comprehensions.html', 'Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb', 'index.ipynb', 'Lecture8-Functions.ipynb', 'Lecture7-Recap.ipynb', 'presenty5.txt~', 'Lecture6-Comprehensions-1.html', 'test.html', 'Lecture5-Comprehensions.ipynb', 'Lecture6-Recap.html', 'Lecture3-Quick_Tour.ipynb', 'Lecture1-Fundamental_ideas_of_programming.ipynb.html', 'Lecture8-Functions.html', 'Makefile', 'Lecture2-Quick_Tour.html', '.ipynb_checkpoints', 'Lecture3-Quick_Tour.html', 'presenty7.txt']
os.listdir("/home/vikrant/trainings/")
['2018', 'nakul', 'day5.org', 'hello.py', 'day5.org~', '__pycache__', '2020', 'trainingvenv', '2021', '2019', '2017', 'indexdata.xlsx']
os.path.exists("index.html")
True
os.path.exists("./Untitled.ipynb")
False
os.path.getsize("index.html")
571776
max(os.listdir(), key=os.path.getsize)
'Lecture1-Fundamental_ideas_of_programming.ipynb.html'
def get_score(record):
print("*", record)
return record[1]
max(records, key=get_score)
* ('Alice', 98)
* ('Alex', 97)
* ('Elsa', 100)
* ('Elvis', 95)
* ('David', 70)
* ('Donald', 75)
('Elsa', 100)
records
[('Alice', 98),
('Alex', 97),
('Elsa', 100),
('Elvis', 95),
('David', 70),
('Donald', 75)]
max(records)
('Elvis', 95)
max([1, 2, 3, 4, 5, 6])
6
def mylen(w):
print("*", w, len(w))
return len(w)
max(words, key=mylen)
* one 3 * two 3 * three 5 * four 4 * five 4
'three'
r = ("name", 45)
r[0]
'name'
r[1]
45
records3 = [
("A", 32, 3.5),
("B", 20, 5.5),
("C", 35, 5.4)
]
def get_field1(r):
return r[1]
def get_field2(r):
return r[2]
sorted(records3, key=get_field1)
[('B', 20, 5.5), ('A', 32, 3.5), ('C', 35, 5.4)]
sorted(records3, key=get_field2)
[('A', 32, 3.5), ('C', 35, 5.4), ('B', 20, 5.5)]
problem
8/(1*3) + 8/(5*7) + 8/(9*11) + ....
def sumofterms( start, end, step, term):
s = 0
for n in range(start, end, step):
s += term(n)
return s
def piseries(n):
return 8/((4*n-3)*(4*n-1))
def piseries1(n):
return 8/(n*(n+2))
sumofterms(1, 10000, 4, piseries1)
3.141392653591789
sumofterms(1, 1000, 1, piseries)
3.1410921531206317
max(1, 2, 3, 4) # arguments
4
max(1, 2) # two arguments
2
max([1, 2, 3, 4, 5]) # one argument
5
Bonus problem
From 2D list, extract nth column. ```
list2d = [[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], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]] column(list2d, 2) [3, 6, 9, 12, 15]
Make use above column function to write a function to transpose a 2D list ```