Ankit Mahato - Learn Python 3-EdPunk (2022)
Ankit Mahato - Learn Python 3-EdPunk (2022)
Ankit Mahato - Learn Python 3-EdPunk (2022)
Introduction 1
Introduction to Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction to Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Advantages of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Installing Python in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Installing Python in macOS (Apple) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Execution Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Interactive Mode of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Script Mode of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Python Fundamentals 11
Tokens: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Tokens: Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Tokens: Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Tokens: Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Tokens: Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Tokens: Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Blocks and Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Input / Output 31
How to Accept User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Displaying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
i
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Syntax Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Runtime Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Logical Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Control Flow 57
Introduction to Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Sequential Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Selection Statements: if .. else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Iteration: for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Iteration: while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Strings 71
Strings: Introduction & Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Accessing Characters of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Introduction to String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Convert Case of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Check Characters of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Split a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Strip Characters from a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Check Prefix or Suffix in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Find & Replace Characters in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Traversing a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
List 83
What is a Python List? How to Create a List? . . . . . . . . . . . . . . . . . . . . . . . . 83
Accessing Items of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Modifying a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Removing Item from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Traversing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Built-in Functions that can be used for a List . . . . . . . . . . . . . . . . . . . . . . . . 88
Introduction to List Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Adding Items to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Removing Items from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Counting or Locating Items in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Reversing Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Sorting a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Copying a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Nested List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Tuple 97
List vs Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
How to Create Tuple? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
What is a Singleton? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Accessing Items of a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Tuples are Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Tuple Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Traversing a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Built-in Functions that can be used for a Tuple . . . . . . . . . . . . . . . . . . . . . . . 101
Locating Items in a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Counting the Occurrence of an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
New Tuple with Reversed Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
ii
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Dictionary 105
What is a Dictionary? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
How to Create a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Accessing Items (Key:Value) of a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . 106
Updating a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Removing an Item (Key:Value) from a Dictionary . . . . . . . . . . . . . . . . . . . . . . . 108
Dictionary Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Traversing a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Built-in Functions for a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Creating a Copy of a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Nested Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
iii
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
iv
Introduction
• A mathematical task, such as finding the square root of a number or solving a set of simultaneous
equations.
• A text-based task such as reading a document and performing search/replace.
• Streaming and playing multimedia files containing audio and video.
• Using a search engine to find and visit a website.
• Playing an online multiplayer game with friends.
• and many more…
Softwares play an important role as they translate human activity into corresponding machine instructions
which are executed to accomplish these tasks.
These instructions have to be provided in machine language or low level language (0s and 1s) that is
difficult to read or write for a human being.
This led to the invention of high-level programming languages in which programs can be easily written
and managed. The human-readable programs written using high-level languages are converted into
computer-readable machine code or bytecode using compilers or interpreters.
There are many high-level programming languages that are currently in wide use.
Some of the popular languages are Java, C, C++, C#, Go, Swift, JavaScript, PHP, Dart, Kotlin and
Python.
Introduction to Python
Guido van Rossum started the development of Python in December 1989. He released the first version
(0.9.9) of Python for general public on February 20, 1991.
The language evolved over the next few decades and so did its definition, the current version of which
is stated below:
Let us break down and analyze the above definition to gain a better understanding of Python:
High-level
Python is a high-level programming language which can be used to write a program in natural language
(english) making it readable, writable, shareable and manageable.
1
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
While developing a Python program one is not required to handle the various components of computer
architecture like registers, memory addresses and call stacks which have to be handled if an assembly
language or a low-level language is used for development.
Python includes high-level language features like variables, data structures (lists, dictionaries, etc.),
objects, expressions, modules, classes, functions, loops, threads, file handling, string handling, error
handling and other computer science abstraction concepts.
Interpreted
In traditional programming languages like C or C++, codes are compiled into computer-readable machine
code before it can be executed.
Python is an interpreted language where the Python interpreter reads and executes the program line
by line.
The process is more time consuming compared to compiled code execution, but allows faster develop-
ment as one does not have to go through the entire compilation step during testing and debugging.
Also, the code can run on any platform as long as it has a valid Python installation (which includes
interpreter) as there is no generation of platform dependent binaries.
Object-oriented
Python does not enforce Object-oriented programming (OOP), but completely supports it.
A programmer can define Classes specifying the data in the form of attributes (or properties) and some
programming logic in the form of member functions (or methods). Once a class is defined, the user can
create an instance of that class which is known as an object.
In Python, everything ( int , list , dict , etc.) is an object. We will cover more about objects in
detail in the later sections.
Dynamic Semantics
As Python is an interpreted language in which the code is executed line-by-line, a python statement or
expression is evaluated during run-time. This allows dynamic typing (type of a variable can change over
its lifetime) and creation of dynamic objects during run-time, which provides more flexibility, usability
and fewer lines of code as compared to statically-typed compiled languages like C/C++.
Advantages of Python
The key advantages of Python are as follows:
1. Easy to Learn
The Python programming language is easy to learn with low technical and conceptual overhead. This
makes it an ideal language for beginners to learn programming.
2. Clear Syntax & Fewer Lines of Code
Python’s simple and easy to learn syntax increases code readability and leads to fewer lines of code.
A typical task which requires an average of twenty lines of code in C and seven in Java can often be
done with just one line in Python.
Also, due to fewer lines of code the chances of making any error is significantly reduced.
3. Open Source
Python is an open source programming language, so anyone can view and contribute to its source
code.
4. Portable & Platform Independent
The Python programming language is portable & platform independent as it can work on any Linux,
MacOS or Windows device.
5. Standard Library & Python Packages
2
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Python has a rich and extensive Standard Library, a collection of predefined functions for various tasks.
Python programmers also have at their disposal the vast ecosystem of more than 250,000 commu-
nity contributed libraries in the Python Package Index (PyPI), where one can find a solution to every
conceivable task.
Some of the most popular web development frameworks (django, flask, etc.) are written in Python. This
coupled with the availablity of packages to connect to any database makes Python a great choice for
web application development.
Let’s start with the Python 3 installation process on Windows operating system.
Download the latest Windows installer from the Python Software Foundation website.
3
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Select the checkbox ✅ Add Python 3.9 to PATH . This will enable you to install python packages
and run python script via command-line.
Once the setup is complete, click on the Start menu and open Python 3.9 -> IDLE (Python 3.9 64 bit)
to launch the Python interpreter.
4
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Let’s start with the Python 3 installation process on macOS operating system.
Download the latest macOS installer from the Python Software Foundation website.
5
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Once the setup is complete, click on the Launchpad -> IDLE to launch the Python interpreter.
Execution Modes
After installing the latest version of the Python interpreter, we can now write and execute some basic
Python codes.
1. Interactive Mode: When the IDLE application is launched, the Python interpreter or the Python
shell pops up on the screen. User can interact with the Python interpreter and execute statements
(single line or multiline code snippets) directly in this Python shell.
2. Script Mode: This is the most commonly used method for executing a Python program. The
entire Python program is written and saved in a file ( .py extension) which can be executed
using the IDLE application.
6
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
To launch the IDLE application click [Windows Start Menu Button] -> [Python 3.9 Folder] -> [IDLE (Pyth
.
Launch IDLE
The Python interpreter or the Python shell will pop-up on the screen.
Python Shell
The version ( 3.9 ) of the Python interpreter is displayed at the top of the window followed by the >>>
symbol which indicates that the interpreter is ready to take instructions.
Python commands or statements can be input on this prompt. The input statements are executed
instantaneously and any variable assignments are retained as long as the session is not terminated.
Basic Arithmetic
Let us perform some basic arithmetic operations in the interactive mode using an integer number ( 2 )
and a floating-point number ( 3.5 ):
>>> 2 + 2
4
7
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> 2 * 3.5
7.0
It can be observed that the results of each of the above computations are displayed immediately in the
shell.
Storing Values/Results
Instead of immediately displaying the results, they can also be stored in variables using the assignment
symbol ( = ) as shown below:
>>> a = 2 + 2
>>> b = 2 * 3.5
The values of a and b can be accessed later for future calculations as shown below:
>>> a
4
>>> b
7.0
>>> a * 5
20
>>> b / 3
2.3333333333333335
Interactive mode is not just restricted to basic arithmetic or assignments. Let us join two strings -
"Hello, " and "world!" .
>>> "Hello, " + "world!"
'Hello, world!'
The complete functionality of Python is easily accessible to a user via the Interactive Mode.
This makes it convenient for testing and instant execution of small code snippets (single line or few lines
of code), a feature not available in compiled languages like C, C++ and Java.
But, the statements cannot be saved for future use and have to retyped for re-execution. This disadvan-
tage is overcome by the use of Python in Script Mode as described in the next section.
To write reusable codes, script mode is the most preferred mode of code execution.
File Creation
To create a new file using the IDLE application click [File] -> [New File]
8
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
and save the script as example.py ( .py file extension for all Python scripts) using
[File] -> [Save As...]
Save File
Script Execution
9
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Execute File
It can be observed that the code has been executed, but no output is displayed on the console (or the
standard output) as all outputs have to be explicitly specified when running a code in the script mode.
This can be done by using the print() function which is used in Python scripts to display output on
the output stream. Let us quickly add the print() function in the above code and execute it.
a = 2 + 2
print(a)
Now, when you run the script you will observe that the value of a , that is 4 , is now displayed on
the console.
10
Python Fundamentals
Tokens: Introduction
When a Python code is executed, the Python interpreter reads each logical line and breaks it into a
sequence of lexical units.
These lexical units are better known as tokens - the smallest individual units of a program. They are the
building blocks of a Python code and can be classified into one of the following categories:
• Keywords : Reserved words that convey special meaning when processed by the Python inter-
preter.
• Identifiers : Names defined by the programmer to refer to objects that can represent variables,
functions, classes, etc.
• Literals : Values specified in the program which belong to exactly one of the Python’s built-in
data types.
• Delimiters : Symbols that denote grouping, punctuation, and assignment/binding.
• Operators : Symbols that can operate on data and compute results.
Tokens: Keywords
Keywords are reserved words that have special meaning when processed by the Python interpreter.
They are case-sensitive and cannot be used for naming identifiers (class, function, variable or structure
names).
The list of keywords in Python are provided below:
Tokens: Identifiers
Identifiers are used for defining the names of Python objects such as variables, functions, classes,
modules, etc. The naming convention for identifiers is as follows:
11
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
• Must begin with a lowercase character ( a-z ) or an uppercase character ( A-Z ) or underscore
sign ( _ ).
• Followed by any number of letters ( a-z , A-Z ), digits ( 0-9 ), or underscores ( _ ).
• Should not be a keyword.
• No special symbols are allowed like ! , @ , # , $ , % , etc.
Some points to keep in mind while naming identifiers:
• Identifiers are case-sensitive in nature and any difference in case of any character refers to a
different identifier. e.g., length and Length are different identifiers.
• Identifiers differing by only underscores are different. e.g., unitlength and unit_length
are different identifiers.
It is also a good practice (although not compulsory) to follow the following procedure while naming
identifiers:
• Identifiers should be named carefully with an emphasis on clarity and readability. For example, in a
program that calculates the area of a rectangle, a good choice for identifier names are - length
, breadth and area .
• Class names should start with uppercase character.
• Identifiers starting with an underscore have special meaning in a program.
• Variable, function and method names should be in lowercase characters, with underscores
separating multiple words like area_of_square , area_of_triangle , etc.
Tokens: Literals
Literals are tokens in the source code which represent fixed or constant values. They are often used in
assignment statements for initializing variables or in comparison expressions.
The various types of literals available in Python are as follows:
Numeric Literals
Numeric literals are used for representing numeric values in the source code. They can be of three types
- integers, float point numbers and imaginary numbers.
Integer Literals
092 is not a valid decimal integer literal as a zero precedes the first non-zero digit 9 .
ii. Binary or base-2 Integer
A binary integer or base-2 integer begins with 0b or 0B followed by binary digits 0-1 .
12
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Thus, it can be observed that number 27 can be written in the program as 27 (decimal), 0b11011
(binary), 0o33 (octal) or 0x1B (hexadecimal).
Underscores in Integer Literals
An optional character _ (underscore) is also allowed in an integer literal to group digits for enhanced
readability.
One underscore can occur between digits, and after base specifiers like 0o .
They are ignored while determining the actual numerical value of the literal.
Some valid underscore usages are - 10_00_00_000 , 0b_1110_0101 , 0x23_123 .
The exponential component can be identified by the letter e or E followed by an optional sign ( +
or - ) and digits ( 0-9 ). This exponent is equivalent to multiplying the real number with the power of
10 .
For example, 3.4E2 is equivalent to 3.4 x 10^2 or 340.0 , whereas 3.4e-2 is equivalent to
3.4 x 10^-2 or .034 .
Imaginary Literals
To specify complex numbers and perform complex number mathematics, Python supports imaginary
literals which are given by real or integer number followed by the letter j or J which represents the
unit imaginary number.
Example imaginary literals:
3.5j
15.j
12j
.005j
3e100j
3.5e-10j
13
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Points to Note
In Python,
• there is no specialized literal such as a complex literal. A complex number is actually represented
in the program using an expression comprising a real number (integer/float numeric literal) and an
imaginary number (imaginary literal). For example, 1 + 2j consists of an integer literal ( 1 )
and a imaginary literal ( 2j ).
• numeric literals do not include the minus sign ( - ). - is actually a unary operator it combines
with a numeric literal to represent negative numbers. For example, in -3.14 the numeric literal
is 3.14 and - is an operator.
Boolean Literals
The reserved words True and False are also boolean literals which can be used to specify the
truth value in a program.
String Literals
String literals are texts which can be specified in a variety of ways in Python:
s1 = """A
multiline
String"""
s2 = '''Also a
multiline
String'''
The backslash ( \ ) character can be used in a string literal to escape characters that otherwise have a
special meaning, such as newline, linefeed, or the quote character.
Although \' and \" can be used to specify quote characters, Python allows embedding double
14
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
quotes inside a single-quoted string ( 'My name is "Python".' ) and single quotes inside a double-
quoted string ( "Python's World" ).
String literals also support unicode characters which can be specified using \u escape sequence
followed by the 4 letter unicode.
>>> print("E = mc\u00B2")
E = mc²
In the above example, \u00B2 is the unicode character which represents the ‘SUPERSCRIPT TWO’.
Special Literal
None is a special literal which is used to denote the absence of value.
It should not be confused with 0 as 0 is an integer literal with a defined finite value, whereas None
implies nothingness.
>>> a = None
>>> a
>>>
In the above example, the Python shell does not display any value of a as it is assigned as None
which has no value.
Collection of Literals
Python has the provision for specifying a collection of literals in the source code using a special syntax
known as “displays”.
One can create specialized containers like list, set and dictionary using this syntax.
Some example collection of literals (displays) are provided below:
Tokens: Operators
Operators are tokens which can be combined with values and variables to create expressions which
evaluate to a single value. Python supports a rich set of operators:
+ - * **
/ // % @
<< >>
& | ^ ~
:= < >
<= >= == !=
Each of the above operators are covered in detail in the chapter - Operators.
Tokens: Delimiters
Delimiters are tokens which are useful for organizing a program and are used in statements, expressions,
functions, literal collections, and various other code structures.
They can be classified based on utility as follows:
15
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Grouping
() , [] and {} are delimiters used for:
sum_6 = (1 + 2 +
3 + 4 +
5 + 6)
is equivalent to
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
sum_6 = (1 + 2 + 3 + 4 + 5 + 6)
Assignment/Binding
The assignment or binding delimiters are used for binding objects to names via assignment statements.
The complete list of tokens are provided below:
= += -= *=
/= //= %= **=
@= &= |= ^=
<<= >>=
Character Set
A set of valid characters that a programming language recognizes is known as its character set.
Python is a new age programming language which supports Unicode encoding standard. The default
encoding for Python source code is UTF-8 (Unicode Transformation Format – 8-bit) which enables
developers to use Unicode characters not only as literals, but also as identifiers.
This makes Python one of the very few programming languages that support multiple languages as
shown in the example below:
Code
16
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
क = 1 # Devanagari Letter KA
ক = 2 # Bengali Letter KA
க = 3 # Tamil Letter KA
ક = 4 # Gujarati Letter KA
print(क + ক + க + ક)
Output
हमें print करे
10
Each code block contains one or more statements which are enclosed between braces - { and }
and are executed sequentially.
A sample C++/Java code is provided below which checks for an input x .
C++
if (x < 10) {
cout << "x is less than 10" << endl;
if (x <= 5) {
cout << "x is less than or equal to 5" << endl;
}
else {
cout << "x is more than 5 but less than 10" << endl;
}
}
else {
cout << "x is not less than 10" << endl;
}
Java
if (x < 10) {
System.out.println("x is less than 10");
if (x <= 5) {
System.out.println("x is less than or equal to 5");
}
else {
System.out.println("x is more than 5 but less than 10");
}
}
else {
System.out.print("x is not less than 10");
}
It can be seen how indentations ( tab at the beginning of line) are added (not required by programming
language) to the code to increase readability, which helps in guiding readers through the code.
Python
Code blocks in Python are inspired by this idea as it makes it easier to understand a Python code.
A block of code is denoted by line indentation, typically 4 spaces (preferred) or a tab. This indentation
17
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
is used to determine the logical group of statements, with all statements within a group having the same
level of indentation.
The corresponding Python code for the above C++/java examples is provided below.
Notice how the code blocks are indented according to the logic.
if x < 10:
print("x is less than 10")
if x <= 5:
print("x is less than or equal to 5")
else:
print("x is more than 5 but less than 10")
else:
print("x is not less than 10")
Comments
Python supports single-line comments and multi-line comments to enhance code readability via adding
documentation.
Multiline comments
A multiline comment begins with ''' or """ and ends with the same.
"""
I am
a multiline
comment.
"""
'''
I am
also a multiline
comment.
'''
As compared to single line comments, multiline comments should begin at the same indentation level
corresponding to their code block.
For example,
"""
Begin program
here
"""
if x < 10:
"""
Enter code block when x
is less than 10
"""
print("x is less than 10")
18
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
if x <= 5:
"""
Enter code block when x
is less than or equal to 5
"""
print("x is less than or equal to 5")
else:
"""
Enter code block when x
is more than 5 but less than 10
"""
print("x is more than 5 but less than 10")
else:
"""
Enter code block when x
is not less than 10
"""
print("x is not less than 10")
19
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
20
Variables, Objects & Data Types
This a is synonymous to a box of fixed dimensions (fixed type) holding something (value 1 ) inside it.
Box ‘a’
In case we want to change the contents of the box, we can replace it with something similar (same type).
a = 2;
The contents of this box can be replicated and placed in a similar (same type) box:
int b = a;
21
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Multiple boxes can exist, each containing an item having the same value.
int x = 3;
int y = 3;
int z = 3;
As shown above, the programming languages in which the variables (named boxes) are declared along
with their types (size of the boxes) are known as statically typed languages.
The size of these boxes cannot change later in the program until the variable is re-initialized with the
same name and different type.
Python is a dynamically-typed language, where every value or data item (of any type like numeric,
string, etc.) is an object.
The variable names are just name-tags pointing to the actual object containing data of any type.
As there is no need of any variable declaration in Python before usage, there is no concept of default
value (an empty box or null ) which exists in other programming languages.
Whenever a new object is created in Python, it is assigned a unique identity (ID) which remains the same
throughout the lifetime of that object. This ID is the address of the object in memory and the built-in
function id() returns the value of this address.
>>> a = 1
>>> id(a)
140407745943856
>>> a = 2
>>> id(a)
140407745943888
Also, when a is assigned to b , instead of creating a new copy, b points to the same object as
a .
22
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
• Must begin with a lowercase character ( a-z ) or an uppercase character ( A-Z ) or underscore
sign ( _ ).
• Followed by any number of letters ( a-z , A-Z ), digits ( 0-9 ), or underscores ( _ ).
• Should not be a keyword.
• No special symbols are allowed like ! , @ , # , $ , % , etc.
Assignment
Variables can be bound to a reference of an object (of any type) using assignment statements.
You can create an object (data) and bind it’s reference to a variable using equal sign ( = ):
count = 100 # integer
pi = 3.141 # real number
name = "Python" # string
Here, L-value refers to the assignable variables ( count , pi , name ) on the left side of the
assignment and R-value refers to the expression on the right side of the assignment operator that has a
value ( 100 , 3.141 , "Python" ).
As variables are just references, you can rebind them to another object of same or different type:
a = 100 # integer
a = 3.141 # real number
a = "Python" # string
Deletion
The del statement can be used to unbind the reference to an object.
>>> a = 10
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Accessing a results in a NameError as the reference (variable) to the object holding value 10 is
deleted.
The object is also automatically cleaned up from the memory if there is no other variable referencing to
it (garbage collection).
Multiple Assignment
In Python, multiple assignment can be used to condense variables which are set to the same value:
>>> x = y = z = 'foo'
>>> x
'foo'
>>> y
'foo'
>>> z
'foo'
23
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Tuple Swapping
In Python, a temporary variable is not required to swap values of two variables. Values can be directly
swapped (tuple swapping) as shown below:
>>> a = 'Hello'
>>> b = 'World'
>>> b, a = a, b
>>> a
'World'
>>> b
'Hello'
Objects holding integer numbers like -1, 0, 200 are of int data type.
Real or Floating-point Numbers
Objects holding real or floating point numbers like -1.1, 3e2, 20.0 are of float data type.
Complex Numbers
Objects storing complex numbers like 2 + 1j, -3j, -1 + 2J are of type complex .
Each complex number has two parts, the real part which is a numeric integer or floating point literal,
and the imaginary part which is an imaginary literal.
Boolean
The boolean data type ( bool ) is a subtype of int . It stores the evaluated value of expressions
represented as keywords - True (integer value 1 ) and False (integer value 0 ).
24
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
A string ( str data type) is a sequence of zero or more unicode characters enclosed within a pair of
single ( ' ) or double ( " ) quotes.
Some example strings are -
"42"
'hello'
"python"
List
A list is sequence of items of same or different data types which are enclosed within brackets -
[ ] .
Some example lists are -
[1, 2, 3]
['abc', 23, 3.14]
['edpunk', 'python']
Tuple
A tuple is an immutable sequence of items of same or different data types which are enclosed within
parentheses - ( ) .
Some example tuples are -
(1, 2, 3)
('abc', 23, 3.14)
('edpunk', 'python')
The key-value pairs are separated by comma ( , ) and enclosed within curly braces - { } .
Some example dictionaries are -
{1: "a", 2: "b", 3: "c"}
{"name": "edpunk", "language": "python"}
25
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
It is neither 0 nor False as these are defined finite values, whereas None implies nothingness.
Type Checking
The built-in type() function can be used to fetch the data type of an object.
Examples:
>>> count = 100
>>> type(count)
<class 'int'>
>>> pi = 3.141
>>> type(pi)
<class 'float'>
This function can be used along with the is operator in an expression to test whether the object is of
the given type.
>>> count = 100
>>> type(count) is int
True
The in operator can be used along with the type() function to test if the data type is one of the
mentioned types.
# count is of type int or float
>>> type(count) in (int, float)
True
Type Casting
The process of converting the data type of an object from one type to another is known as Type Casting
or Type Conversion.
In the below example the final type of c is automatically determined as float by the Python
interpreter.
>>> a = 1 # int
>>> b = 2.0 # float
>>> c = a + b
>>> c
3.0
>>> type(c)
<class 'float'>
26
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Creates an int from a bool , float or str containing integer characters (with or without
sign).
>>> int(True)
1
>>> int(2.3)
2
>>> int("2")
2
2. float()
Creates a float from a bool , int or str containing floating point literals (with or without
sign).
>>> float(True)
1.0
>>> float(2)
2.0
>>> float("2.3")
2.3
• "Infinity"
• "inf"
• "nan" (not a number).
>>> float("Infinity") > 1
True
>>> float("nan")
nan
3. str()
27
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
4. tuple()
5. list()
6. set()
28
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
You can witness in the above example how the object containing the value 1 is different from the
object containing the value 2 , and a points to the latest object.
Sequence data types like strings and tuples are also immutable, i.e., no modifications are permitted to
any item once it is created and any attempt to do so raises an error.
>>> s = "Hello"
>>> s[1] = "P"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> t = (1, 2, 3)
>>> t[1] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Although, similar to numeric types the variables can be re-assigned to new sequences.
>>> s = "Hello"
>>> id_s = id(s)
>>> s = "Help"
>>> id_s2 = id(s)
>>> id_s == id_s2
False
>>> t = (1, 2, 3)
>>> id_t = id(t)
>>> t = (0, 2, 3)
>>> id_t2 = id(t)
>>> id_t == id_t2
False
29
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In the above examples, the id of the objects ( list , dict , set ) do not change, which implies
that no new objects are created and the original objects are modified.
30
Input / Output
When this function is encountered in the code, the python interpreter waits for the user to type a response
which is read as a string and assigned to a variable.
>>> name = input()
edpunk
>>> name
'edpunk'
The function also has an optional string argument which is used as a prompt message for the user.
>>> name2 = input("Enter name: ")
Enter name: EdPunk
>>> name2
'EdPunk'
User input can be converted into integer or floating point numbers using the type conversion functions
int() and float() .
>>> num = int(input("Enter n: "))
Enter n: 10
>>> type(num)
<class 'int'>
>>> num
10
Displaying Output
The built-in print() function is used to display an output (value of variables, expressions, etc.) on
the standard output.
Let us go through a program which computes the area of a rectangle and displays it:
Code
length = 10
breadth = 5
31
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Output
Area: 50
print() function can also be used to output the value of multiple objects when they are provided as
arguments to the function.
Code
a = 2 + 2
b = 2 * 3.5
print(a, b)
Output
4 7.0
In the above code, the values of a and b are separated by a blank space (the default value of sep
).
This property can be modified by providing any user defined separator using the sep option.
Output
4,7.0
When expressions are provided as arguments to the print() function, output is the evaluated value
of those expressions.
For example,
Code
print(2 + 2)
print(2 * 3.5)
print("Hello, " + "world!")
Output
4
7.0
Hello, world!
In the above code snippet, each print() function invocation creates a new line of output. This is
because end parameter has the newline character ( '\n' ) as the default value in the print()
function.
This can be modified by the user as shown below:
Code
print(2 + 2, end=",")
print(2 * 3.5, end=";")
print("Hello, " + "world!")
Output
32
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
4,7.0;Hello, world!
Note
All non-keyword arguments or expressions are converted to strings and written to the output stream by
the print() function. They are separated by sep and followed by end . An empty print()
invocation writes end parameter (an empty line as end defaults to the newline character '\n' ).
33
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
34
Operators & Expressions
Introduction to Operators
Operators are symbols that perform a single simple task or operation on one or more values resulting in
a single evaluated value.
The values on which these operators are applied are called operands.
Unary Operators
Unary operators are applied on a single operand positioned on right side of the operator.
Following are some unary operators available in Python:
• + (plus): The unary + operator does not change the value of the operand.
• - (minus): The unary - operator changes the sign of the operand.
• ~ (bitwise NOT ): The unary ~ results in the bit-wise inversion of the integer operand. The
bit-wise inversion of x is defined as -(x+1) .
>>> x = 5
>>> -x
-5
>>> +x
5
>>> ~x
-6
Binary Operators
Binary operators are applied on two operands.
For example, arithmetic operators ( + , – , * , / ) evaluate the result of mathematical computation
of two values.
Operators in Python
A rich set of operators are available in Python which can be categorized as follows:
• Arithmetic Operators - + , – , * , / , % , ** , //
• Relational Operators - == , != , > , >= , < , <=
• Assignment Operators - += , -= , *= , /= , %= , **= , //=
• Logical Operators - not , or , and
• Identity Operators - is , is not
• Membership Operators - in , not in
• Bitwise and Shift Operators - & , | , ^ , ~ , << , >>
35
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Arithmetic Operators
Arithmetic operations can be performed in Python using the following arithmetic operators:
Addition
The + operator adds the values of numeric operands.
>>> 2 + 3
5
>>> 2 + 3.0
5.0
In case the operands are of type str , list or tuple , the + operator concatenates these two
sequences or strings.
>>> 'edpunk' + 'python'
'edpunkpython'
>>> ["ed", "punk"] + ["python", ]
['ed', 'punk', 'python']
Subtraction
The - operator subtracts the value of operand on right from the value of operand on left.
>>> 2 - 3
-1
Multiplication
The * operator multiplies the values of numeric operands.
>>> 2 * 3
6
In case the operands are of type str , list or tuple , the * operator returns a sequence or
string self-concatenated the specified number of times.
>>> "python" * 3
'pythonpythonpython'
>>> ['ed', 'py'] * 3
['ed', 'py', 'ed', 'py', 'ed', 'py']
Division
The / operator divides the value of operand on left by the value of operand on right and returns the
real number quotient.
>>> 6 / 2
3.0
>>> 5 / 2
2.5
Floor Division
The // operator divides the value of operand on left by the value of operand on right and returns the
integer quotient.
>>> 5 // 2
2
Modulus
The % operator divides the value of operand on left by the value of operand on right and returns the
remainder.
>>> 5 % 2
1
36
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Exponent
The ** operator raises the left operand to the power of the right operand.
>>> 5 ** 2
25
Relational Operators
Relational operators are useful for comparing the values of the operands to determine their relationship.
Following relational operators are available in Python:
Equals to
The == operator returns True if the value of operand on left is same as the value of operand on
right.
>>> 2 == 2
True
>>> 2 == 3
False
In case of sequence operands like str , list or tuple , the result is True if both the sequences
are exactly the same.
>>> "python" == "python"
True
>>> "pypi" == "python"
False
>>> [1, 2, 3] == [1, 2, 3]
True
As a sequence is an ordered collection of items, so the order in which the items are positioned is very
important.
>>> [2, 1, 3] == [1, 2, 3]
False
Not equal to
The != operator returns True if the value of operand on left is not equal to the value of operand on
right.
>>> 2 != 2
False
>>> 2 != 3
True
>>> 'py' != 'oy'
True
>>> [2, 1, 3] != [1, 2, 3]
True
>>> [1, 2, 3] != [1, 2, 3]
False
Greater than
The > operator returns True if the value of operand on left is greater than the value of operand on
right.
>>> 3 > 2
True
>>> 2 > 2
False
37
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In case of strings operands, > operator perform comparison according to the Unicode code point
(integer) of each character one-by-one.
The Unicode code point of a character can be obtained using the ord() function in Python.
The code point of first character of both operands are compared. In case they are equal, the code point
of next character of both operands are compared and the process continues.
For example,
>>> "python" > "Python"
True
The code point of "p" ( 112 ) is greater than the code point of "P" ( 80 ). As 112 is greater
than 80 the expression evaluates to True .
Let us take another example:
>>> "pYthon" > "python"
False
The code point of first character is same ( 112 ), so the next set of characters are compared. The code
point of "Y" ( 89 ) is not greater than the code point of "y" ( 121 ) so the expression evaluates
to False .
If two string operands p and q are of unequal lengths ( len(p) < len(q) ) and p is a substring
of q such that q = pt where t is any string of length greater than 0 , then q > p returns
True .
>>> "python" > "py"
True
In case of sequence operands like list or tuple , the items are compared one-by-one starting
from index 0 .
>>> ["p","py","PY"] > ["p","Py","PY"]
True
>>> [1, 3] > [1, 2]
True
>>> [1, 3, 4] > [1, 2]
True
In the above examples, "py" is greater than "Py" and 3 is greater than 2 respectively.
If two sequences are of unequal lengths and the smaller sequence is the starting subsequence of the
larger one, then the larger sequence is considered greater than the smaller one.
>>> [1, 2, 4] > [1, 2]
True
In case of sequence operands ( str , list , tuple ), the comparison operation performed is along
the same lines as the > operator discussed above.
>>> "python" >= "Python"
True
38
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Less than
The < operator returns True if the value of operand on left is less than the value of operand on
right.
>>> 2 < 3
True
>>> 3 < 3
False
In case of sequence operands ( str , list , tuple ), the comparison operation performed is along
the same lines as the > operator discussed above.
>>> "file" < "Pile"
False
# f(102) is > P(80)
>>> "py" < "python"
True
>>> ["Py", "PY"] < ["py", "PY"]
True
>>> ['a', 2] < ['a', 3]
True
>>> [1, 2] < [1, 2, 4]
True
The <= operator returns True if the value of operand on left is lesser than or equal to the value of
operand on right.
>>> 2 <= 3
True
>>> 3 <= 3
True
In case of sequence operands ( str , list , tuple ), the comparison operation performed is along
the same lines as the > operator discussed above.
>>> "file" <= "Pile"
False
# f(102) is > P(80)
>>> "py" <= "python"
True
>>> ["Py", "PY"] <= ["py", "PY"]
True
>>> ['a', 3] <= ['b', 2]
True
>>> [1, 2] <= [1, 2, 4]
True
39
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Assignment Operators
The assignment symbol ( = ) serves as a delimiter between the name and value in an assignment
statement.
It binds (or rebinds) a value (data, variable, expression) on the right to the target variable on the left.
>>> x = 1
>>> x
1
>>> y = x
>>> y
1
>>> y = "python"
>>> y
'python'
Binary operators can be combined with the assignment symbol to create Augmented Assignment
Operators.
These operators perform the binary operation on the two operands and assign the result to the original
target (left operand).
If <op> is a binary operator, then the expression a <op>= b containing the augmented assignment
operator is equivalent to a = a <op> b .
+=
The += operator adds a value (right operand) to the variable (left operand) and assigns the result to
that variable.
>>> a = 2
>>> a += 3
>>> a
5
>>> x = "hello"
>>> y = "world"
>>> x += y
>>> x
'helloworld'
-=
The -= operator subtracts a value (right operand) from the variable (left operand) and assigns the
result to that variable.
>>> a = 3
>>> a -= 2
>>> a
1
*=
The *= operator multiplies a value (right operand) to the variable (left operand) and assigns the result
to that variable.
>>> a = 3
>>> a *= 2
>>> a
6
>>> x = "hi"
>>> x *= 3
>>> x
'hihihi'
40
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
/=
The /= operator divides the variable (left operand) by a value (right operand) and assigns the result to
that variable.
>>> a = 4
>>> a /= 2
>>> a
2.0
//=
The //= operator floor divides the variable (left operand) by a value (right operand) and assigns the
result to that variable.
>>> a = 5
>>> a //= 2
>>> a
2
**=
The **= operator raises the variable (left operand) to a power (right operand) and assigns the result
to that variable.
>>> a = 4
>>> a **= 2
>>> a
16
%=
The %= operator computes modulus of the variable (left operand) and a value (right operand) and
assigns the result to that variable.
>>> a = 4
>>> a %= 3
>>> a
1
Logical Operators
Expressions using logical operators evaluate to a boolean value ( True or False ) based on the
logical state of the operands.
bool() built-in function can be used to determine the logical state of literals, variables or expressions.
41
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> bool("")
False
>>> bool([])
False
>>> bool(())
False
>>> bool({})
False
Some example literals having boolean state as True are provided below.
>>> bool(True)
True
>>> bool(1)
True
>>> bool(2.0)
True
>>> bool(100)
True
>>> bool("python")
True
>>> bool(["py", "thon"])
True
not
The logical state of an operand can be reversed ( False to True , and vice versa) using the logical
not operator.
>>> n = 5
>>> bool(n)
True
>>> bool(not n)
False
or
The logical or operator returns True if the logical state of any of the two operands is True .
>>> True or False
True
>>> bool(1 or 0)
True
>>> False or False
False
and
The logical and operator returns True if the logical state of both the operands is True .
>>> True and True
True
>>> True and False
False
>>> bool(10 and 20)
True
>>> bool(1 and 0)
False
42
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Identity Operators
We have already witnessed how Python treats every value or data item as an object.
The relational operator == can be used to test whether the operands contain the same value.
>>> n = 1
>>> n2 = 1
>>> n == n2
True
This operator however does not check if both the operands are referring to the same object or different
objects.
The identity operators is and is not are used to test whether two objects have the same or
different identity (pointing to the same location in memory) respectively.
a is b is equivalent to id(a) == id(b) , where id() is the built-in function which returns the
identity of an object.
>>> n = 1
>>> n2 = 1
>>> n is n2
True
In the above example, both variables n and n2 point to that same memory location (same object).
>>> l = [1, 2, 3]
>>> l2 = [1, 2, 3]
>>> l == l2
True
>>> l is l2
False
In the above example, both lists l and l2 although contain items with same values, are actually
two different objects occupying different memory locations.
Membership Operators
The operators in and not in test whether a value is present or not present in an iterable (string,
list, tuple, set, dictionary).
>>> 1 in [1, 2, 3]
True
>>> "ed" in ["ed", "py", "hi"]
True
>>> "ed" in ("ed", "py", "hi")
True
>>> 'ed' in {'ed': 1, 'py': 2}
True
Expressions
Literals (constants), identifiers (variables) and operators can be combined to form an expression which
always evaluates to a single value.
43
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
• a is T
• "Hello" + "World"
• 15 - a*4
• 3*num + 9/4 - 10%count**2
As shown above, standalone literals (like 10 ) and variables (like a ) are considered as expressions,
but standalone operators are not expressions.
Chained Expression
Comparison operators can be chained together in Python.
For example, lower <= age <= upper is a valid chained expression which is equivalent to the
expression -
lower <= age and age <= upper .
Conditional Expression
Python does not have ternary operators ( ?: ) like other programming languages. Hence, the keywords
if and else are used to create conditional expressions which evaluates to a value based on the
given condition.
For example,
var = t_val if cond else f_val
If the above condition cond evaluates to True , then the variable var is assigned t_val , else
it is assigned f_val .
>>> value = 1 if 2 > 3 else -1
>>> value
-1
44
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In Python, we have a large number of operators and a similar rule to determine the order of evaluation
of an expression. This is known as operator precedence where the operator with higher precedence is
evaluated before the operator with lower precedence in an expression.
The table below presents the precedence of operators in Python from highest to lowest. Operators in
the same row have the same precedence, so in such cases the expression is evaluated from left to right.
Operator Description
(expressions...) Parenthesized expression (Group)
** Exponentiation
+x , -x , ~x Unary positive, Unary negative, Bitwise NOT
* , @ , / , // , % Multiplication, Matrix multiplication, Division, Floor division,
Remainder
+ , - Addition, Subtraction
<< , >> Shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in , not in , is , is not , < , Membership, Identity & Comparisons
<= , > , >= , != , ==
not x Boolean NOT
and Boolean AND
or Boolean OR
:= Assignment expression
Exercises
Example 1
Evaluate the expression
15 - 2 * 4
Solution
Step:* has higher precedence over -
15 - 2 * 4
= 15 - 8
= 7
Example 2
Evaluate the expression
15 - 2 + 4
Solution
Step:- and + have the same order of precedence so the expression is evaluated left to right
15 - 2 + 4
= 13 + 4
= 17
Example 3
Evaluate the expression
15 - (2 + 4)
Solution
45
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Example 4
3 * 2 + 9 / 4 - 10 % 2 ** 2
Step 1
** takes precedence
3 * 2 + 9 / 4 - 10 % 2 ** 2
= 3 * 2 + 9 / 4 - 10 % 4
Step 2
* , / and % have the same precedence so they are evaluated left to right.
3 * 2 + 9 / 4 - 10 % 4
= 6 + 2.25 - 2
Step 3
+ and - evaluation
6 + 2.25 - 2
= 6.25
Example 5
20 / 4 // 2 * 2 - 4 + 20
Step 1
* , / , // and % have the same precedence so they are evaluated left to right.
20 / 4 // 2 * 2 - 4 + 20
= 5 // 2 * 2 - 4 + 20
= 2 * 2 - 4 + 20
= 4 - 4 + 20
Step 2
+ and - evaluation
4 - 4 + 20
= 20
Example 6
Step 1
** takes precedence
not 6 <= 4 and 3 ** 3 > 12 / 3
= not 6 <= 4 and 27 > 12 / 3
Step 2
46
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
47
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
48
Errors & Exception Handling
Error Types
A program contains “bug(s)” when it is unable to execute or produces an output which is different from
what is expected. These bugs are generally introduced by a programmer unknowingly.
The process of identifying and eliminating these bugs or errors is known as debugging.
The three major types of errors are:
• Syntax Error
• Runtime Error
• Logical Error
Syntax Error
Syntax error occurs when the program contains any statement that does not follow the prescribed
Python rules or syntax which makes it difficult for the Python interpreter to parse (understand) and
execute it.
Some common syntax errors are:
• Missing/Misspelled keyword
• Missing colon or brackets
• Empty block
• Incorrect position of keyword
• Incorrect block indentation
Script Mode
When a code containing syntactically incorrect statement is executed using script mode via IDLE, an
error dialog box is displayed.
49
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
On closing the dialog box, the incorrect part of the code, the potential cause of error, is highlighted in
red.
Interactive Mode
When a syntactically incorrect statement is executed in the Python console (interactive mode), the
Python interpreter displays it and also adds a little arrow ( ^ ) pointing at the entry point or token where
the error was detected.
Example
>>> while True print('Hi!')
File "<stdin>", line 1
while True print('Hi!')
^
SyntaxError: invalid syntax
In the above example there is a syntax error with ^ pointing to print function which the parser is
unable to understand as there is a missing : (colon) after True .
Runtime Error
A runtime error occurs when the program is terminated prematurely by the Python interpreter as it is
unable to execute a statement although it is correct syntactically.
Some runtime error examples are:
• ImportError: Raised when the import statement has trouble loading a module or any definition
from a module.
• IOError: Raised when the interpreter is not able to open the file specified in the program.
• ZeroDivisionError: Raised when a number is divided or mod by zero.
• NameError: Raised when an identifier is encountered which has not been defined.
• ValueError: Raised when an argument or operand is of required data type, but has undesired
value.
• IndexError: Raised when the provided index in a sequence (string, list, tuple, etc.) is out of range.
• KeyError: Raised when a dictionary key is not found in the set of existing keys.
• TypeError: Raised while performing an operation on incompatible types.
• IndentationError: Raised when the indentation of a statement or code block is incorrect.
50
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
NameError
n = 100
print(d)
KeyError
d = {1: "1st", 2: "2nd"}
print(d[3])
TypeError
n =1
s = "a"
tot = n + s
Logical Error
Logical error or Semantic error is caused when the issue lies in the underlying meaning of the code
which leads to an incorrect output.
As compared to syntax or runtime error there is no termination of the program.
Debugging a logical error requires inspection of the entire code as no guiding error message is displayed.
Example
Let us write a program to calculate the average of two numbers
n = 10
m = 20
avg = n + m / 2
print("Average:", avg)
51
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Average: 20.0
Exceptions
We have witnessed that even if a program is syntactically correct, its execution may lead to a run-time
error.
This error detected during execution is known as an exception which is an object created by the Python
interpreter containing information regarding the error like type of error, file name and the location of the
error (line number, token) in the program.
Some of the built-in exceptions that are raised by the Python interpreter are - ImportError ,
ZeroDivisionError , NameError , ValueError , IndexError , KeyError , TypeError
and IndentationError .
Apart from the Python interpreter, a programmer can also trigger and raise an exception (along with a
custom message) in the code using raise or assert statement.
raise
The raise statement can be used to throw an exception in a program. The exception may or may
not contain the custom error message (recommended).
Let us consider a program which accepts two numbers ( a and b ) from the user and prints the result
a/b .
Code
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("a/b =", a/b)
Output
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in <module>
print("a/b =", a/b)
ZeroDivisionError: division by zero
It can be observed that the Python interpreter raises a ZeroDivisionError when the value of b
is entered as 0 .
Now we can modify the above code to raise an exception for such scenarios.
52
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Code
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise Exception()
print("a/b =", a/b)
Output
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in <module>
raise Exception()
Exception
Code
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise Exception("b is zero")
print("a/b =", a/b)
Output
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in <module>
raise Exception("b is zero")
Exception: b is zero
We can also raise any specific type of error as per the program logic as shown below:
Code
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if b==0:
raise ValueError("The value of b cannot be zero")
print("a/b =", a/b)
Output
Enter a: 10
Enter b: 0
Traceback (most recent call last):
File "/Users/name/test.py",
line 4, in <module>
raise ValueError("The value of b cannot be zero")
ValueError: The value of b cannot be zero
53
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
assert
An assert statement is often used during code development to act like a safety valve which notifies
the programmer in case the test expression is evaluated as False .
If the test expression’s value is True , the code execution continues normally.
Code
a = 3
b = 4
assert a == b
c = 5
Output
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in <module>
assert a == b
AssertionError
Code
a = 3
b = 4
assert a == b, "a is not equal to b"
c = 5
Output:
Traceback (most recent call last):
File "/Users/name/test.py",
line 3, in <module>
assert a == b, "a is not equal to b"
AssertionError: a is not equal to b
Exception Handling
Exception handling is the process of properly handling an exception which can potentially crash a
program during execution.
The runtime system attempts to find an exception handler, a block of code that can handle a particular
type of error. Once located, the suitable exception handler catches the exeception and executes the
code block which can attempt to recover from the error. In case the error is unrecoverable, the handler
provides a way to gently exit the program.
The try statement in Python specifies the exception handlers and/or cleanup code for a code block.
• try block: The block of statements within which an exception might be thrown.
• except clause(s): One or more exception handlers. Each except clause handles a particular
type of exception. In case an exception of a particular type occurs in the try block, the
corresponding except clause code block is executed.
54
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
• else clause: An optional else clause can also be included after the last except block. In
case no exception is raised, none of the except blocks are executed. In this case, the else
code block is executed.
• finally clause: An optional finally clause can be added at the end of the try statement
which includes a block of statements that are executed regardless of whether or not any error
occured inside the try block. This block is usually setup for code cleanup and closing all open file
objects.
Here’s the general form of these statements:
try:
[code block]
except [exception1 [as identifier1]]:
[exception code block 1]
except [exception2 [as identifier2]]:
[exception code block 2]
...
...
else:
[code block executes if no error]
finally:
[code block always executed]
55
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
56
Control Flow
A simple Python program can be treated as a block of code where each statement is executed by the
Python interpreter in a sequential order from top to bottom.
But, in real world we would like to have some control over the execution of code such as:
This control over the flow of execution is provided by Control Flow Statements.
• Sequential
• Selection
• Iteration/Repetition
• Jump
• Procedural Abstraction - A sequence of statements are referenced as a single function or method
call
• Recursion - Calling a method/function in the same method/function
• Exception Handling
Sequential Flow
The below flow chart demonstrates how 3 statements are executed in a sequential order.
57
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Sequential Flow
For example,
a = 2
b = 3
c = a*b
print(c)
58
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Selection Flow
Python supports if compound statement which provides this control. The if statement comprises:
• if keyword followed by the test expression, a colon : and an indented block of code which
gets executed if the condition is satisfied
• (optional) one or many elif clause followed by their test conditions and their corresponding
code blocks
• (optional) else clause and the corresponding code block which gets executed if none of the
above conditions ( if , elif ) are satisfied
59
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Examples
Let us go through some programming problems which utilize selection statements.
1. Absolute Value
Write a program to output the magnitude of difference between two numbers using conditional statement.
Code
n1 = int(input("Enter 1st number: "))
n2 = int(input("Enter 2nd number: "))
if n1 > n2:
diff = n1 - n2
else:
diff = n2 - n1
Output
Enter 1st number: 12
Enter 2nd number: 15
The difference of 12 and 15 is 3
2. Sorting 3 Numbers
Write a program to accept 3 numbers from the user and print them in ascending order of value.
Code
a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
c = int(input("Enter 3rd number: "))
if b < a:
# Swapping the values of a and b
60
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
a, b = b, a
if c < b:
b, c = c, b
if b < a:
a, b = b, a
Output
Enter 1st number: 9
Enter 2nd number: 2
Enter 3rd number: 6
The numbers in sorted order: 2 , 6 , 9
3. Divisibility
Write a program to accept two numbers and test if the first number is divisible by the second number.
Code
a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
Output
Enter 1st number: 9
Enter 2nd number: 2
9 is not divisible by 2
Iteration: for
Iteration statements, also known as Looping statements, allow repeated execution of a code block.
The for statement can be used to iterate over the items of a sequence ( list , string , tuple
, range ). It can also be used to iterate over unordered sequences like set and dict .
61
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Let us go through some code examples to demonstrate how for statement can be used to iterate
over sequences.
List Iteration
Code
cars = ["Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen"]
for make in cars:
print(make)
Output
Hyundai
Honda
Ford
Toyota
BMW
Volkswagen
Tuple Iteration
Code
cars = ("Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen")
for make in cars:
print(make)
Output
Hyundai
Honda
Ford
62
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Toyota
BMW
Volkswagen
String Iteration
Code
name = "python"
for char in name:
print(char)
Output
p
y
t
h
o
n
Range Iteration
The range type represents an immutable sequence of numbers that is usually used in for loops for
looping a certain number of times. range object always take the same (small) amount of memory, no
matter the size of the range it represents, which is an advantage over a regular list or tuple .
Syntax: range(stop) or
range(start, stop[, step])
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 10, 2))
[1, 3, 5, 7, 9]
range() function is widely used in a for statement to control the number of iterations and provide
the index value ( i ) of each iteration.
Example #1
Print multiples of 5 starting from 0 to 20.
Code
for i in range(5):
print(i*5)
Output
0
5
10
15
20
Example #2
Print all integers from 2 to 5 including the boundary values.
Code
63
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Output
2
3
4
5
Example #3
Print all odd numbers between 2 and 10.
Code
for i in range(3, 10, 2):
print(i)
or
for i in range(2, 10):
if i % 2 != 0:
print(i)
Output
3
5
7
9
Example #4
Print the index of all occurrences of o in python programming .
Code
s = "python programming"
for i in range(len(s)):
if s[i] == "o":
print(i)
Output
4
9
Exercises
Let us go through some programming problems which utilize the for iteration statement.
1. Compound Interest
Write a program to calculate the total compound interest payable for given principal, interest rate
(compounded annually) and total time (in years).
Code
prin = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate: "))
time = int(input("Enter the loan duration (in years): "))
amt = prin
for n in range(time):
amt += rate*amt/100
64
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Output
Enter the principal amount: 500000
Enter the annual interest rate: 5
Enter the loan duration (in years): 3
Total interest payable: 78812.5
2. Factorial
The factorial of a positive integer n , denoted by n! , is the product of all positive integers less than
or equal to n .
n! = n×(n-1)×(n-2)...3×2×1
Write a program to calculate n! for a given n (assume n is greater than 0 ).
Code
n = int(input("Enter n: "))
factorial = 1
for i in range(1, n+1):
factorial *= i
Output
Enter n: 6
n! : 720
Iteration: while
while statement repeatedly executes a code block as long as the test condition is satisfied.
Usually there is a statement at the end of the code block which updates the value of the variable being
used in the test expression, so the the loop does not execute infinitely.
65
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
For example, let us traverse a list and print the position(index) and value of each element until we reach
the end of the list.
Code
cars = ["Hyundai", "Honda",
"Ford", "Toyota",
"BMW", "Volkswagen"]
i = 0
while i<len(cars):
print(i, cars[i])
i+=1
Output
0 Hyundai
1 Honda
2 Ford
3 Toyota
4 BMW
5 Volkswagen
In the above example, the test condition is i<len(cars) and the update statement is i+=1 .
Exercises
Let us go through some programming problems which utilize the while iteration statement.
1. Compound Interest
Write a program to calculate the total compound interest payable for given principal, interest rate
(compounded annually) and total time (in years).
Code
prin = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate: "))
66
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
amt = prin
while time > 0:
amt += rate*amt/100
time = time - 1
Output
Enter the principal amount: 500000
Enter the annual interest rate: 5
Enter the loan duration (in years): 3
Total interest payable: 78812.5
2. Factorial
The factorial of a positive integer n , denoted by n! , is the product of all positive integers less than
or equal to n .
n! = n×(n-1)×(n-2)...3×2×1
Write a program to calculate n! for a given n (assume n is greater than 0 ).
Code
n = int(input("Enter n: "))
factorial = 1
while n > 0:
factorial *= n
n = n - 1
Output
Enter n: 6
n! : 720
Jump Statements
Jump statements are used to (abruptly) alter the flow of execution.
Some of the jump statements available in Python are:
pass
A pass statement acts as a placeholder and performs null (no) operation.
Various reasons for using the keyword pass are provided below:
1. Syntactical Requirement
Using pass becomes a syntactical requirement for cases where the Python interpreter can raise a
SyntaxError due to missing statements.
The below code will execute successfully without any operation in the loop
for i in range(6):
pass
67
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Similarly, inside an if
if 2 < 3:
pass
For example,
Code
l = [2, 3, 4, 5, 6]
for i in l:
if i%3 == 0:
pass
else:
print(i, "is not divisible by 3")
Output
2 is not divisible by 3
4 is not divisible by 3
5 is not divisible by 3
3. Placeholders
pass can be used to create valid empty functions and classes as placeholders which can be modified
in the future versions of code.
def emptyFunction():
pass
class EmptyClass:
pass
break
The break statement is used to terminate the execution of immediately enclosing for or while
statement.
The below code will terminate the for loop when i is equal to 4
68
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
for i in range(10):
print(i)
if i == 4:
break
0
1
2
3
4
In a while statement,
i =0
while i <10:
print(i)
if i == 4:
break
i+=1
continue
continue statement is used to skip the execution of successive statements and start the next iteration.
The below code will skip all candidates for an interview who have less than 4 years of work experience.
people = [{"name": "ABC", "experience": 6},
{"name": "EFG", "experience": 2},
{"name": "JKL", "experience": 5},
{"name": "XYZ", "experience": 3},]
for candidate in people:
if candidate["experience"]<4:
continue
print(candidate["name"], "is selected for interview")
Output:
ABC is selected for interview
JKL is selected for interview
Nested Loops
When a loop is present inside another loop, it is known as a nested loop.
For each iteration of the outer loop, the inner loop undergoes complete iteration. Thus, if the outer loop
has to undergo n iterations and the inner loop has to undergo m iterations, the code block inside
the inner loop executes n x m times.
Let us go through a nested loop example:
Factorial
Write a program to print the factorial of all numbers in the range 1 to 10 (inclusive).
69
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Code
for n in range(1, 11):
factorial = 1
for i in range(1, n+1):
factorial *= i
print(n,"! =", factorial)
Output
1 ! = 1
2 ! = 2
3 ! = 6
4 ! = 24
5 ! = 120
6 ! = 720
7 ! = 5040
8 ! = 40320
9 ! = 362880
10 ! = 3628800
Output
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
70
Strings
s1 = """A
multiline
String"""
s2 = '''Also a
multiline
String'''
Escape Characters
The backslash ( \ ) character can be used in a string to escape characters that otherwise have a special
meaning, such as newline, linefeed, or the quote character.
Although \' and \" can be used to specify quote characters, Python allows embedding double
71
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
quotes inside a single-quoted string ( 'My name is "Python".' ) and single quotes inside a double-
quoted string ( "Python's World" ).
Unicode Support
Python string objects support Unicode characters.
A unicode character can be specified as \u followed by the 4 letter unicode ( \uXXXX ).
>>> print("E = mc\u00B2")
E = mc²
In the above example, \u00B2 is the unicode character which represents the ‘SUPERSCRIPT TWO’.
>>> str(10.0)
'10.0'
If s = "hello" ,
Index of -1 denotes the last character of the string, -2 is the second last character and so forth.
If s = "hello" ,
Length of String
The built-in function len() returns the length of a string which is useful during string traversal or
other string operations.
72
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> len("hello")
5
>>> p = "python"
>>> l = len(p)
>>> p[l-1]
'n'
String Operations
We can perform various operations on a string (sequence of characters) such as slicing, membership,
concatenation and repetition.
Slicing
In Python, a character in a string can be easily accessed using its index.
>>> s = "Hello"
>>> s[1]
'e'
Python also provides a way to access a substring from a string. This substring is known as a slice and
it can be obtained using the slice operator [n:m] which returns the part of the string from the start
index ( n ) to the end index ( m ), including the first but excluding the last.
>>> s = "Hello"
>>> s[1:3]
'el'
If the start index ( n ) is omitted, the default value of n is set as 0 which denotes the beginning of
the string. If the end index ( m ) is omitted, the substring ends at the last character of the string.
>>> s = "Hello"
>>> s[:3]
'Hel'
>>> s[3:]
'lo'
>>> s[:]
'Hello'
The slice operator also allows the usage of a third index which is known as step as it allows a user to
step over (skip) characters.
>>> s = "Hello"
>>> s[0:5:2]
'Hlo'
73
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In the above example, the substring begins at the start of the string, takes a step size of 2 skipping
e and ends at the last character again skipping the 4th character l .
Membership
in and not in operators can be used to determine whether a substring is present/not present in a
string.
>>> s = "Hello"
>>> "lo" in s
True
>>> "lp" not in s
True
Concatenation
The + operator can be used to join two strings.
>>> s1 = "Hello"
>>> s2 = "Python"
>>> s1 + s2
'HelloPython'
>>> s1 + "World"
'HelloWorld'
Repetition
The * operator repeats a string the number of times as specified by the integer operand.
>>> s = "Hello"
>>> s*3
'HelloHelloHello'
lower()
All cased characters are converted into lowercase.
>>> "PYthon".lower()
'python'
74
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
upper()
All cased characters are converted into uppercase.
>>> "PYthon".upper()
'PYTHON'
swapcase()
Uppercase characters are converted into lowercase and lowercase characters are converted into
uppercase.
>>> "PYthon".swapcase()
'pyTHON'
capitalize()
First character is capitalized and rest all characters are lowercased.
>>> "hello py".capitalize()
'Hello py'
title()
For each word present in the string, the first character is uppercased and the remaining characters are
lowercased.
>>> "hello python".title()
'Hello Python'
isalpha()
Returns True if all characters in the string are letters ( A-Z a-z ).
>>> "HelloPython".isalpha()
True
>>> "HelloPython2".isalpha()
False # contains digit
isdigit()
Returns True if all characters in the string are digits.
>>> "Hello24".isdigit()
False # contains alphabets
>>> "24".isdigit()
True
75
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
isalnum()
Returns True if all characters in the string are alphanumeric (Alphabets or Numeric).
>>> "02".isalnum()
True
>>> "HelloPython".isalnum()
True
>>> "Hello Python v2".isalnum()
False # contains whitespace
>>> "HelloPythonv2".isalnum()
True
isascii()
Returns True if the string is empty or all characters in the string are ASCII.
>>> "".isascii()
True
>>> "HelloPython".isascii()
True
>>> "Hello Py \u00B2".isascii()
False
>>> "पा से python".isascii()
False
islower()
Returns True if all characters are lowercase.
>>> "hello".islower()
True
>>> "Hello".islower()
False
isupper()
Returns True if all characters are uppercase.
>>> "HELLO".isupper()
True
>>> "Hello".isupper()
False
isspace()
Returns True if there are only whitespace characters in the string. Some popular whitespace
characters are (space), \t (tab), \n (newline), \r (carriage return), \f (form feed) and \v
(vertical tab).
>>> " ".isspace()
True
istitle()
Returns True if the string is title-cased, i.e., the first character of every word in the string is uppercased
and the remaining characters are lowercased.
76
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Split a String
Split methods help in splitting/partitioning a string.
partition()
partition(sep) method splits the string when the separator ( sep ) is encountered for the first time,
and returns a tuple with three items (string before separator, separator, string after separator)
.
>>> "Hi|Ed|Punk".partition('|')
('Hi', '|', 'Ed|Punk')
split()
split(sep=None, maxsplit=-1) method splits a string into a list based on a string separator (
sep ).
If sep is not specified, it defaults to None , where whitespace is regarded as separator, and the
string is stripped of all leading and trailing whitespaces after which it is split into words contained in the
string.
>>> "Hi|Ed|Punk".split('|')
['Hi', 'Ed', 'Punk']
>>> "Hi Ed Punk".split()
['Hi', 'Ed', 'Punk']
>>> " Hi Ed Punk ".split()
['Hi', 'Ed', 'Punk']
If maxsplit is provided, at most maxsplit number of splits are performed and the list will contain
a maximum of maxsplit+1 elements.
maxsplit when not specified defaults to -1 , which implies that there is no limit on the number of
splits.
>>> "Hi|Ed|Punk|v2".split('|', 2)
['Hi', 'Ed', 'Punk|v2']
They accept an optional argument chars which specifies the set of characters which have to be
removed.
If no arguments are provided then chars defaults to ASCII whitespace and the method removes all
leading and/or trailing spaces from the string.
77
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
lstrip()
All leading (on the left) characters are removed from the string.
>>> " Hello|World ".lstrip()
'Hello|World '
>>> "www.edpunk.cc".lstrip('w.')
'edpunk.cc'
rstrip()
All trailing (on the right) characters are removed from the string.
>>> " Hello|World ".rstrip()
' Hello|World'
>>> "www.edpunk.cc".rstrip('.c')
'www.edpunk'
strip()
All leading and trailing characters are removed from the string.
>>> " Hello|World ".strip()
'Hello|World'
>>> "www.edpunk.cc".strip('cw.')
'edpunk'
count()
count(sub[, start[, end]]) returns the number of non-overlapping occurrences of a substring
sub in the range [start, end] .
start and end are optional parameters and they default to 0 and len(string) respectively.
78
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
It has to be noted that the method counts non-overlapping occurrences, so it does not start a new
matching process until the current substring matching is complete.
>>> s = "valhala alala"
>>> s.count("al")
4
>>> s.count("ala")
2
In the above example, ala is counted twice as the first occurence is in valh"ala" and the next
occurance is in "ala"la . Although ala can be located again in al"ala" , it overlaps with the
occurance "ala"la , hence it is not counted.
find()
find(sub[, start[, end]]) returns the lowest index in the string where substring sub is located
in the range [start, end] .
start and end are optional parameters and they default to 0 and len(string) respectively.
rfind()
rfind(sub[, start[, end]]) returns the highest index in the string where substring sub is
located in the range [start, end] .
start and end are optional parameters and they default to 0 and len(string) respectively.
79
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
-1
>>> s.rfind("she", 5)
14
index()
index(sub[, start[, end]]) is similar to find(sub[, start[, end]]) , but instead of re-
turning -1 it raises ValueError when the substring is not found.
>>> s = "she sells sea shells"
>>> s.index("she")
0
>>> s.index("she", 5)
14
>>> s.index("see")
Traceback (most recent call last):
File "<stdin>", line 1,
in <module>
ValueError: substring not found
>>> s.index("she", 5, 10)
Traceback (most recent call last):
File "<stdin>", line 1,
in <module>
ValueError: substring not found
>>> s.index("she", 5, 17)
14
rindex()
s.rindex(sub[, start[, end]]) is similar to rfind(sub[, start[, end]]) , but instead of
returning -1 it raises ValueError when the substring is not found.
>>> s = "she sells sea shells"
>>> s.rindex("she")
14
>>> s.rindex("she", 0, 12)
0
>>> s.rindex("see")
Traceback (most recent call last):
File "<stdin>", line 1,
in <module>
ValueError: substring not found
>>> s.rindex("she", 5)
14
replace()
replace(oldsub, newsub[, count]) returns a copy of the string with all occurrences of oldsub
substring replaced by newsub .
count is an optional parameter which when provided, only replaces the first count occurrences
from the string.
>>> s = "Oh Python! Oh"
>>> s.replace("Oh", "Hi")
'Hi Python! Hi'
80
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Traversing a String
for and while statements are useful for traversing a string.
Using for
As a string is a sequence of characters, the for statement can be used to iterate over a string as
shown below.
Code
name = "python"
for char in name:
print(char)
Output
p
y
t
h
o
n
Using while
A while statement can be used to traverse a string by iterating on the value of index till the last
character index.
Code
name = "python"
i = 0
while i < len(name):
print(name[i])
i += 1
Output
p
y
t
h
o
n
81
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
82
List
Let us create a list of attributes of a particular vehicle available for sale in a car dealership:
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
In this list:
This method of creating a list from a collection of literals is known as list display.
Notice, how this list contains items of multiple data types - str , int and bool .
Apart from the list display shown above, the built-in list() function can also be used to create new
lists.
If a string, tuple or set is passed as an argument, list() functions converts them into a list.
>>> l = list("abcd")
>>> l
['a', 'b', 'c', 'd']
>>> l = list({1, 2, 3})
>>> l
[1, 2, 3]
>>> l = list((1, 2, 3, 4))
>>> l
[1, 2, 3, 4]
83
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
For example,
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
>>> l[0]
'BMW'
>>> l[2]
2019
Length of List
The built-in len() function can be used to return the length of the list.
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
>>> len(l)
6
Negative Indexing
Just like str , sequence types like list support negative indexing, i.e., you can access the values
of list from the end.
Index of -1 denotes the last item in the list, -2 the second last item and so forth.
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
>>> l[-1]
True
>>> l[-3]
4
Modifying a List
As lists are mutable, the assignment operator can be used to modify or replace a single item in the list
at a given index.
>>> l = ["BMW", "Z4", "Red"]
>>> l[2] = "Black"
>>> l
['BMW', 'Z4', 'Black']
84
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
List Operations
We can perform various operations on a list (collection of items) such as membership, concatenation,
repetition and slicing.
Membership
in and not in can be used to detect the membership of an item in a list.
>>> l = ["BMW", "Z4", "Red"]
>>> "Red" in l
True
>>> "Gold" in l
False
>>> "Gold" not in l
True
Concatenation
+ operator can be used to join two lists to create a new list.
>>> l = ["BMW", "Z4"]
>>> k = [2019, "Red"]
>>> l + k
['BMW', 'Z4', 2019, 'Red']
Repetition
The * operator repeats the items of a list the number of times as specified by the integer operand.
>>> l = [1, 2]
>>> l*3
[1, 2, 1, 2, 1, 2]
Slicing
A subset of list l can be obtained using the list slice notation given as l[i:j] , where the item at
start index i is included, but the item at end index j is excluded.
For example, the slice notation [1:4] refers to items from index 1 to index 3 (i.e. 4-1 ).
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
>>> l[1:4]
['Z4', 2019, 4]
The slice notation l[i:j:k] can also include a third number known as the stepper. Here, a list is
sliced from start index i to end index ( j ) - 1 with a step of k items.
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
85
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> l[1:4:2]
['Z4', 4]
Slice notations also have some useful defaults. 0 is the default for the first number and size of the list is
the default for the second number.
>>> l = ["BMW", "Z4", 2019,
... 4, "Red", True]
>>> l[2:]
[2019, 4, 'Red', True]
>>> l[:4]
['BMW', 'Z4', 2019, 4]
Traversing a List
List Traversal is the process of visiting every item in a list, usually from the first item to the last item, and
executing some instruction on the accessed item.
86
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Direct Traversal
As a list is an ordered collection of items, the for statement can be used to directly and sequentially
access each item.
Code
l = ["BMW", "Z4", 2019,
4, "Red", True]
for item in l:
print(item)
Output
BMW
Z4
2019
4
Red
True
This index value can be used to access the item at that index in a list using the index operator [] .
for statement can be used to iterate over the index of the list using the range() and len()
functions.
Code
l = ["BMW", "Z4", 2019,
4, "Red", True]
for i in range(len(l)):
print(i, l[i])
Output
0 BMW
1 Z4
2 2019
3 4
4 Red
5 True
This method is also useful when you need to modify the items of a list (without altering the length of list)
during traversal.
For example, let us convert each item of the list into a string.
Code
l = ["BMW", "Z4", 2019,
4, "Red", True]
for i in range(len(l)):
# modifying item value
l[i] = str(l[i])
print(l)
Output
['BMW', 'Z4', '2019', '4', 'Red', 'True']
87
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
A while statement can be used to traverse a list by iterating on the value of index till the last item
index.
Code
l = ["BMW", "Z4", 2019,
4, "Red", True]
i = 0
while i < len(l):
print(l[i])
i += 1
Output
BMW
Z4
2019
4
Red
True
If the item has to be added at a particular index, insert() method can be used.
88
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
extend() method appends each item of the argument list at the end of the target list.
>>> l = ["T", "C", 2, 4]
>>> l.extend([2.5, "SE"])
>>> l
['T', 'C', 2, 4, 2.5, 'SE']
remove() method removes the first occurrence of an item from the list.
>>> l = ["Hi", "Ed", "Py", "Hi"]
>>> l.remove("Hi")
>>> l
['Ed', 'Py', 'Hi']
If an index is provided, pop() method removes the item at that index, else the last item is removed
from the list.
>>> l = ["T", "C", 2, 4, "S"]
>>> item = l.pop()
>>> item
'S'
>>> l
['T', 'C', 2, 4]
In case i and j are not specified they default to i=0 and j=len(l) .
>>> l = [34, 4, 6, 23, 4]
>>> l.index(4)
1
>>> l.index(4, 3)
4
89
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> l.index(6, 1, 4)
2
Reversing Items
reverse() method can be used to reverse a list in-place.
>>> l = ["T", "C", 2, 4, "S"]
>>> l.reverse()
>>> l
['S', 4, 2, 'C', 'T']
If you do not wish to modify the existing list and create a new list with items in reverse order, use the
built-in function reversed() nested in the built-in list() .
>>> l = ["T", "C", 2, 4, "S"]
>>> new_l = list(reversed(l))
>>> new_l
['S', 4, 2, 'C', 'T']
>>> l
['T', 'C', 2, 4, 'S']
Sorting a List
Python lists have a built-in sort() method which sorts the items in-place using < comparisons
between items.
The method also accepts 2 keyworded arguments:
- key is used to specify a function which is called on each list element prior to making the comparisons.
- reverse is a boolean which specifies whether the list is to be sorted in descending order.
>>> l = [34, 4, 6, 23]
>>> l.sort()
>>> l
[4, 6, 23, 34]
90
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> l.sort(key=str.lower)
>>> l
['ed', 'Hi', 'Oh', 'Py']
If you do not wish to modify the existing list and create a new list with sorted items in, use the built-in
sorted() function which returns a new sorted list.
>>> l = [34, 4, 6, 23]
>>> new_l = sorted(l)
>>> new_l
[4, 6, 23, 34]
Copying a List
In Python, we can create an object (data) and bind its reference to a variable using the assignment
operator ( = ).
As multiple collections or items in collections can point to the same mutable object, a copy is required
so one can change one copy without changing the other.
Let us take an example:
>>> old_l = [1, 2, 3]
It can be seen how the assignment operator does not create a new copy of the list.
The copy() method can be used to create a new list containing the items of the original list.
>>> old_l = [1, 2, 3]
91
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Assigning a slice of the entire list ( [:] ) is also equivalent to creating a new copy.
>>> old_l = [1, 2, 3]
Nested List
A list of lists is also known as a nested list.
Previously, we learnt how to create a list of attributes of a particular vehicle available for sale in a
dealership.
Let us now create a nested list containing some details of vehicles available for sale.
# Make Model Year No_of_wheels
l = [["Kia", "Sonnet", 2019, 4],
["Toyota", "Camry", 2018, 4],
["BMW", "Z4", 2015, 4],
["BMW", "S1000", 2016, 2],
["KTM", "390", 2019, 2],
]
Thus, nested list is useful in representing a dataset where each item is a list (datum) containing the
attributes of an observation.
l[i][j] is the syntax to fetch the j+1th item of the list at index i of the nested list l .
>>> l[2][1]
'Z4'
List Comprehension
A short and succinct way of creating a new list by transforming an existing list is via list comprehension.
Also, due to the optimizations present in the Python interpreter, there are performance benefits of using
list comprehension.
Member-wise Operation
List comprehension can be used to make a new list where each element is the result of some operations
applied to each member of another sequence or iterable.
For example, to create a new list where each item is squared.
92
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> l = [2, 3, 4, 5, 6, 8]
>>> l2 = [i**2 for i in l]
>>> l2
[4, 9, 16, 25, 36, 64]
Filtering or Subsequence
List comprehension can also be used to create a subsequence of those elements that satisfy a certain
condition.
For example, to create a new list where each item is divisible by 2.
>>> l = [2, 3, 4, 5, 6, 8]
>>> l2 = [i for i in l if i%2==0]
>>> l2
[2, 4, 6, 8]
Sample Programs
1. Min, Max and Mean
Write a program to find the minimum, maximum and mean of values stored in a list input by user.
a) Without Using Built-in Functions
Code
#create an empty list
l = []
Output
Enter length of list: 5
Enter item: 3
93
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Enter item: 2
Enter item: 6
Enter item: 9
Enter item: 1
Minimum : 1.0
Maximum : 9.0
Mean : 4.2
Output
Enter length of list: 5
Enter item: 3
Enter item: 2
Enter item: 6
Enter item: 9
Enter item: 1
Minimum : 1.0
Maximum : 9.0
Mean : 4.2
2. Linear Search
Write a program to enter a list and then check if the number entered by a user is present in that list.
Code
#create an empty list
l = []
found = False
for i in range(n):
if l[i] == search:
found = True
break
if found:
94
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Output
Enter length of list: 4
Enter item: 2
Enter item: 6
Enter item: 3
Enter item: 7
Enter search value: 3
Search value located at index 2
3. Frequency of Elements
Write a program to enter a list and then print the frequency of elements present in that list.
a) Without Using Built-in Functions
Code
#create an empty list
l = []
for val in l:
if val not in items:
items.append(val)
counts.append(1)
else:
for idx in range(len(items)):
if items[idx] == val:
counts[idx] += 1
print("Frequency of Elements")
for idx in range(len(items)):
print(items[idx], "-", counts[idx])
Output
95
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
print("Frequency of Elements")
for val in l:
if val not in items:
items.append(val)
print(val, "-", l.count(val))
Output
Enter length of list: 8
Enter item: 2
Enter item: 3
Enter item: 3
Enter item: 4
Enter item: 2
Enter item: 5
Enter item: 6
Enter item: 6
Frequency of Elements
2 - 2
3 - 2
4 - 1
5 - 1
6 - 2
96
Tuple
List vs Tuple
Tuples are like lists, with a difference - they are immutable. This means that once initialized a user
cannot modify its value, which makes it a useful feature to ensure the sanctity of data and guarantee
that it is not being modified by the program.
tuple()
Apart from the method shown above, the built-in tuple() function can also be used to create new
tuples.
If a string, tuple or set is passed as an argument, tuple() functions converts them into a tuple.
>>> t = tuple("abcd")
>>> t
('a', 'b', 'c', 'd')
>>> t = tuple({1, 2, 3})
>>> t
(1, 2, 3)
>>> t = tuple([1, 2, 3, 4])
>>> t
(1, 2, 3, 4)
What is a Singleton?
In case a tuple has only 1 item, it is known as a singleton.
It is a good practice to include a trailing comma to avoid the Python interpreter from treating it as a
value inside regular parentheses as shown in the examples below.
97
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Tuples use zero-based indexing, where 0 is the index of the first element and len(tuple) - 1 is
the index of the last element.
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
>>> t[0]
'BMW'
>>> t[2]
2019
Length of Tuple
The built-in len() function can be used to return the length of the tuple.
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
>>> len(t)
6
Negative Indexing
Just like list , tuple supports negative indexing, i.e., you can access the values of tuple from the
end. Index of -1 denotes the last item in the tuple, -2 is the second last item and so forth.
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
>>> t[-1]
True
>>> t[-3]
4
98
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Tuple Operations
We can perform various operations on a tuple such as membership, concatenation, repetition and
slicing.
Membership
in and not in can be used to detect the membership of an item in a tuple.
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
>>> "Red" in t
True
>>> "Gold" in t
False
>>> "Gold" not in t
True
Concatenation
+ operator can be used to join two tuples to create a new tuple.
>>> t = ("BMW", "Z4")
>>> u = (2019, "Red")
>>> t+u
('BMW', 'Z4', 2019, 'Red')
Repetition
The * operator creates a new tuple with items of a tuple repeated the number of times as specified
by the integer operand.
>>> t = (1, 2)
>>> t*3
(1, 2, 1, 2, 1, 2)
Tuple Slicing
A subset of tuple t can be obtained using the tuple slice notation given as t[i:j] , where the item
at index i is included, but the item at index j is excluded.
For example, the slice notation [1:4] refers to items from index 1 to index 3 (i.e. 4-1 ).
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
99
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> t[1:4]
('Z4', 2019, 4)
The slice notation t[i:j:k] can also include a third number known as the step. Here, a tuple is
sliced from start index i to end index ( j ) - 1 with a step of k items.
>>> t = ("BMW", "Z4", 2019,
... 4, "Red", True)
>>> t[1:4:2]
('Z4', 4)
Traversing a Tuple
Tuple Traversal is the process of visiting every item in a tuple, usually from the first item to the last item,
and executing some instruction on the accessed item.
Python provides 2 ways to traverse a tuple:
Direct Traversal
As a tuple is an ordered collection of items, the for statement can be used to directly and sequentially
access each item.
Code
t = ("BMW", "Z4", 2019,
4, "Red", True)
for item in t:
print(item)
Output
BMW
Z4
2019
4
Red
True
100
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
This index value can be used to access the item at that index in a tuple using the index operator [] .
for statement can be used to iterate over the index of the tuple using the range() and len()
functions.
Code
t = ("BMW", "Z4", 2019,
4, "Red", True)
for i in range(len(t)):
print(t[i])
Output
BMW
Z4
2019
4
Red
True
A while statement can be used to traverse a tuple by iterating on the value of index.
Code
t = ("BMW", "Z4", 2019,
4, "Red", True)
index = 0
while index < len(t):
print(t[index])
index += 1
Output
BMW
Z4
2019
4
Red
True
101
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
An item x can be located in a tuple using the index(x[, i[, j]]) method which returns the
first occurrence of the item at or after index i and before index j . In case i and j are not
specified they default to i=0 and j=len(t) .
>>> t = (34, 4, 6, 23, 4)
>>> t.index(4)
1
>>> t.index(4, 3)
4
>>> t.index(6, 1, 4)
2
102
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> tuple(sorted(t))
('Hi', 'Oh', 'Py', 'it')
# lowercase the words before sorting
>>> tuple(sorted(t, key=str.lower))
('Hi', 'it', 'Oh', 'Py')
Nested Tuple
A tuple of tuples is also known as a nested tuple.
Let us create a nested tuple containing the details of all vehicles available for sale.
# Make Model Year No_of_wheels
t = (
("Kia", "Sonnet", 2019, 4),
("Toyota", "Camry", 2018, 4),
("BMW", "Z4", 2015, 4),
("BMW", "S1000", 2016, 2),
("KTM", "390", 2019, 2),
)
t[i][j] is the syntax to fetch the j+1 th item of the tuple at index i of the nested tuple t .
>>> t[2][1]
'Z4'
As compared to a nested list, a nested tuple is useful in representing a dataset (such as a table fetched
from a database) where it is important to ensure the sanctity of data as the code cannot modify it
because tuples are immutable.
In case of a list, the modified list l still points at the same object.
Now, let us add items to a tuple.
>>> t = ("Hi", "Ed", "Punk")
>>> id_t = id(t)
>>> t += (1, 2)
>>> t
103
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In case of a tuple, the modified tuple is actually a completely new tuple with contents of the original
tuple and the extension.
The original tuple is not modified as it is immutable. But, as t is no longer pointing to the original
tuple, it is freed from memory.
Thus, it is recommended that instead of += , append() and extend() methods should be
employed to add new items programatically as it will raise an error in case the code is trying to modify a
tuple.
>>> l = ["Hi", "Ed", "Punk"]
>>> l.extend([1, 2])
>>> l
['Hi', 'Ed', 'Punk', 1, 2]
104
Dictionary
What is a Dictionary?
Python provides a mapping type collection which contains keys, and values corresponding to those
keys.
This collection is known as a dictionary (type dict ) , where each key is unique and can be used
to easily store or retrieve values (any data-type including string , int , float , list ).
Dictionaries are indexed by keys (any immutable type - numbers, string, tuple) as compared to lists
which are indexed by a range of numbers.
>>> d = dict()
>>> d
{}
>>> type(d)
<class 'dict'>
To create a new dictionary with some initial values, one can use:
105
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Keyword/Named Arguments
Keys and values can be passed as keyword arguments to the dict() function.
>>> d = dict(yr=20, name="Ed",
... is18=True)
>>> d
{'yr': 20, 'name': 'Ed', 'is18': True}
One of the limitations of this method is that the keys of the dictionary are only of type string and their
names must be within the namespace of an identifier.
The zipped iterator can be passed as an argument to the dict() to create a new mapping collection.
>>> m = ["yr", "name", 18]
>>> n = [20, "Ed", True]
>>> list(zip(m, n))
[('yr', 20), ('name', 'Ed'), (18, True)]
>>> dict(zip(m, n))
{'yr': 20, 'name': 'Ed', 18: True}
get()
get(key[, default]) method can also be used to fetch the value for a key if key is in the dictionary,
else a default value is returned.
106
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
This is an elegant method of fetching a value from a dictionary and it should be used in real life software
codes as it never raises a KeyError .
Updating a Dictionary
Index Operator []
d[x] = y can be used to add the value y corresponding to a key x in a dictionary d .
In case key x already exists, assignment updates or overwrites the value corresponding to it.
>>> d = {"yr": 20, 18: True}
>>> d["name"] = 'Ed'
>>> d
{'yr': 20, 18: True, 'name': 'Ed'}
update()
update() method can be used to add new items or update existing items in a dict .
3. Keyworded arguments
>>> d = {"yr": 20, 18: True}
>>> d.update(name="Ed", yr=15)
>>> d
{'yr': 15, 18: True, 'name': 'Ed'}
107
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
setdefault()
setdefault(key[, default]) method returns a value corresponding to a key.
If the key is not present in the dictionary, it is inserted with the provided default value.
clear()
clear() method can be used to clear all values of a dictionary.
>>> d = {"yr": 20, "name": "Ed",
... 18: True}
>>> d.clear()
>>> d
{}
pop()
pop(key[, default]) method can be used to remove a key: value pair.
In case the key is not present in the dictionary, the method returns the value of argument default
provided by the user.
>>> d = {"yr": 20, "name": "Ed",
... 18: True}
>>> d.pop("name")
'Ed'
>>> d
{'yr': 20, 18: True}
>>> d.pop("name")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
108
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
KeyError: 'name'
popitem()
popitem() can be used to destructively iterate over a dictionary removing and returning a
(key, value) pair in LIFO (Last Item First Out) order.
>>> d = {"yr": 20, "name": "Ed",
... 18: True}
>>> d.popitem()
(18, True)
>>> d.popitem()
('name', 'Ed')
>>> d.update({"name": "py"})
>>> d
{'yr': 20, 'name': 'py'}
>>> d.popitem()
('name', 'py')
>>> d.popitem()
('yr', 20)
>>> d
{}
Dictionary Operations
Membership
in keyword can be used to test the presence of a key in a dictionary.
>>> d = {"yr": 20, 18: True}
>>> "yr" in d
True
Union
The | (union) operator can be used to merge two dictionaries.
>>> d = {"yr": 20, 18: True}
>>> n = {"name": "Ed"}
>>> d | n
{'yr': 20, 18: True, 'name': 'Ed'}
In case of common keys between the two operands (dictionaries), the values of the operand on the right
takes priority.
>>> d = {"yr": 20, 18: True}
>>> n = {"yr": 15, "name": "Ed"}
>>> d | n
{'yr': 15, 18: True, 'name': 'Ed'}
109
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In the above example, both d and n shared a common key yr . The value corresponding to yr
in n gets priority.
Traversing a Dictionary
Compared to position based indexing of list or tuple , dictionaries are indexed based on the
key.
Direct Traversal
for loop can be used to directly iterate over the keys of a dict .
d = {"yr": 20, "name": "Ed",
18: True}
for key in d:
print(key,":", d[key])
Output
yr : 20
name : Ed
18 : True
dict_keys , dict_values and dict_items are iterables that can be used in a for statement
to traverse the dictionary.
Code
d = {"yr": 20, "name": "Ed",
18: True}
for key in d.keys():
print(key)
Output
yr
name
18
Code
d = {"yr": 20, "name": "Ed",
18: True}
for value in d.values():
print(value)
Output
110
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
20
Ed
True
Code
d = {"yr": 20, "name": "Ed",
18: True}
for key, value in d.items():
print(key,":", value)
Output
yr : 20
name : Ed
18 : True
111
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
False
>>> new_d["c"] = 3
>>> new_d["b"] = 4
>>> new_d
{'a': 1, 'b': 4, 'c': 3}
>>> old_d
{'a': 1, 'b': 2}
Nested Dictionary
When the value in a key:value pair of a dictionary is of type dict , it is known as a nested
dictionary.
In simple terms, it is a dictionary inside a dictionary.
Records of a table can be represented as a nested dict with primary key as the outer dict keys.
d = { 1: {"name": "Amar", "age": 19},
2: {"name": "Ria", "age": 20},
3: {"name": "Rao", "age": 18}}
print(d[1])
print(d[2]["name"])
Output
{'name': 'Amar', 'age': 19}
Ria
Sample Programs
Frequency of Characters
Write a program to count the number of times a character appears in a given string.
Code
d = {}
s = input("Enter string: ")
for c in s:
if c in d:
d[c] += 1
else:
d[c] = 1
print("Frequency of characters")
for key in d:
print(key, "-", d[key])
Output
Enter string: pythondata
Frequency of characters
p - 1
y - 1
t - 2
h - 1
o - 1
112
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
n - 1
d - 1
a - 2
Salary Book
Write a program to enter employee names and their salaries as input and store them in a dictionary.
Code
n = int(input("Enter number of employees: "))
emp = dict()
for cnt in range(n):
name = input("Enter employee name: ")
salary = int(input("Enter salary: "))
emp[name] = salary
Output
Enter number of employees: 3
Enter employee name: Anil Raj
Enter salary: 12000
Enter employee name: Dinesh Kumar
Enter salary: 15000
Enter employee name: Sam Singh
Enter salary: 10000
Employee Name & Salary
Anil Raj - 12000
Dinesh Kumar - 15000
Sam Singh - 10000
113
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
114
Python Standard Library
Built-in Functions
Python has a rich and extensive Standard Library which gives it an edge over traditional programming
languages.
Python Standard Library contains 69 built-in functions that are commonly used by programmers which
saves a lot of time as they can be directly used in a program.
We have already used some functions in the previous sections like input() , output() , len() ,
sum() , min() , max() , list() , dict() , etc.
Some of the widely used functions can be categorized as follows:
• Mathematical Functions
• Type Functions
• Input/Output Functions
• Base/Unicode Functions
Mathematical Functions
abs()
abs(x) returns the absolute value or magnitude of x of type int , float or complex number.
>>> abs(-2.3)
2.3
>>> abs(-10)
10
>>> abs(2 + 2j)
2.8284271247461903
round()
round(x [,ndigits]) rounds off x ( float ) to ndigits precision after the decimal point.
sum()
sum(sequence [,start]) returns the sum of items in a sequence of type list , tuple ,
range or set .
115
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
min()
min(sequence) returns the minimum value of items in a sequence of type list , tuple ,
range , str or set .
Apart from iterables, min(arg1, arg2, ..) also accepts multiple arguments arg1 , arg2 .. of
numeric type and returns the smallest among these arguments.
>>> min([1, 2, -1])
-1
>>> min(1, 2, -1)
-1
max()
max(sequence) returns the maximum value of items in a sequence of type list , tuple ,
range , str or set .
Apart from iterables, max(arg1, arg2, ..) also accepts multiple arguments arg1 , arg2 .. of
numeric type and returns the largest among these arguments.
>>> max([1, 2, -1])
2
>>> max(1, 2, -1)
2
pow()
pow(base, exp [,mod]) raises a base ( int , float ) to the power exp ( int , float
), i.e. base**exp .
116
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
divmod()
divmod(a, b) returns a tuple (a // b, a % b) consisting of the quotient and remainder when
a ( int , float ) is divided by b ( int , float ).
>>> divmod(23, 3.5)
(6.0, 2.0)
>>> divmod(-10, 7)
(-2, 4)
Type Functions
Python has built-in functions to handle various aspects of datatypes such as checking and conversion.
Type Checking
type() and isinstance() builtin functions are used for checking the data type of objects.
Check out the Type Checking section in the chapter Variable, Objects & Data Types to learn more
about it in detail.
The Type Casting section of the chapter Variable, Objects & Data Types covers these functions in
detail in case any argument is passed.
I/O Functions
Python provides the following built-in functions to handle user input and result output:
input()
input() function can be used to accept a user input and assign it to a variable.
When this function is encountered in the code, the python interpreter waits for the user to type a response
which is read as a string.
117
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Code
name = input("Enter name: ")
print("Hello,", name)
Output
Enter name: Python
Hello, Python
print()
The built-in print() function can be used to display an output (value of variables, expressions, etc.)
on the standard output.
For example, the below code computes the area of a rectangle and displays it:
Code
length = 10
breadth = 5
area = length * breadth
print("Area:", area)
Output
Area: 50
Both, input() and print() functions are covered in detail in the chapter Input & Output.
open()
open() function is used to open a file and return the corresponding file object.
This function supports various modes useful for reading from a file and writing to a file.
oct()
Converts an int to an octal (base-8) string prefixed with '0o' .
>>> oct(9)
'0o11'
118
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
hex()
Converts an int to a lowercase hexadecimal (base-16) string prefixed with "0x" .
>>> hex(29)
'0x1d'
ord()
Returns the integer Unicode code point for the given Unicode character str .
>>> ord("a")
97
>>> ord("β")
946
chr()
Converts the integer Unicode code point into the corresponding Unicode string.
>>> chr(97)
'a'
>>> chr(946)
'β'
The above import statement loads all the functions available in the math module. To access
any function in the module, simply type the module name followed by a period ( . ), followed by the
function name.
>>> import math
>>> math.pow(3, 2)
9.0
Instead of loading all the functions in a module, the from statement can be used to access only
specified functions.
>>> from math import pow
>>> pow(3, 2)
9.0
math Module
math module provides mathematical functions and constants as defined by the C standard.
119
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Constants
The following frequently used mathematical constants are available in math module:
Functions
Following useful mathematical functions are available in the math module:
fabs()
gcd()
ceil()
floor()
fmod()
fmod(x, y) returns the value of the expression x - n*y such that the result has the same sign
as x and magnitude less than |y| for some integer n .
This function should be preferred when working with floating point numbers as compared to x % y
that should be used when working with integers.
>>> import math
>>> math.fmod(2.14, 0.5)
0.14000000000000012
120
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
pow()
sqrt()
sin(x) , cos(x) and tan(x) return the sine, cosine and tangent of x (radians) respectively.
>>> import math
>>> math.cos(math.pi/3)
0.5000000000000001
>>> math.sin(math.pi/2)
1.0
>>> math.tan(math.pi/4)
0.9999999999999999
factorial()
factorial(n) computes the factorial of a positive integer n which is the product of all positive
integers less than or equal to n .
n! = n×(n-1)×(n-2)...3×2×1 ,
where 0! = 1
>>> import math
>>> math.factorial(5)
120
random Module
random module provides access to functions which can generate random numbers.
random()
random() function draws a random float in the half open interval [0.0, 1.0) with a uniform
probability.
>>> import random
>>> random.random()
0.6857133962949904
121
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
randrange(start, stop) is used to randomly select an integer from a range start to stop
(excluding).
>>> import random
>>> random.randrange(5, 10)
8
statistics Module
statistics module provides functions that are useful for calculating mathematical statistics of
numeric (Real-valued) data.
Mean
mean(data) returns the arithmetic mean or average value of data ( list or tuple of int
or float ).
>>> from statistics import mean
>>> mean([1, 2, 3, 6])
3
Median
median(data) returns the median or middle value of data ( list or tuple of int or
float ) using the common “mean of middle two” method.
>>> from statistics import median
>>> median([1, 2, 3, 6])
2.5
Mode
mode(data) returns the most commonly occurring value in the data ( list or tuple of int
, str or float ).
If there are multiple modes with the same count, the first occurrence in the sequence is returned.
>>> from statistics import mode
>>> mode([1, 2, 3, 6])
1
>>> mode([1, 2, 2, 3, 6])
2
>>> mode(["a", "b", "a", "c"])
'a'
122
File Handling
123
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
file_name is a required string parameter specifying the path of the file to be opened.
It can either be an absolute path or a relative path as shown below:
• 'fname.txt' is the relative path of a text file residing in the current working directory from
where the python script is being executed.
• '../fname.txt' is the relative path of a text file outside the current directory where the python
script is being executed.
• '/Users/edpunk/Documents/fname.txt' is the absolute path of a text file which can be
opened by the python script from any location as long as it is in the same system.
mode
mode is an optional string parameter which specifies the mode in which the file has to be opened. It
defaults to 'r' which means open for reading in text mode.
The available modes are:
mode purpose
'r' Opens the file for reading (default).
't' Opens the file in text mode (default).
'w' Opens the file for writing, truncating (emptying) the file if it already exists.
'x' Same as 'w' , but it fails if the file already exists.
'a' Opens the file for writing, where any new data is added at the end. It creates a new file
if the file does not exists.
'b' Opens the file in binary mode.
'rb' Opens the file in binary and read-only mode.
'wb' Opens the file for writing in binary mode, truncating (emptying) the file if it already
exists.
'+' Allows both read and write operations on a file.
'r+' Opens the file in read and write mode. It throws an error in case the file does not
exists. If the file already exists, new data is overwritten on top of the existing data if
the position of stream is not moved to the end of the file.
124
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
mode purpose
'r+b' Opens the file in binary read and write mode. It does not truncate the file if it already
exists.
'w+' Opens the file in read and write mode. It creates a new file or truncates the contents of
the file if it already exists.
'w+b' Opens the file in binary read and write mode. It creates a new file or truncates the
contents of the file if it already exists.
'a+' Opens the file in read and append mode. It creates a new file if it does not exist. If the
file already exists, new data is automatically added at the end of the file after existing
data.
'a+b' Opens the file in binary read and append mode.
After opening a file and performing some file operations, it can be safely closed using the close()
method.
For example, let us write a program to open a file data.txt in read and append mode and close the
file object in the successive statement.
f = open('data.txt', 'a+')
f.close()
File Reading
Opening a file using the open() function creates and returns a file object on which the following
activities can be performed:
Sequential Reading
The following three methods can be used on a file object to sequentially read the contents of a file:
read()
read(size = -1) method is used to read size numbers of bytes of data from a file at a time.
In case size is not specified, it reads and returns the entire content of the file.
Example
Let us consider a file named info.txt containing the below text:
Hello World
Thank You
Code
>>> f = open('info.txt', 'r')
>>> print(f.read(3))
Hel
>>> print(f.read(4))
lo W
>>> f.close()
125
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
readline()
readline(size = -1) method without any arguments is used to read and return one line at a time
from the given file.
If size is specified, it returns at most size bytes of data or the data until it encounters the newline
character.
Example
Let us consider a file named info.txt containing the below text:
Hello World
Thank You
Code
>>> f = open('info.txt', 'r')
>>> f.readline(3)
'Hel'
Since readline() returns one row at a time, it can be used in a while statement to iterate over
the data row-wise. Once it reaches the end of file it returns an empty string.
Code
f = open('info.txt', 'r')
line = f.readline()
while line:
# Removes newline
# character from the
# end of the line
line = line.strip()
print(line)
Output
Hello World
Thank You
for statement can also be used to traverse the file row-wise without any requirement of the
readline() method. Simply iterating over the file object returns the data one row at a time.
Code
f = open('info.txt', 'r')
for line in f:
# Removes newline
126
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Output
Hello World
Thank You
readlines()
readlines() method returns a list of strings representing the lines (along with the newline character)
of text file content.
Example
Let us consider a file named info.txt containing the below text:
Hello World
Thank You
Code
>>> f = open('info.txt', 'r')
>>> f.readlines()
['Hello World\n', 'Thank You']
Ad-hoc Reading
The following two methods can be used on a file object to randomly access contents of a file:
tell()
tell() returns the current byte position (integer) of the file object in the file.
Let us consider the same file info.txt for the below example:
>>> f = open('info.txt', 'r')
>>> f.read(4)
'Hell'
>>> f.tell()
4
seek()
seek(offset, reference=0) can be used to seek a location in the file object which is offset
bytes from the provided reference .
The default value for reference is 0 which stands for the beginning of the file. For this default
reference , the offset has to be a whole number ( >=0 ).
• 1 , which denotes that the offset will be calculated from the current position of the file object (
offset can be positive or negative)
• 2 , which denotes that the offset is calculated from the end of the file ( offset is negative)
127
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Note: In text files (those opened without a b in the mode string), only seek relative to the beginning of
the file ( reference = 0 ) is allowed. reference = 1 or 2 is only valid when the file is opened
in binary mode.
Let us consider the same file info.txt for the below example:
>>> f.seek(6)
6
>>> f.read()
'World\nThank You'
>>> f.tell()
21
>>> f.seek(12)
12
>>> f.read()
'Thank You'
Writing a File
The following modes support file writing:
• Writing from the beginning of a file : r+ , r+b , w , x , wb , w+ , w+b .
• Appending at the end of an existing file : a , ab , a+ , a+b .
The following methods can be used to write data in a file:
write()
>>> f.close()
Now, on opening info.txt using a text editor, the following content is displayed.
Hi World
Thank You
writelines()
128
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
This method does not add the line separator (newline character \n ), so they should be present at the
end of each string.
>>> lines = ["Hi World\n",
... "Thank You\n"]
>>> f = open("info.txt", "w")
>>> f.writelines(lines)
>>> f.close()
Now, on opening info.txt using a text editor, the following content is displayed.
Hi World
Thank You
Note the empty line at the end which is added due to the \n at the end of the last string.
In Python, the protocols to read and write binary files ( .dat or .pickle file extension) have been
implemented in the built-in pickle module.
pickle is a Python specific binary file format which can not only be used to store binary data, but
also store any Python object.
This process of translating data structures (lists, dictionaries, etc.) and code objects (classes, functions,
etc.) into bytes that can be stored in a binary file is known as Serialization. This binary file can be
stored on disk or shared and it can be de-serialized and used later via Python.
Dumping Data
The dump() function can be used to write the pickled representation of any data or object into a file.
Syntax: pickle.dump(obj,file)
where:
• obj is the object to be written
• file is an open file object (opened in binary write wb or append ab mode).
>>> import pickle
>>> l = [["Anita","Maths",83],
... ["Amar","Maths",95],
... ["Ani","Maths",90]]
>>> f = open("marks.dat", "wb")
>>> pickle.dump(l, f)
>>> f.close()
Loading Data
The load() function can be used to read the pickled representation of an object or data from a file.
Syntax: pickle.load(file)
129
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
>>> l
[['Anita', 'Maths', 83], ['Amar', 'Maths', 95], ['Ani', 'Maths', 90]]
print("File contents:")
130
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
Input/Output
Enter student name: Anita
Enter subject: Maths
Enter marks: 83
Enter 1 to add more or 0 to terminate: 1
Enter student name: Amar
Enter subject: Maths
Enter marks: 95
Enter 1 to add more or 0 to terminate: 0
File contents:
['Anita', 'Maths', 83.0]
['Amar', 'Maths', 95.0]
End of file reached
Enter student name: Akash
Enter subject: Science
Enter marks: 92
Enter 1 to add more or 0 to terminate: 1
Enter student name: Ira
Enter subject: Science
Enter marks: 99
Enter 1 to add more or 0 to terminate: 0
File contents after appending:
['Anita', 'Maths', 83.0]
['Amar', 'Maths', 95.0]
['Akash', 'Science', 92.0]
['Ira', 'Science', 99.0]
End of file reached
131
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
To demonstrate why quote characters are required, let us have a look at the below contents of a CSV
file which contains the average marks of some subjects (separated by comma) for each student.
Name,Average Of,Marks
Anita,"Accounting,Maths",83.0
Amar,"Accounting,Maths",95.0
Akash,"Physics,Chemistry,Biology",92.0
Ira,"Physics,Chemistry,Biology",99.0
Python has built-in csv module which provides utilities to read and write CSV files.
The csvfile file object should be opened with newline='' argument as the csv module has
its own newline handling which correctly interprets the newlines depending on platform or in case they
are embedded inside quoted fields.
Example #1
Let us write a program to read the contents of marks.csv .
marks.csv
Name,Subject,Marks
Anita,Maths,83.0
Amar,Maths,95.0
Akash,Science,92.0
Ira,Science,99.0
Code
import csv
with open('marks.csv',
newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
Output
['Name', 'Subject', 'Marks']
['Anita', 'Maths', '83.0']
['Amar', 'Maths', '95.0']
['Akash', 'Science', '92.0']
['Ira', 'Science', '99.0']
Example #2
Let us write a program to read the contents of marks2.csv .
Note the change in delimiter & quote character.
marks2.csv
Name|Average Of|Marks
Anita|^Accounting|Maths^|83.0
Amar|^Accounting|Maths^|95.0
Akash|^Physics|Chemistry|Biology^|92.0
Ira|^Physics|Chemistry|Biology^|99.0
Code
132
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
import csv
with open('marks2.csv',
newline='') as f:
reader = csv.reader(f,
delimiter='|',
quotechar='^')
for row in reader:
print(row)
Output
['Name', 'Average Of', 'Marks']
['Anita', 'Accounting|Maths', '83.0']
['Amar', 'Accounting|Maths', '95.0']
['Akash', 'Physics|Chemistry|Biology', '92.0']
['Ira', 'Physics|Chemistry|Biology', '99.0']
The methods writerow(row) or writerows(rows) can be used to write a row (list of strings) or
list of rows to a CSV file.
Example #3
Write a program to create a CSV file containing subject-wise marks of students delimited by semi-colon
( ; ).
Solution #1: Using writerow()
import csv
h = ["Name","Subject","Marks"]
marks = [["Anita","Maths",83.0],
["Amar","Maths",95.0],
["Akash","Science",92.0],
["Ira","Science",99.0]]
# Writing header
writer.writerow(h)
#Writing Data
for row in marks:
writer.writerow(row)
h = ["Name","Subject","Marks"]
133
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
marks = [["Anita","Maths",83.0],
["Amar","Maths",95.0],
["Akash","Science",92.0],
["Ira","Science",99.0]]
# Writing header
writer.writerow(h)
#Writing Data
writer.writerows(marks)
Output - marks.csv
Name;Subject;Marks
Anita;Maths;83.0
Amar;Maths;95.0
Akash;Science;92.0
Ira;Science;99.0
134
User Defined Functions, Modules &
Packages
Function Structure
Let us take a deep dive into a user defined function (provided below) and understand its various
components:
def adder(f, s, t = None):
"""
Returns the sum of f, s and t.
If t is not provided,
return the sum of f and s.
"""
s = f + s
if t:
s += t
return s
Function Header
def adder(f, s, t = None):
The function name follows the def keyword. As it is an identifier, the same nomenclature is followed
while naming it. adder is the name of the function in the above example.
135
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
In case any parameters are required by the function, they are enclosed inside the parentheses.
f, s, t = None are the three parameters of the function.
Finally, the function header ends with a colon.
Function Body
The body of the function consists of one or more Python statements which have the same amount of
indentation (4 spaces) from the function header.
It is a good practice to include the documentation string at the beginning of the function body that
briefly explains how it works. This docstring can either be a single-line or a multiline string literal. In the
above example the docstring is:
"""
Returns the sum of f, s and t.
If t is not provided,
return the sum of f and s.
"""
This docstring is followed by a series of statements which represent the set of instructions to be executed.
The set of instructions in the above example are
s = f + s
if t:
s += t
The code block finally ends with a return statement which returns one or more values.
return s
After defining the above function, let us now invoke or call the function:
fst = 20
snd = 10
trd = 10
sm1 = adder(fst, snd, trd)
sm2 = adder(fst, snd)
f , s and t are known as positional parameters as they have a defined position in the function
definition.
Also there is a provision to assign a default value to a parameter using = .
136
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
The values of sm1 and sm2 after executing the script are:
>>> sm1
40
>>> sm2
30
Scope of Variables
In Python, every variable has a well defined scope, i.e., it is accessible by a part of the program and it
cannot be accessed from outside its scope.
Based on their scope, there are two types of variables:
Local Variables
Local variables are defined inside a function or a code block and can only be accessed where they are
defined.
They exist only during the execution of the function or the code block.
Code
def triple(a):
result = 3*a
# variable result
# is accessible locally
print("local:", result)
Output
local: 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined
Global Variables
Variables defined outside any function or code block are known as global variables.
137
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
They are often used to specify mathematical constants, file path or other such values and can be
accessed anywhere in the source code (by functions or code blocks).
Below example demonstrates how the global variable n can be accessed by all the functions.
Code
n = 10
def uno():
print(n)
def tres():
print(n*3)
uno()
tres()
Output
10
30
In case a variable is defined inside a function with the same name as that of a global variable, then
the variable is considered as a local variable and all references made to the variable point to this local
variable.
Code
n = 10
uno()
tres()
Output
5
30
Any changes made to a global variable inside a code block or a function can modify it for that session.
This can have an impact on all the code blocks/functions that access and use it.
To modify the value of a global variable one can use the global keyword as shown in the example
below.
Code
n = 10
138
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
n = 5
print(n)
def tres():
print(n*3)
uno()
tres()
Output
5
15
Passing a List
Code
def repeat(l):
for i in range(len(l)):
l[i] = l[i]*2
l = [1, 2, 3]
print(l)
repeat(l)
# l is modified
print(l)
Output
[1, 2, 3]
[2, 4, 6]
Passing a Dictionary
Code
def repeat(d):
for i in d.keys():
d[i] = d[i]*2
Output
{'a': 1, 'b': 2, 'c': 3}
{'a': 2, 'b': 4, 'c': 6}
139
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
To solve this problem we can create a python script with the function definitions at the beginning of teh
file, followed by the rest of the code which includes statements invoking the defined functions.
But, this process is tedious and not managable as what makes user-defined functions powerful is that
the programmer can - Write once, and use many times.
Instead of repeating the function definition again and again for each new program, one can put all the
function definitions in a file from which the required function can be imported and invoked either in
script mode or interactive mode.
This file ( .py extension) is known as a module and it is the most basic form of reusable code
accessible by other programs.
def tripler(a):
"""
Multiplies a by 3 and
returns it
"""
result = 3*a
return result
After saving the basics.py file, reopen IDLE and create a new file test.py in the same directory
as basics.py.
The name of the file is the module name which is also available as the value of the global variable
__name__ in the module.
Import the functions of the basics module in test.py by executing the following statement.
import basics
The above import statement loads all the functions available in the basics module. To access
any function in the module, simply type the module name followed by a period ( . ), followed by the
function name.
Code
import basics
sa = basics.adder(20, 10, 10)
st = basics.tripler(20)
print(sa)
print(st)
Output
140
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
40
60
The as keyword can also be used to create an alias which makes it easier and more manageable to
use in the program.
Code
import basics as ba
sa = ba.adder(20, 10, 10)
st = ba.tripler(20)
print(sa)
print(st)
Output
40
60
Instead of loading all the functions in a module, the from statement can be used to access only
specified functions.
Code
from basics import adder
sa = adder(20, 10, 10)
print(sa)
Output
40
To import all functions and global variables in the module the * wildcard character can be used.
Code
from basics import *
sa = adder(20, 10, 10)
st = tripler(20)
Output
40
60
The above method of using * to import the contents of a module is not recommended as it can
clutter the namespace and cause issues if there are conflicts between the identifiers defined by the
programmer and those defined in the module/package.
141
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
f = int(input("Enter a: "))
s = int(input("Enter b: "))
print(multiply(f, s))
Now when we try to load all functions from the module it automatically executes the input assignment
statements and prints the output.
>>> from test import *
Enter a: 4
Enter b: 5
20
>>>
if __name__ == '__main__':
f = int(input("Enter a: "))
s = int(input("Enter b: "))
print(multiply(f, s))
Now, the block will execute only if the script is executed directly and not when the file is imported as a
module.
>>> from test import *
>>> multiply(4, 5)
20
These modules are present in a folder along with the __init__.py file which tells Python that this
folder is a package.
A package can also contain subfolders (sub-packages), each containing their respective __init__.py
files.
Let us take the example of a package called restaurant which consists of various modules to
perform activities such as order booking, reservation, staff attendance, etc.
Here is a possible structure for the package:
restaurant/
__init__.py
orders.py
reservation.py
employee.py
inventory.py
A package is simply the directory containing sub-packages and modules, but when this package or a
collection of packages are made available for others to use (eg. via PyPI) it is known as a library.
For example, restaurant can be called a library if it provides reusable codes to manage a restaurant
and is built using multiple packages which handle the various aspects of a restaurant like human resource
management, inventory management, order fulfilment and billing, etc.
142
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
One should note that the above definition is not strict and often the terms package and library are used
interchangeably.
143
�㶻 github.com/animator/learn-python · �㰐 edpunk.com Ankit Mahato
144