Experiment No.
Aim: Write a program for:
1. Python to convert decimal to binary, octal and hexadecimal.
2. Import the Calendar
3. Calculator using function
4. To print age and name
5. Keyword Arguments
6. Lambda function
7. Global Variable
8. Variable Length
Software used: Google Collab
Theory -
In Python, a function is a block of reusable code that performs a specific task. Functions
provide modularity and organization to your code by allowing you to break it into
smaller, more manageable pieces.
● Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
● Any input parameters or arguments should be placed within these parentheses.
You can also define parameters inside these parentheses.
● The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
● The code block within every function starts with a colon (:) and is indented.
● The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as
return None.
Syntax:
def my_function():
print("Hello")
my_function()
Calling a Function
To define a function, use the def keyword to give it a name, specify the arguments it
must receive, and organize the code block.
Function Arguments
The following are the types of arguments that we can use to call a function:
1. Default arguments
2. Keyword arguments
3. Required arguments
4. Variable-length arguments
1) Default Arguments
default argument is an argument that assumes a default value if a value is not provided
in the function call for that argument. The following example gives an idea on default
arguments, it prints default age if it is not passed −
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
2) Keyword Arguments
Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter
name.
This allows you to skip arguments or place them out of order because the Python
interpreter is able to use the keywords provided to match the values with
parameters. You can also make keyword calls to the printme() function in the
following ways −
# Function definition is here
def printme( str ):
print str
return;
# Now you can call printme function
printme( str = "My string")
3) Required Arguments
Required arguments are the arguments passed to a function in correct positional
order. Here, the number of arguments in the function call should match exactly with
the function definition.
To call the function printme(), you definitely need to pass one argument,
# Function definition is here
def printme( str ,int ):
#This prints a passed string into this function
print str
return;
# Now you can call printme function
printme("HELLO" 3)
4) Variable-Length Arguments
We can involve unique characters in Python capabilities to pass many contentions.
However, we need a capability. This can be accomplished with one of two types of
characters:
"args" and "kwargs" refer to arguments not based on keywords.
Return Statement
When a defined function is called, a return statement is written to exit the function and
return the calculated value. The statement return [expression] exits a function,
optionally passing back an expression to the caller. A return statement with no
arguments is the same as return None. The return statement can be an argument, a
statement, or a value, and it is provided as output when a particular job or function is
finished. A declared function will return an empty string if no return statement is
written.
Syntax:
return < expression to be returned as output >
Example:
def multiplyNum(num1):
return num1 * 8
result = multiplyNum(8)
print(result)
# Output: 64
Lambda Function
Lambda functions are similar to user-defined functions but without a name. They're
commonly referred to as anonymous functions.
Lambda functions are efficient whenever you want to create a function that will only
contain simple expressions – that is, expressions that are usually a single line of a
statement. They're also useful when you want to use the function once.
lambda is a keyword in Python for defining the anonymous function.
Lambda functions have exactly one line in their syntax:
lambda [argument1 [,argument2... .argumentn]] : expression
Example:
# Python code to demonstrate lambda functions
# Defining a function
lambda_ = lambda argument1, argument2: argument1 + argument2;
# Calling the function and passing values
print( "Value of the function is : ", lambda_( 20, 30 ) )
print( "Value of the function is : ", lambda_( 40, 50 ) )
Output:
Value of the function is : 50
Value of the function is : 90
Variable Length
A variable's scope refers to the program's domain wherever it is declared. A capability's
contentions and factors are not external to the characterized capability. They only have
a local domain as a result.
The length of time a variable remains in RAM is its lifespan. The lifespan of a function
is the same as the lifespan of its internal variables. When we exit the function, they are
taken away from us. As a result, the value of a variable in a function does not persist
from previous executions.
Example:
# Python program demonstrate scope and lifetime of variables
# *args with first extra argument
def myFun(x,y, *argv):
print ("First argument :", x)
#print ("Second argument :", y)
for i in argv:
print("Next argument through *argv :",i)
myFun('Hello', 'Welcome', 'to','Atharva', 'Engineering', 'College')