Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
109 views

Chapter-IV Python Functions, Modules and Pacakges

Python PPT Chapter 4 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Chapter-IV Python Functions, Modules and Pacakges

Python PPT Chapter 4 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

PROGRAMMING WITH PYTHON

PWP - 22616

UNIT-IV
PYTHON FUNCTIONS, MODULES &
PACKAGES

Mr. Naresh A. Kamble


POINTS TO BE COVERED
• PYTHON BUILT-IN FUNCTIONS
• USER DEFINED FUNCTIONS
• MODULES
• PYTHON PACKAGES

2
3
PYTHON BUILT-IN FUNCTIONS
• Functions like input(), print() etc. are examples of built-in
functions.

• The code of these functions are already defined.

• These are called as built-in functions.

4
5
PYTHON BUILT-IN FUNCTIONS
• Python defines type conversion functions to directly convert
one data type to another.

• There are two types of Type Conversion in Python:

• Implicit Type Conversion

• Explicit Type Conversion

6
PYTHON BUILT-IN FUNCTIONS
• IMPLICIT TYPE CONVERSION

• In this conversion, the Python interpreter automatically


converts one data type to another without any user
involvement.

7
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

8
PYTHON BUILT-IN FUNCTIONS
• EXPLICIT TYPE CONVERSION

• In this conversion, the data type is manually changed by the


user as per their requirement.

• Various forms of explicit type conversion are explained below:

• int(a, base): This function converts any data type to integer.


‘Base’ specifies the base in which string is if the data type is a
string.

• float(): This function is used to convert any data type to a


floating-point number
9
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

10
PYTHON BUILT-IN FUNCTIONS
• EXPLICIT TYPE CONVERSION

• ord() : This function is used to convert a character to integer.

• hex() : This function is to convert integer to hexadecimal


string.

• oct() : This function is to convert integer to octal string

11
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

12
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

13
PYTHON BUILT-IN FUNCTIONS
• EXPLICIT TYPE CONVERSION

• complex(real,imag) : This function converts real numbers to


complex(real,imag) number.

• chr(number): This function converts number to its


corresponding ASCII character.

14
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

15
PYTHON BUILT-IN FUNCTIONS
• EXAMPLE

16
17
PYTHON BUILT-IN FUNCTIONS

18
19
USER DEFINED FUNCTIONS
• INTRODUCTION
• FUNCTION DEFINING
• FUNCTION CALLING
• FUNCTION ARGUMENTS
• SCOPE OF VARIABLES

20
21
USER DEFINED FUNCTIONS
• INTRODUCTION

• When we create our own functions to perform a particular


task are called as user defined functions.

22
23
USER DEFINED FUNCTIONS
• RULES FOR DEFINING USER DEFINED FUNCTIONS

• Function block or Function header begins with the keyword


def followed by the function name and parentheses (()).

• Any input parameter or arguments should be placed within


these parentheses. We can also define parameters inside
these parameters.

24
USER DEFINED FUNCTIONS
• RULES FOR DEFINING USER DEFINED FUNCTIONS

• The first string after the function header is called the


doctstring and is short for documentation string. It is used to
explain in brief, what a function does.

• The code block within every function starts with a colon (:)
and is intended.

25
USER DEFINED FUNCTIONS
• RULES FOR DEFINING USER DEFINED FUNCTIONS

• The return statement [expression] exits a function, optionally


passing back an expression to the caller.

• A return statement with no arguments is the same as return


none.

26
USER DEFINED FUNCTIONS
• SYNTAX

def functionname ( parameters ):

“function_doctstring”

function_suite

return [expression]

27
USER DEFINED FUNCTIONS
• EXAMPLE

28
29
USER DEFINED FUNCTIONS
• After defining a function, we call the function from another
function or directly from the python prompt.

• The order of parameters specified in the function definition


should be preserved in function call also.

30
USER DEFINED FUNCTIONS
• EXAMPLE - 1

31
USER DEFINED FUNCTIONS
• EXAMPLE - 2

32
33
USER DEFINED FUNCTIONS
• ARGUMENTS

• Information can be passed into functions as arguments.

• Arguments are specified after the function name, inside the


parentheses.

• You can add as many arguments as you want, just separate


them with a comma.

34
USER DEFINED FUNCTIONS
• We can call the function by any 4 arguments.

• Required Arguments

• Keyword Arguments

• Default Arguments

• Variable-Length Arguments

35
USER DEFINED FUNCTIONS
• REQUIRED ARGUMENTS

• These arguments are passed to a function in correct positional


order.

• Here, the number of arguments in the function call should


match exactly with the functional definition.

36
USER DEFINED FUNCTIONS
• EXAMPLE

37
USER DEFINED FUNCTIONS
• KEYWORD ARGUMENTS

• By using these arguments in a function call, the caller


identifies the arguments by the parameter name.

• This allows us 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.

38
USER DEFINED FUNCTIONS
• EXAMPLE

39
USER DEFINED FUNCTIONS
• DEFAULT ARGUMENTS

• A default argument is an argument that assumes a default


value if a value is not provided in function call for that
argument.

40
USER DEFINED FUNCTIONS
• EXAMPLE

41
USER DEFINED FUNCTIONS
• VARIABLE-LENGTH ARGUMENTS

• In some cases we may need to process a function for more


arguments than specified while defining the function.

• These arguments are called as variable-length arguments and


are not named in function definition.

• An asterisk (*) is placed before the variable name that holds


the values of all non-keyword variable arguments.

• This tuple remains empty if no additional arguments are


specified during the function call.
42
USER DEFINED FUNCTIONS
• EXAMPLE

43
USER DEFINED FUNCTIONS

44
45
USER DEFINED FUNCTIONS
• SCOPE OF VARIABLES

• In programming languages, variables need to be defined


before using them.

• These variables can only be accessed in the area where they


are defined, this is called scope.

• You can think of this as a block where you can access


variables.
46
USER DEFINED FUNCTIONS
• TYPES PYTHON VARIABLES SCOPE

• There are four types of variable scope in Python

• LOCAL SCOPE

• GLOBAL SCOPE

• ENCLOSING SCOPE

• BUILT-IN SCOPE

47
USER DEFINED FUNCTIONS
• LOCAL SCOPE

• Local scope variables can only be accessed within its block.

48
USER DEFINED FUNCTIONS
• LOCAL SCOPE

49
USER DEFINED FUNCTIONS
• GLOBAL SCOPE

• The variables that are declared in the global scope can be


accessed from anywhere in the program.

• Global variables can be used inside any functions.

• We can also change the global variable value.

50
USER DEFINED FUNCTIONS
• GLOBAL SCOPE

51
USER DEFINED FUNCTIONS
• GLOBAL SCOPE
• What would happen if you declare a local variable with the same name as
a global variable inside a function?

52
USER DEFINED FUNCTIONS
• ENCLOSING SCOPE

• A scope that isn’t local or global comes under enclosing scope

53
USER DEFINED FUNCTIONS
• BUILT-IN SCOPE

• All the reserved names in Python built-in modules have a


built-in scope.

54
55
56
MODULES
• INTRODUCTION

• PYTHON BUILT-IN MODULES

• CREATING MODULES

• IMPORTING OBJECTS FROM MODULES

• CREATING CUSTOM MODULES

57
MODULES
• INTRODUCTION

• A module a file that contains a Python code that we can use in


our program

58
MODULES
• PYTHON BUILT-IN MODULES

• All Built-in modules can be accessed using following command

help(‘modules’)

• All modules are placed in an alphabetic order (a to z)

59
60
MODULES
• CREATING MODULES

• For creating any module, we choose the module name and


import it using import statement

import module

• For example

import math

61
62
MODULES
• IMPORTING MODULES

63
64
MODULES
• IMPORTING MODULES – CONSTANT VALUES

65
MODULES
• IMPORTING MODULES – RENAMING

66
MODULES
• IMPORTING MODULES – from…….import statement

67
MODULES
• IMPORTING MODULES – from…….import statement

68
MODULES
• IMPORTING MODULES – all names *

69
MODULES
• IMPORTING MODULES – dir() function

70
71
MODULES
• CUSTOM MODULE

• In python we can create custom modules as per our need.

• This helps us to keep our code organized.

72
MODULES
• CUSTOM MODULE - CALCULATOR

• To perform ARITHMETIC CALCULATIONS

73
MODULES
• CUSTOM MODULE - CALCULATOR

• To perform ARITHMETIC CALCULATIONS

74
MODULES
• CUSTOM MODULE - CALCULATOR

75
76
PACKAGES
• INTRODUCTION

• CREATING PYTHON PACKAGE

• STANDARD PACKAGES

• USER DEFINED PACKAGES

77
PACKAGES
• INTRODUCTION

• We usually organize our files in different folders and


subfolders based on some criteria, so that they can be
managed easily and efficiently.

• For example, we keep all our games in a Games folder and we


can even subcategorize according to the genre of the game or
something like this.

• The same analogy is followed by the Python package.

78
PACKAGES
• INTRODUCTION

• A Python module may contain several classes, functions,


variables, etc. whereas a Python package can contains several
module.

• In simpler terms a package is folder that contains various


modules as files.

• A package is directory containing modules or subpackages.

79
80
81
PACKAGES
• EXAMPLE 1

• Let’s create a package named mypckg that will contain two


modules mod1 and mod2.

• To create this module follow the below steps –

– Create a folder named mypckg.

– Inside this folder create an empty Python file i.e.


__init__.py (for considering as package)

– Then create two modules mod1 and mod2 in this folder.

82
83
84
85
86
PACKAGES
• EXAMPLE 2

• Let’s create a package named game that will contain two


subpackages characters and weapons.

• To create this module follow the below steps –

– Create a folder named game.

– Inside this folder create an empty Python file i.e.


__init__.py (for considering as package)

– Then create two subpackages characters and weapons in


this folder.
87
PACKAGES
– Inside each subpackage create an empty Python file i.e.
__init__.py (for considering as package)

– Create two python files named player.py and boss.py in


characters package.

– Create one python file named category.py in weapons


package.

– Finally create main.py python file outside the game


package to import and call all packages.

88
89
90
91
92
PACKAGES
• DATE AND TIME MODULES

• The Time Module

• It contains a lot of functions to process time.

• Time intervals are represented as floating point numbers


in seconds.

93
EPOCH
94
ctime() – Current Time
95
PACKAGES
• DATE AND TIME MODULES

• The datetime Module – TIME CLASS

• It contains functions and classes for doing date and time


parsing, formatting and arithmetic.

• It contains functions and classes for working with dates


and times, separately and together.

96
Time class 97
PACKAGES
• DATE AND TIME MODULES

• The datetime Module – DATE CLASS


• Calendar date values are represented with the date class.

• Instances have attributes for year, month, and day.

• It is easy to create a date representing today’s date using


the today() class method.
98
Date class 99
PACKAGES
• CALENDAR MODULES

• The datetime Module – DATE CLASS

• It has many built-in functions for printing and formatting


the output.

• By default calendar takes Monday as the first day of the


week and Sunday as the last.

100
101
102
103
104
PACKAGES
• STANDARD PACKAGE

• Numpy

• NumPy is a Python library used for working with arrays.

• It also has functions for working in domain of linear


algebra, fourier transform, and matrices.

• NumPy was created in 2005 by Travis Oliphant. It is an


open source project and you can use it freely.

• NumPy stands for Numerical Python.

105
PACKAGES
• Why to use Numpy?

• In Python we have lists that serve the purpose of arrays, but


they are slow to process.

• NumPy aims to provide an array object that is up to 50x faster


than traditional Python lists.

• The array object in NumPy is called ndarray, it provides a lot of


supporting functions that make working with ndarray very easy.

• Arrays are very frequently used in data science, where speed


and resources are very important.

106
py –m pip install numpy

107
PACKAGES
• STANDARD PACKAGE

• Pandas

• Pandas is a Python library.

• Pandas is used to analyze data.

• Pandas is a Python package providing fast, flexible, and


expressive data structures designed to make working with
“relational” or “labeled” data both easy and intuitive.

• It aims to be the fundamental high-level building block for


doing practical, real-world data analysis in Python.
108
PACKAGES
• Pandas
• Pandas makes it simple to do many of the time consuming, repetitive
tasks associated with working with data, including:
• Data cleansing
• Data fill
• Data normalization
• Merges and joins
• Data visualization
• Statistical analysis
• Data inspection
• Loading and saving data
109
py –m pip install pandas

110

You might also like