Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions EnumerateFunction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# We user enumerate function with for loop to track position of our
# in iterable

# How we can do this enumerate function
names=['abc','abcdef','xyz']
pos=0
# print 0--> 'abc' ...
for name in names:
print(f'{pos}--> {name}')
pos =+1



# with enumerate function
print("\nwith Enumerate function...")
for position,name in enumerate(names):
print(f'{position}-->{name}')


#task
def findIndex(str_list,str):
for index,el in enumerate(str_list):
if el == str:
return index
return -1

print( findIndex(
['abc','abcdef','xyz'],'xyz'
))


25 changes: 25 additions & 0 deletions MapFunc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# map function

numbers =[1,2,3,4,5]

# creates the list of nsquare of each elemnt list

def square(a):
return a*a

squares= list( map(square,numbers))

print(squares)

# we can also lamda function

squares2=list(
map( lambda a: a*a , numbers)
)

print(squares2)

# by list comprehasion

squares3= [el*el for el in numbers]
print(squares3)
45 changes: 45 additions & 0 deletions args_intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# make flexible functions

# * operator
# *args

def total(a,b):# total function can tak only 2 arguments
return a+b

print(
total(3,4)
)


# * operator converts the all parameters into one tuple(3,4,5,5,6).
def all_total(*args):
return sum(args)

print(
all_total(2,3,4,5,5,6)
)

# it can also work like rest operator in js
def all_total1(num,*args):
print(
"Value of num is:",num
)
return sum(args)+num

print(
all_total1(2,3,4,5,5,6)
)


# * also work as spread operator like in js
# e.g. : *[2,3,4,5] -> 2,3,4,5 unpacking of elements of the list


l= [1,2,3,0,4,5]
t= (1,2,3,4,5)
def sum_all(*args):
return sum(args)

print(
sum_all(*t) # here * works as spread operator
)
53 changes: 53 additions & 0 deletions filterFunc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# filter function

numbers =[1,2,3,4,5,4,6,8,9,10]

#create a list evens[] that contains the even number only

def is_even(a):
return a%2==0

# creating list using filter( function , list/tuple)
evens =list(
filter(is_even,numbers)
)

print(evens)

#we can use the lambda expression
print("\nusing lambda expression")
even = list(
filter(lambda a: a%2==0,numbers )
)

print(even)


print("\nusing list compreshion")
even1= [el for el in numbers if el%2==0]
print(even1)



########################### Iterable and Iterater
# list tuple dictionary stings are iterable
# when a for loop runs it calls iter function that converts the iterable-> iterator

numbers_iter= iter(numbers) # list iterator object type
#then next function next(numbers_iter) is called for 1st item , 2nd item ,3rd item so on...

print(
next(numbers_iter),
next(numbers_iter),
next(numbers_iter),
next(numbers_iter),
next(numbers_iter),
next(numbers_iter)
)

#filter function also return a iterator means next function can be called directly
print(filter(lambda a: a%2==0,numbers ))

print(
next( filter(lambda a: a%2==0,numbers ) )
)
39 changes: 39 additions & 0 deletions kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# kwargs -> keyword arguments
# **kwargs (double star operator)

#kwargs as parameter
# ** double star opeartor take key value pari as argument and convert it into the
# dictionary
## it is like dict(key1=value1,key2=value2) function which converts the parameter into the dictionary
def func(**kwargs):
print(kwargs)
print(type(kwargs))

for key in kwargs:
print(f"{key}:{kwargs[key]}")

func(first_name="Kailash",middle_name="Kumar",last_name="Mandal")

# dictionary unpacking
dic={
"name":"Kailash",
"age":22
}

#use ** operator to split the dictionary key value pairs
func( **dic)

## all types parameters

# parameters
# *args
# default parameters
# **kwargs

def funct(name , *args,last_name="unknown",**kwargs):
print(name)
print(args)
print(last_name)
print(kwargs)

funct("Karan",1,2,3,last_name="Mandal",a=1,b=2 )
29 changes: 29 additions & 0 deletions lambdaExpression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# lambda expression (anonymous function )

# normal function definition
def add(a,b):
return a+b


# lambda function definition
add2 = lambda a,b : a+b

print( add2(2,3))

# lambda expression is used in built in function

multiply = lambda a,b : a*b

print( multiply(2,3))

isEven = lambda a : a%2 ==2

print(isEven(67))

func = lambda s : True if len(s) > 5 else False

print( func("Karan"))

fun = lambda s : len(s)>5

print(fun("karann"))
58 changes: 58 additions & 0 deletions zip_function1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# zip function
user_id=['user1', 'user2', 'user3']
names=['Karan','Kailash','Rohit']

# use zip function to combine the name and id
# zip function return the zip object in tuples

# ('user1', 'Karan'), ('user2', 'Kailash').. which is an iterator
# we can convert iterator(zip object ) into list

print(

list(
zip(user_id,names)
)
)

# w# we can also convert it into dictionary(if only two items tuple are present)
print("\nDictionary format:\n")
print(
dict(
zip(user_id,names)
)
)

last_names=['kumar','singh','rajput']

# zip function also work with three lists
# but with three list dictionary can not be created
print('\nzip function with three lists\n')
print(
list(
zip(
user_id,
names,
last_names
)
)
)

## more on zip
l1=[1,3,5,7]
l2=[2,4,6,8]

new_list =[max(pair) for pair in zip(l1,l2)]

print(new_list)

l=[(1,2),(3,4),(5,6),(7,8)]
print(*l)
print(
list(
zip(*l)
)
)

l3,l4= list( zip(*l) )
print(l3,l4)