Introduction to Advanced Python Programming Jagdish D. Chavan Data Scientist| Emerging Industry Trainer| Certified Python, Data Science Trainer| AI,ML,DL Expert Github | Linkedin
Functional Programming ● Functional programming decomposes a problem into a set of functions. ● Ideally, Functions only take inputs and produce output, and don’t have any internal state that affects the output produced for a given input. ● Functional techniques common to many languages : such as lambda, map, reduce. *Note:- for more code & projects follow me on Linkedin | Github
Anonymous Functions or Lambdas ● Anonymous functions are the functions without name. ● Anonymous functions are not defined using ‘def’ keyword. ● They are defined using the keyword lambda hence also called as lambda functions. *Note:- for more code & projects follow me on Linkedin | Github
Syntax for Lambda function. Syntax for normal function def function_name(parameters): function_block return expression Example:- Square of Given value code def square(x): #declaring function return x*x square(9) #calling a function Output 81 *Note:- for more code & projects follow me on Linkedin | Github Syntax for Lambda Function Lambda argument_list : expression Example:- Square of Given value Code y = lambda x: x*x #declaring value = y(9) #calling a function print(value) Output 81
Syntax for Lambda function. *Note:- for more code & projects follow me on Linkedin | Github Syntax :- Lambda <argument_list> : <expression> f = lambda x: x*x ● Observe the keyword ‘lambda’. This represents that an anonymous function is being created. ● ‘X’ is argument of function followed by colon(:) representing beginning of function containing expression x*x. ● Normally, if a function returns some value, we assign that value to a variable as: y = square(9) ● but , lambda function returns a function and hence they should to assign to function as: f = lambda x:x*x ● Here ‘f’ is the function name to which lambda expression is assigned. Now if we call the functions f() as: value =f(5)
Properties of Lambda function. *Note:- for more code & projects follow me on Linkedin | Github ● We can pass n number of arguments in lambda function, but only one expression is allowed. ● They return the result implicitly, hence we should not write any ‘return’ statements in the lambda functions. ● Because a lambda functions is always represented by a function, we can pass a lambda function to another function . ● It means we are passing a function (i.e lambda ) to another function, making processing data very easy lambda function are generally used with functions like filter(), map(), or reduce().
Exercise no 1. *Note:- for more code & projects follow me on Linkedin | Github 1) Write a Python Program to create a lambda function that returns a square value of given number. 2) Write a Python Program to calculate the sum of two numbers using lambda function 3) Write a Python Program to find bigger number in two given number using Lambda Function . Note:- for more examples and answers please do visit Advance Python Programming
Using Lambda with Filter() Function. *Note:- for more code & projects follow me on Linkedin | Github ● The filter() function is useful to filter out the elements of a sequence depending on the result of a function. ● We should supply a function and a sequence to the filter() function as Syntax :-filter(function, sequence) ● function:- represent the function name that may return either True or False. ● Sequence:-represents a list, string or tuple. ● The ‘function’ is applied to every element of the ‘sequence’ and when function returns True, the element is extracted otherwise it is ignored.
Using Lambda with Filter() Function. *Note:- for more code & projects follow me on Linkedin | Github Example #write a python Program using lambda function that returns even number from the list. lst = [10,23,45,46,70,99] lst1 = list(filter(lambda x:(x%2==0),lst)) print(lst1) Output [10, 46, 70]
Using Lambda with map() Function. *Note:- for more code & projects follow me on Linkedin | Github ● map () function acts on each element of the sequence and perhaps changes the element ● The syntax of map() function is as follow Syntax:- map(function, sequence) ● The function performs specific operations on all the elements of the sequence and modified element are returned which can be stored in another sequence. Example #write a python program using lambda function that returns squares of elements in a list. lst = [1,2,3,4,5] lst1 = list(map(lambda x: x*x, lst)) print(lst1) Output [1, 4, 9, 16, 25]
Using Lambda with map() Function. *Note:- for more code & projects follow me on Linkedin | Github Exercise 1) Write a Python program to find the products of elements of two different lists using lambda function. Note:- for more examples and answers please do visit Advance Python Programming
Using Lambda with reduce() Function. *Note:- for more code & projects follow me on Linkedin | Github ● reduce () function reduces a sequence of elements to a single value by processing elemets according to a function supplied. ● Syntax :- reduce(function, sequence) Example #write a Python program using lambda function to calculate product of elements of list from functools import * lst = [1,2,3,4,5] result = reduce(lambda x,y: x*y,lst) print(result) Output 120

Advance python programming

  • 1.
    Introduction to Advanced PythonProgramming Jagdish D. Chavan Data Scientist| Emerging Industry Trainer| Certified Python, Data Science Trainer| AI,ML,DL Expert Github | Linkedin
  • 2.
    Functional Programming ● Functionalprogramming decomposes a problem into a set of functions. ● Ideally, Functions only take inputs and produce output, and don’t have any internal state that affects the output produced for a given input. ● Functional techniques common to many languages : such as lambda, map, reduce. *Note:- for more code & projects follow me on Linkedin | Github
  • 3.
    Anonymous Functions orLambdas ● Anonymous functions are the functions without name. ● Anonymous functions are not defined using ‘def’ keyword. ● They are defined using the keyword lambda hence also called as lambda functions. *Note:- for more code & projects follow me on Linkedin | Github
  • 4.
    Syntax for Lambdafunction. Syntax for normal function def function_name(parameters): function_block return expression Example:- Square of Given value code def square(x): #declaring function return x*x square(9) #calling a function Output 81 *Note:- for more code & projects follow me on Linkedin | Github Syntax for Lambda Function Lambda argument_list : expression Example:- Square of Given value Code y = lambda x: x*x #declaring value = y(9) #calling a function print(value) Output 81
  • 5.
    Syntax for Lambdafunction. *Note:- for more code & projects follow me on Linkedin | Github Syntax :- Lambda <argument_list> : <expression> f = lambda x: x*x ● Observe the keyword ‘lambda’. This represents that an anonymous function is being created. ● ‘X’ is argument of function followed by colon(:) representing beginning of function containing expression x*x. ● Normally, if a function returns some value, we assign that value to a variable as: y = square(9) ● but , lambda function returns a function and hence they should to assign to function as: f = lambda x:x*x ● Here ‘f’ is the function name to which lambda expression is assigned. Now if we call the functions f() as: value =f(5)
  • 6.
    Properties of Lambdafunction. *Note:- for more code & projects follow me on Linkedin | Github ● We can pass n number of arguments in lambda function, but only one expression is allowed. ● They return the result implicitly, hence we should not write any ‘return’ statements in the lambda functions. ● Because a lambda functions is always represented by a function, we can pass a lambda function to another function . ● It means we are passing a function (i.e lambda ) to another function, making processing data very easy lambda function are generally used with functions like filter(), map(), or reduce().
  • 7.
    Exercise no 1. *Note:-for more code & projects follow me on Linkedin | Github 1) Write a Python Program to create a lambda function that returns a square value of given number. 2) Write a Python Program to calculate the sum of two numbers using lambda function 3) Write a Python Program to find bigger number in two given number using Lambda Function . Note:- for more examples and answers please do visit Advance Python Programming
  • 8.
    Using Lambda withFilter() Function. *Note:- for more code & projects follow me on Linkedin | Github ● The filter() function is useful to filter out the elements of a sequence depending on the result of a function. ● We should supply a function and a sequence to the filter() function as Syntax :-filter(function, sequence) ● function:- represent the function name that may return either True or False. ● Sequence:-represents a list, string or tuple. ● The ‘function’ is applied to every element of the ‘sequence’ and when function returns True, the element is extracted otherwise it is ignored.
  • 9.
    Using Lambda withFilter() Function. *Note:- for more code & projects follow me on Linkedin | Github Example #write a python Program using lambda function that returns even number from the list. lst = [10,23,45,46,70,99] lst1 = list(filter(lambda x:(x%2==0),lst)) print(lst1) Output [10, 46, 70]
  • 10.
    Using Lambda withmap() Function. *Note:- for more code & projects follow me on Linkedin | Github ● map () function acts on each element of the sequence and perhaps changes the element ● The syntax of map() function is as follow Syntax:- map(function, sequence) ● The function performs specific operations on all the elements of the sequence and modified element are returned which can be stored in another sequence. Example #write a python program using lambda function that returns squares of elements in a list. lst = [1,2,3,4,5] lst1 = list(map(lambda x: x*x, lst)) print(lst1) Output [1, 4, 9, 16, 25]
  • 11.
    Using Lambda withmap() Function. *Note:- for more code & projects follow me on Linkedin | Github Exercise 1) Write a Python program to find the products of elements of two different lists using lambda function. Note:- for more examples and answers please do visit Advance Python Programming
  • 12.
    Using Lambda withreduce() Function. *Note:- for more code & projects follow me on Linkedin | Github ● reduce () function reduces a sequence of elements to a single value by processing elemets according to a function supplied. ● Syntax :- reduce(function, sequence) Example #write a Python program using lambda function to calculate product of elements of list from functools import * lst = [1,2,3,4,5] result = reduce(lambda x,y: x*y,lst) print(result) Output 120