0% found this document useful (0 votes)
4 views

Functions

Module III covers Python functions, modules, and packages, detailing function creation, calling, parameters, return values, and recursion. It also explains type conversion, including implicit and explicit conversion, and type coercion. The module emphasizes the differences between type conversion and coercion, highlighting their implications on data handling in Python.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Functions

Module III covers Python functions, modules, and packages, detailing function creation, calling, parameters, return values, and recursion. It also explains type conversion, including implicit and explicit conversion, and type coercion. The module emphasizes the differences between type conversion and coercion, highlighting their implications on data handling in Python.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Module -III

Python Functions, Modules and Packages


Module 3 9 hours
Python Functions, Modules and Packages: Function definition, calling functions,
parameters and arguments, the return statement, type conversion and coercion,
composition
of functions, Lambda function, mathematical functions, user-defined functions, Recursion,
Modules- Built-in modules, creating modules, import statement. Packages in Python -
importing modules from a package.
Python Functions
• A function is a block of code which only runs when it is called
• can pass data, known as parameters, into a function
• A function can return data as a result
• Creating a Function
• Using def keyword
Calling a Function
Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the
parentheses.
• can add as many arguments (should be separated them with a
comma)
Multiple Arguments
Arbitrary Arguments, *args
• If the number of arguments is unknown, add a * before the
parameter name
• function will receive a tuple of arguments, and can access the items
accordingly
Default Parameter Value
If we call the function without argument, it uses the default value
Passing a list as an Argument
• Can send any data types of argument to a function (string, number,
list, dictionary etc.)
• It will be treated as the same data type inside the function.
Passing a Tuple as an Argument
Passing a Dictionary as an Argument

{'breakfast': 'apple', 'Lunch': 'banana', 'Dinner': 'cherry'}


Return Values
• To let a function return a value, use the return statement:
Recursion
• Python also accepts function recursion, which means a defined
function can call itself.
Type Conversion
• Type Conversion is the process of converting the type of objects from
one data type into another as per requirement.

• Python supports various built-in type conversion functions such as


int(), long(), float(), str() etc,.
Type Conversion
• Python has two types of type conversion.
1.Implicit Type Conversion
2.Explicit Type Conversion

• In Implicit type conversion of data types in Python


• the Python interpreter automatically converts one data type to another
without any user involvement
Type Conversion -Implicit

The reason for the float value not being converted into an integer instead is due to
type promotion that allows performing operations by converting data into a wider-
sized data type without any loss of information.
Explicit Type Conversion
• In Explicit Type Conversion in Python, the data type is manually
changed by the user as per their requirement.

• With explicit type conversion, there is a risk of data loss since we are
forcing an expression to be changed in some specific data type.

• Syntax :
<required_datatype>(expression)
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
Addition of string and integer using explicit conversion
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
Explicit Type Conversion
• str() function – To convert a data Type to Strings
• There is no restriction in this function.
• It will return the string value to everything which is passed with the function
Key Points to Remember
• Type Conversion is the conversion of object from one data type to another
data type.

• Implicit Type Conversion is automatically performed by the Python


interpreter.

• Python avoids the loss of data in Implicit Type Conversion.

• Explicit Type Conversion is also called Type Casting, the data types of
objects are converted using predefined functions by the user.

• In Type Casting, loss of data may occur as we enforce the object to a


specific data type.
Type Coercion
• The automatic type conversion from one data type to another is
called as Type Coercion.
• This is also called as Implicit Type Conversion.

• The major difference between Type Conversion and Type Coercion is


that, type conversion is done manually using built-in functions where
as type coercion is done automatically.

You might also like