Chapter 04 Python
Chapter 04 Python
A function is a block of organized , reusable code that is used to perform some task.
• It is usually called by its name when its task need execution.
• You can also pass values to it or have it return results to you.
Convert one data type to another data type is called typecasting. Some situations, when
you want to convert the data type.
For example, you want to add two numbers in which one existing value of a variable is an
integer and the second is a string.
Then you need to use Python typecasting to convert string data type in integer before
adding.
Why and When to Use Type-casting in Python?
◦ Casting is when you convert a variable value from one type to another. That is, in Python, done with
functions like int() or float() or str(). A pattern is that you convert a number as a string into a number.
◦ The practice of converting the value of a single data type (integer, string, float, etc.) to a different data
type is known as type conversion.
• Explicit Conversion
• Implicit Conversion
Python Implicit Data
Type Conversion
Python Explicit Data Type Conversion
CHAPTER 04
Python Functions,Modules,and Packages(14 Marks)
◦ Built-In Mathematical Functions
◦ User Defined Functions
◦ Function definition
◦ Function calling
◦ Function arguments
Agenda
◦ Function parameters
◦ Return statement
◦ Scope of a variable
Built-In Mathematical Functions
◦ Function can be described as a piece of code that may or may not take some value(s) as input,
process it, and then finally may or may not return any value as output.
◦ Python’s math module is used to solve problems related to mathematical calculations.
◦ Some functions are directly executed for maths functions we need to import math module first.
◦ In python, there are two types of pre-defined functions:
• Inbuilt functions(Mathematical):
• These are the functions which doesn't require any other(external) code file (known
as, Modules or Library Files). These are a part of the python core and are just built within the
Python compiler hence there is no hassle of importing these modules/libraries in our code.
• Built-in Functions(Math Module):
• The second type of functions require some external files(modules) in order to be used. The process
of using these external files in our code is called importing. So all we have to do is import the file
into our code and use the functions which are already written in that file.
What Is A Function?
◦ A function is a group of Python statements that work together to perform a task.
◦ Examples : Input
◦ A function that calculates the gas mileage of a car
◦ A function that darkens the color of an image
◦ A function that counts the number of words in a text file
◦ A function that prints text to screen Function Name Statement 1
Statement 2
◦ Properties of a Function:
◦ Contains multiple statements …
◦ Has a name Statement N
◦ Can accept one or more input data
to work with them
◦ Can produce one output data Output
4.2 User Defined Functions
◦ Function Definition:
◦ Function definition is a block where the statements inside the function body are written.
◦ Functions allow us to define a reusable block of code that can be used repeatedly in a program.
Syntax:
def function_name(argument1, argument2, ...) :
“function_docstring”
function statements
return[expression]
◦ Docstrings provide a convenient way of associating documentation with Python modules, functions, classes, and methods.
◦ It's specified in source code that is used, like a comment, to document a specific segment of code.
◦ This allows the program to inspect these comments at run time, for instance as an interactive help system, or as metadata
◦ The doc string line should begin with a capital letter and end with a period. The first line should be a short description.
What Are User- Functions that readily come with Python are called built-in functions.
If we use functions written by others in the form of the library, it can
Defined Functions be termed as library functions.
In Python? All the other functions that we write on our own fall under user-
defined functions. So, our user-defined function could be a library
function to someone else.
v User defined functions are the functions that programmers create for their requirement and use.
v These functions can then be combined to form module which can be used in other programs by importing them.
1. User-defined functions help to decompose a large program into small
segments which makes the program easy to understand, maintain and
debug. Advantages of User-
2. If repeated code occurs in a program. The function can be used to
include those codes and execute when needed by calling that function.
Defined functions
3. Programmers working on a large project can divide the workload by
making different functions.
Function Calling
◦ The def statement only creates a function but does not call it. After
the def has run,
◦ Vocabulary parameters and arguments are not limited to python, but they
are same across different programming languages.
◦ Arguments are values that are passed into function(or method) when the
calling function
Parameters are variables(identifiers) specified in the (header of) function
definition.
Concept of Actual And Formal Parameters
Formal arguments are identifiers used in the function definition to represent corresponding actual arguments.
Actual arguments are values(or variables)/expressions that are used inside the parentheses of a function call.
Function Arguments
Types of formal Arguments are:
◼ Default Arguments
◼ Keyword Arguments
◼ Required Arguments
◼ Variable-length Arguments
◼ Keyword–only Parameters
Default Argument Values
◼ For some functions, you may want to make some parameters as optional and use default values if the
user does not want to provide values for such parameters.
◼ This is done with the help of default argument values.
◼ Note that the default argument value should be immutable.
◼ you cannot use mutable objects such as lists for default argument values.
An asterisk (*) is placed before the variable name that will hold the values of all non-keyword
variable arguments. This tuple remains empty if no additional arguments are specified during the
function call.
Example For Variable Length Arguments
Note :
➢ When we declare a starred parameter
such as *param, then all the positional
arguments from that point till the end are
collected as a list called 'param'.
➢ When we declare a double-starred
parameter such as **param, then all the
keyword arguments from that point till
the end are collected as a dictionary
called 'param'.
Keyword-only Parameters
◼ If we want to specify certain keyword parameters to be available as keyword-only and not as positional
arguments, they can be declared after a starred parameter
Return Statement
◼ The return statement is used to return from a function i.e. break out of the function. We can optionally return a value from the
function as well.
◼ The return statement is followed by an expression which is evaluated.
◼ Every function implicitly contains a return None statement. You can see this by running print someFunction() where the function
someFunction does not use the return statement
Example:
Scope of Variable
◼ Disadvantages of Recursion
Sometimes the logic behind recursion is hard to follow through
Recursive calls are expensive (inefficient) as they take up a lot of memory and time
Recursive functions are hard to debug
CHAPTER 04
Python Functions,Modules,and Packages(14 Marks)
➢ A module is a python file with a ‘.py’ extension
which consists of functions, statements and
variables. What are Modules in
➢The reason we use modules is to break down big Python?
programs into simpler and smaller ones.
➢We can reuse module files anytime as well.
What Is A Python Module
A module is simply a file containing python code .It may contain functions ,classes etc.
How To Create A Module In
Python?
EXAMPLE 1: EXAMPLE 2:
✓ The import keyword to incorporate the ✓ from keyword is used to get only a few or specific
module into our program. methods or functions from a module.
✓ In the above code, we have created an alias ✓ In the above code, we have imported all the functions
using the as keyword. using the asterisk and we can simply mention the
function name to get the results.
◦ EXAMPLE 3: IMPORT INSIDE A MODULE(from x import a) ◦ EXAMPLE 4: IMPORT INSIDE A MODULE(from x import a,b,c)