From f1a47f06da6ec59104f6facabcd9e6f0163517b6 Mon Sep 17 00:00:00 2001 From: Kailashmandal <88301928+Kailashmandal@users.noreply.github.com> Date: Sat, 19 Nov 2022 23:39:42 +0530 Subject: [PATCH 1/6] * operator intro --- args_intro.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 args_intro.py diff --git a/args_intro.py b/args_intro.py new file mode 100644 index 0000000..521968e --- /dev/null +++ b/args_intro.py @@ -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 +) \ No newline at end of file From 0d0117145beb000eb137a006cfb5ddcf588a3362 Mon Sep 17 00:00:00 2001 From: Kailashmandal <88301928+Kailashmandal@users.noreply.github.com> Date: Sun, 20 Nov 2022 20:37:05 +0530 Subject: [PATCH 2/6] ** kwargs in python --- kwargs.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 kwargs.py diff --git a/kwargs.py b/kwargs.py new file mode 100644 index 0000000..3403231 --- /dev/null +++ b/kwargs.py @@ -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 ) \ No newline at end of file From 9ce4622a8324ee2fff6901c36ce440ce6a801788 Mon Sep 17 00:00:00 2001 From: Kailash <88301928+Kailashmandal@users.noreply.github.com> Date: Mon, 21 Nov 2022 22:53:36 +0530 Subject: [PATCH 3/6] lambda Expression in python --- lambdaExpression.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lambdaExpression.py diff --git a/lambdaExpression.py b/lambdaExpression.py new file mode 100644 index 0000000..cc4581c --- /dev/null +++ b/lambdaExpression.py @@ -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")) \ No newline at end of file From 1fa7c2a50b677e1ead58102ad9e4bdbe73855acd Mon Sep 17 00:00:00 2001 From: Kailash <88301928+Kailashmandal@users.noreply.github.com> Date: Tue, 22 Nov 2022 23:33:40 +0530 Subject: [PATCH 4/6] Map and Enumerate function in python --- EnumerateFunction.py | 31 +++++++++++++++++++++++++++++++ MapFunc.py | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 EnumerateFunction.py create mode 100644 MapFunc.py diff --git a/EnumerateFunction.py b/EnumerateFunction.py new file mode 100644 index 0000000..a232c98 --- /dev/null +++ b/EnumerateFunction.py @@ -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' +)) + + diff --git a/MapFunc.py b/MapFunc.py new file mode 100644 index 0000000..046b36e --- /dev/null +++ b/MapFunc.py @@ -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) \ No newline at end of file From f2e369bc1a3ec33714b6a07271733020cdbb7717 Mon Sep 17 00:00:00 2001 From: Kailash <88301928+Kailashmandal@users.noreply.github.com> Date: Thu, 24 Nov 2022 23:03:50 +0530 Subject: [PATCH 5/6] filter function in python --- filterFunc.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 filterFunc.py diff --git a/filterFunc.py b/filterFunc.py new file mode 100644 index 0000000..565ea1c --- /dev/null +++ b/filterFunc.py @@ -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 ) ) +) From bfb7e337982147fa2c69dcb2f2d9b359e475a5f7 Mon Sep 17 00:00:00 2001 From: Kailash <88301928+Kailashmandal@users.noreply.github.com> Date: Fri, 25 Nov 2022 18:36:11 +0530 Subject: [PATCH 6/6] Zip function in python --- zip_function1.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 zip_function1.py diff --git a/zip_function1.py b/zip_function1.py new file mode 100644 index 0000000..b1db1d7 --- /dev/null +++ b/zip_function1.py @@ -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) \ No newline at end of file