python miscellaneous

pass statement 

Python has the syntactical requirement that code blocks after if, except, def, class etc. cannot be empty. But there are scenarios where we do not want to perform any logic. In those scenarios we use pass statement as a placeholder. 

As an example, if we do not want to implement the method callMe(), we just insert a pass statement as shown below. 

def callMe(): 
pass 

callMe() 

The above program does not print any output.

break statement 

break statement is used to exit a current loop. 

The below program prints from N to the next multiple of 11. 

N = int(input()) 
for num in range(N,N+11): 
print (num, end=" ") 
if num%11 == 0: 
break 

- If the input is 11 the output is 11 
- If the input is 14 the output is 22

N = int(input()) 
for num in range(N,N+9): 
print (num, end=" ") 
if num%9 == 0: 
break

Input: 
12 
Output
12 13 14 15 16 17 18

List Comprehensions 

List comprehensions provide a concise way to create lists. 

Consider the program below which prints creates the list squares which will contain the squares of odd numbers from 1 to 20. 
The list squares will contain the following values - 1 9 25 49 81 121 169 225 289 361 

oddsquares = [] 
for num in range(1,20,2): 
oddsquares.append(num*num) 

Using List Comprehensions this can be concisely written as 

oddsquares = [num*num for num in range(1,20,2)]




x=4 
y=20 
oldlist = [i for i in range(x,y) if i%3 == 0] 
newlist = [j*3 for j in oldlist] 
for num in newlist: 
print (num, end=" ")

the output of the program is 18 27 36 45 54

rowcolarr = input().split(" ") 
rows = int(rowcolarr[0]) 
cols = int(rowcolarr[1]) 
arr = [[0]*cols for r in range(rows)] 
for r in range(rows): 
arr[r] = [int(cellvalue) for cellvalue in input().strip().split(" ")] 
cornersum = arr[0][0]+arr[0][cols-1]+arr[rows-1][0]+arr[rows-1][cols-1] 
print (cornersum)

Input: 
3 3 
1 2 3 
4 5 6 
7 8 9 

Output: 
20 

Explanation: The sum of the corner elements is = 1+3+7+9 = 20

filter function 

filter(function, list) offers an elegant way to filter out all the elements of the list, for which the function returns True. 

The below program filters all the multiples of 4 in the list numbers into the multiples list. 

numbers = [1,3,4,6,9,12,22,24,26,27,36,48,50,53] 
multiples = filter(lambda num: num%4==0, numbers) 
for m in multiples: 
print (m, end=" ") 

The output of the above program is 4 12 24 36 48

numbers = list(map(int,input().strip().split(" ")))
multiples = filter(lambda num: num%2,numbers)
for m in multiples:
    print(m, end=" ")

Input:
16 79 44 33 50 5 57 51 64 80 18 95 
Output: 
79 33 5 57 51 95


map function 

map(function, list) offers an elegant way to apply the function to all the elements in the sequence list. 
The below program multiplies all the values by 10 in the list numbers and assigns it to the list result. 

numbers = [1,21,88,4,5,100] 
result = map(lambda num: num*10, numbers) 
for r in result: 
print (r, end=" ") 

The output of the above program is 10 210 880 40 50 1000

numbers = list(map(int,input().strip().split(" ")))
result = map(lambda num:num*5, numbers)
for r in result:
    print(r,end=" ")

Input: 
10 4 18 
Output: 
50 20 90

reduce function 

reduce(function, list) offers an elegant way to apply the function to all the elements in the sequence list and finally reduce it to a single value. 
The below program finds the sum of numbers from x to y (inclusive of x and y) 

from functools import reduce 
x = int(input()) 
y = int(input()) 
print (reduce(lambda a,b: a+b, range(x,y+1))) 

- If the input is 2 and 5, then the output of the above program is 14 
- If the input is 1 and 50, then the output of the above program is 1275

from functools import reduce
numbers = list(map(int,input().strip().split(" ")))
maxnum = reduce(lambda x,y: x if  x>y else y,numbers)
    
print (maxnum)

Input: 
10 4 181 76 223 211 
Output: 
223


Comments

  1. It was a beneficial workout for me to go through your webpage. Private tutor in Florida It definitely stretches the limits with the mind when you go through very good info and make an effort to interpret it properly. I am going to glance up this web site usually on my PC. Thanks for sharing

    ReplyDelete

Post a Comment