Python SDFSD
Python SDFSD
While working on Ubuntu Linux, you have to follow the same steps, save the code and run from Linux
terminal. We use vi editor for saving the program.
Thus, we can write and run Hello World program in Python using the interpreter mode and script mode.
Python - Application Areas
Python is a general-purpose programming language. It is suitable for development of wide range of
software applications. Over last few years Python is the preferred language of choice for developers in
following application areas −
Python for Data Science
Python's recent meteoric rise in the popularity charts is largely due its Data science libraries. Python has
become an essential skill for data scientists. Today, real time web applications, mobile applications and
other devices generate huge amount of data. Python's data science libraries help companies generate
business insights from this data.
Libraries like NumPy, Pandas and Matplotlib are extensively used to apply mathematical algorithms to the
data and generate visualizations. Commercial and community Python distributions like Anaconda and
ActiveState bundle all the essential libraries required for data science.
Python for Machine Learning
Python libraries such as Scikit-learn and TensorFlow help in building models for prediction of trends like
customer satisfaction, projected values of stocks etc. based upon the past data. Machine learning
applications include (but not restricted to) medical diagnosis, statistical arbitrage, basket analysis, sales
prediction etc.
Python for Web Development
Python's web frameworks facilitate rapid web application development. Django, Pyramid, Flask are very
popular among the web developer community. etc. make it very easy to develop and deploy simple as
well as complex web applications.
Latest versions of Python provide asynchronous programming support. Modern web frameworks leverage
this feature to develop fast and high performance web apps and APIs.
Python for Computer Vision and Image processing
OpenCV is a widely popular library for capturing and processing images. Image processing algorithms
extract information from images, reconstruct image and video data. Computer Vision uses image
processing for face detection and pattern recognition. OpenCV is a C++ library. Its Python port is
extensively used because of its rapid development feature.
Some of the application areas of computer vision are robotics, industrial surveillance and automation,
biometrics etc.
Python for Embedded Systems and IoT
Micropython (https://micropython.org/), a lightweight version especially for microcontrollers like
Arduino. Many automation products, robotics, IoT, and kiosk applications are built around Arduino and
programmed with Micropython. Raspberry Pi is also very popular alow cost single board computer used
for these type of applications.
Python for Job Scheduling and Automation
Python found one of its first applications in automating CRON (Command Run ON) jobs. Certain tasks like
periodic data backups, can be written in Python scripts scheduled to be invoked automatically by
operating system scheduler.
Many software products like Maya embed Python API for writing automation scripts (something similar to
Excel micros).
Try Python Online
If you are new to Python, it is a good idea to get yourself familiar with the language syntax and features
by trying out one of the many online resources, before you proceed to install Python software on your
computer.
You can launch Python interactive shell from the home page of Python's official website
https://www.python.org/.
In front of the Python prompt (>>>), any valid Python expression can be entered and evaluated.
Python - Interpreter
Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/
directory. For Windows, the executable (python.exe) is found in the installation folder (for example C:\
python311). In this chapter, you will how Python interpreter works, its interactive and scripted mode.
Python code is executed by one statement at a time method. Python interpreter has two components.
The translator checks the statement for syntax. If found correct, it generates an intermediate byte code.
There is a Python virtual machine which then converts the byte code in native binary and executes it. The
following diagram illustrates the mechanism:
IPython provides information (introspection) of any object by putting '?' in front of it. It includes
docstring, function definitions and constructor details of class. For example to explore the string object
var defined above, in the input prompt enter var?.
In [5]: var = "Hello World"
In [6]: var?
Type: str
String form: Hello World
Length: 11
Docstring:
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
IPython's magic functions are extremely powerful. Line magics let you run DOS commands inside IPython.
Let us run the dir command from within IPython console
In [8]: !dir *.exe
Volume in drive F has no label.
Volume Serial Number is E20D-C4B9
Directory of F:\Python311
Although you can straight away proceed by clicking the Install Now button, it is advised to choose the
installation folder with a relatively shorter path, and tick the second check box to update the PATH
variable.
Accept defaults for rest of the steps in this installation wizard to complete the installation.
Open the Window Command Prompt terminal and run Python to check the success of installation.
C:\Users\Acer>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Python's standard library has an executable module called IDLE − short for Integrated Development and
Learning Environment. Find it from Window start menu and launch.
IDLE contains Python shell (interactive interpreter) and a customizable multi-window text editor with
features such as syntax highlighting, smart indent, auto completion etc. It is cross-platform so works the
same on Windows, MacOS and Linux. It also has a debugger with provision to set breakpoints, stepping,
and viewing of global and local namespaces.
Install Python on MacOS
Earlier versions of MacOS used to have Python 2.7 pre-installed in it. However, now that the version no
longer supported, it has been discontinued. Hence, you need to install Python on your own.
On a Mac computer, Python can be installed by two methods −
Using the official installer
Manual installation with homebrew
You can find macOS 64-bit universal2 installer on the downloads page of the official website −
https://www.python.org/ftp/python/3.11.2/python-3.11.2-macos11.pkg
The installation process is more or less similar to that on Windows. Usually, accepting the default options
during the wizard steps should do the work.
The frequently required utilities such as PIP and IDLE are also installed by this installation wizard.
Alternately, you can opt for the installation from command line. You need to install Homebrew, Mac's
package manager, if it is not already available. You can follow the instructions for installation at
https://docs.brew.sh/Installation.
After that, open the terminal and enter the following commands −
brew update && brew upgrade
brew install python3
Latest version of Python will now be installed.
Install Python from Source Code
If you are an experienced developer, with good knowledge of C++ and Git tool, you can follow the
instructions in this section to build Python executable along with the modules in the standard library.
You must have the C compiler for the OS that you are on. In Ubuntu and MacOS, gcc compiler is available.
For Windows, you should install Visual Studio 2017 or later.
Steps to Build Python on Linux/Mac
Download the source code of Python's latest version either from Python's official website or its GitHub
repository.
Download the source tarball : https://www.python.org/ftp/python/3.11.2/Python3.11.2.tgz
Extract the files with the command −
tar -xvzf /home/python/Python-3.11.2.tgz
Alternately, clone the main branch of Python's GitHub repository. (You should have git installed)
git clone -b main https://github.com/python/cpython
A configure script comes in the source code. Running this script will create the makefile.
./configure --enable-optimizations
Followed by this, use the make tool to build the files and then make install to put the final files in
/usr/bin/ directory.
make
make install
Python has been successfully built from the source code.
If you use Windows, make sure you have Visual Studio 2017 and Git for Windows installed. Clone the
Python source code repository by the same command as above.
Open the windows command prompt in the folder where the source code is placed. Run the following
batch file
PCbuild\get_externals.bat
This downloads the source code dependencies (OpenSSL, Tk etc.)
Open Visual Studio and PCbuild/sbuild.sln solution, and build (press F10) the debug folder shows
python_d.exe which is the debug version of Python executable.
To build from command prompt, use the following command −
PCbuild\build.bat -e -d -p x64
Thus, in this chapter, you learned how to install Python from the pre-built binaries as well as from the
source code.
Setting Up the PATH
When the Python software is installed, it should be accessible from anywhere in the file system. For this
purpose, the PATH environment variable needs to be updated. A system PATH is a string consisting of
folder names separated by semicolon (;). Whenever an executable program is invoked from the
command line, the operating system searches for it in the folders listed in the PATH variable. We need to
append Python's installation folder to the PATH string.
In case of Windows operating system, if you have enabled "add python.exe to system path" option on the
first screen of the installation wizard, the path will be automatically updated. To do it manually, open
Environment Variables section from Advanced System Settings.
Edit the Path variable, and add a new entry. Enter the name of the installation folder in which Python has
been installed, and press OK.
To add the Python directory to the path for a particular session in Linux −
In the bash shell (Linux) − type export PATH="$PATH:/usr/bin/python3.11" and press Enter.
Python Command Line Options
We know that interactive Python interpreter can be invoked from the terminal simply by calling Python
executable. Note that no additional parameters or options are needed to start the interactive session.
user@ubuntu20:~$ python3.11
Python 3.11.2 (main, Feb 8 2023, 14:49:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("Hello World")
Hello World
>>>
Python interpreter also responds to the following command line options −
-c <command>
Interpreters execute one or more statements in a string, separated by newlines (;) symbol.
user@ubuntu20:~$ python3 -c "a=2;b=3;print(a+b)"
5
-m <module-name>
Interpreter executes the contents of named module as the __main__ module. Since the argument is a
module name, you must not give a file extension (.py).
Consider the following example. Here, the timeit module in standard library has a command line
interface. The -s option sets up the arguments for the module.
C:\Users\Acer>python -m timeit -s "text = 'sample string'; char = 'g'
'char in text'"
5000000 loops, best of 5: 49.4 nsec per loop
<script>
Interpreter executes the Python code contained in script with .py extension, which must be a filesystem
path (absolute or relative).
Assuming that a text file with the name hello.py contains print ("Hello World") statement is present in the
current directory. The following command line usage of script option.
C:\Users\Acer>python hello.py
Hello World
? Or -h or −help
This command line option prints a short description of all command line options and corresponding
environment variables and exit.
-V or --version
This command line option prints the Python version number
C:\Users\Acer>python -V
Python 3.11.2
C:\Users\Acer>python --version
Python 3.11.2
Python Environment Variables
The operating system uses path environment variable to search for any executable (not only Python
executable). Python specific environment variables allow you to configure the behaviour of Python. For
example, which folder locations to check to import a module. Normally Python interpreter searches for
the module in the current folder. You can set one or more alternate folder locations.
Python environment variables may be set temporarily for the current session or may be persistently
added in the System Properties as in case of path variable.
PYTHONPATH
As mentioned above, if you want the interpreter should search for a module in other folders in addition
to the current, one or more such folder locations are stored as PYTHONPATH variable.
First, save hello.py script in a folder different from Python's installation folder, let us say c:\modulepath\
hello.py
To make the module available to the interpreter globally, set PYTHONPATH
C:\Users\Acer>set PYTHONPATH= c:\modulepath
C:\Users\Acer>echo %PYTHONPATH%
c:\modulepath
Now you can import the module even from any directory other than c:\modulepath directory.
>>> import hello
Hello World
>>>
PYTHONHOME
Set this variable to change the location of the standard Python libraries. By default, the libraries are
searched in /usr/local/pythonversion in case of Linux and instalfolder\lib in Windows. For example, c:\
python311\lib.
PYTHONSTARTUP
Usually, this variable is set to a Python script, which you intend to get automatically executed every time
Python interpreter starts.
Let us create a simple script as follows and save it as startup.py in the Python installation folder −
print ("Example of Start up file")
print ("Hello World")
Now set the PYTHONSTARTUP variable and assign name of this file to it. After that start the Python
interpreter. It shows the output of this script before you get the prompt.
F:\311_2>set PYTHONSTARTUP=startup.py
F:\311_2>echo %PYTHONSTARTUP%
startup.py
F:\311_2>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Example of Start up file
Hello World
>>>
PYTHONCASEOK
This environment is available for use only on Windows and MacOSX, not on Linux. It causes Python to
ignore the cases in import statement.
PYTHONVERBOSE
If this variable is set to a non-empty string it is equivalent to specifying python -v command. It results in
printing a message, showing the place (filename or built-in module) each time a module is initialized. If
set to an integer − say 2, it is equivalent to specifying -v two times. (python --v).
PYTHONDONTWRITEBYTECODE
Normally, the imported modules are compiled to .pyc file. If this variable is set to a not null string,the .pyc
files on the import of source modules are not created.
PYTHONWARNINGS
Python's warning messages are redirected to the standard error stream, sys.stderr. This environment
variable is equivalent to the python -W option. The following are allowed values of this variable −
# Warn once per call location PYTHONWARNINGS=default
PYTHONWARNINGS=error # Convert to exceptions
# Warn every time PYTHONWARNINGS=always
# Warn once per calling module PYTHONWARNINGS=module
PYTHONWARNINGS=once # Warn once per Python process
# Never warn PYTHONWARNINGS=ignore
Python - Virtual Environment
In this chapter, you will get to know what a virtual environment in Python is, how to create and use a
virtual environment for building a Python application.
When you install Python software on your computer, it is available for use from anywhere in the
filesystem. This is a system-wide installation.
While developing an application in Python, one or more libraries may be required to be installed using the
pip utility (e.g., pip3 install somelib). Moreover, an application (let us say App1) may require a particular
version of the library − say somelib 1.0. At the same time another Python application (for example App2)
may require newer version of same library say somelib 2.0. Hence by installing a new version, the
functionality of App1 may be compromised because of conflict between two different versions of same
library.
This conflict can be avoided by providing two isolated environments of Python in the samemachine. These
are called virtual environment. A virtual environment is a separatedirectory structure containing isolated
installation having a local copy of Python interpreter, standard library and other modules.
The following figure shows the purpose of advantage of using virtual environment. Using the global
Python installation, more than one virtual environments are created, each having different version of the
same library, so that conflict is avoided.
This functionality is supported by venv module in standard Python distribution. Use following commands
to create a new virtual environment.
C:\Users\Acer>md\pythonapp
C:\Users\Acer>cd\pythonapp
C:\pythonapp>python -m venv myvenv
Here, myvenv is the folder in which a new Python virtual environment will be created showing following
directory structure −
Directory of C:\pythonapp\myvenv
22-02-2023 09:53 <DIR> .
22-02-2023 09:53 <DIR> ..
22-02-2023 09:53 <DIR> Include
22-02-2023 09:53 <DIR> Lib
22-02-2023 09:53 77 pyvenv.cfg
22-02-2023 09:53 <DIR> Scripts
The utilities for activating and deactivating the virtual environment as well as the local copy of Python
interpreter will be placed in the scripts folder.
Directory of C:\pythonapp\myvenv\scripts
22-02-2023 09:53 <DIR> .
22-02-2023 09:53 <DIR> ..
22-02-2023 09:53 2,063 activate
22-02-2023 09:53 992 activate.bat
22-02-2023 09:53 19,611 Activate.ps1
22-02-2023 09:53 393 deactivate.bat
22-02-2023 09:53 106,349 pip.exe
22-02-2023 09:53 106,349 pip3.10.exe
22-02-2023 09:53 106,349 pip3.exe
22-02-2023 09:53 242,408 python.exe
22-02-2023 09:53 232,688 pythonw.exe
To enable this new virtual environment, execute activate.bat in Scripts folder.
C:\pythonapp>myvenv\scripts\activate
(myvenv) C:\pythonapp>
Note the name of the virtual environment in the parentheses. The Scripts folder contains a local copy of
Python interpreter. You can start a Python session in this virtual environment.
To confirm whether this Python session is in virtual environment check the sys.path.
(myvenv) C:\pythonapp>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Python310\\python310.zip', 'C:\\Python310\\DLLs',
'C:\\Python310\\lib', 'C:\\Python310', 'C:\\pythonapp\\myvenv',
'C:\\pythonapp\\myvenv\\lib\\site-packages']
>>>
The scripts folder of this virtual environment also contains pip utilities. If you install a package from PyPI,
that package will be active only in current virtual environment. To deactivate this environment, run
deactivate.bat.
Python - Basic Syntax
In Python, the term syntax refers to the rules of forming a statement or expression. Python language is
known for its clean and simple syntax. It also has a limited set of keywords and simpler punctuation rules
as compared to other languages. In this chapter, let us understand about basic syntax of Python.
A Python program comprises of predefined keywords and identifiers representing functions, classes,
modules etc. Python has clearly defined rules for forming identifiers, writing statements and comments in
Python source code.
Python Keywords
A predefined set of keywords is the most important aspect of any programming language. These
keywords are reserved words. They have a predefined meaning, they must be used only for its predefined
purpose and as per the predefined rules of syntax. Programming logic is encoded with these keywords.
As of Python 3.11 version, there are 35 (Thirty Five) keywords in Python. To obtain the list of Python
keywords, enter the following help command in Python shell.
>>> help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more
help.
1. False 10. class 19. from 28. or
2. None 11. continue 20. global 29. pass
3. True 12. def 21. if 30. raise
4. and 13. del 22. import 31. return
5. as 14. elif 23. in 32. try
6. assert 15. else 24. is 33. while
7. async 16. except 25. lambda 34. with
8. await 17. finally 26. nonlocal 35. yield
9. break 18. for 27. not
All the keywords are alphabetic and all (except False, None and True) are in lowercase. The list of
keywords is also given by kwlist property defined in keyword module
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']
How to verify if any word is a keyword or not? Most Python IDEs provide coloured syntax highlighting
feature, where keywords are represented in a specific colour.
Shown below is a Python code in VS Code. Keywords (if and else), identifiers and constants appear in
distinct colour scheme.
The keyword module also has a iskeyword() function. It returns True for a valid keyword, False otherwise.
>>> import keyword
>>> keyword.iskeyword("else")
True
>>> keyword.iskeyword("Hello")
False
Python keywords can be broadly classified in following categories −
Value Keywords True, False, None
Operator Keywords and, or, not, in, is
Conditional Flow Keywords if, elif, else
Keywords for loop control for, while, break, continue
Structure Keywords def, class, with, pass, lambda
Keywords for returning return, yield
Import Keywords import, from, as
Keywords about ExceptionHandling try, except, raise, finally, assert
Keywords for Asynchronous Programming async, await
Variable Scope Keywords del, global, nonlocal
We shall learn about the usage of each of these keywords as we go along in this tutorial.
Python Identifiers
Various elements in a Python program, other than keywords, are called identifiers. An identifier is a user-
given name to variables, functions, classes, modules, packages etc. in the source code. Python has laid
down certain rules to form an identifier. These rules are −
An identifier should start with either an alphabet (lower or upper case) or underscore (_). More
than one alpha-numeric characters or underscores may follow.
Use of any keyword as n identifier is not allowed, as keywords have a predefined meaning.
Conventionally, name of class begins with uppercase alphabet. Other elements like variable or
function start with lowercase alphabet.
As per another Python convention, single underscore in the beginning of a variable name is used
to indicate a private variable.
Use two underscores in beginning of identifier indicates that the variable is strongly private.
Two leading and trailing underscores are used in language itself for special purpose. For example,
__add__, __init__
According to the above rules, here are some valid identifiers −
Student
score
aTotal
sum_age
__count
TotalValue
price1
cost_of_item
__init__
Some invalid formations of identifiers are also given below −
1001
Name of student
price-1
ft.in
It may be noted that identifiers are case sensitive. As a result, Name and name are two different
identifiers.
Python Indents
Use of indents in code is one of the important features of Python's syntax. Often in a program, you might
require grouping more than one statements together as a block. For example, in case of more than one
statements if a condition is true/false. Different programming languages have different methods to mark
the scope and extent of group of statements in constructs like class, function, conditional and loop. C, C+
+, Java etc. make use of curly brackets to mark the block. Python uses uniform indentation to mark block
of statements, thereby it increases the readability of the code.
To mark the beginning of a block, type the ":" symbol and press Enter. Any Python-aware editor (like IDLE,
or VS Code) goes to the next line leaving additional whitespace (called indent). Subsequent statements in
the block follow same level of indent. To signal end of the block, the whitespace is dedented by pressing
the backspace key. The following example illustrates the use of indents in Python:
At this juncture, you may not understand how the code works. But don't worry. Just see how indent level
increases after colon symbol.
Python Statements
A statement in Python is any instruction that the Python interpreter can execute. A statement comprises
of one or more keywords, operators, identifiers, a : symbol to mark beginning of block, or backslash \ as
continuation character.
The statement may be a simple assignment statement such as amount = 1000 or it may be a compound
statement with multiple statements grouped together in uniformly indented block, as in conditional or
looping constructs.
You can enter a statement in front of the Python prompt of the interactive shell, or in the editor window.
Usually, text terminated by Enter key (called newline character) is recognized as a statement by Python
interpreter. Hence, each line in the editor is a statement, unless it starts with the comment character (#).
print ("My first program")
price = 100
qty = 5
ttl = price*qty
print ("Total = ", ttl)
Each line in the above code is a statement. Occasionally, a Python statement may spill over multiple lines.
To do so, use backslash (\) as continuation character. A long string can be conveniently broken in multiple
lines as shown below −
name = "Ravi"
string = "Hello {} \
Welcome to Python Tutorial \
from TutorialsPoint".format(name)
print (string)
The string (with an embedded string variable name) spreads over multiple lines for better readability. The
output will be −
Hello Ravi Welcome to Python Tutorial from TutorialsPoint
The continuation character also helps in writing a long arithmetic expression in a more readable manner.
For example, the equation (a+b)×(c−d)(a−b)×(c+d)
is coded in Python as follows −
a=10
b=5
c=5
d=10
expr = (a+b)*(c-d)/ \
(a-b)*(c+d)
print (expr)
The use of back-slash symbol (\) is not necessary if items in a list, tuple or dictionary object spill over
multiple lines.
Subjects = ["English", "French", "Sanskrit",
"Physics", "Maths",
"Computer Sci", "History"]
Python also allows use of semicolon to put more than one statements in a single line in the editor. Look at
the following examples −
a=10; b=5; c=5; d=10
if a>10: b=20; c=50
Python - Variables
In this chapter, you will learn what are variables in Python and how to use them.
Data items belonging to different data types are stored in computer's memory. Computer's memory
locations are having a number or address, internally represented in binary form. Data is also stored in
binary form as the computer works on the principle of binary representation. In the following diagram, a
string May and a number 18 is shown as stored in memory locations.
If you know the assembly language, you will covert these data items and the memory address, and give a
machine language instruction. However, it is not easy for everybody. Language translator such as Python
interpreter performs this type of conversion. It stores the object in a randomly chosen memory location.
Python's built-in id() function returns the address where the object is stored.
>>> "May"
>>> id("May")
2167264641264
>>> 18
18
>>> id(18)
140714055169352
Once the data is stored in the memory, it should be accessed repeatedly for performing a certain process.
Obviously, fetching the data from its ID is cumbersome. High level languages like Python make it possible
to give a suitable alias or a label to refer to the memory location.
In the above example, let us label the location of May as month, and location in which 18 is stored as age.
Python uses the assignment operator (=) to bind an object with the label.
>>> month="May"
>>> age=18
The data object (May) and its name (month) have the same id(). The id() of 18 and age are also same.
>>> id(month)
2167264641264
>>> id(age)
140714055169352
The label is an identifier. It is usually called as a variable. A Python variable is a symbolic name that is a
reference or pointer to an object.
Naming Convention
Name of the variable is user specified, and is formed by following the rules of forming an identifier.
Name of Python variable should start with either an alphabet (lower or upper case) or underscore
(_). More than one alpha-numeric characters or underscores may follow.
Use of any keyword as Python variable is not allowed, as keywords have a predefined meaning.
Name of a variable in Python is case sensitive. As a result, age and Age cannot be used
interchangeably.
You should choose the name of variable that is mnemonic, such that it indicates the purpose. It
should not be very short, but not vary lengthy either.
If the name of variable contains multiple words, we should use these naming patterns −
Camel case − First letter is a lowercase, but first letter of each subsequent word is in uppercase.
For example: kmPerHour, pricePerLitre
Pascal case − First letter of each word is in uppercase. For example: KmPerHour, PricePerLitre
Snake case − Use single underscore (_) character to separate words. For example: km_per_hour,
price_per_litre
Once you use a variable to identify a data object, it can be used repeatedly without its id() value. Here, we
have a variables height and width of a rectangle. We can compute the area and perimeter with these
variables.
>>> width=10
>>> height=20
>>> area=width*height
>>> area
200
>>> perimeter=2*(width+height)
>>> perimeter
60
Use of variables is especially advantageous when writing scripts or programs. Following script also uses
the above variables.
#! /usr/bin/python3.11
width = 10
height = 20
area = width*height
perimeter = 2*(width+height)
print ("Area = ", area)
print ("Perimeter = ", perimeter)
Save the above script with .py extension and execute from command-line. The result would be −
Area = 200
Perimeter = 60
Assignment Statement
In languages such as C/C++ and Java, one needs to declare the variable and its type before assigning it any
value. Such prior declaration of variable is not required in Python.
Python uses = symbol as the assignment operator. Name of the variable identifier appears on the left of =
symbol. The expression on its right id evaluated and the value is assigned to the variable. Following are
the examples of assignment statements
>>> counter = 10
>>> counter = 10 # integer assignment
>>> price = 25.50 # float assignment
>>> city = "Hyderabad" # String assignment
>>> subjects = ["Physics", "Maths", "English"] # List assignment
>>> mark_list = {"Rohit":50, "Kiran":60, "Lata":70} # dictionary
assignment
Python's built-in print() function displays the value of one or more variables.
>>> print (counter, price, city)
10 25.5 Hyderabad
>>> print (subjects)
['Physics', 'Maths', 'English']
>>> print (mark_list)
{'Rohit': 50, 'Kiran': 60, 'Lata': 70}
Value of any expression on the right of = symbol is assigned to the variable on left.
>>> x = 5
>>> y = 10
>>> z = x+y
However, the expression on the left and variable on the right of = operator is not allowed.
>>> x = 5
>>> y = 10
>>> x+y=z
File "<stdin>", line 1
x+y=z
^^^
SyntaxError: cannot assign to expression here. Maybe you meant '=='
instead of '='?
Though z=x+y and x+y=z are equivalent in Mathematics, it is not so here. It's because = is an equation
symbol, while in Python it is an assignment operator.
Multiple Assignments
In Python, you can initialize more than one variables in a single statement. In the following case, three
variables have same value.
>>> a=10
>>> b=10
>>> c=10
Instead of separate assignments, you can do it in a single assignment statement as follows −
>>> a=b=c=10
>>> print (a,b,c)
10 10 10
In the following case, we have three variables with different values.
>>> a=10
>>> b=20
>>> c=30
These separate assignment statements can be combined in one. You need to give comma separated
variable names on left, and comma separated values on the right of = operator.
>>> a,b,c = 10,20,30
>>> print (a,b,c)
10 20 30
The concept of variable works differently in Python than in C/C++.
In C/C++, a variable is a named memory location. If a=10 and also b=10, both are two different memory
locations. Let us assume their memory address is 100 and 200 respectively.
If a different value is assigned to "a" − say 50, 10 in the address 100 is overwritten.
A Python variable refers to the object and not the memory location. An object is stored in memory only
once. Multiple variables are really the multiple labels to the same object.
The statement a=50 creates a new int object 50 in the memory at some other location, leaving the object
10 referred by "b".
Further, if you assign some other value to b, the object 10 remains unreferred.
Python's garbage collector mechanism releases the memory occupied by any unreferred object.
Python's identity operator is returns True if both the operands have same id() value.
>>> a=b=10
>>> a is b
True
>>> id(a), id(b)
(140731955278920, 140731955278920)
Python - Data Types
Computer is a data processing device. Computer stores the data in its memory and processes it as per the
given program. Data is a representation of facts about a certain object.
Some examples of data −
Data of students − name, gender, class, marks, age, fee etc.
Data of books in library − title, author, publisher, price, pages, year of publication etc.
Data of employees in an office − name, designation, salary, department, branch, etc.
Data type represents a kind of value and determines what operations can be done on it. Numeric, non-
numeric and Boolean (true/false) data are the most obvious data types. However, each programming
language has its own classification largely reflecting its programming philosophy.
Python identifies the data by different data types as per the following diagram −
Python's data model defines four main data types. They are Number, Sequence, Set and Dictionary (also
called Mapping)
Number Type
Any data item having a numeric value is a number. There are Four standard number data types in Python.
They are integer, floating point, Boolean and Complex. Each of them have built-in classes in Python
library, called int, float, bool and complex respectively.
In Python, a number is an object of its corresponding class. For example, an integer number 123 is an
object of int class. Similarly, 9.99 is a floating point number, which is an object of float class.
Python's standard library has a built-in function type(), which returns the class of the given object. Here, it
is used to check the type of an integer and floating point number.
>>> type(123)
<class 'int'>
>>> type(9.99)
<class 'float'>
The fractional component of a float number can also be represented in scientific format. A number -
0.000123 is equivalent to its scientific notation 1.23E-4 (or 1.23e-4).
A complex number is made up of two parts − real and imaginary. They are separated by '+' or '-' signs.
The imaginary part is suffixed by 'j' which is the imaginary number. The square root of -1 (−1−−−√
), is defined as imaginary number. Complex number in Python is represented as x+yj, where x is the real
part, and y is the imaginary part. So, 5+6j is a complex number.
>>> type(5+6j)
<class 'complex'>
A Boolean number has only two possible values, as represented by the keywords, True and False. They
correspond to integer 1 and 0 respectively.
>>> type (True)
<class 'bool'>
>>> type(False)
<class 'bool'>
With Python's arithmetic operators you can perform operations such as addition, subtraction etc.
Sequence Types
Sequence is a collection data type. It is an ordered collection of items. Items in the sequence have a
positional index starting with 0. It is conceptually similar to an array in C or C++. There are three sequence
types defined in Python. String, List and Tuple.
Strings in Python
A string is a sequence of one or more Unicode characters, enclosed in single, double or triple quotation
marks (also called inverted commas). As long as the same sequence of characters is enclosed, single or
double or triple quotes don't matter. Hence, following string representations are equivalent.
>>> 'Welcome To TutorialsPoint'
'Welcome To TutorialsPoint'
>>> "Welcome To TutorialsPoint"
'Welcome To TutorialsPoint'
>>> '''Welcome To TutorialsPoint'''
'Welcome To TutorialsPoint'
A string in Python is an object of str class. It can be verified with type() function.
>>> type("Welcome To TutorialsPoint")
<class 'str'>
You want to embed some text in double quotes as a part of string, the string itself should be put in single
quotes. To embed a single quoted text, string should be written in double quotes.
>>> 'Welcome to "Python Tutorial" from TutorialsPoint'
'Welcome to "Python Tutorial" from TutorialsPoint'
>>> "Welcome to 'Python Tutorial' from TutorialsPoint"
"Welcome to 'Python Tutorial' from TutorialsPoint"
Since a string is a sequence, each character in it is having a positional index starting from 0. To form a
string with triple quotes, you may use triple single quotes, or triple double quotes − both versions are
similar.
>>> '''Welcome To TutorialsPoint'''
'Welcome To TutorialsPoint'
>>> """Welcome To TutorialsPoint"""
'Welcome To TutorialsPoint'
Triple quoted string is useful to form a multi-line string.
>>> '''
... Welcome To
... Python Tutorial
... from TutorialsPoint
... '''
'\nWelcome To\nPython Tutorial \nfrom TutorialsPoint\n'
A string is a non-numeric data type. Obviously, we cannot perform arithmetic operations on it. However,
operations such as slicing and concatenation can be done. Python's str class defines a number of useful
methods for string processing. We shall learn these methods in the subsequent chapter on Strings.
List in Python
In Python, List is an ordered collection of any type of data items. Data items are separated by comma (,)
symbol and enclosed in square brackets ([]). A list is also a sequence, hence.
each item in the list has an index referring to its position in the collection. The index starts from 0.
The list in Python appears to be similar to array in C or C++. However, there is an important difference
between the two. In C/C++, array is a homogenous collection of data of similar types. Items in the Python
list may be of different types.
>>> [2023, "Python", 3.11, 5+6j, 1.23E-4]
A list in Python is an object of list class. We can check it with type() function.
>>> type([2023, "Python", 3.11, 5+6j, 1.23E-4])
<class 'list'>
As mentioned, an item in the list may be of any data type. It means that a list object can also be an item in
another list. In that case, it becomes a nested list.
>>> [['One', 'Two', 'Three'], [1,2,3], [1.0, 2.0, 3.0]]
A list item may be a tuple, dictionary, set or object of user defined class also.
List being a sequence, it supports slicing and concatenation operations as in case of string. With the
methods/functions available in Python's built-in list class, we can add, delete or update items, and sort or
rearrange the items in the desired order. We shall study these aspects in a subsequent chapter.
Tuples in Python
In Python, a Tuple is an ordered collection of any type of data items. Data items are separated by comma
(,) symbol and enclosed in parentheses or round brackets (). A tuple is also a sequence, hence each item
in the tuple has an index referring to its position in the collection. The index starts from 0.
>>> (2023, "Python", 3.11, 5+6j, 1.23E-4)
In Python, a tuple is an object of tuple class. We can check it with the type() function.
>>> type((2023, "Python", 3.11, 5+6j, 1.23E-4))
<class 'tuple'>
As in case of a list, an item in the tuple may also be a list, a tuple itself or an object of any other Python
class.
>>> (['One', 'Two', 'Three'], 1,2.0,3, (1.0, 2.0, 3.0))
To form a tuple, use of parentheses is optional. Data items separated by comma without any enclosing
symbols are treated as a tuple by default.
>>> 2023, "Python", 3.11, 5+6j, 1.23E-4
(2023, 'Python', 3.11, (5+6j), 0.000123)
The two sequence types list and tuple appear to be similar except the use of delimiters, list uses square
brackets ([]) while tuple uses parentheses. However, there is one major
difference between list and tuple. List is mutable object, whereas tuple is immutable. An object is
immutable means once it is stored in the memory, it cannot be changed.
Let us try to understand the mutability concept. We have a list and tuple object with same data items.
>>> l1=[1,2,3]
>>> t1=(1,2,3)
Both are sequences, hence each item in both has an index. Item at index number 1 in both is 2.
>>> l1[1]
2
>>> t1[1]
2
Let us try to change the value of item index number 1 from 2 to 20 in list as well as tuple.
>>> l1[1]
2
>>> t1[1]
2
>>> l1[1]=20
>>> l1
[1, 20, 3]
>>> t1[1]=20
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
The error message 'tuple' object does not support item assignment tells you that a tuple object cannot
be modified once it is formed. This is called an immutable object.
Immutability of tuple also means that Python's tuple class doesn't have the functionality to add, delete or
sort items in a tuple. However, since it is a sequence, we can perform slicing and concatenation.
Dictionary Type
Python's dictionary is example of mapping type. A mapping object 'maps' value of one object with
another. In a language dictionary we have pairs of word and corresponding meaning. Two parts of pair
are key (word) and value (meaning). Similarly, Python dictionary is also a collection of key:value pairs. The
pairs are separated by comma and put inside curly brackets {}. To establish mapping between key and
value, the semicolon':' symbol is put between the two.
>>> {1:'one', 2:'two', 3:'three'}
Each key in a dictionary must be unique, and should be a number, string or tuple. The value object may be
of any type, and may be mapped with more than one keys (they need not be unique)
In Python, dictionary is an object of the built-in dict class. We can check it with the type() function.
>>> type({1:'one', 2:'two', 3:'three'})
<class 'dict'>
Python's dictionary is not a sequence. It is a collection of items but each item (key:value pair) is not
identified by positional index as in string, list or tuple. Hence, slicing operation cannot be done on a
dictionary. Dictionary is a mutable object, so it is possible to perform add, modify or delete actions with
corresponding functionality defined in dict class. These operations will be explained in a subsequent
chapter.
Set Type
Set is a Python implementation of set as defined in Mathematics. A set in Python is a collection, but is not
an indexed or ordered collection as string, list or tuple. An object cannot appear more than once in a set,
whereas in List and Tuple, same object can appear more than once.
Comma separated items in a set are put inside curly brackets or braces. Items in the set collection may be
of different data types.
>>> {2023, "Python", 3.11, 5+6j, 1.23E-4}
{(5+6j), 3.11, 0.000123, 'Python', 2023}
Note that items in the set collection may not follow the same order in which they are entered. The
position of items is optimized by Python to perform operations over set as defined in mathematics.
Python's Set is an object of built-in set class, as can be checked with the type() function.
>>> type({2023, "Python", 3.11, 5+6j, 1.23E-4})
<class 'set'>
A set can store only immutable objects such as number (int, float, complex or bool), string or tuple. If you
try to put a list or a dictionary in the set collection, Python raises a TypeError.
>>> {['One', 'Two', 'Three'], 1,2,3, (1.0, 2.0, 3.0)}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Hashing is a mechanism in computer science which enables quicker searching of objects in computer's
memory. Only immutable objects are hashable.
Even if a set doesn't allow mutable items, the set itself is mutable. Hence, add/delete/update operations
are permitted on a set object, using the methods in built-in set class. Python also has a set of operators to
perform set manipulation. The methods and operators are explained in a latter chapter
Python - Type Casting
In manufacturing, casting is the process of pouring a liquefied or molten metal into a mold, and letting it
cool to obtain the desired shape. In programming, casting refers to converting an object of one type into
another. Here, we shall learn about type casting in Python.
In Python there are different data types, such as numbers, sequences, mappings etc. There may be a
situation where, you have the available data of one type but you want to use it in another form. For
example, the user has input a string but you want to use it as a number. Python's type casting mechanism
let you do that.
Implicit Casting in Python
Casting is of two types − implicit and explicit.
When any language compiler/interpreter automatically converts object of one type into other, it is called
implicit casting. Python is a strongly typed language. It doesn't allow automatic type conversion between
unrelated data types. For example, a string cannot be converted to any number type. However, an integer
can be cast into a float. Other languages such as JavaScript is a weakly typed language, where an integer
is coerced into a string for concatenation.
Note that memory requirement of each type is different. For example, an integer object in Python
occupies 4 bytes of memory, while a float object needs 8 bytes because of its fractional part. Hence,
Python interpreter doesn't automatically convert a float to int, because it will result in loss of data. On the
other hand, int can be easily converted into float by setting its fractional part to 0.
Implicit int to float casting takes place when any arithmetic operation one int and float operands is done.
We have an integer and one float variable
>>> a=10 # int object
>>> b=10.5 # float object
To perform their addition, 10 − the integer object is upgraded to 10.0. It is a float, but equivalent to its
earlier numeric value. Now we can perform addition of two floats.
>>> c=a+b
>>> print (c)
20.5
In implicit type casting, the object with lesser byte size is upgraded to match the byte size of other object
in the operation. For example, a Boolean object is first upgraded to int and then to float, before the
addition with a floating point object. In the following example, we try to add a Boolean object in a float.
>>> a=True
>>> b=10.5
>>> c=a+b
>>> print (c)
11.5
Note that True is equal to 1, and False is equal to 0.
Although automatic or implicit casting is limited to int to float conversion, you can use Python's built-in
functions to perform the explicit conversions such as string to integer.
int() Function
Python's built-in int() function converts an integer literal to an integer object, a float to integer, and a
string to integer if the string itself has a valid integer literal representation.
Using int() with an int object as argument is equivalent to declaring an int object directly.
>>> a = int(10)
>>> a
10
is same as −
>>> a = 10
>>> a
10
>>> type(a)
<class 'int>
If the argument to int() function is a float object or floating point expression, it returns an int object. For
example −
>>> a = int(10.5) #converts a float object to int
>>> a
10
>>> a = int(2*3.14) #expression results float, is converted to int
>>> a
6
>>> type(a)
<class 'int'>
The int() function also returns integer 1 if a Boolean object is given as argument.
>>> a=int(True)
>>> a
1
>>> type(a)
<class 'int'>
String to Integer
The int() function returns an integer from a string object, only if it contains a valid integer representation.
>>> a = int("100")
>>> a
100
>>> type(a)
<class 'int'>
>>> a = ("10"+"01")
>>> a = int("10"+"01")
>>> a
1001
>>> type(a)
<class 'int'>
However, if the string contains a non-integer representation, Python raises ValueError.
>>> a = int("10.5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '10.5'
>>> a = int("Hello World")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello World'
The int() function also returns integer from binary, octal and hexa-decimal string. For this, the function
needs a base parameter which must be 2, 8 or 16 respectively. The string should have a valid
binary/octal/Hexa-decimal representation.
Binary String to Integer
The string should be made up of 1 and 0 only, and the base should be 2.
>>> a = int("110011", 2)
>>> a
51
The Decimal equivalent of binary number 110011 is 51.
Octal String to Integer
The string should only contain 0 to 7 digits, and the base should be 8.
>>> a = int("20", 8)
>>> a
16
The Decimal equivalent of octal 20 is 16.
Hexa-Decimal String to Integer
The string should contain only the Hexadecimal symbols i.e., 0-9 and A, B, C, D, E or F. Base should be 16.
>>> a = int("2A9", 16)
>>> a
681
Decimal equivalent of Hexadecimal 2A9 is 681.
You can easily verify these conversions with calculator app in Windows, Ubuntu or Smartphones.
float() Function
float() is a built-in function in Python. It returns a float object if the argument is a float literal, integer or a
string with valid floating point representation.
Using float() with an float object as argument is equivalent to declaring a float object directly
>>> a = float(9.99)
>>> a
9.99
>>> type(a)
<class 'float'>
is same as −
>>> a = 9.99
>>> a
9.99
>>> type(a)
<class 'float'>
If the argument to float() function is an integer, the returned value is a floating point with fractional part
set to 0.
>>> a = float(100)
>>> a
100.0
>>> type(a)
<class 'float'>
The float() function returns float object from a string, if the string contains a valid floating point number,
otherwise ValueError is raised.
>>> a = float("9.99")
>>> a
9.99
>>> type(a)
<class 'float'>
>>> a = float("1,234.50")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1,234.50'
The reason of ValueError here is the presence of comma in the string.
For the purpose of string to float conversion, the sceientific notation of floating point is also considered
valid.
>>> a = float("1.00E4")
>>> a
10000.0
>>> type(a)
<class 'float'>
>>> a = float("1.00E-4")
>>> a
0.0001
>>> type(a)
<class 'float'>
str() Function
We saw how a Python obtains integer or float number from corresponding string representation. The str()
function works the opposite. It surrounds an integer or a float object with quotes (') to return a str object.
The str() function returns the string.
representation of any Python object. In this section, we shall see different examples of str() function in
Python.
The str() function has three parameters. First required parameter (or argument) is the object whose string
representation we want. Other two operators, encoding and errors, are optional.
We shall execute str() function in Python console to easily verify that the returned object is a string, with
the enclosing quotation marks (').
Integer to string
>>> a = str(10)
>>> a
'10'
>>> type(a)
<class 'str'>
Float to String
str() function converts floating point objects with both the notations of floating point, standard notation
with a decimal point separating integer and fractional part, and the scientific notation to string object.
>>> a=str(11.10)
>>> a
'11.1'
>>> type(a)
<class 'str'>
>>> a = str(2/5)
>>> a
'0.4'
>>> type(a)
<class 'str'>
In the second case, a division expression is given as argument to str() function. Note that the expression is
evaluated first and then result is converted to string.
Floating points in scientific notations using E or e and with positive or negative power are converted to
string with str() function.
>>> a=str(10E4)
>>> a
'100000.0'
>>> type(a)
<class 'str'>
>>> a=str(1.23e-4)
>>> a
'0.000123'
>>> type(a)
<class 'str'>
When Boolean constant is entered as argument, it is surrounded by (') so that True becomes 'True'. List
and Tuple objects can also be given argument to str() function. The resultant string is the list/tuple
surrounded by (').
>>> a=str('True')
>>> a
'True'
>>> a=str([1,2,3])
>>> a
'[1, 2, 3]'
>>> a=str((1,2,3))
>>> a
'(1, 2, 3)'
>>> a=str({1:100, 2:200, 3:300})
>>> a
'{1: 100, 2: 200, 3: 300}'
Conversion of Sequence Types
List, Tuple and String are Python's sequence types. They are ordered or indexed collection of items.
A string and tuple can be converted into a list object by using the list() function. Similarly, the tuple()
function converts a string or list to a tuple.
We shall an object each of these three sequence types and study their inter-conversion.
>>> a=[1,2,3,4,5]
>>> b=(1,2,3,4,5)
>>> c="Hello"
### list() separates each character in the string and builds the list
>>> obj=list(c)
>>> obj
['H', 'e', 'l', 'l', 'o']
### The parentheses of tuple are replaced by square brackets
>>> obj=list(b)
>>> obj
[1, 2, 3, 4, 5]
### tuple() separates each character from string and builds a tuple of
characters
>>> obj=tuple(c)
>>> obj
('H', 'e', 'l', 'l', 'o')
### square brackets of list are replaced by parentheses.
>>> obj=tuple(a)
>>> obj
(1, 2, 3, 4, 5)
### str() function puts the list and tuple inside the quote symbols.
>>> obj=str(a)
>>> obj
'[1, 2, 3, 4, 5]'
>>> obj=str(b)
>>> obj
'(1, 2, 3, 4, 5)'
Thus Python's explicit type casting feature allows conversion of one data type to other with the help of its
built-in functions.
Python - Unicode System
Software applications often require to display messages output in a variety in different languages such as
in English, French, Japanese, Hebrew, or Hindi. Python's string type uses the Unicode Standard for
representing characters. It makes the program possible to work with all these different possible
characters.
A character is the smallest possible component of a text. 'A', 'B', 'C', etc., are all different characters. So
are 'È' and 'Í'.
According to The Unicode standard, characters are represented by code points. A code point value is an
integer in the range 0 to 0x10FFFF.
A sequence of code points is represented in memory as a set of code units, mapped to 8-bit bytes. The
rules for translating a Unicode string into a sequence of bytes are called a character encoding.
Three types of encodings are present, UTF-8, UTF-16 and UTF-32. UTF stands for Unicode Transformation
Format.
Python 3.0 onwards has built-in support for Unicode. The str type contains Unicode characters, hence any
string created using single, double or the triple-quoted string syntax is stored as Unicode. The default
encoding for Python source code is UTF-8.
Hence, string may contain literal representation of a Unicode character (3/4) or its Unicode value (\
u00BE).
var = "3/4"
print (var)
var = "\u00BE"
print (var)
This above code will produce the following output −
'3/4'
3/4
In the following example, a string '10' is stored using the Unicode values of 1 and 0 which are \u0031 and
u0030 respectively.
var = "\u0031\u0030"
print (var)
It will produce the following output −
10
Strings display the text in a human-readable format, and bytes store the characters as binary data.
Encoding converts data from a character string to a series of bytes. Decoding translates the bytes back to
human-readable characters and symbols. It is important not
to confuse these two methods. encode is a string method, while decode is a method of the Python byte
object.
In the following example, we have a string variable that consists of ASCII characters. ASCII is a subset of
Unicode character set. The encode() method is used to convert it into a bytes object.
string = "Hello"
tobytes = string.encode('utf-8')
print (tobytes)
string = tobytes.decode('utf-8')
print (string)
The decode() method converts byte object back to the str object. The encodeing method used is utf-8.
b'Hello'
Hello
In the following example, the Rupee symbol (₹) is stored in the variable using its Unicode value. We
convert the string to bytes and back to str.
string = "\u20B9"
print (string)
tobytes = string.encode('utf-8')
print (tobytes)
string = tobytes.decode('utf-8')
print (string)
When you execute the above code, it will produce the following output −
₹
b'\xe2\x82\xb9'
₹
Python - Literals
In computer science, a literal is a notation for representing a fixed value in source code. For example, in
the assignment statement.
x = 10
Here 10 is a literal as numeric value representing 10 is directly stored in memory. However,
y = x*2
Here, even if the expression evaluates to 20, it is not literally included in source code. You can also
declare an int object with built-in int() function −
x = int(10)
However, this is also an indirect way of instantiation and not with literal.
You can create use literal representation for creating object of any built-in data type.
Integer Literal
Any representation involving only the digit symbols (0 to 9) creates an object of int type. The object so
declared may be referred by a variable using an assignment operator.
Take a look at the following example −
x = 10
y = -25
z=0
Python allows an integer to be represented as an octal number or a hexadecimal number. A numeric
representation with only eight digit symbols (0 to 7) but prefixed by 0o or 0O is an octal number.
x = 0O34
Similarly, a series of hexadecimal symbols (0 to 9 and a to f), prefixed by 0x or 0X represents an integer in
Hexedecimal form.
x = 0X1C
However, it may be noted that, even if you use octal or hexadecimal literal notation, Python internally
treats it as of int type.
# Using Octal notation
x = 0O34
print ("0O34 in octal is", x, type(x))
# Using Hexadecimal notation
x = 0X1c
print ("0X1c in Hexadecimal is", x, type(x))
When you run this code, it will produce the following output −
0O34 in octal is 28 <class 'int'>
0X1c in Hexadecimal is 28 <class 'int'>
Float Literal
A floating point number consists of an integral part and a fractional part. Conventionally, a decimal point
symbol (.) separates these two parts in a literal representation of a float. For example,
x = 25.55
y = 0.05
z = -12.2345
For a floating point number which is too large or too small, where number of digits before or after
decimal point is more, a scientific notation is used for a compact literal representation. The symbol E or e
followed by positive or negative integer, follows after the integer part.
For example, a number 1.23E05 is equivalent to 123000.00. Similarly, 1.23e-2 is equivalent to 0.0123
# Using normal floating point notation
x = 1.23
print ("1.23 in normal float literal is", x, type(x))
# Using Scientific notation
x = 1.23E5
print ("1.23E5 in scientific notation is", x, type(x))
x = 1.23E-2
print ("1.23E-2 in scientific notation is", x, type(x))
Here, you will get the following output −
1.23 in normal float literal is 1.23 <class 'float'>
1.23E5 in scientific notation is 123000.0 <class 'float''>
1.23E-2 in scientific notation is 0.0123 <class 'float''>
Complex Literal
A complex number comprises of a real and imaginary component. The imaginary component is any
number (integer or floating point) multiplied by square root of "-1"
(−1−−−√
). In literal representation (−1−−−√
) is representation by "j" or "J". Hence, a literal representation of a complex number takes a form x+yj.
#Using literal notation of complex number
x = 2+3j
print ("2+3j complex literal is", x, type(x))
y = 2.5+4.6j
print ("2.5+4.6j complex literal is", x, type(x))
This code will produce the following output −
2+3j complex literal is (2+3j) <class 'complex'>
2.5+4.6j complex literal is (2+3j) <class 'complex'>
String Literal
A string object is one of the sequence data types in Python. It is an immutable sequence of Unicode code
points. Code point is a number corresponding to a character according to Unicode standard. Strings are
objects of Python's built-in class 'str'.
String literals are written by enclosing a sequence of characters in single quotes ('hello'), double quotes
("hello") or triple quotes ('''hello''' or """hello""").
var1='hello'
print ("'hello' in single quotes is:", var1, type(var1))
var2="hello"
print ('"hello" in double quotes is:', var1, type(var1))
var3='''hello'''
print ("''''hello'''' in triple quotes is:", var1, type(var1))
var4="""hello"""
print ('"""hello""" in triple quotes is:', var1, type(var1))
Here, you will get the following output −
'hello' in single quotes is: hello <class 'str'>
"hello" in double quotes is: hello <class 'str'>
''''hello'''' in triple quotes is: hello <class 'str'>
"""hello""" in triple quotes is: hello <class 'str'>
If it is required to embed double quotes as a part of string, the string itself should be put in single quotes.
On the other hand, if single quoted text is to be embedded, string should be written in double quotes.
var1='Welcome to "Python Tutorial" from TutorialsPoint'
print (var1)
var2="Welcome to 'Python Tutorial' from TutorialsPoint"
print (var2)
It will produce the following output −
Welcome to "Python Tutorial" from TutorialsPoint
Welcome to 'Python Tutorial' from TutorialsPoint
List Literal
List object in Python is a collection of objects of other data type. List is an ordered collection of items not
necessarily of same type. Individual object in the collection is accessed by index starting with zero.
Literal representation of a list object is done with one or more items which are separated by comma and
enclosed in square brackets [].
L1=[1,"Ravi",75.50, True]
print (L1, type(L1))
It will produce the following output −
[1, 'Ravi', 75.5, True] <class 'list'>
Tuple Literal
Tuple object in Python is a collection of objects of other data type. Tuple is an ordered collection of items
not necessarily of same type. Individual object in the collection is accessed by index starting with zero.
Literal representation of a tuple object is done with one or more items which are separated by comma
and enclosed in parentheses ().
T1=(1,"Ravi",75.50, True)
print (T1, type(T1))
It will produce the following output −
[1, 'Ravi', 75.5, True] <class tuple>
Default delimiter for Python sequence is parentheses, which means a comma separated sequence
without parentheses also amounts to declaration of a tuple.
T1=1,"Ravi",75.50, True
print (T1, type(T1))
Here too, you will get the same output −
[1, 'Ravi', 75.5, True] <class tuple>
Dictionary Literal
Like list or tuple, dictionary is also a collection data type. However, it is not a sequence. It is an unordered
collection of items, each of which is a key-value pair. Value is bound to key by the ":" symbol. One or
more key:value pairs separated by comma are put inside curly brackets to form a dictionary object.
capitals={"USA":"New York", "France":"Paris", "Japan":"Tokyo",
"India":"New Delhi"}
numbers={1:"one", 2:"Two", 3:"three",4:"four"}
points={"p1":(10,10), "p2":(20,20)}
Key should be an immutable object. Number, string or tuple can be used as key. Key cannot appear more
than once in one collection. If a key appears more than once, only the last one will be retained. Values
can be of any data type. One value can be assigned to more than one keys. For example,
staff={"Krishna":"Officer", "Rajesh":"Manager", "Ragini":"officer", "Anil":"Clerk", "Kavita":"Manager"}
Python - Operators
In Python as well as any programming language, Operators are symbols (sometimes keywords) that are
predefined to perform a certain most commonly required operations on one or more operands.
Types of Operators
Python language supports the following types of operators −
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Let us have a look at all the operators one by one.
Python - Arithmetic Operators
In Python, numbers are the most frequently used data type. Python uses the same symbols for basic
arithmetic operations Everybody is familiar with, i.e., "+" for addition, "-" for subtraction, "*" for
multiplication (most programming languages use "*" instead of the "x" as used in maths/algebra), and "/"
for division (again for the "÷" used in Mathematics).
In addition, Python defines few more arithmetic operators. They are "%" (Modulus), "**" (Exponent) and
"//" (Floor division).
Arithmetic operators are binary operators in the sense they operate on two operands. Python fully
supports mixed arithmetic. That is, the two operands can be of two different number types. In such a
situation, Python widens the narrower of the operands. An integer object is narrower than float object,
and float is narrower than complex object. Hence, the result of arithmetic operation of int and a float is a
float. Result of float and a complex is a complex number, similarly, operation on an integer and a complex
object results in a complex object.
Let us study these operators with examples.
Python − Addition Operator (+)
This operator pronounced as plus, is a basic arithmetic operator. It adds the two numeric operands on the
either side and returns the addition result.
In the following example, the two integer variables are the operands for the "+" operator.
a=10
b=20
print ("Addition of two integers")
print ("a =",a,"b =",b,"addition =",a+b)
It will produce the following output −
Addition of two integers
a = 10 b = 20 addition = 30
Addition of integer and float results in a float.
a=10
b=20.5
print ("Addition of integer and float")
print ("a =",a,"b =",b,"addition =",a+b)
It will produce the following output −
Addition of integer and float
a = 10 b = 20.5 addition = 30.5
The result of adding float to complex is a complex number.
a=10+5j
b=20.5
print ("Addition of complex and float")
print ("a=",a,"b=",b,"addition=",a+b)
It will produce the following output −
Addition of complex and float
a= (10+5j) b= 20.5 addition= (30.5+5j)
Python − Subtraction Operator (-)
This operator, known as minus, subtracts the second operand from the first. The resultant number is
negative if the second operand is larger.
First example shows subtraction of two integers.
a=10
b=20
print ("Subtraction of two integers:")
print ("a =",a,"b =",b,"a-b =",a-b)
print ("a =",a,"b =",b,"b-a =",b-a)
Result −
Subtraction of two integers
a = 10 b = 20 a-b = -10
a = 10 b = 20 b-a = 10
Subtraction of an integer and a float follows the same principle.
a=10
b=20.5
print ("subtraction of integer and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)
It will produce the following output −
subtraction of integer and float
a= 10 b= 20.5 a-b= -10.5
a= 10 b= 20.5 b-a= 10.5
In the subtraction involving a complex and a float, real component is involved in the operation.
a=10+5j
b=20.5
print ("subtraction of complex and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)
It will produce the following output −
subtraction of complex and float
a= (10+5j) b= 20.5 a-b= (-10.5+5j)
a= (10+5j) b= 20.5 b-a= (10.5-5j)
Python − Multiplication Operator (*)
The * (asterisk) symbol is defined as a multiplication operator in Python (as in many languages). It returns
the product of the two operands on its either side. If any of the operands negative, the result is also
negative. If both are negative, the result is positive. Changing the order of operands doesn't change the
result
a=10
b=20
print ("Multiplication of two integers")
print ("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of two integers
a = 10 b = 20 a*b = 200
In multiplication, a float operand may have a standard decimal point notation, or a scientific notation.
a=10
b=20.5
print ("Multiplication of integer and float")
print ("a=",a,"b=",b,"a*b=",a*b)
a=-5.55
b=6.75E-3
print ("Multiplication of float and float")
print ("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of integer and float
a = 10 b = 20.5 a-b = -10.5
Multiplication of float and float
a = -5.55 b = 0.00675 a*b = -0.037462499999999996
For the multiplication operation involving one complex operand, the other operand multiplies both the
real part and imaginary part.
a=10+5j
b=20.5
print ("Multiplication of complex and float")
print ("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of complex and float
a = (10+5j) b = 20.5 a*b = (205+102.5j)
Python − Division Operator (/)
The "/" symbol is usually called as forward slash. The result of division operator is numerator (left
operand) divided by denominator (right operand). The resultant number is negative if any of the operands
is negative. Since infinity cannot be stored in the memory, Python raises ZeroDivisionError if the
denominator is 0.
The result of division operator in Python is always a float, even if both operands are integers.
a=10
b=20
print ("Division of two integers")
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)
It will produce the following output −
Division of two integers
a= 10 b= 20 a/b= 0.5
a= 10 b= 20 b/a= 2.0
In Division, a float operand may have a standard decimal point notation, or a scientific notation.
a=10
b=-20.5
print ("Division of integer and float")
print ("a=",a,"b=",b,"a/b=",a/b)
a=-2.50
b=1.25E2
print ("Division of float and float")
print ("a=",a,"b=",b,"a/b=",a/b)
It will produce the following output −
Division of integer and float
a= 10 b= -20.5 a/b= -0.4878048780487805
Division of float and float
a= -2.5 b= 125.0 a/b= -0.02
When one of the operands is a complex number, division between the other operand and both parts of
complex number (real and imaginary) object takes place.
a=7.5+7.5j
b=2.5
print ("Division of complex and float")
print ("a =",a,"b =",b,"a/b =",a/b)
print ("a =",a,"b =",b,"b/a =",b/a)
It will produce the following output −
Division of complex and float
a = (7.5+7.5j) b = 2.5 a/b = (3+3j)
a = (7.5+7.5j) b = 2.5 b/a = (0.16666666666666666-0.16666666666666666j)
If the numerator is 0, the result of division is always 0 except when denominator is 0, in which case,
Python raises ZeroDivisionError wirh Division by Zero error message.
a=0
b=2.5
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)
It will produce the following output −
a= 0 b= 2.5 a/b= 0.0
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 20, in <module>
print ("a=",a,"b=",b,"b/a=",b/a)
~^~
ZeroDivisionError: float division by zero
Python − Modulus Operator (%)
Python defines the "%" symbol, which is known aa Percent symbol, as Modulus (or modulo) operator. It
returns the remainder after the denominator divides the numerator. It can also be called Remainder
operator. The result of the modulus operator is the number that remains after the integer quotient. To
give an example, when 10 is divided by 3, the quotient is 3 and remainder is 1. Hence, 10%3 (normally
pronounced as 10 mod 3) results in 1.
If both the operands are integer, the modulus value is an integer. If numerator is completely divisible,
remainder is 0. If numerator is smaller than denominator, modulus is equal to the numerator. If
denominator is 0, Python raises ZeroDivisionError.
a=10
b=2
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=4
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=",a, "b=",b, "b%a=", b%a)
a=0
b=10
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=", a, "b=", b, "b%a=",b%a)
It will produce the following output −
a= 10 b= 2 a%b= 0
a= 10 b= 4 a%b= 2
a= 10 b= 4 b%a= 4
a= 0 b= 10 a%b= 0
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 13, in <module>
print ("a=", a, "b=", b, "b%a=",b%a)
~^~
ZeroDivisionError: integer modulo by zero
If any of the operands is a float, the mod value is always float.
a=10
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=1.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=7.7
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=12.4
b=3
print ("a=",a, "b=",b, "a%b=", a%b)
It will produce the following output −
a= 10 b= 2.5 a%b= 0.0
a= 10 b= 1.5 a%b= 1.0
a= 7.7 b= 2.5 a%b= 0.20000000000000018
a= 12.4 b= 3 a%b= 0.40000000000000036
Python doesn't accept complex numbers to be used as operand in modulus operation. It throws
TypeError: unsupported operand type(s) for %.
Python − Exponent Operator (**)
Python uses ** (double asterisk) as the exponent operator (sometimes called raised to operator). So, for
a**b, you say a raised to b, or even bth power of a.
If in the exponentiation expression, both operands are integer, result is also an integer. In case either one
is a float, the result is float. Similarly, if either one operand is complex number, exponent operator returns
a complex number.
If the base is 0, the result is 0, and if the index is 0 then the result is always 1.
a=10
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=10
b=1.5
print ("a=",a, "b=",b, "a**b=", a**b)
a=7.7
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=1+2j
b=4
print ("a=",a, "b=",b, "a**b=", a**b)
a=12.4
b=0
print ("a=",a, "b=",b, "a**b=", a**b)
print ("a=",a, "b=",b, "b**a=", b**a)
It will produce the following output −
a= 10 b= 2 a**b= 100
a= 10 b= 1.5 a**b= 31.622776601683793
a= 7.7 b= 2 a**b= 59.290000000000006
a= (1+2j) b= 4 a**b= (-7-24j)
a= 12.4 b= 0 a**b= 1.0
a= 12.4 b= 0 b**a= 0.0
Python − Floor Division Operator (//)
Floor division is also called as integer division. Python uses // (double forward slash) symbol for the
purpose. Unlike the modulus or modulo which returns the remainder, the floor division gives the quotient
of the division of operands involved.
If both operands are positive, floor operator returns a number with fractional part removed from it. For
example, the floor division of 9.8 by 2 returns 4 (pure division is 4.9, strip the fractional part, result is 4).
But if one of the operands is negative, the result is rounded away from zero (towards negative infinity).
Floor division of -9.8 by 2 returns 5 (pure division is -4.9, rounded away from 0).
a=9
b=2
print ("a=",a, "b=",b, "a//b=", a//b)
a=9
b=-2
print ("a=",a, "b=",b, "a//b=", a//b)
a=10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
a=-10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
It will produce the following output −
a= 9 b= 2 a//b= 4
a= 9 b= -2 a//b= -5
a= 10 b= 1.5 a//b= 6.0
a= -10 b= 1.5 a//b= -7.0
Python − Complex Number Arithmetic
Arithmetic operators behave slightly differently when the both operands are complex number objects.
Addition and subtraction of complex numbers is a simple addition/subtraction of respective real and
imaginary components.
a=2.5+3.4j
b=-3+1.0j
print ("Addition of complex numbers - a=",a, "b=",b, "a+b=", a+b)
print ("Subtraction of complex numbers - a=",a, "b=",b, "a-b=", a-b)
It will produce the following output −
Addition of complex numbers - a= (2.5+3.4j) b= (-3+1j) a+b= (-0.5+4.4j)
Subtraction of complex numbers - a= (2.5+3.4j) b= (-3+1j) a-b=
(5.5+2.4j)
Multiplication of complex numbers is similar to multiplication of two binomials in algebra. If "a+bj" and
"x+yj" are two complex numbers, then their multiplication is given by this formula −
(a+bj)*(x+yj) = ax+ayj+xbj+byj2 = (ax-by)+(ay+xb)j
For example,
a=6+4j
b=3+2j
c=a*b
c=(18-8)+(12+12)j
c=10+24j
The following program confirms the result −
a=6+4j
b=3+2j
print ("Multplication of complex numbers - a=",a, "b=",b, "a*b=", a*b)
To understand the how the division of two complex numbers takes place, we should use the conjugate of
a complex number. Python's complex object has a conjugate() method that returns a complex number
with the sign of imaginary part reversed.
>>> a=5+6j
>>> a.conjugate()
(5-6j)
To divide two complex numbers, divide and multiply the numerator as well as the denominator with the
conjugate of denominator.
a=6+4j
b=3+2j
c=a/b
c=(6+4j)/(3+2j)
c=(6+4j)*(3-2j)/3+2j)*(3-2j)
c=(18-12j+12j+8)/(9-6j+6j+4)
c=26/13
c=2+0j
To verify, run the following code −
a=6+4j
b=3+2j
print ("Division of complex numbers - a=",a, "b=",b, "a/b=", a/b)
Complex class in Python doesn't support the modulus operator (%) and floor division operator (//).
Python - Assignment Operators
The = (equal to) symbol is defined as assignment operator in Python. The value of Python expression on
its right is assigned to a single variable on its left. The = symbol as in programming in general (and Python
in particular) should not be confused with its usage in Mathematics, where it states that the expressions
on the either side of the symbol are equal.
In addition to the simple assignment operator, Python provides few more assignment operators for
advanced use. They are called cumulative or augmented assignment operators. In this chapter, we shall
learn to use augmented assignment operators defined in Python.
Consider following Python statements −
a=10
b=5
a=a+b
print (a)
At the first instance, at least for somebody new to programming but who knows maths, the statement
"a=a+b" looks strange. How could a be equal to "a+b"? However, it needs to be reemphasized that the =
symbol is an assignment operator here and not used to show the equality of LHS and RHS.
Because it is an assignment, the expression on right evaluates to 15, the value is assigned to a.
In the statement "a+=b", the two operators "+" and "=" can be combined in a "+=" operator. It is called as
add and assign operator. In a single statement, it performs addition of two operands "a" and "b", and
result is assigned to operand on left, i.e., "a".
The += operator is an augmented operator. It is also called cumulative addition operator, as it adds "b" in
"a" and assigns the result back to a variable.
Python has the augmented assignment operators for all arithmetic and comparison operators.
Python - Augmented Addition Operator (+=)
This operator combines addition and assignment in one statement. Since Python supports mixed
arithmetic, the two operands may be of different types. However, the type of left operand changes to the
operand of on right, if it is wider.
Following examples will help in understanding how the "+=" operator works −
a=10
b=5
print ("Augmented addition of int and int")
a+=b #equivalent to a=a+b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented addition of int and float")
a+=b #equivalent to a=a+b
print ("a=",a, "type(a):", type(a))
a=10.50
b=5+6j
print ("Augmented addition of float and complex")
a+=b #equivalent to a=a+b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented addition of int and int
a= 15 type(a): <class 'int'>
Augmented addition of int and float
a= 15.5 type(a): <class 'float'>
Augmented addition of float and complex
a= (15.5+6j) type(a): <class 'complex'>
Python − Augmented Subtraction Operator (-=)
Use -= symbol to perform subtract and assign operations in a single statement. The "a-=b" statement
performs "a=a-b" assignment. Operands may be of any number type. Python performs implicit type
casting on the object which is narrower in size.
a=10
b=5
print ("Augmented subtraction of int and int")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented subtraction of int and float")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))
a=10.50
b=5+6j
print ("Augmented subtraction of float and complex")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented subtraction of int and int
a= 5 type(a): <class 'int'>
Augmented subtraction of int and float
a= 4.5 type(a): <class 'float'>
Augmented subtraction of float and complex
a= (5.5-6j) type(a): <class 'complex'>
Python − Augmented Multiplication Operator (*=)
The "*=" operator works on similar principle. "a*=b" performs multiply and assign operations, and is
equivalent to "a=a*b". In case of augmented multiplication of two complex numbers, the rule of
multiplication as discussed in the previous chapter is applicable.
a=10
b=5
print ("Augmented multiplication of int and int")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented multiplication of int and float")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))
a=6+4j
b=3+2j
print ("Augmented multiplication of complex and complex")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented multiplication of int and int
a= 50 type(a): <class 'int'>
Augmented multiplication of int and float
a= 55.0 type(a): <class 'float'>
Augmented multiplication of complex and complex
a= (10+24j) type(a): <class 'complex'>
Python − Augmented Division Operator (/=)
The combination symbol "/=" acts as divide and assignment operator, hence "a/=b" is equivalent to
"a=a/b". The division operation of int or float operands is float. Division of two complex numbers returns
a complex number. Given below are examples of augmented division operator.
a=10
b=5
print ("Augmented division of int and int")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented division of int and float")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))
a=6+4j
b=3+2j
print ("Augmented division of complex and complex")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented division of int and int
a= 2.0 type(a): <class 'float'>
Augmented division of int and float
a= 1.8181818181818181 type(a): <class 'float'>
Augmented division of complex and complex
a= (2+0j) type(a): <class 'complex'>
Python − Augmented Modulus Operator (%=)
To perform modulus and assignment operation in a single statement, use the %= operator. Like the mod
operator, its augmented version also is not supported for complex number.
a=10
b=5
print ("Augmented modulus operator with int and int")
a%=b #equivalent to a=a%b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented modulus operator with int and float")
a%=b #equivalent to a=a%b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented modulus operator with int and int
a= 0 type(a): <class 'int'>
Augmented modulus operator with int and float
a= 4.5 type(a): <class 'float'>
Python − Augmented Exponent Operator (**=)
The "**=" operator results in computation of "a" raised to "b", and assigning the value back to "a". Given
below are some examples −
a=10
b=5
print ("Augmented exponent operator with int and int")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented exponent operator with int and float")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))
a=6+4j
b=3+2j
print ("Augmented exponent operator with complex and complex")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented exponent operator with int and int
a= 100000 type(a): <class 'int'>
Augmented exponent operator with int and float
a= 316227.7660168379 type(a): <class 'float'>
Augmented exponent operator with complex and complex
a= (97.52306038414744-62.22529992036203j) type(a): <class 'complex'>
Python − Augmented Floor division Operator (//=)
For performing floor division and assignment in a single statement, use the "//=" operator. "a//=b" is
equivalent to "a=a//b". This operator cannot be used with complex numbers.
a=10
b=5
print ("Augmented floor division operator with int and int")
a//=b #equivalent to a=a//b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("Augmented floor division operator with int and float")
a//=b #equivalent to a=a//b
print ("a=",a, "type(a):", type(a))
It will produce the following output −
Augmented floor division operator with int and int
a= 2 type(a): <class 'int'>
Augmented floor division operator with int and float
a= 1.0 type(a): <class 'float'>
Python - Comparison Operators
Comparison operators in Python are very important in Python's conditional statements (if, else and elif)
and looping statements (while and for loops). Like arithmetic operators, the comparison operators "-"
also called relational operators, ("<" stands for less than, and">" stands for greater than) are well known.
Python uses two more operators, combining "=" symbol with these two. The "<=" symbol is for less than
or equal to. The ">=" symbol is for greater than or equal to.
Python has two more comparison operators in the form of "==" and "!=". They are for is equal to and is
not equal to operators. Hence, there are six comparison operators in Python.
< Less than a<b
> Greater than a>b
<= Less than or equal to a<=b
>= Greater than or equal to a>=b
== Is equal to a==b
!= Is not equal to a!=b
Comparison operators are binary in nature, requiring two operands. An expression involving a
comparison operator is called a Boolean expression, and always returns either True or False.
a=5
b=7
print (a>b)
print (a<b)
It will produce the following output −
False
True
Both the operands may be Python literals, variables or expressions. Since Python supports mixed
arithmetic, you can have any number type operands.
The following code demonstrates the use of Python's comparison operators with integer numbers −
print ("Both operands are integer")
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
Both operands are integer
a= 5 b= 7 a>b is False
a= 5 b= 7 a<b is True
a= 5 b= 7 a==b is False
a= 5 b= 7 a!=b is True
Comparison of Float Number
In the following example, an integer and a float operand are compared.
print ("comparison of int and float")
a=10
b=10.0
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of int and float
a= 10 b= 10.0 a>b is False
a= 10 b= 10.0 a<b is False
a= 10 b= 10.0 a==b is True
a= 10 b= 10.0 a!=b is False
Comparison of Complex umbers
Although complex object is a number data type in Python, its behavior is different from others. Python
doesn't support < and > operators, however it does support equality (==) and inequality (!=) operators.
print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of complex numbers
a= (10+1j) b= (10-1j) a==b is False
a= (10+1j) b= (10-1j) a!=b is True
You get a TypeError with less than or greater than operators.
print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
It will produce the following output −
comparison of complex numbers
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 5, in <module>
print ("a=",a, "b=",b,"a<b is",a<b)
^^^
TypeError: '<' not supported between instances of 'complex' and
'complex
Comparison of Booleans
Boolean objects in Python are really integers: True is 1 and False is 0. In fact, Python treats any non-zero
number as True. In Python, comparison of Boolean objects is possible. "False < True" is True!
print ("comparison of Booleans")
a=True
b=False
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of Booleans
a= True b= False a<b is False
a= True b= False a>b is True
a= True b= False a==b is False
a= True b= False a!=b is True
Comparison of Sequence Types
In Python, comparison of only similar sequence objects can be performed. A string object is comparable
with another string only. A list cannot be compared with a tuple, even if both have same items.
print ("comparison of different sequence types")
a=(1,2,3)
b=[1,2,3]
print ("a=",a, "b=",b,"a<b is",a<b)
It will produce the following output −
comparison of different sequence types
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 5, in <module>
print ("a=",a, "b=",b,"a<b is",a<b)
^^^
TypeError: '<' not supported between instances of 'tuple' and 'list'
Sequence objects are compared by lexicographical ordering mechanism. The comparison starts from item
at 0th index. If they are equal, comparison moves to next index till the items at certain index happen to
be not equal, or one of the sequences is exhausted. If one sequence is an initial sub-sequence of the
other, the shorter sequence is the smaller (lesser) one.
Which of the operands is greater depends on the difference in values of items at the index where they are
unequal. For example, 'BAT'>'BAR' is True, as T comes after R in Unicode order.
If all items of two sequences compare equal, the sequences are considered equal.
print ("comparison of strings")
a='BAT'
b='BALL'
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of strings
a= BAT b= BALL a<b is False
a= BAT b= BALL a>b is True
a= BAT b= BALL a==b is False
a= BAT b= BALL a!=b is True
In the following example, two tuple objects are compared −
print ("comparison of tuples")
a=(1,2,4)
b=(1,2,3)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
a= (1, 2, 4) b= (1, 2, 3) a<b is False
a= (1, 2, 4) b= (1, 2, 3) a>b is True
a= (1, 2, 4) b= (1, 2, 3) a==b is False
a= (1, 2, 4) b= (1, 2, 3) a!=b is True
Comparison of Dictionary Objects
The use of "<" and ">" operators for Python's dictionary is not defined. In case of these operands,
TypeError: '<' not supported between instances of 'dict' and 'dict' is reported.
Equality comparison checks if the length of both the dict items is same. Length of dictionary is the number
of key-value pairs in it.
Python dictionaries are simply compared by length. The dictionary with fewer elements is considered less
than a dictionary with more elements.
print ("comparison of dictionary objects")
a={1:1,2:2}
b={2:2, 1:1, 3:3}
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
It will produce the following output −
comparison of dictionary objects
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a==b is False
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a!=b is True
Python - Logical Operators
With Logical operators in Python, we can form compound Boolean expressions. Each operand for these
logical operators is itself a Boolean expression. For example,
age>16 and marks>80
percentage<50 or attendance<75
Along with the keyword False, Python interprets None, numeric zero of all types, and empty sequences
(strings, tuples, lists), empty dictionaries, and empty sets as False. All other values are treated as True.
There are three logical operators in Python. They are "and", "or" and "not". They must be in lowercase.
The "and" Operator
For the compound Boolean expression to be True, both the operands must be True. If any or both
operands evaluate to False, the expression returns False. The following table shows the scenarios.
a b a and b
FF F
FT F
TF F
TT T
The "or" Operator
In contrast, the or operator returns True if any of the operands is True. For the compound Boolean
expression to be False, both the operands have to be False, ss the following table shows −
a b a or b
FFF
FTT
TFF
TTT
The "not" Operator
This is a unary operator. The state of Boolean operand that follows, is reversed. As a result, not True
becomes False and not False becomes True.
a not (a)
FT
TF
How the Python interpreter evaluates the logical operators?
The expression "x and y" first evaluates "x". If "x" is false, its value is returned; otherwise, "y" is evaluated
and the resulting value is returned.
The expression "x or y" first evaluates "x"; if "x" is true, its value is returned; otherwise, "y" is evaluated
and the resulting value is returned.
Looping or Iteration − Most of the processes require a group of instructions to be repeatedly executed. In
programming terminology, it is called a loop. Instead of the next step, if the flow is redirected towards
any earlier step, it constitutes a loop.
The following diagram illustrates how the looping works −
If the control goes back unconditionally, it forms an infinite loop which is not desired as the rest of the
code would never get executed.
In a conditional loop, the repeated iteration of block of statements goes on till a certain condition is met.
Python - Decision Making
Python's decision making functionality is in its keywords − if, else and elif. The if keyword requires a
boolean expression, followed by colon symbol.
The colon (:) symbol starts an indented block. The statements with the same level of indentation are
executed if the boolean expression in if statement is True. If the expression is not True (False), the
interpreter bypasses the indented block and proceeds to execute statements at earlier indentation level.
Python − The if Statement
The following flowchart illustrates how Python if statement works −
Syntax
The logic in the above flowchart is expressed by the following syntax −
if expr==True:
stmt1
stmt2
stmt3
..
..
Stmt4
The if statement is similar to that of other languages. The if statement contains a boolean expression
using which the data is compared and a decision is made based on the result of the comparison.
If the boolean expression evaluates to True, then the block of statement(s) inside the if statement is
executed. In Python, statements in a block are uniformly indented after the ":" symbol. If boolean
expression evaluates to False, then the first set of code after the end of block is executed.
Example
Let us consider an example of a customer entitiled to 10% discount if his purchase amount is >1000; if
not, no discount applicable. This flowchart shows the process.
In Python, we first set a discount variable to 0 and accept the amount as input from user.
Then comes the conditional statement if amt>1000. Put : symbol that starts conditional block wherein
discount applicable is calculated. Obviously, discount or not, next statement by default prints amount-
discount. If applied, it will be subtracted, if not it is 0.
discount = 0
amount = 1200
if amount > 1000:
discount = amount * 10 / 100
print("amount = ",amount - discount)
Here the amout is 1200, hence discount 120 is deducted. On executing the code, you will get the
following output −
amount = 1080.0
Change the variable amount to 800, and run the code again. This time, no discount is applicable. And, you
will get the following output −
amount = 800
Python - The if-else Statement
Along with the if statement, else keyword can also be optionally used. It provides an alternate block of
statements to be executed if the Boolean expression (in if statement) is not true. this flowchart shows
how else block is used.
If the expr is True, block of stmt1,2,3 is executed then the default flow continues with stmt7. However,
the If expr is False, block stmt4,5,6 runs then the default flow continues.
Syntax
Python implementation of the above flowchart is as follows −
if expr==True:
stmt1
stmt2
stmt3
else:
stmt4
stmt5
stmt6
Stmt7
Example
Let us understand the use of else clause with following example. The variable age can take different
values. If the expression "age > 18" is true, message you are eligible to vote is displayed otherwise not
eligible message should be displayed. Following flowchart illustrates this logic.
Example
We can write a Python code for the above logic with if-else statements −
amount = int(input('Enter amount: '))
if amount > 10000:
discount = amount * 20 / 100
else:
if amount > 5000:
discount = amount * 10 / 100
else:
if amount > 1000:
discount = amount * 5 / 100
else:
dicount = 0
print('amount: ',amount - discount)
While the code will work perfectly ok, if you look at the increasing level of indentation at each if and else
statement, it will become difficult to manage if there are still more conditions.
The elif statement makes the code easy to read and comprehend.
Elif is short for "else if". It allows the logic to be arranged in a cascade of elif statements after the first if
statement. If the first if statement evaluates to false, subsequent elif statements are evaluated one by
one and comes out of the cascade if any one is satisfied.
Last in the cascade is the else block which will come in picture when all preceding if/elif conditions fail.
amount = 800
print('amount = ',amount)
if amount > 10000:
discount = amount * 20 / 100
elif amount > 5000:
discount = amount * 10 / 100
elif amount > 1000:
discount = amount * 5 / 100
else:
discount=0
Since the loop is executed for each member element in a sequence, there is no need for explicit
verification of Boolean expression controlling the loop (as in while loop).
The sequence objects such as list, tuple or string are called iterables, as the for loop iterates through the
collection. Any iterator object can be iterated by the for loop.
The view objects returned by items(), keys() and values() methods of dictionary are also iterables, hence
we can run a for loop with them.
Python's built-in range() function returns an iterator object that streams a sequence of numbers. We can
run a for loop with range.
Using "for" with a String
A string is a sequence of Unicode letters, each having a positional index. The following example compares
each character and displays if it is not a vowel ('a', 'e', 'I', 'o' or 'u')
Example
zen = '''
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
'''
for char in zen:
if char not in 'aeiou':
print (char, end='')
Output
On executing, this code will produce the following output −
Btfl s bttr thn gly.
Explct s bttr thn mplct.
Smpl s bttr thn cmplx.
Cmplx s bttr thn cmplctd.
Using "for" with a Tuple
Python's tuple object is also an indexed sequence, and hence we can traverse its items with a for loop.
Example
In the following example, the for loop traverses a tuple containing integers and returns the total of all
numbers.
numbers = (34,54,67,21,78,97,45,44,80,19)
total = 0
for num in numbers:
total+=num
print ("Total =", total)
Output
On executing, this code will produce the following output −
Total = 539
Using "for" with a List
Python's list object is also an indexed sequence, and hence we can traverse its items with a for loop.
Example
In the following example, the for loop traverses a list containing integers and prints only those which are
divisible by 2.
numbers = [34,54,67,21,78,97,45,44,80,19]
total = 0
for num in numbers:
if num%2 == 0:
print (num)
Output
On executing, this code will produce the following output −
34
54
78
44
80
Using "for" with a Range Object
Python's buil-in range() function returns a range object. Python's range object is an iterator which
generates an integer with each iteration. The object contains integrrs from start to stop, separated by
step parameter.
Syntax
The range() function has the following syntax −
range(start, stop, step)
Parameters
Start − Starting value of the range. Optional. Default is 0
Stop − The range goes upto stop-1
Step − Integers in the range increment by the step value. Option, default is 1.
Return Value
The range() function returns a range object. It can be parsed to a list sequence.
Example
numbers = range(5)
'''
start is 0 by default,
step is 1 by default,
range generated from 0 to 4
'''
print (list(numbers))
# step is 1 by default, range generated from 10 to 19
numbers = range(10,20)
print (list(numbers))
# range generated from 1 to 10 increment by step of 2
numbers = range(1, 10, 2)
print (list(numbers))
Output
On executing, this code will produce the following output −
[0, 1, 2, 3, 4]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 3, 5, 7, 9]
Example
Once we obtain the range, we can use the for loop with it.
for num in range(5):
print (num, end=' ')
print()
for num in range(10,20):
print (num, end=' ')
print()
for num in range(1, 10, 2):
print (num, end=' ')
Output
On executing, this code will produce the following output −
01234
10 11 12 13 14 15 16 17 18 19
13579
Example: Factorial of a Number
Factorial is a product of all numbers from 1 to that number say n. It can also be defined as product of 1, 2,
up to n.
Factorial of a number n! = 1 * 2 * . . . . . * n
We use the range() function to get the sequence of numbers from 1 to n-1 and perform cumumulative
multplication to get the factorial value.
fact=1
N=5
for x in range(1, N+1):
fact=fact*x
print ("factorial of {} is {}".format(N, fact))
Output
On executing, this code will produce the following output −
factorial of 5 is 120
In the above program, change the value of N to obtain factorial value of different numbers.
Using "for" Loop with Sequence Index
To iterate over a sequence, we can obtain the list of indices using the range() function
Indices = range(len(sequence))
We can then form a for loop as follows:
numbers = [34,54,67,21,78]
indices = range(len(numbers))
for index in indices:
print ("index:",index, "number:",numbers[index])
On executing, this code will produce the following output −
index: 0 number: 34
index: 1 number: 54
index: 2 number: 67
index: 3 number: 21
index: 4 number: 78
Using "for" with Dictionaries
Unlike a list, tuple or a string, dictionary data type in Python is not a sequence, as the items do not have a
positional index. However, traversing a dictionary is still possible with different techniques.
Running a simple for loop over the dictionary object traverses the keys used in it.
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
for x in numbers:
print (x)
On executing, this code will produce the following output −
10
20
30
40
Once we are able to get the key, its associated value can be easily accessed either by using square
brackets operator or with the get() method. Take a look at the following example −
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
for x in numbers:
print (x,":",numbers[x])
It will produce the following output −
10 : Ten
20 : Twenty
30 : Thirty
40 : Forty
The items(), keys() and values() methods of dict class return the view objects dict_items, dict_keys and
dict_values respectively. These objects are iterators, and hence we can run a for loop over them.
The dict_items object is a list of key-value tuples over which a for loop can be run as follows −
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
for x in numbers.items():
print (x)
It will produce the following output −
(10, 'Ten')
(20, 'Twenty')
(30, 'Thirty')
(40, 'Forty')
Here, "x" is the tuple element from the dict_items iterator. We can further unpack this tuple in two
different variables. Check the following code −
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
for x,y in numbers.items():
print (x,":", y)
It will produce the following output −
10 : Ten
20 : Twenty
30 : Thirty
40 : Forty
Similarly, the collection of keys in dict_keys object can be iterated over. Take a look at the following
example −
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
for x in numbers.keys():
print (x, ":", numbers[x])
It will produce the same output −
10 : Ten
20 : Twenty
30 : Thirty
40 : Forty
Python - The forelse Loop
Python supports having an "else" statement associated with a "for" loop statement. If the "else"
statement is used with a "for" loop, the "else" statement is executed when the sequence is exhausted
before the control shifts to the main line of execution.
The following flow diagram illustrates how to use else statement with for loop −
Example
The following example illustrates the combination of an else statement with a for statement. Till the
count is less than 5, the iteration count is printed. As it becomes 5, the print statement in else block is
executed, before the control is passed to the next statement in the main program.
for count in range(6):
print ("Iteration no. {}".format(count))
else:
print ("for loop over. Now in else block")
print ("End of for loop")
On executing, this code will produce the following output −
Iteration no. 1
Iteration no. 2
Iteration no. 3
Iteration no. 4
Iteration no. 5
for loop over. Now in else block
End of for loop
Nested Loops
Python programming language allows the use of one loop inside another loop. The following section
shows a few examples to illustrate the concept.
Syntax
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows −
while expression:
while expression:
statement(s)
statement(s)
A final note on loop nesting is that you can put any type of loop inside any other type of loop. For
example a for loop can be inside a while loop or vice versa.
Example
The following program uses a nested-for loop to display multiplication tables from 1-10.
#!/usr/bin/python3
for i in range(1,11):
for j in range(1,11):
k=i*j
print ("{:3d}".format(k), end=' ')
print()
The print() function inner loop has end=' ' which appends a space instead of default newline. Hence, the
numbers will appear in one row.
The last print() will be executed at the end of inner for loop.
When the above code is executed, it produces the following output −
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Python - The while Loop
Normally, flow of execution of steps in a computer program goe from start to end. However, instead of
the next step, if the flow is redirected towards any earlier step, it constitutes a loop.
A while loop statement in Python programming language repeatedly executes a target statement as long
as a given boolean expression is true.
Syntax
The syntax of a while loop in Python programming language is −
while expression:
statement(s)
The while keyword is followed by a boolean expression, and then by colon symbol, to start an indented
block of statements. Here, statement(s) may be a single statement or a block of statements with uniform
indent. The condition may be any expression, and true is any non-zero value. The loop iterates while the
boolean expression is true.
As soon as the expression becomes false, the program control passes to the line immediately following
the loop.
If it fails to turn false, the loop continues to run, and doesn't stop unless forcefully stopped. Such a loop is
called infinite loop, which is undesired in a computer program.
The following flow diagram illustrates the while loop −
Example 1
In Python, all the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its method of
grouping statements.
count=0
while count<5:
count+=1
print ("Iteration no. {}".format(count))
Example
The following example illustrates the combination of an else statement with a while statement. Till the
count is less than 5, the iteration count is printed. As it becomes 5, the print statement in else block is
executed, before the control is passed to the next statement in the main program.
count=0
while count<5:
count+=1
print ("Iteration no. {}".format(count))
else:
print ("While loop over. Now in else block")
print ("End of while loop")
Output
On executing, this code will produce the following output −
Iteration no. 1
Iteration no. 2
Iteration no. 3
Iteration no. 4
Iteration no. 5
While loop over. Now in else block
End of while loop
Python - The break Statement
Loop Control Statements
The Loop control statements change the execution from its normal sequence. When the execution leaves
a scope, all automatic objects that were created in that scope are destroyed.
Python supports the following control statements −
Sr.No. Control Statement & Description
break statement
1 Terminates the loop statement and transfers execution to the statement immediately following the
loop.
continue statement
2 Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.
pass statement
3 The pass statement in Python is used when a statement is required syntactically but you do not
want any command or code to execute.
Let us go through the loop control statements briefly.
Python − The break Statement
The break statement is used for premature termination of the current loop. After abandoning the loop,
execution at the next statement is resumed, just like the traditional break statement in C.
The most common use of break is when some external condition is triggered requiring a hasty exit from a
loop. The break statement can be used in both while and for loops.
If you are using nested loops, the break statement stops the execution of the innermost loop and starts
executing the next line of the code after the block.
Syntax
The syntax for a break statement in Python is as follows −
break
Flow Diagram
Its flow diagram looks like this −
Example 1
Now let's take an example to understand how the "break" statement works in Python −
#!/usr/bin/python3
print ('First example')
for letter in 'Python': # First Example
if letter == 'h':
break
print ('Current Letter :', letter)
print ('Second example')
var = 10 # Second Example
while var > 0:
print ('Current variable value :', var)
var = var -1
if var == 5:
break
print ("Good bye!")
When the above code is executed, it produces the following output −
First example
Current Letter : P
Current Letter : y
Current Letter : t
Second example
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
Example 2
The following program demonstrates the use of break in a for loop iterating over a list. User inputs a
number, which is searched in the list. If it is found, then the loop terminates with the 'found' message.
#!/usr/bin/python3
no=int(input('any number: '))
numbers=[11,33,55,39,55,75,37,21,23,41,13]
for num in numbers:
if num==no:
print ('number found in list')
break
else:
print ('number not found in list')
The above program will produce the following output −
any number: 33
number found in list
any number: 5
number not found in list
Example 3: Checking for Prime Number
Note that when the break statement is encountered, Python abandons the remaining statements in the
loop, including the else block.
The following example takes advantage of this behaviour to find whether a number is prime or not. By
definition, a number is prime if it is not divisible by any other number except 1 and itself.
The following code runs a for loop over numbers from 2 to the desired number-1. If it divisible by any
value of looping variable, the number is not prime, hence the program breaks from the loop. If the
number is not divisible by any number between 2 and x-1, the else block prints the message that the
given number is prime.
num = 37
print ("Number: ", num)
for x in range(2,num):
if num%x==0:
print ("{} is not prime".format(num))
break
else:
print ("{} is prime".format(num))
Output
Assign different values to num to check if it is a prime number or not.
Number: 37
37 is prime
Number: 49
49 is not prime
Python - The Continue Statement
The continue statement in Python returns the control to the beginning of the current loop. When
encountered, the loop starts next iteration without executing the remaining statements in the current
iteration.
The continue statement can be used in both while and for loops.
Syntax
continue
Flow Diagram
The flow diagram of the continue statement looks like this −
The continue statement is just the opposite to that of break. It skips the remaining statements in the
current loop and starts the next iteration.
Example 1
Now let's take an example to understand how the continue statement works in Python −
for letter in 'Python': # First Example
if letter == 'h':
continue
print ('Current Letter :', letter)
var = 10 # Second Example
while var > 0:
var = var -1
if var == 5:
continue
print ('Current variable value :', var)
print ("Good bye!")
When the above code is executed, it produces the following output −
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!
Example 2: Checking Prime Factors
Following code uses continue to find the prime factors of a given number. To find prime factors, we need
to successively divide the given number starting with 2, increment the divisior and continue the same
process till the input reduces to 1.
The algorithm for finding prime factors is as follows −
Accept input from user (n)
Set divisor (d) to 2
Perform following till n>1
Check if given number (n) is divisible by divisor (d).
If n%d==0
o a. Print d as a factor
o Set new value of n as n/d
o Repeat from 4
If not
Increment d by 1
Repeat from 3
Given below is the Python code for the purpose −
num = 60
print ("Prime factors for: ", num)
d=2
while num>1:
if num%d==0:
print (d)
num=num/d
continue
d=d+1
On executing, this code will produce the following output −
Prime factors for: 60
2
2
3
5
Assign different value (say 75) to num in the above program and test the result for its prime factors.
Prime factors for: 75
3
5
5
Python - The pass Statement
The pass statement is used when a statement is required syntactically but you do not want any command
or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass statement is also
useful in places where your code will eventually go, but has not been written yet, i.e., in stubs).
Syntax
pass
Example
The following code shows how you can use the pass statement in Python −
for letter in 'Python':
if letter == 'h':
pass
print ('This is pass block')
print ('Current Letter :', letter)
print ("Good bye!")
When the above code is executed, it produces the following output −
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!
Python - Functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions
provide better modularity for your application and a high degree of code reusing.
A top-to-down approach towards building the processing logic involves defining blocks of independent
reusable functions. A function may be invoked from any other function by passing required data (called
parameters or arguments). The called function returns its result back to the calling environment.
C/C++ functions are said to be called by value. When a function in C/C++ is called, the value of actual
arguments is copied to the variables representing the formal arguments. If the function modifies the
value of formal aergument, it doesn't reflect the variable that was passed to it.
Python uses pass by reference mechanism. As variable in Python is a label or reference to the object in
the memory, the both the variables used as actual argument as well as formal arguments really refer to
the same object in the memory. We can verify this fact by checking the id() of the passed variable before
and after passing.
def testfunction(arg):
print ("ID inside the function:", id(arg))
var="Hello"
print ("ID before passing:", id(var))
testfunction(var)
If the above code is executed, the id() before passing and inside the function is same.
ID before passing: 1996838294128
ID inside the function: 1996838294128
The behaviour also depends on whether the passed object is mutable or immutable. Python numeric
object is immutable. When a numeric object is passed, and then the function changes the value of the
formal argument, it actually creates a new object in the memory, leaving the original variable unchanged.
def testfunction(arg):
print ("ID inside the function:", id(arg))
arg=arg+1
print ("new object after increment", arg, id(arg))
var=10
print ("ID before passing:", id(var))
testfunction(var)
print ("value after function call", var)
It will produce the following output −
ID before passing: 140719550297160
ID inside the function: 140719550297160
new object after increment 11 140719550297192
value after function call 10
Let us now pass a mutable object (such as a list or dictionary) to a function. It is also passed by reference,
as the id() of lidt before and after passing is same. However, if we modify the list inside the function, its
global representation also reflects the change.
Here we pass a list, append a new item, and see the contents of original list object, which we will find has
changed.
def testfunction(arg):
print ("Inside function:",arg)
print ("ID inside the function:", id(arg))
arg=arg.append(100)
Example
Let's modify greetings function and have name an argument. A string passed to it whilcalling becomes
name variable inside the function.
def greetings(name):
"This is docstring of greetings function"
print ("Hello {}".format(name))
return
greetings("Samay")
greetings("Pratima")
greetings("Steven")
It will produce the following output −
Hello Samay
Hello Pratima
Hello Steven
Function with Return Value
The return keyword as the last statement in function definition indicates end of function block, and the
program flow goes back to the calling function. Although reduced indent after the last statement in the
block also implies return but using explicit return is a good practice.
Along with the flow control, the function can also return value of an expression to the calling function.
The value of returned expression can be stored in a variable for further processing.
Example
Let us define the add() function. It adds the two values passed to it and returns the addition. The returned
value is stored in a variable called result.
def add(x,y):
z=x+y
return z
a=10
b=20
result = add(a,b)
print ("a = {} b = {} a+b = {}".format(a, b, result))
It will produce the following output −
a = 10 b = 20 a+b = 30
Types of Function Arguments
Based on how the arguments are declared while defining a Python function, there are classified into the
following categories −
Positional or required arguments
Keyword arguments
Default arguments
Positional-only arguments
Keyword-only arguments
Arbitrary or variable-length arguments
In the next few chapters, we will discuss these function arguments at length.
Order of Arguments
A function can have arguments of any of the types defined above. However, the arguments must be
declared in the following order −
The argument list begins with the positional-only args, followed by the slash (/) symbol.
It is followed by regular positional args that may or may not be called as keyword arguments.
Then there may be one or more args with default values.
Next, arbitrary positional arguments represented by a variable prefixed with single asterisk, that is
treated as tuple. It is the next.
If the function has any keyword-only arguments, put an asterisk before their names start. Some of
the keyword-only arguments may have a default value.
Last in the bracket is argument with two asterisks ** to accept arbitrary number of keyword
arguments.
The following diagram shows the order of formal arguments −
phy = 60
maths = 70
result = percent(phy,maths)
print ("percentage:", result)
phy = 40
maths = 46
result = percent(phy,maths, 100)
print ("percentage:", result)
It will produce the following output −
percentage: 65.0
percentage: 86.0
Python - Keyword Arguments
Keyword argument are also called named arguments. Variables in the function definition are used as
keywords. When the function is called, you can explicitly mention the name and its value.
Example
# Function definition is here
def printinfo( name, age ):
"This prints a passed info into this function"
print ("Name: ", name)
print ("Age ", age)
return
# by keyword arguments
printinfo(name="miki", age = 30)
By default, the function assigns the values to arguments in the order of appearance. In the second
function call, we have assigned the value to a specific argument
It will produce the following output −
Name: Naveen
Age 29
Name: miki
Age 30
Let us try to understand more about keyword argument with the help of following function definition −
def division(num, den):
quotient = num/den
print ("num:{} den:{} quotient:{}".format(num, den, quotient))
division(10,5)
division(5,10)
Since the values are assigned as per the position, the output is as follows −
num:10 den:5 quotient:2.0
num:5 den:10 quotient:0.5
Instead ofpassing the values with positional arguments, let us call the function with keyword arguments −
division(num=10, den=5)
division(den=5, num=10)
It will produce the following output −
num:10 den:5 quotient:2.0
num:10 den:5 quotient:2.0
When using keyword arguments, it is not necessary to follow the order of formal arguments in function
definition.
Using keyword arguments is optional. You can use mixed calling. You can pass values to some arguments
without keywords, and for others with keyword.
division(10, den=5)
However, the positional arguments must be before the keyword arguments while using mixed calling.
Try to call the division() function with the following statement.
division(num=5, 10)
As the Positional argument cannot appear after keyword arguments, Python raises the following error
message −
division(num=5, 10)
^
SyntaxError: positional argument follows keyword argument
Python - Keyword-Only Arguments
You can use the variables in formal argument list as keywords to pass value. Use of keyword arguments is
optional. But, you can force the function be given arguments by keyword only. You should put an astreisk
(*) before the keyword-only arguments list.
Let us say we have a function with three arguments, out of which we want second and third arguments to
be keyword-only. For that, put * after the first argument.
The built-in print() function is an example of keyword-only arguments. You can give list of expressions to
be printed in the parentheses. The printed values are separated by a white space by default. You can
specify any other separation character instead with sep argument.
print ("Hello", "World", sep="-")
It will print −
Hello-World
The sep argument is keyword-only. Try using it as non-keyword argument.
print ("Hello", "World", "-")
You'll get different output − not as desired.
Hello World -
Example
In the following user defined function intr() with two arguments, amt and rate. To make the rate
argument keyword-only, put "*" before it.
def intr(amt,*, rate):
val = amt*rate/100
return val
To call this function, the value for rate must be passed by keyword.
interest = intr(1000, rate=10)
However, if you try to use the default positional way of calling the function, you get an error.
interest = intr(1000, 10)
^^^^^^^^^^^^^^
TypeError: intr() takes 1 positional argument but 2 were given
Python - Positional Arguments
The list of variables declared in the parentheses at the time of defining a function are the formal
arguments. A function may be defined with any number of formal arguments.
While calling a function −
All the arguments are required
The number of actual arguments must be equal to the number of formal arguments.
Formal arguments are positional. They Pick up values in the order of definition.
The type of arguments must match.
Names of formal and actual arguments need not be same.
Example
def add(x,y):
z=x+y
print ("x={} y={} x+y={}".format(x,y,z))
a=10
b=20
add(a,b)
It will produce the following output −
x=10 y=20 x+y=30
Here, the add() function has two formal arguments, both are numeric. When integers 10 and 20 passed to
it. The variable a takes 10 and b takes 20, in the order of declaration. The add() function displays the
addition.
Python also raises error when the number of arguments don't match. Give only one argument and check
the result.
add(b)
TypeError: add() missing 1 required positional argument: 'y'
Pass more than number of formal arguments and check the result −
add(10, 20, 30)
TypeError: add() takes 2 positional arguments but 3 were given
Data type of corresponding actual and formal arguments must match. Change a to a string value and see
the result.
a="Hello"
b=20
add(a,b)
It will produce the following output −
z=x+y
~^~
TypeError: can only concatenate str (not "int") to str
Python - Positional-Only Arguments
It is possible to define a function in which one or more arguments can not accept their value with
keywords. Such arguments may be called positional-only arguments.
Python's built-in input() function is an example of positional-only arguments. The syntax of input function
is −
input(prompt = "")
Prompt is an explanatory string for the benefit of the user. For example −
name = input("enter your name ")
However, you cannot use the prompt keyword inside the parantheses.
name = input (prompt="Enter your name ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: input() takes no keyword arguments
To make an argument positional-only, use the "/" symbol. All the arguments before this symbol will be
treated as position-only.
Example
We make both the arguments of intr() function as positional-only by putting "/" at the end.
def intr(amt, rate, /):
val = amt*rate/100
return val
If we try to use the arguments as keywords, Python raises following error message −
interest = intr(amt=1000, rate=10)
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: intr() got some positional-only arguments passed as keyword arguments: 'amt, rate'
A function may be defined in such a way that it has some keyword-only and some positional-only
arguments.
def myfunction(x, /, y, *, z):
print (x, y, z)
In this function, x is a required positional-only argument, y is a regular positional argument (you can use it
as keyword if you want), and z is a keyword-only argument.
The following function calls are valid −
myfunction(10, y=20, z=30)
myfunction(10, 20, z=30)
However, these calls raise errors −
myfunction(x=10, y=20, z=30)
TypeError: myfunction() got some positional-only arguments passed as keyword arguments: 'x'
result = add(1,2,3)
print (result)
The args variable prefixed with "*" stores all the values passed to it. Here, args becomes a tuple. We can
run a loop over its items to add the numbers.
It will produce the following output −
100
6
It is also possible to have a function with some required arguments before the sequence of variable
number of values.
Example
The following example has avg() function. Assume that a student can take any number of tests. First test
is mandatory. He can take as many tests as he likes to better his score. The function calculates the
average of marks in first test and his maximum score in the rest of tests.
The function has two arguments, first is the required argument and second to hold any number of values.
#avg of first test and best of following tests
def avg(first, *rest):
second=max(rest)
return (first+second)/2
result=avg(40,30,50,25)
print (result)
Following call to avg() function passes first value to the required argument first, and the remaining values
to a tuple named rest. We then find the maximum and use it to calculate the average.
It will produce the following output −
45.0
If a variable in the argument list has two asterisks prefixed to it, the function can accept arbitrary number
of keyword arguments. The variable becomes a dictionary of keyword:value pairs.
Example
The following code is an example of a function with arbitrary keyword arguments. The addr() function has
an argument **kwargs which is able to accept any number of address elements like name, city, phno, pin,
etc. Inside the function kwargs dictionary of kw:value pairs is traversed using items() method.
def addr(**kwargs):
for k,v in kwargs.items():
print ("{}:{}".format(k,v))
The life of a certain variable is restricted to the namespace in which it is defined. As a result, it is not
possible to access a variable present in the inner namespace from any outer namespace.
globals() Function
Python's standard library includes a built-in function globals(). It returns a dictionary of symbols currently
available in global namespace.
Run the globals() function directly from the Python prompt.
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class
'_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module
'builtins' (built-in)>}
It can be seen that the builtins module which contains definitions of all built-in functions and built-in
exceptions is loaded.
Save the following code that contains few variables and a function with few more variables inside it.
name = 'TutorialsPoint'
marks = 50
result = True
def myfunction():
a = 10
b = 20
return a+b
print (globals())
Calling globals() from inside this script returns following dictionary object −
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':
<_frozen_importlib_external.SourceFileLoader object at 0x00000263E7255250>, '__spec__': None,
'__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\user\\examples\\
main.py', '__cached__': None, 'name': 'TutorialsPoint', 'marks': 50, 'result': True, 'myfunction': <function
myfunction at 0x00000263E72004A0>}
The global namespace now contains variables in the program and their values and the function object in it
(and not the variables in the function).
locals() Function
Python's standard library includes a built-in function locals(). It returns a dictionary of symbols currently
available in namespace of the function.
Modify the above script to print dictionary of global and local namespaces from within the function.
name = 'TutorialsPoint'
marks = 50
result = True
def myfunction():
a = 10
b = 20
c = a+b
print ("globals():", globals())
print ("locals():", locals())
return c
myfunction()
The output shows that locals() returns a dictionary of variables and their values currently available in the
function.
globals(): {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':
<_frozen_importlib_external.SourceFileLoader object at 0x00000169AE265250>, '__spec__': None,
'__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\mlath\\
examples\\main.py', '__cached__': None, 'name': 'TutorialsPoint', 'marks': 50, 'result': True, 'myfunction':
<function myfunction at 0x00000169AE2104A0>}
locals(): {'a': 10, 'b': 20, 'c': 30}
Since both globals() and locals functions return dictionary, you can access value of a variable from
respective namespace with dictionary get() method or index operator.
print (globals()['name']) #displays TutorialsPoint
print (locals().get('a')) #displays 10
Namespace Conflict
If a variable of same name is present in global as well as local scope, Python interpreter gives priority to
the one in local namespace.
marks = 50 # this is a global variable
def myfunction():
marks = 70 # this is a local variable
print (marks)
myfunction()
print (marks) # prints global value
It will produce the following output −
70
50
If you try to manipulate value of a global variable from inside a function, Python raises
UnboundLocalError.
marks = 50 # this is a global variable
def myfunction():
marks = marks + 20
print (marks)
myfunction()
print (marks) # prints global value
It will produce the following output −
marks = marks + 20
^^^^^
UnboundLocalError: cannot access local variable 'marks' where it is not associated with a value
To modify a global variable, you can either update it with a dictionary syntax, or use the global keyword
to refer it before modifying.
var1 = 50 # this is a global variable
var2 = 60 # this is a global variable
def myfunction():
"Change values of global variables"
globals()['var1'] = globals()['var1']+10
global var2
var2 = var2 + 20
myfunction()
print ("var1:",var1, "var2:",var2) #shows global variables with changed values
It will produce the following output −
var1: 60 var2: 80
Lastly, if you try to access a local variable in global scope, Python raises NameError as the variable in local
scope can't be accessed outside it.
var1 = 50 # this is a global variable
var2 = 60 # this is a global variable
def myfunction(x, y):
total = x+y
print ("Total is a local variable: ", total)
myfunction(var1, var2)
print (total) # This gives NameError
It will produce the following output −
Total is a local variable: 110
Traceback (most recent call last):
File "C:\Users\user\examples\main.py", line 9, in <module>
print (total) # This gives NameError
^^^^^
NameError: name 'total' is not defined
Python - Function Annotations
The function annotation feature of Python enables you to add additional explanatory metada about the
arguments declared in a function definition, and also the return data type.
Although you can use the docstring feature of Python for documentation of a function, it may be obsolete
if certain changes in the function's prototype are made. Hence, the annotation feature was introduced in
Python as a result of PEP 3107.
The annotations are not considered by Python interpreter while executing the function. They are mainly
for the Python IDEs for providing a detailed documentation to the programmer.
Annotations are any valid Python expressions added to the arguments or return data type. Simplest
example of annotation is to prescribe the data type of the arguments. Annotation is mentioned as an
expression after putting a colon in front of the argument.
def myfunction(a: int, b: int):
c = a+b
return c
Remember that Python is a dynamically typed language, and doesn't enforce any type checking at
runtime. Hence annotating the arguments with data types doesn't have any effect while calling the
function. Even if non-integer arguments are given, Python doesn't detect any error.
def myfunction(a: int, b: int):
c = a+b
return c
print (myfunction(10,20))
print (myfunction("Hello ", "Python"))
It will produce the following output −
30
Hello Python
Annotations are ignored at runtime, but are helpful for the IDEs and static type checker libraries such as
mypy.
You can give annotation for the return data type as well. After the parentheses and before the colon
symbol, put an arrow (->) followed by the annotation. For example −
def myfunction(a: int, b: int) -> int:
c = a+b
return c
As using the data type as annotation is ignored at runtime, you can put any expression which acts as the
metadata for the arguments. Hence, function may have any arbitrary expression as annotation as in
following example −
def total(x : 'marks in Physics', y: 'marks in chemistry'):
return x+y
If you want to specify a default argument along with the annotation, you need to put it after the
annotation expression. Default arguments must come after the required arguments in the argument list.
def myfunction(a: "physics", b:"Maths" = 20) -> int:
c = a+b
return c
print (myfunction(10))
The function in Python is also an object, and one of its attributes is __annotations__. You can check with
dir() function.
print (dir(myfunction))
This will print the list of myfunction object containing __annotations__ as one of the attributes.
['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__',
'__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__',
'__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
The __annotations__ attribute itself is a dictionary in which arguments are keys and anootations their
values.
def myfunction(a: "physics", b:"Maths" = 20) -> int:
c = a+b
return c
print (myfunction.__annotations__)
It will produce the following output −
{'a': 'physics', 'b': 'Maths', 'return': <class 'int'>}
You may have arbitrary positional and/or arbitrary keyword arguments for a function. Annotations can be
given for them also.
def myfunction(*args: "arbitrary args", **kwargs: "arbitrary keyword args") -> int:
pass
print (myfunction.__annotations__)
It will produce the following output −
{'args': 'arbitrary args', 'kwargs': 'arbitrary keyword args', 'return': <class 'int'>}
In case you need to provide more than one annotation expressions to a function argument, give it in the
form of a dictionary object in front of the argument itself.
def division(num: dict(type=float, msg='numerator'), den: dict(type=float, msg='denominator')) -> float:
return num/den
print (division.__annotations__)
It will produce the following output −
{'num': {'type': <class 'float'>, 'msg': 'numerator'}, 'den': {'type': <class 'float'>, 'msg': 'denominator'},
'return': <class 'float'>}
Python - Modules
A function is a block of organized, reusable code that is used to perform a single, related action. Functions
provide better modularity for your application and a high degree of code reusing.
The concept of module in Python further enhances the modularity. You can define more than one related
functions together and load required functions. A module is a file containing definition of functions,
classes, variables, constants or any other Python object. Contents of this file can be made available to any
other program. Python has the import keyword for this purpose.
Example
import math
print ("Square root of 100:", math.sqrt(100))
It will produce the following output −
Square root of 100: 10.0
Built in Modules
Python's standard library comes bundled with a large number of modules. They are called built-in
modules. Most of these built-in modules are written in C (as the reference implementation of Python is in
C), and pre-compiled into the library. These modules pack useful functionality like system-specific OS
management, disk IO, networking, etc.
Here is a select list of built-in modules −
Sr.No. Name & Brief Description
os
1
This module provides a unified interface to a number of operating system functions.
string
2
This module contains a number of functions for string processing
re
3 This module provides a set of powerful regular expression facilities. Regular expression (RegEx),
allows powerful string search and matching for a pattern in a string
math
4 This module implements a number of mathematical operations for floating point numbers. These
functions are generally thin wrappers around the platform C library functions.
cmath
5
This module contains a number of mathematical operations for complex numbers.
6 datetime
This module provides functions to deal with dates and the time within a day. It wraps the C runtime
library.
gc
7
This module provides an interface to the built-in garbage collector.
asyncio
8
This module defines functionality required for asynchronous processing
Collections
9
This module provides advanced Container datatypes.
Functools
10 This module has Higher-order functions and operations on callable objects. Useful in functional
programming
operator
11
Functions corresponding to the standard operators.
pickle
12
Convert Python objects to streams of bytes and back.
socket
13
Low-level networking interface.
sqlite3
14
A DB-API 2.0 implementation using SQLite 3.x.
statistics
15
Mathematical statistics functions
typing
16
Support for type hints
venv
17
Creation of virtual environments.
json
18
Encode and decode the JSON format.
wsgiref
19
WSGI Utilities and Reference Implementation.
unittest
20
Unit testing framework for Python.
random
21
Generate pseudo-random numbers
User Defined Modules
Any text file with .py extension and containing Python code is basically a module. It can contain
definitions of one or more functions, variables, constants as well as classes. Any Python object from a
module can be made available to interpreter session or another Python script by import statement. A
module can also include runnable code.
Create a Module
Creating a module is nothing but saving a Python code with the help of any editor. Let us save the
following code as mymodule.py
def SayHello(name):
print ("Hi {}! How are you?".format(name))
return
You can now import mymodule in the current Python terminal.
>>> import mymodule
>>> mymodule.SayHello("Harish")
Hi Harish! How are you?
You can also import one module in another Python script. Save the following code as example.py
import mymodule
mymodule.SayHello("Harish")
Run this script from command terminal
C:\Users\user\examples> python example.py
Hi Harish! How are you?
The import Statement
In Python, the import keyword has been provided to load a Python object from one module. The object
may be a function, class, a variable etc. If a module contains multiple definitions, all of them will be
loaded in the namespace.
Let us save the following code having three functions as mymodule.py.
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
The import mymodule statement loads all the functions in this module in the current namespace. Each
function in the imported module is an attribute of this module object.
>>> dir(mymodule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
'average', 'power', 'sum']
To call any function, use the module object's reference. For example, mymodule.sum().
import mymodule
print ("sum:",mymodule.sum(10,20))
print ("average:",mymodule.average(10,20))
print ("power:",mymodule.power(10, 2))
It will produce the following output −
sum:30
average:15.0
power:100
The from ... import Statement
The import statement will load all the resources of the module in the current namespace. It is possible to
import specific objects from a module by using this syntax. For example −
Out of three functions in mymodule, only two are imported in following executable script example.py
from mymodule import sum, average
print ("sum:",sum(10,20))
print ("average:",average(10,20))
It will produce the following output −
sum: 30
average: 15.0
Note that function need not be called by prefixing name of its module to it.
The from...import * Statement
It is also possible to import all the names from a module into the current namespace by using the
following import statement −
from modname import *
This provides an easy way to import all the items from a module into the current namespace; however,
this statement should be used sparingly.
The import ... as Statement
You can assign an alias name to the imported module.
from modulename as alias
The alias should be prefixed to the function while calling.
Take a look at the following example −
import mymodule as x
print ("sum:",x.sum(10,20))
print ("average:", x.average(10,20))
print ("power:", x.power(10, 2))
Module Attributes
In Python, a module is an object of module class, and hence it is characterized by attributes.
Following are the module attributes −
__file__ returns the physical name of the module.
__package__ returns the package to which the module belongs.
__doc__ returns the docstring at the top of the module if any
__dict__ returns the entire scope of the module
__name__ returns the name of the module
Example
Assuming that the following code is saved as mymodule.py
"The docstring of mymodule"
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
Let us check the attributes of mymodule by importing it in the following script −
import mymodule
print ("sum:",sum(10,20))
You can see that sum() function is called within the same script in which it is defined.
C:\Users\user\examples> python mymodule.py
sum: 30
Now let us import this function in another script example.py.
import mymodule
print ("sum:",mymodule.sum(10,20))
It will produce the following output −
C:\Users\user\examples> python example.py
sum: 30
sum: 30
The output "sum:30" appears twice. Once when mymodule module is imported. The executable
statements in imported module are also run. Second output is from the calling script, i.e., example.py
program.
What we want to happen is that when a module is imported, only the function should be imported, its
executable statements should not run. This can be done by checking the value of __name__. If it is
__main__, means it is being run and not imported. Include the executable statements like function calls
conditionally.
Add if statement in mymodule.py as shown −
"The docstring of mymodule"
def sum(x,y):
return x+y
if __name__ == "__main__":
print ("sum:",sum(10,20))
Now if you run example.py program, you will find that the sum:30 output appears only once.
C:\Users\user\examples> python example.py
sum: 30
The reload() Function
Sometimes you may need to reload a module, especially when working with the interactive interpreter
session of Python.
Assume that we have a test module (test.py) with the following function −
def SayHello(name):
print ("Hi {}! How are you?".format(name))
return
We can import the module and call its function from Python prompt as −
>>> import test
>>> test.SayHello("Deepak")
Hi Deepak! How are you?
However, suppose you need to modify the SayHello() function, such as −
def SayHello(name, course):
print ("Hi {}! How are you?".format(name))
print ("Welcome to {} Tutorial by TutorialsPoint".format(course))
return
Even if you edit the test.py file and save it, the function loaded in the memory won't update. You need to
reload it, using reload() function in imp module.
>>> import imp
>>> imp.reload(test)
>>> test.SayHello("Deepak", "Python")
Hi Deepak! How are you?
Welcome to Python Tutorial by TutorialsPoint
Python - Built-in Functions
As of Python 3.11.2 version, there are 71 built-in functions in Pyhthon. The list of built-in functions is
given below −
Sr.No. Function & Description
abs()
1
Returns absolute value of a number
aiter()
2
Returns an asynchronous iterator for an asynchronous iterable
all()
3
Returns true when all elements in iterable is true
anext()
4
Returns the next item from the given asynchronous iterator
any()
5
Checks if any Element of an Iterable is True
ascii()
6
Returns String Containing Printable Representation
bin()
7
Converts integer to binary string
bool()
8
Converts a Value to Boolean
breakpoint()
9
This function drops you into the debugger at the call site and calls sys.breakpointhook()
bytearray()
10
returns array of given byte size
bytes()
11
returns immutable bytes object
callable()
12
Checks if the Object is Callable
chr()
13
Returns a Character (a string) from an Integer
classmethod()
14
Returns class method for given function
compile()
15
Returns a code object
complex()
16
Creates a Complex Number
delattr()
17
Deletes Attribute From the Object
dict()
18
Creates a Dictionary
19 dir()
Tries to Return Attributes of Object
divmod()
20
Returns a Tuple of Quotient and Remainder
enumerate()
21
Returns an Enumerate Object
eval()
22
Runs Code Within Program
exec()
23
Executes Dynamically Created Program
filter()
24
Constructs iterator from elements which are true
float()
25
Returns floating point number from number, string
format()
26
Returns formatted representation of a value
frozenset()
27
Returns immutable frozenset object
getattr()
28
Returns value of named attribute of an object
globals()
29
Returns dictionary of current global symbol table
hasattr()
30
Returns whether object has named attribute
hash()
31
Returns hash value of an object
help()
32
Invokes the built-in Help System
hex()
33
Converts to Integer to Hexadecimal
id()
34
Returns Identify of an Object
input()
35
Reads and returns a line of string
int()
36
Returns integer from a number or string
isinstance()
37
Checks if a Object is an Instance of Class
issubclass()
38
Checks if a Class is Subclass of another Class
iter()
39
Returns an iterator
len()
40
Returns Length of an Object
list()
41
Creates a list in Python
locals()
42
Returns dictionary of a current local symbol table
43 map()
Applies Function and Returns a List
max()
44
Returns the largest item
memoryview()
45
Returns memory view of an argument
min()
46
Returns the smallest value
next()
47
Retrieves next item from the iterator
object()
48
Creates a featureless object
oct()
49
Returns the octal representation of an integer
open()
50
Returns a file object
ord()
51
Returns an integer of the Unicode character
pow()
52
Returns the power of a number
print()
53
Prints the Given Object
property()
54
Returns the property attribute
range()
55
Returns a sequence of integers
repr()
56
Returns a printable representation of the object
reversed()
57
Returns the reversed iterator of a sequence
round()
58
Rounds a number to specified decimals
set()
59
Constructs and returns a set
setattr()
60
Sets the value of an attribute of an object
slice()
61
Returns a slice object
sorted()
62
Returns a sorted list from the given iterable
staticmethod()
63
Transforms a method into a static method
str()
64
Returns the string version of the object
sum()
65
Adds items of an Iterable
super()
66
Returns a proxy object of the base class
67 tuple()
Returns a tuple
type()
68
Returns the type of the object
vars()
69
Returns the __dict__ attribute
zip()
70
Returns an iterator of tuples
__import__()
71
Function called by the import statement
Built-in Mathematical Functions
Following mathematical functions are built into the Python interpreter, hence you don't need to import
them from any module.
Sr.No. Function & Description
abs() function
1
The abs() function returns the absolute value of x, i.e. the positive distance between x and zero.
max() function
2 The max() function returns the largest of its arguments or largest number from the iterable (list or
tuple).
min() function
3 The function min() returns the smallest of its arguments i.e. the value closest to negative infinity, or
smallest number from the iterable (list or tuple)
pow() function
4 The pow() function returns x raised to y. It is equivalent to x**y. The function has third optional
argument mod. If given, it returns (x**y) % mod value
round() Function
5
round() is a built-in function in Python. It returns x rounded to n digits from the decimal point.
sum() function
6 The sum() function returns the sum of all numeric items in any iterable (list or tuple). An optional
start argument is 0 by default. If given, the numbers in the list are added to start value.
Python - Strings
In Python, a string is an immutable sequence of Unicode characters. Each character has a unique numeric
value as per the UNICODE standard. But, the sequence as a whole, doesn't have any numeric value even if
all the characters are digits. To differentiate the string from numbers and other identifiers, the sequence
of characters is included within single, double or triple quotes in its literal representation. Hence, 1234 is
a number (integer) but '1234' is a string.
As long as the same sequence of characters is enclosed, single or double or triple quotes don't matter.
Hence, following string representations are equivalent.
>>> 'Welcome To TutorialsPoint'
'Welcome To TutorialsPoint'
>>> "Welcome To TutorialsPoint"
'Welcome To TutorialsPoint'
>>> '''Welcome To TutorialsPoint'''
'Welcome To TutorialsPoint'
>>> """Welcome To TutorialsPoint"""
'Welcome To TutorialsPoint'
Looking at the above statements, it is clear that, internally Python stores strings as included in single
quotes.
A string in Python is an object of str class. It can be verified with type() function.
var = "Welcome To TutorialsPoint"
print (type(var))
It will produce the following output −
<class 'str'>
You want to embed some text in double quotes as a part of string, the string itself should be put in single
quotes. To embed a single quoted text, string should be written in double quotes.
var = 'Welcome to "Python Tutorial" from TutorialsPoint'
print ("var:", var)
Python allows you to access any individual character from the string by its index. In this case, 0 is the
lower bound and 11 is the upper bound of the string. So, var[0] returns H, var[6] returns P. If the index in
square brackets exceeds the upper bound, Python raises IndexError.
>>> var="HELLO PYTHON"
>>> var[0]
'H'
>>> var[7]
'Y'
>>> var[11]
'N'
>>> var[12]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
One of the unique features of Python sequence types (and therefore a string object) it has a negative
indexing scheme also. In the example above, a positive indexing scheme is used where the index
increments from left to right. In case of negative indexing, the character at the end has -1 index and the
index decrements from right to left, as a result the first character H has -12 index.
print ("var:",var)
print ("var[3:8]:", var[3:8])
It will produce the following output −
var: HELLO PYTHON
var[3:8]: LO PY
Negative indexes can also be used for slicing.
var="HELLO PYTHON"
print ("var:",var)
print ("var[3:8]:", var[3:8])
print ("var[-9:-4]:", var[-9:-4])
It will produce the following output −
var: HELLO PYTHON
var[3:8]: LO PY
var[-9:-4]: LO PY
Both the operands for Python's Slice operator are optional. The first operand defaults to zero, which
means if we do not give the first operand, the slice starts of character at 0th index, i.e. the first character.
It slices the leftmost substring up to "y-1" characters.
var="HELLO PYTHON"
print ("var:",var)
print ("var[0:5]:", var[0:5])
print ("var[:5]:", var[:5])
It will produce the following output −
var: HELLO PYTHON
var[0:5]: HELLO
var[:5]: HELLO
Similarly, y operand is also optional. By default, it is "-1", which means the string will be sliced from the
xth position up to the end of string.
var="HELLO PYTHON"
print ("var:",var)
print ("var[6:12]:", var[6:12])
print ("var[6:]:", var[6:])
It will produce the following output −
var: HELLO PYTHON
var[6:12]: PYTHON
var[6:]: PYTHON
Naturally, if both the operands are not used, the slice will be equal to the original string. That's because
"x" is 0, and "y" is the last index+1 (or -1) by default.
var="HELLO PYTHON"
print ("var:",var)
print ("var[0:12]:", var[0:12])
print ("var[:]:", var[:])
It will produce the following output −
var: HELLO PYTHON
var[0:12]: HELLO PYTHON
var[:]: HELLO PYTHON
The left operand must be smaller than the operand on right, for getting a substring of the original string.
Python doesn't raise any error, if the left operand is greater, bu returns a null string.
var="HELLO PYTHON"
print ("var:",var)
print ("var[-1:7]:", var[-1:7])
print ("var[7:0]:", var[7:0])
It will produce the following output −
var: HELLO PYTHON
var[-1:7]:
var[7:0]:
Slicing returns a new string. You can very well perform string operations like concatenation, or slicing on
the sliced string.
var="HELLO PYTHON"
print ("var:",var)
print ("var[:6][:2]:", var[:6][:2])
var1=var[:6]
print ("slice:", var1)
print ("var1[:2]:", var1[:2])
It will produce the following output −
var: HELLO PYTHON
var[:6][:2]: HE
slice: HELLO
var1[:2]: HE
Python - Modify Strings
In Python, a string (object of str class) is of immutable type. An immutable object is the one which can be
modified in place, one created in the memory. Hence, unlike a list, any character in the sequence cannot
be overwritten, nor can we insert or append characters to it unless we use certain string method that
returns a new string object.
However, we can use one of the following tricks as a workaround to modify a string.
Converting a String to a List
Since both string and list objects are sequences, they are interconvertible. Hence, if we cast a string
object to a list, modify the list either by insert(), append() or remove() methods and convert the list back
to a string, to get back the modified version.
We have a string variable s1 with WORD as its value. With list() built-in function, let us convert it to a l1
list object, and insert a character L at index 3. The we use the join() method in str class to concatenate all
the characters.
s1="WORD"
print ("original string:", s1)
l1=list(s1)
l1.insert(3,"L")
print (l1)
s1=''.join(l1)
print ("Modified string:", s1)
It will produce the following output −
original string: WORD
['W', 'O', 'R', 'L', 'D']
Modified string: WORLD
Using the Array Module
To modify a string, construct an array object. Python standard library includes array module. We can have
an array of Unicode type from a string variable.
import array as ar
s1="WORD"
sar=ar.array('u', s1)
Items in the array have a zero based index. So, we can perform array operations such as append, insert,
remove etc. Let us insert L before the character D
sar.insert(3,"L")
Now, with the help of tounicode() method, get back the modified string
import array as ar
s1="WORD"
print ("original string:", s1)
sar=ar.array('u', s1)
sar.insert(3,"L")
s1=sar.tounicode()
s1="WORD"
print ("original string:", s1)
sio=io.StringIO(s1)
sio.seek(3)
sio.write("LD")
s1=sio.getvalue()
# escape backslash
s=s = 'The \\character is called backslash'
print (s)
# newline
s='Hello\nPython'
print (s)
# Horizontal tab
s='Hello\tPython'
print (s)
# form feed
s= "hello\fworld"
print (s)
# Octal notation
s="\101"
print(s)
# Hexadecimal notation
s="\x41"
print (s)
It will produce the following output −
This string will not include backslashes or newline characters.
The \character is called backslash
Hello 'Python'
Hello "Python"
Helo
Hello
Hello
Python
Hello Python
hello
world
A
A
Python - String Methods
Python's built-in str class defines different methods. They help in manipulating strings. Since string is an
immutable object, these methods return a copy of the original string, performing the respective
processing on it.
The string methods can be classified in following categories −
Case conversion
Alignment
Split and join
Boolean
Find and replace
Formatting
Translate
Python - String Exercises
Example 1
Python program to find number of vowels in a given string.
mystr = "All animals are equal. Some are more equal"
vowels = "aeiou"
count=0
for x in mystr:
if x.lower() in vowels: count+=1
print ("Number of Vowels:", count)
It will produce the following output −
Number of Vowels: 18
Example 2
Python program to convert a string with binary digits to integer.
mystr = '10101'
def strtoint(mystr):
for x in mystr:
if x not in '01': return "Error. String with non-binary characters"
num = int(mystr, 2)
return num
print ("binary:{} integer: {}".format(mystr,strtoint(mystr)))
It will produce the following output −
binary:10101 integer: 21
Change mystr to '10, 101'
binary:10,101 integer: Error. String with non-binary characters
Example 3
Python program to drop all digits from a string.
digits = [str(x) for x in range(10)]
mystr = 'He12llo, Py00th55on!'
chars = []
for x in mystr:
if x not in digits:
chars.append(x)
newstr = ''.join(chars)
print (newstr)
It will produce the following output −
Hello, Python!
Exercise Programs
Python program to sort the characters in a string
Python program to remove duplicate characters from a string
Python program to list unique characters with their count in a string
Python program to find number of words in a string
Python program to remove all non-alphabetic characters from a string
Python - Lists
List is one of the built-in data types in Python. A Python list is a sequence of comma separated items,
enclosed in square brackets [ ]. The items in a Python list need not be of the same data type.
Following are some examples of Python lists −
list1 = ["Rohan", "Physics", 21, 69.75]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]
list4 = [25.50, True, -55, 1+2j]
In Python, a list is a sequence data type. It is an ordered collection of items. Each item in a list has a
unique position index, starting from 0.
A list in Python is similar to an array in C, C++ or Java. However, the major difference is that in C/C++/Java,
the array elements must be of same type. On the other hand, Python lists may have objects of different
data types.
A Python list is mutable. Any item from the list can be accessed using its index, and can be modified. One
or more objects from the list can be removed or added. A list may have same item at more than one
index positions.
Python List Operations
In Python, List is a sequence. Hence, we can concatenate two lists with "+" operator and concatenate
multiple copies of a list with "*" operator. The membership operators "in" and "not in" work with list
object.
Python Expression Results Description
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
3 in [1, 2, 3] True Membership
Python - Access List Items
In Python, a list is a sequence. Each object in the list is accessible with its index. The index starts from 0.
Index or the last item in the list is "length-1". To access the values in a list, use the square brackets for
slicing along with the index or indices to obtain value available at that index.
The slice operator fetches one or more items from the list. Put index on square brackets to retrieve item
at its position.
obj = list1[i]
Example 1
Take a look at the following example −
list1 = ["Rohan", "Physics", 21, 69.75]
list2 = [1, 2, 3, 4, 5]
list1.insert(2, 'Chemistry')
print ("List after appending: ", list1)
list1.insert(-1, 'Pass')
print ("List after appending: ", list1)
Output
Original list ['Rohan', 'Physics', 21, 69.75]
List after appending: ['Rohan', 'Physics', 'Chemistry', 21, 69.75]
List after appending: ['Rohan', 'Physics', 'Chemistry', 21, 'Pass', 69.75]
We know that "-1" index points to the last item in the list. However, note that, the item at index "-1" in
the original list is 69.75. This index is not refreshed after appending 'chemistry'. Hence, 'Pass' is not
inserted at the updated index "-1", but the previous index "-1".
Python - Remove List Items
The list class methods remove() and pop() both can remove an item from a list. The difference between
them is that remove() removes the object given as argument, while pop() removes an item at the given
index.
Using the remove() Method
The following example shows how you can use the remove() method to remove list items −
list1 = ["Rohan", "Physics", 21, 69.75]
print ("Original list: ", list1)
list1.remove("Physics")
print ("List after removing: ", list1)
It will produce the following output −
Original list: ['Rohan', 'Physics', 21, 69.75]
List after removing: ['Rohan', 21, 69.75]
Using the pop() Method
The following example shows how you can use the pop() method to remove list items −
list2 = [25.50, True, -55, 1+2j]
print ("Original list: ", list2)
list2.pop(2)
print ("List after popping: ", list2)
It will produce the following output −
Original list: [25.5, True, -55, (1+2j)]
List after popping: [25.5, True, (1+2j)]
Using the "del" Keyword
Python has the "del" keyword that deletes any Python object from the memory.
Example
We can use "del" to delete an item from a list. Take a look at the following example −
list1 = ["a", "b", "c", "d"]
print ("Original list: ", list1)
del list1[2]
print ("List after deleting: ", list1)
It will produce the following output −
Original list: ['a', 'b', 'c', 'd']
List after deleting: ['a', 'b', 'd']
Example
You can delete a series of consecutive items from a list with the slicing operator. Take a look at the
following example −
list2 = [25.50, True, -55, 1+2j]
print ("List before deleting: ", list2)
del list2[0:2]
print ("List after deleting: ", list2)
It will produce the following output −
List before deleting: [25.5, True, -55, (1+2j)]
List after deleting: [-55, (1+2j)]
Python - Loop Lists
You can traverse the items in a list with Python's for loop construct. The traversal can be done, using list
as an iterator or with the help of index.
Syntax
Python list gives an iterator object. To iterate a list, use the for statement as follows −
for obj in list:
...
...
Example 1
Take a look at the following example −
lst = [25, 12, 10, -21, 10, 100]
for num in lst:
print (num, end = ' ')
Output
25 12 10 -21 10 100
Example 2
To iterate through the items in a list, obtain the range object of integers "0" to "len-1". See the following
example −
lst = [25, 12, 10, -21, 10, 100]
indices = range(len(lst))
for i in indices:
print ("lst[{}]: ".format(i), lst[i])
Output
lst[0]: 25
lst[1]: 12
lst[2]: 10
lst[3]: -21
lst[4]: 10
lst[5]: 100
Python - List Comprehension
List comprehension is a very powerful programming tool. It is similar to set builder notation in
mathematics. It is a concise way to create new list by performing some kind of process on each item on
existing list. List comprehension is considerably faster than processing a list by for loop.
Example 1
Suppose we want to separate each letter in a string and put all non-vowel letters in a list object. We can
do it by a for loop as shown below −
chars=[]
for ch in 'TutorialsPoint':
if ch not in 'aeiou':
chars.append(ch)
print (chars)
The chars list object is displayed as follows −
['T', 't', 'r', 'l', 's', 'P', 'n', 't']
List Comprehension Technique
We can easily get the same result by a list comprehension technique. A general usage of list
comprehension is as follows −
listObj = [x for x in iterable]
Applying this, chars list can be constructed by the following statement −
chars = [ char for char in 'TutorialsPoint' if char not in 'aeiou']
print (chars)
The chars list will be displayed as before −
['T', 't', 'r', 'l', 's', 'P', 'n', 't']
Example 2
The following example uses list comprehension to build a list of squares of numbers between 1 to 10
squares = [x*x for x in range(1,11)]
print (squares)
The squares list object is −
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Nested Loops in List Comprehension
In the following example, all combinations of items from two lists in the form of a tuple are added in a
third list object.
Example 3
list1=[1,2,3]
list2=[4,5,6]
CombLst=[(x,y) for x in list1 for y in list2]
print (CombLst)
It will produce the following output −
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
Condition in List Comprehension
The following statement will create a list of all even numbers between 1 to 20.
Example 4
list1=[x for x in range(1,21) if x%2==0]
print (list1)
It will produce the following output −
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Python - Sort Lists
The sort() method of list class rearranges the items in ascending or descending order with the use of
lexicographical ordering mechanism. The sorting is in-place, in the sense the rearrangement takes place in
the same list object, and that it doesn't return a new object.
Syntax
list1.sort(key, reverse)
Parameters
Key − The function applied to each item in the list. The return value is used to perform sort.
Optional
reverse − Boolean value. If set to True, the sort takes place in descending order. Optional
Return value
This method returns None.
Example 1
Now let's take a look at some examples to understand how we can sort lists in Python −
list1 = ['physics', 'Biology', 'chemistry', 'maths']
print ("list before sort", list1)
list1.sort()
print ("list after sort : ", list1)
for x in L2:
L1.append(x)
list1 = list(tup1)
list1[2]='F'
list1.append('Z')
list1.sort()
print ("updated list", list1)
tup1 = tuple(list1)
print ("Tuple after update", tup1, "id(): ", id(tup1))
It will produce the following output −
Tuple before update ('a', 'b', 'c', 'd') id(): 2295023084192
updated list ['F', 'Z', 'a', 'b', 'd']
Tuple after update ('F', 'Z', 'a', 'b', 'd') id(): 2295021518128
However, note that the id() of tup1 before update and after update are different. It means that a new
tuple object is created and the original tuple object is not modified in-place.
Python - Unpack Tuple Items
The term "unpacking" refers to the process of parsing tuple items in individual variables. In Python, the
parentheses are the default delimiters for a literal representation of sequence object.
Following statements to declare a tuple are identical.
>>> t1 = (x,y)
>>> t1 = x,y
>>> type (t1)
<class 'tuple'>
Example 1
To store tuple items in individual variables, use multiple variables on the left of assignment operator, as
shown in the following example −
tup1 = (10,20,30)
x, y, z = tup1
print ("x: ", x, "y: ", "z: ",z)
It will produce the following output −
x: 10 y: 20 z: 30
That's how the tuple is unpacked in individual variables.
Using to Unpack a T uple
In the above example, the number of variables on the left of assignment operator is equal to the items in
the tuple. What if the number is not equal to the items?
Example 2
If the number of variables is more or less than the length of tuple, Python raises a ValueError.
tup1 = (10,20,30)
x, y = tup1
x, y, p, q = tup1
It will produce the following output −
x, y = tup1
^^^^
ValueError: too many values to unpack (expected 2)
x, y, p, q = tup1
^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 3)
In such a case, the "*" symbol is used for unpacking. Prefix "*" to "y", as shown below −
tup1 = (10,20,30)
x, *y = tup1
print ("x: ", "y: ", y)
It will produce the following output −
x: y: [20, 30]
The first value in tuple is assigned to "x", and rest of items to "y" which becomes a list.
Example 3
In this example, the tuple contains 6 values and variables to be unpacked are 3. We prefix "*" to the
second variable.
tup1 = (10,20,30, 40, 50, 60)
x, *y, z = tup1
print ("x: ",x, "y: ", y, "z: ", z)
It will produce the following output −
x: 10 y: [20, 30, 40, 50] z: 60
Here, values are unpacked in "x" and "z" first, and then the rest of values are assigned to "y" as a list.
Example 4
What if we add "*" to the first variable?
tup1 = (10,20,30, 40, 50, 60)
*x, y, z = tup1
print ("x: ",x, "y: ", y, "z: ", z)
It will produce the following output −
x: [10, 20, 30, 40] y: 50 z: 60
Here again, the tuple is unpacked in such a way that individual variables take up the value first, leaving
the remaining values to the list "x".
Python - Loop Tuples
You can traverse the items in a tuple with Python's for loop construct. The traversal can be done, using
tuple as an iterator or with the help of index.
Syntax
Python tuple gives an iterator object. To iterate a tuple, use the for statement as follows −
for obj in tuple:
...
...
Example 1
The following example shows a simple Python for loop construct −
tup1 = (25, 12, 10, -21, 10, 100)
for num in tup1:
print (num, end = ' ')
It will produce the following output −
25 12 10 -21 10 100
Example 2
To iterate through the items in a tuple, obtain the range object of integers "0" to "len-1".
tup1 = (25, 12, 10, -21, 10, 100)
indices = range(len(tup1))
for i in indices:
print ("tup1[{}]: ".format(i), tup1[i])
It will produce the following output −
tup1[0]: 25
tup1 [1]: 12
tup1 [2]: 10
tup1 [3]: -21
tup1 [4]: 10
tup1 [5]: 100
Python - Join Tuples
In Python, a Tuple is classified as a sequence type object. It is a collection of items, which may be of
different data types, with each item having a positional index starting with 0. Although this definition also
applies to a list, there are two major differences in list and tuple. First, while items are placed in square
brackets in case of List (example: [10,20,30,40]), the tuple is formed by putting the items in parentheses
(example: (10,20,30,40)).
In Python, a Tuple is an immutable object. Hence, it is not possible to modify the contents of a tuple one it
is formed in the memory.
However, you can use different ways to join two Python tuples.
Example 1
All the sequence type objects support concatenation operator, with which two lists can be joined.
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
T3 = T1+T2
print ("Joined Tuple:", T3)
It will produce the following output −
Joined Tuple: (10, 20, 30, 40, 'one', 'two', 'three', 'four')
Example 2
You can also use the augmented concatenation operator with the "+=" symbol to append T2 to T1
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
T1+=T2
print ("Joined Tuple:", T1)
Example 3
The same result can be obtained by using the extend() method. Here, we need cast the two tuple objects
to lists, extend so as to add elements from one list to another, and convert the joined list back to a tuple.
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
L1 = list(T1)
L2 = list(T2)
L1.extend(L2)
T1 = tuple(L1)
print ("Joined Tuple:", T1)
Example 4
Python's built-in sum() function also helps in concatenating tuples. We use an expression
sum((t1, t2), ())
The elements of the first tuple are appended to an empty tuple first, and then elements from second
tuple are appended and returns a new tuple that is concatenation of the two.
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
T3 = sum((T1, T2), ())
print ("Joined Tuple:", T3)
Example 5
A slightly complex approach for merging two tuples is using list comprehension, as following code shows
−
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
L1, L2 = list(T1), list(T2)
L3 = [y for x in [L1, L2] for y in x]
T3 = tuple(L3)
print ("Joined Tuple:", T3)
Example 6
You can run a for loop on the items in second loop, convert each item in a single item tuple and
concatenate it to first tuple with the "+=" operator
T1 = (10,20,30,40)
T2 = ('one', 'two', 'three', 'four')
for t in T2:
T1+=(t,)
print (T1)
Python - Tuple Methods
Since a tuple in Python is immutable, the tuple class doesn't define methods for adding or removing
items. The tuple class defines only two methods.
Sr.No Methods & Description
tuple.count(obj)
1
Returns count of how many times obj occurs in tuple
2 tuple.index(obj)
Returns the lowest index in tuple that obj appears
Finding the Index of a Tuple Item
The index() method of tuple class returns the index of first occurrence of the given item.
Syntax
tuple.index(obj)
Return value
The index() method returns an integer, representing the index of the first occurrence of "obj".
Example
Take a look at the following example −
tup1 = (25, 12, 10, -21, 10, 100)
print ("Tup1:", tup1)
x = tup1.index(10)
print ("First index of 10:", x)
It will produce the following output −
Tup1: (25, 12, 10, -21, 10, 100)
First index of 10: 2
Counting Tuple Items
The count() method in tuple class returns the number of times a given object occurs in the tuple.
Syntax
tuple.count(obj)
Return Value
Number of occurrence of the object. The count() method returns an integer.
Example
tup1 = (10, 20, 45, 10, 30, 10, 55)
print ("Tup1:", tup1)
c = tup1.count(10)
print ("count of 10:", c)
It will produce the following output −
Tup1: (10, 20, 45, 10, 30, 10, 55)
count of 10: 3
Example
Even if the items in the tuple contain expressions, they will be evaluated to obtain the count.
Tup1 = (10, 20/80, 0.25, 10/40, 30, 10, 55)
print ("Tup1:", tup1)
c = tup1.count(0.25)
print ("count of 10:", c)
It will produce the following output −
Tup1: (10, 0.25, 0.25, 0.25, 30, 10, 55)
count of 10: 3
Python Tuple Exercises
Example 1
Python program to find unique numbers in a given tuple −
T1 = (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)
T2 = ()
for x in T1:
if x not in T2:
T2+=(x,)
print ("original tuple:", T1)
print ("Unique numbers:", T2)
It will produce the following output −
original tuple: (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)
Unique numbers: (1, 9, 6, 3, 4, 5, 2, 7, 8)
Example 2
Python program to find sum of all numbers in a tuple −
T1 = (1, 9, 1, 6, 3, 4)
ttl = 0
for x in T1:
ttl+=x
ttl = sum(T1)
print ("Sum of all numbers sum() function:", ttl)
It will produce the following output −
Sum of all numbers Using loop: 24
Sum of all numbers sum() function: 24
Example 3
Python program to create a tuple of 5 random integers −
import random
t1 = ()
for i in range(5):
x = random.randint(0, 100)
t1+=(x,)
print (t1)
It will produce the following output −
(64, 21, 68, 6, 12)
Exercise Programs
Python program to remove all duplicates numbers from a list.
Python program to sort a tuple of strings on the number of alphabets in each word.
Python program to prepare a tuple of non-numeric items from a given tuple.
Python program to create a tuple of integers representing each character in a string
Python program to find numbers common in two tuples.
Python - Sets
A set is one of the built-in data types in Python. In mathematics, set is a collection of distinct objects. Set
data type is Python's implementation of a set. Objects in a set can be of any data type.
Set in Python also a collection data type such as list or tuple. However, it is not an ordered collection, i.e.,
items in a set or not accessible by its positional index. A set object is a collection of one or more
immutable objects enclosed within curly brackets {}.
Example 1
Some examples of set objects are given below −
s1 = {"Rohan", "Physics", 21, 69.75}
s2 = {1, 2, 3, 4, 5}
s3 = {"a", "b", "c", "d"}
s4 = {25.50, True, -55, 1+2j}
print (s1)
print (s2)
print (s3)
print (s4)
It will produce the following output −
{'Physics', 21, 'Rohan', 69.75}
{1, 2, 3, 4, 5}
{'a', 'd', 'c', 'b'}
{25.5, -55, True, (1+2j)}
The above result shows that the order of objects in the assignment is not necessarily retained in the set
object. This is because Python optimizes the structure of set for set operations.
In addition to the literal representation of set (keeping the items inside curly brackets), Python's built-in
set() function also constructs set object.
set() Function
set() is one of the built-in functions. It takes any sequence object (list, tuple or string) as argument and
returns a set object
Syntax
Obj = set(sequence)
Parameters
sequence − An object of list, tuple or str type
Return value
The set() function returns a set object from the sequence, discarding the repeated elements in it.
Example 2
L1 = ["Rohan", "Physics", 21, 69.75]
s1 = set(L1)
T1 = (1, 2, 3, 4, 5)
s2 = set(T1)
string = "TutorialsPoint"
s3 = set(string)
print (s1)
print (s2)
print (s3)
It will produce the following output −
{'Rohan', 69.75, 21, 'Physics'}
{1, 2, 3, 4, 5}
{'u', 'a', 'o', 'n', 'r', 's', 'T', 'P', 'i', 't', 'l'}
Example 3
Set is a collection of distinct objects. Even if you repeat an object in the collection, only one copy is
retained in it.
s2 = {1, 2, 3, 4, 5, 3,0, 1, 9}
s3 = {"a", "b", "c", "d", "b", "e", "a"}
print (s2)
print (s3)
It will produce the following output −
{0, 1, 2, 3, 4, 5, 9}
{'a', 'b', 'd', 'c', 'e'}
Example 4
Only immutable objects can be used to form a set object. Any number type, string and tuple is allowed,
but you cannot put a list or a dictionary in a set.
s1 = {1, 2, [3, 4, 5], 3,0, 1, 9}
print (s1)
s2 = {"Rohan", {"phy":50}}
print (s2)
It will produce the following output −
s1 = {1, 2, [3, 4, 5], 3,0, 1, 9}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
s2 = {"Rohan", {"phy":50}}
^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'dict'
Python raises TypeError with a message unhashable types 'list' or 'dict'. Hashing generates a unique
number for an immutable item that enables quick search inside computer's memory. Python has built-in
hash() function. This function is not supported by list or dictionary.
Even though mutable objects are not stored in a set, set itself is a mutable object. Python has a special
operators to work with sets, and there are different methods in set class to perform add, remove, update
operations on elements of a set object.
Python - Access Set Items
Since set is not a sequence data type, its items cannot be accessed individually as they do not have a
positional index (as in list or tuple). Set items do not have a key either (as in dictionary) to access. You can
only traverse the set items using a for loop.
Example 1
langs = {"C", "C++", "Java", "Python"}
for lang in langs:
print (lang)
It will produce the following output −
Python
C
C++
Java
Example 2
Python's membership operators let you check if a certain item is available in the set. Take a look at the
following example −
langs = {"C", "C++", "Java", "Python"}
print ("PHP" in langs)
print ("Java" in langs)
It will produce the following output −
False
True
Python - Add Set Items
Even if a set holds together only immutable objects, set itself is mutable. We can add new items in it with
any of the following ways −
add() Method
The add() method in set class adds a new element. If the element is already present in the set, there is no
change in the set.
Syntax
set.add(obj)
Parameters
obj − an object of any immutable type.
Example
Take a look at the following example −
lang1 = {"C", "C++", "Java", "Python"}
lang1.add("Golang")
print (lang1)
It will produce the following output −
{'Python', 'C', 'Golang', 'C++', 'Java'}
update() Method
The update() method of set class includes the items of the set given as argument. If elements in the other
set has one or more items that are already existing, they will not be included.
Syntax
set.update(obj)
Parameters
obj − a set or a sequence object (list, tuple, string)
Example
The following example shows how the update() method works −
lang1 = {"C", "C++", "Java", "Python"}
lang2 = {"PHP", "C#", "Perl"}
lang1.update(lang2)
print (lang1)
It will produce the following output −
{'Python', 'Java', 'C', 'C#', 'PHP', 'Perl', 'C++'}
Example
The update() method also accepts any sequence object as argument. Here, a tuple is the argument for
update() method.
lang1 = {"C", "C++", "Java", "Python"}
lang2 = ("PHP", "C#", "Perl")
lang1.update(lang2)
print (lang1)
It will produce the following output −
{'Java', 'Perl', 'Python', 'C++', 'C#', 'C', 'PHP'}
Example
In this example, a set is constructed from a string, and another string is used as argument for update()
method.
set1 = set("Hello")
set1.update("World")
print (set1)
It will produce the following output −
{'H', 'r', 'o', 'd', 'W', 'l', 'e'}
union() Method
The union() method of set class also combines the unique items from two sets, but it returns a new set
object.
Syntax
set.union(obj)
Parameters
obj − a set or a sequence object (list, tuple, string)
Return value
The union() method returns a set object
Example
The following example shows how the union() method works −
lang1 = {"C", "C++", "Java", "Python"}
lang2 = {"PHP", "C#", "Perl"}
lang3 = lang1.union(lang2)
print (lang3)
It will produce the following output −
{'C#', 'Java', 'Perl', 'C++', 'PHP', 'Python', 'C'}
Example
If a sequence object is given as argument to union() method, Python automatically converts it to a set first
and then performs union.
lang1 = {"C", "C++", "Java", "Python"}
lang2 = ["PHP", "C#", "Perl"]
lang3 = lang1.union(lang2)
print (lang3)
It will produce the following output −
{'PHP', 'C#', 'Python', 'C', 'Java', 'C++', 'Perl'}
Example
In this example, a set is constructed from a string, and another string is used as argument for union()
method.
set1 = set("Hello")
set2 = set1.union("World")
print (set2)
It will produce the following output −
{'e', 'H', 'r', 'd', 'W', 'o', 'l'}
Python - Remove Set Items
Python's set class provides different methods to remove one or more items from a set object.
remove() Method
The remove() method removes the given item from the set collection, if it is present in it. However, if it is
not present, it raises KeyError.
Syntax
set.remove(obj)
Parameters
obj − an immutable object
Example
lang1 = {"C", "C++", "Java", "Python"}
print ("Set before removing: ", lang1)
lang1.remove("Java")
print ("Set after removing: ", lang1)
lang1.remove("PHP")
It will produce the following output −
Set before removing: {'C', 'C++', 'Python', 'Java'}
Set after removing: {'C', 'C++', 'Python'}
lang1.remove("PHP")
KeyError: 'PHP'
discard() Method
The discard() method in set class is similar to remove() method. The only difference is, it doesn't raise
error even if the object to be removed is not already present in the set collection.
Syntax
set.discard(obj)
Parameters
obj − An immutable object
Example
lang1 = {"C", "C++", "Java", "Python"}
print ("Set before discarding C++: ", lang1)
lang1.discard("C++")
print ("Set after discarding C++: ", lang1)
print ("Set before discarding PHP: ", lang1)
lang1.discard("PHP")
print ("Set after discarding PHP: ", lang1)
It will produce the following output −
Set before discarding C++: {'Java', 'C++', 'Python', 'C'}
Set after discarding C++: {'Java', 'Python', 'C'}
Set before discarding PHP: {'Java', 'Python', 'C'}
Set after discarding PHP: {'Java', 'Python', 'C'}
pop() Method
The pop() method in set class removes an arbitrary item from the set collection. The removed item is
returned by the method. Popping from an empty set results in KeyError.
Syntax
obj = set.pop()
Return value
The pop() method returns the object removed from set.
Example
lang1 = {"C", "C++"}
print ("Set before popping: ", lang1)
obj = lang1.pop()
print ("object popped: ", obj)
print ("Set after popping: ", lang1)
obj = lang1.pop()
obj = lang1.pop()
It will produce the following output −
Set before popping: {'C++', 'C'}
object popped: C++
Set after popping: {'C'}
Traceback (most recent call last):
obj = lang1.pop()
^^^^^^^^^^^
KeyError: 'pop from an empty set'
At the time of call to pop() for third time, the set is empty, hence KeyError is raised.
clear() Method
The clear() method in set class removes all the items in a set object, leaving an empty set.
Syntax
set.clear()
Example
lang1 = {"C", "C++", "Java", "Python"}
print (lang1)
print ("After clear() method")
lang1.clear()
print (lang1)
It will produce the following output −
{'Java', 'C++', 'Python', 'C'}
After clear() method
set()
difference_update() Method
The difference_update() method in set class updates the set by removing items that are common
between itself and another set given as argument.
Syntax
set.difference_update(obj)
Parameters
obj − a set object
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1 before running difference_update: ", s1)
s1.difference_update(s2)
print ("s1 after running difference_update: ", s1)
It will produce the following output −
s1 before running difference_update: {1, 2, 3, 4, 5}
s1 after running difference_update: {1, 2, 3}
set()
difference() Method
The difference() method is similar to difference_update() method, except that it returns a new set object
that contains the difference of the two existing sets.
Syntax
set.difference(obj)
Parameters
obj − a set object
Return value
The difference() method returns a new set with items remaining after removing those in obj.
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.difference(s2)
print ("s3 = s1-s2: ", s3)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s3 = s1-s2: {1, 2, 3}
intersection_update() Method
As a result of intersection_update() method, the set object retains only those items which are common in
itself and other set object given as argument.
Syntax
set.intersection_update(obj)
Parameters
obj − a set object
Return value
The intersection_update() method removes uncommon items and keeps only those items which are
common to itself and obj.
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s1.intersection_update(s2)
print ("a1 after intersection: ", s1)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 after intersection: {4, 5}
intersection() Method
The intersection() method in set class is similar to its intersection_update() method, except that it returns
a new set object that consists of items common to existing sets.
Syntax
set.intersection(obj)
Parameters
obj − a set object
Return value
The intersection() method returns a set object, retaining only those items common in itself and obj.
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.intersection(s2)
print ("s3 = s1 & s2: ", s3)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s3 = s1 & s2: {4, 5}
symmetric_difference_update() method
The symmetric difference between two sets is the collection of all the uncommon items, rejecting the
common elements. The symmetric_difference_update() method updates a set with symmetric difference
between itself and the set given as argument.
Syntax
set.symmetric_difference_update(obj)
Parameters
obj − a set object
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s1.symmetric_difference_update(s2)
print ("s1 after running symmetric difference ", s1)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 after running symmetric difference {1, 2, 3, 6, 7, 8}
symmetric_difference() Method
The symmetric_difference() method in set class is similar to symmetric_difference_update() method,
except that it returns a new set object that holds all the items from two sets minus the common items.
Syntax
set.symmetric_difference(obj)
Parameters
obj − a set object
Return value
The symmetric_difference() method returns a new set that contains only those items not common
between the two set objects.
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.symmetric_difference(s2)
print ("s1 = s1^s2 ", s3)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 = s1^s2 {1, 2, 3, 6, 7, 8}
Python - Loop Sets
A set in Python is not a sequence, nor is it a mapping type class. Hence, the objects in a set cannot be
traversed with index or key. However, you can traverse each item in a set using a for loop.
Example 1
The following example shows how you can traverse through a set using a for loop −
langs = {"C", "C++", "Java", "Python"}
for lang in langs:
print (lang)
It will produce the following output −
C
Python
C++
Java
Example 2
The following example shows how you can run a for loop over the elements of one set, and use the add()
method of set class to add in another set.
s1={1,2,3,4,5}
s2={4,5,6,7,8}
for x in s2:
s1.add(x)
print (s1)
It will produce the following output −
{1, 2, 3, 4, 5, 6, 7, 8}
Python - Join Sets
In Python, a Set is an ordered collection of items. The items may be of different types. However, an item
in the set must be an immutable object. It means, we can only include numbers, string and tuples in a set
and not lists. Python's set class has different provisions to join set objects.
Using the "|" Operator
The "|" symbol (pipe) is defined as the union operator. It performs the A∪B operation and returns a set of
items in A, B or both. Set doesn't allow duplicate items.
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1|s2
print (s3)
It will produce the following output −
{1, 2, 3, 4, 5, 6, 7, 8}
Using the union() Method
The set class has union() method that performs the same operation as | operator. It returns a set object
that holds all items in both sets, discarding duplicates.
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1.union(s2)
print (s3)
Using the update() Method
The update() method also joins the two sets, as the union() method. However it doen't return a new set
object. Instead, the elements of second set are added in first, duplicates not allowed.
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s1.update(s2)
print (s1)
Using the unpacking Operator
In Python, the "*" symbol is used as unpacking operator. The unpacking operator internally assign each
element in a collection to a separate variable.
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = {*s1, *s2}
print (s3)
Python - Copy Sets
The copy() method in set class creates a shallow copy of a set object.
Syntax
set.copy()
Return Value
The copy() method returns a new set which is a shallow copy of existing set.
Example
lang1 = {"C", "C++", "Java", "Python"}
print ("lang1: ", lang1, "id(lang1): ", id(lang1))
lang2 = lang1.copy()
print ("lang2: ", lang2, "id(lang2): ", id(lang2))
lang1.add("PHP")
print ("After updating lang1")
print ("lang1: ", lang1, "id(lang1): ", id(lang1))
print ("lang2: ", lang2, "id(lang2): ", id(lang2))
Output
lang1: {'Python', 'Java', 'C', 'C++'} id(lang1): 2451578196864
lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312
After updating lang1
lang1: {'Python', 'C', 'C++', 'PHP', 'Java'} id(lang1): 2451578196864
lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312
Python - Set Operators
In the Set Theory of Mathematics, the union, intersection, difference and symmetric difference
operations are defined. Python implements them with following operators −
Union Operator (|)
The union of two sets is a set containing all elements that are in A or in B or both. For example,
{1,2}∪{2,3}={1,2,3}
The following diagram illustrates the union of two sets.
Python uses the "|" symbol as a union operator. The following example uses the "|" operator and returns
the union of two sets.
Example
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s3 = s1 | s2
print ("Union of s1 and s2: ", s3)
It will produce the following output −
Union of s1 and s2: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection Operator (&)
The intersection of two sets AA and BB, denoted by A∩B, consists of all elements that are both in A and B.
For example,
{1,2}∩{2,3}={2}
The following diagram illustrates intersection of two sets.
Python uses the "&" symbol as an intersection operator. Following example uses & operator and returns
intersection of two sets.
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s3 = s1 & s2
print ("Intersection of s1 and s2: ", s3)
It will produce the following output −
Intersection of s1 and s2: {4, 5}
Difference Operator (-)
The difference (subtraction) is defined as follows. The set A−B consists of elements that are in A but not in
B. For example,
If A={1,2,3} and B={3,5}, then A−B={1,2}
The following diagram illustrates difference of two sets −
d1["b"] = 100
print ("id:", id(d1), "dict: ",d1)
print ("id:", id(d2), "dict: ",d2)
Output
id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33}
id: 2215278891200 dict: {'a': 11, 'b': 22, 'c': 33}
id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33}
id: 2215278891200 dict: {'a': 11, 'b': 100, 'c': 33}
To avoid this, and make a shallow copy of a dictionary, use the copy() method instead of assignment.
Example 2
d1 = {"a":11, "b":22, "c":33}
d2 = d1.copy()
print ("id:", id(d1), "dict: ",d1)
print ("id:", id(d2), "dict: ",d2)
d1["b"] = 100
print ("id:", id(d1), "dict: ",d1)
print ("id:", id(d2), "dict: ",d2)
Output
When "d1" is updated, "d2" will not change now because "d2" is the copy of dictionary object, not merely
a reference.
id: 1586671734976 dict: {'a': 11, 'b': 22, 'c': 33}
id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33}
id: 1586671734976 dict: {'a': 11, 'b': 100, 'c': 33}
id: 1586673973632 dict: {'a': 11, 'b': 22, 'c': 33}
Python - Nested Dictionaries
A Python dictionary is said to have a nested structure if value of one or more keys is another dictionary. A
nested dictionary is usually employed to store a complex data structure.
The following code snippet represents a nested dictionary:
marklist = {
"Mahesh" : {"Phy" : 60, "maths" : 70},
"Madhavi" : {"phy" : 75, "maths" : 68},
"Mitchell" : {"phy" : 67, "maths" : 71}
}
Example 1
You can also constitute a for loop to traverse nested dictionary, as in the previous section.
marklist = {
"Mahesh" : {"Phy" : 60, "maths" : 70},
"Madhavi" : {"phy" : 75, "maths" : 68},
"Mitchell" : {"phy" : 67, "maths" : 71}
}
for k,v in marklist.items():
print (k, ":", v)
It will produce the following output −
Mahesh : {'Phy': 60, 'maths': 70}
Madhavi : {'phy': 75, 'maths': 68}
Mitchell : {'phy': 67, 'maths': 71}
Example 2
It is possible to access value from an inner dictionary with [] notation or get() method.
print (marklist.get("Madhavi")['maths'])
obj=marklist['Mahesh']
print (obj.get('Phy'))
print (marklist['Mitchell'].get('maths'))
It will produce the following output −
68
60
71
Python - Dictionary Methods
A dictionary in Python is an object of the built-in dict class, which defines the following methods −
Sr.No. Method and Description
dict.clear()
1
Removes all elements of dictionary dict.
dict.copy()
2
Returns a shallow copy of dictionary dict.
dict.fromkeys()
3
Create a new dictionary with keys from seq and values set to value.
dict.get(key, default=None)
4
For key key, returns value or default if key not in dictionary.
dict.has_key(key)
5
Returns true if a given key is available in the dictionary, otherwise it returns a false.
dict.items()
6
Returns a list of dict's (key, value) tuple pairs.
dict.keys()
7
Returns list of dictionary dict's keys.
dict.pop()
8
Removes the element with specified key from the collection
dict.popitem()
9
Removes the last inserted key-value pair
dict.setdefault(key, default=None)
10
Similar to get(), but will set dict[key]=default if key is not already in dict.
dict.update(dict2)
11
Adds dictionary dict2's key-values pairs to dict.
dict.values()
12
Returns list of dictionary dict's values.
Python - Dictionary Exercises
Example 1
Python program to create a new dictionary by extracting the keys from a given dictionary.
d1 = {"one":11, "two":22, "three":33, "four":44, "five":55}
keys = ['two', 'five']
d2={}
for k in keys:
d2[k]=d1[k]
print (d2)
It will produce the following output −
{'two': 22, 'five': 55}
Example 2
Python program to convert a dictionary to list of (k,v) tuples.
d1 = {"one":11, "two":22, "three":33, "four":44, "five":55}
L1 = list(d1.items())
print (L1)
It will produce the following output −
[('one', 11), ('two', 22), ('three', 33), ('four', 44), ('five', 55)]
Example 3
Python program to remove keys with same values in a dictionary.
d1 = {"one":"eleven", "2":2, "three":3, "11":"eleven", "four":44, "two":2}
vals = list(d1.values())#all values
uvals = [v for v in vals if vals.count(v)==1]#unique values
d2 = {}
for k,v in d1.items():
if v in uvals:
d = {k:v}
d2.update(d)
print ("dict with unique value:",d2)
It will produce the following output −
dict with unique value: {'three': 3, 'four': 44}
Exercise Programs
Python program to sort list of dictionaries by values
Python program to extract dictionary with each key having non-numeric value from a given
dictionary.
Python program to build a dictionary from list of two item (k,v) tuples.
Python program to merge two dictionary objects, using unpack operator.
Python - Arrays
Python's standard data types list, tuple and string are sequences. A sequence object is an ordered
collection of items. Each item is characterized by incrementing index starting with zero. Moreover, items
in a sequence need not be of same type. In other words, a list or tuple may consist of items of different
data type.
This feature is different from the concept of an array in C or C++. In C/C++, an array is also an indexed
collection of items, but the items must be of similar data type. In C/C++, you have an array of integers or
floats, or strings, but you cannot have an array with some elements of integer type and some of different
type. A C/C++ array is therefore a homogenous collection of data types.
Python's standard library has array module. The array class in it allows you to construct an array of three
basic types, integer, float and Unicode characters.
Syntax
The syntax of creating array is −
import array
obj = array.array(typecode[, initializer])
Parameters
typecode − The typecode character used to create the array.
initializer − array initialized from the optional value, which must be a list, a bytes-like object, or
iterable over elements of the appropriate type.
Return type
The array() constructor returns an object of array.array class
Example
import array as arr
#is equivalent to
import sys
sys.stdout.write(name)
Any object that interacts with input and output steam is called File object. Python's built-in function
open() returns a file object.
The open() Function
This function creates a file object, which would be utilized to call other support methods associated with
it.
Syntax
file object = open(file_name [, access_mode][, buffering])
Here are the parameter details −
file_name − The file_name argument is a string value that contains the name of the file that you
want to access.
access_mode − The access_mode determines the mode in which the file has to be opened, i.e.,
read, write, append, etc. A complete list of possible values is given below in the table. This is an
optional parameter and the default file access mode is read (r).
buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line
buffering is performed while accessing a file. If you specify the buffering value as an integer
greater than 1, then buffering action is performed with the indicated buffer size. If negative, the
buffer size is the system default (default behavior).
File Opening Modes
Following are the file opening modes −
Sr.No. Modes & Description
r
1 Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.
rb
2 Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
This is the default mode.
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+
4 Opens a file for both reading and writing in binary format. The file pointer placed at the beginning
of the file.
5 w
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a
new file for writing.
b
6
Opens the file in binary mode
t
7
Opens the file in text mode (default)
+
8
open file for updating (reading and writing)
wb
9 Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.
w+
10 Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does
not exist, creates a new file for reading and writing.
wb+
11 Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
a
12 Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is
in the append mode. If the file does not exist, it creates a new file for writing.
ab
13 Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file
14
exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading
and writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the
15
file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new
file for reading and writing.
x
16
open for exclusive creation, failing if the file already exists
Once a file is opened and you have one file object, you can get various information related to that file.
Example
# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)
print ("Closed or not: ", fo.closed)
print ("Opening mode: ", fo.mode)
fo.close()
It will produce the following output −
Name of the file: foo.txt
Closed or not: False
Opening mode: wb
Python - Write to File
To write data to a file in Python, you need to open a file. Any object that interacts with input and output
steam is called File object. Python's built-in function open() returns a file object.
fileObject = open(file_name [, access_mode][, buffering])
After you obtain the file object with the open() function, you can use the write() method to write any
string to the file represented by the file object. It is important to note that Python strings can have binary
data and not just text.
The write() method does not add a newline character ('\n') to the end of the string.
Syntax
fileObject.write(string)
Here, passed parameter is the content to be written into the opened file.
Example
# Open a file
fo = open("foo.txt", "w")
fo.write( "Python is a great language.\nYeah its great!!\n")
Attributes
Name, class, subjects, marks, etc., of student
Name, designation, department, salary, etc., of employee
Invoice number, customer, product code and name, price and quantity, etc., in an invoice
Registration number, owner, company, brand, horsepower, speed, etc., of car
Each attribute will have a value associated with it. Attribute is equivalent to data.
Behavior
Processing attributes associated with an object.
Compute percentage of student's marks
Calculate incentives payable to employee
Apply GST to invoice value
Measure speed of car
Behavior is equivalent to function. In real life, attributes and behavior are not independent of each other,
rather they co-exist.
The most important feature of object-oriented approach is defining attributes and their functionality as a
single unit called class. It serves as a blueprint for all objects having similar attributes and behavior.
In OOP, class defines what are the attributes its object has, and how is its behavior. Object, on the other
hand, is an instance of the class.
Object-oriented programming paradigm is characterized by the following principles −
Class
A user-defined prototype for an object that defines a set of attributes that characterize any object of the
class. The attributes are data members (class variables and instance variables) and methods, accessed via
dot notation.
Object
An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an
instance of the class Circle. A unique instance of a data structure that is defined by its class. An object
comprises both data members (class variables and instance variables) and methods.
Encapsulation
Data members of class are available for processing to functions defined within the class only. Functions of
class on the other hand are accessible from outside class context. So object data is hidden from
environment that is external to class. Class function (also called method) encapsulates object data so that
unwarranted access to it is prevented.
Inheritance
A software modelling approach of OOP enables extending capability of an existing class to build new class
instead of building from scratch. In OOP terminology, existing class is called base or parent class, while
new class is called child or sub class.
Child class inherits data definitions and methods from parent class. This facilitates reuse of features
already available. Child class can add few more definitions or redefine a base class function.
Polymorphism
Polymorphism is a Greek word meaning having multiple forms. In OOP, polymorphism occurs when each
sub class provides its own implementation of an abstract method in base class.
Python - Object and Classes
Python is a highly object-oriented language. In Python, each and every element in a Python program is an
object of one or the other class. A number, string, list, dictionary etc. used in a program they are objects
of corresponding built-in classes.
Example
num=20
print (type(num))
num1=55.50
print (type(num1))
s="TutorialsPoint"
print (type(s))
dct={'a':1,'b':2,'c':3}
print (type(dct))
def SayHello():
print ("Hello World")
return
print (type(SayHello))
When you execute this code, it will produce the following output −
<class 'int'>
<class 'float'>
<class 'str'>
<class 'dict'>
<class 'function'>
In Python, the Object class is the base or parent class for all the classes, built-in as well as user defined.
The class keyword is used to define a new class. The name of the class immediately follows the keyword
class followed by a colon as follows −
class ClassName:
'Optional class documentation string'
class_suite
The class has a documentation string, which can be accessed via ClassName.__doc__.
The class_suite consists of all the component statements defining class members, data attributes
and functions.
Example
class Employee(object):
'Common base class for all employees'
pass
Any class in Python is a subclass of object class, hence object is written in parentheses. However, later
versions of Python don't require object to be put in parentheses.
class Employee:
'Common base class for all employees'
pass
To define an object of this class, use the following syntax −
e1 = Employee()
Python - Class Attributes
Every Python class keeps the following built-in attributes and they can be accessed using dot operator like
any other attribute −
__dict__ − Dictionary containing the class's namespace.
__doc__ − Class documentation string or none, if undefined.
__name__ − Class name.
__module__ − Module name in which the class is defined. This attribute is "__main__" in
interactive mode.
__bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in
the base class list.
For the above class, let us try to access all these attributes −
class Employee:
def __init__(self, name="Bhavana", age=24):
self.name = name
self.age = age
def displayEmployee(self):
print ("Name : ", self.name, ", age: ", self.age)
e1 = Employee("Bhavana", 24)
e2 = Employee("Rajesh", 26)
e3 = Employee("John", 27)
Output
We have declared three objects. Every time, the empCount increments by 1.
Name: Bhavana Age: 24
Employee Number: 1
Name: Rajesh Age: 26
Employee Number: 2
Name: John Age: 27
Employee Number: 3
Python - Class Methods
An instance method accesses the instance variables of the calling object because it takes the reference to
the calling object. But it can also access the class variable as it is common to all the objects.
Python has a built-in function classmethod() which transforms an instance method to a class method
which can be called with the reference to the class only and not the object.
Syntax
classmethod(instance_method)
Example
In the Employee class, define a showcount() instance method with the "self" argument (reference to
calling object). It prints the value of empCount. Next, transform the method to class method counter()
that can be accessed through the class reference.
class Employee:
empCount = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
Employee.empCount += 1
def showcount(self):
print (self.empCount)
counter=classmethod(showcount)
e1 = Employee("Bhavana", 24)
e2 = Employee("Rajesh", 26)
e3 = Employee("John", 27)
e1.showcount()
Employee.counter()
Output
Call showcount() with object and call count() with class, both show the value of employee count.
3
3
Using @classmethod() decorator is the prescribed way to define a class method as it is more convenient
than first declaring an instance method and then transforming to a class method.
@classmethod
def showcount(cls):
print (cls.empCount)
Employee.showcount()
The class method acts as an alternate constructor. Define a newemployee() class method with arguments
required to construct a new object. It returns the constructed object, something that the __init__()
method does.
@classmethod
def showcount(cls):
print (cls.empCount)
return
@classmethod
def newemployee(cls, name, age):
return cls(name, age)
e1 = Employee("Bhavana", 24)
e2 = Employee("Rajesh", 26)
e3 = Employee("John", 27)
e4 = Employee.newemployee("Anil", 21)
Employee.showcount()
There are four Employee objects now.
Python - Static Methods
is that the static method doesn't have a mandatory argument like reference to the object − self or
reference to the class − cls. Python's standard library fimction staticmethod() returns a static method.
In the Employee class below, a method is converted into a static method. This static method can now be
called by its object or reference of class itself.
class Employee:
empCount = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
Employee.empCount += 1
#@staticmethod
def showcount():
print (Employee.empCount)
return
counter = staticmethod(showcount)
e1 = Employee("Bhavana", 24)
e2 = Employee("Rajesh", 26)
e3 = Employee("John", 27)
e1.counter()
Employee.counter()
Python also has @staticmethod decorator that conveniently returns a static method.
@staticmethod
def showcount():
print (Employee.empCount)
e1.showcount()
Employee.showcount()
Python - Constructors
In object-oriented programming, an object of a class is characterized by one or more instance variables or
attributes, whose values are unique to each object. For example, if the Employee class has an instance
attribute as name. Each of its objects e1 and e2 may have different value for the name variable.
A constructor is an instance method in a class, that is automatically called whenever a new object of the
class is declared. The constructor' role is to assign value to instance variables as soon as the object is
declared.
Python uses a special method called __init__() to initialize the instance variables for the object, as soon as
it is declared.
The __init__() method acts as a constructor. It needs a mandatory argument self, which the reference to
the object.
def __init__(self):
#initialize instance variables
The __init__() method as well as any instance method in a class has a mandatory parameter, self.
However, you can give any name to the first parameter, not necessarily self.
Let us define the constructor in Employee class to initialize name and age as instance variables. We can
then access these attributes of its object.
Example
class Employee:
'Common base class for all employees'
def __init__(self):
self.name = "Bhavana"
self.age = 24
e1 = Employee()
print ("Name: {}".format(e1.name))
print ("age: {}".format(e1.age))
It will produce the following output −
Name: Bhavana
age: 24
Parameterized Constructor
For the above Employee class, each object we declare will have same value for its instance variables name
and age. To declare objects with varying attributes instead of the default, define arguments for the
__init__() method. (A method is nothing but a function defined inside a class.)
Example
In this example, the __init__() constructor has two formal arguments. We declare Employee objects with
different values −
class Employee:
'Common base class for all employees'
def __init__(self, name, age):
self.name = name
self.age = age
e1 = Employee("Bhavana", 24)
e2 = Employee("Bharat", 25)
e1 = Employee()
e2 = Employee("Bharat", 25)
e1 = Employee()
e2 = Employee("Bharat", 25)
e1.displayEmployee()
e2.displayEmployee()
It will produce the following output −
Name : Bhavana , age: 24
Name : Bharat , age: 25
You can add, remove, or modify attributes of classes and objects at any time −
emp1.salary = 7000 # Add a 'salary' attribute.
emp1.name = 'xyz' # Modify 'name' attribute.
del emp1.salary # Delete 'salary' attribute.
Instead of using the normal statements to access attributes, you can use the following functions −
The getattr(obj, name[, default]) − to access the attribute of object.
The hasattr(obj,name) − to check if an attribute exists or not.
The setattr(obj,name,value) − to set an attribute. If attribute does not exist, then it would be
created.
The delattr(obj, name) − to delete an attribute.
print (hasattr(e1, 'salary')) # Returns true if 'salary' attribute exists
print (getattr(e1, 'name')) # Returns value of 'name' attribute
setattr(e1, 'salary', 7000) # Set attribute 'salary' at 8
delattr(e1, 'age') # Delete attribute 'age'
It will produce the following output −
False
Bhavana
Python - Access Modifiers
The languages such as C++ and Java, use access modifiers to restrict access to class members (i.e.,
variables and methods). These languages have keywords public, protected, and private to specify the type
of access.
A class member is said to be public if it can be accessed from anywhere in the program. Private members
are allowed to be accessed from within the class only.
Usually, methods are defined as public and instance variable are private. This arrangement of
private instance variables and public methods ensures implementation of principle of
encapsulation.
Protected members are accessible from within the class as well as by classes derived from that
class.
Unlike these languages, Python has no provision to specify the type of access that a class member may
have. By default, all the variables and methods in a class are public.
Example
Here, we have Employee class with instance variables name and age. An object of this class has these two
attributes. They can be directly accessed from outside the class, because they are public.
class Employee:
'Common base class for all employees'
def __init__(self, name="Bhavana", age=24):
self.name = name
self.age = age
e1 = Employee()
e2 = Employee("Bharat", 25)
print (e1.name)
print (e1._salary)
print (e1.__age)
When you run this code, it will produce the following output −
Bhavana
10000
Traceback (most recent call last):
File "C:\Users\user\example.py", line 14, in <module>
print (e1.__age)
^^^^^^^^
AttributeError: 'Employee' object has no attribute '__age'
Python displays AttributeError because __age is private, and not available for use outside the class.
Name Mangling
Python doesn't block access to private data, it just leaves for the wisdom of the programmer, not to write
any code that access it from outside the class. You can still access the private members by Python's name
mangling technique.
Name mangling is the process of changing name of a member with double underscore to the form
object._class__variable. If so required, it can still be accessed from outside the class, but the practice
should be refrained.
In our example, the private instance variable "__name" is mangled by changing it to the format
obj._class__privatevar
So, to access the value of "__age" instance variable of "e1" object, change it to "e1._Employee__age".
Change the print() statement in the above program to −
print (e1._Employee__age)
It now prints 24, the age of e1.
Python Property Object
Python's standard library has a built-in property() function. It returns a property object. It acts as an
interface to the instance variables of a Python class.
The encapsulation principle of object-oriented programming requires that the instance variables should
have a restricted private access. Python doesn't have efficient mechanism for the purpose. The property()
function provides an alternative.
The property() function uses the getter, setter and delete methods defined in a class to define a property
object for the class.
Syntax
property(fget=None, fset=None, fdel=None, doc=None)
Parameters
fget − an instance method that retrieves value of an instance variable.
fset − an instance method that assigns value to an instance variable.
fdel − an instance method that removes an instance variable
fdoc − Documentation string for the property.
The function uses getter and setter methods to return the property object.
Getters and Setter Methods
A getter method retrieves the value of an instance variable, usually named as get_varname, whereas the
setter method assigns value to an instance variable − named as set_varname.
Let us define getter methods get_name() and get_age(), and setters set_name() and set_age() in the
Employee class.
Example
class Employee:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_name(self, name):
self.__name = name
return
def set_age(self, age):
self.__age=age
e1=Employee("Bhavana", 24)
print ("Name:", e1.get_name(), "age:",
e1.get_age())
e1.set_name("Archana")
e1.set_age(21)
print ("Name:", e1.get_name(), "age:", e1.get_age())
It will produce the following output −
Name: Bhavana age: 24
Name: Archana age: 21
The getter and setter methods can retrieve or assign value to instance variables. The property() function
uses them to add property objects as class attributes.
The name property is defined as −
name = property(get_name, set_name, "name")
Similarly, you can add the age property −
age = property(get_age, set_age, "age")
The advantage of the property object is that you can use to retrieve the value of its associated instance
variable, as well as assign value.
For example,
print (e1.name) displays value of e1.__name
e1.name = "Archana" assigns value to e1.__age
Example
The complete program with property objects and their use is given below −
class Employee:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_name(self, name):
self.__name = name
return
def set_age(self, age):
self.__age=age
return
name = property(get_name, set_name, "name")
age = property(get_age, set_age, "age")
e1=Employee("Bhavana", 24)
print ("Name:", e1.name, "age:", e1.age)
e1.name = "Archana"
e1.age = 23
print ("Name:", e1.name, "age:", e1.age)
It will produce the following output −
Name: Bhavana age: 24
Name: Archana age: 23
Python - Inheritance
Inheritance is one of the most important features of Object-oriented programming methodology. It is
most often used in software development process using many languages such as Java, PHP, Python, etc.
Instead of starting from scratch, you can create a class by deriving it from a pre-existing class by listing the
parent class in parentheses after the new class name.
Instead of starting from scratch, you can create a class by deriving it from a pre-existing class by listing the
parent class in parentheses after the new class name.
If you have to design a new class whose most of the attributes are already well defined in an existing
class, then why redefine them? Inheritance allows capabilities of existing class to be reused and if
required extended to design new class.
Inheritance comes into picture when a new class possesses 'IS A' relationship with an existing class. Car IS
a vehicle. Bus IS a vehicle; Bike IS also a vehicle. Vehicle here is the parent class, whereas car, bus and bike
are the child classes.
Syntax
Derived classes are declared much like their parent class; however, a list of base classes to inherit from is
given after the class name −
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string'
class_suite
Example
class Parent: # define parent class
def __init__(self):
self.attr = 100
print ("Calling parent constructor")
def parentMethod(self):
print ('Calling parent method')
def set_attr(self, attr):
self.attr = attr
def get_attr(self):
print ("Parent attribute :", self.attr)
def childMethod(self):
print ('Calling child method')
class parent2:
#statements
class div_mod(division,modulus):
def __init__(self, a,b):
self.n=a
self.d=b
def div_and_mod(self):
divval=division.divide(self)
modval=modulus.mod_divide(self)
return (divval, modval)
The child class has a new method div_and_mod() which internally calls the divide() and mod_divide()
methods from its inherited classes to return the division and mod values.
x=div_mod(10,3)
print ("division:",x.divide())
print ("mod_division:",x.mod_divide())
print ("divmod:",x.div_and_mod())
Output
division: 3.3333333333333335
mod_division: 1
divmod: (3.3333333333333335, 1)
Method Resolution Order (MRO)
The term "method resolution order" is related to multiple inheritance in Python. In Python, inheritance
may be spread over more than one levels. Let us say A is the parent of B, and B the parent for C. The class
C can override the inherited method or its object may invoke it as defined in its parent. So, how does
Python find the appropriate method to call.
Each Python has a mro() method that returns the hierarchical order that Python uses to resolve the
method to be called. The resolution order is from bottom of inheritance order to top.
In our previous example, the div_mod class inherits division and modulus classes. So, the mro method
returns the order as follows −
[<class '__main__.div_mod'>, <class '__main__.division'>, <class '__main__.modulus'>, <class 'object'>]
Python - Polymorphism
The term "polymorphism" refers to a function or method taking different form in different contexts. Since
Python is a dynamically typed language, Polymorphism in Python is very easily implemented.
If a method in a parent class is overridden with different business logic in its different child classes, the
base class method is a polymorphic method.
Example
As an example of polymorphism given below, we have shape which is an abstract class. It is used as
parent by two classes circle and rectangle. Both classes overrideparent's draw() method in different ways.
from abc import ABC, abstractmethod
class shape(ABC):
@abstractmethod
def draw(self):
"Abstract method"
return
class circle(shape):
def draw(self):
super().draw()
print ("Draw a circle")
return
class rectangle(shape):
def draw(self):
super().draw()
print ("Draw a rectangle")
return
class SalesOfficer(Employee):
def __init__(self,nm, sal, inc):
super().__init__(nm,sal)
self.incnt=inc
def getSalary(self):
return self.salary+self.incnt
e1=Employee("Rajesh", 9000)
print ("Total salary for {} is Rs {}".format(e1.getName(),e1.getSalary()))
s1=SalesOfficer('Kiran', 10000, 1000)
print ("Total salary for {} is Rs {}".format(s1.getName(),s1.getSalary()))
When you execute this code, it will produce the following output −
Total salary for Rajesh is Rs 9000
Total salary for Kiran is Rs 11000
Base Overridable Methods
The following table lists some generic functionality of the object class, which is the parent class for all
Python classes. You can override these methods in your own class −
Sr.No Method, Description & Sample Call
__init__ ( self [,args...] )
1 Constructor (with any optional arguments)
Sample Call : obj = className(args)
2 __del__( self )
Destructor, deletes an object
Sample Call : del obj
__repr__( self )
3 Evaluatable string representation
Sample Call : repr(obj)
__str__( self )
4 Printable string representation
Sample Call : str(obj)
Python - Method Overloading
Method overloading is an important feature of object-oriented programming. Java, C++, C# languages
support method overloading, but in Python it is not possible to perform method overloading.
When you have a class with method of one name defined more than one but with different argument
types and/or return type, it is a case of method overloading. Python doesn't support this mechanism as
the following code shows −
Example
class example:
def add(self, a, b):
x = a+b
return x
def add(self, a, b, c):
x = a+b+c
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))
Output
The first call to add() method with three arguments is successful. However, calling add() method with two
arguments as defined in the class fails.
60
Traceback (most recent call last):
File "C:\Users\user\example.py", line 12, in <module>
print (obj.add(10,20))
^^^^^^^^^^^^^^
TypeError: example.add() missing 1 required positional argument: 'c'
The output tells you that Python considers only the latest definition of add() method, discarding the
earlier definitions.
To simulate method overloading, we can use a workaround by defining default value to method
arguments as None, so that it can be used with one, two or three arguments.
Example
class example:
def add(self, a = None, b = None, c = None):
x=0
if a !=None and b != None and c != None:
x = a+b+c
elif a !=None and b != None and c == None:
x = a+b
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))
It will produce the following output −
60
30
With this workaround, we are able to incorporate method overloading in Python class.
Python's standard library doesn't have any other provision for implementing method overloading.
However, we can use dispatch function from a third party module named MultipleDispatch for this
purpose.
First, you need to install the Multipledispatch module.
pip install multipledispatch
This module has a @dispatch decorator. It takes the number of arguments to be passed to the method to
be overloaded. Define multiple copies of add() method with @dispatch decorator as below −
Example
from multipledispatch import dispatch
class example:
@dispatch(int, int)
def add(self, a, b):
x = a+b
return x
@dispatch(int, int, int)
def add(self, a, b, c):
x = a+b+c
return x
obj = example()
print (obj.add(10,20,30))
print (obj.add(10,20))
Output
When you execute this code, it will produce the following output −
60
30
Python - Dynamic Binding
In object-oriented programming, the concept of dynamic binding is closely related to polymorphism. In
Python, dynamic binding is the process of resolving a method or attribute at runtime, instead of at
compile time.
According to the polymorphism feature, different objects respond differently to the same method call
based on their individual implementations. This behavior is achieved through method overriding, where a
subclass provides its own implementation of a method defined in its superclass.
The Python interpreter determines which is the appropriate method or attribute to invoke by based on
the object's type or class hierarchy at runtime. This means that the specific method or attribute to be
called is determined dynamically, based on the actual type of the object.
Example
The following example illustrates dynamic binding in Python −
class shape:
def draw(self):
print ("draw method")
return
class circle(shape):
def draw(self):
print ("Draw a circle")
return
class rectangle(shape):
def draw(self):
print ("Draw a rectangle")
return
class rectangle:
def draw(self):
print ("Draw a rectangle")
return
class area:
def area(self):
print ("calculate area")
return
def duck_function(obj):
obj.draw()
class concreteclass(democlass):
def method1(self):
super().method1()
return
obj = concreteclass()
obj.method1()
obj.method2()
Output
When you execute this code, it will produce the following output −
abstract method
concrete method
Python - Encapsulation
The principle of Encapsulation is one of the main pillars on which the object-oriented programming
paradigm is based. Python takes a different approach towards the implementation of encapsulation.
We know that a class is a user-defined prototype for an object. It defines a set of data members and
methods, capable of processing the data. According to principle of data encapsulation, the data members
that describe an object are hidden from environment that is external to class. They are available for
processing to methods defined within the class only. Methods themselves on the other hand are
accessible from outside class context. Hence object data is said to be encapsulated by the methods. The
result of such encapsulation is that any unwarranted access to the object data is prevented.
Languages such as C++ and Java use access modifiers to restrict access to class members (i.e., variables
and methods). These languages have keywords public, protected, and private to specify the type of
access.
A class member is said to be public if it can be accessed from anywhere in the program. Private members
are allowed to be accessed from within the class only. Usually, methods are defined as public and
instance variable are private. This arrangement of private instance variables and public methods ensures
the implementation of encapsulation.
Unlike these languages, Python has no provision to specify the type of access that a class member may
have. By default, all the variables and methods in a Python class are public, as is demonstrated by the
following example.
Example 1
Here, we have an Employee class with instance variables, name and age. An object of this class has these
two attributes. They can be directly accessed from outside the class, because they are public.
class Student:
def __init__(self, name="Rajaram", marks=50):
self.name = name
self.marks = marks
s1 = Student()
s2 = Student("Bharat", 25)
s1 = Student()
s2 = Student("Bharat", 25)
s1.studentdata()
s2.studentdata()
print ("Name: {} marks: {}".format(s1.__name, s2.__marks))
print ("Name: {} marks: {}".format(s2.__name, __s2.marks))
When you run this code, it will produce the following output −
Name: Rajaram marks: 50
Name: Bharat marks: 25
Traceback (most recent call last):
File "C:\Python311\hello.py", line 14, in <module>
print ("Name: {} marks: {}".format(s1.__name, s2.__marks))
AttributeError: 'Student' object has no attribute '__name'
The above output makes it clear that the instance variables name and age, although they can be accessed
by a method declared inside the class (the studentdata() method), but since the double underscores
prefix makes the variables private, and hence accessing them outside the class is disallowed, raising
Attribute error.
Python doesn't block access to private data entirely. It just leaves it for the wisdom of the programmer,
not to write any code that access it from outside the class. You can still access the private members by
Python's name mangling technique.
Name mangling is the process of changing name of a member with double underscore to the form
object._class__variable. If so required, it can still be accessed from outside the class, but the practice
should be refrained.
In our example, the private instance variable "__name" is mangled by changing it to the format
obj._class__privatevar
So, to access the value of "__marks" instance variable of "s1" object, change it to "s1._Student__marks".
Change the print() statement in the above program to −
print (s1._Student__marks)
It now prints 50, the marks of s1.
Hence, we can conclude that Python doesn't implement encapsulation exactly as per the theory of object
oriented programming. It adapts a more mature approach towards it by prescribing a name convention,
and letting the programmer to use name mangling if it is really required to have access to private data in
the public scope.
Python - Interfaces
In software engineering, an interface is a software architectural pattern. An interface is like a class but its
methods just have prototype signature definition without any body to implement. The recommended
functionality needs to be implemented by a concrete class.
In languages like Java, there is interface keyword which makes it easy to define an interface. Python
doesn't have it or any similar keyword. Hence the same ABC class and @abstractmethod decorator is
used as done in an abstract class.
An abstract class and interface appear similar in Python. The only difference in two is that the abstract
class may have some non-abstract methods, while all methods in interface must be abstract, and the
implementing class must override all the abstract methods.
Example
from abc import ABC, abstractmethod
class demoInterface(ABC):
@abstractmethod
def method1(self):
print ("Abstract method1")
return
@abstractmethod
def method2(self):
print ("Abstract method1")
return
The above interface has two abstract methods. As in abstract class, we cannot instantiate an interface.
obj = demoInterface()
^^^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class demoInterface with abstract methods method1, method2
Let us provide a class that implements both the abstract methods. If doesn't contain implementations of
all abstract methods, Python shows following error −
obj = concreteclass()
^^^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class concreteclass with abstract method method2
The following class implements both methods −
class concreteclass(demoInterface):
def method1(self):
print ("This is method1")
return
def method2(self):
print ("This is method2")
return
obj = concreteclass()
obj.method1()
obj.method2()
Output
When you execute this code, it will produce the following output −
This is method1
This is method2
Python - Packages
In Python, module is a Python script with .py extension and contains objects such as classes, functions etc.
Packages in Python extend the concept of modular approach further. Package is a folder containing one
or more module files; additionally a special file "__init__.py" file which may be empty but may contain the
package list.
Let us create a Python package with the name mypackage. Follow the steps given below −
Create an outer folder to hold the contents of mypackage. Let its name be packagedemo.
Inside it, create another folder mypackage. This will be the Python package we are going to
construct.Two Python modules areafunctions.py and mathfunctions.py will be created inside
mypackage.
Create an empty "__.init__.py" file inside mypackage folder.
Inside the outer folder, we shall later on store a Python script example.py to test our package.
The file/folder structure should be as shown below −
Using your favorite code editor, save the following two Python modules in mypackage folder.
# mathfunctions.py
def sum(x,y):
val = x+y
return val
def average(x,y):
val = (x+y)/2
return val
def power(x,y):
val = x**y
return val
Create another Python script −
# areafunctions.py
def rectangle(w,h):
area = w*h
return area
def circle(r):
import math
area = math.pi*math.pow(r,2)
return area
Let us now test the myexample package with the help of a Python script above this package folder. Refer
to the folder structure above.
#example.py
from mypackage.areafunctions import rectangle
print ("Area :", rectangle(10,20))
s1 = student()
s1.show()
When you execute this code, it will produce the following output −
Name: Ashish
Subjects: Phy Che
It is quite possible to declare an object of outer class independently, and make it call its own display()
method.
sub = student().subjects().display()
It will list out the subjects.
Python - Anonymous Class and Objects
Python's built-in type() function returns the class that an object belongs to. In Python, a class, both a
built-in class or a user-defined class are objects of type class.
Example
class myclass:
def __init__(self):
self.myvar=10
return
obj = myclass()
def __new__(cls):
if cls._instance is None:
print('Creating the object')
cls._instance = super(SingletonClass, cls).__new__(cls)
return cls._instance
obj1 = SingletonClass()
print(obj1)
obj2 = SingletonClass()
print(obj2)
This is how the above code works −
When an instance of a Python class declared, it internally calls the __new__() method. We override the
__new__() method that is called internally by Python when you create an object of a class. It checks
whether our instance variable is None. If the instance variable is None, it creates a new object and call the
super() method and returns the instance variable that contains the object of this class.
If multiple objects are created, it becomes clear that the object is only created the first time; after that,
the same object instance is returned.
Creating the object
<__main__.SingletonClass object at 0x000002A5293A6B50>
<__main__.SingletonClass object at 0x000002A5293A6B50>
Python - Wrapper Classes
A function in Python is a first-order object. A function can have another function as its argument and
wrap another function definition inside it. This helps in modifying a function without actually changing it.
Such functions are called decorators.
This feature is also available for wrapping a class. This technique is used to manage the class after it is
instantiated by wrapping its logic inside a decorator.
Example
def decorator_function(Wrapped):
class Wrapper:
def __init__(self,x):
self.wrap = Wrapped(x)
def print_name(self):
return self.wrap.name
return Wrapper
@decorator_function
class Wrapped:
def __init__(self,x):
self.name = x
obj = Wrapped('TutorialsPoint')
print(obj.print_name())
Here, Wrapped is the name of the class to be wrapped. It is passed as argument to a function. Inside the
function, we have a Wrapper class, modify its behavior with the attributes of the passed class, and return
the modified class. The returned class is instantiated and its method can now be called.
When you execute this code, it will produce the following output −
TutorialsPoint
Python - Enums
The term 'enumeration' refers to the process of assigning fixed constant values to a set of strings, so that
each string can be identified by the value bound to it. Python's standard library offers the enum module.
The Enum class included in enum module is used as the parent class to define enumeration of a set of
identifiers − conventionally written in upper case.
Example 1
from enum import Enum
class subjects(Enum):
ENGLISH = 1
MATHS = 2
SCIENCE = 3
SANSKRIT = 4
In the above code, "subjects" is the enumeration. It has different enumeration members, e.g.,
subjects.MATHS. Each member is assigned a value.
Each member is ab object of the enumeration class subjects, and has name and value attributes.
obj = subjects.MATHS
print (type(obj), obj.value)
It results in following output −
<enum 'subjects'> 2
Example 2
Value bound to the enum member needn't always be an integer, it can be a string as well. See the
following example −
from enum import Enum
class subjects(Enum):
ENGLISH = "E"
MATHS = "M"
GEOGRAPHY = "G"
SANSKRIT = "S"
obj = subjects.SANSKRIT
print (type(obj), obj.name, obj.value)
It will produce the following output −
<enum 'subjects'> SANSKRIT S
Example 3
You can iterate through the enum members in the order of their appearance in the definition, with the
help of a for loop −
for sub in subjects:
print (sub.name, sub.value)
It will produce the following output −
ENGLISH E
MATHS M
GEOGRAPHY G
SANSKRIT S
The enum member can be accessed with the unique value assigned to it, or by its name attribute. Hence,
subjects("E") as well as subjects["ENGLISH"] returns subjects.ENGLISH member.
Example 4
An enum class cannot have same member appearing twice, however, more than one members may be
assigned same value. To ensure that each member has a unique value bound to it, use the @unique
decorator.
from enum import Enum, unique
@unique
class subjects(Enum):
ENGLISH = 1
MATHS = 2
GEOGRAPHY = 3
SANSKRIT = 2
This will raise an exception as follows −
@unique
^^^^^^
raise ValueError('duplicate values found in %r: %s' %
ValueError: duplicate values found in <enum 'subjects'>: SANSKRIT -> MATHS
The Enum class is a callable class, hence you can use the following alternative method of defining
enumeration −
from enum import Enum
subjects = Enum("subjects", "ENGLISH MATHS SCIENCE SANSKRIT")
The Enum constructor uses two arguments here. First one is the name of enumeration. Second argument
is a string consisting of enumeration member symbolic names, separated by a whitespace.
Python - Reflection
In object-oriented programming, reflection refers to the ability to extract information about any object in
use. You can get to know the type of object, is it a subclass of any other class, what are its attributes and
much more. Python's standard library has a number of functions that reflect on different properties of an
object. Reflection is also sometimes called introspect.
Let us take a review of reflection functions.
The type() Function
We have used this function many times. It tells you which class does an object belong to.
Example
Following statements print the respective class of different built-in data type objects
print (type(10))
print (type(2.56))
print (type(2+3j))
print (type("Hello World"))
print (type([1,2,3]))
print (type({1:'one', 2:'two'}))
Here, you will get the following output −
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'list'>
<class 'dict'>
Let us verify the type of an object of a user-defined class −
class test:
pass
obj = test()
print (type(obj))
It will produce the following output −
<class '__main__.test'>
The isinstance() Function
This is another built-in function in Python which ascertains if an object is an instance of the given class
Syntax
isinstance(obj, class)
This function always returns a Boolean value, true if the object is indeed belongs to the given class and
false if not.
Example
Following statements return True −
print (isinstance(10, int))
print (isinstance(2.56, float))
print (isinstance(2+3j, complex))
print (isinstance("Hello World", str))
In contrast, these statements print False.
print (isinstance([1,2,3], tuple))
print (isinstance({1:'one', 2:'two'}, set))
It will produce the following output −
True
True
True
True
False
False
You can also perform check with a user defined class
class test:
pass
obj = test()
print (isinstance(obj, test))
It will produce the following output −
True
In Python, even the classes are objects. All classes are objects of object class. It can be verified by
following code −
class test:
pass
print (callable("Hello"))
print (callable(abs))
print (callable(list.clear([1,2])))
print (callable(test))
A string object is not callable. But abs is a function which is callable. The pop method of list is callable, but
clear() is actually call to the function and not a function object, hence not a callable
It will produce the following output −
False
True
True
False
True
A class instance is callable if it has a __call__() method. In the example below, the test class includes
__call__() method. Hence, its object can be used as if we are calling function. Hence, object of a class with
__call__() function is a callable.
class test:
def __init__(self):
pass
def __call__(self):
print ("Hello")
obj = test()
obj()
print ("obj is callable?", callable(obj))
It will produce the following output −
Hello
obj is callable? True
The getattr() Function
The getattr() built-in function retrieves the value of the named attribute of object.
Example
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print (getattr(obj, "name"))
It will produce the following output −
Manav
The setattr() Function
The setattr() built-in function adds a new attribute to the object and assigns it a value. It can also change
the value of an existing attribute.
In the example below, the object of test class has a single attribute − name. We use setattr to add age
attribute and to modify the value of name attribute.
class test:
def __init__(self):
self.name = "Manav"
obj = test()
setattr(obj, "age", 20)
setattr(obj, "name", "Madhav")
print (obj.name, obj.age)
It will produce the following output −
Madhav 20
The hasattr() Function
This built-in function returns True if the given attribute is available to the object argument, and false if
not. We use the same test class and check if it has a certain attribute or not.
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print (hasattr(obj, "age"))
print (hasattr(obj, "name"))
It will produce the following output −
False
True
The dir() Function
If his built in function called without an argument, return the names in the current scope. Fpr any object
as argument, it returns a list the attributes of the given object, and of attributes reachable from it.
For a module object − the function returns the module's attributes.
For a class object − the function returns its attributes, and recursively the attributes of its bases.
For any other object − its attributes, its class's attributes, and recursively the attributes of its
class's base classes.
Example
print ("dir(int):", dir(int))
It will produce the following output −
dir(int): ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__',
'__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__',
'__getattribute__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__index__', '__init__',
'__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__',
'__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__',
'__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__',
'__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__',
'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator',
'real', 'to_bytes']
Example
print ("dir(dict):", dir(dict))
It will produce the following output −
dir(dict): ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__',
'__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys',
'pop', 'popitem', 'setdefault', 'update', 'values']
Example
class test:
def __init__(self):
self.name = "Manav"
obj = test()
print ("dir(obj):", dir(obj))
It will produce the following output −
dir(obj): ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'name']
Python - Syntax Errors
Generally, three types of errors appear in a computer program: Syntax errors, logical errors and runtime
errors. Syntax errors are the most common type of errors one faces while writing a program, whether you
are new to programming or an experienced programmer. Syntax errors are basically related to the rules
of grammar of a certain language.
Syntax errors occur whenever the rules laid down by the language are not followed. In Python, there are
well defined rules for giving name to an identifier, that is, a variable, a function, a class, a module or any
Python object. Similarly, Python keywords should be used as per the syntax defined. Whenever these
rules are not followed, Python interpreter displays a syntax error message.
A simple example of declaring a variable in Python interactive shell is given below.
>>> name="Python
File "<stdin>", line 1
name="Python
^
SyntaxError: unterminated string literal (detected at line 1)
Python interpreter displays syntax error along with a certain explanatory message. In the above example,
because the quotation symbol is not closed, the Syntax error occurs.
Similarly, Python requires each function name should be followed by parantheses inside which the
function arguments should be given.
In the following example, we get a syntax error −
>>> print "Hello"
File "<stdin>", line 1
print "Hello"
^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
The reason can be understood from the error message, that the print() function is missing parentheses.
There are many popular IDEs for Python programming. Most of them use colorized syntax highlighting,
which makes it easy to visually identify the error.
One such IDE is VS Code. While entering an instruction, the syntax errors are suitably highlighted.
The error is highlighted. If you put the cursor there, VS Code tells more about the error. If you still go
ahead and execute the code, error messages appear in the command terminal.
Syntax errors are easy to identify and rectify. The IDE such as VS Code makes it easy. However,
sometimes, your code doesn't show any syntax errors, but still the output of the program is not what you
anticipate. Such errors are logical errors. They are hard to detect, as the error lies in the logic used in the
code. You learn by experience how to correct logical errors. VS Code and other IDEs have features such as
watches and breakpoints to trap these errors.
Third type of error is a runtime error also called exception. There is no syntax error nor there is any logical
error in your program. Most of the times, the program gives desired output, but in some specific
situations you get abnormal behaviour of the program, such as the program abnormally terminates or
gives some absurd result.
The factors causing exceptions are generally external to the program. For example incorrect input, type
conversion or malfunction IO device etc.
What is Exception?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of
the program's instructions. In general, when a Python script encounters a situation that it cannot cope
with, it raises an exception. An exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it
terminates and quits.
Python's standard library defines standard exception classes. As with other Python classes, Exceptions are
also subclasses of Object class. Following is the object hierarchy of Python's Exceptions.
object
BaseException
Exception
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
AssertionError
AttributeError
BufferError
EOFError
ImportError
ModuleNotFoundError
LookupError
IndexError
KeyError
MemoryError
NameError
OSError
ReferenceError
RuntimeError
StopAsyncIteration
StopIteration
SyntaxError
Python - Exceptions Handling
If you have some suspicious code that may raise an exception, you can defend your program by placing
the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block
of code which handles the problem as elegantly as possible.
The try: block contains statements which are susceptible for exception
If exception occurs, the program jumps to the except: block.
If no exception in the try: block, the except: block is skipped.
Syntax
Here is the simple syntax of try...except...else blocks −
try:
You do your operations here
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
Here are few important points about the above-mentioned syntax −
A single try statement can have multiple except statements. This is useful when the try block
contains statements that may throw different types of exceptions.
You can also provide a generic except clause, which handles any exception.
After the except clause(s), you can include an else clause. The code in the else block executes if
the code in the try: block does not raise an exception.
The else block is a good place for code that does not need the try: block's protection.
Example
This example opens a file, writes content in the file and comes out gracefully because there is no problem
at all.
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print ("Error: can\'t find file or read data")
else:
print ("Written content in the file successfully")
fh.close()
It will produce the following output −
Written content in the file successfully
However, change the mode parameter in open() function to "w". If the testfile is not already present, the
program encounters IOError in except block, and prints following error message −
Error: can't find file or read data
Python - The try-except Block
You can also use the except statement with no exceptions defined as follows −
try:
You do your operations here
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.
This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-except
statement is not considered a good programming practice though, because it catches all exceptions but
does not make the programmer identify the root cause of the problem that may occur.
You can also use the same except statement to handle multiple exceptions as follows −
try:
You do your operations here
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
Python - The try-finally Block
You can use a finally: block along with a try: block. The finally: block is a place to put any code that must
execute, whether the try-block raised an exception or not.
The syntax of the try-finally statement is this −
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
Note − You can provide except clause(s), or a finally clause, but not both. You cannot use else clause as
well along with a finally clause.
Example
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print ("Error: can\'t find file or read data")
fh.close()
If you do not have permission to open the file in writing mode, then it will produce the following output −
Error: can't find file or read data
The same example can be written more cleanly as follows −
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print ("Going to close the file")
fh.close()
except IOError:
print ("Error: can\'t find file or read data")
When an exception is thrown in the try block, the execution immediately passes to the finally block. After
all the statements in the finally block are executed, the exception is raised again and is handled in the
except statements if present in the next higher layer of the try-except statement.
Exception with Arguments
An exception can have an argument, which is a value that gives additional information about the problem.
The contents of the argument vary by exception. You capture an exception's argument by supplying a
variable in the except clause as follows −
try:
You do your operations here
......................
except ExceptionType as Argument:
You can print value of Argument here...
If you write the code to handle a single exception, you can have a variable follow the name of the
exception in the except statement. If you are trapping multiple exceptions, you can have a variable follow
the tuple of the exception.
This variable receives the value of the exception mostly containing the cause of the exception. The
variable can receive a single value or multiple values in the form of a tuple. This tuple usually contains the
error string, the error number, and an error location.
Example
Following is an example for a single exception −
# Define a function here.
def temp_convert(var):
try:
return int(var)
except ValueError as Argument:
print("The argument does not contain numbers\n",Argument)
# Call above function here.
temp_convert("xyz")
It will produce the following output −
The argument does not contain numbers
invalid literal for int() with base 10: 'xyz'
Python - Raising Exceptions
You can raise exceptions in several ways by using the raise statement. The general syntax for the raise
statement is as follows −
Syntax
raise [Exception [, args [, traceback]]]
Here, Exception is the type of exception (for example, NameError) and argument is a value for the
exception argument. The argument is optional; if not supplied, the exception argument is None.
The final argument, traceback, is also optional (and rarely used in practice), and if present, is the
traceback object used for the exception.
Example
An exception can be a string, a class or an object. Most of the exceptions that the Python core raises are
classes, with an argument that is an instance of the class. Defining new exceptions is quite easy and can
be done as follows −
def functionName( level ):
if level <1:
raise Exception(level)
# The code below to this would not be executed
# if we raise the exception
return level
Note − In order to catch an exception, an "except" clause must refer to the same exception thrown either
as a class object or a simple string. For example, to capture the above exception, we must write the
except clause as follows −
try:
Business Logic here...
except Exception as e:
Exception handling here using e.args...
else:
Rest of the code here...
The following example illustrates the use of raising an exception −
def functionName( level ):
if level <1:
raise Exception(level)
# The code below to this would not be executed
# if we raise the exception
return level
try:
l=functionName(-10)
print ("level=",l)
except Exception as e:
print ("error in level argument",e.args[0])
This will produce the following output −
error in level argument -10
Python - Exception Chaining
Exception chaining is a technique of handling exceptions by re-throwing a caught exception after
wrapping it inside a new exception. The original exception is saved as a property (such as cause) of the
new exception.
During the handling of one exception 'A', it is possible that another exception 'B' may occur. It is useful to
know about both exceptions in order to debug the problem. Sometimes it is useful for an exception
handler to deliberately re-raise an exception, either to provide extra information or to translate an
exception to another type.
In Python 3.x, it is possible to implement exception chaining. If there is any unhandled exception inside an
except section, it will have the exception being handled attached to it and included in the error message.
Example
In the following code snippet, trying to open a non-existent file raises FileNotFoundError. It is detected by
the except block. While handling another exception is raised.
try:
open("nofile.txt")
except OSError:
raise RuntimeError("unable to handle error")
It will produce the following output −
Traceback (most recent call last):
File "/home/cg/root/64afcad39c651/main.py", line 2, in <module>
open("nofile.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'nofile.txt'
The above exception was the direct cause of the following exception:
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside outer finally block")
It will produce the following output −
Division by 0
inside outer finally block
Example 3
Now we reverse the situation. Out of the nested try blocks, the outer one doesn't have any exception
raised, but the statement causing division by 0 is inside inner try, and hence the exception handled by
inner except block. Obviously, the except part corresponding to outer try: will not be called upon.
a=10
b=0
try:
print ("This is outer try block")
try:
print (a/b)
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside inner finally block")
except Exception:
print ("General Exception")
finally:
print ("inside outer finally block")
It will produce the following output −
This is outer try block
Division by 0
inside inner finally block
inside outer finally block
In the end, let us discuss another situation which may occur in case of nested blocks. While there isn't any
exception in the outer try:, there isn't a suitable except block to handle the one inside the inner try: block.
Example 4
In the following example, the inner try: faces "division by 0", but its corresponding except: is looking for
KeyError instead of ZeroDivisionError. Hence, the exception object is passed on to the except: block of the
subsequent except statement matching with outer try: statement. There, the zeroDivisionError exception
is trapped and handled.
a=10
b=0
try:
print ("This is outer try block")
try:
print (a/b)
except KeyError:
print ("Key Error")
finally:
print ("inside inner finally block")
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside outer finally block")
It will produce the following output −
This is outer try block
inside inner finally block
Division by 0
inside outer finally block
Python - User-Defined Exceptions
Python also allows you to create your own exceptions by deriving classes from the standard built-in
exceptions.
Here is an example that has a user-defined MyException class. Here, a class is created that is subclassed
from base Exception class. This is useful when you need to display more specific information when an
exception is caught.
In the try block, the user-defined exception is raised whenever value of num variable is less than 0 or
more than 100 and caught in the except block. The variable e is used to create an instance of the class
MyException.
Example
class MyException(Exception):
"Invalid marks"
pass
num = 10
try:
if num <0 or num>100:
raise MyException
except MyException as e:
print ("Invalid marks:", num)
else:
print ("Marks obtained:", num)
Output
For different values of num, the program shows the following output −
Marks obtained: 10
Invalid marks: 104
Invalid marks: -10
Python - Logging
The term "logging" refers to the mechanism of recording different intermediate events in a certain
process. Recording logs in a software application proves helpful for the developer in debugging and
tracing any errors in the application logic. Python's standard library includes logging module with which
application logs can be generated and recorded.
It is a normal practice to use print() statements intermittently in a program to check intermediate values
of different variables and objects. It helps the developer to verify if the program is behaving as per
expectation or not. However, logging is more beneficial than the intermittent print statements as it gives
more insight into the events.
Logging Levels
One of the important features of logging is that you can generate log message of different severity levels.
The logging module defines following levels with their values.
Level When it's used Value
DEBUG Detailed information, typically of interest only when diagnosing problems. 10
INFO Confirmation that things are working as expected. 20
An indication that something unexpected happened, or indicative of some problem in
WARNING 30
the near future (e.g. 'disk space low'). The software is still working as expected.
Due to a more serious problem, the software has not been able to perform some
ERROR 40
function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running. 50
Example
The following code illustrates how to generate logging messages.
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('This message will get logged')
It will produce the following output −
DEBUG:root:This message will get logged
To record the logging messages in a file instead of echoing them on the console, use filename parameter.
import logging
logging.basicConfig(level=logging.DEBUG)
marks = 120
logging.error("Invalid marks:{} Marks must be between 0 to 100".format(marks))
subjects = ["Phy", "Maths"]
logging.warning("Number of subjects: {}. Should be at least three".format(len(subjects)))
It will produce the following output −
ERROR:root:Invalid marks:120 Marks must be between 0 to 100
WARNING:root:Number of subjects: 2. Should be at least three
Python - Assertions
An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the
program.
The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more
accurate, a raise-if-not statement). An expression is tested, and if the result comes up false, an
exception is raised.
Assertions are carried out by the assert statement, the newest keyword to Python, introduced in
version 1.5.
Programmers often place assertions at the start of a function to check for valid input, and after a
function call to check for valid output.
The assert Statement
When it encounters an assert statement, Python evaluates the accompanying expression, which is
hopefully true. If the expression is false, Python raises an AssertionError exception.
The syntax for assert is −
assert Expression[, Arguments]
If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.
AssertionError exceptions can be caught and handled like any other exception, using the try-except
statement. If they are not handled, they will terminate the program and produce a traceback.
Example
print ('enter marks out of 100')
num=75
assert num>=0 and num<=100
print ('marks obtained: ', num)
num=125
assert num>=0 and num<=100
print ('marks obtained: ', num)
It will produce the following output −
enter marks out of 100
marks obtained: 75
Traceback (most recent call last):
File "C:\Users\user\example.py", line 7, in <module>
assert num>=0 and num<=100
^^^^^^^^
AssertionError
To display custom error message, put a string after the expression in the assert statement −
assert num>=0 and num<=100, "only numbers in 0-100 accepted"
The AssertionError is also a built-in exception. So it can be used as argument in except block. When input
causes AssertionError exception, it will be handled by except block. The except block treats string in assert
statement goes as exception object.
try:
num=int(input('enter a number'))
assert (num >=0), "only non negative numbers accepted"
print (num)
except AssertionError as msg:
print (msg)
Python - Built-in Exceptions
Here is a list of Standard Exceptions available in Python −
Sr.No. Exception Name & Description
Exception
1
Base class for all exceptions
StopIteration
2
Raised when the next() method of an iterator does not point to any object.
SystemExit
3
Raised by the sys.exit() function.
StandardError
4
Base class for all built-in exceptions except StopIteration and SystemExit.
ArithmeticError
5
Base class for all errors that occur for numeric calculation.
OverflowError
6
Raised when a calculation exceeds maximum limit for a numeric type.
FloatingPointError
7
Raised when a floating point calculation fails.
ZeroDivisonError
8
Raised when division or modulo by zero takes place for all numeric types.
AssertionError
9
Raised in case of failure of the Assert statement.
AttributeError
10
Raised in case of failure of attribute reference or assignment.
EOFError
11 Raised when there is no input from either the raw_input() or input() function and the end of file is
reached.
ImportError
12
Raised when an import statement fails.
KeyboardInterrupt
13
Raised when the user interrupts program execution, usually by pressing Ctrl+C.
LookupError
14
Base class for all lookup errors.
IndexError
15
Raised when an index is not found in a sequence.
KeyError
16
Raised when the specified key is not found in the dictionary.
NameError
17
Raised when an identifier is not found in the local or global namespace.
UnboundLocalError
18 Raised when trying to access a local variable in a function or method but no value has been
assigned to it.
EnvironmentError
19
Base class for all exceptions that occur outside the Python environment.
IOError
20 Raised when an input/ output operation fails, such as the print statement or the open() function
when trying to open a file that does not exist.
OSError
21
Raised for operating system-related errors.
SyntaxError
22
Raised when there is an error in Python syntax.
IndentationError
23
Raised when indentation is not specified properly.
SystemError
24 Raised when the interpreter finds an internal problem, but when this error is encountered the
Python interpreter does not exit.
SystemExit
25 Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code,
causes the interpreter to exit.
TypeError
26
Raised when an operation or function is attempted that is invalid for the specified data type.
ValueError
27 Raised when the built-in function for a data type has the valid type of arguments, but the
arguments have invalid values specified.
RuntimeError
28
Raised when a generated error does not fall into any category.
NotImplementedError
29 Raised when an abstract method that needs to be implemented in an inherited class is not actually
implemented.
Here are some examples of standard exceptions −
IndexError
It is shown when trying to access item at invalid index.
numbers=[10,20,30,40]
for n in range(5):
print (numbers[n])
It will produce the following output −
10
20
30
40
Traceback (most recent call last):
print (numbers[n])
IndexError: list index out of range
ModuleNotFoundError
This is displayed when module could not be found.
import notamodule
Traceback (most recent call last):
import notamodule
ModuleNotFoundError: No module named 'notamodule'
KeyError
It occurs as dictionary key is not found.
D1={'1':"aa", '2':"bb", '3':"cc"}
print ( D1['4'])
Traceback (most recent call last):
D1['4']
KeyError: '4'
ImportError
It is shown when specified function is not available for import.
from math import cube
Traceback (most recent call last):
next(it)
StopIteration
TypeError
This is shown when operator or function is applied to an object of inappropriate type.
print ('2'+2)
Traceback (most recent call last):
'2'+2
TypeError: must be str, not int
ValueError
It is displayed when function's argument is of inappropriate type.
print (int('xyz'))
Traceback (most recent call last):
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
NameError
This is encountered when object could not be found.
print (age)
Traceback (most recent call last):
age
NameError: name 'age' is not defined
ZeroDivisionError
It is shown when second operator in division is zero.
x=100/0
Traceback (most recent call last):
x=100/0
ZeroDivisionError: division by zero
KeyboardInterrupt
When user hits the interrupt key normally Control-C during execution of program.
name=input('enter your name')
enter your name^c
Traceback (most recent call last):
A process always starts with a single thread (main thread). As and when required, a new thread can be
started and sub task is delegated to it. Now the two threads are working in an overlapping manner. When
the task assigned to the secondary thread is over, it merges with the main thread.
Python - Thread Life cycle
A thread object goes through different stages. When a new thread object is created, it must be started.
This calls the run() method of thread class. This method contains the logic of the process to be performed
by the new thread. The thread completes its task as the run() method is over, and the newly created
thread merges with the main thread.
While a thread is running, it may be paused either for a predefined duration or it may be asked to pause
till a certain event occurs. The thread resumes after the specified interval or the process is over.
Python's standard library has two modules, "_thread" and "threading", that include the functionality to
handle threads. The "_thread" module is a low-level API. In Python 3, the threading module has been
included, which provides more comprehensive functionality for thread management.
Python The _thread Module
The _thread module (earlier thread module) has been a part of Python's standard library since version 2.
It is a low-level API for thread management, and works as a support for many of the other modules with
advanced concurrent execution features such as threading and multiprocessing.
Python - The threading Module
The newer threading module provides much more powerful, high-level support for thread management.
The Thread class represents an activity that is run in a separate thread of control. There are two ways to
specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a
subclass.
threading.Thread(target, name, args, kwarg, daemon)
Parameters
target − function to be invoked when a new thread starts. Defaults to None, meaning nothing is
called.
name − is the thread name. By default, a unique name is constructed such as "Thread-N".
daemon − If set to True, the new thread runs in the background.
args and kwargs − optional arguments to be passed to target function.
Python - Creating a Thread
The start_new_thread() function included in the _thread module is used to create a new thread in the
running program.
Syntax
_thread.start_new_thread ( function, args[, kwargs] )
This function starts a new thread and returns its identifier.
Parameters
function − Newly created thread starts running and calls the specified function. If any arguments
are required for the function, that may be passed as parameters args and kwargs.
Example
import _thread
import time
# Define a function for the thread
def thread_task( threadName, delay):
for count in range(1, 6):
time.sleep(delay)
print ("Thread name: {} Count: {}".format ( threadName, count ))
while True:
pass
It will produce the following output −
Thread name: Thread-1 Count: 1
Thread name: Thread-2 Count: 1
Thread name: Thread-1 Count: 2
Thread name: Thread-1 Count: 3
Thread name: Thread-2 Count: 2
Thread name: Thread-1 Count: 4
Thread name: Thread-1 Count: 5
Thread name: Thread-2 Count: 3
Thread name: Thread-2 Count: 4
Thread name: Thread-2 Count: 5
Traceback (most recent call last):
File "C:\Users\user\example.py", line 17, in <module>
while True:
KeyboardInterrupt
The program goes in an infinite loop. You will have to press "ctrl-c" to stop.
Python - Starting a Thread
This start() method starts the thread's activity. It must be called once a thread object is created.
The start() method automatically invokes the object's run() method in a separate thread. However, if it is
called more than once, then a RuntimeError will be raised.
Syntax
Here is the syntax to use the start() method in order to start a thread −
threading.thread.start()
Example
Take a look at the following example −
thread1 = myThread("Thread-1")
def run(self):
print ("Starting " + self.name)
for count in range(1,6):
time.sleep(5)
print ("Thread name: {} Count: {}".format ( self.name, count ))
print ("Exiting " + self.name)
start = time.time()
print('START:', time.ctime(start))
scheduler.enter(2, 1, schedule_event, ('EVENT_1', start))
scheduler.enter(5, 1, schedule_event, ('EVENT_2', start))
scheduler.run()
It will produce the following output −
START: Mon Jun 5 15:37:29 2023
elapsed= 2 name= EVENT_1
elapsed= 5 name= EVENT_2
Example 2
Let's take another example to understand the concept better −
import sched
from datetime import datetime
import time
def addition(a,b):
print("Performing Addition : ", datetime.now())
print("Time : ", time.monotonic())
print("Result : ", a+b)
s = sched.scheduler()
queue = PriorityQueue()
def producer(queue):
print('Producer: Running')
for i in range(5):
queue.put(None)
print('Producer: Done')
def consumer(queue):
print('Consumer: Running')
while True:
sleep(item[1])
print(item)
queue.task_done()
print('Consumer: Done')
producer.join()
consumer.join()
It will produce the following output −
Producer: Running
Consumer: Running
(0, 0.15332707626852804)
(2, 0.4730737391435892)
(2, 0.8679231358257962)
(3, 0.051924220435665025)
(4, 0.23945882716108446)
Producer: Done
Consumer: Done
Python - Daemon Threads
Sometimes, it is necessary to execute a task in the background. A special type of thread is used for
background tasks, called a daemon thread. In other words, daemon threads execute tasks in the
background.
It may be noted that daemon threads execute such non-critical tasks that although may be useful to the
application but do not hamper it if they fail or are canceled mid-operation.
Also, a daemon thread will not have control over when it is terminated. The program will terminate once
all non-daemon threads finish, even if there are daemon threads still running at that point of time.
This is a major difference between daemon threads and non-daemon threads. The process will exit if only
daemon threads are running, whereas it cannot exit if at least one non-daemon thread is running.
Daemon Non-daemon
A process will exit if only daemon threads are running (or A process will not exit if at least one non-
if no threads are running). daemon thread is running.
Creating a Daemon Thread
To create a daemon thread, you need to set the daemon property to True.
t1=threading.Thread(daemon=True)
If a thread object is created without any parameter, its daemon property can also be set to True, before
calling the start() method.
t1=threading.Thread()
t1.daemon=True
Example
Take a look at the following example −
from time import sleep
from threading import current_thread
from threading import Thread
threadLock = threading.Lock()
threads = []
def signal_state():
while True:
time.sleep(5)
print("Traffic Police Giving GREEN Signal")
event.set()
time.sleep(10)
print("Traffic Police Giving RED Signal")
event.clear()
def traffic_flow():
num=0
while num<10:
print("Waiting for GREEN Signal")
event.wait()
print("GREEN Signal ... Traffic can move")
while event.is_set():
num=num+1
print("Vehicle No:", num," Crossing the Signal")
time.sleep(2)
print("RED Signal ... Traffic has to wait")
event=Event()
t1=Thread(target=signal_state)
t2=Thread(target=traffic_flow)
t1.start()
t2.start()
Output
Waiting for GREEN Signal
Traffic Police Giving GREEN Signal
GREEN Signal ... Traffic can move
Vehicle No: 1 Crossing the Signal
Vehicle No: 2 Crossing the Signal
Vehicle No: 3 Crossing the Signal
Vehicle No: 4 Crossing the Signal
Vehicle No: 5 Crossing the Signal
Signal is RED
RED Signal ... Traffic has to wait
Waiting for GREEN Signal
Traffic Police Giving GREEN Signal
GREEN Signal ... Traffic can move
Vehicle No: 6 Crossing the Signal
Vehicle No: 7 Crossing the Signal
Vehicle No: 8 Crossing the Signal
Vehicle No: 9 Crossing the Signal
Vehicle No: 10 Crossing the Signal
The Condition Object
Condition class in threading module class implements condition variable objects. Condition object forces
one or more threads to wait until notified by another thread. Condition is associated with a Reentrant
Lock. A condition object has acquire() and release() methods that call the corresponding methods of the
associated lock.
threading.Condition(lock=None)
Following are the methods of the Condition object −
acquire(*args)
Acquire the underlying lock. This method calls the corresponding method on the underlying lock; the
return value is whatever that method returns.
release()
Release the underlying lock. This method calls the corresponding method on the underlying lock; there is
no return value.
wait(timeout=None)
This method releases the underlying lock, and then blocks until it is awakened by a notify() or notify_all()
call for the same condition variable in another thread, or until the optional timeout occurs. Once
awakened or timed out, it re-acquires the lock and returns.
wait_for(predicate, timeout=None)
This utility method may call wait() repeatedly until the predicate is satisfied, or until a timeout occurs. The
return value is the last return value of the predicate and will evaluate to False if the method timed out.
notify(n=1)
This method wakes up at most n of the threads waiting for the condition variable; it is a no-op if no
threads are waiting.
notify_all()
Wake up all threads waiting on this condition. This method acts like notify(), but wakes up all waiting
threads instead of one. If the calling thread has not acquired the lock when this method is called, a
RuntimeError is raised.
Example
In the following code, the thread t2 runs taskB() function and t1 runs taskA() function. The t1 thread
acquires the condition and notifies it. By that time the t2 thread is in waiting state. After the condition is
released, the waiting thread proceeds to consume the random number generated by the notifying
function.
from threading import *
import time
import random
numbers=[]
def taskA(c):
while True:
c.acquire()
num=random.randint(1,10)
print("Generated random number:", num)
numbers.append(num)
print("Notification issued")
c.notify()
c.release()
time.sleep(5)
def taskB(c):
while True:
c.acquire()
print("waiting for update")
c.wait()
print("Obtained random number", numbers.pop())
c.release()
time.sleep(5)
c=Condition()
t1=Thread(target=taskB, args=(c,))
t2=Thread(target=taskA, args=(c,))
t1.start()
t2.start()
When you execute this code, it will produce the following output −
waiting for update
Generated random number: 4
Notification issued
Obtained random number 4
waiting for update
Generated random number: 6
Notification issued
Obtained random number 6
waiting for update
Generated random number: 10
Notification issued
Obtained random number 10
waiting for update
Python - Thread Deadlock
A deadlock may be described as a concurrency failure mode. It is a situation in a program where one or
more threads wait for a condition that never occurs. As a result, the threads are unable to progress and
the program is stuck or frozen and must be terminated manually.
Deadlock situation may arise in many ways in your concurrent program. Deadlocks are never not
developed intentionally, instead, they are in fact a side effect or bug in the code.
Common causes of thread deadlocks are listed below −
A thread that attempts to acquire the same mutex lock twice.
Threads that wait on each other (e.g. A waits on B, B waits on A).
When a thread that fails to release a resource such as lock, semaphore, condition, event, etc.
Threads that acquire mutex locks in different orders (e.g. fail to perform lock ordering).
If more than one threads in a multi-threaded application try to gain access to same resource, such as
performing read/write operation on same file, it may cause data inconsistency. Hence it is important that
the concurrent handling is synchronized so that it is locked for other threads when one thread is using the
resource.
The threading module provided with Python includes a simple-to-implement locking mechanism that
allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the
new lock.
The Lock Object
An object of Lock class has two possible states − locked or unlocked, initially in unlocked state when first
created. A lock doesn't belong to any particular thread.
The Lock class defines acquire() and release() methods.
The acquire() Method
When the state is unlocked, this method changes the state to locked and returns immediately. The
method takes an optional blocking argument.
Syntax
Lock.acquire(blocking, timeout)
Parameters
blocking − If set to False, it means do not block. If a call with blocking set to True would block,
return False immediately; otherwise, set the lock to locked and return True.
The return value of this method is True if the lock is acquired successfully; False if not.
The release() Method
When the state is locked, this method in another thread changes it to unlocked. This can be called from
any thread, not only the thread which has acquired the lock
Syntax
Lock.release()
The release() method should only be called in the locked state. If an attempt is made to release an
unlocked lock, a RuntimeError will be raised.
When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the
lock to become unlocked, allow exactly one of them to proceed. There is no return value of this method.
Example
In the following program, two threads try to call the synchronized() method. One of them acquires the
lock and gains the access while the other waits. When the run() method is completed for the first thread,
the lock is released and the synchronized method is available for second thread.
When both the threads join, the program comes to an end.
from threading import Thread, Lock
import time
lock=Lock()
threads=[]
class myThread(Thread):
def __init__(self,name):
Thread.__init__(self)
self.name=name
def run(self):
lock.acquire()
synchronized(self.name)
lock.release()
def synchronized(threadName):
print ("{} has acquired lock and is running synchronized method".format(threadName))
counter=5
while counter:
print ('**', end='')
time.sleep(2)
counter=counter-1
print('\nlock released for', threadName)
t1=myThread('Thread1')
t2=myThread('Thread2')
t1.start()
threads.append(t1)
t2.start()
threads.append(t2)
for t in threads:
t.join()
print ("end of main thread")
It will produce the following output −
Thread1 has acquired lock and is running synchronized method
**********
lock released for Thread1
Thread2 has acquired lock and is running synchronized method
**********
lock released for Thread2
end of main thread
The Semaphore Object
Python supports thread synchronization with another mechanism called semaphore. It is one of the
oldest synchronization techniques invented by a well-known computer scientist, Edsger W. Dijkstra.
The basic concept of semaphore is to use an internal counter which is decremented by each acquire() call
and incremented by each release() call. The counter can never go below zero; when acquire() finds that it
is zero, it blocks, waiting until some other thread calls release().
The Semaphore class in threading module defines acquire() and release() methods.
The acquire() Method
If the internal counter is larger than zero on entry, decrement it by one and return True immediately.
If the internal counter is zero on entry, block until awoken by a call to release(). Once awoken (and the
counter is greater than 0), decrement the counter by 1 and return True. Exactly one thread will be
awoken by each call to release(). The order in which threads awake is arbitrary.
If blocking parameter is set to False, do not block. If a call without an argument would block, return False
immediately; otherwise, do the same thing as when called without arguments, and return True.
The release() Method
Release a semaphore, incrementing the internal counter by 1. When it was zero on entry and other
threads are waiting for it to become larger than zero again, wake up n of those threads.
Example
from threading import *
import time
# creating instance
def synchronized(name):
for n in range(3):
print('Hello! ', end = '')
time.sleep(1)
print( name)
class MyThread(Thread):
def __init__(self, event):
super(MyThread, self).__init__()
self.event = event
def run(self):
i=0
while True:
i+=1
print ('Child thread running...',i)
sleep(0.5)
if self.event.is_set():
break
print()
print('Child Thread Interrupted')
event = Event()
thread1 = MyThread(event)
thread1.start()
sleep(3)
print('Main thread stopping child thread')
event.set()
thread1.join()
When you execute this code, it will produce the following output −
Child thread running... 1
Child thread running... 2
Child thread running... 3
Child thread running... 4
Child thread running... 5
Child thread running... 6
Main thread stopping child thread
Child Thread Interrupted
Python - Network Programming
The threading module in Python's standard library is capable of handling multiple threads and their
interaction within a single process. Communication between two processes running on the same machine
is handled by Unix domain sockets, whereas for the processes running on different machines connected
with TCP (Transmission control protocol), Internet domain sockets are used.
Python's standard library consists of various built-in modules that support interprocess communication
and networking. Python provides two levels of access to the network services. At a low level, you can
access the basic socket support in the underlying operating system, which allows you to implement
clients and servers for both connection-oriented and connectionless protocols.
Python also has libraries that provide higher-level access to specific application-level network protocols,
such as FTP, HTTP, and so on.
Protocol Common function Port No Python module
HTTP Web pages 80 httplib, urllib, xmlrpclib
NNTP Usenet news 119 nntplib
FTP File transfers 20 ftplib, urllib
SMTP Sending email 25 smtplib
POP3 Fetching email 110 poplib
IMAP4 Fetching email 143 imaplib
Telnet Command lines 23 telnetlib
Gopher Document transfers 70 gopherlib, urllib
Python - Socket Programming
The socket module in the standard library included functionality required for communication between
server and client at hardware level.
This module provides access to the BSD socket interface. It is available on all operating systems such as
Linux, Windows, MacOS.
What are Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a
process, between processes on the same machine, or between processes on different continents.
A socket is identified by the combination of IP address and the port number. It should be properly
configured at both ends to begin communication.
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP,
and so on. The socket library provides specific classes for handling the common transports as well as a
generic interface for handling the rest.
The term socket programming implies programmatically setting up sockets to be able to send and receive
data.
There are two types of communication protocols −
connection-oriented protocol
connection-less protocol
TCP or Transmission Control Protocol is a connection-oriented protocol. The data is transmitted in packets
by the server, and assembled in the same order of transmission by the receiver. Since the sockets at
either end of the communication need to be set before starting, this method is more reliable.
UDP or User Datagram Protocol is connectionless. The method is not reliable because the sockets does
not require establishing any connection and termination process for transferring the data.
Python The socket Module
This module includes Socket class. A socket object represents the psir of hostname and port number. The
constructor method has the following signature −
Syntax
socket.socket (socket_family, socket_type, protocol=0)
Parameters
family − AF_INET by default. Other values - AF_INET6 (eight groups of four hexadecimal digits),
AF_UNIX, AF_CAN (Controller Area Network) or AF_RDS (Reliable Datagram Sockets).
socket_type − should be SOCK_STREAM (the default), SOCK_DGRAM, SOCK_RAW or perhaps one
of the other SOCK_ constants.
protocol − number is usually zero and may be omitted.
Return Type
This method returns a socket object.
Once you have the socket object, then you can use the required methods to create your client or server
program.
Server Socket Methods
The socket instantiated on server is called server socket. Following methods are available to the socket
object on the server −
bind() method − This method binds the socket to specified IP address and port number.
listen() method − This method starts server and runs into a listen loop looking for connection
request from client.
accept() method − When connection request is intercepted by server, this method accepts it and
identifies the client socket with its address.
To create a socket on a server, use the following snippet −
import socket
server = socket.socket()
server.bind(('localhost',12345))
server.listen()
client, addr = server.accept()
print ("connection request from: " + str(addr))
By default, the server is bound to local machine's IP address 'localhost' listening at arbitrary empty port
number.
Client Socket Methods
Similar socket is set up on the client end. It mainly sends connection request to server socket listening at
its IP address and port number
connect() method
This method takes a two-item tuple object as argument. The two items are IP address and port number of
the server.
obj=socket.socket()
obj.connect((host,port))
Once the connection is accepted by the server, both the socket objects can send and/or receive data.
send() method
The server sends data to client by using the address it has intercepted.
client.send(bytes)
Client socket sends data to socket it has established connection with.
sendall() method
similar to send(). However, unlike send(),this method continues to send data from bytes until either all
data has been sent or an error occurs. None is returned on success.
sendto() method
This method is to be used in case of UDP protocol only.
recv() method
This method is used to retrieve data sent to the client. In case of server, it uses the remote socket whose
request has been accepted.
client.recv(bytes)
recvfrom() method
This method is used in case of UDP protocol.
Python - Socket Server
To write Internet servers, we use the socket function available in socket module to create a socket object.
A socket object is then used to call other functions to setup a socket server.
Now call the bind(hostname, port) function to specify a port for your service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the port
you specified, and then returns a connection object that represents the connection to that client.
import socket
host = "127.0.0.1"
port = 5001
server = socket.socket()
server.bind((host,port))
server.listen()
conn, addr = server.accept()
print ("Connection from: " + str(addr))
while True:
data = conn.recv(1024).decode()
if not data:
break
data = str(data).upper()
print (" from client: " + str(data))
data = input("type message: ")
conn.send(data.encode())
conn.close()
Python - Socket Client
Let us write a very simple client program, which opens a connection to a given port 5001 and a given
localhost. It is very simple to create a socket client using the Python's socket module function.
The socket.connect(hosname, port) opens a TCP connection to hostname on the port. Once you have a
socket open, you can read from it like any IO object. When done, remember to close it, as you would
close a file.
The following code is a very simple client that connects to a given host and port, reads any available data
from the socket, and then exits when 'q' is entered.
import socket
host = '127.0.0.1'
port = 5001
obj = socket.socket()
obj.connect((host,port))
message = input("type message: ")
while message != 'q':
obj.send(message.encode())
data = obj.recv(1024).decode()
print ('Received from server: ' + data)
message = input("type message: ")
obj.close()
Run Server code first. It starts listening.
Then start client code. It sends request.
Request accepted. Client address identified
Type some text and press Enter.
Data received is printed. Send data to client.
Data from server is received.
Loop terminates when 'q' is input.
Server-client interaction is shown below −
We have implemented client-server communication with socket module on the local machine. To put
server and client codes on two different machines on a network, we need to find the IP address of the
server machine.
On Windows, you can find the IP address by running ipconfig command. The ifconfig command is the
equivalent command on Ubuntu.
Change host string in both the server and client codes with IPv4 Address value and run them as before.
Python File Transfer with Socket Module
The following program demonstrates how socket communication can be used to transfer a file from
server to the client
Server Code
The code for establishing connection is same as before. After the connection request is accepted, a file on
server is opened in binary mode for reading, and bytes are successively read and sent to the client stream
till end of file is reached.
import socket
host = "127.0.0.1"
port = 5001
server = socket.socket()
server.bind((host, port))
server.listen()
conn, addr = server.accept()
data = conn.recv(1024).decode()
filename='test.txt'
f = open(filename,'rb')
while True:
l = f.read(1024)
if not l:
break
conn.send(l)
print('Sent ',repr(l))
f.close()
print('File transferred')
conn.close()
Client Code
On the client side, a new file is opened in wb mode. The stream of data received from server is written to
the file. As the stream ends, the output file is closed. A new file will be created on the client machine.
import socket
s = socket.socket()
host = "127.0.0.1"
port = 5001
s.connect((host, port))
s.send("Hello server!".encode())
f.close()
print('Successfully received')
s.close()
print('connection closed')
Python The socketserver Module
The socketserver module in Python's standard library is a framework for simplifying task of writing
network servers. There are following classes in module, which represent synchronous servers −
These classes work with corresponding RequestHandler classes for implementing the service. BaseServer
is the superclass of all Server objects in the module.
TCPServer class uses the internet TCP protocol, to provide continuous streams of data between the client
and server. The constructor automatically attempts to invoke server_bind() and server_activate(). The
other parameters are passed to the BaseServer base class.
You must also create a subclass of StreamRequestHandler class. IT provides self.rfile and self.wfile
attributes to read or write to get the request data or return data to the client.
UDPServer and DatagramRequestHandler − These classes are meant to be used for UDP protocol.
DatagramRequestHandler and UnixDatagramServer − These classes use Unix domain sockets;
they're not available on non-Unix platforms.
Server Code
You must write a RequestHandler class. It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the client.
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip()
host,port=self.client_address
print("{}:{} wrote:".format(host,port))
print(self.data.decode())
msg=input("enter text .. ")
self.request.sendall(msg.encode())
On the server's assigned port number, an object of TCPServer class calls the forever() method to put the
server in the listening mode and accepts incoming requests from clients.
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
server.serve_forever()
Client Code
When working with socketserver, the client code is more or less similar with the socket client application.
import socket
import sys
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
data = input("enter text .. .")
sock.sendall(bytes(data + "\n", "utf-8"))
obj = Request("http://www.nosuchserver.com")
try:
urlopen(obj)
except err.URLError as e:
print(e)
It will produce the following output −
HTTP Error 403: Forbidden
HTTPError
Every time the server sends a HTTP response it is associated with a numeric "status code". It code
indicates why the server is unable to fulfil the request. The default handlers will handle some of these
responses for you. For those it can't handle, urlopen() function raises an HTTPError. Typical examples of
HTTPErrors are '404' (page not found), '403' (request forbidden), and '401' (authentication required).
from urllib.request import Request, urlopen
import urllib.error as err
obj = Request("http://www.python.org/fish.html")
try:
urlopen(obj)
except err.HTTPError as e:
print(e.code)
It will produce the following output −
404
Python - Generics
In Python, generics is a mechanism with which you to define functions, classes, or methods that can
operate on multiple types while maintaining type safety. With the implementation of Generics enable it is
possible to write reusable code that can be used with different data types. It ensures promoting code
flexibility and type correctness.
Generics in Python are implemented using type hints. This feature was introduced in Python with version
3.5 onwards.
Normally, you don't need to declare a variable type. The type is determined dynamically by the value
assigned to it. Python's interpreter doesn't perform type checks and hence it may raise runtime
exceptions.
Python's new type hinting feature helps in prompting the user with the expected type of the parameters
to be passed.
Type hints allow you to specify the expected types of variables, function arguments, and return values.
Generics extend this capability by introducing type variables, which represent generic types that can be
replaced with specific types when using the generic function or class.
Example 1
Let us have a look at the following example that defines a generic function −
from typing import List, TypeVar, Generic
T = TypeVar('T')
def reverse(items: List[T]) -> List[T]:
return items[::-1]
Here, we define a generic function called 'reverse'. The function takes a list ('List[T]') as an argument and
returns a list of the same type. The type variable 'T' represents the generic type, which will be replaced
with a specific type when the function is used.
Example 2
The function reverse() function is called with different data types −
numbers = [1, 2, 3, 4, 5]
reversed_numbers = reverse(numbers)
print(reversed_numbers)
box2 = Box('Hello')
print(box2.get_item())
It will produce the following output −
42
Hello
Python - Date and Time
A Python program can handle date and time in several ways. Converting between date formats is a
common chore for computers. Following modules in Python's standard library handle date and time
related processing −
DateTime module
Time module
Calendar module
What are Tick Intervals
Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in
seconds since 12:00am, January 1, 1970(epoch).
There is a popular time module available in Python, which provides functions for working with times, and
for converting between representations. The function time.time() returns the current system time in ticks
since 12:00am, January 1, 1970(epoch).
Example
import time # This is required to include time module.
ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)
This would produce a result something as follows −
Number of ticks since 12:00am, January 1, 1970: 1681928297.5316436
Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this
form. Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038
for UNIX and Windows.
What is TimeTuple?
Many of the Python's time functions handle time as a tuple of 9 numbers, as shown below −
Index Field Values
0 4-digit year 2016
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0 to 61 (60 or 61 are leap-seconds)
6 Day of Week 0 to 6 (0 is Monday)
7 Day of year 1 to 366 (Julian day)
8 Daylight savings -1, 0, 1, -1 means library determines DST
For example,
>>>import time
>>> print (time.localtime())
This would produce an output as follows −
time.struct_time(tm_year=2023, tm_mon=4, tm_mday=19, tm_hour=23, tm_min=49, tm_sec=8,
tm_wday=2, tm_yday=109, tm_isdst=0)
The above tuple is equivalent to struct_time structure. This structure has the following attributes −
Index Attributes Values
0 tm_year 2016
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
5 tm_sec 0 to 61 (60 or 61 are leap-seconds)
6 tm_wday 0 to 6 (0 is Monday)
7 tm_yday 1 to 366 (Julian day)
8 tm_isdst -1, 0, 1, -1 means library determines DST
Getting the Current Time
To translate a time instant from seconds since the epoch floating-point value into a time-tuple, pass the
floating-point value to a function (e.g., localtime) that returns a time-tuple with all valid nine items.
import time
localtime = time.localtime(time.time())
print ("Local current time :", localtime)
This would produce the following result, which could be formatted in any other presentable form −
Local current time : time.struct_time(tm_year=2023, tm_mon=4, tm_mday=19, tm_hour=23, tm_min=42,
tm_sec=41, tm_wday=2, tm_yday=109, tm_isdst=0)
Getting the Formatted Time
You can format any time as per your requirement, but a simple method to get time in a readable format is
asctime() −
import time
print (date.today())
d1=date.fromisoformat('2023-04-20')
print (d1)
d2=date.fromisoformat('20230420')
print (d2)
d3=date.fromisoformat('2023-W16-4')
print (d3)
It will produce the following output −
2023-04-20
2023-04-20
2023-04-20
2023-04-20
Instance methods in date class
replace() − Return a date by replacing specified attributes with new values by keyword arguments
are specified.
timetuple() − Return a time.struct_time such as returned by time.localtime().
toordinal() − Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has
ordinal 1. For any date object d, date.fromordinal(d.toordinal()) == d.
weekday() − Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
isoweekday() − Return the day of the week as an integer, where Monday is 1 and Sunday is 7.
isocalendar() − Return a named tuple object with three components: year, week and weekday.
isoformat() − Return a string representing the date in ISO 8601 format, YYYY-MM-DD:
__str__() − For a date d, str(d) is equivalent to d.isoformat()
ctime() − Return a string representing the date:
strftime(format) − Return a string representing the date, controlled by an explicit format string.
__format__(format) − Same as date.strftime().
Example
from datetime import date
d = date.fromordinal(738630) # 738630th day after 1. 1. 0001
print (d)
print (d.timetuple())
# Methods related to formatting string output
print (d.isoformat())
print (d.strftime("%d/%m/%y"))
print (d.strftime("%A %d. %B %Y"))
print (d.ctime())
ic = d.isocalendar()
for i in ic:
print(i)
time3 = time()
print("time", time3)
max = datetime.max
print("Max DateTime ", max)
It will produce the following output −
Min DateTime 0001-01-01 00:00:00
Max DateTime 9999-12-31 23:59:59.999999
Instance Attributes
datetime.year − Between MINYEAR and MAXYEAR inclusive.
datetime.month − Between 1 and 12 inclusive.
datetime.day − Between 1 and the number of days in the given month of the given year.
datetime.hour − In range(24)
datetime.minute − In range(60)
datetime.second − In range(60)
datetime.microsecond − In range(1000000).
datetime.tzinfo − The object passed as the tzinfo argument to the datetime constructor, or None
if none was passed.
datetime.fold − In [0, 1]. Used to disambiguate wall times during a repeated interval.
Example
from datetime import datetime
dt = datetime.now()
# Using datetime.combine()
d = date(2022, 4, 20)
t = time(12, 30)
datetime.combine(d, t)
# Using datetime.now()
d = datetime.now()
print (d)
# Using datetime.strptime()
dt = datetime.strptime("23/04/20 16:30", "%d/%m/%y %H:%M")
date1 = datetime.now()
date2= date1+timedelta(days = 4)
print("Date after 4 days:", date2)
date3 = date1-timedelta(15)
print("Date before 15 days:", date3)
It will produce the following output −
Date after 4 days: 2023-04-24 18:05:39.509905
Date before 15 days: 2023-04-05 18:05:39.509905
Class Attributes
timedelta.min − The most negative timedelta object, timedelta(-999999999).
timedelta.max − The most positive timedelta object, timedelta(days=999999999, hours=23,
minutes=59, seconds=59, microseconds=999999).
timedelta.resolution − The smallest possible difference between non-equal timedelta objects,
timedelta(microseconds=1)
Example
from datetime import timedelta
max = timedelta.max
print("Maximum value", max)
It will produce the following output −
Minimum value: -999999999 days, 0:00:00
Maximum value 999999999 days, 23:59:59.999999
Instance Attributes
Since only day, second and microseconds are stored internally, those are the only instance attributes for a
timedelta object.
days − Between -999999999 and 999999999 inclusive
seconds − Between 0 and 86399 inclusive
microseconds − Between 0 and 999999 inclusive
Instance Methods
timedelta.total_seconds() − Return the total number of seconds contained in the duration.
Example
from datetime import timedelta
year = timedelta(days=365)
years = 5 * year
print (years)
print (years.days // 365)
646
year_1 = years // 5
print(year_1.days)
It will produce the following output −
1825 days, 0:00:00
5
365
Python - Maths
Python's standard library provides math module. This module includes many pre-defined functions for
performing different mathematical operations. These functions do not work with complex numbers.
There is a cmath module contains mathematical functions for complex numbers.
Functions in Math Module
Here is the list of functions available in the math module −
Sr.No Method & Description
acos (x)
1
Return the arc cosine of x, in radians.
acosh (x)
2
Return the inverse hyperbolic cosine of x.
asin
3
Return the arc sine of x, in radians.
asinh (x)
4
Return the inverse hyperbolic sine of x.
atan
5
Return the arc tangent of x, in radians.
atan2
6
Return atan(y/x), in radians.
atanh (x)
7
Return the inverse hyperbolic tangent of x.
cbrt (x)
8
Return the cube root of x.
cell(x)
9
The ceiling of x: the smallest integer not less than x.
comb (x,y)
10
Return the number of ways to choose x items from y iter repetition and without order.
copysign(x,y)
11
Return a float with the magnitude of x but the sign of y.
cos (x)
12
Return the cosine of x radians.
cosh (x)
13
Return the hyperbolic cosine of x.
degrees
14
Converts angle x from radians to degrees.
dist (x,y)
15
Return the Euclidean distance between two points x and y.
e
16
The mathematical constant e = 2.718281..., to available precision.
erf (x)
17
Return the error function at x.
erfc (x)
18
Return the complementary error function at x.
exp (x)
19
Return e raised to the power x, where e = 2.718281...
exp2 (x)
20
Return 2 raised to the power x.
expm1 (x)
21
Return e raised to the power x, minus 1.
fabs(x)
22
The absolute value of x in float
23 factorial(x)
Return x factorial as an Integer.
floor (x)
24
The floor of x: the largest integer not greater than x.
fmod (x,y)
25
Always returns float, similar to x%y
frexp (x)
26
Returns the mantissa and exponent for a given number x.
fsum (iterable)
27
Sum of all numbers in any iterable, returns float.
gamma (x)
28
Return the Gamma function at x..
gcd (x,y,z)
29
Return the greatest common divisor of the specified integer arguments.
hypot
30
Return the Euclidean norm, sqrt(x*x + y*y).
inf
31
A floating-point positive infinity. Equivalent to the output of float('inf").
isclose (x,y)
32
Return True if the values x and y are close to each other and False otherwise.
isfinite (x)
33
Returns True if neither an infinity nor a NaN, and False otherwise.
isinf (x)
34
Return True if x is a positive or negative infinity, and False otherwise.
isnan (x)
35
Return True if x is a NaN (not a number), and False otherwise.
isqrt (x)
36
Return the integer square root of the nonnegative integer x
lcm (x1, x2, ..)
37
Return the least common multiple of the specified integer arguments.
ldexp (x,y)
38
Return x * (2**y). This is the inverse of function frexp().
lgamma (x)
39
Return the natural logarithm of the absolute value of the Gamma function at x.
log (x)
40
Return the natural logarithm of x (to base e).
log10 (x)
41
Return the base-10 logarithm of x.
log1p (x)
42
Return the natural logarithm of 1+x (base e).
log2 (x)
43
Return the base-2 logarithm of x.
modf (x)
44 The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x.
The integer part is returned as a float.
nan
45
A floating-point "not a number" (NaN) value.
nextafter (x,y)
46
Return the next floating-point value after x towards y.
perm (x,y)
47
Return the number of ways to choose x items from y items without repetition and with order.
pi
48
The mathematical constant π = 3.141592..., to available precision.
pow (x,y)
49
Returns x raised to y
prod (iterable)
50
Return the product of all the elements in the input iterable.
radians
51
Converts angle x from degrees to radians.
remainder (x,y)
52
Returns the remainder of x with respect to y
sin (x)
53
Return the sine of x radians.
sinh (x)
54
Return the inverse hyperbolic sine of x.
sqrt (x)
55
Return the square root of x.
tan (x)
56
Return the tangent of x radians.
tanh (x)
57
Return the hyperbolic tangent of x.
tau
58
The mathematical constant τ = 6.283185..., to available precision.
trunc (x)
59
Return x with the fractional part removed, leaving the integer part.
ulp
60
Return the value of the least significant bit of the float x.
These functions can be classified in following categories −
Theoretic and Representation functions
Power and Logarithmic functions
Trigonometric functions
Angular Conversion functions
Hyperbolic functions
Special functions
Mathematical constants
Python - Iterators
Iterator in Python is an object representing a stream of data. It follows iterator protocol which requires it
to support __iter__() and __next__() methods. Python's built-in method iter() implements __iter__()
method. It receives an iterable and returns iterator object. The built-in next() function internally calls to
the iterator's __next__() method returns successive items in the stream. When no more data are
available a StopIteration exception is raised.
Python uses iterators are implicitly while working with collection data types such as list, tuple or string.
That's why these data types are called iterables. We normally use for loop to iterate through an iterable
as follows −
for element in sequence:
print (element)
Python's built-in method iter() implements __iter__() method. It receives an iterable and returns iterator
object.
Example
Following code obtains iterator object from sequence types list, string and tuple. The iter() function also
returns keyiterator from dictionary. However, int id not iterable, hence it produces TypeError.
print (iter("aa"))
print (iter([1,2,3]))
print (iter((1,2,3)))
print (iter({}))
print (iter(100))
It will produce the following output −
<str_ascii_iterator object at 0x000001BB03FFAB60>
<list_iterator object at 0x000001BB03FFAB60>
<tuple_iterator object at 0x000001BB03FFAB60>
<dict_keyiterator object at 0x000001BB04181670>
Traceback (most recent call last):
File "C:\Users\user\example.py", line 5, in <module>
print (iter(100))
^^^^^^^^^
TypeError: 'int' object is not iterable
Iterator object has __next__() method. Every time it is called, it returns next element in iterator stream.
When the stream gets exhausted, StopIteration error is raised. Call to next() function is equivalent to
calling __next__() method of iterator object.
Example
it = iter([1,2,3])
print (next(it))
print (it.__next__())
print (it.__next__())
print (next(it))
It will produce the following output −
1
2
3
Traceback (most recent call last):
File "C:\Users\user\example.py", line 5, in <module>
print (next(it))
^^^^^^^^
StopIteration
Example
You can use exception handling mechanism to catch StopIteration.
it = iter([1,2,3, 4, 5])
print (next(it))
while True:
try:
no = next(it)
print (no)
except StopIteration:
break
It will produce the following output −
1
2
3
4
5
To define a custom iterator class in Python, the class must define __iter__() and __next__() methods.
In the following example, the Oddnumbers is a class implementing __iter__() and __next__() methods. On
every call to __next__(), the number increments by 2, thereby streaming odd numbers in the range 1 to
10.
Example
class Oddnumbers:
def __iter__(self):
return self
def __next__(self):
if self.start < self.end-1:
self.start += 2
return self.start
else:
raise StopIteration
countiter = Oddnumbers(10)
while True:
try:
no = next(countiter)
print (no)
except StopIteration:
break
It will produce the following output −
1
3
5
7
9
Asynchronous Iterator
Two built-in functions aiter() and anext() have been added in Python 3.10 version onwards. The aiter()
function returns an asynchronous iterator object. It is an asynchronous counter part of the classical
iterator. Any asynchronous iterator must support __aiter__() and __anext__() methods. These methods
are internally called by the two built-in functions.
Like the classical iterator, the asynchronous iterator gives a stream of objects. When the stream is
exhausted, the StopAsyncIteration exception is raised.
In the example give below, an asynchronous iterator class Oddnumbers is declared. It implements
__aiter__() and __anext__() method. On each iteration, a next odd number is returned, and the program
waits for one second, so that it can perform any other process asynchronously.
Unlike regular functions, asynchronous functionsare called coroutines and are executed with
asyncio.run() method. The main() coroutine contains a while loop that successively obtains odd numbers
and raises StopAsyncIteration if the number exceeds 9.
Example
import asyncio
class Oddnumbers():
def __init__(self):
self.start = -1
def __aiter__(self):
return self
asyncio.run(main())
It will produce the following output −
1
3
5
7
9
Python - Generators
A generator in Python is a special type of function that returns an iterator object. It appears similar to a
normal Python function in that its definition also starts with def keyword. However, instead of return
statement at the end, generator uses the yield keyword.
Syntax
def generator():
...
...
yield obj
it = generator()
next(it)
...
The return statement at the end of function indicates that the execution of the function body is over, all
the local variables in the function go out of the scope. If the function is called again, the local variables are
re-initialized.
Generator function behaves differently. It is invoked for the first time like a normal function, but when its
yield statement comes, its execution is temporarily paused, transferring the control back. The yielded
result is consumed by the caller. The call to next() built-in function restarts the execution of generator
from the point it was paused, and generates the next object for the iterator. The cycle repeats as
subsequent yield provides next item in the iterator it is exhausted.
Example 1
The function in the code below is a generator that successively yield integers from 1 to 5. When called, it
returns an iterator. Every call to next() transfers the control back to the generator and fetches next
integer.
def generator(num):
for x in range(1, num+1):
yield x
return
it = generator(5)
while True:
try:
print (next(it))
except StopIteration:
break
It will produce the following output −
1
2
3
4
5
The generator function returns a dynamic iterator. Hence, it is more memory efficient than a normal
iterator that you obtain from a Python sequence object. For example, if you want to get first n numbers in
Fibonacci series. You can write a normal function and build a list of Fibonacci numbers, and then iterate
the list using a loop.
Example 2
Given below is the normal function to get a list of Fibonacci numbers −
def fibonacci(n):
fibo = []
a, b = 0, 1
while True:
c=a+b
if c>=n:
break
fibo.append(c)
a, b = b, c
return fibo
f = fibonacci(10)
for i in f:
print (i)
It will produce the following output −
1
2
3
5
8
The above code collects all Fibonacci series numbers in a list and then the list is traversed using a loop.
Imagine that we want Fibonacci series going upto a large number. In which case, all the numbers must be
collected in a list requiring huge memory. This is where generator is useful, as it generates a single
number in the list and gives it for consumption.
Example 3
Following code is the generator-based solution for list of Fibonacci numbers −
def fibonacci(n):
a, b = 0, 1
while True:
c=a+b
if c>=n:
break
yield c
a, b = b, c
return
f = fibonacci(10)
while True:
try:
print (next(f))
except StopIteration:
break
Asynchronous Generator
An asynchronous generator is a coroutine that returns an asynchronous iterator. A coroutine is a Python
function defined with async keyword, and it can schedule and await other coroutines and tasks. Just like a
normal generator, the asynchronous generator yields incremental item in the iterator for every call to
anext() function, instead of next() function.
Syntax
async def generator():
...
...
yield obj
it = generator()
anext(it)
...
Example 4
Following code demonstrates a coroutine generator that yields incrementing integers on every iteration
of an async for loop.
import asyncio
asyncio.run(main())
It will produce the following output −
1
2
3
4
5
Example 5
Let us now write an asynchronous generator for Fibonacci numbers. To simulate some asynchronous task
inside the coroutine, the program calls sleep() method for a duration of 1 second before yielding the next
number. As a result, you will get the numbers printed on the screen after a delay of one second.
import asyncio
asyncio.run(main())
It will produce the following output −
1
2
3
5
8
Python - Closures
In this chapter, let us discuss the concept of closures in Python. In Python, functions are said to be first
order objects. Just like the primary data types, functions can also be used assigned to variables, or passed
as arguments.
Nested Functions
You can also have a nested declaration of functions, wherein a function is defined inside the body of
another function.
Example
def functionA():
print ("Outer function")
def functionB():
print ("Inner function")
functionB()
functionA()
It will produce the following output −
Outer function
Inner function
In the above example, functionB is defined inside functionA. Inner function is then called from inside the
outer function's scope.
If the outer function receives any argument, it can be passed to the inner function.
def functionA(name):
print ("Outer function")
def functionB():
print ("Inner function")
print ("Hi {}".format(name))
functionB()
functionA("Python")
It will produce the following output −
Outer function
Inner function
Hi Python
What is a Closure?
A closure is a nested function which has access to a variable from an enclosing function that has finished
its execution. Such a variable is not bound in the local scope. To use immutable variables (number or
string), we have to use the nonlocal keyword.
The main advantage of Python closures is that we can help avoid the using global values and provide
some form of data hiding. They are used in Python decorators.
Example
def functionA(name):
name ="New name"
def functionB():
print (name)
return functionB
myfunction = functionA()
retval = myfunction()
print ("Counter:", retval)
retval = myfunction()
print ("Counter:", retval)
retval = myfunction()
print ("Counter:", retval)
It will produce the following output −
Counter: 1
Counter: 2
Counter: 3
Python - Decorators
A Decorator in Python is a function that receives another function as argument. The argument function is
the one to be decorated by decorator. The behaviour of argument function is extended by the decorator
without actually modifying it.
In this chapter, we whall learn how to use Python decorator.
Function in Python is a first order object. It means that it can be passed as argument to another function
just as other data types such as number, string or list etc. It is also possible to define a function inside
another function. Such a function is called nested function. Moreover, a function can return other
function as well.
Syntax
The typical definition of a decorator function is as under −
def decorator(arg_function): #arg_function to be decorated
def nested_function():
#this wraps the arg_function and extends its behaviour
#call arg_function
arg_function()
return nested_function
Here a normal Python function −
def function():
print ("hello")
You can now decorate this function to extend its behaviour by passing it to decorator −
function=decorator(function)
If this function is now executed, it will show output extended by decorator.
Example 1
Following code is a simple example of decorator −
def my_function(x):
print("The number is=",x)
def my_decorator(some_function,num):
def wrapper(num):
print("Inside wrapper to check odd/even")
if num%2 == 0:
ret= "Even"
else:
ret= "Odd!"
some_function(num)
return ret
print ("wrapper function is called")
return wrapper
no=10
my_function = my_decorator(my_function, no)
print ("It is ",my_function(no))
The my_function() just prints out the received number. However, its behaviour is modified by passing it to
a my_decorator. The inner function receives the number and returns whether it is odd/even. Output of
above code is −
wrapper function is called
Inside wrapper to check odd/even
The number is= 10
It is Even
Example 2
An elegant way to decorate a function is to mention just before its definition, the name of decorator
prepended by @ symbol. The above example is re-written using this notation −
def my_decorator(some_function):
def wrapper(num):
print("Inside wrapper to check odd/even")
if num%2 == 0:
ret= "Even"
else:
ret= "Odd!"
some_function(num)
return ret
print ("wrapper function is called")
return wrapper
@my_decorator
def my_function(x):
print("The number is=",x)
no=10
print ("It is ",my_function(no))
Python's standard library defines following built-in decorators −
@classmethod Decorator
The classmethod is a built-in function. It transforms a method into a class method. A class method is
different from an instance method. Instance method defined in a class is called by its object. The method
received an implicit object referred to by self. A class method on the other hand implicitly receives the
class itself as first argument.
Syntax
In order to declare a class method, the following notation of decorator is used −
class Myclass:
@classmethod
def mymethod(cls):
#....
The @classmethod form is that of function decorator as described earlier. The mymethod receives
reference to the class. It can be called by the class as well as its object. That means Myclass.mymethod as
well as Myclass().mymethod both are valid calls.
Example 3
Let us understand the behaviour of class method with the help of following example −
class counter:
count=0
def __init__(self):
print ("init called by ", self)
counter.count=counter.count+1
print ("count=",counter.count)
@classmethod
def showcount(cls):
print ("called by ",cls)
print ("count=",cls.count)
c1=counter()
c2=counter()
print ("class method called by object")
c1.showcount()
print ("class method called by class")
counter.showcount()
In the class definition count is a class attribute. The __init__() method is the constructor and is obviously
an instance method as it received self as object reference. Every object declared calls this method and
increments count by 1.
The @classmethod decorator transforms showcount() method into a class method which receives
reference to the class as argument even if it is called by its object. It can be seen even when c1 object
calls showcount, it displays reference of counter class.
It will display the following output −
init called by <__main__.counter object at 0x000001D32DB4F0F0>
count= 1
init called by <__main__.counter object at 0x000001D32DAC8710>
count= 2
class method called by object
called by <class '__main__.counter'>
count= 2
class method called by class
called by <class '__main__.counter'>
@staticmethod Decorator
The staticmethod is also a built-in function in Python standard library. It transforms a method into a static
method. Static method doesn't receive any reference argument whether it is called by instance of class or
class itself. Following notation used to declare a static method in a class −
Syntax
class Myclass:
@staticmethod
def mymethod():
#....
Even though Myclass.mymethod as well as Myclass().mymethod both are valid calls, the static method
receives reference of neither.
Example 4
The counter class is modified as under −
class counter:
count=0
def __init__(self):
print ("init called by ", self)
counter.count=counter.count+1
print ("count=",counter.count)
@staticmethod
def showcount():
print ("count=",counter.count)
c1=counter()
c2=counter()
print ("class method called by object")
c1.showcount()
print ("class method called by class")
counter.showcount()
As before, the class attribute count is increment on declaration of each object inside the __init__()
method. However, since mymethod(), being a static method doesn't receive either self or cls parameter.
Hence value of class attribute count is displayed with explicit reference to counter.
The output of the above code is as below −
init called by <__main__.counter object at 0x000002512EDCF0B8>
count= 1
init called by <__main__.counter object at 0x000002512ED48668>
count= 2
class method called by object
count= 2
class method called by class
count= 2
@property Decorator
Python's property() built-in function is an interface for accessing instance variables of a class. The
@property decorator turns an instance method into a "getter" for a read-only attribute with the same
name, and it sets the docstring for the property to "Get the current value of the instance variable."
You can use the following three decorators to define a property −
@property − Declares the method as a property.
@<property-name>.setter: − Specifies the setter method for a property that sets the value to a
property.
@<property-name>.deleter − Specifies the delete method as a property that deletes a property.
A property object returned by property() function has getter, setter, and delete methods.
property(fget=None, fset=None, fdel=None, doc=None)
The fget argument is the getter method, fset is setter method. It optionally can have fdel as method to
delete the object and doc is the documentation string.
The property() object's setter and getter may also be assigned with the following syntax also.
speed = property()
speed=speed.getter(speed, get_speed)
speed=speed.setter(speed, set_speed)
Where get_speed() and set_speeds() are the instance methods that retrieve and set the value to an
instance variable speed in Car class.
The above statements can be implemented by @property decorator. Using the decorator car class is re-
written as −
class car:
def __init__(self, speed=40):
self._speed=speed
return
@property
def speed(self):
return self._speed
@speed.setter
def speed(self, speed):
if speed<0 or speed>100:
print ("speed limit 0 to 100")
return
self._speed=speed
return
c1=car()
print (c1.speed) #calls getter
c1.speed=60 #calls setter
Property decorator is very convenient and recommended method of handling instance attributes.
Python - Recursion
A function that calls itself is called a recursive function. This method is used when a certain problem is
defined in terms of itself. Although this involves iteration, using iterative approach to solve such problem
can be tedious. Recursive approach provides a very concise solution to seemingly complex problem.
The most popular example of recursion is calculation of factorial. Mathematically factorial is defined as −
n! = n × (n-1)!
It can be seen that we use factorial itself to define factorial. Hence this is a fit case to write a recursive
function. Let us expand above definition for calculation of factorial value of 5.
5! = 5 × 4!
5 × 4 × 3!
5 × 4 × 3 × 2!
5 × 4 × 3 × 2 × 1!
5×4×3×2×1
= 120
While we can perform this calculation using a loop, its recursive function involves successively calling it by
decrementing the number till it reaches 1.
Example 1
The following example shows hows you can use a recursive function to calculate factorial −
def factorial(n):
if n == 1:
print (n)
return 1
else:
print (n,'*', end=' ')
return n * factorial(n-1)
Example 2
The following code implements the recursive binary searching technique −
def bsearch(my_list, low, high, elem):
if high >= low:
mid = (high + low) // 2
if my_list[mid] == elem:
return mid
elif my_list[mid] > elem:
return bsearch(my_list, low, mid - 1, elem)
else:
return bsearch(my_list, mid + 1, high, elem)
else:
return -1
if my_result != -1:
print("Element found at index ", str(my_result))
else:
print("Element not found!")
It will produce the following output −
The list is
[5, 12, 23, 45, 49, 67, 71, 77, 82]
Check for number: 67
Element found at index 5
You can check the output for different numbers in the list, as well as not in the list.
Python - Regular Expressions
A regular expression is a special sequence of characters that helps you match or find other strings or sets
of strings, using a specialized syntax held in a pattern. A regular expression also known as regex is a
sequence of characters that defines a search pattern. Popularly known as as regex or regexp; it is a
sequence of characters that specifies a match pattern in text. Usually, such patterns are used by string-
searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
Large scale text processing in data science projects requires manipulation of textual data. The regular
expressions processing is supported by many programming languages including Python. Python's
standard library has 're' module for this purpose.
Since most of the functions defined in re module work with raw strings, let us first understand what the
raw strings are.
Raw Strings
Regular expressions use the backslash character ('\') to indicate special forms or to allow special
characters to be used without invoking their special meaning. Python on the other hand uses the same
character as escape character. Hence Python uses the raw string notation.
A string become a raw string if it is prefixed with r or R before the quotation symbols. Hence 'Hello' is a
normal string were are r'Hello' is a raw string.
>>> normal="Hello"
>>> print (normal)
Hello
>>> raw=r"Hello"
>>> print (raw)
Hello
In normal circumstances, there is no difference between the two. However, when the escape character is
embedded in the string, the normal string actually interprets the escape sequence, where as the raw
string doesn't process the escape character.
>>> normal="Hello\nWorld"
>>> print (normal)
Hello
World
>>> raw=r"Hello\nWorld"
>>> print (raw)
Hello\nWorld
In the above example, when a normal string is printed the escape character '\n' is processed to introduce
a newline. However because of the raw string operator 'r' the effect of escape character is not translated
as per its meaning.
Metacharacters
Most letters and characters will simply match themselves. However, some characters are special
metacharacters, and don't match themselves. Meta characters are characters having a special meaning,
similar to * in wild card.
Here's a complete list of the metacharacters −
.^$*+?{}[]\|()
The square bracket symbols[ and ] indicate a set of characters that you wish to match. Characters can be
listed individually, or as a range of characters separating them by a '-'.
Sr.No. Metacharacters & Description
[abc]
1
match any of the characters a, b, or c
[a-c]
2
which uses a range to express the same set of characters.
[a-z]
3
match only lowercase letters.
[0-9]
4
match only digits.
'^'
5
complements the character set in [].[^5] will match any character except'5'.
'\'is an escaping metacharacter. When followed by various characters it forms various special sequences.
If you need to match a [ or \, you can precede them with a backslash to remove their special meaning: \
[ or \\.
Predefined sets of characters represented by such special sequences beginning with '\' are listed below −
Sr.No. Metacharacters & Description
\d
1
Matches any decimal digit; this is equivalent to the class [0-9].
\D
2
Matches any non-digit character; this is equivalent to the class [^0-9].
3 \sMatches any whitespace character; this is equivalent to the class [\t\n\r\f\v].
\S
4
Matches any non-whitespace character; this is equivalent to the class [^\t\n\r\f\v].
\w
5
Matches any alphanumeric character; this is equivalent to the class [a-zAZ0-9_].
\W
6
Matches any non-alphanumeric character. equivalent to the class [^a-zAZ0-9_].
.
7
Matches with any single character except newline '\n'.
?
8
match 0 or 1 occurrence of the pattern to its left
+
9
1 or more occurrences of the pattern to its left
*
10
0 or more occurrences of the pattern to its left
\b
11
boundary between word and non-word and /B is opposite of /b
[..]
12 Matches any single character in a square bracket and [^..] matches any single character not in
square bracket.
\
13
It is used for special meaning characters like \. to match a period or \+ for plus sign.
{n,m}
14
Matches at least n and at most m occurrences of preceding
a| b
15
Matches either a or b
Python's re module provides useful functions for finding a match, searching for a pattern, and substitute a
matched string with other string etc.
re.match() Function
This function attempts to match RE pattern at the start of string with optional flags.
Here is the syntax for this function −
re.match(pattern, string, flags=0)
Here is the description of the parameters −
Sr.No. Parameter & Description
pattern
1
This is the regular expression to be matched.
String
2
This is the string, which would be searched to match the pattern at the beginning of string.
Flags
3 You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the
table below.
The re.match function returns a match object on success, None on failure. A match object instance
contains information about the match: where it starts and ends, the substring it matched, etc.
The match object's start() method returns the starting position of pattern in the string, and end() returns
the endpoint.
If the pattern is not found, the match object is None.
We use group(num) or groups() function of match object to get matched expression.
Sr.No. Match Object Methods & Description
1 group(num=0)This method returns entire match (or specific subgroup num)
2 groups()This method returns all matching subgroups in a tuple (empty if there weren't any)
Example
import re
line = "Cats are smarter than dogs"
matchObj = re.match( r'Cats', line)
print (matchObj.start(), matchObj.end())
print ("matchObj.group() : ", matchObj.group())
It will produce the following output −
04
matchObj.group() : Cats
re.search() Function
This function searches for first occurrence of RE pattern within the string, with optional flags.
Here is the syntax for this function −
re.search(pattern, string, flags=0)
Here is the description of the parameters −
Sr.No. Parameter & Description
Pattern
1
This is the regular expression to be matched.
String
2
This is the string, which would be searched to match the pattern anywhere in the string.
Flags
3 You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the
table below.
The re.search function returns a match object on success, none on failure. We use group(num) or
groups() function of match object to get the matched expression.
Sr.No. Match Object Methods & Description
1 group(num=0)This method returns entire match (or specific subgroup num)
2 groups()This method returns all matching subgroups in a tuple (empty if there weren't any)
Example
import re
line = "Cats are smarter than dogs"
matchObj = re.search( r'than', line)
print (matchObj.start(), matchObj.end())
print ("matchObj.group() : ", matchObj.group())
It will produce the following output −
17 21
matchObj.group() : than
Matching Vs Searching
Python offers two different primitive operations based on regular expressions :match checks for a match
only at the beginning of the string, while search checks for a match anywhere in the string (this is what
Perl does by default).
Example
import re
line = "Cats are smarter than dogs";
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print ("match --> matchObj.group() : ", matchObj.group())
else:
print ("No match!!")
searchObj = re.search( r'dogs', line, re.M|re.I)
if searchObj:
print ("search --> searchObj.group() : ", searchObj.group())
else:
print ("Nothing found!!")
When the above code is executed, it produces the following output −
No match!!
search --> matchObj.group() : dogs
re.findall() Function
The findall() function returns all non-overlapping matches of pattern in string, as a list of strings or tuples.
The string is scanned left-to-right, and matches are returned in the order found. Empty matches are
included in the result.
Syntax
re.findall(pattern, string, flags=0)
Parameters
Sr.No. Parameter & Description
Pattern
1
This is the regular expression to be matched.
String
2
This is the string, which would be searched to match the pattern anywhere in the string.
Flags
3 You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the
table below.
Example
import re
string="Simple is better than complex."
obj=re.findall(r"ple", string)
print (obj)
It will produce the following output −
['ple', 'ple']
Following code obtains the list of words in a sentence with the help of findall() function.
import re
string="Simple is better than complex."
obj=re.findall(r"\w*", string)
print (obj)
It will produce the following output −
['Simple', '', 'is', '', 'better', '', 'than', '', 'complex', '', '']
re.sub() Function
One of the most important re methods that use regular expressions is sub.
Syntax
re.sub(pattern, repl, string, max=0)
This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences
unless max is provided. This method returns modified string.
Example
import re
phone = "2004-959-559 # This is Phone Number"
obj=pattern.findall(string)
print (obj)
obj=pattern.sub(r'was', string)
print (obj)
It will produce the following output −
79
['is', 'is']
Simple was better than complex. Complex was better than complicated.
re.finditer() Function
This function returns an iterator yielding match objects over all non-overlapping matches for the RE
pattern in string.
Syntax
re.finditer(pattern, string, flags=0)
Example
import re
string="Simple is better than complex. Complex is better than
complicated."
pattern=re.compile(r'is')
iterator = pattern.finditer(string)
print (iterator )
Python's standard library includes sqlite3 module which is a DB_API compatible driver for SQLite3
database, it is also a reference implementation of DB-API.
Since the required DB-API interface is built-in, we can easily use SQLite database with a Python
application. For other types of databases, you will have to install the relevant Python package.
Database Python Package
Oracle cx_oracle, pyodbc
SQL Server pymssql, pyodbc
PostgreSQL psycopg2
MySQL MySQL Connector/Python, pymysql
A DB-API module such as sqlite3 contains connection and cursor classes. The connection object is
obtained with connect() method by providing required connection credentials such as name of server and
port number, and username and password if applicable. The connection object handles opening and
closing the database, and transaction control mechanism of committing or rolling back a transaction.
The cursor object, obtained from the connection object, acts as the handle of the database when
performing all the CRUD operations.
The sqlite3 Module
SQLite is a server-less, file-based lightweight transactional relational database. It doesn't require any
installation and no credentials such as username and password are needed to access the database.
Python's sqlite3 module contains DB-API implementation for SQLite database. It is written by Gerhard
Häring. Let us learn how to use sqlite3 module for database access with Python.
Let us start by importing sqlite3 and check its version.
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.39.4'
The Connection Object
A connection object is set up by connect() function in sqlite3 module. First positional argument to this
function is a string representing path (relative or absolute) to a SQLite database file. The function returns
a connection object referring to the database.
>>> conn=sqlite3.connect('testdb.sqlite3')
>>> type(conn)
<class 'sqlite3.Connection'>
Various methods are defined in connection class. One of them is cursor() method that returns a cursor
object, about which we shall know in next section. Transaction control is achieved by commit() and
rollback() methods of connection object. Connection class has important methods to define custom
functions and aggregates to be used in SQL queries.
The Cursor Object
Next, we need to get the cursor object from the connection object. It is your handle to the database when
performing any CRUD operation on the database. The cursor() method on connection object returns the
cursor object.
>>> cur=conn.cursor()
>>> type(cur)
<class 'sqlite3.Cursor'>
We can now perform all SQL query operations, with the help of its execute() method available to cursor
object. This method needs a string argument which must be a valid SQL statement.
Creating a Database Table
We shall now add Employee table in our newly created 'testdb.sqlite3' database. In following script, we
call execute() method of cursor object, giving it a string with CREATE TABLE statement inside.
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry='''
CREATE TABLE Employee (
EmpID INTEGER PRIMARY KEY AUTOINCREMENT,
FIRST_NAME TEXT (20),
LAST_NAME TEXT(20),
AGE INTEGER,
SEX TEXT(1),
INCOME FLOAT
);
'''
try:
cur.execute(qry)
print ('Table created successfully')
except:
print ('error in creating table')
conn.close()
When the above program is run, the database with Employee table is created in the current working
directory.
We can verify by listing out tables in this database in SQLite console.
sqlite> .open mydb.sqlite
sqlite> .tables
Employee
INSERT Operation
The INSERT Operation is required when you want to create your records into a database table.
Example
The following example, executes SQL INSERT statement to create a record in the EMPLOYEE table −
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry="""INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
cur.execute(qry)
conn.commit()
print ('Record inserted successfully')
except:
conn.rollback()
print ('error in INSERT operation')
conn.close()
You can also use the parameter substitution technique to execute the INSERT query as follows −
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry="""INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES (?, ?, ?, ?, ?)"""
try:
cur.execute(qry, ('Makrand', 'Mohan', 21, 'M', 5000))
conn.commit()
print ('Record inserted successfully')
except Exception as e:
conn.rollback()
print ('error in INSERT operation')
conn.close()
READ Operation
READ Operation on any database means to fetch some useful information from the database.
Once the database connection is established, you are ready to make a query into this database. You can
use either fetchone() method to fetch a single record or fetchall() method to fetch multiple values from a
database table.
fetchone() − It fetches the next row of a query result set. A result set is an object that is returned
when a cursor object is used to query a table.
fetchall() − It fetches all the rows in a result set. If some rows have already been extracted from
the result set, then it retrieves the remaining rows from the result set.
rowcount − This is a read-only attribute and returns the number of rows that were affected by an
execute() method.
Example
In the following code, the cursor object executes SELECT * FROM EMPLOYEE query. The resultset is
obtained with fetchall() method. We print all the records in the resultset with a for loop.
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry="SELECT * FROM EMPLOYEE"
try:
# Execute the SQL command
cur.execute(qry)
# Fetch all the rows in a list of lists.
results = cur.fetchall()
for row in results:
fname = row[1]
lname = row[2]
age = row[3]
sex = row[4]
income = row[5]
# Now print fetched result
print ("fname={},lname={},age={},sex={},income={}".format(fname, lname, age, sex, income ))
except Exception as e:
print (e)
print ("Error: unable to fecth data")
conn.close()
It will produce the following output −
fname=Mac,lname=Mohan,age=20,sex=M,income=2000.0
fname=Makrand,lname=Mohan,age=21,sex=M,income=5000.0
Update Operation
UPDATE Operation on any database means to update one or more records, which are already available in
the database.
The following procedure updates all the records having income=2000. Here, we increase the income by
1000.
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry="UPDATE EMPLOYEE SET INCOME = INCOME+1000 WHERE INCOME=?"
try:
# Execute the SQL command
cur.execute(qry, (1000,))
# Fetch all the rows in a list of lists.
conn.commit()
print ("Records updated")
except Exception as e:
print ("Error: unable to update data")
conn.close()
DELETE Operation
DELETE operation is required when you want to delete some records from your database. Following is the
procedure to delete all the records from EMPLOYEE where INCOME is less than 2000.
import sqlite3
conn=sqlite3.connect('testdb.sqlite3')
cur=conn.cursor()
qry="DELETE FROM EMPLOYEE WHERE INCOME<?"
try:
# Execute the SQL command
cur.execute(qry, (2000,))
# Fetch all the rows in a list of lists.
conn.commit()
print ("Records deleted")
except Exception as e:
print ("Error: unable to delete data")
conn.close()
Performing Transactions
Transactions are a mechanism that ensure data consistency. Transactions have the following four
properties −
Atomicity − Either a transaction completes or nothing happens at all.
Consistency − A transaction must start in a consistent state and leave the system in a consistent
state.
Isolation − Intermediate results of a transaction are not visible outside the current transaction.
Durability − Once a transaction was committed, the effects are persistent, even after a system
failure.
The Python DB API 2.0 provides two methods to either commit or rollback a transaction.
Example
You already know how to implement transactions. Here is a similar example −
# Prepare SQL query to DELETE required records
sql = "DELETE FROM EMPLOYEE WHERE AGE > ?"
try:
# Execute the SQL command
cursor.execute(sql, (20,))
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
COMMIT Operation
Commit is an operation, which gives a green signal to the database to finalize the changes, and after this
operation, no change can be reverted back.
Here is a simple example to call the commit method.
db.commit()
ROLLBACK Operation
If you are not satisfied with one or more of the changes and you want to revert back those changes
completely, then use the rollback() method.
Here is a simple example to call the rollback() method.
db.rollback()
The PyMySQL Module
PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the
Python Database API v2.0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be
a drop-in replacement for MySQLdb.
Installing PyMySQL
Before proceeding further, you make sure you have PyMySQL installed on your machine. Just type the
following in your Python script and execute it −
import PyMySQL
If it produces the following result, then it means MySQLdb module is not installed −
Traceback (most recent call last):
File "test.py", line 3, in <module>
Import PyMySQL
ImportError: No module named PyMySQL
The last stable release is available on PyPI and can be installed with pip −
pip install PyMySQL
Note − Make sure you have root privilege to install the above module.
MySQL Database Connection
Before connecting to a MySQL database, make sure of the following points −
You have created a database TESTDB.
You have created a table EMPLOYEE in TESTDB.
This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
User ID "testuser" and password "test123" are set to access TESTDB.
Python module PyMySQL is installed properly on your machine.
You have gone through MySQL tutorial to understand MySQL Basics.
Example
To use MySQL database instead of SQLite database in earlier examples, we need to change the connect()
function as follows −
import PyMySQL
# Open database connection
db = PyMySQL.connect("localhost","testuser","test123","TESTDB" )
Apart from this change, every database operation can be performed without difficulty.
Handling Errors
There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a
connection failure, or calling the fetch method for an already cancelled or finished statement handle.
The DB API defines a number of errors that must exist in each database module. The following table lists
these exceptions.
Sr.No. Exception & Description
Warning
1
Used for non-fatal issues. Must subclass StandardError.
Error
2
Base class for errors. Must subclass StandardError.
InterfaceError
3
Used for errors in the database module, not the database itself. Must subclass Error.
DatabaseError
4
Used for errors in the database. Must subclass Error.
5 DataError
Subclass of DatabaseError that refers to errors in the data.
OperationalError
6 Subclass of DatabaseError that refers to errors such as the loss of a connection to the database.
These errors are generally outside of the control of the Python scripter.
IntegrityError
7 Subclass of DatabaseError for situations that would damage the relational integrity, such as
uniqueness constraints or foreign keys.
InternalError
8 Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no
longer being active.
ProgrammingError
9 Subclass of DatabaseError that refers to errors such as a bad table name and other things that can
safely be blamed on you.
NotSupportedError
10
Subclass of DatabaseError that refers to trying to call unsupported functionality.
Python - Weak References
Python uses reference counting mechanism while implementing garbage collection policy. Whenever an
object in the memory is referred, the count is incremented by one. On the other hand, when the
reference is removed, the count is decremented by 1. If the garbage collector running in the background
finds any object with count as 0, it is removed and the memory occupied is reclaimed.
Weak reference is a reference that does not protect the object from getting garbage collected. It proves
important when you need to implement caches for large objects, as well as in a situation where reduction
of Pain from circular references is desired.
To create weak references, Python has provided us with a module named weakref.
The ref class in this module manages the weak reference to an object. When called, it retrieves the
original object.
To create a weak reference −
weakref.ref(class())
Example
import weakref
class Myclass:
def __del__(self):
print('(Deleting {})'.format(self))
obj = Myclass()
r = weakref.ref(obj)
print('object:', obj)
print('reference:', r)
print('call r():', r())
print('deleting obj')
del obj
print('r():', r())
Calling the reference object after deleting the referent returns None.
It will produce the following output −
object: <__main__.Myclass object at 0x00000209D7173290>
reference: <weakref at 0x00000209D7175940; to 'Myclass' at
0x00000209D7173290>
call r(): <__main__.Myclass object at 0x00000209D7173290>
deleting obj
(Deleting <__main__.Myclass object at 0x00000209D7173290>)
r(): None
The callback Function
The constructor of ref class has an optional parameter called callback function, which gets called when
the referred object is deleted.
import weakref
class Myclass:
def __del__(self):
print('(Deleting {})'.format(self))
def mycallback(rfr):
"""called when referenced object is deleted"""
print('calling ({})'.format(rfr))
obj = Myclass()
r = weakref.ref(obj, mycallback)
print('object:', obj)
print('reference:', r)
print('call r():', r())
print('deleting obj')
del obj
print('r():', r())
It will produce the following output −
object: <__main__.Myclass object at 0x000002A0499D3590>
reference: <weakref at 0x000002A0499D59E0; to 'Myclass' at
0x000002A0499D3590>
call r(): <__main__.Myclass object at 0x000002A0499D3590>
deleting obj
(Deleting <__main__.Myclass object at 0x000002A0499D3590>)
calling (<weakref at 0x000002A0499D59E0; dead>)
r(): None
Finalizing Objects
The weakref module provides finalize class. Its object is called when the garbage collector collects the
object. The object survives until the reference object is called.
import weakref
class Myclass:
def __del__(self):
print('(Deleting {})'.format(self))
def finalizer(*args):
print('Finalizer{!r})'.format(args))
obj = Myclass()
r = weakref.finalize(obj, finalizer, "Call to finalizer")
print('object:', obj)
print('reference:', r)
print('call r():', r())
print('deleting obj')
del obj
print('r():', r())
It will produce the following output −
object: <__main__.Myclass object at 0x0000021015103590>
reference: <finalize object at 0x21014eabe80; for 'Myclass' at
0x21015103590>
Finalizer('Call to finalizer',))
call r(): None
deleting obj
(Deleting <__main__.Myclass object at 0x0000021015103590>)
r(): None
The weakref module provides WeakKeyDictionary and WeakValueDictionary classes. They don't keep the
objects alive as they appear in the mapping objects. They are more appropriate for creating a cache of
several objects.
WeakKeyDictionary
Mapping class that references keys weakly. Entries in the dictionary will be discarded when there is no
longer a strong reference to the key.
An instance of WeakKeyDictionary class is created with an existing dictionary or without any argumentThe
functionality is the same as a normal dictionary to add and remove mapping entries to it.
In the code given below three Person instances are created. It then creates an instance of
WeakKeyDictionary with a dictionary where the key is the Person instance and the value is the Person's
name.
We call the keyrefs() method to retrieve weak references. When the reference to Peron1 is deleted,
dictionary keys are printed again. A new Person instance is added to a dictionary with weakly referenced
keys. At last, we are printing keys of dictionary again.
Example
import weakref
class Person:
def __init__(self, person_id, name, age):
self.emp_id = person_id
self.name = name
self.age = age
def __repr__(self):
return "{} : {} : {}".format(self.person_id, self.name, self.age)
Person1 = Person(101, "Jeevan", 30)
Person2 = Person(102, "Ramanna", 35)
Person3 = Person(103, "Simran", 28)
weak_dict = weakref.WeakKeyDictionary({Person1: Person1.name, Person2: Person2.name, Person3:
Person3.name})
print("Weak Key Dictionary : {}\n".format(weak_dict.data))
print("Dictionary Keys : {}\n".format([key().name for key in weak_dict.keyrefs()]))
del Person1
print("Dictionary Keys : {}\n".format([key().name for key in weak_dict.keyrefs()]))
Person4 = Person(104, "Partho", 32)
weak_dict.update({Person4: Person4.name})
class Person:
def __init__(self, person_id, name, age):
self.emp_id = person_id
self.name = name
self.age = age
def __repr__(self):
return "{} : {} : {}".format(self.person_id, self.name, self.age)
['name', 'age']
Example 6
The ""symbolhasbeendefinedasthesubstitutioncharacter.Ifyouneed
itself to appear in the string, it has to be escaped. In other words, use $$ to use it in the string.
from string import Template
class myTemplate(Template):
delimiter = '#'
tempStr = myTemplate('Hello. My name is #name and my age is #age')
print (tempStr.substitute(name='Harsha', age=30))
Python - Output Formatting
In this chapter, different techniques for formatting the output will be discussed.
String Formatting Operator
One of Python's coolest features is the string format operator %. This operator is unique to strings and
makes up for the pack of having functions from C's printf() family. Format specification symbols (%d %c %f
%s etc) used in C language are used as placeholders in a string.
Following is a simple example −
print ("My name is %s and weight is %d kg!" % ('Zara', 21))
It will produce the following output −
My name is Zara and weight is 21 kg!
The format() Method
Python 3.0, introduced format() method to str class for handling complex string formatting more
efficiently. This method has since been backported to Python 2.6 and Python 2.7.
This method of in-built string class provides ability to do complex variable substitutions and value
formatting. This new formatting technique is regarded as more elegant.
Syntax
The general syntax of format() method is as follows −
str.format(var1, var2,...)
Return Value
The method returns a formatted string.
The string itself contains placeholders {} in which values of variables are successively inserted.
Example
name="Rajesh"
age=23
print ("my name is {} and my age is {} years".format(name, age))
It will produce the following output −
my name is Rajesh and my age is 23 years
You can use variables as keyword arguments to format() method and use the variable name as the
placeholder in the string.
print ("my name is {name} and my age is {age}
years".format(name="Rajesh", age=23))
You can also specify C style formatting symbols. Only change is using : instead of %. For example, instead
of %s use {:s} and instead of %d use (:d}
name="Rajesh"
age=23
print ("my name is {:s} and my age is {:d} years".format(name, age))
f-strings
In Python, f-strings or Literal String Interpolation is another formatting facility. With this formatting
method you can use embedded Python expressions inside string constants. Python f-strings are a faster,
more readable, more concise, and less error prone.
The string starts with a 'f' prefix, and one or more place holders are inserted in it, whose value is filled
dynamically.
name = 'Rajesh'
age = 23
text = '''
Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code
readability with the use of significant indentation via the off-side rule.
wrapper = textwrap.TextWrapper(width=40)
wrapped = wrapper.wrap(text = text)
# Print output
for element in wrapped:
print(element)
It will produce the following output −
Python is a high-level, general-purpose
programming language. Its design
philosophy emphasizes code readability
with the use of significant indentation
via the off-side rule. Python is
dynamically typed and garbage-collected.
It supports multiple programming
paradigms, including structured
(particularly procedural), objectoriented and functional programming. It
is often described as a "batteries
included" language due to its
comprehensive standard library.
Following attributes are defined for a TextWrapper object −
width − (default: 70) The maximum length of wrapped lines.
expand_tabs − (default: True) If true, then all tab characters in text will be expanded to spaces
using the expandtabs() method of text.
tabsize − (default: 8) If expand_tabs is true, then all tab characters in text will be expanded to zero
or more spaces, depending on the current column and the given tab size.
replace_whitespace − (default: True) If true, after tab expansion but before wrapping, the wrap()
method will replace each whitespace character with a single space.
drop_whitespace − (default: True) If true, whitespace at the beginning and ending of every line
(after wrapping but before indenting) is dropped. Whitespace at the beginning of the paragraph,
however, is not dropped if non-whitespace follows it. If whitespace being dropped takes up an
entire line, the whole line is dropped.
initial_indent − (default: '') String that will be prepended to the first line of wrapped output.
subsequent_indent − (default: '') String that will be prepended to all lines of wrapped output
except the first.
fix_sentence_endings − (default: False) If true, TextWrapper attempts to detect sentence endings
and ensure that sentences are always separated by exactly two spaces. This is generally desired
for text in a monospaced font.
break_long_words − (default: True) If true, then words longer than width will be broken in order
to ensure that no lines are longer than width. If it is false, long words will not be broken, and some
lines may be longer than width.
break_on_hyphens − (default: True) If true, wrapping will occur preferably on whitespaces and
right after hyphens in compound words, as it is customary in English. If false, only whitespaces will
be considered as potentially good places for line breaks.
The shorten() Function
Collapse and truncate the given text to fit in the given width. The text first has its whitespace collapsed. If
it then fits in the *width*, it is returned as is. Otherwise, as many words as possible are joined and then
the placeholder is appended −
Example
import textwrap
l1=['Tests','ODI','T20']
l2=[[140, 45, 39],[15,122,36,67, 100, 49],[78,44, 12, 0, 23, 75]]
p1=player("virat",l1,l2)
pp=pprint.PrettyPrinter()
pp.pprint(p1)
The output of above code is −
{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49],
'T20': [78, 44, 12, 0, 23, 75]}}
Python - Performance Measurement
A given problem may be solved by more than one alternative algorithms. Hence, we need to optimize the
performance of the solution. Python's timeit module is a useful tool to measure the performance of a
Python application.
The timit() function in this module measures execution time of your Python code.
Syntax
timeit.timeit(stmt, setup, timer, number)
Parameters
stmt − code snippet for measurement of performance.
setup − setup details arguments to be passed or variables.
timer − uses default timer, so, it may be skipped.
number − the code will be executed this number of times. The default is 1000000.
Example
The following statement uses list comprehension to return a list of multiple of 2 for each number in the
range upto 100.
>>> [n*2 for n in range(100)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68,
70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100,
102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126,
128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152,
154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178,
180, 182, 184, 186, 188, 190, 192, 194, 196, 198]
To measure the execution time of the above statement, we use the timeit() function as follows −
>>> from timeit import timeit
>>> timeit('[n*2 for n in range(100)]', number=10000)
0.0862189000035869
Compare the execution time with the process of appending the numbers using a for loop.
>>> string = '''
... numbers=[]
... for n in range(100):
... numbers.append(n*2)
... '''
>>> timeit(string, number=10000)
0.1010853999905521
The result shows that list comprehension is more efficient.
The statement string can contain a Python function to which one or more arguments My be passed as
setup code.
We shall find and compare the execution time of a factorial function using a loop with that of its recursive
version.
The normal function using for loop is −
def fact(x):
fact = 1
for i in range(1, x+1):
fact*=i
return fact
Definition of recursive factorial.
def rfact(x):
if x==1:
return 1
else:
return x*fact(x-1)
Test these functions to calculate factorial of 10.
print ("Using loop:",fact(10))
print ("Using Recursion",rfact(10))
Result
Using loop: 3628800
Using Recursion 3628800
Now we shall find their respective execution time with timeit() function.
import timeit
setup1="""
from __main__ import fact
x = 10
"""
setup2="""
from __main__ import rfact
x = 10
"""
<Directory "/var/www/cgi-bin">
Options All
</Directory>
The following line should also be added for apache server to treat .py file as cgi script.
AddHandler cgi-script .py
Here, we assume that you have Web Server up and running successfully and you are able to run any other
CGI program like Perl or Shell, etc.
First CGI Program
Here is a simple link, which is linked to a CGI script called hello.py. This file is kept in /var/www/cgi-bin
directory and it has following content. Before running your CGI program, make sure you have change
mode of file using chmod 755 hello.py UNIX command to make file executable.
print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')
Note − First line in the script must be the path to Python executable. It appears as a comment in Python
program, but it is called shebang line.
In Linux, it should be #!/usr/bin/python3.
In Windows, it should be #!c:/python311/python.exd.
Enter the following URL in your browser −
http://localhost/cgi-bin/hello.py
Hello Word! This is my first CGI program
This hello.py script is a simple Python script, which writes its output on STDOUT file, i.e., screen. There is
one important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\
n. This line is sent back to the browser and it specifies the content type to be displayed on the browser
screen.
By now you must have understood basic concept of CGI and you can write many complicated CGI
programs using Python. This script can interact with any other external system also to exchange
information such as RDBMS.
HTTP Header
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to
understand the content. All the HTTP header will be in the following form −
HTTP Field Name: Field Content
For Example
Content-type: text/html\r\n\r\n
There are few other important HTTP headers, which you will use frequently in your CGI Programming.
Sr.No. Header & Description
Content-type:
1
A MIME string defining the format of the file being returned. Example is Content-type:text/html
Expires: Date
2 The date the information becomes invalid. It is used by the browser to decide when a page needs
to be refreshed. A valid date string is in the format 01 Jan 1998 12:00:00 GMT.
Location: URL
3 The URL that is returned instead of the URL requested. You can use this field to redirect a request
to any file.
Last-modified: Date
4
The date of last modification of the resource.
5 Content-length: N
The length, in bytes, of the data being returned. The browser uses this value to report the
estimated download time for a file.
Set-Cookie: String
6
Set the cookie passed through the string
CGI Environment Variables
All the CGI programs have access to the following environment variables. These variables play an
important role while writing any CGI program.
Sr.No. Variable Name & Description
CONTENT_TYPE
1 The data type of the content. Used when the client is sending attached content to the server. For
example, file upload.
CONTENT_LENGTH
2
The length of the query information. It is available only for POST requests.
HTTP_COOKIE
3
Returns the set cookies in the form of key & value pair.
HTTP_USER_AGENT
4 The User-Agent request-header field contains information about the user agent originating the
request. It is name of the web browser.
PATH_INFO
5
The path for the CGI script.
QUERY_STRING
6
The URL-encoded information that is sent with GET method request.
REMOTE_ADDR
7
The IP address of the remote host making the request. This is useful logging or for authentication.
REMOTE_HOST
8 The fully qualified name of the host making the request. If this information is not available, then
REMOTE_ADDR can be used to get IR address.
REQUEST_METHOD
9
The method used to make the request. The most common methods are GET and POST.
SCRIPT_FILENAME
10
The full path to the CGI script.
SCRIPT_NAME
11
The name of the CGI script.
SERVER_NAME
12
The server's hostname or IP Address
SERVER_SOFTWARE
13
The name and version of the software the server is running.
Here is small CGI program to list out all the CGI variables. Click this link to see the result Get Environment
import os
print ("Content-type:text/html")
print()
print ("<html>")
print ('<head>')
print ("<title>Hello - Second CGI Program</title>")
print ('</head>')
print ('<body>')
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ('</body>')
print ('</html>')
This would generate the following result −
Hello Malhar Lathkar
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
Let us take again same example as above which passes two values using HTML FORM and submit button.
We use same CGI script hello_get.py to handle this input.
<form action = "/cgi-bin/hello_get.py" method = "post">
First Name: <input type = "text" name = "first_name"><br />
Last Name: <input type = "text" name = "last_name" />
if form.getvalue('physics'):
physics_flag = "ON"
else:
physics_flag = "OFF"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"
Passing Radio Button Data to CGI Program
Radio Buttons are used when only one option is required to be selected.
Here is example HTML code for a form with two radio buttons −
<form action = "/cgi-bin/radiobutton.py" method = "post" target = "_blank">
<input type = "radio" name = "subject" value = "maths" /> Maths
<input type = "radio" name = "subject" value = "physics" /> Physics
<input type = "submit" value = "Select Subject" />
</form>
The result of this code is the following form −
Maths Physics
Below is radiobutton.py script to handle input given by web browser for radio button −
# Import modules for CGI handling
import cgi, cgitb
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Passing Text Area Data to CGI Program
TEXTAREA element is used when multiline text has to be passed to the CGI Program.
Here is example HTML code for a form with a TEXTAREA box −
<form action = "/cgi-bin/textarea.py" method = "post" target = "_blank">
<textarea name = "textcontent" cols = "40" rows = "4">
Type your text here...
</textarea> Type your text here...
<input type = "submit" value = "Submit" />
</form>
The result of this code is the following form −
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"
Passing Drop Down Box Data to CGI Program
Drop Down Box is used when we have many options available but only one or two will be selected.
Here is example HTML code for a form with one drop down box −
<form action = "/cgi-bin/dropdown.py" method = "post" target = "_blank">
<select name = "dropdown">
<option value = "Maths" selected>Maths</option>
<option value = "Physics">Physics</option>
</select>
<input type = "submit" value = "Submit"/>
</form>
The result of this code is the following form −
Below is dropdown.py script to handle input given by web browser.
# Import modules for CGI handling
import cgi, cgitb
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Using Cookies in CGI
HTTP protocol is a stateless protocol. For a commercial website, it is required to maintain session
information among different pages. For example, one user registration ends after completing many pages.
How to maintain user's session information across all the web pages?
In many situations, using cookies is the most efficient method of remembering and tracking preferences,
purchases, commissions, and other information required for better visitor experience or site statistics.
How It Works?
Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the
cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor
arrives at another page on your site, the cookie is available for retrieval. Once retrieved, your server
knows/remembers what was stored.
Cookies are a plain text data record of 5 variable-length fields −
Expires − The date the cookie will expire. If this is blank, the cookie will expire when the visitor
quits the browser.
Domain − The domain name of your site.
Path − The path to the directory or web page that sets the cookie. This may be blank if you want
to retrieve the cookie from any directory or page.
Secure − If this field contains the word "secure", then the cookie may only be retrieved with a
secure server. If this field is blank, no such restriction exists.
Name = Value − Cookies are set and retrieved in the form of key and value pairs.
Setting up Cookies
It is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to
Content-type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done
as follows −
print "Set-Cookie:UserID = XYZ;\r\n"
print "Set-Cookie:Password = XYZ123;\r\n"
print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT;\r\n"
print "Set-Cookie:Domain = www.tutorialspoint.com;\r\n"
print "Set-Cookie:Path = /perl;\n"
print "Content-type:text/html\r\n\r\n"
...........Rest of the HTML Content....
From this example, you must have understood how to set cookies. We use Set-Cookie HTTP header to set
cookies.
It is optional to set cookies attributes like Expires, Domain, and Path. It is notable that cookies are set
before sending magic line "Content-type:text/html\r\n\r\n.
Retrieving Cookies
It is very easy to retrieve all the set cookies. Cookies are stored in CGI environment variable HTTP_COOKIE
and they will have following form −
key1 = value1;key2 = value2;key3 = value3....
Here is an example of how to retrieve cookies.
# Import modules for CGI handling
from os import environ
import cgi, cgitb
if environ.has_key('HTTP_COOKIE'):
for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
(key, value ) = split(cookie, '=');
if key == "UserID":
user_id = value
if key == "Password":
password = value
form = cgi.FieldStorage()
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
If you run the above script on Unix/Linux, then you need to take care of replacing file separator as follows,
otherwise on your windows machine above open() statement should work fine.
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
How To Raise a "File Download" Dialog Box?
Sometimes, it is desired that you want to give option where a user can click a link and it will pop up a "File
Download" dialogue box to the user instead of displaying actual content. This is very easy and can be
achieved through HTTP header. This HTTP header is be different from the header mentioned in previous
section.
For example, if you want make a FileName file downloadable from a given link, then its syntax is as
follows −
# HTTP Header
print "Content-Type:application/octet-stream; name = \"FileName\"\r\n";
print "Content-Disposition: attachment; filename = \"FileName\"\r\n\n";
str = fo.read();
print str
if ( __name__ == "__main__"):
# create an XMLReader
parser = xml.sax.make_parser()
type = movie.getElementsByTagName('type')[0]
print ("Type: %s" % type.childNodes[0].data)
format = movie.getElementsByTagName('format')[0]
print ("Format: %s" % format.childNodes[0].data)
rating = movie.getElementsByTagName('rating')[0]
print ("Rating: %s" % rating.childNodes[0].data)
description = movie.getElementsByTagName('description')[0]
print ("Description: %s" % description.childNodes[0].data)
This would produce the following output −
Root element : New Arrivals
*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Rating: PG
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Rating: R
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Description: Viewable boredom
For a complete detail on DOM API documentation, please refer to standard Python DOM APIs.
ElementTree XML API
The xml package has an ElementTree module. This is a simple and lightweight XML processor API.
XML is a tree-like hierarchical data format. The 'ElementTree' in this module treats the whole XML
document as a tree. The 'Element' class represents a single node in this tree. Reading and writing
operations on XML files are done on the ElementTree level. Interactions with a single XML element and its
sub-elements are done on the Element level.
Create an XML File
The tree is a hierarchical structure of elements starting with root followed by other elements. Each
element is created by using the Element() function of this module.
import xml.etree.ElementTree as et
e=et.Element('name')
Each element is characterized by a tag and attrib attribute which is a dict object. For tree's starting
element, attrib is an empty dictionary.
>>> root=xml.Element('employees')
>>> root.tag
'employees'
>>> root.attrib
{}
You may now set up one or more child elements to be added under the root element. Each child may
have one or more sub elements. Add them using the SubElement() function and define its text attribute.
child=xml.Element("employee")
nm = xml.SubElement(child, "name")
nm.text = student.get('name')
age = xml.SubElement(child, "salary")
age.text = str(student.get('salary'))
Each child is added to root by append() function as −
root.append(child)
After adding required number of child elements, construct a tree object by elementTree() function −
tree = et.ElementTree(root)
The entire tree structure is written to a binary file by tree object's write() function −
f=open('employees.xml', "wb")
tree.write(f)
Example
In this example, a tree is constructed out of a list of dictionary items. Each dictionary item holds key-value
pairs describing a student data structure. The tree so constructed is written to 'myfile.xml'
import xml.etree.ElementTree as et
employees=[{'name':'aaa','age':21,'sal':5000},{'name':xyz,'age':22,'sal':6000}]
root = et.Element("employees")
for employee in employees:
child=xml.Element("employee")
root.append(child)
nm = xml.SubElement(child, "name")
nm.text = student.get('name')
age = xml.SubElement(child, "age")
age.text = str(student.get('age'))
sal=xml.SubElement(child, "sal")
sal.text=str(student.get('sal'))
tree = et.ElementTree(root)
with open('employees.xml', "wb") as fh:
tree.write(fh)
The 'myfile.xml' is stored in current working directory.
<employees><employee><name>aaa</name><age>21</age><sal>5000</sal></
employee><employee><name>xyz</name><age>22</age><sal>60</sal></employee></employee>
Parse an XML File
Let us now read back the 'myfile.xml' created in above example. For this purpose, following functions in
ElementTree module will be used −
ElementTree() − This function is overloaded to read the hierarchical structure of elements to a tree
objects.
tree = et.ElementTree(file='students.xml')
getroot() − This function returns root element of the tree.
root = tree.getroot()
You can obtain the list of sub-elements one level below of an element.
children = list(root)
In the following example, elements and sub-elements of the 'myfile.xml' are parsed into a list of
dictionary items.
Example
import xml.etree.ElementTree as et
tree = et.ElementTree(file='employees.xml')
root = tree.getroot()
employees=[]
children = list(root)
for child in children:
employee={}
pairs = list(child)
for pair in pairs:
employee[pair.tag]=pair.text
employees.append(employee)
print (employees)
It will produce the following output −
[{'name': 'aaa', 'age': '21', 'sal': '5000'}, {'name': 'xyz', 'age':'22', 'sal': '6000'}]
Modify an XML file
We shall use iter() function of Element. It creates a tree iterator for given tag with the current element as
the root. The iterator iterates over this element and all elements below it, in document (depth first)
order.
Let us build iterator for all 'marks' subelements and increment text of each sal tag by 100.
import xml.etree.ElementTree as et
tree = et.ElementTree(file='students.xml')
root = tree.getroot()
for x in root.iter('sal'):
s=int (x.text)
s=s+100
x.text=str(s)
with open("employees.xml", "wb") as fh:
tree.write(fh)
Our 'employees.xml' will now be modified accordingly. We can also use set() to update value of a certain
key.
x.set(marks, str(mark))
Python - GUI Programming
Python provides various options for developing graphical user interfaces (GUIs). The most important
features are listed below.
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look
at this option in this chapter.
wxPython − This is an open-source Python interface for wxWidgets GUI toolkit. You can find a
complete tutorial on WxPython here.
PyQt − This is also a Python interface for a popular cross-platform Qt GUI library. TutorialsPoint
has a very good tutorial on PyQt5 here.
PyGTK − PyGTK is a set of wrappers written in Python and C for GTK + GUI library. The complete
PyGTK tutorial is available here.
PySimpleGUI − PySimpleGui is an open source, cross-platform GUI library for Python. It aims to
provide a uniform API for creating desktop GUIs based on Python's Tkinter, PySide and WxPython
toolkits. For a detaile PySimpleGUI tutorial, click here.
Pygame − Pygame is a popular Python library used for developing video games. It is free, open
source and cross-platform wrapper around Simple DirectMedia Library (SDL). For a comprehensive
tutorial on Pygame, visit this link.
Jython − Jython is a Python port for Java, which gives Python scripts seamless access to the Java
class libraries on the local machinehttp: //www.jython.org.
There are many other interfaces available, which you can find them on the net.
Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and
easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI
toolkit.
The tkinter package includes following modules −
Tkinter − Main Tkinter module.
tkinter.colorchooser − Dialog to let the user choose a color.
tkinter.commondialog − Base class for the dialogs defined in the other modules listed here.
tkinter.filedialog − Common dialogs to allow the user to specify a file to open or save.
tkinter.font − Utilities to help work with fonts.
tkinter.messagebox − Access to standard Tk dialog boxes.
tkinter.scrolledtext − Text widget with a vertical scroll bar built in.
tkinter.simpledialog − Basic dialogs and convenience functions.
tkinter.ttk − Themed widget set introduced in Tk 8.5, providing modern alternatives for many of
the classic widgets in the main tkinter module.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps.
Import the Tkinter module.
Create the GUI application main window.
Add one or more of the above-mentioned widgets to the GUI application.
Enter the main event loop to take action against each event triggered by the user.
Example
# note that module name has changed from Tkinter in Python 2
# to tkinter in Python 3
import tkinter
top = tkinter.Tk()
When the program becomes more complex, using an object-oriented programming approach makes the
code more organized.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
app = App()
app.mainloop()
Tkinter Widgets
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These
controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. We present these widgets as well as a brief description
in the following table −
Sr.No. Operator & Description
Button
1
The Button widget is used to display the buttons in your application.
Canvas
2 The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your
application.
Checkbutton
3 The Checkbutton widget is used to display a number of options as checkboxes. The user can select
multiple options at a time.
Entry
4
The Entry widget is used to display a single-line text field for accepting values from a user.
Frame
5
The Frame widget is used as a container widget to organize other widgets.
Label
6 The Label widget is used to provide a single-line caption for other widgets. It can also contain
images.
Listbox
7
The Listbox widget is used to provide a list of options to a user.
Menubutton
8
The Menubutton widget is used to display menus in your application.
Menu
9 The Menu widget is used to provide various commands to a user. These commands are contained
inside Menubutton.
Message
10
The Message widget is used to display multiline text fields for accepting values from a user.
Radiobutton
11 The Radiobutton widget is used to display a number of options as radio buttons. The user can select
only one option at a time.
Scale
12
The Scale widget is used to provide a slider widget.
Scrollbar
13
The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.
Text
14
The Text widget is used to display text in multiple lines.
Toplevel
15
The Toplevel widget is used to provide a separate window container.
Spinbox
16 The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select
from a fixed number of values.
PanedWindow
17 A PanedWindow is a container widget that may contain any number of panes, arranged
horizontally or vertically.
LabelFrame
18 A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for
complex window layouts.
tkMessageBox
19
This module is used to display message boxes in your applications.
Let us study these widgets in detail.
Standard Attributes
Let us look at how some of the common attributes, such as sizes, colors and fonts are specified.
Dimensions
Colors
Fonts
Anchors
Relief styles
Bitmaps
Cursors
Let us study them briefly −
Geometry Management
All Tkinter widgets have access to the specific geometry management methods, which have the purpose
of organizing widgets throughout the parent widget area. Tkinter exposes the following geometry
manager classes: pack, grid, and place.
The pack() Method − This geometry manager organizes widgets in blocks before placing them in
the parent widget.
The grid() Method − This geometry manager organizes widgets in a table-like structure in the
parent widget.
The place() Method − This geometry manager organizes widgets by placing them in a specific
position in the parent widget.
Let us study the geometry management methods briefly −
SimpleDialog
The simpledialog module in tkinter package includes a dialog class and convenience functions for
accepting user input through a modal dialog. It consists of a label, an entry widget and two buttons Ok
and Cancel. These functions are −
askfloat(title, prompt, **kw) − Accepts a floating point number.
askinteger(title, prompt, **kw) − Accepts an integer input.
askstring(title, prompt, **kw) − Accepts a text input from the user.
The above three functions provide dialogs that prompt the user to enter a value of the desired type. If Ok
is pressed, the input is returned, if Cancel is pressed, None is returned.
askinteger
from tkinter.simpledialog import askinteger
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
def show():
num = askinteger("Input", "Input an Integer")
print(num)
top.mainloop()
It will produce the following output −
askfloat
from tkinter.simpledialog import askfloat
from tkinter import *
top = Tk()
top.geometry("100x100")
def show():
num = askfloat("Input", "Input a floating point number")
print(num)
top.mainloop()
It will produce the following output −
askstring
from tkinter.simpledialog import askstring
from tkinter import *
top = Tk()
top.geometry("100x100")
def show():
name = askstring("Input", "Enter you name")
print(name)
top.mainloop()
It will produce the following output −
top.geometry("100x100")
def show():
filename = askopenfile()
print(filename)
top.mainloop()
It will produce the following output −
ColorChooser
The colorchooser module included in tkinter package has the feature of letting the user choose a desired
color object through the color dialog. The askcolor() function presents with the color dialog with
predefined color swatches and facility to choose custome color by setting RGB values. The dialog returns a
tuple of RGB values of chosen color as well as its hex value.
from tkinter.colorchooser import askcolor
from tkinter import *
top = Tk()
top.geometry("100x100")
def show():
color = askcolor()
print(color)
top.mainloop()
It will produce the following output −
top = Tk()
top.geometry("200x150")
frame = Frame(top)
frame.pack()
Progressbar
The ttk ProgressBar widget, and how it can be used to create loading screens or show the progress of a
current task.
Syntax
ttk.Progressbar(parent, orient, length, mode)
Parameters
Parent − The container in which the ProgressBar is to be placed, such as root or a Tkinter frame.
Orient − Defines the orientation of the ProgressBar, which can be either vertical of horizontal.
Length − Defines the width of the ProgressBar by taking in an integer value.
Mode − There are two options for this parameter, determinate and indeterminate.
Example
The code given below creates a progressbar with three buttons which are linked to three different
functions.
The first function increments the "value" or "progress" in the progressbar by 20. This is done with the
step() function which takes an integer value to change progress amount. (Default is 1.0)
The second function decrements the "value" or "progress" in the progressbar by 20.
The third function prints out the current progress level in the progressbar.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
frame= ttk.Frame(root)
def increment():
progressBar.step(20)
def decrement():
progressBar.step(-20)
def display():
print(progressBar["value"])
frame.pack(padx = 5, pady = 5)
root.mainloop()
It will produce the following output −
Notebook
Tkinter ttk module has a new useful widget called Notebook. It is a of collection of of containers (e.g
frames) which have many widgets as children inside.
Each "tab" or "window" has a tab ID associated with it, which is used to determine which tab to swap to.
You can swap between these containers like you would on a regular text editor.
Syntax
notebook = ttk.Notebook(master, *options)
Example
In this example, add 3 windows to our Notebook widget in two different ways. The first method involves
the add() function, which simply appends a new tab to the end. The other method is the insert() function
which can be used to add a tab to a specific position.
The add() function takes one mandatory parameter which is the container widget to be added, and the
rest are optional parameters such as text (text to be displayed as tab title), image and compound.
The insert() function requires a tab_id, which defines the location where it should be inserted. The tab_id
can be either an index value or it can be string literal like "end", which will append it to the end.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
nb = ttk.Notebook(root)
# Frame 1 and 2
frame1 = ttk.Frame(nb)
frame2 = ttk.Frame(nb)
frame3 = ttk.Frame(nb)
label3 = ttk.Label(frame3, text = "This is Window Three")
label3.pack(pady = 50, padx = 20)
frame3.pack(fill= tk.BOTH, expand=True)
nb.insert("end", frame3, text = "Window 3")
nb.pack(padx = 5, pady = 5, expand = True)
root.mainloop()
It will produce the following output −
Treeview
The Treeview widget is used to display items in a tabular or hierarchical manner. It has support for
features like creating rows and columns for items, as well as allowing items to have children as well,
leading to a hierarchical format.
Syntax
tree = ttk.Treeview(container, **options)
Options
Sr.No. Option & Description
columns
1
A list of column names
displaycolumns
2 A list of column identifiers (either symbolic or integer indices) specifying which data columns are
displayed and the order in which they appear, or the string "#all".
height
3
The number of rows visible.
padding
4
Specifies the internal padding for the widget. Can be either an integer or a list of 4 values.
selectmode
One of "extended", "browse" or "none". If set to "extended" (default), multiple items can be
5
selected. If "browse", only a single item can be selected at a time. If "none", the selection cannot
be changed by the user.
show
6 A list containing zero or more of the following values, specifying which elements of the tree to
display. The default is "tree headings", i.e., show all elements.
Example
In this example we will create a simple Treeview ttk Widget and fill in some data into it. We have some
data already stored in a list which will be reading and adding to the Treeview widget in our read_data()
function.
We first need to define a list/tuple of column names. We have left out the column "Name" because there
already exists a (default) column with a blank name.
We then assign that list/tuple to the columns option in Treeview, followed by defining the "headings",
where the column is the actual column, whereas the heading is just the title of the column that appears
when the widget is displayed. We give each a column a name. "#0" is the name of the default column.
The tree.insert() function has the following parameters −
Parent − which is left as an empty string if there is none.
Position − where we want to add the new item. To append, use tk.END
Iid − which is the item ID used to later track the item in question.
Text − to which we will assign the first value in the list (the name).
Value we will pass the the other 2 values we obtained from the list.
The Complete Code
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import simpledialog
root = tk.Tk()
data = [
["Bobby",26,20000],
["Harrish",31,23000],
["Jaya",18,19000],
["Mark",22, 20500],
]
index=0
def read_data():
for index, line in enumerate(data):
tree.insert('', tk.END, iid = index,
text = line[0], values = line[1:])
columns = ("age", "salary")
tree.heading('#0', text='Name')
tree.heading('age', text='Age')
tree.heading('salary', text='Salary')
read_data()
root.mainloop()
It will produce the following output −
Sizegrip
The Sizegrip widget is basically a small arrow-like grip that is typically placed at the bottom-right corner of
the screen. Dragging the Sizegrip across the screen also resizes the container to which it is attached to.
Syntax
sizegrip = ttk.Sizegrip(parent, **options)
Example
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.geometry("100x100")
frame = ttk.Frame(root)
label = ttk.Label(root, text = "Hello World")
label.pack(padx = 5, pady = 5)
sizegrip = ttk.Sizegrip(frame)
sizegrip.pack(expand = True, fill = tk.BOTH, anchor = tk.SE)
frame.pack(padx = 10, pady = 10, expand = True, fill = tk.BOTH)
root.mainloop()
It will produce the following output −
Separator
The ttk Separator widget is a very simple widget, that has just one purpose and that is to help "separate"
widgets into groups/partitions by drawing a line between them. We can change the orientation of this
line (separator) to either horizontal or vertical, and change its length/height.
Syntax
separator = ttk.Separator(parent, **options)
The "orient", which can either be tk.VERTICAL or tk.HORIZTONAL, for a vertical and horizontal separator
respectively.
Example
Here we have created two Label widgets, and then created a Horizontal Separator between them.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.geometry("200x150")
frame = ttk.Frame(root)
root.mainloop()
It will produce the following output −
Python - Command-Line Arguments
To run a Python program, we execute the following command in the command prompt terminal of the
operaing system. For example, in windows, the following command is entered in Windows command
prompt terminal.
The line in front of the command prompt C:\> ( or $ in case of Linux operating system) is called as
command-line.
If the program needs to accept input from the user, Python's input() function is used. When the program
is executed from command line, user input is accepted from the command terminal.
Example
name = input("Enter your name: ")
print ("Hello {}. How are you?".format(name))
The program is run from the command prompt terminal as follows −
Very often, you may need to put the data to be used by the program in the command line itself and use it
inside the program. An example of giving the data in the command line could be any DOS commands in
Windows or Linux.
In Windows, you use the following DOS command to rename a file hello.py to hi.py.
C:\Python311>ren hello.py hi.py
In Linux you may use the mv command −
$ mv hello.py hi.py
Here ren or mv are the commands which need the old and new file names. Since they are put in line with
the command, they are called command-line arguments.
You can pass values to a Python program from command line. Python collects the arguments in a list
object. Python's sys module provides access to any command-line arguments via the sys.argv variable.
sys.argv is the list of command-line arguments and sys.argv[0] is the program i.e. the script name.
The hello.py script used input() function to accept user input after the script is run. Let us change it to
accept input from command line.
import sys
print ('argument list', sys.argv)
name = sys.argv[1]
print ("Hello {}. How are you?".format(name))
Run the program from command-line as shown in the following figure −
Docstring as Comment
A string literal appearing anywhere other than these objects (function, method, class, module or package)
is ignored by the interpreter, hence they are similar to comments (which start with # symbol).
# This is a comment
print ("Hello World")
'''This is also a comment'''
print ("How are you?")
Python - JSON
JSON stands for JavaScript Object Notation. It is a lightweight data interchange format. It is similar to
pickle. However, pickle serialization is Python specific whereas JSON format is implemented by many
languages. The json module in Python's standard library implements object serialization functionality that
is similar to pickle and marshal modules.
Just as in pickle module, the json module also provides dumps() and loads() function for serialization of
Python object into JSON encoded string, and dump() and load() functions write and read serialized Python
objects to/from file.
dumps() − This function converts the object into JSON format.
loads() − This function converts a JSON string back to Python object.
The following example the demonstrates basic usage of these functions −
Example 1
import json
data=['Rakesh',{'marks':(50,60,70)}]
s=json.dumps(data)
print (s, type(s))
data = json.loads(s)
print (data, type(data))
It will produce the following output −
["Rakesh", {"marks": [50, 60, 70]}] <class 'str'>
['Rakesh', {'marks': [50, 60, 70]}] <class 'list'>
The dumps() function can take optional sort_keys argument. By default it is False. If set to True, the
dictionary keys appear in sorted order in the JSON string.
data=['Rakesh',{'marks':(50,60,70)}]
s=json.dumps(data, sort_keys=True)
Example 2
The dumps() function has another optional parameter called indent which takes a number as value. It
decides length of each segment of formatted representation of json string, similar to pprint output.
import json
data=['Rakesh',{'marks':(50,60,70)}]
s=json.dumps(data, indent = 2)
print (s)
It will produce the following output −
[
"Rakesh",
{
"marks": [
50,
60,
70
]
}
]
The json module also has object-oriented API corresponding to above functions. There are two classes
defined in the module − JSONEncoder and JSONDecoder.
JSONEncoder Class
Object of this class is encoder for Python data structures. Each Python data type is converted in
corresponding JSON type as shown in following table −
Python JSON
Dict object
list, tuple array
Str string
int, float, int- & float-derived Enums number
True true
False false
None null
The JSONEncoder class is instantiated by JSONEncoder() constructor. Following important methods are
defined in encoder class −
encode() − serializes Python object into JSON format.
iterencode() − Encodes the object and returns an iterator yielding encoded form of each item in
the object.
indent − Determines indent level of encoded string.
sort_keys − is either true or false to make keys appear in sorted order or not.
check_circular − if True, check for circular reference in container type object.
The following example encodes Python list object.
Example
import json
data=['Rakesh',{'marks':(50,60,70)}]
e=json.JSONEncoder()
Using iterencode() method, each part of the encoded string is displayed as below −
import json
data=['Rakesh',{'marks':(50,60,70)}]
e=json.JSONEncoder()
for obj in e.iterencode(data):
print (obj)
It will produce the following output −
["Rakesh"
,
{
"marks"
:
[50
, 60
, 70
]
}
]
JSONDEcoder class
Object of this class helps in decoded in json string back to Python data structure. Main method in this
class is decode(). Following example code retrieves Python list object from encoded string in earlier step.
Example
import json
data=['Rakesh',{'marks':(50,60,70)}]
e=json.JSONEncoder()
s = e.encode(data)
d=json.JSONDecoder()
obj = d.decode(s)
print (obj, type(obj))
It will produce the following output −
['Rakesh', {'marks': [50, 60, 70]}] <class 'list'>
JSON with Files/Streams
The json module defines load() and dump() functions to write JSON data to a file like object − which may
be a disk file or a byte stream and read data back from them.
dump() Function
This function encodes Python object data in JSON format and writes it to a file. The file must be having
write permission.
Example
import json
data=['Rakesh', {'marks': (50, 60, 70)}]
fp=open('json.txt','w')
json.dump(data,fp)
fp.close()
This code will create 'json.txt' in current directory. It shows the contents as follows −
["Rakesh", {"marks": [50, 60, 70]}]
load() Function
This function loads JSON data from the file and constructs Python object from it. The file must be opened
with read permission.
Example
import json
fp=open('json.txt','r')
ret=json.load(fp)
print (ret)
Python - Sending Email
An application that handles and delivers e-mail over the Internet is called a "mail server". Simple Mail
Transfer Protocol (SMTP) is a protocol, which handles sending an e-mail and routing e-mail between mail
servers. It is an Internet standard for email transmission.
Python provides smtplib module, which defines an SMTP client session object that can be used to send
mails to any Internet machine with an SMTP or ESMTP listener daemon.
smptlib.SMTP() Function
To send an email, you need to obtain the object of SMTP class with the following function −
import smtplib
def prompt(prompt):
return input(prompt).strip()
If everything goes well, execute the above script. The message should be delivered to the recipient's
inbox.
Python - Further Extensions
Any code that you write using any compiled language like C, C++, or Java can be integrated or imported
into another Python script. This code is considered as an "extension."
A Python extension module is nothing more than a normal C library. On Unix machines, these libraries
usually end in .so (for shared object). On Windows machines, you typically see .dll (for dynamically linked
library).
Pre-Requisites for Writing Extensions
To start writing your extension, you are going to need the Python header files.
On Unix machines, this usually requires installing a developer-specific package.
Windows users get these headers as part of the package when they use the binary Python
installer.
Additionally, it is assumed that you have a good knowledge of C or C++ to write any Python Extension
using C programming.
First look at a Python Extension
For your first look at a Python extension module, you need to group your code into four parts −
The header file Python.h.
The C functions you want to expose as the interface from your module..
A table mapping the names of your functions as Python developers see them as C functions inside
the extension module..
An initialization function.
The Header File Python.h
You need to include Python.h header file in your C source file, which gives you the access to the internal
Python API used to hook your module into the interpreter.
Make sure to include Python.h before any other headers you might need. You need to follow the includes
with the functions you want to call from Python.
The C Functions
The signatures of the C implementation of your functions always takes one of the following three forms −
static PyObject *MyFunction(PyObject *self, PyObject *args);
static PyObject *MyFunctionWithKeywords(PyObject *self,
PyObject *args,
PyObject *kw);
static PyObject *MyFunctionWithNoArgs(PyObject *self);
Each one of the preceding declarations returns a Python object. There is no such thing as a void function
in Python as there is in C. If you do not want your functions to return a value, return the C equivalent of
Python's None value. The Python headers define a macro, Py_RETURN_NONE, that does this for us.
The names of your C functions can be whatever you like as they are never seen outside of the extension
module. They are defined as static function.
Your C functions usually are named by combining the Python module and function names together, as
shown here −
static PyObject *module_func(PyObject *self, PyObject *args) {
/* Do your stuff here. */
Py_RETURN_NONE;
}
This is a Python function called func inside the module module. You will be putting pointers to your C
functions into the method table for the module that usually comes next in your source code.
The Method Mapping Table
This method table is a simple array of PyMethodDef structures. That structure looks something like this −
struct PyMethodDef {
char *ml_name;
PyCFunction ml_meth;
int ml_flags;
char *ml_doc;
};
Here is the description of the members of this structure −
ml_name − This is the name of the function as the Python interpreter presents when it is used in
Python programs.
ml_meth − This is the address of a function that has any one of the signatures, described in the
previous section.
ml_flags − This tells the interpreter which of the three signatures ml_meth is using.
o This flag usually has a value of METH_VARARGS.
o This flag can be bitwise OR'ed with METH_KEYWORDS if you want to allow keyword
arguments into your function.
o This can also have a value of METH_NOARGS that indicates you do not want to accept any
arguments.
mml_doc − This is the docstring for the function, which could be NULL if you do not feel like
writing one.
This table needs to be terminated with a sentinel that consists of NULL and 0 values for the appropriate
members.
Example
For the above-defined function, we have the following method mapping table −
static PyMethodDef module_methods[] = {
{ "func", (PyCFunction)module_func, METH_NOARGS, NULL },
{ NULL, NULL, 0, NULL }
};
The Initialization Function
The last part of your extension module is the initialization function. This function is called by the Python
interpreter when the module is loaded. It is required that the function be named initModule, where
Module is the name of the module.
The initialization function needs to be exported from the library you will be building. The Python headers
define PyMODINIT_FUNC to include the appropriate incantations for that to happen for the particular
environment in which we are compiling. All you have to do is use it when defining the function.
Your C initialization function generally has the following overall structure −
PyMODINIT_FUNC initModule() {
Py_InitModule3(func, module_methods, "docstring...");
}
Here is the description of Py_InitModule3 function −
func − This is the function to be exported.
module_methods − This is the mapping table name defined above.
docstring − This is the comment you want to give in your extension.
Putting all this together, it looks like the following −
#include <Python.h>
static PyObject *module_func(PyObject *self, PyObject *args) {
/* Do your stuff here. */
Py_RETURN_NONE;
}
static PyMethodDef module_methods[] = {
{ "func", (PyCFunction)module_func, METH_NOARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initModule() {
Py_InitModule3(func, module_methods, "docstring...");
}
Example
A simple example that makes use of all the above concepts −
#include <Python.h>
static PyObject* helloworld(PyObject* self)
{
return Py_BuildValue("s", "Hello, Python extensions!!");
}
static char helloworld_docs[] =
"helloworld( ): Any message you want to put here!!\n";
static PyMethodDef helloworld_funcs[] = {
{"helloworld", (PyCFunction)helloworld,
METH_NOARGS, helloworld_docs},
{NULL}
};
void inithelloworld(void)
{
Py_InitModule3("helloworld", helloworld_funcs,
"Extension module example!");
}
Here the Py_BuildValue function is used to build a Python value. Save above code in hello.c file. We would
see how to compile and install this module to be called from Python script.
Building and Installing Extensions
The distutils package makes it very easy to distribute Python modules, both pure Python and extension
modules, in a standard way. Modules are distributed in the source form, built and installed via a setup
script usually called setup.pyas.
For the above module, you need to prepare the following setup.py script −
from distutils.core import setup, Extension
setup(name='helloworld', version='1.0', \
ext_modules=[Extension('helloworld', ['hello.c'])])
Now, use the following command, which would perform all needed compilation and linking steps, with
the right compiler and linker commands and flags, and copies the resulting dynamic library into an
appropriate directory −
$ python setup.py install
On Unix-based systems, you will most likely need to run this command as root in order to have
permissions to write to the site-packages directory. This usually is not a problem on Windows.
Importing Extensions
Once you install your extensions, you would be able to import and call that extension in your Python
script as follows −
import helloworld
print helloworld.helloworld()
This would produce the following output −
Hello, Python extensions!!
Passing Function Parameters
As you will most likely want to define functions that accept arguments, you can use one of the other
signatures for your C functions. For example, the following function, that accepts some number of
parameters, would be defined like this −
static PyObject *module_func(PyObject *self, PyObject *args) {
/* Parse args and do something interesting here. */
Py_RETURN_NONE;
}
The method table containing an entry for the new function would look like this −
static PyMethodDef module_methods[] = {
{ "func", (PyCFunction)module_func, METH_NOARGS, NULL },
{ "func", module_func, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
You can use the API PyArg_ParseTuple function to extract the arguments from the one PyObject pointer
passed into your C function.
The first argument to PyArg_ParseTuple is the args argument. This is the object you will be parsing. The
second argument is a format string describing the arguments as you expect them to appear. Each
argument is represented by one or more characters in the format string as follows.
static PyObject *module_func(PyObject *self, PyObject *args) {
int i;
double d;
char *s;
if (!PyArg_ParseTuple(args, "ids", &i, &d, &s)) {
return NULL;
}
4 4 LOAD_CONST 2 (20)
6 STORE_FAST 1 (varb)
6 8 LOAD_FAST 0 (vara)
10 LOAD_FAST 1 (varb)
12 BINARY_ADD
14 STORE_FAST 2 (sum)
7 16 LOAD_GLOBAL 0 (print)
18 LOAD_CONST 3 ('vara + varb = %d')
20 LOAD_FAST 2 (sum)
22 BINARY_MODULO
24 CALL_FUNCTION 1
26 POP_TOP
28 LOAD_CONST 0 (None)
30 RETURN_VALUE
The pdb Module
The pdb module is the standard Python debugger. It is based on the bdb debugger framework.
You can run the debugger from the command line (type n [or next] to go to the next line and help to get a
list of available commands) −
Example
Before you try to run pdb.py, set your path properly to Python lib directory. So let us try with above
example sum.py −
$pdb.py sum.py
> /test/sum.py(3)<module>()
-> import dis
(Pdb) n
> /test/sum.py(5)<module>()
-> def sum():
(Pdb) n
>/test/sum.py(14)<module>()
-> dis.dis(sum)
(Pdb) n
6 0 LOAD_CONST 1 (10)
3 STORE_FAST 0 (vara)
7 6 LOAD_CONST 2 (20)
9 STORE_FAST 1 (varb)
9 12 LOAD_FAST 0 (vara)
15 LOAD_FAST 1 (varb)
18 BINARY_ADD
19 STORE_FAST 2 (sum)
A new editor window opens in which you can enter the Python code. Save it and run it with Run menu.
Jupyter Notebook
Initially developed as a web interface for IPython, Jupyter Notebook supports multiple languages. The
name itself derives from the alphabets from the names of the supported languages − Julia, PYThon and R.
Jupyter notebook is a client server application. The server is launched at the localhost, and the browser
acts as its client.
Install Jupyter notebook with PIP −
pip3 install jupyter
Invoke from the command line.
C:\Users\Acer>jupyter notebook
The server is launched at localhost's 8888 port number.
The default browser of your system opens a link http://localhost:8888/tree to display the dashboard.
Open a new Python notebook. It shows IPython style input cell. Enter Python instructions and run the cell.
Jupyter notebook is a versatile tool, used very extensively by data scientists to display inline data
visualizations. The notebook can be conveniently converted and distributed in PDF, HTML or Markdown
format.
VS Code
Microsoft has developed a source code editor called VS Code (Visual Studio Code) that supports multiple
languages including C++, Java, Python and others. It provides features such as syntax highlighting,
autocomplete, debugger and version control.
VS Code is a freeware. It is available for download and install from https://code.visualstudio.com/.
Launch VS Code from the start menu (in Windows).
After activating Python extension, you need to set the Python interpreter. Press Ctrl+Shift+P and select
Python interpreter.
Open a new text file, enter Python code and save the file.
PyCharm
PyCharm is another popular Python IDE. It has been developed by JetBrains, a Czech software company.
Its features include code analysis, a graphical debugger, integration with version control systems etc.
PyCharm supports web development with Django.
The community as well as professional editions can be downloaded from
https://www.jetbrains.com/pycharm/download.
Download, install the latest Version: 2022.3.2 and open PyCharm. The Welcome screen appears as below
−
When you start a new project, PyCharm creates a virtual environment for it based on the choice of folder
location and the version of Python interpreter chosen.
You can now add one or more Python scripts required for the project. Here we add a sample Python code
in main.py file.
To execute the program, choose from Run menu or use Shift+F10 shortcut.
Introduction
What is Programming?
To communicate with people, we use one of the comfortable languages similarly when it comes to
communicating with computers we use one of the programming languages such as Python, C, C++ and so.
A programmer instructs a computer to perform a task through a programming language.
What is Python?
Features of Python
Easy to understand
Open Source
High-level language
Portable
Modules: A module is a file containing written code that can be imported and used in our program.
Pip: Pip is the package manager for python you can use ‘pip’ to install a module on your computer.
for example, pip install plyer
Types of Modules
Comments :
Comments are used in the program to add descriptions or brief details of what the program does, it
makes a more readable for others.
Types of comments
Operators in Python
type() function is used to find the data type of a given variable in python.
For example : x = 87
type(a) → class
x =’87’
type(a) → class
A number can be converted into a string and vice versa (if possible)
There are many functions to convert one data type into another.
str(31) → ‘31’
int(’31’) → 31
float(31) → 31.0
input function
This function allows the user to take input from the keyboard as a string.
a = input(”Enter a value”)
Note: The output of input is always a string ( even if numeric data is entered)
Strings
The string is a data type in python that stores a group of characters enclosed in quotes.
We can write a string in the following ways
Single quoted strings → m = ‘Hello’
Double quoted strings → m = “Hello”
Triple quoted strings → m = ‘’’ Hello’’’
String Slicing
String Function
len() function
string.endwith()
string.count()
string.capitalize()
string.find()
string.replace()
Tech startups use Circuit to rapidly scale awareness and adoption. Is your startup ready to scale?
Learn more
char = 'Shamim'# String functions
print(len(char)) # returns length of string with space
print(char.endswith("im")) # returns true if arguments matches
print(char.find('a')) # finds 'a' in whole string
print(char.count('m')) # returns no. of occurance of arguments
print(char.replace("Shamim", "Sameer")) # replace the old one with new value
print(char.capitalize()) #converts the first charcter into uppercase (if not)
In python, List and tuples are to store a set of values of any data type.
List
mt = [] # Empty list
print(mt)mylist = ['apple',4,7.9,False] # Any data types can be stored in list collectively
print(mylist)mylist[1] = 'hello' # Can be overwritten
print(mylist)print(mylist[3]) # Accessing list by using its index
List methods
Tuple
t1 = () # Empty tuple
print(t1)mytuple = ('Python', 123, 34.4 ,True) # Any datatypes can be stored into one tuple
print(mytuple)
# mytuple[1] = '9' # We cannot overwrite
# print(mytuple)t = (1,3,3) # Accessing tuple's value by using its index
print(t[2])
Tuple methods
1. It is unordered
2. It is mutable
3. It is indexed
4. It cannot contain duplicate keys
Dictionary Methods
Set
Properties of set
Conditional Expressions
In real life, we want to make things happen if a particular becomes TRUE. Let’s take an example
sometimes we decide if it’s 9 ‘O clock, I will take breakfast.
Syntax :
if (condition1) :
#Code
elif(condition2) :
#Code
else :
#Code
Loop
Sometimes we need to repeat a set of codes until a given condition meet. So for that, we need LOOP.
In python, we have two types of the loop here
1. While Loop
2. For loop
While Loop
With the while loop, we can execute a set of statements as long as a condition is true.
While syntax :
While condition :
#Body of the loop# While Loop
i=1
while i <= 50 :
print(i)
i=i+1
For Loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
This is less like the for a keyword in other programming languages and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop, we can execute a set of statements, once for each item in a list, tuple, set, etc.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Range function
To loop through a set of code a specified number of times, we can use the range() function.
The range() function returns a sequence of numbers, starting from 0 by default, increments by 1
(by default), and ends at a specified number.
num = int(input("Enter a number :"))
for i in range(10,0,-1) : # (start, stop, step size)
table = num * i
print(table)
Break: With the break statement we can stop the loop even if the while condition is true:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
Continue: With the continue statement we can stop the current iteration, and continue with the next:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Function
#code
Function definition: The part where the function is defined such as what argument takes a function or
what it returns using the ‘def’ keyword.
def sum(a,b) : # funtion definition
return a + b # returns value
Function call: Whenever a programmer wants to call a function. he/she put the function’s name followed
by parenthesis ()
sum = sum(2,4) # function call
print(sum)
1. Built-in function : It’s already present in python for instance print(), input(), etc
2. User-defined function: It’s defined by the user itself for instance sum in the above program.
A function can take arguments so that it can work with. A programmer puts these values when he/she
calls the function. for instance, in the following program ‘sum’ takes two arguments‘ a’ and ‘b’ so that it
can return the sum of a and b.
def sum(a,b) : # funtion definition
return a + b # returns valuesum = sum(2,4) # function call
print(sum)
In function, the default parameter is given so that if no argument is given by the programmer then it’ll
consider the default parameter as an argument.
def company(company_name='Google') :
print(company_name)company('Facebook') # Argument's given so it'll print 'Facebook'
company() # No Argument's given so it'll print 'Google'
Recursion
Recursion is a function that calls itself to solve a particular problem. It’s used to directly use a
mathematical formula as a function.
For example :
factorial (n) = n x factorial (n-1)
This function can be defined as follows :
def factorial (n)
if i ==0 or i == 1 :
return 1
else :
return n * factorial (n-1) # Function will call itself
Files I/O
So as we know RAM ( Random Access Memory) is volatile which means once you power off your
computer or program terminates. All the data stored in it will be erased. So in order to store permanently,
we use files. Data of a file is stored in a storage device.
A programmer can perform certain operations on a file such as reading, writing, and appending a file.
Types of Files
To open a file in python we use ‘open()’ function. It takes two parameters. The syntax is given below
Syntax :
open(”file_name”, “reading_mode”)
We can also use the f.readline() function to read on full line at a time
f.readline() # Reads one line from the file
To write in a file in python open that in write mode and then use f.write() to write.
filename = 'D:\\Python\\Practice set\\Biodata.txt'
f = open(filename,"w")
text = f.write("Hello")
print(text)
f.close()
With statement
With the above method, a programmer has to close the file manually. So, The best way to open and close
the file automatically is the ‘with’ statement.
filename = 'D:\\Python\\Practice set\\Biodata.txt'
with open (filename) as f:
text = f.read()
print(text)
Python Programming for Class 10 AI CBSE Notes
When you instruct a computer to perform specific tasks then this is called Computer Programming.
Programming Languages
We learned that we need to Instruct a computer to perform certain tasks and this is called Programming,
But How to Program a machine? How do we instruct computers?
Well, How does your teacher instruct you, Simply she uses a language as a medium because you and the
teacher both know the language like English. But what if one of you doesn’t understand that language, in
that case, you need to use another language which you both know.
This is where the Concept of Programming languages comes, programming language is a formal language
designed to communicate instructions to a computer.
Binary or Machine Code
We know that Computers do not understand the language which we speak, like English. Computers
understand Only understand Machine language, also known as Binary Language, technically speaking
this is the only language which computers understand… for example “01001000 01100101 01101100
01101100 01101111 00100001”
Those ones and zeros might not look like anything to you, but in binary code, these numbers are actually
saying “Hello!”.
Here’s how a processor works, the processor already contains the circuit to control all of these
instructions, but the correct circuitry gets connected together when, the instruction is provided for that,
for example, certain transistors will open or close with 1s or 0s, because as we discussed 0 means false
and 1 means true.
If everything was a bouncer for you then it’s totally fine you will be able to understand these concepts
clearly in the future,
so here is the representation of mostly used letters and symbols in machine code or binary code.
CONCLUSION:-
So as we discussed, machines can’t understand English or any other language, they only know machine
language that is in 0s and 1s and we cant use it as a medium between us and computers, as u saw we
need to write a lot for a few words to be printed, like hello. So it isn’t efficient for us to do that.
Assembly Language
The Problem: We need to program the machine but it’s not easy to learn the Binary Code or Machine
language
The Solution: Due to this Assembly language was Introduced as a first step to improve programming
structure and make machine language more understandable by humans.
Features of Assembly Language
hide
1. First step to improve programming structure and make machine language more understandable
by humans.
2. This is the second generation of computer language
3. Replacing the 0s and 1s this language used a set of symbols and letters.
4. Uses Assembler for converting Assembly lanuguage code to machine level code.
5. Example: To add or subtract it uses to add or sub respectively
But wait we discussed that computer can only understand machine language so how it is going to
understand Assembly Language because it’s not in 0s and 1s, well it is simple, for example when
presidents of 2 countries want to talk to each other considering the fact that they don’t understand each
other’s language then you know how do they communicate? if you said translators, then you are
absolutely correct, they have translators who understand both languages and can convert one language
to another.
Similarly, Assembly language can be translated or rather converted into Machine level language using
Assemblers. So assemblers act as a translator between assembly language and machine-level language.
High Level Language
Problems with Assembly Language: Assembly language was not as hard as Machine Level Language but it
was still hard for many of us, to understand the difference we can see the same Hello world code in
assembly language also.
So We need to find another language that is more human-understandable and simple to code.
Here comes the programming languages like Python, Java, C, C++. and these languages are called High-
Level Languages.
Which are way simpler to understand, to just compare Machine level language, assembly language, and
High-level languages we can see the same hello world program in python also.
# An expression in braces {} (i.e., associative array) can also span multiple lines
>>> x = {'name':'Peter',
'gender':'male',
'age':21
} # Re-assign a dictionary denoted as {k1:v1, k2:v2,...} to variable x
>>> x
{'name': 'Peter', 'gender': 'male', 'age': 21}
# You can break a long string into several lines with parentheses () too
>>> s = ('testing ' # No commas
'hello, '
'world!')
>>> s
'testing hello, world!'
Block, Indentation and Compound Statements
A block is a group of statements executing as a unit. Unlike C/C++/C#/Java, which use braces {} to group
statements in a body block, Python uses indentation for body block. In other words, indentation is
syntactically significant in Python - the body block must be properly indented. This is a good syntax to
force you to indent the blocks correctly for ease of understanding!!!
A compound statement, such as conditional (if-else), loop (while, for) and function definition (def), begins
with a header line terminated with a colon (:); followed by the indented body block, as follows:
header_1: # Headers are terminated by a colon
statement_1_1 # Body blocks are indented (recommended to use 4 spaces)
statement_1_2
......
header_2:
statement_2_1
statement_2_2
......
# You can place the body-block in the same line, separating the statement by semi-colon (;)
# This is NOT recommended.
header_1: statement_1_1
header_2: statement_2_1; statement_2_2; ......
For examples,
# if-else
x=0
if x == 0:
print('x is zero')
else:
print('x is not zero')
As mentioned, Python is dynamic typed. Python associates types with the objects, not the variables, i.e., a
variable can hold object of any types, as shown in the above examples.
Rules of Identifier (Names)
An identifier starts with a letter (A-Z, a-z) or an underscore (_), followed by zero or more letters,
underscores and digits (0-9). Python does not allow special characters such as $ and @.
By convention, Variables start with an underscore (_) are private variables.
Keywords
Python 3 has 35 reserved words (or keywords), which cannot be used as identifiers.
True, False, None (boolean and special literals)
import, as, from
if, elif, else, for, in, while, break, continue, pass, with (flow control)
def, return, lambda, global, nonlocal (function)
class
and, or, not, is, del (operators)
try, except, finally, raise, assert (error handling)
await, async, yield
Variable Naming Convention
A variable name is a noun, or a noun phrase made up of several words. There are two conventions:
1. In lowercase words and optionally joined with underscore if it improves readability, e.g.,
num_students, x_max, myvar, isvalid, etc.
2. In the so-called camel-case where the first word is in lowercase, and the remaining words are
initial-capitalized, e.g., numStudents, xMax, yMin, xTopLeft, isValidInput, and
thisIsAVeryVeryLongVariableName.
Camel-case is the Java's naming convention; while lowercases joined with underscores is C/C++
convention.
Recommendations
1. It is important to choose a name which is self-descriptive and closely reflects the meaning of the
variable. For example, use numStudents (not n or x), to store the number of students. It is alright
to use abbreviations, e.g., idx for index.
2. Do not use meaningless names like a, b, c, i, j, k, n, i1, i2, i3, j99, exercise85 (what is the purpose of
this exercise?), and example12 (What is this example about?).
3. Avoid single-letter names like i, j, k, a, b, c, which are easier to type but often meaningless.
Exceptions are common names like x, y, z for coordinates, i for index. Long names are harder to
type, but self-document your program. (I suggest you spend sometimes practicing your typing.)
4. Use singular and plural nouns prudently to differentiate between singular and plural variables. For
example, you may use the variable row to refer to a single row number and the variable rows to
refer to many rows (such as a list of rows - to be discussed later).
Constants
Python does not support constants, where its contents cannot be modified.
C/C++ supports constants via keyword const, Java via keyword final.
It is a convention to name a variable in uppercase joined with underscores, e.g., MAX_ROWS,
SCREEN_X_MAX, to indicate that it should not be modified in the program. Nevertheless, nothing
prevents it from being modified in Python.
Data Types: Number, String and List
Python supports various number type such as int (for integers such as 123, -456), float (for floating-point
number such as 3.1416, 1.2e3, -4.5E-6), and bool (for boolean of either True or False).
Python supports text string (a sequence of characters). In Python, strings can be delimited with single-
quotes or double-quotes, e.g., 'hello', "world", '' or "" (empty string).
Python supports a dynamic-array structure called list, denoted as lst = [v1, v2, ..., vn]. You can reference
the i-th element as lst[i]. Python's list is similar to C/C++/Java's array, but it is NOT fixed size, and can be
expanded dynamically during runtime.
I will describe these data types in detail in the later section.
Console Input/Output: input() and print() Built-in Functions
You can use built-in function input() to read input from the keyboard (as a string) and print() to print
output to the console. For example,
>>> x = input('Enter a number: ')
Enter a number: 5
>>> x
'5' # A quoted string
>>> type(x) # Check data type
<class 'str'>
>>> print(x)
5
# Python 2
>>> print('Hello')
Hello
>>> print 'hello'
hello
>>> print('aaa', 'bbb')
('aaa', 'bbb')
# Treated as a tuple (of items). Print the tuple with parentheses
>>> print 'aaa', 'bbb'
aaa bbb
# Treated as multiple arguments
Important: Always use print() function with parentheses, for portability!
Data Types and Dynamic Typing
Python has a large number of built-in data types, such as Numbers (Integer, Float, Boolean, Complex
Number), String, List, Tuple, Set, Dictionary and File. More high-level data types, such as Decimal and
Fraction, are supported by external modules.
You can use the built-in function type(varName) to check the type of a variable or literal.
Number Types
Python supports these built-in number types:
1. Integers (type int): e.g., 123, -456. Unlike C/C++/Java, integers are of unlimited size in Python. For
example,
2. >>> 123 + 456 - 789
3. -210
4. >>> 123456789012345678901234567890 + 1
5. 123456789012345678901234567891
6. >>> 1234567890123456789012345678901234567890 + 1
7. 1234567890123456789012345678901234567891
8. >>> 2 ** 888 # Raise 2 to the power of 888
9. ......
10. >>> len(str(2 ** 888)) # Convert integer to string and get its length
11. 268 # 2 to the power of 888 has 268 digits
12. >>> type(123) # Get the type
13. <class 'int'>
>>> help(int) # Show the help menu for type int
You can also express integers in hexadecimal with prefix 0x (or 0X); in octal with prefix 0o (or 0O);
and in binary with prefix 0b (or 0B). For examples, 0x1abc, 0X1ABC, 0o1776, 0b11000011.
14. Floating-point numbers (type float): e.g., 1.0, -2.3, 3.4e5, -3.4E-5, with a decimal point and an
optional exponent (denoted by e or E). floats are 64-bit double precision floating-point numbers.
For example,
15. >>> 1.23 * -4e5
16. -492000.0
17. >>> type(1.2) # Get the type
18. <class 'float'>
19. >>> import math # Using the math module
20. >>> math.pi
21. 3.141592653589793
22. >>> import random # Using the random module
23. >>> random.random() # Generate a random number in [0, 1)
0.890839384187198
24. Booleans (type bool): takes a value of either True or False. Take note of the spelling in initial-
capitalized.
25. >>> 8 == 8 # Compare
26. True
27. >>> 8 == 9
28. False
29. >>> type(True) # Get type
30. <class 'bool'>
>>> type (8 == 8)
<class 'bool'>
In Python, integer 0, an empty value (such as empty string '', "", empty list [], empty tuple (),
empty dictionary {}), and None are treated as False; anything else are treated as True.
>>> bool(0) # Cast int 0 to bool
False
>>> bool(1) # Cast int 1 to bool
True
>>> bool('') # Cast empty string to bool
False
>>> bool('hello') # Cast non-empty string to bool
True
>>> bool([]) # Cast empty list to bool
False
>>> bool([1, 2, 3]) # Cast non-empty list to bool
True
Booleans can also act as integers in arithmetic operations with 1 for True and 0 for False. For
example,
>>> True + 3
4
>>> False + 1
1
31. Complex Numbers (type complex): e.g., 1+2j, -3-4j. Complex numbers have a real part and an
imaginary part denoted with suffix of j (or J). For example,
32. >>> x = 1 + 2j # Assign variable x to a complex number
33. >>> x # Display x
34. (1+2j)
35. >>> x.real # Get the real part
36. 1.0
37. >>> x.imag # Get the imaginary part
38. 2.0
39. >>> type(x) # Get type
40. <class 'complex'>
41. >>> x * (3 + 4j) # Multiply two complex numbers
(-5+10j)
42. Others: Other number types are provided by external modules, such as decimal module for
decimal fixed-point numbers, fraction module for rational numbers.
43. # floats are imprecise
44. >>> 0.1 * 3
45. 0.30000000000000004
46.
47. # Decimal are precise
48. >>> import decimal # Using the decimal module
49. >>> x = decimal.Decimal('0.1') # Construct a Decimal object
50. >>> x * 3 # Multiply with overloaded * operator
51. Decimal('0.3')
52. >>> type(x) # Get type
<class 'decimal.Decimal'>
Dynamic Typing and Assignment Operator
Recall that Python is dynamic typed (instead of static typed like C/C++/Java).
Python associates type with object, instead of variable. That is, a variable does not have a fixed
type and can be assigned an object of any type. A variable simply provides a reference to an
object.
You do not need to declare a variable before using a variable. A variable is created automatically
when a value is first assigned, which links the assigned object to the variable.
You can use built-in function type(var_name) to get the object type referenced by a variable.
>>> x = 1 # Assign an int value to create variable x
>>> x # Display x
1
>>> type(x) # Get the type of x
<class 'int'>
>>> x = 1.0 # Re-assign a float to x
>>> x
1.0
>>> type(x) # Show the type
<class 'float'>
>>> x = 'hello' # Re-assign a string to x
>>> x
'hello'
>>> type(x) # Show the type
<class 'str'>
>>> x = '123' # Re-assign a string (of digits) to x
>>> x
'123'
>>> type(x) # Show the type
<class 'str'>
Type Casting: int(x), float(x), str(x)
You can perform type conversion (or type casting) via built-in functions int(x), float(x), str(x), bool(x), etc.
For example,
>>> x = '123' # string
>>> type(x)
<class 'str'>
>>> x = int(x) # Parse str to int, and assign back to x
>>> x
123
>>> type(x)
<class 'int'>
>>> x = float(x) # Convert x from int to float, and assign back to x
>>> x
123.0
>>> type(x)
<class 'float'>
>>> x = str(x) # Convert x from float to str, and assign back to x
>>> x
'123.0'
>>> type(x)
<class 'str'>
>>> len(x) # Get the length of the string
5
>>> x = bool(x) # Convert x from str to boolean, and assign back to x
>>> x # Non-empty string is converted to True
True
>>> type(x)
<class 'bool'>
>>> x = str(x) # Convert x from bool to str
>>> x
'True'
In summary, a variable does not associate with a type. Instead, a type is associated with an object. A
variable provides a reference to an object (of a certain type).
Check Instance's Type: isinstance(instance, type)
You can also use the built-in function isinstance(instance, type) to check if the instance belong to the type.
For example,
>>> isinstance(123, int)
True
>>> isinstance('a', int)
False
>>> isinstance('a', str)
True
The Assignment Operator (=)
In Python, you do not need to declare variables before using the variables. The initial assignment creates
a variable and links the assigned value to the variable. For example,
>>> x = 8 # Create a variable x by assigning a value
>>> x = 'Hello' # Re-assign a value (of a different type) to x
# Python 3
>>> round(x) # Round to the nearest integer
1
>>> type(round(x))
<class 'int'>
# Python 2
>>> round(x)
1.0
>>> type(round(x))
<type 'float'>
# Indexing
>>> s[0] # Get character at index 0; index begins at 0
'H'
>>> s[1]
'e'
>>> s[-1] # Get Last character, same as s[len(s) - 1]
'd'
>>> s[-2] # 2nd last character
'l'
# Slicing
>>> s[1:3] # Substring from index 1 (included) to 3 (excluded)
'el'
>>> s[1:-1]
'ello, worl'
>>> s[:4] # Same as s[0:4], from the beginning
'Hell'
>>> s[4:] # Same as s[4:-1], till the end
'o, world'
>>> s[:] # Entire string; same as s[0:len(s)]
'Hello, world'
# str can only concatenate with str, not with int and other types
>>> s = 'hello'
>>> print('The length of \"' + s + '\" is ' + len(s)) # len() is int
TypeError: can only concatenate str (not "int") to str
>>> print('The length of \"' + s + '\" is ' + str(len(s)))
The length of "hello" is 5
# String is immutable
>>> s[0] = 'a'
TypeError: 'str' object does not support item assignment
Character Type?
Python does not have a dedicated character data type. A character is simply a string of length 1. You can
use the indexing operator to extract individual character from a string, as shown in the above example; or
process individual character using for-in loop (to be discussed later).
The built-in functions ord() and chr() operate on character, e.g.,
# ord(c) returns the integer ordinal (Unicode) of a one-character string
>>> ord('A')
65
>>> ord('水')
27700
# chr(i) returns a one-character string with Unicode ordinal i; 0 <= i <= 0x10ffff.
>>> chr(65)
'A'
>>> chr(27700)
'水'
Unicode vs ASCII
In Python 3, strings are defaulted to be Unicode. ASCII strings are represented as byte strings, prefixed
with b, e.g., b'ABC'.
In Python 2, strings are defaulted to be ASCII strings (byte strings). Unicode strings are prefixed with u.
You should always use Unicode for internationalization (i18n)!
String-Specific Member Functions
Python supports strings via a built-in class called str (We will describe class in the Object-Oriented
Programming chapter). The str class provides many member functions. Since string is immutable, most of
these functions return a new string. The commonly-used member functions are as follows, supposing that
s is a str object:
str.strip(), str.rstrip(), str.lstrip(): strip the leading and trailing whitespaces, the right (trailing)
whitespaces; and the left (leading) whitespaces, respectively.
str.upper(), str.lower(): Return a uppercase/lowercase counterpart, respectively.
str.isupper(), str.islower(): Check if the string is uppercase/lowercase, respectively.
str.find(key_str):
str.index(key_str):
str.startswith(key_str):
str.endswith(key_str):
str.split(delimiter_str), delimiter_str.join(strings_list):
>>> dir(str) # List all attributes of the class str
[..., 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal',
'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> s.startswith('Hell')
True
>>> s.endswith('world')
True
>>> s.replace('ll', 'xxx')
'Hexxxo, world'
>>> s.isupper()
False
>>> s.upper()
'HELLO, WORLD'
>>> s.split(', ') # Split into a list with the given delimiter
['Hello', 'world']
>>> ', '.join(['hello', 'world', '123']) # Join all strings in the list using the delimiter
'hello, world, 123'
# You can use 'positional' index in the form of {0}, {1}, ...
>>> '|{0}|{1}|more|'.format('Hello', 'world')
'|Hello|world|more|'
>>> '|{1}|{0}|more|'.format('Hello', 'world')
'|world|Hello|more|'
# dictionary
>>> dict = {'c': 33, 'cc': 33.33}
>>> '|{0[cc]}|{0[c]}|'.format(dict)
'|33.33|33|'
>>> '|{cc}|{c}|'.format(**dict) # As keywords via **
'|33.33|33|'
String Formatting 2: Using str.rjust(n), str.ljust(n), str.center(n), str.zfill(n)
You can also use str's member functions like str.rjust(n) (where n is the field-width), str.ljust(n),
str.center(n), str.zfill(n) to format a string. For example,
# Setting field width and alignment
>>> '123'.rjust(5)
' 123'
>>> '123'.ljust(5)
'123 '
>>> '123'.center(5)
' 123 '
>>> '123'.zfill(5) # Pad (Fill) with leading zeros
'00123'
# Floats
>>> '1.2'.rjust(5)
' 1.2'
>>> '-1.2'.zfill(6)
'-001.2'
String Formatting 3 (Old Style): Using % operator
The old style (in Python 2) is to use the % operator, with C-like printf() format specifiers. For examples,
# %s for str
# %ns for str with field-width of n (default right-align)
# %-ns for left-align
>>> '|%s|%8s|%-8s|more|' % ('Hello', 'world', 'again')
'|Hello| world|again |more|'
# %d for int
# %nd for int with field-width of n
# %f for float
# %n.mf for float with field-with of n and m decimal digits
>>> '|%d|%4d|%6.2f|' % (11, 222, 33.333)
'|11| 222| 33.33|'
Avoid using old style for formatting.
Conversion between String and Number: int(), float() and str()
You can use built-in functions int() and float() to parse a "numeric" string to an integer or a float; and str()
to convert a number to a string. For example,
# Convert string to int
>>> s = '12345'
>>> s
'12345'
>>> type(s)
<class 'str'>
>>> i = int(s)
>>> i
12345
>>> type(i)
<class 'int'>
>>> lst + ['c'] # '+' returns a new list; while slicing-assignment modifies the list and returns None
[123, 'world', 4.5, 6, 7, 8, 'nine', 'a', 'b', 'c']
>>> lst # No change
[123, 'world', 4.5, 6, 7, 8, 'nine', 'a', 'b']
Copying a list
>>> l1 = [123, 4.5, 'hello']
>>> l2 = l1[:] # Make a copy via slicing
>>> l2
[123, 4.5, 'hello']
>>> l2[0] = 8 # Modify new copy
>>> l2
[8, 4.5, 'hello']
>>> l1 # No change in original
[123, 4.5, 'hello']
>>> len(lst)
4
>>> lst.append('apple') # Append item at the back
>>> lst
[123, 4.5, 'hello', [6, 7, 8], 'apple']
>>> len(lst)
5
>>> lst.pop(1) # Retrieve and remove item at index
4.5
>>> lst
[123, 'hello', [6, 7, 8], 'apple']
>>> len(lst)
4
>>> lst.insert(2, 55.66) # Insert item before the index
>>> lst
[123, 'hello', 55.66, [6, 7, 8], 'apple']
>>> del lst[3:] # Delete the slice (del is an operator , not function)
>>> lst
[123, 'hello', 55.66]
>>> lst.append(55.66) # A list can contain duplicate values
>>> lst
[123, 'hello', 55.66, 55.66]
>>> lst.remove(55.66) # Remove the first item of given value
>>> lst
[123, 'hello', 55.66]
>>> lst.reverse() # Reverse the list in place
>>> lst
[55.66, 'hello', 123]
# Searching and Sorting
>>> lst2 = [5, 8, 2, 4, 1]
>>> lst2.sort() # In-place sorting
>>> lst2
[1, 2, 4, 5, 8]
>>> lst2.index(5) # Get the index of the given item
3
>>> lst2.index(9)
......
ValueError: 9 is not in list
>>> lst2.append(1)
>>> lst2
[1, 2, 4, 5, 8, 1]
>>> lst2.count(1) # Count the occurrences of the given item
2
>>> lst2.count(9)
0
>>> sorted(lst2) # Built-in function that returns a sorted list
[1, 1, 2, 4, 5, 8]
>>> lst2
[1, 2, 4, 5, 8, 1] # Not modified
Using list as a last-in-first-out Stack
To use a list as a last-in-first-out (LIFO) stack, use append(item) to add an item to the top-of-stack (TOS)
and pop() to remove the item from the TOS.
Using list as a first-in-first-out Queue
To use a list as a first-in-first-out (FIFO) queue, use append(item) to add an item to the end of the queue
and pop(0) to remove the first item of the queue.
However, pop(0) is slow! The standard library provide a class collections.deque to efficiently implement
deque with fast appends and pops from both ends.
Tuple (v1, v2,...)
Tuple is similar to list except that it is immutable (just like string). Hence, tuple is more efficient than list. A
tuple consists of items separated by commas, enclosed in parentheses ().
>>> tup = (123, 4.5, 'hello') # A tuple can contain different types
>>> tup
(123, 4.5, 'hello')
>>> tup[1] # Indexing to get an item
4.5
>>> tup[1:3] # Slicing to get a sub-tuple
(4.5, 'hello')
>>> tup[1] = 9 # Tuple, unlike list, is immutable
TypeError: 'tuple' object does not support item assignment
>>> type(tup)
<class 'tuple'>
>>> lst = list(tup) # Convert to list
>>> lst
[123, 4.5, 'hello']
>>> type(lst)
<class 'list'>
An one-item tuple needs a comma to differentiate from parentheses:
>>> tup = (5,) # An one-item tuple needs a comma
>>> tup
(5,)
>>> x = (5) # Treated as parentheses without comma
>>> x
5
The parentheses are actually optional, but recommended for readability. Nevertheless, the commas are
mandatory. For example,
>>> tup = 123, 4.5, 'hello'
>>> tup
(123, 4.5, 'hello')
>>> tup2 = 88, # one-item tuple needs a trailing commas
>>> tup2
(88,)
# You can also use get() to retrieve the value of a given key
>>> dct.get('age', 'not such key') # Retrieve item
22
>>> dct.get('height', 'not such key')
'not such key'
>>> dct['height']
KeyError: 'height'
# Indexing an invalid key raises KeyError, while get() could gracefully handle invalid key
>>> dct.pop('gender') # Remove and return the item with the given key
'male'
>>> dct
{'name': 'Peter', 'weight': 75, 'height': 180}
>>> dct.pop('no_such_key') # Raise KeyError if key not found
KeyError: 'no_such_key'
>>> dct.pop('no_such_key', 'not found') # Provide a default if key does not exist
'not found'
Set {k1, k2,...}
A set is an unordered, non-duplicate collection of objects. A set is delimited by curly braces {}, just like
dictionary. You can think of a set as a collection of dictionary keys without associated values. Sets are
mutable.
For example,
>>> st = {123, 4.5, 'hello', 123, 'Hello'}
>>> st # Duplicate removed and ordering may change
{'Hello', 'hello', 123, 4.5}
>>> 123 in st # Test membership
True
>>> 88 in st
False
# Nested-if
if test_1:
block_1
elif test_2:
block_2
elif test_3:
block_3
......
......
elif test_n:
block_n
else:
else_block
For example:
if x == 0: # No need for parentheses around the test condition
print('x is zero')
elif x > 0:
print('x is more than zero')
print('xxxx')
else:
print('x is less than zero')
print('yyyy')
There is no switch-case statement in Python (as in C/C++/Java).
Comparison and Logical Operators
Python supports these comparison (relational) operators, which return a bool of either True or False.
< (less than), <= (less than or equal to), == (equal to), != (not equal to), > (greater than), >= (greater
than or equal to). (This is the same as C/C++/Java.)
in, not in: Check if an item is|is not in a sequence (list, tuple, string, set, etc).
is, is not: Check if two variables have the same reference.
Python supports these logical (boolean) operators: and, or, not. (C/C++/Java uses &&, ||, !.)
Chain Comparison v1 < x < v2
Python supports chain comparison in the form of v1 < x < v2, e.g.,
>>> x = 8
>>> 1 < x < 10
True
>>> 1 < x and x < 10 # Same as above
True
>>> 10 < x < 20
False
>>> 10 > x > 1
True
>>> not (10 < x < 20)
True
Comparing Sequences
The comparison operators (such as ==, <=) are overloaded to support sequences (such as string, list and
tuple).
In comparing sequences, the first items from both sequences are compared. If they differ the outcome is
decided. Otherwise, the next items are compared, and so on.
# String
>>> 'a' < 'b' # First items differ
True
>>> 'ab' < 'aa' # First items the same. Second items differ
False
>>> 'a' < 'b' < 'c' # with chain comparison
True
# Tuple
>>> (1, 2, 3) < (1, 2, 4) # First and second items the same. Third items differ
True
# List
>>> [1, 2, 3] <= [1, 2, 3] # All items are the same
True
>>> [1, 2, 3] < [1, 2, 3]
False
Shorthand if-else (or Conditional Expression)
The syntax is:
true_expr if test else false_expr
# Evaluate and return true_expr if test is True; otherwise, evaluate and return false_expr
For example,
>>> x = 0
>>> print('zero' if x == 0 else 'not zero')
zero
>>> x = -8
>>> abs_x = x if x > 0 else -x
>>> abs_x
8
Note: Python does not use "? :" for shorthand if-else, as in C/C++/Java.
The while loop
The syntax is as follows:
while test:
true_block
data = func()
while data:
do_something_on_data
data = func() # Need to repeat the function call
The for-in loop
The for-in loop has the following syntax:
for item in sequence: # sequence: string, list, tuple, dictionary, set
true_block
# You can also use enumerate() to get the indexes to modify the list
>>> lst = [11, 22, 33]
>>> for idx, value in enumerate(lst): lst[idx] += 1
>>> lst
[12, 23, 34]
# Tuple
>>> tup = ('d', 'e', 'f')
>>> for idx, value in enumerate(tup): print(idx, value)
0d
1e
2f
Multiple Sequences and the zip() Built-in Function
To loop over two or more sequences concurrently, you can pair the entries with the zip() built-in function.
For examples,
>>> lst1 = ['a', 'b', 'c']
>>> lst2 = [11, 22, 33]
>>> for i1, i2 in zip(lst1, lst2): print(i1, i2)
a 11
b 22
c 33
>>> zip(lst1, lst2) # Return a list of tuples
[('a', 11), ('b', 22), ('c', 33)]
# Same as
result_list = []
for item in list:
if test:
result_list.append(item)
For examples,
>>> sq_lst = [item * item for item in range(1, 11)]
>>> sq_lst
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# Same as
>>> sq_lst = []
>>> for item in range(1, 11):
sq_lst.append(item * item)
# Nested for
>>> lst = [(x, y) for x in range(1, 3) for y in range(1, 4) if x != y]
>>> lst
[(1, 2), (1, 3), (2, 1), (2, 3)]
# Same as
>>> lst = []
>>> for x in range(1,3):
for y in range(1,4):
if x != y: lst.append((x, y))
>>> lst
[(1, 2), (1, 3), (2, 1), (2, 3)]
Similarly, you can create dictionary and set (mutable sequences) via comprehension. For example,
# Dictionary {k1:v1, k2:v2,...}
>>> dct = {x:x**2 for x in range(1, 5)} # Use braces for dictionary
>>> dct
{1: 1, 2: 4, 3: 9, 4: 16}
:param x: number
:return: number
def my_cube(x):
"""
(number) -> (number)
Return the cube of the given number.
:param x: number
:return: number
if __name__ == "__main__":
import doctest
doctest.testmod()
> python3 test_doctest.py
no output - silently PASS all tests (try changing an expected output to fail a test)
> python3 test_doctest.py -v # run in verbose mode to show output
Trying:
my_cube(5)
Expecting:
125
ok
Trying:
my_cube(-5)
Expecting:
-125
ok
Trying:
my_cube(0)
Expecting:
0
ok
1 items had no tests:
__main__
1 items passed all tests:
3 tests in __main__.my_cube
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
Function Parameters and Arguments
There is a subtle difference between function parameters and arguments. Function parameters are
named variables listed in a function definition. Parameter variables are used to import arguments into
functions. For example,
>>> def foo(parameter):
print(parameter)
greet('Peter')
#'hello, Peter'
Instead of hard-coding the 'hello, ', it is more flexible to use a parameter with a default value, as follows:
def greet(name, prefix='hello'): # 'name' is required, 'prefix' is optional
return prefix + ', ' + name
print(foo(n2 = 2, n1 = 1, n3 = 3)) # Keyword arguments need not follow their positional order
print(foo(n2 = 2, n1 = 1)) # n3 defaults
print(foo(n1 = 1)) # n2 and n3 default
print(foo(1, n3 = 3)) # n2 default. Place positional arguments before keyword arguments
print(foo(n2 = 2)) # TypeError, n1 missing
You can also mix the positional arguments and keyword arguments, but you need to place the positional
arguments first, as shown in the above examples.
Variable Number of Positional Arguments (*args)
Python supports variable (arbitrary) number of arguments. In the function definition, you can use * to
pack all the remaining positional arguments into a tuple. For example,
def foo(a, *args): # Accept one positional argument, followed by arbitrary number of arguments pack
into tuple
"""Return the sum of all the arguments (one or more)"""
sum = a
print('args is:', args) # for testing
for item in args: # args is a tuple
sum += item
return sum
>>> foo(1, 2, 3)
version 2
>>> foo(1, 2)
TypeError: foo() missing 1 required positional argument: 'c'
You can archive function overloading by defining the datatype as a parameter (e.g., datatype='int',
datatype='str'), *args or param=None (parameter with default of None). For examples,
[TODO]
Function Return Values
You can return multiple values from a Python function, e.g.,
>>> def foo():
return 1, 'a', 'hello' # Return a tuple
say_hello('Peter')
The type hints annotations are ignored, and merely serves as documentation. But there are external
library that can perform the type check.
The Python runtime does not enforce function and variable type annotations. They can be used by third
party tools such as type checkers, IDEs, linters, etc.
Read: "PEP 484 -- Type Hints".
Modules, Import-Statement and Packages
Modules
A Python module is a file containing Python codes - including statements, variables, functions and classes.
It shall be saved with file extension of ".py". The module name is the filename, i.e., a module shall be
saved as "<module_name>.py".
By convention, modules names shall be short and all-lowercase (optionally joined with underscores if it
improves readability).
A module typically begins with a triple-double-quoted documentation string (doc-string) (which is
available in <module_name>.__doc__), followed by variables, functions and class definitions.
Example: The greet Module
Create a module called greet and save as "greet.py" as follows:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
greet
~~~~~
This module contains the greeting message 'msg' and greeting function 'greet()'.
"""
def foo():
print('y is: {}'.format(y))
def bar():
foo()
Let's try out import:
$ python3
>>> import imtest
x is: 1
>>> imtest.y # All attributes are available, qualifying by the module name
2
>>> imtest.bar()
y is: 2
Now, try the from-import and note that the entire module is loaded, just like the import statement.
$ python3
>>> from imtest import x, bar
x is: 1
>>> x # Can reference directly, without qualifying with the module name
1
>>> bar()
y is: 2
>>> foo() # Only the imported attributes are available
NameError: name 'foo' is not defined
Conditional Import
Python supports conditional import too. For example,
if ....: # E.g., check the version number
import xxx
else:
import yyy
sys.path and PYTHONPATH/PATH environment variables
The environment variable PATH shall include the path to Python Interpreter "python3".
The Python module search path is maintained in a Python variable path of the sys module, i.e., sys.path.
The sys.path is initialized from the environment variable PYTHONPATH, plus an installation-dependent
default. The environment variable PYTHONPATH is empty by default.
For example,
>>> import sys
>>> sys.path
['', '/usr/lib/python3.5', '/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3.5/dist-packages', ...]
sys.path default includes the current working directory (denoted by an empty string), the standard
Python directories, plus the extension directories in dist-packages.
The imported modules must be available in one of the sys.path entries.
>>> import some_mod
ImportError: No module named 'some_mod'
>>> some_mod.var
NameError: name 'some_mod' is not defined
To show the PATH and PYTHONPATH environment variables, use one of these commands:
# Windows
> echo %PATH%
> set PATH
> PATH
> echo %PYTHONPATH%
> set PYTHONPATH
# macOS / Ubuntu
$ echo $PATH
$ printenv PATH
$ echo $PYTHONPATH
$ printenv PYTHONPATH
Reloading Module using imp.reload() or importlib.reload()
If you modify a module, you can use reload() function of the imp (for import) module to reload the
module, for example,
>>> import greet
# Make changes to greet module
>>> import imp
>>> imp.reload(greet)
NOTE: Since Python 3.4, the imp package is pending deprecation in favor of importlib.
>>> import greet
# Make changes to greet module
>>> import importlib # Use 'importlib' in Python 3
>>> importlib.reload(greet)
Template for Python Standalone Module
The following is a template of standalone module for performing a specific task:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
<package_name>.<module_name>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A description which can be long and explain the complete
functionality of this module even with indented code examples.
Class/Function however should not be documented here.
:author: <author-name>
:version: x.y.z (version.release.modification)
:copyright: ......
:license: ......
"""
import <standard_library_modules>
import <third_party_library_modules>
import <application_modules>
# Define global variables
#......
foo('abc')
print(x)
#print(y) # locals are not visible outside the function
#print(arg)
Function Variables (Variables of Function Object)
Function is an object in Python (like JavaScript, unlike Java).
In Python, a variable takes a value or object (such as int, str). It can also take a function. For example,
>>> def square(n): return n * n
>>> square(5)
25
>>> sq = square # Assign a function to a variable. No parameters.
>>> sq(5)
25
>>> type(square)
<class 'function'>
>>> type(sq)
<class 'function'>
>>> square
<function square at 0x7f0ba7040f28>
>>> sq
<function square at 0x7f0ba7040f28> # Exactly the same reference as square
A variable in Python can hold anything, a value, a function or an object.
Nested Functions
Python supports nested functions, i.e., defining a function inside a function. For example,
def outer(a): # Outer function
print('outer() begins with arg =', a)
x = 1 # Local variable of outer function
print('outer() ends')
# Call outer function, which in turn calls the inner function
outer('aaa')
The expected output is:
outer begins with arg = aaa
inner begins with arg = bbb
a = aaa, x = 1, y = 2
inner ends
outer ends
Take note that the inner function has read-access to all the attributes of the enclosing outer function, and
the global variable of this module.
Lambda Function (Anonymous Function)
Lambda functions are anonymous function or un-named function. They are used to inline a function
definition, or to defer execution of certain codes. The syntax is:
lambda arg1, arg2, ...: return_expression
For example,
# Define an ordinary function using def and name
>>> def f1(a, b, c): return a + b + c
>>> f1(1, 2, 3)
6
>>> type(f1)
<class 'function'>
print(my_apply(my_add, 3, 2)) #5
print(my_apply(my_sub, 3, 2)) #1
plus_88 = increase_by(88)
print(plus_88(1))
# 89
@logger
def myfun(a, b, c=3, d=4):
pass # Python syntax needs a dummy statement here
myfun(1, 2, c=33, d=44) # Output: The arguments are: (1, 2), {'c': 33, 'd': 44}
myfun(1, 2, c=33) # Output: The arguments are: (1, 2), {'c': 33}
We can also modify our earlier clamp_range() to handle an arbitrary number of arguments:
def clamp_range(func):
"""Decorator to clamp the value of ALL arguments to [0,100]"""
def _wrapper(*args):
newargs = []
for item in args:
if item < 0:
newargs.append(0)
elif item > 100:
newargs.append(100)
else:
newargs.append(item)
return func(*newargs) # Run the original function with clamped arguments
return _wrapper
@clamp_range
def my_add(x, y, z): return x + y + z
def without_wraps(func):
def _wrapper(*args, **kwargs):
"""_wrapper without_wraps doc-string"""
return func(*args, **kwargs)
return _wrapper
def with_wraps(func):
@wraps(func)
def _wrapper(*args, **kwargs):
"""_wrapper with_wraps doc-string"""
return func(*args, **kwargs)
return _wrapper
@without_wraps
def fun_without_wraps():
"""fun_without_wraps doc-string"""
pass
@with_wraps
def fun_with_wraps():
"""fun_with_wraps doc-string"""
pass
@clamp_range(1, 10)
def my_add(x, y, z):
"""Clamped Add"""
return x + y + z
# Same as
# my_add = clamp_range(min, max)(my_add)
# 'clamp_range(min, max)' returns '_decorator(func)'; apply 'my_add' as 'func'
>>> locals()
...same outputs as global() under the global-scope...
# import
>>> import random
>>> globals()
{'x': 88,
'random': <module 'random' from '/usr/lib/python3.4/random.py'>, # Hook to the imported module
......}
>>> from math import pi
>>> globals()
{'x': 88,
'pi': 3.141592653589793, # Added directly into global namespace
'random': <module 'random' from '/usr/lib/python3.4/random.py'>,
......}
To show the difference between locals and globals, we need to define a function to create a local scope.
For example,
$ python3
>>> x = 88 # x is a global variable
>>> myfun(11)
88
{'__built-ins__': <module 'built-ins' (built-in)>, # Name-value pairs of globals
'myfun': <function myfun at 0x7f550d1b5268>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'__doc__': None,
'__loader__': <class '_frozen_importlib.built-inImporter'>,
'x': 88}
{'y': 99, 'arg': 11} # Name-value pairs of locals
['arg', 'y'] # Names only of locals
More on Module's Global Namespace
Let's create two modules: mod1 and mod2, where mod1 imports mod2, as follows:
"""mod1.py: Module 1"""
import mod2
if __name__ == '__main__':
print('Run module 1')
"""mod2.py: Module 2"""
mod2_var = 'mod2 global variable'
print('Inside mod2, __name__ = ', __name__)
if __name__ == '__main__':
print('Run module 2')
Let's import mod1 (which in turn import mod2) under the interpreter shell, and check the namespaces:
>>> import mod1
Inside mod2, __name__ = mod2 # from imported mod2
Inside mod1, __name__ = mod1
>>> dir()
['__built-ins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'mod1'] # no mod2,
which is referenced as mod1.mod2
>>> dir(mod1)
['__built-ins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
'mod1_var', 'mod2']
>>> dir(mod2)
NameError: name 'mod2' is not defined
>>> dir(mod1.mod2)
['__built-ins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
'mod2_var']
Take note that the interpreter's current scope __name__ is __main__. It's namespace contains mod1
(imported). The mod1's namespace contains mod2 (imported) and mod1_var. To refer to mod2, you need
to go thru mod1, in the form of mod1.mod2. The mod1.mod2's namespace contains mod2_var.
Now, let run mod1 instead, under IDLE3, and check the namespaces:
Inside mod2, __name__ = mod2
Inside mod1, __name__ = __main__
Run module 1
>>> dir()
['__built-ins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'mod1_var',
'mod2']
>>> dir(mod2)
['__built-ins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
'mod2_var']
Take note that the current scope's name is again __main__, which is the executing module mod1. Its
namespace contains mod2 (imported) and mod1_var.
Name Resolution
When you ask for a name (variable), says x, Python searches the LEGB namespaces, in this order, of the
current scope:
1. L: Local namespace which is specific to the current function
2. E: for nested function, the Enclosing function's namespace
3. G: Global namespace for the current module
4. B: Built-in namespace for all the modules
If x cannot be found, Python raises a NameError.
Modifying Global Variables inside a Function
Recall that names created inside a function are local, while names created outside all functions are global
for that module. You can "read" the global variables inside all functions defined in that module. For
example,
x = 'global' # Global file-scope
def myfun():
y = 'local' # Function local-scope
print(y)
print(x) # Can read global variable
myfun()
print(x)
#print(y) # Local out-of-scope
If you assign a value to a name inside a function, a local name is created, which hides the global name. For
example,
x = 'global' # Global file-scope
def myfun():
x = 'change' # Local x created which hides the global x
print(x) # Show local. Global is hidden
myfun()
print(x) # Global does not change
To modify a global variable inside a function, you need to use a global statement to declare the name
global; otherwise, the modification (assignment) will create a local variable (see above). For example,
x = 'global' # Global file-scope
def myfun():
global x # Declare x global, so as to modify global variable
x = 'change' # Else, a local x created which hides the global x
print(x)
myfun()
print(x) # Global changes
For nested functions, you need to use the nonlocal statement in the inner function to modify names in
the enclosing outer function. For example,
def outer(): # Outer function
count = 0
print(count) # Output: 0
inner() # Call inner function
print(count) # Output: 1
def outer():
count = 0 # Local created, hide global
def inner():
global count # Needed to modify global
count += 1 # Else, a local created, which hides the outer
print(count) # Output: 0
inner() # Call inner function
print(count) # Output: 0
>>> myfun()
>>> a
['hello']
In the above example, we modify the contents of the array. The global statement is not needed.
>>> a = 1
>>> def myfun():
global a
a=8
>>> myfun()
>>> a
8
In the above example, we are modifying the reference to the variable. global is needed, otherwise, a local
variable will be created inside the function.
Built-in Namespace
The built-in namespace is defined in the __built-ins__ module, which contains built-in functions such as
len(), min(), max(), int(), float(), str(), list(), tuple() and etc. You can use help(__built-ins__) or dir(__built-
ins__) to list the attributes of the __built-ins__ module.
[TODO]
del Statement
You can use del statement to remove names from the namespace, for example,
>>> del x, pi # delete variables or imported attributes
>>> globals()
...... x and pi removed ......
>>> del random # remove imported module
>>> globals()
...... random module removed ......
If you override a built-in function, you could also use del to remove it from the namespace to recover the
function from the built-in space.
>>> len = 8 # Override built-in function len() (for length)
>>> len('abc') # built-in function len() no longer available
TypeError: 'int' object is not callable
>>> del len # Delete len from global and local namespace
>>> len('abc') # built-in function len() is available
3
Assertion and Exception Handling
assert Statement
You can use assert statement to test a certain assertion (or constraint). For example, if x is supposed to be
0 in a certain part of the program, you can use the assert statement to test this constraint. An
AssertionError will be raised if x is not zero.
For example,
>>> x = 0
>>> assert x == 0, 'x is not zero?!' # Assertion true, no output
>>> x = 1
>>> assert x == 0, 'x is not zero?!' # Assertion false, raise AssertionError with the message
......
AssertionError: x is not zero?!
The assertions are always executed in Python.
Syntax
The syntax for assert is:
assert test, error-message
If the test if True, nothing happens; otherwise, an AssertionError will be raised with the error-message.
Exceptions
In Python, errors detected during execution are called exceptions. For example,
>>> 1/0 # Divide by 0
ZeroDivisionError: division by zero
>>> zzz # Variable not defined
NameError: name 'zzz' is not defined
>>> '1' + 1 # Cannot concatenate string and int
TypeError: Can't convert 'int' object to str implicitly
# Continue into the next statement after try-except-finally instead of abruptly terminated.
print('continue after try-except')
return result
def __str__(self):
return repr(self.value)
# Test logging
logging.debug('a debug message') # Not logged
logging.info('an info message') # Output: INFO:root:an info message
logging.error('an error message') # Output: ERROR:root:an error message
Log Record Format
To set the log message format, use the format keyword:
import logging
logging.basicConfig(
format='%(asctime)s|%(levelname)s|%(name)s|%(pathname)s:%(lineno)d|%(message)s',
level=logging.DEBUG)
where asctime for date/time, levelname for log level, name for logger name, pathname for full-path
filename (filename for filename only), lineno (int) for the line number, and message for the log message.
Advanced Logging: Logger, Handler, Filter and Formatter
So far, we presented the basic logging facilities. The logging library is extensive and organized into these
components:
Loggers: expose the methods to application for logging.
Handlers: send the log records created by the loggers to the appropriate destination, such as file,
console (sys.stderr), email via SMTP, or network via HTTP/FTP.
Filters: decide which log records to output.
Formatters: specify the layout format of log records.
Loggers
To create a Logger instance, invoke the logging.getLogger(logger-name), where the optional logger-name
specifies the logger name (default of root).
The Logger's methods falls into two categories: configuration and logging.
The commonly-used logging methods are: debug(), info(), warning(), error(), critical() and the general
log().
The commonly-used configuration methods are:
setLevel()
addHandler() and removeHandler()
addFilter() and removeFilter()
Handlers
The logging library provides handlers like StreamHandler (sys.stderr, sys.stdout), FileHandler,
RotatingFileHandler, and SMTPHandler (emails).
The commonly-used methods are:
setLevel(): The logger's setLevel() determines which message levels to be passed to the handler;
while the handler's setLevel() determines which message level to be sent to the destination.
setFormatter(): for formatting the message sent to the destination.
addFilter() and removeFilter()
You can add more than one handlers to a logger, possibly handling different log levels. For example, you
can add a SMTPHandler to receive emails for ERROR level; and a RotatingFileHandler for INFO level.
Formatters
Attach to a handler (via <handler>.setFormatter()) to format the log messages.
Example: Using Logger with Console Handler and a Formatter
import logging
# Create a logger
logger = logging.getLogger('MyApp')
logger.setLevel(logging.INFO)
# Test logging
logger.debug('a debug message')
logger.info('an info message')
logger.warn('a warn message')
logger.error('error %d, an error message', 1234)
logger.critical('a critical message')
1. There is probably no standard for log record format (unless you have an analysis tool in mind)?!
But I recommend that you choose a field delimiter which does not appear in the log messages, for
ease of processing of log records (e.g., export to spreadsheet).
The expected outputs are:
2015-12-09 00:32:33,521|MyApp|INFO|an info message
2015-12-09 00:32:33,521|MyApp|WARNING|a warn message
2015-12-09 00:32:33,521|MyApp|ERROR|error 1234: an error message
2015-12-09 00:32:33,521|MyApp|CRITICAL|a critical message
Example: Using Rotating Log Files with RotatingFileHandler
import logging
from logging.handlers import RotatingFileHandler
# Add handler
logger.addHandler(handler)
# Test
logger.info('An info message')
logger.debug('A debug message')
for i in range(1, 10): # Test rotating log files
logger.error('Error message %d', i)
1. We keep all the logging parameters in a dictionary, which are usually retrieved from a
configuration file.
2. In the constructor of RotatingFileHandler, the maxBytes sets the log file size-limit; the
backupCount appends '.1', '.2', etc to the old log files, such that '.1' is always the newer backup of
the log file. Both maxBytes and backupCount default to 0. If either one is zero, roll-over never
occurs.
3. The above example produces 4 log files: test.log, test.log.1 to test.log.3. The file being written to is
always test.log. When this file is filled, it is renamed to test.log.1; and if test.log.1 and test.log.2
exist, they will be renamed to test.log.2 and test.log.3 respectively, with the old test.log.3 deleted.
Example: Using an Email Log for CRITICAL Level and Rotating Log Files for INFO Level
import logging
from logging.handlers import RotatingFileHandler, SMTPHandler
# Add handlers
logger.addHandler(fileHandler)
logger.addHandler(emailHandler)
# Test
logger.debug('A debug message')
logger.info('An info message')
logger.warning('A warning message')
logger.error('An error message')
logger.critical('A critical message')
Example: Separating ERROR Log and INFO Log with Different Format
import logging, sys
from logging.handlers import RotatingFileHandler
class MaxLevelFilter(logging.Filter):
"""Custom filter that passes messages with level <= maxlevel"""
def __init__(self, maxlevel):
"""Constructor takes the max level to pass"""
self.maxlevel = maxlevel
def filter(self, record):
"""Return True to pass the record"""
return (record.levelno <= self.maxlevel)
logger = logging.getLogger("myapp")
logger.setLevel(logging.DEBUG) # Lowest
logger.addHandler(file_handler)
logger.addHandler(err_handler)
# Test
logger.debug("A DEBUG message")
logger.info("An INFO message")
logger.warning("A WARNING message")
logger.error("An ERROR message")
logger.critical("A CRITICAL message")
ConfigParser (Python 2) or configparser (Python 3) Module
The ConfigParser module implements a basic configuration file parser for .ini.
A .ini file contains key-value pairs organized in sections and looks like:
# This is a comment
[app]
name = my application
version = 0.9.1
authors = ["Peter", "Paul"]
debug = False
[db]
host = localhost
port = 3306
[DEFAULT]
message = hello
1. A configuration file consists of sections (marked by [section-name] header). A section contains
key=value or key:value pairs. The leading and trailing whitespaces are trimmed from the value.
Lines beginning with '#' or ';' are comments.
You can use ConfigParser to parse the .ini file, e.g.,
import ConfigParser
cp = ConfigParser.SafeConfigParser()
cp.read('test1.ini')
# Print all contents. Also save into a dictionary
config = {}
for section in cp.sections():
print("Section [%s]" % section)
for option in cp.options(section):
print("|%s|%s|" % (option,
cp.get(section, option))) # Print
config[option] = cp.get(section, option) # Save in dict
print(config)
[DEFAULT]
head = aaa
The msg will be interpolated as aaa + bbb, interpolated from the SAME section and DEFAULT section.
datetime Module
The datetime module supplies classes for manipulating dates and time in both simple and complex ways.
datetime.date.today(): Return the current local date.
>>> import datetime
>>> dir(datetime)
['MAXYEAR', 'MINYEAR', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo', ...]
>>> dir(datetime.date)
['today', ...]
while(1):
print("Wait...")
time.sleep(10)
Run the program in the background (with &) and send signals to the process:
$ ./test_signal.py &
[1] 24078
$ Wait...
Spyder
“Spyder” (previously known as “Pydee”) stands for “Scientific Python Development EnviRonment” (shown
in figure 1-3), and it is a powerful IDE for the Python language with advanced editing, interactive testing,
debugging and introspection features. This IDE also has support of “IPython” (enhanced interactive
Python interpreter) and popular Python libraries such as NumPy, Matplotlib (interactive 2D/3D plotting)
etc. Some of the key features are:
Syntax coloring (or highlighting).
Typing helpers like automatically inserting closing parentheses etc.
Support IPython interpreter.
Contains basic terminal command window.
Spyder runs on all major platforms (Windows, Mac OSX, Linux), and the easiest way to install Spyder in
Windows is through Python(x,y) package (visit http://www.pythonxy.com).
The expressions/codes discussed in this book are written and tested in Spyder IDE.
Python download and installation
There are many different ways to install Python, the best approach depends upon the operating system
one is using, what is already installed, and how the person intends to use it. To avoid wading through all
the details, the easiest approach is to use one of the pre-packaged Python distribution that provide built-
in required libraries. An excellent choice for Windows operating system user is to install using a binary file
which can be downloaded from official Python’s website (http://www. python, org/download/).
One can install IDLE and Spyder in Ubuntu (Linux) operating system by executing the following commands
in the terminal (as shown in figure 1-4).
sudo apt-get install idle-python2.7 spyder
These can be independently installed using separate commands.
sudo apt-get install idle-python2.7
sudo apt-get install spyder
Python(x,y)
“Python(x,y)” is a free scientific and engineering development software for numerical computations, data
analysis and data visualization based on Python programming language and Spyder interactive
development environment, the launcher (current version 2.7.6.0) is shown in figure 1-5. The executable
file of Python(x,y) can be downloaded and then installed from the website link:
http://code.google.eom/p/pythonxy/. The main features of Python(x,y) are:
Bundled with scientific oriented Python libraries and development environment tools.
Extensive documentation of various Python packages.
Providing all-in-one setup program, so that the user can install or uninstall all these packages and
features by clicking one button only.
Object
“Object” (also called “name”) is Python’s abstraction for data. All data in a Python program is represented
by objects or by relations between objects. Every object has an identity, a type and a value. An object’s
identity never changes once it has been created; it can be thought of it as the object’s address in memory.
The id () function returns an integer representing its identity (currently implemented as its address). An
object’s type determines the operations that the object supports and also defines the possible values for
objects of that type. An object’s type is also unchangeable and the type () function returns an object’s
type. The value of some objects can change. Objects whose value can change are said to be “mutable”;
objects whose value is unchangeable once they are created are called “immutable”. In the example
below, object a has identity 31082544, type int and value 5.
>>> a=5
>>> id(a)
31082544
>>> type(a)
<type 'int'>
Some objects contain references to other objects; these are called “containers”. Examples of containers
are tuples, lists and dictionaries. The value of an immutable container object that contains a reference to
a mutable object can change when the latter’s value is changed; however the container is still considered
immutable, because the collection of objects it contains cannot be changed. So, immutability is not
strictly the same as having an unchangeable value.
An object has attribute(s), which are referenced using dotted expressions. For example, if an object abc
has an attribute pq, then it would be referenced as abc . pq. In the following example, upper () is an
attribute of var object.
>>> var='hello'
>>> var.upper()
'HELLO'
In the above example, upper () is function on some object var, and this function is called “method”. More
information on “method” is given in chapter 6.
Interactive mode
One of Python’s most useful features is its interactive interpreter. It allows very fast testing of ideas
without the overhead of creating test files, as is typical in most programming languages. However, the
interpreter supplied with the standard Python distribution is somewhat limited for extended interactive
use. IPython is a good choice for comprehensive environment for interactive and exploratory computing.
To start interactive mode, launch Python with no arguments (possibly by selecting it from your
computer’s main menu). It is a very powerful way to test out new ideas or inspect modules and packages.
Interactive mode prompts for the next command with the “primary prompt”, usually three greater- than
signs (>>>); a continuation line is prompted with the “secondary prompt”, which is by default represented
by three dots (…). The interpreter prints a welcome message stating its version number and some
additional information before printing the first prompt:
$ python
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>
Continuation lines are needed when entering a multi-line statement. As an example, take a look at this if
statement:
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
........ print("Be careful not to fall off!")
........
Be careful not to fall off!
Invoking Python interpreter
In Unix/Linux platforms, the Python interpreter is usually installed at /usr/local/bin/python. It is possible
to start interpreter by typing the following command (same command for MS Windows)
$ python
in the shell. Since the choice of the directory where the interpreter lives is an installation option, other
places are possible (e.g., /usr/local/python is a popular alternative location).
On Windows machines, the Python installation is available at path C:\Python27, though, this can be
changed when running the installer. To add this directory to Path environmental variable, type the
following command into the MS DOS command prompt:
set path=%path%;C:\python27
Inputting end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes
the interpreter to exit. If that does not work, you can exit the interpreter by typing the following
command:
>>> quit ( )
Script mode
If Python interpreter is closed and then invoked again, the definitions that were made (functions,
variables etc.) are lost. Therefore, to write a long program, the programmer should use a text editor to
prepare the input for the interpreter and run it with that file as input instead. This is known as creating a
“script”. Most of the examples in this book are discussed using interactive mode, but few scripts are also
incorporated.
First program
This section will demonstrate to write a simple Python program, which prints “Hello World”. Type the
following lines in IDLE text editor and save it as “HelloWorld.py”.
#! /usr/bin/env python
print ('Hello world')
The first line is called “shebang line” or “hashbang line” (more information in next section). The second
line gives the output: “Hello World”. There are numerous ways to run a Python program. The simplest
approach is to press F5 functional key after saving the program in IDLE text editor. The output is shown
below:
>>>
Hello world
Executing Python script
As discussed in previous section, Python script can be executed using F5 functional key, from Python’s
IDE. It can also be executed using command prompt by typing the following command:
$ python<file name>
On different platforms, the execution of Python scripts (apart from running from inside Python’s IDE) can
be carried out as follows:
Linux
On Unix/Linux system, Python script can be made directly executable, like shell scripts, by including the
following expression as first line of the script (assuming that the interpreter is on the user’s PATH) and
giving the file an executable mode.
#! /usr/bin/env python
The ‘# !’ must be the first two characters of the file. Note that the hash or pound character ‘#’ is used to
start a comment in Python. The script can be given an executable mode/permission, using the chmod
command:
$ chmod +x HelloWorld.py
Windows
On Windows system, the Python installer automatically associates .py files with python.exe, so that
double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the
console window that normally appears is suppressed. At MS DOS prompt, the Python script can be
executed by going to the directory containing the script and just entering the script name (with
extension).