Introduction-to-programming

A single repo for introduction to programming in X

View on GitHub

Inbuilt Functions

Map

syntax

r = map(func, seq)
#takes each element in seq 
#applies func function to them
#inserts them to r

implementation


#for a single list involved
a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21']
dt=list(map(str.split, a)) #split function is applied to each element and it returns a list corresponding to each element
print(dt)

#for multiple lists involved
a = [1,2,3,4] 
b = [17,12,11,10]
print(list(map(lambda x,y:x+y, a,b)))
#goes through each element of a,b 
#adds them correspondingly till one of the lists end

Output

[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']]
[18, 14, 14, 14]

Filter

Syntax

filter(filter_fnc,list)

implementation

def fn(x):
	return (x<0) #(x<0) is condition automatically returns true or false
number_list = range(-10, 10)
less_than_zero = list(filter(fn, number_list))
print(less_than_zero)

Output

[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

Sort/Sorted

syntax

list.sort()
sorted(container)#sort in ascending order
sorted(list,reverse=True)#sort in descending order
sorted(list,key=<insert the key>)#sort in ascending order of the key set

implementation


a = [5, 2, 3, 1, 4]
a.sort() #sort() function is specific to lists
print(a)

chir={2: 'D', 1: 'B', 4: 'B', 3: 'E', 5: 'A'}# declaring a dictionary
for ind in sorted(chir): #sorted is general approach, although return type is a list
	print("{}:{}".format(ind,chir[ind]),end=" ")
print()
for ind in sorted(chir,reverse=True): #note the order is descending of the keys
	print("{}:{}".format(ind,chir[ind]),end=" ")
def gv(key):
	return chir[key];
print()
for ind in sorted(chir,key=gv,reverse=True): #note the order is descending of the values of each key
	print("{}:{}".format(ind,chir[ind]),end=" ")

Output

[1, 2, 3, 4, 5]
1:B 2:D 3:E 4:B 5:A 
5:A 4:B 3:E 2:D 1:B 
3:E 2:D 1:B 4:B 5:A

max/min

implementation


list = [1, 4, 3, 5,9,2]
print(max(list)) #returns maximum value of list
print(min(list)) #returns minimum value in the list
print(max(list[2:-2])) #returns max value combined with slicing 
print(min(list[4:])
print(max(4,2,3,5))
print(min(7,2,1,8,4))

Output

9
1
5
9
5
1

Reduce

implementation


#Normal way to find product of elements in a list
product = 1
list = [1, 2, 3, 4]
for num in list:
    product = product * num

#Using reduce function to do so
from functools import reduce
def func(x,y):
	return x*y
product = reduce(func, [1, 2, 3, 4])

Output

24
24