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

Functions and Local-Global Variables2

Uploaded by

ashikavenu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions and Local-Global Variables2

Uploaded by

ashikavenu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

FUNCTIONS AND LOCAL-

GLOBAL VARIABLES
Vaisakh M Nair
Mtech Biomedical Engineering
FUNCTIONS

• In Python, a function is a block of code that performs a specific task and can be called
multiple times from anywhere in a program.
• The idea is to put some commonly or repeatedly done tasks together and make a
function so that instead of writing the same code again and again for different inputs.
• Types of Functions in Python:
• Built-in library function: These are Standard functions in Python that are available
to use.
• User-defined function: We can create our own functions based on our
requirements.
EXAMPLE PROBLEM FOR FUNCTION

• Write a code to add 2 numbers using Function


• Write a code to enter the data about the items in a grocery shop using
function and format.
• Write a code to do algebraic calculations using function
PASS STATEMENT IN A FUNCTION
• Pass statement is used as a placeholder for the future code which will be
placed at its place to avoid errors from empty code blocks.
ARBITRARY ARGUMENT
• Arbitrary argument in python is used to give an arbitrary or random value for
the number of arguments in scenario when we cannot determine the exact
number of arguments that we are going to use in a particular function
program
• It is represented by placing a star in front of one argument in the paranthesis
DOUBLE ASTERKEY OPERATOR IN
FUNCTION
• This keyword is used to unpack 2 dictionaries and merge it into single output
FUNCTION AND IF STATEMENT
EXAMPLE

• Write a code for checking whether the number is even or odd using function
and formatting
• Find the largest of 2 numbers using function
• Sum of number in a list using for loop and function
BUILD IN FUNCTIONS(DATA CONVERSION)

•int() – Converts a value to an integer.


•float() – Converts a value to a floating-point number.
•str() – Converts a value to a string.
•list() – Converts a sequence to a list.
•tuple() – Converts a sequence to a tuple.
•set() – Converts a sequence to a set.
•dict() – Creates a dictionary(Unordered, changeable and indexed).
BUILD IN FUNCTIONS(MATHEMATICAL)

• abs(): returns the absolute value of the number


• pow():returns the power value of the number(exponential)
• round():returns the round off value of the decimel numbers
• max():returns the largest value of the iterable
• min():returns the smallest value of the iterable
• sum():returns the value of the sum of the iterable
BUILD IN FUNCTIONS(SEQUENCE AND
ITERABLES)
• len():returns the number of items in a sequence
• range():from where to where the list should be iterated
• enumerate():returns the index and the element or value.
• zip():combines 2 or more iterables element wise
• Reversed():returns the reversed iterable
BUILD IN FUNCTIONS(STRING
OPERATIONS)
• len():it returns the length of the string
• Chr ():it converts unicode to character type
• String.upper(): converts the string into upper cases
• String.lower():converts the string into lower cases
LOGICAL AND COMPARISON BUILD IN
FUNCTIONS
• all(): returns true if all the iterables in a set or list are true
• any(): returns true if atleast any of the following Iterables in a set or list are
true
BUILD IN FUNCTION(OBJECT AND TYPE
INSPECTION)
• type():returns the type of the object in a list whether it is char or int or float
etc.
• id(): returns the address of the object
• dir():returns the list of attribute of an object
MISCELLANEOUS BUILD IN FUNCTIONS

• bin():converts an integer to binary


• hex():converts an integer to hexadecimal value
• oct():converts an integer to octal value
• bool( ): converts a value to a Boolean value
USER DEFINED FUNCTIONS

• These are the functions that you create yourself to perform different tasks
• These functions allows you to organize code into reusable blocks, improving
readability and reducing repitition
FUNCTION WITH PARAMETERS
USER DEFINED FUNCTION WITH MULTIPLE
VALUE
USER DEFINED FUNCTION THAT RETURNS
MULTIPLE VALUES
RECURSIVE FUNCTION(USERDEFINED)

• Recursive function: it is a function that repeats or uses its own previous term
to calculate subsequent terms and thus forms a sequence of terms.
NESTED FUNCTION
NESTED FUNCTION WITH MULTIPLE
LEVELS

You might also like