Lambda and Recursive functions in python

By // No comments:

Lambda function in python:

  • Normal functions have their names and they are defined using the def keyword.An anonymous functions do not have names and they are defined using the lambda keyword.
Syntax:-
lambda arguments:expression
  • An anonymous function defined using lambda can take any number of arguments but can return only one value.
  • Lambda functions can be used wherever function objects are required usually.It is used as an argument to other functions.
  • An anonymous function contains a small piece of code.
3

Function concepts in python Part 2

By // No comments:

We have already explained some basic features of functions in our previous lectures.Before learn this part ,you have to learn function concepts in python part 1.

Types of Arguments:-

There are four types of arguments in python functions as given below:-

  1. Required arguments or Positional arguments.
  2. Keyword arguments
  3. Default arguments
  4. Variable Length arguments
3

Function Concepts in Python part 1

By // No comments:

What are functions:-

  • Python function is a block of code that performs a specific task.
  • Python function allows us to divide a large program into the basic building block.
  • Python function helps to break the program code  into smaller part.It is used to avoid the repetition of the code.
Types of functions

There are two types of the functions in python.

  1. Built-in Functions
  2. User defined functions
 1.) Built-in functions:- The Built-in functions are those functions which are predefined in python.I have already explained more built-in functions in our previous lectures.e.g. len(),min(),max(),range() ,print() etc.
2.) User defined functions:-The user defined functions are those functions which are defined by user for any specific tasks in python.e.g. show(),display(),calculate() etc.
Advantage of functions:-
There are two main advantage of functions as given below:-
  • Functions help us to divide our program into multiple tasks.For each task we can define a separate functions.This makes the code modular.
  • Functions provide us a reuse mechanism.
Properties of functions
  • The body of the function must be indented suitably.
  • A function can be called any number of times.
fun1()               #first function call
fun2()               #second function call
fun3()               #third function call
  • When a function is called ,control is transferred to the function and its statements are executed and control is returned to place from where the call originated.
  • Built-in Function Name always use lowercase characters.
  • User-defined function Name can be used lower or uppercase characters
  • Function names connect multiple words using  hyphen sign (-).

Creating a python Function:-

Python  provides the def keyword to define the function.

Syntax:-

def fun (parameter):
    Block of codes
    return

Descriptions:-

  • The def keyword is used to define the function in python.
  • The identifier rules must be follow to write the name of the function in python.
  • A function accepts the parameters(arguments) according to the requirements of the function.The parameters or arguments can be optional.
  • The function block is started with colon : and block of codes must be at the same indentation.
  • The function may have a return statement.It is used to return the value to the calling function.
  • A function can have only one return statement in python.It can be optional.
Arguments in Function:-
A arguments are the type of information which can be passed into the function.This comes under small brackets and separate with a comma.
e.g.
#define a function
def sum (a,b):
    result=a+b
    print ("The sum of two numbers is:",result)
#callig  the function
sum(20,40) 
Output:-
The sum of two numbers is: 60
>>> 
Function Calling:

  • We first create function after that we can call it from another location.
  • A function must be defined first ,after that call it otherwise python interpreter gives an error.
e.g.
#define a function
def fun ():
    print("Welcome to MNT LAB")
#function calling    
fun()   
Output:-

Welcome to MNT LAB

>>> 

How to use return statement in function:-

The return statement is used at the end of the function and returns the result to the calling function.

Syntax:-

return [expression]

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    return result
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
print("Multiplication of three number is:",multiplication(x,y,z))

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

>>> 

Description:-

In above example,i have defined a multiplication function with three arguments.It has a statement result=a*b*c , which is used to compute the a,b and c  values and stored it in result variable.This result value is return by this function to the calling function.Here i have taken these three value by user as shown in above code.

Calling the Function Without Return statement:-

We can call the python functions without return statement as given below:-

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    print("Multiplication of three number is:",result)
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
multiplication(x,y,z)

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

Welcome to MNT LAB

>>> 

Call by reference in Python

The Memory of Call by reference is to passing the actual value as an argument in the function.All the functions are called by reference,Means all the changes made to the reference inside the function revert back to the original value referred by the reference. 

e.g. 

Passing Mutable object(list1) in program

list1=[10,20,30,40]
def modify_list(list1):
    list1.append(50)
    list1.append(60)
    list1.append(70)
    print("The list inside the function is:",list1)
# calling the function
modify_list(list1)
print("The list outside the function is:",list1)

Output:-

The list inside the function is: [10, 20, 30, 40, 50, 60, 70]

The list outside the function is: [10, 20, 30, 40, 50, 60, 70]

>>>

e.g. 

Passing Immutable object(string) in program

string="Hello Friends "
#define the function
def modify_string(str1):
    str1=str1+"Welcome to MNT LAB"
    print("print the string inside function is: ",str1)
#calling the function
modify_string(string)    
print("print the function outside the function is ",string)

Output:-

print the string inside function is:  Hello Friends Welcome to MNT LAB

print the function outside the function is  Hello Friends 

>>> 

Watch Complete Lecture Video:-

For More...

  1. List concepts in python part 2
  2. How to save image in database and display in picture box.
  3. For oop concepts in python with examples
  4. WCF concepts in .net
  5. Algorithm concepts in python with examples

3

Dictionary concepts in python

By // No comments:

What is Dictionary:

  • Dictionary is a collection of key-value pairs.It is used to store the data in a key-value pairs.It is used to store the data in key-value format.
  • Dictionaries are indexed by keys.
  • Dictionary is the data type in python.
  • Dictionaries are mutable.So we can perform add/delete/modify operations on a dictionary.
  • The keys must be a single element in dictionary.
  • The values can be any type such as list,tuple,integer etc.
  • Dictionaries are also known as maps or associative arrays.
  • The keys in a dictionary must must be unique and immutable.So number,strings and tuples can be used as keys.
  • Though key values are unique,different keys may have same value.
3

Set Concepts in Python

By // No comments:
What are sets
  • A python set is the collection of unordered elements.Hence order of insertion is not same as the order of access. 
e.g.
set1=set() #create an empty set
set1={10,20,30,40,50}
print(set1)
Output:-
{40, 10, 50, 20, 30}
>>> 
  • A set does not contain duplicate items like list and tuple.
e.g.
#set1 contains duplicate items
set1={10,20,20,10,50,60,60,70,80,30,50}
print(set1)
Output:-
{70, 10, 80, 50, 20, 60, 30}
>>> 
  • A set is mutable (changeable),Means we can change the items after created but set items must be immutable as shown in below examples.
3

Tuple concepts in python

By // No comments:
What is Tuple:-
  • Tuple is usually a collection of heterogeneous objects enclosed within ()(small bracket).
  • A tuple is the collection of comma (,)separated values enclosed with small brackets.
  • The parentheses (small bracket) are optional in tuple
    .
e.g.
tuple1=(10,20,180,105)
tuple2=('Horse','Lion','Cow')
tuple3=("MNT LAB")
tuple4=("MNT LAB",)
print(type(tuple1))
print(type(tuple2))
print(type(tuple3))
print(type(tuple4))
Output:-
<class 'tuple'>
<class 'tuple'>
<class 'str'>
<class 'tuple'>
>>> 
Properties of Tuple:-
There are some properties of tuple as given below:-
  • An empty tuple can created as given below.
3

List concepts in python part 2

By // No comments:
There are some basic list operations in python as given below.
  • Lists are mutable but strings are immutable.
e.g.
animals=['cow','horse','lion','tiger','deer']
numbers=[1,2,3,4,5,6,7,8,9,10]
#Replace list items
animals[4]='dog'
numbers[2:5]=[30,40,50]
print(animals)
print(numbers)
#delete items from 7 to 9 in numbers list
numbers[6:9]=[]
print(numbers)
#clear all items in the numbers list
numbers[:]=[]
print(numbers)
Output:-
['cow', 'horse', 'lion', 'tiger', 'dog']
[1, 2, 30, 40, 50, 6, 7, 8, 9, 10]
[1, 2, 30, 40, 50, 6, 10]
[]
>>> 
  • The Repetition operator is used to repeat  the elements in the list multiple times as given below.
e.g.
list1=[10,20,30,40,50]
list2=list1*2
print(list2)
Output:-
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
>>> 
  • The concatenation operator is used to concatenate the one list with other list.
e.g.
list1=[10,20,30,40,50]
list2=['a','b','c','d','e','f']
list3=list1+list2
print(list3)
Output:-
[10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e', 'f']
>>> 
  • The membership operator returns True if a particular element exists in a list otherwise False.
e.g.
list=['a','e','i','o','u']
print('a' in list)
print('o' in list)
print('p' in list)
Output
True
True
False
  • A list can be iterated by using for loop as given below.
e.g.
list=['Ram',20,3.5,'lion','neha']
for i in list:
    print(i)
    #print(i,end=" ")
Output:
Ram
20
3.5
lion
neha
>>> 

There are some basic operations can be performed on a list.
  • Create a list
e.g.
list=['Ram',20,3.5,'lion','neha']
  • Concatenation (addition) of two more lists.
e.g.
list1=[10,20,30,40,50]
list2=[60,70,80,90,100]
list=list1+list2
print(list)
Output:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> 
  • del() function is used to delete the elements in the list.
e.g.
list1=[10,20,30,40,50,60,70,80,90,100]
#delete the 3rd index value from the list
del(list1[3])
print(list1)
#delete the 6th index to last index from the list
del(list1[6:])
print(list1)
#delete the whole list
del(list1[:])
print(list1)
Output:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
[10, 20, 30, 50, 60, 70]
[]
>>> 
  • len() function is used to calculate the number of items in given list.
e.g.
list1=[10,20,30,40,50,60,70,80,90]
a=len(list1)
print("Lenght of given list is = ",a)
Output:
Lenght of given list is =  9
>>> 
  • list() function is used to convert the string to a list.
e.g.

str="AMERICA"
a=list(str)
print("Convert the string value to the list value: ",a)
Output:
Convert the string value to the list value:  ['A', 'M', 'E', 'R', 'I', 'C', 'A']
>>> 
  • max() function is used in python to return the maximum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Maximum value of list is=",max(str1))
print("Maximum value of list is=",max(str2))
Output:-
Maximum value of list is= R
Maximum value of list is= 8
>>> 
  • min() function is used in python to return the minimum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Minimum value of list is=",min(str1))
print("Minimum value of list is=",min(str2))
Output:-
Minimum value of list is= A
Minimum value of list is= 1
>>> 
  • sorted() function is used to return the sorted list without any change in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[70,50,60,40,20,80,30,40]
print("Sorted value of the list is=",sorted(str1))
print("Sorted value of the list is=",sorted(str2))
Output:-
Sorted value of the list is= ['A', 'A', 'C', 'E', 'I', 'M', 'R']
Sorted value of the list is= [20, 30, 40, 40, 50, 60, 70, 80]
>>> 
  • sum() function is used to return the sum of all the list values.
e.g.
str1=[70,50,60,40,20,80,30,40]
print("sum of all list value is=",sum(str1))
Output:-
sum of all list value is= 390
>>> 
  • Comparison between two list is possible.Comparison is done item by item till there is mismatch.
e.g.
a=[1,5,10,15,20]
b=[1,5,10,30,3]
if(a<b):
    print("a is less than b")
elif(a==b):
    print("a is equal to b") 
else:
     print("b is less than a")
Output:-
a is less than b
>>> 

List Methods:- 
List methods are accessed using the syntax list.function().
There are important methods used in list operations as given below:-
  • list.append():-This is used to add the new items at the end of the list.This Method takes only one argument.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.append(90)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60, 70, 90]
>>> 
  • list.remove():-This method is used to delete the item from the list.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.remove(40)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 50, 60, 70]
>>> 
  • list.pop():-This method is used to remove the last item from the list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
list.pop()
print(list)
#delete the 4th index value in the list
list.pop(4)     
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 60]
>>> 
  • list.insert():-This method is used to insert the element at the specified position in the list.This method takes two argument.first argument denote the index value and second argument denote item which you want to add in the given list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
#insert the item at the 4th index in the list
list.insert(4,122)
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 122, 50, 60, 70]
>>> 
  • list.reverse():-This is used to reverse the element in the list.
e.g.
list=[2,4,6,8,10,12,14,16]
list.reverse()
print(list)
Output:-
[16, 14, 12, 10, 8, 6, 4, 2]
>>> 
  • list.sort():-This is used to sort the items in the list.
e.g.
list=[70,20.5,40,50,60,16]
list.sort()
print(list)
Output:-
[16, 20.5, 40, 50, 60, 70]
>>>  
  • list.count():-This is used to return the number of times element appears in the list.
e.g.
list=[16,40,40,50,60,70,40,20,40]
a=list.count(40)
print("Above given number present in the list: ",a)
Output:-
Above given number present in the list:  4
>>> 
  • list.index():-This used to find the index of the particular item in the list.
e.g.
list=[16,40,50,60,70,20,40]
a=list.index(70)
print("70 is present in the list at index : ",a)
Output:-
70 is present in the list at index :  4
>>> 
List Varieties:-
There are some list varieties as given below:-
  • It is possible to create new list from another.
e.g.
list1=[16,40,50,60,70,20,40]
list2=list1
print(list1)
print(list2)
Output:-
[16, 40, 50, 60, 70, 20, 40]
[16, 40, 50, 60, 70, 20, 40]
>>> 
Note:-list1 and list2 are pointing the same list.
  • It is possible to create a list of lists.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,4,6,8,10,12,14]
c=[a,b]
print(c[0][4],c[1][6])
#c[0][4]-> 0th list(first list) of 4th index
#c[1][6]-> 1th list(second list) of 6th index
Output:-
9 14
>>>
  • It is possible that a list may be embedded in another list.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,a,4,6,8,10,12,14]
print("After embedded list is: ",b)
Output:-
After embedded list is:  [2, [1, 3, 5, 7, 9, 11, 13], 4, 6, 8, 10, 12, 14]
>>> 
  • It is possible to unpack a list within a list using the * operator.
e.g
a=[1,3,5,7,9,11,13]
b=[2,4,6,*a,8,10,12]
print("After unpacked list is: ",b)
Output:-
After unpacked list is:  [2, 4, 6, 1, 3, 5, 7, 9, 11, 13, 8, 10, 12]
>>> 
Program:1 
How to add items in empty list by user manually.
#declare the empty list
list=[]
num=int(input("Enter the number of elements in the list : "))
for i in range(0,num):
    #The input is taken by user and add to the list
    list.append(input("Enter the value:"))
#print the list
for j in list:
    print(j,end="")
Output:-
Enter the number of elements in the list : 10
Enter the value:1
Enter the value:2
Enter the value:3
Enter the value:4
Enter the value:5
Enter the value:6
Enter the value:7
Enter the value:8
Enter the value:9
Enter the value:10
12345678910
>>> 
Program:2
Write a program to remove the duplicate items of the list.
l1=[10,20,30,40,50,20,20,70,10,20,30]
#declare the empty list
l2=[]
for i in l1:
    if i not in l2:
        l2.append(i)

print("After remove duplicate item, list is= ",l2)
Output:-
After remove duplicate item, list is=  [10, 20, 30, 40, 50, 70]
>>> 
Program:3
Write a program to find the common element between two list.
l1=[1,3,5,7,9,11]
l2=[2,4,5,8,10,12,9]
for i in l1:
    for j in l2:
        if(i==j):
            print("The common element is: ",i)
Output:-
The common element is:  5
The common element is:  9
>>> 
For More..
Watch complete Lecture 25 Video:-


3
Powered by Blogger.