0% found this document useful (0 votes)
821 views1,018 pages

Python Notes

This document provides an overview of Python programming concepts including: - Python is an interpreted, high-level, general-purpose programming language. - Topics covered in the Python tutorial include installation, control statements, strings, lists, tuples, dictionaries, modules, exceptions, date and time, file I/O and Python programs. - Popular Python frameworks and libraries include Django, Flask for web development, TensorFlow and PyTorch for machine learning, and NumPy and Pandas for mathematics.

Uploaded by

Rameshwar Kanade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
821 views1,018 pages

Python Notes

This document provides an overview of Python programming concepts including: - Python is an interpreted, high-level, general-purpose programming language. - Topics covered in the Python tutorial include installation, control statements, strings, lists, tuples, dictionaries, modules, exceptions, date and time, file I/O and Python programs. - Popular Python frameworks and libraries include Django, Flask for web development, TensorFlow and PyTorch for machine learning, and NumPy and Pandas for mathematics.

Uploaded by

Rameshwar Kanade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1018

Python Tutorial

Python tutorial provides basic and advanced concepts of Python. Our Python
tutorialis designed for beginners and professionals.

Python is a simple, general purpose, high level, and object-oriented programming


language.

Python is an interpreted scripting language also. Guido Van Rossum is known as the
founder of Python programming.

Our Python tutorial includes all topics of Python Programming such as installation,
control statements, Strings

, Lists
, Tuples
, Dictionary
, Modules
, Exceptions
, Date and Time, File I/O, Programs, etc. There are also given Python interview questions to
help you better understand Python Programming.
1

What is Python
Python is a general purpose, dynamic, high-level

, and interpreted programming language. It supports Object Oriented programming approach


to develop applications. It is simple and easy to learn and provides lots of high-level data
structures.

Python is easy to learn yet powerful and versatile scripting language, which makes it
attractive for Application Development.

Python's syntax and dynamic typing with its interpreted nature make it an ideal
language for scripting and rapid application development.

Python supports multiple programming pattern, including object-oriented, imperative,


and functional or procedural programming styles.

Python is not intended to work in a particular area, such as web programming. That is
why it is known as multipurpose programming language because it can be used with
web, enterprise, 3D CAD, etc.
We don't need to use data types to declare variable because it is dynamically typed so
we can write a=10 to assign an integer value in an integer variable.

Python makes the development and debugging fast because there is no compilation
step included in Python development, and edit-test-debug cycle is very fast.

Python 2 vs. Python 3


In most of the programming languages, whenever a new version releases, it supports
the features and syntax of the existing version of the language, therefore, it is easier
for the projects to switch in the newer version. However, in the case of Python, the two
versions Python 2 and Python 3 are very much different from each other.

A list of differences between Python 2 and Python 3 are given below:

1. Python 2 uses print as a statement and used as print "something" to print some
string on the console. On the other hand, Python 3 uses print as a function and
used as print("something") to print something on the console.
2. Python 2 uses the function raw_input() to accept the user's input. It returns the
string representing the value, which is typed by the user. To convert it into the
integer, we need to use the int() function in Python. On the other hand, Python
3 uses input() function which automatically interpreted the type of input
entered by the user. However, we can cast this value to any type by using
primitive functions (int(), str(), etc.).
3. In Python 2, the implicit string type is ASCII, whereas, in Python 3, the implicit
string type is Unicode.
4. Python 3 doesn't contain the xrange() function of Python 2. The xrange() is the
variant of range() function which returns a xrange object that works similar to
Java iterator. The range() returns a list for example the function range(0,3)
contains 0, 1, 2.
5. There is also a small change made in Exception handling in Python 3. It defines
a keyword as which is necessary to be used. We will discuss it in Exception
handling section of Python programming tutorial.

Python History
Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of
Python programming language has taken from the ABC programming language or we
can say that ABC is a predecessor of Python language.
There is also a fact behind the choosing name Python. Guido van Rossum was a fan of
the popular BBC comedy show of that time, "Monty Python's Flying Circus". So he
decided to pick the name Python for his newly created programming language.

Python has the vast community across the world and releases its version within the
short period.

Why learn Python?


Python provides many useful features to the programmer. These features make it most
popular and widely used language. We have listed below few-essential feature of
Python.

o Easy to use and Learn


o Expressive Language
o Interpreted Language
o Object-Oriented Language
o Open Source Language
o Extensible
o Learn Standard Library
o GUI Programming Support
o Integrated
o Embeddable
o Dynamic Memory Allocation
o Wide Range of Libraries and Frameworks

Where is Python used?


Python is a general-purpose, popular programming language and it is used in almost
every technical field. The various areas of Python use are given below.

o Data Science
o Date Mining
o Desktop Applications
o Console-based Applications
o Mobile Applications
o Software Development
o Artificial Intelligence
o Web Applications
o Enterprise Applications
o 3D CAD Applications
o Machine Learning
o Computer Vision or Image Processing Applications.
o Speech Recognitions

Python Basic Syntax


There is no use of curly braces or semicolon in Python programming language. It is
English-like language. But Python uses the indentation to define a block of code.
Indentation is nothing but adding whitespace before the statement when it is
needed. For example -

1. def func():
2. statement 1
3. statement 2
4. …………………
5. …………………
6. statement N

In the above example, the statements that are same level to right belong to the
function. Generally, we can use four whitespaces to define indentation.

Python First Program


Unlike the other programming languages, Python provides the facility to execute the
code using few lines. For example - Suppose we want to print the "Hello
World" program in Java; it will take three lines to print it.

1. public class HelloWorld {


2. public static void main(String[] args){
3. // Prints "Hello, World" to the terminal window.
4. System.out.println("Hello World");
5. }
6. }

On the other hand, we can do this using one statement in Python.

1. print("Hello World")

Both programs will print the same result, but it takes only one statement without using
a semicolon or curly braces in Python.

Python Popular Frameworks and Libraries


Python has wide range of libraries and frameworks widely used in various fields such
as machine learning, artificial intelligence, web applications, etc. We define some
popular frameworks and libraries of Python as follows.

o Web development (Server-side) - Django Flask, Pyramid, CherryPy


o GUIs based applications - Tk, PyGTK, PyQt, PyJs, etc.
o Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
o Mathematics - Numpy, Pandas, etc.

Python print() Function


The print() function displays the given object to the standard output device (screen)
or to the text stream file.

Unlike the other programming languages, Python print() function is most unique and
versatile function.

The syntax of print() function is given below.

1. print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Let's explain its parameters one by one.


o objects - An object is nothing but a statement that to be printed. The * sign
represents that there can be multiple statements.
o sep - The sep parameter separates the print values. Default values is ' '.
o end - The end is printed at last in the statement.
o file - It must be an object with a write(string) method.
o flush - The stream or file is forcibly flushed if it is true. By default, its value is
false.

Let's understand the following example.

Example - 1: Return a value

1. print("Welcome to javaTpoint.")
2.
3. a = 10
4. # Two objects are passed in print() function
5. print("a =", a)
6.
7. b = a
8. # Three objects are passed in print function
9. print('a =', a, '= b')

Output:

Welcome to javaTpoint.
a = 10
a = 10 = b

As we can see in the above output, the multiple objects can be printed in the
single print() statement. We just need to use comma (,) to separate with each other.

Example - 2: Using sep and end argument

1. a = 10
2. print("a =", a, sep='dddd', end='\n\n\n')
3. print("a =", a, sep='0', end='$$$$$')

Output:

a =dddd10
a =010$$$$$

In the first print() statement, we use the sep and end arguments. The given object is
printed just after the sep values. The value of end parameter printed at the last of given
object. As we can see that, the second print() function printed the result after the three
black lines.

Taking Input to the User


Python provides the input() function which is used to take input from the user. Let's
understand the following example.

Example -

1. name = input("Enter a name of student:")


2. print("The student name is: ", name)

Output:

Enter a name of student: Devansh


The student name is: Devansh

By default, the input() function takes the string input but what if we want to take other
data types as an input.

If we want to take input as an integer number, we need to typecast


the input() function into an integer.

For example -

Example -

1. a = int(input("Enter first number: "))


2. b = int(input("Enter second number: "))
3.
4. print(a+b)

Output:

Enter first number: 50


Enter second number: 100
150

We can take any type of values using input() function.


Python Operators
Operators are the symbols which perform various operations on Python objects.
Python operators are the most essential to work with the Python data types. In
addition, Python also provides identify membership and bitwise operators. We will
learn all these operators with the suitable example in following tutorial.

o Python Operators

Python Conditional Statements


Conditional statements help us to execute a particular block for a particular condition.
In this tutorial, we will learn how to use the conditional expression to execute a
different block of statements. Python provides if and else keywords to set up logical
conditions. The elif keyword is also used as conditional statement.

o Python if..else statement

Python Loops
Sometimes we may need to alter the flow of the program. The execution of a specific
code may need to be repeated several numbers of times. For this purpose, the
programming languages provide various types of loops capable of repeating some
specific code several times. Consider the following tutorial to understand the
statements in detail.

o Python Loops
o Python For Loop
o Python While Loop

Python Data Structures


Data structures are referred which can hold some data together or we say that they
are used to store the data in organized way. Python provides built-in data structures
such as list, tuple, dictionary, and set. We can perform complex tasks using data
structures.

Python List
Python list holds the ordered collection of items. We can store a sequence of items in
a list. Python list is mutable which means it can be modified after its creation. The items
of lists are enclosed within the square bracket [] and separated by the comma. Let's
see the example of list.

1. L1 = ["John", 102, "USA"]


2. L2 = [1, 2, 3, 4, 5, 6]

If we try to print the type of L1, L2, and L3 using type() function then it will come out
to be a list.

1. print(type(L1))
2. print(type(L2))

Output:

<class 'list'>
<class 'list'>

To learn more about list, visit the following tutorial.

o Python List
o Python List Functions

Python Tuple
Python Tuple is used to store the sequence of immutable Python objects. The tuple is
similar to lists since the value of the items stored in the list can be changed, whereas
the tuple is immutable, and the value of the items stored in the tuple cannot be
changed.

Tuple can be defined as follows

Example -

1. tup = ("Apple", "Mango" , "Orange" , "Banana")


2. print(type(tup))
3. print(tup)
Output:

<class 'tuple'>
('Apple', 'Mango', 'Orange', 'Banana')

If we try to add new to the tuple, it will throw an error.

Example -

1. tup = ("Apple", "Mango" , "Orange" , "Banana")


2.
3. tup[2] = "Papaya"
4. print(tup)

Output:

Traceback (most recent call last):


File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/gamewithturtle.py",
line 3, in
tup[2] = "Papaya"
TypeError: 'tuple' object does not support item assignment

The above program throws an error because tuples are immutable type. To learn more
about tuple, visit the Python Tuples.

o Python Tuple

Python String
Python string is a sequence of characters. It is a collection of the characters surrounded
by single quotes, double quotes, or triple quotes. It can also define as collection of the
Unicode characters. We can create a string as follows.

Example -

1. # Creating string using double quotes


2. str1 = "Hi Python"
3. print(str1)
4. # Creating string using single quotes
5. str1 = 'Hi Python'
6. print(str1)
7. # Creating string using triple quotes
8. str1 = '''Hi Python'''
9. print(str1)

Output:

Hi Python
Hi Python
Hi Python

Python doesn't support the character data-type. A single character written as 'p' is
treated as a string of length 1.

Stings are also immutable. We can't change after it is declared. To learn more about
the string, visit the following tutorial.

o Python Strings
o Python String Method

Dictionaries
Python Dictionary is a most efficient data structure and used to store the large amount
of data. It stores the data in the key-value pair format. Each value is stored
corresponding to its key.

Keys must be a unique and value can be any type such as integer, list, tuple, etc.

It is a mutable type; we can reassign after its creation. Below is the example of creating
dictionary in Python.

Example -

1. employee = {"Name": "John", "Age": 29, "salary":250000,"Company":"GOOGLE"


}
2. print(type(employee))
3. print("printing Employee data .... ")
4. print(employee)

Output:

<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 250000, 'Company': 'GOOGLE'}
The empty curly braces {} are used to create empty dictionary. To learn more, visit the
complete tutorial of the dictionary.

o Python Dictionary
o Python Dictionary Methods

Python Sets
A Python set is a collection of unordered elements. Each element in set must be unique
and immutable. Sets are mutable which means we can modify anytime throughout the
program. Let's understand the example of creating set in Python.

Example -

1. # Creating Set
2. Month = {"January", "February", "March", "April", "May", "June", "July"}
3. print(Month)
4. print(type(Month))

Output:

{'March', 'July', 'April', 'May', 'June', 'February', 'January'}


<class 'set'>

To get the more information about sets, visit the following resources.

o Python Sets
o Python Set Methods

Python Functional Programming


This section of Python tutorial defines some important tools related to functional
programming such as lambda and recursive functions. These functions are very
efficient in accomplishing the complex tasks. We define a few important functions,
such as reduce, map, and filter. Python provides the functools module that includes
various functional programming tools. Visit the following tutorial to learn more
about functional programming.

o Python Function
o Python map() Function
o Python filter() Function
o Python reduce() Function
o Python functool Module
o Python Lambda Function

Python File I/O


Files are used to store data in a computer disk. In this tutorial, we explain the built-in
file object of Python. We can open a file using Python script and perform various
operations such as writing, reading, and appending. There are various ways of opening
a file. We are explained with the relevant example. We will also learn to perform
read/write operations on binary files.

o Python File I/O

Python Modules
Python modules are the program files that contain a Python code or functions. There
are two types of module in the Python - User-define modules and built-in modules. A
module that the user defines, or we can say that our Python code saved
with .py extension, is treated as a user-define module.

Built-in modules are predefined modules of Python. To use the functionality of the
modules, we need to import them into our current working program.

o Python Modules

Python Exceptions
An exception can be defined as an unusual condition in a program resulting in the
interruption in the flow of the program.

Whenever an exception occurs, the program stops the execution, and thus the further
code is not executed. Therefore, an exception is the run-time errors that are unable to
handle to Python script. An exception is a Python object that represents an error.

o Python Exceptions

Python CSV
A csv stands for "comma separated values", which is defined as a simple file format
that uses specific structuring to arrange tabular data. It stores tabular data such as
spreadsheet or database in plain text and has a common format for data interchange.
A csv file opens into the excel sheet, and the rows and columns data define the
standard format. Visit the following tutorial to learn the CSV module in detail.

o Python Read CSV File


o Python Write CSV File

Python Sending Mail


We can send or read a mail using the Python script. Python's standard library modules
are useful for handling various protocols such as PoP3 and IMAP. We will learn how to
send a mail with the popular email service SMTP from a Python script.

o Python Sending Emails

Python Magic Methods


Python magic method is defined as the special method which adds "magic" to a class.
It starts and ends with double underscores, for example, _init_ or _str_.

The built-in classes define many magic methods. The dir() function can be used to see
the number of magic methods inherited by a class. It has two prefixes and suffix
underscores in the method name.

o Python Magic Methods

Python Oops Concepts


Everything in Python is treated as an object including integer values, floats, functions,
classes, and none. Apart from that, Python supports all oriented concepts. Below is the
brief introduction of oops concepts of Python.

o Classes and Objects - Python classes are the blueprint of the object. An object
is a collection of data and method that act on the data.
o Inheritance - An inheritance is a technique where one class inherits the
properties of other classes.
o Constructor - Python provides a special method __init__() which is known as a
constructor. This method is automatically called when an object is instantiated.
o Data Member - A variable that holds data associated with a class and its
objects.

To read the oops concept in detail, visit the following resources.

o Python Oops Concepts


o Python Object and classes
o Python Constructor
o Python Inheritance
o Python Polymorphism

Python Advance Topics


Python includes many advance and useful concepts that help the programmer to solve
the complex tasks. These concepts are given below.

Python Iterator
An iterator is simply an object that can be iterated upon. It returns one object at a time.
It can be implemented using the two special methods, __iter__() and __next__().

To learn more about the iterators visit our Python Iterators tutorial.

Python Generators
The Generators are an easiest way of creating Iterators. To learn more about, visit
our Python Generators tutorial.

Python Decorators
These are used to modify the behavior of the function. Decorators provide the
flexibility to wrap another function to expand the working of wrapped function,
without permanently modifying it.

To learn more about, visit the Python Decorators tutorial.

Python Database Connections


We can use various databases along with Python. You can learn the full tutorial to visit
below resources. Python DBI-API acclaims standard sets of functionality to be included
in the database connectivity modules for respective RDBMS products. We explain all
important database connectivity using Python DBI-API.
Python MySQL
Environment Setup

Database Connection

Creating New Database

Creating Tables

Insert Operation

Read Operation

Update Operation

Join Operation

Performing Transactions

Python MongoDB
Python MongoDB

Python SQLite
Python SQLite

Python CGI
Python CGI stands for "Common Gateway Interface", which is used to define how to
exchange information between the webserver and a custom Python scripts.
The Common Gateway Interface is a standard for external gateway programs to
interface with the server, such as HTTP Servers. To learn more about Python CGI, visit
the following tutorial.

o Python CGI

Python Features
Python provides many useful features which make it popular and valuable from the
other programming languages. It supports object-oriented programming, procedural
programming approaches and provides dynamic memory allocation. We have listed
below a few essential features.
1) Easy to Learn and Use
Python is easy to learn as compared to other programming languages. Its syntax is
straightforward and much the same as the English language. There is no use of the
semicolon or curly-bracket, the indentation defines the code block. It is the
recommended programming language for beginners.

2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple example, the
hello world program you simply type print("Hello World"). It will take only one line
to execute, while Java or C takes multiple lines.

3) Interpreted Language
Python is an interpreted language; it means the Python program is executed one line
at a time. The advantage of being interpreted language, it makes debugging easy and
portable.

Hello Java Program for Beginners

4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX, and
Macintosh, etc. So, we can say that Python is a portable language. It enables
programmers to develop the software for several competing platforms by writing a
program only once.

5) Free and Open Source


Python is freely available for everyone. It is freely available on its official
website www.python.org. It has a large community across the world that is dedicatedly
working towards make new python modules and functions. Anyone can contribute to
the Python community. The open-source means, "Anyone can download its source
code without paying any penny."

6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects come
into existence. It supports inheritance, polymorphism, and encapsulation, etc. The
object-oriented procedure helps to programmer to write reusable code and develop
applications in less code.

7) Extensible
It implies that other languages such as C/C++ can be used to compile the code and
thus it can be used further in our Python code. It converts the program into byte code,
and any platform can use that byte code.

8) Large Standard Library


It provides a vast range of libraries for the various fields such as machine learning, web
developer, and also for the scripting. There are various machine learning libraries, such
as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are
the popular framework for Python web development.

9) GUI Programming Support


Graphical User Interface is used for the developing Desktop application. PyQT5,
Tkinter, Kivy are the libraries which are used for developing the web application.

10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code
line by line like C,C++ Java. It makes easy to debug the code.

11. Embeddable
The code of the other programming language can use in the Python source code. We
can use Python source code in another programming language as well. It can embed
other language into our code.

12. Dynamic Memory Allocation


In Python, we don't need to specify the data-type of the variable. When we assign
some value to the variable, it automatically allocates the memory to the variable at run
time. Suppose we are assigned integer value 15 to x, then we don't need to write int
x = 15. Just write x = 15.

Python Applications
Python is known for its general-purpose nature that makes it applicable in almost every
domain of software development. Python makes its presence in every emerging field.
It is the fastest-growing programming language and can develop any application.

Here, we are specifying application areas where Python can be applied.


1) Web Applications
We can use Python to develop web applications. It provides libraries to handle internet
protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup,
Feedparser, etc. One of Python web-framework named Django is used on Instagram.
Python provides many useful frameworks, and these are given below:

o Django and Pyramid framework(Use for heavy applications)


o Flask and Bottle (Micro-framework)
o Plone and Django CMS (Advance Content management)

2) Desktop GUI Applications


The GUI stands for the Graphical User Interface, which provides a smooth interaction
to any application. Python provides a Tk GUI library to develop a user interface. Some
popular GUI libraries are given below.

HTML Tutorial
o Tkinter or Tk
o wxWidgetM
o Kivy (used for writing multitouch applications )
o PyQt or Pyside

3) Console-based Application
Console-based applications run from the command-line or shell. These applications
are computer program which are used commands to execute. This kind of application
was more popular in the old generation of computers. Python can develop this kind
of application very effectively. It is famous for having REPL, which means the Read-
Eval-Print Loop that makes it the most suitable language for the command-line
applications.

Python provides many free library or module which helps to build the command-line
apps. The necessary IO libraries are used to read and write. It helps to parse argument
and create console help text out-of-the-box. There are also advance libraries that can
develop independent console apps.

4) Software Development
Python is useful for the software development process. It works as a support language
and can be used to build control and management, testing, etc.

o SCons is used to build control.


o Buildbot and Apache Gumps are used for automated continuous compilation
and testing.
o Round or Trac for bug tracking and project management.

5) Scientific and Numeric


This is the era of Artificial intelligence where the machine can perform the task the
same as the human. Python language is the most suitable language for Artificial
intelligence or machine learning. It consists of many scientific and mathematical
libraries, which makes easy to solve complex calculations.

Implementing machine learning algorithms require complex mathematical calculation.


Python has many libraries for scientific and numeric such as Numpy, Pandas, Scipy,
Scikit-learn, etc. If you have some basic knowledge of Python, you need to import
libraries on the top of the code. Few popular frameworks of machine libraries are given
below.
o SciPy
o Scikit-learn
o NumPy
o Pandas
o Matplotlib

6) Business Applications
Business Applications differ from standard applications. E-commerce and ERP are an
example of a business application. This kind of application requires extensively,
scalability and readability, and Python provides all these features.

Oddo is an example of the all-in-one Python-based application which offers a range


of business applications. Python provides a Tryton platform which is used to develop
the business application.

7) Audio or Video-based Applications


Python is flexible to perform multiple tasks and can be used to create multimedia
applications. Some multimedia applications which are made by using Python
are TimPlayer, cplay, etc. The few multimedia libraries are given below.

o Gstreamer
o Pyglet
o QT Phonon

8) 3D CAD Applications
The CAD (Computer-aided design) is used to design engineering related architecture.
It is used to develop the 3D representation of a part of a system. Python can create a
3D CAD application by using the following functionalities.

o Fandango (Popular )
o CAMVOX
o HeeksCNC
o AnyCAD
o RCAM

9) Enterprise Applications
Python can be used to create applications that can be used within an Enterprise or an
Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc.

10) Image Processing Application


Python contains many libraries that are used to work with the image. The image can
be manipulated according to our requirements. Some libraries of image processing
are given below.

o OpenCV
o Pillow
o SimpleITK

In this topic, we have described all types of applications where Python plays an
essential role in the development of these applications. In the next tutorial, we will
learn more concepts about Python.

Python Variables
Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.

In Python, we don't need to specify the type of variable because Python is a infer
language and smart enough to get variable type.

Variable names can be a group of both the letters and digits, but they have to begin
with a letter or an underscore.

It is recommended to use lowercase letters for the variable name. Rahul and rahul both
are two different variables. SQL CREATE TABLE

Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals used
in the program. The rules to name an identifier are given below.

o The first character of the variable must be an alphabet or underscore ( _ ).


o All the characters except the first character may be an alphabet of lower-case(a-
z), upper-case (A-Z), underscore, or digit (0-9).
o Identifier name must not contain any white-space, or special character (!, @, #,
%, ^, &, *).
o Identifier name must not be similar to any keyword defined in the language.
o Identifier names are case sensitive; for example, my name, and MyName is not
the same.
o Examples of valid identifiers: a123, _n, n_9, etc.
o Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values


Python does not bind us to declare a variable before using it in the application. It allows
us to create a variable at the required time.

We don't need to declare explicitly variable in Python. When we assign any value to
the variable, that variable is declared automatically.

The equal (=) operator is used to assign value to a variable.

Object References
It is necessary to understand how the Python interpreter works when we declare a
variable. The process of treating variables is somewhat different from many other
programming languages.

Python is the highly object-oriented programming language; that's why every data
item belongs to a specific type of class. Consider the following example.

1. print("John")

Output:

John

The Python object creates an integer object and displays it to the console. In the above
print statement, we have created a string object. Let's check the type of it using the
Python built-in type() function.

1. type("John")

Output:

<class 'str'>

In Python, variables are a symbolic name that is a reference or pointer to an object.


The variables are used to denote objects by that name.
Let's understand the following example

1. a = 50

In the above image, the variable a refers to an integer object.

Suppose we assign the integer value 50 to a new variable b.

a = 50

b=a

The variable b refers to the same object that a points to because Python does not
create another object.

Let's assign the new value to b. Now both variables will refer to the different objects.

a = 50

b =100
Python manages memory efficiently if we assign the same variable to two different
values.

Object Identity
In Python, every created object identifies uniquely in Python. Python provides the
guaranteed that no two objects will have the same identifier. The built-in id() function,
is used to identify the object identifier. Consider the following example.

1. a = 50
2. b = a
3. print(id(a))
4. print(id(b))
5. # Reassigned variable a
6. a = 500
7. print(id(a))

Output:

140734982691168
140734982691168
2822056960944

We assigned the b = a, a and b both point to the same object. When we checked by
the id() function it returned the same number. We reassign a to 500; then it referred
to the new object identifier.

Variable Names
We have already discussed how to declare the valid variable. Variable names can be
any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and
underscore character(_). Consider the following example of valid variables names.

1. name = "Devansh"
2. age = 20
3. marks = 80.50
4.
5. print(name)
6. print(age)
7. print(marks)

Output:
Devansh
20
80.5

Consider the following valid variables name.

1. name = "A"
2. Name = "B"
3. naMe = "C"
4. NAME = "D"
5. n_a_m_e = "E"
6. _name = "F"
7. name_ = "G"
8. _name_ = "H"
9. na56me = "I"
10.
11. print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_nam
e, na56me)

Output:

A B C D E D E F G F I

In the above example, we have declared a few valid variable names such as name,
_name_ , etc. But it is not recommended because when we try to read code, it may
create confusion. The variable name should be descriptive to make code more
readable.

The multi-word keywords can be created by the following method.

o Camel Case - In the camel case, each word or abbreviation in the middle of
begins with a capital letter. There is no intervention of whitespace. For example
- nameOfStudent, valueOfVaraible, etc.
o Pascal Case - It is the same as the Camel Case, but here the first word is also
capital. For example - NameOfStudent, etc.
o Snake Case - In the snake case, Words are separated by the underscore. For
example - name_of_student, etc.

Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement, which is
also known as multiple assignments.

We can apply multiple assignments in two ways, either by assigning a single value to
multiple variables or assigning multiple values to multiple variables. Consider the
following example.

1. Assigning single value to multiple variables

Eg:

1. x=y=z=50
2. print(x)
3. print(y)
4. print(z)

Output:

50
50
50

2. Assigning multiple values to multiple variables:

Eg:

1. a,b,c=5,10,15
2. print a
3. print b
4. print c

Output:

5
10
15

The values will be assigned in the order in which variables appear.

Python Variable Types


There are two types of variables in Python - Local variable and Global variable. Let's
understand the following variables.

Local Variable
Local variables are the variables that declared inside the function and have scope
within the function. Let's understand the following example.

Example -

1. # Declaring a function
2. def add():
3. # Defining local variables. They has scope only within a function
4. a = 20
5. b = 30
6. c=a+b
7. print("The sum is:", c)
8.
9. # Calling a function
10. add()

Output:

The sum is: 50

Explanation:

In the above code, we declared a function named add() and assigned a few variables
within the function. These variables will be referred to as the local variables which
have scope only inside the function. If we try to use them outside the function, we get
a following error.

1. add()
2. # Accessing local variable outside the function
3. print(a)

Output:

The sum is: 50


print(a)
NameError: name 'a' is not defined

We tried to use local variable outside their scope; it threw the NameError.

Global Variables
Global variables can be used throughout the program, and its scope is in the entire
program. We can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default. Python
provides the global keyword to use global variable inside the function. If we don't use
the global keyword, the function treats it as a local variable. Let's understand the
following example.

Example -

1. # Declare a variable and initialize it


2. x = 101
3.
4. # Global variable in function
5. def mainFunction():
6. # printing a global variable
7. global x
8. print(x)
9. # modifying a global variable
10. x = 'Welcome To Javatpoint'
11. print(x)
12.
13. mainFunction()
14. print(x)

Output:

101
Welcome To Javatpoint
Welcome To Javatpoint

Explanation:

In the above code, we declare a global variable x and assign a value to it. Next, we
defined a function and accessed the declared variable using the global keyword inside
the function. Now we can modify its value. Then, we assigned a new string value to the
variable x.

Now, we called the function and proceeded to print x. It printed the as newly assigned
value of x.

Delete a variable
We can delete the variable using the del keyword. The syntax is given below.

Syntax -
1. del <variable_name>

In the following example, we create a variable x and assign value to it. We deleted
variable x, and print it, we get the error "variable x is not defined". The variable x will
no longer use in future.

Example -

1. # Assigning a value to x
2. x = 6
3. print(x)
4. # deleting a variable.
5. del x
6. print(x)

Output:

6
Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py",
line 389, in
print(x)
NameError: name 'x' is not defined

Maximum Possible Value of an Integer in Python


Unlike the other programming languages, Python doesn't have long int or float data
types. It treats all integer values as an int data type. Here, the question arises. What is
the maximum possible value can hold by the variable in Python? Consider the
following example.

Example -

1. # A Python program to display that we can store


2. # large numbers in Python
3.
4. a = 10000000000000000000000000000000000000000000
5. a = a + 1
6. print(type(a))
7. print (a)

Output:

<class 'int'>
10000000000000000000000000000000000000000001

As we can see in the above example, we assigned a large integer value to


variable x and checked its type. It printed class <int> not long int. Hence, there is no
limitation number by bits and we can expand to the limit of our memory.

Python doesn't have any special data type to store larger numbers.

Print Single and Multiple Variables in Python


We can print multiple variables within the single print statement. Below are the
example of single and multiple printing values.

Example - 1 (Printing Single Variable)

1. # printing single value


2. a = 5
3. print(a)
4. print((a))

Output:

5
5

Example - 2 (Printing Multiple Variables)

1. a = 5
2. b = 6
3. # printing multiple variables
4. print(a,b)
5. # separate the variables by the comma
6. Print(1, 2, 3, 4, 5, 6, 7, 8)

Output:

5 6
1 2 3 4 5 6 7 8

Basic Fundamentals:
This section contains the fundamentals of Python, such as:

i)Tokens and their types.


ii) Comments

a)Tokens:

o The tokens can be defined as a punctuator mark, reserved words, and each word
in a statement.
o The token is the smallest unit inside the given program.

There are following tokens in Python:

o Keywords.
o Identifiers.
o Literals.
o Operators.

We will discuss above the tokens in detail next tutorials.

Python Data Types


Variables can hold values, and every value has a data-type. Python is a dynamically
typed language; hence we do not need to define the type of the variable while
declaring it. The interpreter implicitly binds the value with its type.

1. a = 5

The variable a holds integer value five and we did not define its type. Python
interpreter will automatically interpret variables a as an integer type.

Python enables us to check the type of the variable used in the program. Python
provides us the type() function, which returns the type of the variable passed.

Consider the following example to define the values of different data types and
checking its type.How to find Nth Highest Salary in SQL

1. a=10
2. b="Hi Python"
3. c = 10.5
4. print(type(a))
5. print(type(b))
6. print(type(c))
Output:

<type 'int'>
<type 'str'>
<type 'float'>

Standard data types


A variable can hold different types of values. For example, a person's name must be
stored as a string whereas its id must be stored as an integer.

Python provides various standard data types that define the storage method on each
of them. The data types defined in Python are given below.

1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary

In this section of the tutorial, we will give a brief introduction of the above data-types.
We will discuss each one of them in detail later in this tutorial.

Numbers
Number stores numeric values. The integer, float, and complex values belong to a
Python Numbers data-type. Python provides the type() function to know the data-
type of the variable. Similarly, the isinstance() function is used to check an object
belongs to a particular class.

Python creates Number objects when a number is assigned to a variable. For example;

1. a = 5
2. print("The type of a", type(a))
3.
4. b = 40.5
5. print("The type of b", type(b))
6.
7. c = 1+3j
8. print("The type of c", type(c))
9. print(" c is a complex number", isinstance(1+3j,complex))

Output:

The type of a <class 'int'>


The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True

Python supports three types of numeric data.

1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc.
Python has no restriction on the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It
is accurate upto 15 decimal points.
3. complex - A complex number contains an ordered pair, i.e., x + iy where x and
y denote the real and imaginary parts, respectively. The complex numbers like
2.14j, 2.0 + 2.3j, etc.

Sequence Type
String

The string can be defined as the sequence of characters represented in the quotation
marks. In Python, we can use single, double, or triple quotes to define a string.
String handling in Python is a straightforward task since Python provides built-in
functions and operators to perform operations in the string.

In the case of string handling, the operator + is used to concatenate two strings as the
operation "hello"+" python" returns "hello python".

The operator * is known as a repetition operator as the operation "Python" *2 returns


'Python Python'.

The following example illustrates the string in Python.

Example - 1

1. str = "string using double quotes"


2. print(str)
3. s = '''''A multiline
4. string'''
5. print(s)

Output:

string using double quotes


A multiline
string

Consider the following example of string handling.

Example - 2

1. str1 = 'hello javatpoint' #string str1


2. str2 = ' how are you' #string str2
3. print (str1[0:2]) #printing first two character using slice operator
4. print (str1[4]) #printing 4th character of the string
5. print (str1*2) #printing the string twice
6. print (str1 + str2) #printing the concatenation of str1 and str2

Output:

he
o
hello javatpointhello javatpoint
hello javatpoint how are you

List
Python Lists are similar to arrays in C. However, the list can contain data of different
types. The items stored in the list are separated with a comma (,) and enclosed within
square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator
(+) and repetition operator (*) works with the list in the same way as they were working
with the strings.

Consider the following example.

1. list1 = [1, "hi", "Python", 2]


2. #Checking type of given list
3. print(type(list1))
4.
5. #Printing the list1
6. print (list1)
7.
8. # List slicing
9. print (list1[3:])
10.
11. # List slicing
12. print (list1[0:2])
13.
14. # List Concatenation using + operator
15. print (list1 + list1)
16.
17. # List repetation using * operator
18. print (list1 * 3)

Output:

[1, 'hi', 'Python', 2]


[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection
of the items of different data types. The items of the tuple are separated with a comma
(,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of the items
of a tuple.

Let's see a simple example of the tuple.

1. tup = ("hi", "Python", 2)


2. # Checking type of tup
3. print (type(tup))
4.
5. #Printing the tuple
6. print (tup)
7.
8. # Tuple slicing
9. print (tup[1:])
10. print (tup[0:1])
11.
12. # Tuple concatenation using + operator
13. print (tup + tup)
14.
15. # Tuple repatation using * operator
16. print (tup * 3)
17.
18. # Adding value to tup. It will throw an error.
19. t[2] = "hi"

Output:

<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)

Traceback (most recent call last):


File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment

Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an associative array
or a hash table where each key stores a specific value. Key can hold any primitive data
type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the curly
braces {}.

Consider the following example.

1. d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}


2.
3. # Printing dictionary
4. print (d)
5.
6. # Accesing value using keys
7. print("1st name is "+d[1])
8. print("2nd name is "+ d[4])
9.
10. print (d.keys())
11. print (d.values())

Output:

1st name is Jimmy


2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])

Boolean
Boolean type provides two built-in values, True and False. These values are used to
determine the given statement true or false. It denotes by the class bool. True can be
represented by any non-zero value or 'T' whereas false can be represented by the 0 or
'F'. Consider the following example.

1. # Python program to check the boolean type


2. print(type(True))
3. print(type(False))
4. print(false)

Output:

<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined

Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can
modify after creation), and has unique elements. In set, the order of the elements is
undefined; it may return the changed sequence of the element. The set is created by
using a built-in function set(), or a sequence of elements is passed in the curly braces
and separated by the comma. It can contain various types of values. Consider the
following example.

1. # Creating Empty set


2. set1 = set()
3.
4. set2 = {'James', 2, 3,'Python'}
5.
6. #Printing Set value
7. print(set2)
8.
9. # Adding element to the set
10.
11. set2.add(10)
12. print(set2)
13.
14. #Removing element from the set
15. set2.remove(2)
16. print(set2)

Output:

{3, 'Python', 'James', 2}


{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}

Python Keywords
Python Keywords are special reserved words that convey a special meaning to the
compiler/interpreter. Each keyword has a special meaning and a specific operation.
These keywords can't be used as a variable. Following is the List of Python Keywords.

True False None and as

asset def class continue break


else finally elif del except

global for if from import

raise try or return pass

nonlocal in not is lambda

Consider the following explanation of keywords.

1. True - It represents the Boolean true, if the given condition is true, then it returns
"True". Non-zero values are treated as true.
2. False - It represents the Boolean false; if the given condition is false, then it returns
"False". Zero value is treated as false
3. None - It denotes the null value or void. An empty list or Zero can't be treated as None.
4. and - It is a logical operator. It is used to check the multiple conditions. It returns true
if both conditions are true. Consider the following truth table.

A B A and B

True True True

True False False

False True False

False False False

5. or - It is a logical operator in Python. It returns true if one of the conditions is true.


Consider the following truth table.

A B A and B

True True True


True False True

False True True

False False False

6. not - It is a logical operator and inverts the truth value. Consider the following truth
table.

8.8M

157

Prime Ministers of India | List of Prime Minister of India (1947-2020)

A Not A

True False

False True

7. assert - This keyword is used as the debugging tool in Python. It checks the
correctness of the code. It raises an AssertionError if found any error in the code and
also prints the message with an error.

Example:

1. a = 10
2. b = 0
3. print('a is dividing by Zero')
4. assert b != 0
5. print(a / b)

Output:

a is dividing by Zero
Runtime Exception:
Traceback (most recent call last):
File "/home/40545678b342ce3b70beb1224bed345f.py", line 4, in
assert b != 0, "Divide by 0 error"
AssertionError: Divide by 0 error
8. def - This keyword is used to declare the function in Python. If followed by the
function name.

1. def my_func(a,b):
2. c = a+b
3. print(c)
4. my_func(10,20)

Output:

30

9. class - It is used to represents the class in Python. The class is the blueprint of the
objects. It is the collection of the variable and methods. Consider the following class.

1. class Myclass:
2. #Variables……..
3. def function_name(self):
4. #statements………

10. continue - It is used to stop the execution of the current iteration. Consider the
following example.

1. a = 0
2. while a < 4:
3. a += 1
4. if a == 2:
5. continue
6. print(a)

Output:

1
3
4

11. break - It is used to terminate the loop execution and control transfer to the end
of the loop. Consider the following example.

Example

1. for i in range(5):
2. if(i==3):
3. break
4. print(i)
5. print("End of execution")

Output:

0
1
2
End of execution

12. If - It is used to represent the conditional statement. The execution of a particular


block is decided by if statement. Consider the following example.

Example

1. i = 18
2. if (1 < 12):
3. print("I am less than 18")

Output:

I am less than 18

13. else - The else statement is used with the if statement. When if statement returns
false, then else block is executed. Consider the following example.

Example:

1. n = 11
2. if(n%2 == 0):
3. print("Even")
4. else:
5. print("odd")

Output:

Odd

14. elif - This Keyword is used to check the multiple conditions. It is short for else-if.
If the previous condition is false, then check until the true condition is found. Condition
the following example.

Example:
1. marks = int(input("Enter the marks:"))
2. if(marks>=90):
3. print("Excellent")
4. elif(marks<90 and marks>=75):
5. print("Very Good")
6. elif(marks<75 and marks>=60):
7. print("Good")
8. else:
9. print("Average")

Output:

Enter the marks:85


Very Good

15. del - It is used to delete the reference of the object. Consider the following
example.

Example:

1. a=10
2. b=12
3. del a
4. print(b)
5. # a is no longer exist
6. print(a)

Output:

12
NameError: name 'a' is not defined

16. try, except - The try-except is used to handle the exceptions. The exceptions are
run-time errors. Consider the following example.

Example:

1. a = 0
2. try:
3. b = 1/a
4. except Exception as e:
5. print(e)
Output:

division by zero

17. raise - The raise keyword is used to through the exception forcefully. Consider the
following example.

Example

1. a = 5
2. if (a>2):
3. raise Exception('a should not exceed 2 ')

Output:

Exception: a should not exceed 2

18. finally - The finally keyword is used to create a block of code that will always be
executed no matter the else block raises an error or not. Consider the following
example.

Example:

1. a=0
2. b=5
3. try:
4. c = b/a
5. print(c)
6. except Exception as e:
7. print(e)
8. finally:
9. print('Finally always executed')

Output:

division by zero
Finally always executed

19. for, while - Both keywords are used for iteration. The for keyword is used to
iterate over the sequences (list, tuple, dictionary, string). A while loop is executed until
the condition returns false. Consider the following example.

Example: For loop


1. list = [1,2,3,4,5]
2. for i in list:
3. print(i)

Output:

1
2
3
4
5

Example: While loop

1. a = 0
2. while(a<5):
3. print(a)
4. a = a+1

Output:

0
1
2
3
4

20. import - The import keyword is used to import modules in the current Python
script. The module contains a runnable Python code.

Example:

1. import math
2. print(math.sqrt(25))

Output:

21. from - This keyword is used to import the specific function or attributes in the
current Python script.

Example:

1. from math import sqrt


2. print(sqrt(25))
Output:

22. as - It is used to create a name alias. It provides the user-define name while
importing a module.

Example:

1. import calendar as cal


2. print(cal.month_name[5])

Output:

May

23. pass - The pass keyword is used to execute nothing or create a placeholder for
future code. If we declare an empty class or function, it will through an error, so we
use the pass keyword to declare an empty class or function.

Example:

1. class my_class:
2. pass
3.
4. def my_func():
5. pass

24. return - The return keyword is used to return the result value or none to called
function.

Example:

1. def sum(a,b):
2. c = a+b
3. return c
4.
5. print("The sum is:",sum(25,15))

Output:

The sum is: 40


25. is - This keyword is used to check if the two-variable refers to the same object. It
returns the true if they refer to the same object otherwise false. Consider the following
example.

Example

1. x = 5
2. y = 5
3.
4. a = []
5. b = []
6. print(x is y)
7. print(a is b)

Output:

True
False
Note: A mutable data-types do not refer to the same object.

26. global - The global keyword is used to create a global variable inside the function.
Any function can access the global. Consider the following example.

Example

1. def my_func():
2. global a
3. a = 10
4. b = 20
5. c = a+b
6. print(c)
7.
8. my_func()
9.
10. def func():
11. print(a)
12.
13. func()

Output:

30
10

27. nonlocal - The nonlocal is similar to the global and used to work with a variable
inside the nested function(function inside a function). Consider the following example.

Example

1. def outside_function():
2. a = 20
3. def inside_function():
4. nonlocal a
5. a = 30
6. print("Inner function: ",a)
7. inside_function()
8. print("Outer function: ",a)
9. outside_function()

Output:

Inner function: 30
Outer function: 30

28. lambda - The lambda keyword is used to create the anonymous function in
Python. It is an inline function without a name. Consider the following example.

Example

1. a = lambda x: x**2
2. for i in range(1,6):
3. print(a(i))

Output:

1
4
9
16
25

29. yield - The yield keyword is used with the Python generator. It stops the function's
execution and returns value to the caller. Consider the following example.

Example

1. def fun_Generator():
2. yield 1
3. yield 2
4. yield 3
5.
6.
7. # Driver code to check above generator function
8. for value in fun_Generator():
9. print(value)

Output:

1
2
3

30. with - The with keyword is used in the exception handling. It makes code cleaner
and more readable. The advantage of using with, we don't need to call close().
Consider the following example.

Example

1. with open('file_path', 'w') as file:


2. file.write('hello world !')

31. None - The None keyword is used to define the null value. It is remembered
that None does not indicate 0, false, or any empty data-types. It is an object of its data
type, which is Consider the following example.

Example:

1. def return_none():
2. a = 10
3. b = 20
4. c=a+b
5.
6. x = return_none()
7. print(x)

Output:

None
We have covered all Python keywords. This is the brief introduction of Python
Keywords. We will learn more in the upcoming tutorials.

Python Literals
Python Literals can be defined as data that is given in a variable or constant.

Python supports the following literals:

1. String literals:
String literals can be formed by enclosing a text in the quotes. We can use both single
as well as double quotes to create a string.

Example:

HTML Tutorial

1. "Aman" , '12345'

Types of Strings:

There are two types of Strings supported in Python:

a) Single-line String- Strings that are terminated within a single-line are known as
Single line Strings.

Example:

1. text1='hello'

b) Multi-line String - A piece of text that is written in multiple lines is known as


multiple lines string.

There are two ways to create multiline strings:

1) Adding black slash at the end of each line.

Example:

1. text1='hello\
2. user'
3. print(text1)
'hellouser'
2) Using triple quotation marks:-

Example:

1. str2='''''welcome
2. to
3. SSSIT'''
4. print str2

Output:

welcome
to
SSSIT

II. Numeric literals:


Numeric Literals are immutable. Numeric literals can belong to following four different
numerical types.

Int(signed Long(long float(floating Complex(complex)


integers) integers) point)

Numbers( can be Integers of Real numbers with In the form of a+bj where a
both positive and unlimited size both integer and forms the real part and b forms
negative) with no followed by fractional part eg: - the imaginary part of the
fractional part.eg: lowercase or 26.2 complex number. eg: 3.14j
100 uppercase L eg:
87032845L

Example - Numeric Literals

1. x = 0b10100 #Binary Literals


2. y = 100 #Decimal Literal
3. z = 0o215 #Octal Literal
4. u = 0x12d #Hexadecimal Literal
5.
6. #Float Literal
7. float_1 = 100.5
8. float_2 = 1.5e2
9.
10. #Complex Literal
11. a = 5+3.14j
12.
13. print(x, y, z, u)
14. print(float_1, float_2)
15. print(a, a.imag, a.real)

Output:

20 100 141 301


100.5 150.0
(5+3.14j) 3.14 5.0

III. Boolean literals:


A Boolean literal can have any of the two values: True or False.

Example - Boolean Literals

1. x = (1 == True)
2. y = (2 == False)
3. z = (3 == True)
4. a = True + 10
5. b = False + 10
6.
7. print("x is", x)
8. print("y is", y)
9. print("z is", z)
10. print("a:", a)
11. print("b:", b)

Output:

x is True
y is False
z is False
a: 11
b: 10

IV. Special literals.


Python contains one special literal i.e., None.

None is used to specify to that field that is not created. It is also used for the end of
lists in Python.
Example - Special Literals

1. val1=10
2. val2=None
3. print(val1)
4. print(val2)

Output:

10
None

V. Literal Collections.
Python provides the four types of literal collection such as List literals, Tuple literals,
Dict literals, and Set literals.

List:

o List contains items of different data types. Lists are mutable i.e., modifiable.
o The values stored in List are separated by comma(,) and enclosed within square
brackets([]). We can store different types of data in a List.

Example - List literals

1. list=['John',678,20.4,'Peter']
2. list1=[456,'Andrew']
3. print(list)
4. print(list + list1)

Output:

['John', 678, 20.4, 'Peter']


['John', 678, 20.4, 'Peter', 456, 'Andrew']

Dictionary:

o Python dictionary stores the data in the key-value pair.


o It is enclosed by curly-braces {} and each pair is separated by the commas(,).

Example

1. dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}


2. print(dict)

Output:

{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}

Tuple:

o Python tuple is a collection of different data-type. It is immutable which means


it cannot be modified after creation.
o It is enclosed by the parentheses () and each element is separated by the
comma(,).

Example

1. tup = (10,20,"Dev",[2,3,4])
2. print(tup)

Output:

(10, 20, 'Dev', [2, 3, 4])

Set:

o Python set is the collection of the unordered dataset.


o It is enclosed by the {} and each element is separated by the comma(,).

Example: - Set Literals

1. set = {'apple','grapes','guava','papaya'}
2. print(set)

Output:

{'guava', 'apple', 'papaya', 'grapes'}

Python Operators
The operator can be defined as a symbol which is responsible for a particular operation
between two operands. Operators are the pillars of a program on which the logic is
built in a specific programming language. Python provides a variety of operators,
which are described as follows.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators

Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between two
operands. It includes + (addition), - (subtraction), *(multiplication), /(divide),
%(reminder), //(floor division), and exponent (**) operators.

Consider the following table for a detailed explanation of arithmetic operators.

Operator Description

+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30

- (Subtraction) It is used to subtract the second operand from the first operand. If the first
operand is less than the second operand, the value results negative. For
example, if a = 20, b = 10 => a - b = 10

/ (divide) It returns the quotient after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a/b = 2.0

* It is used to multiply one operand with the other. For example, if a = 20, b =
(Multiplication) 10 => a * b = 200

% (reminder) It returns the reminder after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a%b = 0

** (Exponent) It is an exponent operator represented as it calculates the first operand power


to the second operand.

// (Floor It gives the floor value of the quotient produced by dividing the two
division) operands.

Comparison operator
Comparison operators are used to comparing the value of the two operands and
returns Boolean true or false accordingly. The comparison operators are described in
the following table.

OOPs Concepts in Java

Operator Description

== If the value of two operands is equal, then the condition becomes true.

!= If the value of two operands is not equal, then the condition becomes true.

<= If the first operand is less than or equal to the second operand, then the condition
becomes true.

>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.

> If the first operand is greater than the second operand, then the condition becomes
true.

< If the first operand is less than the second operand, then the condition becomes
true.

Assignment Operators
The assignment operators are used to assign the value of the right expression to the
left operand. The assignment operators are described in the following table.

Operator Description

= It assigns the value of the right expression to the left operand.

+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20 =>
a+ = b will be equal to a = a+ b and therefore, a = 30.

-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10 =>
a- = b will be equal to a = a- b and therefore, a = 10.

*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10, b
= 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assigns
the reminder back to the left operand. For example, if a = 20, b = 10 => a % = b
will be equal to a = a % b and therefore, a = 0.

**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 =
16 to a.

//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3
= 1 to a.

Bitwise Operators
The bitwise operators perform bit by bit operation on the values of the two operands.
Consider the following example.

For example,

1. if a = 7
2. b=6
3. then, binary (a) = 0111
4. binary (b) = 0110
5.
6. hence, a & b = 0011
7. a | b = 0111
8. a ^ b = 0100
9. ~ a = 1000

Operator Description

& (binary If both the bits at the same place in two operands are 1, then 1 is copied to the
and) result. Otherwise, 0 is copied.

| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will
be 1.

^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit
will be 0.

~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the right
operand.

>> (right The left operand is moved right by the number of bits present in the right
shift) operand.

Logical Operators
The logical operators are used primarily in the expression evaluation to make a
decision. Python supports the following logical operators.

Operator Description

and If both the expression are true, then the condition will be true. If a and b are the two
expressions, a → true, b → true => a and b → true.

or If one of the expressions is true, then the condition will be true. If a and b are the
two expressions, a → true, b → false => a or b → true.

not If an expression a is true, then not (a) will be false and vice versa.

Membership Operators
Python membership operators are used to check the membership of value inside a
Python data structure. If the value is present in the data structure, then the resulting
value is true otherwise it returns false.

Operator Description

in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).

not in It is evaluated to be true if the first operand is not found in the second operand (list,
tuple, or dictionary).

Identity Operators
The identity operators are used to decide whether an element certain class or type.

Operator Description
is It is evaluated to be true if the reference present at both sides point to the same
object.

is not It is evaluated to be true if the reference present at both sides do not point to the
same object.

Operator Precedence
The precedence of the operators is essential to find out since it enables us to know
which operator should be evaluated first. The precedence table of the operators in
Python is given below.

Operator Description

** The exponent operator is given priority over all the others used in the
expression.

~+- The negation, unary plus, and minus.

* / % // The multiplication, divide, modules, reminder, and floor division.

+- Binary plus, and minus

>> << Left shift. and right shift

& Binary and.

^| Binary xor, and or

<= < > >= Comparison operators (less than, less than equal to, greater than, greater
then equal to).

<> == != Equality operators.

= %= /= //= -= Assignment operators


+=
*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators


Python Comments
Python Comment is an essential tool for the programmers. Comments are generally
used to explain the code. We can easily understand the code if it has a proper
explanation. A good programmer must use the comments because in the future
anyone wants to modify the code as well as implement the new module; then, it can
be done easily.

In the other programming language such as C++, It provides the // for single-lined
comment and /*.... */ for multiple-lined comment, but Python provides the single-lined
Python comment. To apply the comment in the code we use the hash(#) at the
beginning of the statement or code.

Let's understand the following example.

1. # This is the print statement


2. print("Hello Python")

Here we have written comment over the print statement using the hash(#). It will not
affect our print statement.

Java Try Catch

Multiline Python Comment


We must use the hash(#) at the beginning of every line of code to apply the multiline
Python comment. Consider the following example.

1. # First line of the comment


2. # Second line of the comment
3. # Third line of the comment

Example:

1. # Variable a holds value 5


2. # Variable b holds value 10
3. # Variable c holds sum of a and b
4. # Print the result
5. a = 5
6. b = 10
7. c = a+b
8. print("The sum is:", c)
Output:

The sum is: 15

The above code is very readable even the absolute beginners can under that what is
happening in each line of the code. This is the advantage of using comments in code.

We can also use the triple quotes ('''''') for multiline comment. The triple quotes are
also used to string formatting. Consider the following example.

Docstrings Python Comment


The docstring comment is mostly used in the module, function, class or method. It is
a documentation Python string. We will explain the class/method in further tutorials.

Example:

1. def intro():
2. """
3. This function prints Hello Joseph
4. """
5. print("Hi Joseph")
6. intro()

Output:

Hello Joseph

We can check a function's docstring by using the __doc__ attribute.

Generally, four whitespaces are used as the indentation. The amount of indentation depends
on user, but it must be consistent throughout that block.

1. def intro():
2. """
3. This function prints Hello Joseph
4. """
5. print("Hello Joseph")
6. intro.__doc__

Output:

Output:
'\n This function prints Hello Joseph\n '
Note: The docstring must be the first thing in the function; otherwise, Python
interpreter cannot get the docstring.

Python indentation
Python indentation uses to define the block of the code. The other programming
languages such as C, C++, and Java use curly braces {}, whereas Python uses an
indentation. Whitespaces are used as indentation in Python.

Indentation uses at the beginning of the code and ends with the unintended line. That
same line indentation defines the block of the code (body of a function, loop, etc.)

Generally, four whitespaces are used as the indentation. The amount of indentation
depends on user, but it must be consistent throughout that block.

1. for i in range(5):
2. print(i)
3. if(i == 3):
4. break

To indicate a block of code we indented each line of the block by the same
whitespaces.

Consider the following example.

1. dn = int(input("Enter the number:"))


2. if(n%2 == 0):
3. print("Even Number")
4. else:
5. print("Odd Number")
6.
7. print("Task Complete")

Output:

Enter the number: 10


Even Number
Task Complete

The above code, if and else are two separate code blocks. Both code blocks are
indented four spaces. The print("Task Complete") statement is not indented four
whitespaces and it is out of the if-else block.
If the indentation is not used properly, then that will result in IndentationError.

Python If-else statements


Decision making is the most important aspect of almost all the programming
languages. As the name implies, decision making allows us to run a particular block of
code for a particular decision. Here, the decisions are made on the validity of the
particular conditions. Condition checking is the backbone of decision making.

In python, decision making is performed by the following statements.

Statement Description

If Statement The if statement is used to test a specific condition. If the condition is true, a
block of code (if-block) will be executed.

If - else The if-else statement is similar to if statement except the fact that, it also provides
Statement the block of the code for the false case of the condition to be checked. If the
condition provided in the if statement is false, then the else statement will be
executed.

Nested if Nested if statements enable us to use if ? else statement inside an outer if


Statement statement.

Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use
of parentheses for the block level code. In Python, indentation is used to declare a
block. If two statements are at the same indentation level, then they are the part of the
same block.

Generally, four spaces are given to indent the statements which are a typical amount
of indentation in python.

Hello Java Program for Beginners

Indentation is the most used part of the python language since it declares the block
of code. All the statements of one block are intended at the same level indentation.
We will see how the actual indentation takes place in decision making and other stuff
in python.

The if statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be any
valid logical expression which can be either evaluated to true or false.

The syntax of the if-statement is given below.

1. if expression:
2. statement

Example 1

1. num = int(input("enter the number?"))


2. if num%2 == 0:
3. print("Number is even")

Output:

enter the number?10


Number is even

Example 2 : Program to print the largest of the three numbers.

1. a = int(input("Enter a? "));
2. b = int(input("Enter b? "));
3. c = int(input("Enter c? "));
4. if a>b and a>c:
5. print("a is largest");
6. if b>a and b>c:
7. print("b is largest");
8. if c>a and c>b:
9. print("c is largest");
Output:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

The if-else statement


The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.

The syntax of the if-else statement is given below.

1. if condition:
2. #block of statements
3. else:
4. #another block of statements (else-block)

Example 1 : Program to check whether a person is eligible to


vote or not.

1. age = int (input("Enter your age? "))


2. if age>=18:
3. print("You are eligible to vote !!");
4. else:
5. print("Sorry! you have to wait !!");

Output:
Enter your age? 90
You are eligible to vote !!

Example 2: Program to check whether a number is even or not.

1. num = int(input("enter the number?"))


2. if num%2 == 0:
3. print("Number is even...")
4. else:
5. print("Number is odd...")

Output:

enter the number?10


Number is even

The elif statement


The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them. We can have
any number of elif statements in our program depending upon our need. However,
using elif is optional.

The elif statement works like an if-else-if ladder statement in C. It must be succeeded
by an if statement.

The syntax of the elif statement is given below.

1. if expression 1:
2. # block of statements
3.
4. elif expression 2:
5. # block of statements
6.
7. elif expression 3:
8. # block of statements
9.
10. else:
11. # block of statements
Example 1

1. number = int(input("Enter the number?"))


2. if number==10:
3. print("number is equals to 10")
4. elif number==50:
5. print("number is equal to 50");
6. elif number==100:
7. print("number is equal to 100");
8. else:
9. print("number is not equal to 10, 50 or 100");

Output:

Enter the number?15


number is not equal to 10, 50 or 100

Example 2

1. marks = int(input("Enter the marks? "))


2. f marks > 85 and marks <= 100:
3. print("Congrats ! you scored grade A ...")
4. lif marks > 60 and marks <= 85:
5. print("You scored grade B + ...")
6. lif marks > 40 and marks <= 60:
7. print("You scored grade B ...")
8. lif (marks > 30 and marks <= 40):
9. print("You scored grade C ...")
10. lse:
11. print("Sorry you are fail ?")

Python Loops
The flow of the programs written in any programming language is sequential by
default. Sometimes we may need to alter the flow of the program. The execution of a
specific code may need to be repeated several numbers of times.

For this purpose, The programming languages provide various types of loops which
are capable of repeating some specific code several numbers of times. Consider the
following diagram to understand the working of a loop statement.

Why we use loops in python?


The looping simplifies the complex problems into the easy ones. It enables us to alter
the flow of the program so that instead of writing the same code again and again, we
can repeat the same code for a finite number of times. For example, if we need to print
the first 10 natural numbers then, instead of using the print statement 10 times, we
can print inside a loop which runs up to 10 iterations.

Advantages of loops
There are the following advantages of loops in Python.OOPs Concepts in Java

1. It provides code re-usability.


2. Using loops, we do not need to write the same code again and again.
3. Using loops, we can traverse over the elements of data structures (array or
linked lists).

There are the following loop statements in Python.

Loop Description
Statement

for loop The for loop is used in the case where we need to execute some part of the code
until the given condition is satisfied. The for loop is also called as a per-tested
loop. It is better to use for loop if the number of iteration is known in advance.

while loop The while loop is to be used in the scenario where we don't know the number of
iterations in advance. The block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is also called a pre-tested
loop.

do-while The do-while loop continues until a given condition satisfies. It is also called post
loop tested loop. It is used when it is necessary to execute the loop at least once
(mostly menu driven programs).

Python for loop


The for loop in Python is used to iterate the statements or a part of the program
several times. It is frequently used to traverse the data structures like list, tuple, or
dictionary.

The syntax of for loop in python is given below.

1. for iterating_var in sequence:


2. statement(s)

The for loop flowchart


For loop Using Sequence
Example-1: Iterating string using for loop

1. str = "Python"
2. for i in str:
3. print(i)

Output:

12.8M
214
Hello Java Program for Beginners
P
y
t
h
o
n

Example- 2: Program to print the table of the given number .

1. list = [1,2,3,4,5,6,7,8,9,10]
2. n = 5
3. for i in list:
4. c = n*i
5. print(c)

Output:

5
10
15
20
25
30
35
40
45
50s

Example-4: Program to print the sum of the given list.

1. list = [10,30,23,43,65,12]
2. sum = 0
3. for i in list:
4. sum = sum+i
5. print("The sum is:",sum)

Output:

The sum is: 183

For loop Using range() function


The range() function

The range() function is used to generate the sequence of the numbers. If we pass the
range(10), it will generate the numbers from 0 to 9. The syntax of the range() function
is given below.

Syntax:

1. range(start,stop,step size)

o The start represents the beginning of the iteration.


o The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
o The step size is used to skip the specific numbers from the iteration. It is optional
to use. By default, the step size is 1. It is optional.

Consider the following examples:

Example-1: Program to print numbers in sequence.

1. for i in range(10):
2. print(i,end = ' ')
Output:

0 1 2 3 4 5 6 7 8 9

Example - 2: Program to print table of given number.

1. n = int(input("Enter the number "))


2. for i in range(1,11):
3. c = n*i
4. print(n,"*",i,"=",c)

Output:

Enter the number 10


10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

Example-3: Program to print even number using step size in range().

1. n = int(input("Enter the number "))


2. for i in range(2,n,2):
3. print(i)

Output:

Enter the number 20


2
4
6
8
10
12
14
16
18

We can also use the range() function with sequence of numbers. The len() function is
combined with range() function which iterate through a sequence using indexing.
Consider the following example.

1. list = ['Peter','Joseph','Ricky','Devansh']
2. for i in range(len(list)):
3. print("Hello",list[i])

Output:

Hello Peter
Hello Joseph
Hello Ricky
Hello Devansh

Nested for loop in python


Python allows us to nest any number of for loops inside a for loop. The inner loop is
executed n number of times for every iteration of the outer loop. The syntax is given
below.

Syntax

1. for iterating_var1 in sequence: #outer loop


2. for iterating_var2 in sequence: #inner loop
3. #block of statements
4. #Other statements

Example- 1: Nested for loop

1. # User input for number of rows


2. rows = int(input("Enter the rows:"))
3. # Outer loop will print number of rows
4. for i in range(0,rows+1):
5. # Inner loop will print number of Astrisk
6. for j in range(i):
7. print("*",end = '')
8. print()

Output:

Enter the rows:5


*
**
***
****
*****

Example-2: Program to number pyramid.


1. rows = int(input("Enter the rows"))
2. for i in range(0,rows+1):
3. for j in range(i):
4. print(i,end = '')
5. print()

Output:

1
22
333
4444
55555

Using else statement with for loop


Unlike other languages like C, C++, or Java, Python allows us to use the else statement
with the for loop which can be executed only when all the iterations are exhausted.
Here, we must notice that if the loop contains any of the break statement then the else
statement will not be executed.

Example 1

1. for i in range(0,5):
2. print(i)
3. else:
4. print("for loop completely exhausted, since there is no break.")

Output:

0
1
2
3
4
for loop completely exhausted, since there is no break.

The for loop completely exhausted, since there is no break.

Example 2

1. for i in range(0,5):
2. print(i)
3. break;
4. else:print("for loop is exhausted");
5. print("The loop is broken due to break statement...came out of the loop")

In the above example, the loop is broken due to the break statement; therefore, the
else statement will not be executed. The statement present immediate next to else
block will be executed.

Output:

The loop is broken due to the break statement...came out of the loop. We will learn
more about the break statement in next tutorial.

Python While loop


The Python while loop allows a part of the code to be executed until the given
condition returns false. It is also known as a pre-tested loop.

It can be viewed as a repeating if statement. When we don't know the number of


iterations then the while loop is most effective to use.

The syntax is given below.

1. while expression:
2. statements

Here, the statements can be a single statement or a group of statements. The


expression should be any valid Python expression resulting in true or false. The true is
any non-zero value and false is 0.

Hello Java Program for Beginners

While loop Flowchart


Loop Control Statements
We can change the normal sequence of while loop's execution using the loop control
statement. When the while loop's execution is completed, all automatic objects
defined in that scope are demolished. Python offers the following control statement
to use within the while loop.

1. Continue Statement - When the continue statement is encountered, the control


transfer to the beginning of the loop. Let's understand the following example.

Example:

1. # prints all letters except 'a' and 't'


2. i = 0
3. str1 = 'javatpoint'
4.
5. while i < len(str1):
6. if str1[i] == 'a' or str1[i] == 't':
7. i += 1
8. continue
9. print('Current Letter :', a[i])
10. i += 1

Output:

Current Letter : j
Current Letter : v
Current Letter : p
Current Letter : o
Current Letter : i
Current Letter : n

2. Break Statement - When the break statement is encountered, it brings control out
of the loop.

Example:

1. # The control transfer is transfered


2. # when break statement soon it sees t
3. i = 0
4. str1 = 'javatpoint'
5.
6. while i < len(str1):
7. if str1[i] == 't':
8. i += 1
9. break
10. print('Current Letter :', str1[i])
11. i += 1

Output:

Current Letter : j
Current Letter : a
Current Letter : v
Current Letter : a

3. Pass Statement - The pass statement is used to declare the empty loop. It is also
used to define empty class, function, and control statement. Let's understand the
following example.

Example -

1. # An empty loop
2. str1 = 'javatpoint'
3. i = 0
4.
5. while i < len(str1):
6. i += 1
7. pass
8. print('Value of i :', i)

Output:

Value of i : 10

Example-1: Program to print 1 to 10 using while loop

1. i=1
2. #The while loop will iterate until condition becomes false.
3. While(i<=10):
4. print(i)
5. i=i+1

Output:

1
2
3
4
5
6
7
8
9
10

Example -2: Program to print table of given numbers.

1. i=1
2. number=0
3. b=9
4. number = int(input("Enter the number:"))
5. while i<=10:
6. print("%d X %d = %d \n"%(number,i,number*i))
7. i = i+1

Output:

Enter the number:10


10 X 1 = 10

10 X 2 = 20

10 X 3 = 30

10 X 4 = 40

10 X 5 = 50

10 X 6 = 60

10 X 7 = 70

10 X 8 = 80

10 X 9 = 90

10 X 10 = 100

Infinite while loop


If the condition is given in the while loop never becomes false, then the while loop will
never terminate, and it turns into the infinite while loop.

Any non-zero value in the while loop indicates an always-true condition, whereas
zero indicates the always-false condition. This type of approach is useful if we want
our program to run continuously in the loop without any disturbance.
Example 1

1. while (1):
2. print("Hi! we are inside the infinite while loop")

Output:

Hi! we are inside the infinite while loop


Hi! we are inside the infinite while loop

Example 2

1. var = 1
2. while(var != 2):
3. i = int(input("Enter the number:"))
4. print("Entered value is %d"%(i))

Output:

Enter the number:10


Entered value is 10
Enter the number:10
Entered value is 10
Enter the number:10
Entered value is 10
Infinite time

Using else with while loop


Python allows us to use the else statement with the while loop also. The else block is
executed when the condition given in the while statement becomes false. Like for loop,
if the while loop is broken using break statement, then the else block will not be
executed, and the statement present after else block will be executed. The else
statement is optional to use with the while loop. Consider the following example.

Example 1

1. i=1
2. while(i<=5):
3. print(i)
4. i=i+1
5. else:
6. print("The while loop exhausted")
Example 2

1. i=1
2. while(i<=5):
3. print(i)
4. i=i+1
5. if(i==3):
6. break
7. else:
8. print("The while loop exhausted")

Output:

1
2

In the above code, when the break statement encountered, then while loop stopped
its execution and skipped the else statement.

Example-3 Program to print Fibonacci numbers to given limit

1. terms = int(input("Enter the terms "))


2. # first two intial terms
3. a = 0
4. b = 1
5. count = 0
6.
7. # check if the number of terms is Zero or negative
8. if (terms <= 0):
9. print("Please enter a valid integer")
10. elif (terms == 1):
11. print("Fibonacci sequence upto",limit,":")
12. print(a)
13. else:
14. print("Fibonacci sequence:")
15. while (count < terms) :
16. print(a, end = ' ')
17. c=a+b
18. # updateing values
19. a=b
20. b=c
21.
22. count += 1

Output:

Enter the terms 10


Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34

Python break statement


The break is a keyword in python which is used to bring the program control out of
the loop. The break statement breaks the loops one by one, i.e., in the case of nested
loops, it breaks the inner loop first and then proceeds to outer loops. In other words,
we can say that break is used to abort the current execution of the program and the
control goes to the next line after the loop.

The break is commonly used in the cases where we need to break the loop for a given
condition.

The syntax of the break is given below.

1. #loop statements
2. break;

Example 1

1. list =[1,2,3,4]
2. count = 1;
3. for i in list:
4. if i == 4:
5. print("item matched")
6. count = count + 1;
7. break
8. print("found at",count,"location");

Output:

Darwin Carpet Pythons on the Balcony


item matched
found at 2 location
Example 2

1. str = "python"
2. for i in str:
3. if i == 'o':
4. break
5. print(i);

Output:

p
y
t
h

Example 3: break statement with while loop

1. i = 0;
2. while 1:
3. print(i," ",end=""),
4. i=i+1;
5. if i == 10:
6. break;
7. print("came out of while loop");

Output:

0 1 2 3 4 5 6 7 8 9 came out of while loop

Example 3

1. n=2
2. while 1:
3. i=1;
4. while i<=10:
5. print("%d X %d = %d\n"%(n,i,n*i));
6. i = i+1;
7. choice = int(input("Do you want to continue printing the table, press 0 for n
o?"))
8. if choice == 0:
9. break;
10. n=n+1

Output:

2 X 1 = 2

2 X 2 = 4

2 X 3 = 6

2 X 4 = 8

2 X 5 = 10

2 X 6 = 12

2 X 7 = 14

2 X 8 = 16

2 X 9 = 18

2 X 10 = 20

Do you want to continue printing the table, press 0 for no?1

3 X 1 = 3

3 X 2 = 6

3 X 3 = 9

3 X 4 = 12

3 X 5 = 15

3 X 6 = 18

3 X 7 = 21

3 X 8 = 24

3 X 9 = 27

3 X 10 = 30

Do you want to continue printing the table, press 0 for no?0

ython continue Statement


The continue statement in Python is used to bring the program control to the
beginning of the loop. The continue statement skips the remaining lines of code inside
the loop and start with the next iteration. It is mainly used for a particular condition
inside the loop so that we can skip some specific code for a particular condition.The
continue statement in Python is used to bring the program control to the beginning
of the loop. The continue statement skips the remaining lines of code inside the loop
and start with the next iteration. It is mainly used for a particular condition inside the
loop so that we can skip some specific code for a particular condition.

Syntax

1. #loop statements
2. continue
3. #the code to be skipped

Flow Diagram

Consider the following examples.

Example 1

1. i = 0
2. while(i < 10):
3. i = i+1
4. if(i == 5):
5. continue
6. print(i)

Output:

1
2
3
4
6
7
8
9
10
Observe the output of above code, the value 5 is skipped because we have provided
the if condition using with continue statement in while loop. When it matched with
the given condition then control transferred to the beginning of the while loop and it
skipped the value 5 from the code.

History of Java

Let's have a look at another example:

Example 2

1. str = "JavaTpoint"
2. for i in str:
3. if(i == 'T'):
4. continue
5. print(i)

Output:

J
a
v
a
p
o
i
n
t

Pass Statement
The pass statement is a null operation since nothing happens when it is executed. It is
used in the cases where a statement is syntactically needed but we don't want to use
any executable statement at its place.

For example, it can be used while overriding a parent class method in the subclass but
don't want to give its specific implementation in the subclass.

Pass is also used where the code will be written somewhere but not yet written in the
program file. Consider the following example.

Example

1. list = [1,2,3,4,5]
2. flag = 0
3. for i in list:
4. print("Current element:",i,end=" ");
5. if i==3:
6. pass
7. print("\nWe are inside pass block\n");
8. flag = 1
9. if flag==1:
10. print("\nCame out of pass\n");
11. flag=0

Output:

Current element: 1 Current element: 2 Current element: 3


We are inside pass block

Came out of pass

Current element: 4 Current element: 5

Python Pass
In Python, the pass keyword is used to execute nothing; it means, when we don't want
to execute code, the pass can be used to execute empty. It is the same as the name
refers to. It just makes the control to pass by without executing any code. If we want
to bypass any code pass statement can be used.

It is beneficial when a statement is required syntactically, but we want we don't want


to execute or execute it later. The difference between the comments and pass is that,
comments are entirely ignored by the Python interpreter, where the pass statement is
not ignored.

Suppose we have a loop, and we do not want to execute right this moment, but we
will execute in the future. Here we can use the pass.

Consider the following example.

Hello Java Program for Beginners


Example - Pass statement

1. # pass is just a placeholder for


2. # we will adde functionality later.
3. values = {'P', 'y', 't', 'h','o','n'}
4. for val in values:
5. pass

Example - 2

1. for i in [1,2,3,4,5]:
2. if(i==4):
3. pass
4. print("This is pass block",i)
5. print(i)

Output:

1. 1
2. 2
3. 3
4. This is pass block 4
5. 4
6. 5

We can create empty class or function using the pass statement.

1. # Empty Function
2. def function_name(args):
3. pass
4. #Empty Class
5. class Python:
6. pass

Python String
Till now, we have discussed numbers as the standard data-types in Python. In this section of
the tutorial, we will discuss the most popular data type in Python, i.e., string.
Python string is the collection of the characters surrounded by single quotes, double quotes,
or triple quotes. The computer does not understand the characters; internally, it stores
manipulated character as the combination of the 0's and 1's.

Each character is encoded in the ASCII or Unicode character. So we can say that Python
strings are also called the collection of Unicode characters.

In Python, strings can be created by enclosing the character or the sequence of characters in
the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the
string.

Consider the following example in Python to create a string.

Syntax:

1. str = "Hi Python !"

Here, if we check the type of the variable str using a Python script

1. print(type(str)), then it will print a string (str).

In Python, strings are treated as the sequence of characters, which means that Python doesn't
support the character data-type; instead, a single character written as 'p' is treated as the string
of length 1.

Creating String in Python :- We can create a string by enclosing the


characters in single-quotes or double- quotes. Python also provides triple-quotes to represent
the string, but it is generally used for multiline string or docstrings.

1. #Using single quotes


2. str1 = 'Hello Python'
3. print(str1)
4. #Using double quotes
5. str2 = "Hello Python"
6. print(str2)
7.
8. #Using triple quotes
9. str3 = '''''Triple quotes are generally used for
10. represent the multiline or
11. docstring'''
12. print(str3)

Output:
Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring

Strings indexing and splitting


Like other languages, the indexing of the Python strings starts from 0. For example, The
string "HELLO" is indexed as given in the below figure.

Consider the following example:

1. str = "HELLO"
2. print(str[0])
3. print(str[1])
4. print(str[2])
5. print(str[3])
6. print(str[4])
7. # It returns the IndexError because 6th index doesn't exist
8. print(str[6])

Output:

H
E
L
L
O
IndexError: string index out of range

As shown in Python, the slice operator [] is used to access the individual characters of the
string. However, we can use the : (colon) operator in Python to access the substring from the
given string. Consider the following example.
Here, we must notice that the upper range given in the slice operator is always exclusive i.e.,
if str = 'HELLO' is given, then str[1:3] will always include str[1] = 'E', str[2] = 'L' and nothing
else.

Consider the following example:

1. # Given String
2. str = "JAVATPOINT"
3. # Start Oth index to end
4. print(str[0:])
5. # Starts 1th index to 4th index
6. print(str[1:5])
7. # Starts 2nd index to 3rd index
8. print(str[2:4])
9. # Starts 0th to 2nd index
10. print(str[:3])
11. #Starts 4th to 6th index
12. print(str[4:7])

Output:

JAVATPOINT
AVAT
VA
JAV
TPO

We can do the negative slicing in the string; it starts from the rightmost character, which is
indicated as -1. The second rightmost index indicates -2, and so on. Consider the following
image.
Consider the following example

1. str = 'JAVATPOINT'
2. print(str[-1])
3. print(str[-3])
4. print(str[-2:])
5. print(str[-4:-1])
6. print(str[-7:-2])
7. # Reversing the given string
8. print(str[::-1])
9. print(str[-12])

Output:

T
I
NT
OIN
ATPOI
TNIOPTAVAJ
IndexError: string index out of range

Reassigning Strings
Updating the content of the strings is as easy as assigning it to a new string. The string object
doesn't support item assignment i.e., A string can only be replaced with new string since its
content cannot be partially replaced. Strings are immutable in Python.
Consider the following example.

Example 1

1. str = "HELLO"
2. str[0] = "h"
3. print(str)

Output:

Traceback (most recent call last):


File "12.py", line 2, in <module>
str[0] = "h";
TypeError: 'str' object does not support item assignment

However, in example 1, the string str can be assigned completely to a new content as
specified in the following example.

Example 2

1. str = "HELLO"
2. print(str)
3. str = "hello"
4. print(str)

Output:

HELLO
hello

Deleting the String


As we know that strings are immutable. We cannot delete or remove the characters from the
string. But we can delete the entire string using the del keyword.

1. str = "JAVATPOINT"
2. del str[1]

Output:

TypeError: 'str' object doesn't support item deletion

Now we are deleting entire string.

1. str1 = "JAVATPOINT"
2. del str1
3. print(str1)

Output:

NameError: name 'str1' is not defined

String Operators

Operator Description

+ It is known as concatenation operator used to join the strings given either side
of the operator.

* It is known as repetition operator. It concatenates the multiple copies of the


same string.

[] It is known as slice operator. It is used to access the sub-strings of a particular


string.

[:] It is known as range slice operator. It is used to access the characters from the
specified range.

in It is known as membership operator. It returns if a particular sub-string is present


in the specified string.

not in It is also a membership operator and does the exact reverse of in. It returns true
if a particular substring is not present in the specified string.

r/R It is used to specify the raw string. Raw strings are used in the cases where we
need to print the actual meaning of escape characters such as "C://python". To
define any string as a raw string, the character r or R is followed by the string.

% It is used to perform string formatting. It makes use of the format specifiers used
in C programming like %d or %f to map their values in python. We will discuss
how formatting is done in python.

Example
Consider the following example to understand the real use of Python operators.

1. str = "Hello"
2. str1 = " world"
3. print(str*3) # prints HelloHelloHello
4. print(str+str1)# prints Hello world
5. print(str[4]) # prints o
6. print(str[2:4]); # prints ll
7. print('w' in str) # prints false as w is not present in str
8. print('wo' not in str1) # prints false as wo is present in str1.
9. print(r'C://python37') # prints C://python37 as it is written
10. print("The string str : %s"%(str)) # prints The string str : Hello

Output:

HelloHelloHello
Hello world
o
ll
False
False
C://python37
The string str : Hello

Python String Formatting


Escape Sequence
Let's suppose we need to write the text as - They said, "Hello what's going on?"- the given
statement can be written in single quotes or double quotes but it will raise
the SyntaxError as it contains both single and double-quotes.

Example
Consider the following example to understand the real use of Python operators.

1. str = "They said, "Hello what's going on?""


2. print(str)

Output:

SyntaxError: invalid syntax

We can use the triple quotes to accomplish this problem but Python provides the escape
sequence.

The backslash(/) symbol denotes the escape sequence. The backslash can be followed by a
special character and it interpreted differently. The single quotes inside the string must be
escaped. We can apply the same as in the double quotes.

Example -
1. # using triple quotes
2. print('''''They said, "What's there?"''')
3.
4. # escaping single quotes
5. print('They said, "What\'s going on?"')
6.
7. # escaping double quotes
8. print("They said, \"What's going on?\"")

Output:

They said, "What's there?"


They said, "What's going on?"
They said, "What's going on?"

The list of an escape sequence is given below:

Sr. Escape Sequence Description Example

1. \newline It ignores the new line. print("Python1 \


Python2 \
Python3")
Output:
Python1 Python2 Python3

2. \\ Backslash print("\\")
Output:
\

3. \' Single Quotes print('\'')


Output:
'

4. \\'' Double Quotes print("\"")


Output:
"

5. \a ASCII Bell print("\a")

6. \b ASCII Backspace(BS) print("Hello \b World")


Output:
Hello World

7. \f ASCII Formfeed print("Hello \f World!")


Hello World!

8. \n ASCII Linefeed print("Hello \n World!")


Output:
Hello
World!

9. \r ASCII Carriege Return(CR) print("Hello \r World!")


Output:
World!

10. \t ASCII Horizontal Tab print("Hello \t World!")


Output:
Hello World!

11. \v ASCII Vertical Tab print("Hello \v World!")


Output:
Hello
World!

12. \ooo Character with octal value print("\110\145\154\154\157")


Output:
Hello

13 \xHH Character with hex value. print("\x48\x65\x6c\x6c\x6f")


Output:
Hello

Here is the simple example of escape sequence.

1. print("C:\\Users\\DEVANSH SHARMA\\Python32\\Lib")
2. print("This is the \n multiline quotes")
3. print("This is \x48\x45\x58 representation")

Output:

C:\Users\DEVANSH SHARMA\Python32\Lib
This is the
multiline quotes
This is HEX representation

We can ignore the escape sequence from the given string by using the raw string. We can do
this by writing r or R in front of the string. Consider the following example.

1. print(r"C:\\Users\\DEVANSH SHARMA\\Python32")

Output:

C:\\Users\\DEVANSH SHARMA\\Python32

The format() method


The format() method is the most flexible and useful method in formatting strings. The curly
braces {} are used as the placeholder in the string and replaced by the format() method
argument. Let's have a look at the given an example:

1. # Using Curly braces


2. print("{} and {} both are the best friend".format("Devansh","Abhishek"))
3.
4. #Positional Argument
5. print("{1} and {0} best players ".format("Virat","Rohit"))
6.
7. #Keyword Argument
8. print("{a},{b},{c}".format(a = "James", b = "Peter", c = "Ricky"))

Output:

Devansh and Abhishek both are the best friend


Rohit and Virat best players
James,Peter,Ricky

Python String Formatting Using % Operator


Python allows us to use the format specifiers used in C's printf statement. The format
specifiers in Python are treated in the same way as they are treated in C. However, Python
provides an additional operator %, which is used as an interface between the format specifiers
and their values. In other words, we can say that it binds the format specifiers to the values.

Consider the following example.

1. Integer = 10;
2. Float = 1.290
3. String = "Devansh"
4. print("Hi I am Integer ... My value is %d\nHi I am float ... My value is %f\nHi I am s
tring ... My value is %s"%(Integer,Float,String))

Output:

Hi I am Integer ... My value is 10


Hi I am float ... My value is 1.290000
Hi I am string ... My value is Devansh

Python String functions


Python provides various in-built functions that are used for string handling. Many String fun
Method Description

capitalize() It capitalizes the first character of the String. This function


is deprecated in python3

casefold() It returns a version of s suitable for case-less comparisons.

center(width ,fillchar) It returns a space padded string with the original string
centred with equal number of left and right spaces.

count(string,begin,end) It counts the number of occurrences of a substring in a


String between begin and end index.

decode(encoding = 'UTF8', errors = Decodes the string using codec registered for encoding.
'strict')

encode() Encode S using the codec registered for encoding. Default


encoding is 'utf-8'.

endswith(suffix It returns a Boolean value if the string terminates with given


,begin=0,end=len(string)) suffix between begin and end.

expandtabs(tabsize = 8) It defines tabs in string to multiple spaces. The default


space value is 8.

find(substring ,beginIndex, It returns the index value of the string where substring is
endIndex) found between begin index and end index.

format(value) It returns a formatted version of S, using the passed value.

index(subsring, beginIndex, It throws an exception if string is not found. It works same


endIndex) as find() method.

isalnum() It returns true if the characters in the string are


alphanumeric i.e., alphabets or numbers and there is at
least 1 character. Otherwise, it returns false.

isalpha() It returns true if all the characters are alphabets and there
is at least one character, otherwise False.

isdecimal() It returns true if all the characters of the string are decimals.

isdigit() It returns true if all the characters are digits and there is at
least one character, otherwise False.
isidentifier() It returns true if the string is the valid identifier.

islower() It returns true if the characters of a string are in lower case,


otherwise false.

isnumeric() It returns true if the string contains only numeric


characters.

isprintable() It returns true if all the characters of s are printable or s is


empty, false otherwise.

isupper() It returns false if characters of a string are in Upper case,


otherwise False.

isspace() It returns true if the characters of a string are white-space,


otherwise false.

istitle() It returns true if the string is titled properly and false


otherwise. A title string is the one in which the first
character is upper-case whereas the other characters are
lower-case.

isupper() It returns true if all the characters of the string(if exists) is


true otherwise it returns false.

join(seq) It merges the strings representation of the given sequence.

len(string) It returns the length of a string.

ljust(width[,fillchar]) It returns the space padded strings with the original string
left justified to the given width.

lower() It converts all the characters of a string to Lower case.

lstrip() It removes all leading whitespaces of a string and can also


be used to remove particular character from leading.

partition() It searches for the separator sep in S, and returns the part
before it, the separator itself, and the part after it. If the
separator is not found, return S and two empty strings.

maketrans() It returns a translation table to be used in translate


function.
replace(old,new[,count]) It replaces the old sequence of characters with the new
sequence. The max characters are replaced if max is given.

rfind(str,beg=0,end=len(str)) It is similar to find but it traverses the string in backward


direction.

rindex(str,beg=0,end=len(str)) It is same as index but it traverses the string in backward


direction.

rjust(width,[,fillchar]) Returns a space padded string having original string right


justified to the number of characters specified.

rstrip() It removes all trailing whitespace of a string and can also


be used to remove particular character from trailing.

rsplit(sep=None, maxsplit = -1) It is same as split() but it processes the string from the
backward direction. It returns the list of words in the string.
If Separator is not specified then the string splits according
to the white-space.

split(str,num=string.count(str)) Splits the string according to the delimiter str. The string
splits according to the space if the delimiter is not
provided. It returns the list of substring concatenated with
the delimiter.

splitlines(num=string.count('\n')) It returns the list of strings at each line with newline


removed.

startswith(str,beg=0,end=len(str)) It returns a Boolean value if the string starts with given str
between begin and end.

strip([chars]) It is used to perform lstrip() and rstrip() on the string.

swapcase() It inverts case of all characters in a string.

title() It is used to convert the string into the title-case i.e., The
string meEruT will be converted to Meerut.

translate(table,deletechars = '') It translates the string according to the translation table


passed in the function .

upper() It converts all the characters of a string to Upper Case.


zfill(width) Returns original string leftpadded with zeros to a total of
width characters; intended for numbers, zfill() retains any
sign given (less one zero).

rpartition()

Python List
A list in Python is used to store the sequence of various types of data. Python lists are
mutable type its mean we can modify its element after it created. However, Python
consists of six data-types that are capable to store the sequences, but the most
common and reliable type is the list.

A list can be defined as a collection of values or items of different types. The items in
the list are separated with the comma (,) and enclosed with the square brackets [].

A list can be define as below

1. L1 = ["John", 102, "USA"]


2. L2 = [1, 2, 3, 4, 5, 6]

IIf we try to print the type of L1, L2, and L3 using type() function then it will come out
to be a list.

1. print(type(L1))
2. print(type(L2))

Output:

<class 'list'>
<class 'list'>

Characteristics of Lists
The list has the following characteristics:

o The lists are ordered.


o The element of the list can access by index.
o The lists are the mutable type.
o The lists are mutable types.
o A list can store the number of various elements.

Let's check the first statement that lists are the ordered.

1. a = [1,2,"Peter",4.50,"Ricky",5,6]
2. b = [1,2,5,"Peter",4.50,"Ricky",6]
3. a ==b

Output:

False

Both lists have consisted of the same elements, but the second list changed the index
position of the 5th element that violates the order of lists. When compare both lists it
returns the false.

Lists maintain the order of the element for the lifetime. That's why it is the ordered
collection of objects.

1. a = [1, 2,"Peter", 4.50,"Ricky",5, 6]


2. b = [1, 2,"Peter", 4.50,"Ricky",5, 6]
3. a == b

Output:

True

Let's have a look at the list example in detail.

1. emp = ["John", 102, "USA"]


2. Dep1 = ["CS",10]
3. Dep2 = ["IT",11]
4. HOD_CS = [10,"Mr. Holding"]
5. HOD_IT = [11, "Mr. Bewon"]
6. print("printing employee data...")
7. print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2]))
8. print("printing departments...")
9. print("Department 1:\nName: %s, ID: %d\nDepartment 2:\nName: %s, ID: %s"
%(Dep1[0],Dep2[1],Dep2[0],Dep2[1]))
10. print("HOD Details ....")
11. print("CS HOD Name: %s, Id: %d"%(HOD_CS[1],HOD_CS[0]))
12. print("IT HOD Name: %s, Id: %d"%(HOD_IT[1],HOD_IT[0]))
13. print(type(emp),type(Dep1),type(Dep2),type(HOD_CS),type(HOD_IT))

Output:

printing employee data...


Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>

In the above example, we have created the lists which consist of the employee and
department details and printed the corresponding details. Observe the above code to
understand the concept of the list better.

List indexing and splitting


The indexing is processed in the same way as it happens with the strings. The elements
of the list can be accessed by using the slice operator [].

The index starts from 0 and goes to length - 1. The first element of the list is stored at
the 0th index, the second element of the list is stored at the 1st index, and so on.

We can get the sub-list of the list using the following syntax.

1. list_varible(start:stop:step)

o The start denotes the starting index position of the list.


o The stop denotes the last index position of the list.
o The step is used to skip the nth element within a start:stop

Consider the following example:

list = [1,2,3,4,5,6,7]
1. print(list[0])
2. print(list[1])
3. print(list[2])
4. print(list[3])
5. # Slicing the elements
6. print(list[0:6])
7. # By default the index value is 0 so its starts from the 0th element and go for i
ndex -1.
8. print(list[:])
9. print(list[2:5])
10. print(list[1:6:2])

Output:

1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]

Unlike other languages, Python provides the flexibility to use the negative indexing
also. The negative indices are counted from the right. The last element (rightmost) of
the list has the index -1; its adjacent left element is present at the index -2 and so on
until the left-most elements are encountered.

Let's have a look at the following example where we will use negative indexing to
access the elements of the list.

1. list = [1,2,3,4,5]
2. print(list[-1])
3. print(list[-3:])
4. print(list[:-1])
5. print(list[-3:-1])

Output:

5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]

As we discussed above, we can get an element by using negative indexing. In the above
code, the first print statement returned the rightmost element of the list. The second
print statement returned the sub-list, and so on.

Updating List values


Lists are the most versatile data structures in Python since they are mutable, and their
values can be updated by using the slice and assignment operator.

Python also provides append() and insert() methods, which can be used to add values
to the list.

Consider the following example to update the values inside the list.

1. list = [1, 2, 3, 4, 5, 6]
2. print(list)
3. # It will assign value to the value to the second index
4. list[2] = 10
5. print(list)
6. # Adding multiple-element
7. list[1:3] = [89, 78]
8. print(list)
9. # It will add value at the end of the list
10. list[-1] = 25
11. print(list)

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
The list elements can also be deleted by using the del keyword. Python also provides
us the remove() method if we do not know which element is to be deleted from the
list.

Consider the following example to delete the list elements.

1. list = [1, 2, 3, 4, 5, 6]
2. print(list)
3. # It will assign value to the value to second index
4. list[2] = 10
5. print(list)
6. # Adding multiple element
7. list[1:3] = [89, 78]
8. print(list)
9. # It will add value at the end of the list
10. list[-1] = 25
11. print(list)

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

Python List Operations


The concatenation (+) and repetition (*) operators work in the same way as they were
working with the strings.

Let's see how the list responds to various operators.

1. Consider a Lists l1 = [1, 2, 3, 4], and l2 = [5, 6, 7, 8] to perform operation.

Operator Description Example

Repetition The repetition operator enables the list elements to L1*2 = [1, 2, 3, 4, 1,
2, 3, 4]
be repeated multiple times.

Concatenation It concatenates the list mentioned on either side of l1+l2 = [1, 2, 3, 4, 5,


6, 7, 8]
the operator.
Membership It returns true if a particular item exists in a particular print(2 in l1) prints
True.
list otherwise false.

Iteration The for loop is used to iterate over the list elements. for i in l1:
print(i)
Output
1
2
3
4

Length It is used to get the length of the list len(l1) = 4

Iterating a List
A list can be iterated by using a for - in loop. A simple list containing four strings, which
can be iterated as follows.

1. list = ["John", "David", "James", "Jonathan"]


2. for i in list:
3. # The i variable will iterate over the elements of the List and contains each e
lement in each iteration.
4. print(i)

Output:

John
David
James
Jonathan

Adding elements to the list


Python provides append() function which is used to add an element to the list.
However, the append() function can only add value to the end of the list.

Consider the following example in which, we are taking the elements of the list from
the user and printing the list on the console.

1. #Declaring the empty list


2. l =[]
3. #Number of elements will be entered by the user
4. n = int(input("Enter the number of elements in the list:"))
5. # for loop to take the input
6. for i in range(0,n):
7. # The input is taken from the user and added to the list as the item
8. l.append(input("Enter the item:"))
9. print("printing the list items..")
10. # traversal loop to print the list items
11. for i in l:
12. print(i, end = " ")

Output:

Enter the number of elements in the list:5


Enter the item:25
Enter the item:46
Enter the item:12
Enter the item:75
Enter the item:42
printing the list items
25 46 12 75 42

Removing elements from the list


Python provides the remove() function which is used to remove the element from the
list. Consider the following example to understand this concept.

Example -

1. list = [0,1,2,3,4]
2. print("printing original list: ");
3. for i in list:
4. print(i,end=" ")
5. list.remove(2)
6. print("\nprinting the list after the removal of first element...")
7. for i in list:
8. print(i,end=" ")

Output:

printing original list:


0 1 2 3 4
printing the list after the removal of first element...
0 1 3 4

Python List Built-in functions


Python provides the following built-in functions, which can be used with the lists.

SN Function Description Example

1 cmp(list1, It compares the elements of This method is not used in the Python 3 and
list2) both the lists. the above versions.

2 len(list) It is used to calculate the L1 = [1,2,3,4,5,6,7,8]


print(len(L1))
length of the list. 8

3 max(list) It returns the maximum L1 = [12,34,26,48,72]


print(max(L1))
element of the list. 72

4 min(list) It returns the minimum L1 = [12,34,26,48,72]


print(min(L1))
element of the list. 12

5 list(seq) It converts any sequence to str = "Johnson"


s = list(str)
the list. print(type(s))
<class list>

Let's have a look at the few list examples.

Example: 1- Write the program to remove the duplicate element of the list.

1. list1 = [1,2,2,3,55,98,65,65,13,29]
2. # Declare an empty list that will store unique values
3. list2 = []
4. for i in list1:
5. if i not in list2:
6. list2.append(i)
7. print(list2)

Output:

[1, 2, 3, 55, 98, 65, 13, 29]

Example:2- Write a program to find the sum of the element in the list.

1. list1 = [3,4,5,9,10,12,24]
2. sum = 0
3. for i in list1:
4. sum = sum+i
5. print("The sum is:",sum)

Output:

The sum is: 67

Example: 3- Write the program to find the lists consist of at least one common
element.

1. list1 = [1,2,3,4,5,6]
2. list2 = [7,8,9,2,10]
3. for x in list1:
4. for y in list2:
5. if x == y:
6. print("The common element is:",x)

Output:

The common element is: 2

Python Tuple
Python Tuple is used to store the sequence of immutable Python objects. The tuple is
similar to lists since the value of the items stored in the list can be changed, whereas
the tuple is immutable, and the value of the items stored in the tuple cannot be
changed.

Creating a tuple
A tuple can be written as the collection of comma-separated (,) values enclosed with
the small () brackets. The parentheses are optional but it is good practice to use. A
tuple can be defined as follows.

1. T1 = (101, "Peter", 22)


2. T2 = ("Apple", "Banana", "Orange")
3. T3 = 10,20,30,40,50
4.
5. print(type(T1))
6. print(type(T2))
7. print(type(T3))
Output:

<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
Note: The tuple which is created without using parentheses is also known as tuple
packing.

An empty tuple can be created as follows.

History of Java

T4 = ()

Creating a tuple with single element is slightly different. We will need to put comma
after the element to declare the tuple.

1. tup1 = ("JavaTpoint")
2. print(type(tup1))
3. #Creating a tuple with single element
4. tup2 = ("JavaTpoint",)
5. print(type(tup2))

Output:

<class 'str'>
<class 'tuple'>

A tuple is indexed in the same way as the lists. The items in the tuple can be accessed
by using their specific index value.

Consider the following example of tuple:

Example - 1

1. tuple1 = (10, 20, 30, 40, 50, 60)


2. print(tuple1)
3. count = 0
4. for i in tuple1:
5. print("tuple1[%d] = %d"%(count, i))
6. count = count+1

Output:

(10, 20, 30, 40, 50, 60)


tuple1[0] = 10
tuple1[1] = 20
tuple1[2] = 30
tuple1[3] = 40
tuple1[4] = 50
tuple1[5] = 60

Example - 2

1. tuple1 = tuple(input("Enter the tuple elements ..."))


2. print(tuple1)
3. count = 0
4. for i in tuple1:
5. print("tuple1[%d] = %s"%(count, i))
6. count = count+1

Output:

Enter the tuple elements ...123456


('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6

A tuple is indexed in the same way as the lists. The items in the tuple can be accessed
by using their specific index value.

We will see all these aspects of tuple in this section of the tutorial.

Tuple indexing and slicing


The indexing and slicing in the tuple are similar to lists. The indexing in the tuple starts
from 0 and goes to length(tuple) - 1.

The items in the tuple can be accessed by using the index [] operator. Python also
allows us to use the colon operator to access multiple items in the tuple.

Consider the following image to understand the indexing and slicing in detail.
Consider the following example:

1. tup = (1,2,3,4,5,6,7)
2. print(tup[0])
3. print(tup[1])
4. print(tup[2])
5. # It will give the IndexError
6. print(tup[8])

Output:

1
2
3
tuple index out of range

In the above code, the tuple has 7 elements which denote 0 to 6. We tried to access
an element outside of tuple that raised an IndexError.

1. tuple = (1,2,3,4,5,6,7)
2. #element 1 to end
3. print(tuple[1:])
4. #element 0 to 3 element
5. print(tuple[:4])
6. #element 1 to 4 element
7. print(tuple[1:5])
8. # element 0 to 6 and take step of 2
9. print(tuple[0:6:2])

Output:

(2, 3, 4, 5, 6, 7)
(1, 2, 3, 4)
(1, 2, 3, 4)
(1, 3, 5)
Negative Indexing
The tuple element can also access by using negative indexing. The index of -1 denotes
the rightmost element and -2 to the second last item and so on.

The elements from left to right are traversed using the negative indexing. Consider the
following example:

1. tuple1 = (1, 2, 3, 4, 5)
2. print(tuple1[-1])
3. print(tuple1[-4])
4. print(tuple1[-3:-1])
5. print(tuple1[:-1])
6. print(tuple1[-2:])

Output:

5
2
(3, 4)
(1, 2, 3, 4)
(4, 5)

Deleting Tuple
Unlike lists, the tuple items cannot be deleted by using the del keyword as tuples are
immutable. To delete an entire tuple, we can use the del keyword with the tuple name.

Consider the following example.

1. tuple1 = (1, 2, 3, 4, 5, 6)
2. print(tuple1)
3. del tuple1[0]
4. print(tuple1)
5. del tuple1
6. print(tuple1)

Output:

(1, 2, 3, 4, 5, 6)
Traceback (most recent call last):
File "tuple.py", line 4, in <module>
print(tuple1)
NameError: name 'tuple1' is not defined
Basic Tuple operations
The operators like concatenation (+), repetition (*), Membership (in) works in the same
way as they work with the list. Consider the following table for more detail.

Let's say Tuple t = (1, 2, 3, 4, 5) and Tuple t1 = (6, 7, 8, 9) are declared.

Operator Description Example

Repetition The repetition operator enables the tuple elements to T1*2 = (1, 2, 3, 4, 5, 1,
2, 3, 4, 5)
be repeated multiple times.

Concatenation It concatenates the tuple mentioned on either side of T1+T2 = (1, 2, 3, 4, 5, 6,


7, 8, 9)
the operator.

Membership It returns true if a particular item exists in the tuple print (2 in T1) prints
True.
otherwise false

Iteration The for loop is used to iterate over the tuple elements. for i in T1:
print(i)
Output
1
2
3
4
5

Length It is used to get the length of the tuple. len(T1) = 5

Python Tuple inbuilt functions

SN Function Description

1 cmp(tuple1, It compares two tuples and returns true if tuple1 is greater than tuple2
tuple2) otherwise false.

2 len(tuple) It calculates the length of the tuple.

3 max(tuple) It returns the maximum element of the tuple


4 min(tuple) It returns the minimum element of the tuple.

5 tuple(seq) It converts the specified sequence to the tuple.

Where use tuple?


Using tuple instead of list is used in the following scenario.

1. Using tuple instead of list gives us a clear idea that tuple data is constant and must
not be changed.

2. Tuple can simulate a dictionary without keys. Consider the following nested
structure, which can be used as a dictionary.

1. [(101, "John", 22), (102, "Mike", 28), (103, "Dustin", 30)]

List vs. Tuple

SN List Tuple

1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().

2 The List is mutable. The tuple is immutable.

3 The List has the a variable length. The tuple has the fixed length.

4 The list provides more functionality than a The tuple provides less functionality than the list.
tuple.

5 The list is used in the scenario in which we The tuple is used in the cases where we need to
need to store the simple collections with no store the read-only collections i.e., the value of the
constraints where the value of the items can items cannot be changed. It can be used as the key
be changed. inside the dictionary.

6 The lists are less memory efficient than a The tuples are more memory efficient because of its
tuple. immutability.
Python Set
A Python set is the collection of the unordered items. Each element in the set must be
unique, immutable, and the sets remove the duplicate elements. Sets are mutable
which means we can modify it after its creation.

Unlike other collections in Python, there is no index attached to the elements of the
set, i.e., we cannot directly access any element of the set by the index. However, we
can print them all together, or we can get the list of elements by looping through the
set.

Creating a set
The set can be created by enclosing the comma-separated immutable items with the
curly braces {}. Python also provides the set() method, which can be used to create the
set by the passed sequence.

Example 1: Using curly braces

1. Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",


"Sunday"}
2. print(Days)
3. print(type(Days))
4. print("looping through the set elements ... ")
5. for i in Days:
6. print(i) 239
Difference between JDK, JRE, and JVM
{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday',
'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday
Example 2: Using set() method

1. Days = set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturda


y", "Sunday"])
2. print(Days)
3. print(type(Days))
4. print("looping through the set elements ... ")
5. for i in Days:
6. print(i)

Output:

{'Friday', 'Wednesday', 'Thursday', 'Saturday', 'Monday', 'Tuesday',


'Sunday'}
<class 'set'>
looping through the set elements ...
Friday
Wednesday
Thursday
Saturday
Monday
Tuesday
Sunday

It can contain any type of element such as integer, float, tuple etc. But mutable
elements (list, dictionary, set) can't be a member of set. Consider the following
example.

1. # Creating a set which have immutable elements


2. set1 = {1,2,3, "JavaTpoint", 20.5, 14}
3. print(type(set1))
4. #Creating a set which have mutable element
5. set2 = {1,2,3,["Javatpoint",4]}
6. print(type(set2))

Output:

<class 'set'>

Traceback (most recent call last)


<ipython-input-5-9605bb6fbc68> in <module>
4
5 #Creating a set which holds mutable elements
----> 6 set2 = {1,2,3,["Javatpoint",4]}
7 print(type(set2))

TypeError: unhashable type: 'list'


In the above code, we have created two sets, the set set1 have immutable elements
and set2 have one mutable element as a list. While checking the type of set2, it raised
an error, which means set can contain only immutable elements.

Creating an empty set is a bit different because empty curly {} braces are also used to
create a dictionary as well. So Python provides the set() method used without an
argument to create an empty set.

1. # Empty curly braces will create dictionary


2. set3 = {}
3. print(type(set3))
4.
5. # Empty set using set() function
6. set4 = set()
7. print(type(set4))

Output:

<class 'dict'>
<class 'set'>

Let's see what happened if we provide the duplicate element to the set.

1. set5 = {1,2,4,4,5,8,9,9,10}
2. print("Return set with unique elements:",set5)

Output:

Return set with unique elements: {1, 2, 4, 5, 8, 9, 10}

In the above code, we can see that set5 consisted of multiple duplicate elements when
we printed it remove the duplicity from the set.

Adding items to the set


Python provides the add() method and update() method which can be used to add
some particular item to the set. The add() method is used to add a single element
whereas the update() method is used to add multiple elements to the set. Consider
the following example.
Example: 1 - Using add() method

1. Months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(months)
4. print("\nAdding other months to the set...");
5. Months.add("July");
6. Months.add ("August");
7. print("\nPrinting the modified set...");
8. print(Months)
9. print("\nlooping through the set elements ... ")
10. for i in Months:
11. print(i)

Output:

printing the original set ...


{'February', 'May', 'April', 'March', 'June', 'January'}

Adding other months to the set...

Printing the modified set...


{'February', 'July', 'May', 'April', 'March', 'August', 'June', 'January'}

looping through the set elements ...


February
July
May
April
March
August
June
January

To add more than one item in the set, Python provides the update() method. It
accepts iterable as an argument.

Consider the following example.

Example - 2 Using update() function

1. Months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(Months)
4. print("\nupdating the original set ... ")
5. Months.update(["July","August","September","October"]);
6. print("\nprinting the modified set ... ")
7. print(Months);

Output:

printing the original set ...


{'January', 'February', 'April', 'May', 'June', 'March'}

updating the original set ...


printing the modified set ...
{'January', 'February', 'April', 'August', 'October', 'May', 'June', 'July',
'September', 'March'}

Removing items from the set


Python provides the discard() method and remove() method which can be used to
remove the items from the set. The difference between these function, using discard()
function if the item does not exist in the set then the set remain unchanged whereas
remove() method will through an error.

Consider the following example.

Example-1 Using discard() method

1. months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(months)
4. print("\nRemoving some months from the set...");
5. months.discard("January");
6. months.discard("May");
7. print("\nPrinting the modified set...");
8. print(months)
9. print("\nlooping through the set elements ... ")
10. for i in months:
11. print(i)

Output:

printing the original set ...


{'February', 'January', 'March', 'April', 'June', 'May'}

Removing some months from the set...

Printing the modified set...


{'February', 'March', 'April', 'June'}
looping through the set elements ...
February
March
April
June

Python provides also the remove() method to remove the item from the set. Consider
the following example to remove the items using remove() method.

Example-2 Using remove() function

1. months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(months)
4. print("\nRemoving some months from the set...");
5. months.remove("January");
6. months.remove("May");
7. print("\nPrinting the modified set...");
8. print(months)

Output:

printing the original set ...


{'February', 'June', 'April', 'May', 'January', 'March'}

Removing some months from the set...

Printing the modified set...


{'February', 'June', 'April', 'March'}

We can also use the pop() method to remove the item. Generally, the pop() method
will always remove the last item but the set is unordered, we can't determine which
element will be popped from set.

Consider the following example to remove the item from the set using pop() method.

1. Months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(Months)
4. print("\nRemoving some months from the set...");
5. Months.pop();
6. Months.pop();
7. print("\nPrinting the modified set...");
8. print(Months)
Output:

printing the original set ...


{'June', 'January', 'May', 'April', 'February', 'March'}

Removing some months from the set...

Printing the modified set...


{'May', 'April', 'February', 'March'}

In the above code, the last element of the Month set is March but the pop() method
removed the June and January because the set is unordered and the pop() method
could not determine the last element of the set.

Python provides the clear() method to remove all the items from the set.

Consider the following example.

1. Months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(Months)
4. print("\nRemoving all the items from the set...");
5. Months.clear()
6. print("\nPrinting the modified set...")
7. print(Months)

Output:

printing the original set ...


{'January', 'May', 'June', 'April', 'March', 'February'}

Removing all the items from the set...

Printing the modified set...


set()

Difference between discard() and remove()


Despite the fact that discard() and remove() method both perform the same task,
There is one main difference between discard() and remove().

If the key to be deleted from the set using discard() doesn't exist in the set, the Python
will not give the error. The program maintains its control flow.

On the other hand, if the item to be deleted from the set using remove() doesn't exist
in the set, the Python will raise an error.
Consider the following example.

Example-

1. Months = set(["January","February", "March", "April", "May", "June"])


2. print("\nprinting the original set ... ")
3. print(Months)
4. print("\nRemoving items through discard() method...");
5. Months.discard("Feb"); #will not give an error although the key feb is not avail
able in the set
6. print("\nprinting the modified set...")
7. print(Months)
8. print("\nRemoving items through remove() method...");
9. Months.remove("Jan") #will give an error as the key jan is not available in the s
et.
10. print("\nPrinting the modified set...")
11. print(Months)

Output:

printing the original set ...


{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through discard() method...

printing the modified set...


{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through remove() method...


Traceback (most recent call last):
File "set.py", line 9, in
Months.remove("Jan")
KeyError: 'Jan'

Python Set Operations


Set can be performed mathematical operation such as union, intersection, difference,
and symmetric difference. Python provides the facility to carry out these operations
with operators or methods. We describe these operations as follows.

Union of two Sets


The union of two sets is calculated by using the pipe (|) operator. The union of the two
sets contains all the items that are present in both the sets.
Consider the following example to calculate the union of two sets.

Example 1: using union | operator

1. Days1 = {"Monday","Tuesday","Wednesday","Thursday", "Sunday"}


2. Days2 = {"Friday","Saturday","Sunday"}
3. print(Days1|Days2) #printing the union of the sets

Output:

{'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday',


'Thursday'}

Python also provides the union() method which can also be used to calculate the
union of two sets. Consider the following example.

Example 2: using union() method

1. Days1 = {"Monday","Tuesday","Wednesday","Thursday"}
2. Days2 = {"Friday","Saturday","Sunday"}
3. print(Days1.union(Days2)) #printing the union of the sets

Output:

{'Friday', 'Monday', 'Tuesday', 'Thursday', 'Wednesday', 'Sunday',


'Saturday'}

Intersection of two sets


The intersection of two sets can be performed by the and & operator or
the intersection() function. The intersection of the two sets is given as the set of the
elements that common in both sets.
Consider the following example.

Example 1: Using & operator

1. Days1 = {"Monday","Tuesday", "Wednesday", "Thursday"}


2. Days2 = {"Monday","Tuesday","Sunday", "Friday"}
3. print(Days1&Days2) #prints the intersection of the two sets

Output:

{'Monday', 'Tuesday'}

Example 2: Using intersection() method

1. set1 = {"Devansh","John", "David", "Martin"}


2. set2 = {"Steve", "Milan", "David", "Martin"}
3. print(set1.intersection(set2)) #prints the intersection of the two sets

Output:

{'Martin', 'David'}

Example 3:

1. set1 = {1,2,3,4,5,6,7}
2. set2 = {1,2,20,32,5,9}
3. set3 = set1.intersection(set2)
4. print(set3)

Output:

{1,2,5}

The intersection_update() method


The intersection_update() method removes the items from the original set that are
not present in both the sets (all the sets if more than one are specified).

The intersection_update() method is different from the intersection() method since it


modifies the original set by removing the unwanted items, on the other hand, the
intersection() method returns a new set.
Consider the following example.

1. a = {"Devansh", "bob", "castle"}


2. b = {"castle", "dude", "emyway"}
3. c = {"fuson", "gaurav", "castle"}
4.
5. a.intersection_update(b, c)
6.
7. print(a)

Output:

{'castle'}

Difference between the two sets


The difference of two sets can be calculated by using the subtraction (-) operator
or intersection() method. Suppose there are two sets A and B, and the difference is
A-B that denotes the resulting set will be obtained that element of A, which is not
present in the set B.

Consider the following example.

Example 1 : Using subtraction ( - ) operator

1. Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"}


2. Days2 = {"Monday", "Tuesday", "Sunday"}
3. print(Days1-Days2) #{"Wednesday", "Thursday" will be printed}

Output:

{'Thursday', 'Wednesday'}

Example 2 : Using difference() method

1. Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"}


2. Days2 = {"Monday", "Tuesday", "Sunday"}
print(Days1.difference(Days2)) # prints the difference of the two sets Days1 and Days
2

Output:

{'Thursday', 'Wednesday'}

Symmetric Difference of two sets


The symmetric difference of two sets is calculated by ^ operator
or symmetric_difference() method. Symmetric difference of sets, it removes that
element which is present in both sets. Consider the following example:

Example - 1: Using ^ operator

1. a = {1,2,3,4,5,6}
2. b = {1,2,9,8,10}
3. c = a^b
4. print(c)

Output:

{3, 4, 5, 6, 8, 9, 10}

Example - 2: Using symmetric_difference() method

1. a = {1,2,3,4,5,6}
2. b = {1,2,9,8,10}
3. c = a.symmetric_difference(b)
4. print(c)

Output:

{3, 4, 5, 6, 8, 9, 10}

Set comparisons
Python allows us to use the comparison operators i.e., <, >, <=, >= , == with the sets
by using which we can check whether a set is a subset, superset, or equivalent to other
set. The boolean true or false is returned depending upon the items present inside the
sets.

Consider the following example.

1. Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"}


2. Days2 = {"Monday", "Tuesday"}
3. Days3 = {"Monday", "Tuesday", "Friday"}
4.
5. #Days1 is the superset of Days2 hence it will print true.
6. print (Days1>Days2)
7.
8. #prints false since Days1 is not the subset of Days2
9. print (Days1<Days2)
10.
11. #prints false since Days2 and Days3 are not equivalent
12. print (Days2 == Days3)

Output:

True
False
False

FrozenSets
The frozen sets are the immutable form of the normal sets, i.e., the items of the frozen
set cannot be changed and therefore it can be used as a key in the dictionary.

The elements of the frozen set cannot be changed after the creation. We cannot
change or append the content of the frozen sets by using the methods like add() or
remove().

The frozenset() method is used to create the frozenset object. The iterable sequence is
passed into this method which is converted into the frozen set as a return type of the
method.
Consider the following example to create the frozen set.

1. Frozenset = frozenset([1,2,3,4,5])
2. print(type(Frozenset))
3. print("\nprinting the content of frozen set...")
4. for i in Frozenset:
5. print(i);
6. Frozenset.add(6) #gives an error since we cannot change the content of Froze
nset after creation

Output:

<class 'frozenset'>

printing the content of frozen set...


1
2
3
4
5
Traceback (most recent call last):
File "set.py", line 6, in <module>
Frozenset.add(6) #gives an error since we can change the content of
Frozenset after creation
AttributeError: 'frozenset' object has no attribute 'add'

Frozenset for the dictionary


If we pass the dictionary as the sequence inside the frozenset() method, it will take only
the keys from the dictionary and returns a frozenset that contains the key of the
dictionary as its elements.

Consider the following example.

1. Dictionary = {"Name":"John", "Country":"USA", "ID":101}


2. print(type(Dictionary))
3. Frozenset = frozenset(Dictionary); #Frozenset will contain the keys of the dicti
onary
4. print(type(Frozenset))
5. for i in Frozenset:
6. print(i)

Output:

<class 'dict'>
<class 'frozenset'>
Name
Country
ID

Set Programming Example


Example - 1: Write a program to remove the given number from the set.

1. my_set = {1,2,3,4,5,6,12,24}
2. n = int(input("Enter the number you want to remove"))
3. my_set.discard(n)
4. print("After Removing:",my_set)

Output:

Enter the number you want to remove:12


After Removing: {1, 2, 3, 4, 5, 6, 24}

Example - 2: Write a program to add multiple elements to the set.

1. set1 = set([1,2,4,"John","CS"])
2. set1.update(["Apple","Mango","Grapes"])
3. print(set1)

Output:

{1, 2, 4, 'Apple', 'John', 'CS', 'Mango', 'Grapes'}

Example - 3: Write a program to find the union between two set.

1. set1 = set(["Peter","Joseph", 65,59,96])


2. set2 = set(["Peter",1,2,"Joseph"])
3. set3 = set1.union(set2)
4. print(set3)

Output:

{96, 65, 2, 'Joseph', 1, 'Peter', 59}


Example- 4: Write a program to find the intersection between two sets.

1. set1 = {23,44,56,67,90,45,"Javatpoint"}
2. set2 = {13,23,56,76,"Sachin"}
3. set3 = set1.intersection(set2)
4. print(set3)

Output:

{56, 23}

Example - 5: Write the program to add element to the frozenset.

1. set1 = {23,44,56,67,90,45,"Javatpoint"}
2. set2 = {13,23,56,76,"Sachin"}
3. set3 = set1.intersection(set2)
4. print(set3)

Output:

TypeError: 'frozenset' object does not support item assignment

Above code raised an error because frozensets are immutable and can't be changed
after creation.

Example - 6: Write the program to find the issuperset, issubset and superset.

1. set1 = set(["Peter","James","Camroon","Ricky","Donald"])
2. set2 = set(["Camroon","Washington","Peter"])
3. set3 = set(["Peter"])
4.
5. issubset = set1 >= set2
6. print(issubset)
7. issuperset = set1 <= set2
8. print(issuperset)
9. issubset = set3 <= set2
10. print(issubset)
11. issuperset = set2 >= set3
12. print(issuperset)
Output:

False
False
True
True

Python Built-in set methods


Python contains the following methods to be used with the sets.

SN Method Description

1 add(item) It adds an item to the set. It has no effect if the item is


already present in the set.

2 clear() It deletes all the items from the set.

3 copy() It returns a shallow copy of the set.

4 difference_update(....) It modifies this set by removing all the items that are
also present in the specified sets.

5 discard(item) It removes the specified item from the set.

6 intersection() It returns a new set that contains only the common


elements of both the sets. (all the sets if more than two
are specified).

7 intersection_update(....) It removes the items from the original set that are not
present in both the sets (all the sets if more than one
are specified).

8 Isdisjoint(....) Return True if two sets have a null intersection.

9 Issubset(....) Report whether another set contains this set.

10 Issuperset(....) Report whether this set contains another set.

11 pop() Remove and return an arbitrary set element that is the


last element of the set. Raises KeyError if the set is
empty.

12 remove(item) Remove an element from a set; it must be a member. If


the element is not a member, raise a KeyError.
13 symmetric_difference(....) Remove an element from a set; it must be a member. If
the element is not a member, raise a KeyError.

14 symmetric_difference_update(....) Update a set with the symmetric difference of itself and


another.

15 union(....) Return the union of sets as a new set.


(i.e. all elements that are in either set.)

16 update() Update a set with the union of itself and others.

Python Dictionary
Python Dictionary is used to store the data in a key-value pair format. The dictionary is the
data type in Python, which can simulate the real-life data arrangement where some specific
value exists for some particular key. It is the mutable data-structure. The dictionary is defined
into element Keys and values.

o Keys must be a single element


o Value can be any type such as list, tuple, integer, etc.

In other words, we can say that a dictionary is the collection of key-value pairs where the
value can be any Python object. In contrast, the keys are the immutable Python object, i.e.,
Numbers, string, or tuple.

Creating the dictionary


The dictionary can be created by using multiple key-value pairs enclosed with the curly
brackets {}, and each key is separated from its value by the colon (:).The syntax to define the
dictionary is given below.

Syntax:

Snake bites man sitting on toilet

1. Dict = {"Name": "Tom", "Age": 22}

In the above dictionary Dict, The keys Name and Age are the string that is an immutable
object.

Let's see an example to create a dictionary and print its content.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}


2. print(type(Employee))
3. print("printing Employee data .... ")
4. print(Employee)

Output

<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}

Python provides the built-in function dict() method which is also used to create dictionary.
The empty curly braces {} is used to create empty dictionary.

1. # Creating an empty Dictionary


2. Dict = {}
3. print("Empty Dictionary: ")
4. print(Dict)
5.
6. # Creating a Dictionary
7. # with dict() method
8. Dict = dict({1: 'Java', 2: 'T', 3:'Point'})
9. print("\nCreate Dictionary by using dict(): ")
10. print(Dict)
11.
12. # Creating a Dictionary
13. # with each item as a Pair
14. Dict = dict([(1, 'Devansh'), (2, 'Sharma')])
15. print("\nDictionary with each item as a pair: ")
16. print(Dict)

Output:

Empty Dictionary:
{}

Create Dictionary by using dict():


{1: 'Java', 2: 'T', 3: 'Point'}

Dictionary with each item as a pair:


{1: 'Devansh', 2: 'Sharma'}

Accessing the dictionary values


We have discussed how the data can be accessed in the list and tuple by using the indexing.
However, the values can be accessed in the dictionary by using the keys as keys are unique in
the dictionary.

The dictionary values can be accessed in the following way.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}


2. print(type(Employee))
3. print("printing Employee data .... ")
4. print("Name : %s" %Employee["Name"])
5. print("Age : %d" %Employee["Age"])
6. print("Salary : %d" %Employee["salary"])
7. print("Company : %s" %Employee["Company"])

Output:

<class 'dict'>
printing Employee data ....
Name : John
Age : 29
Salary : 25000
Company : GOOGLE

Python provides us with an alternative to use the get() method to access the dictionary values.
It would give the same result as given by the indexing.

Adding dictionary values


The dictionary is a mutable data type, and its values can be updated by using the specific
keys. The value can be updated along with key Dict[key] = value. The update() method is
also used to update an existing value.

Note: If the key-value already present in the dictionary, the value gets updated. Otherwise,
the new keys added in the dictionary.

Let's see an example to update the dictionary values.

Example - 1:

1. # Creating an empty Dictionary


2. Dict = {}
3. print("Empty Dictionary: ")
4. print(Dict)
5.
6. # Adding elements to dictionary one at a time
7. Dict[0] = 'Peter'
8. Dict[2] = 'Joseph'
9. Dict[3] = 'Ricky'
10. print("\nDictionary after adding 3 elements: ")
11. print(Dict)
12.
13. # Adding set of values
14. # with a single Key
15. # The Emp_ages doesn't exist to dictionary
16. Dict['Emp_ages'] = 20, 33, 24
17. print("\nDictionary after adding 3 elements: ")
18. print(Dict)
19.
20. # Updating existing Key's Value
21. Dict[3] = 'JavaTpoint'
22. print("\nUpdated key value: ")
23. print(Dict)

Output:

Empty Dictionary:
{}

Dictionary after adding 3 elements:


{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}

Dictionary after adding 3 elements:


{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}

Updated key value:


{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}

Example - 2:

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. print(type(Employee))
3. print("printing Employee data .... ")
4. print(Employee)
5. print("Enter the details of the new employee....");
6. Employee["Name"] = input("Name: ");
7. Employee["Age"] = int(input("Age: "));
8. Employee["salary"] = int(input("Salary: "));
9. Employee["Company"] = input("Company:");
10. print("printing the new data");
11. print(Employee)

Output:

Empty Dictionary:
{}

Dictionary after adding 3 elements:


{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}

Dictionary after adding 3 elements:


{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}

Updated key value:


{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}

Deleting elements using del keyword


The items of the dictionary can be deleted by using the del keyword as given below.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. print(type(Employee))
3. print("printing Employee data .... ")
4. print(Employee)
5. print("Deleting some of the employee data")
6. del Employee["Name"]
7. del Employee["Company"]
8. print("printing the modified information ")
9. print(Employee)
10. print("Deleting the dictionary: Employee");
11. del Employee
12. print("Lets try to print it again ");
13. print(Employee)

Output:

<class 'dict'>
printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Deleting some of the employee data
printing the modified information
{'Age': 29, 'salary': 25000}
Deleting the dictionary: Employee
Lets try to print it again
NameError: name 'Employee' is not defined

The last print statement in the above code, it raised an error because we tried to print the
Employee dictionary that already deleted.

o Using pop() method

The pop() method accepts the key as an argument and remove the associated value. Consider
the following example.

1. # Creating a Dictionary
2. Dict = {1: 'JavaTpoint', 2: 'Peter', 3: 'Thomas'}
3. # Deleting a key
4. # using pop() method
5. pop_ele = Dict.pop(3)
6. print(Dict)

Output:

{1: 'JavaTpoint', 2: 'Peter'}

Python also provides a built-in methods popitem() and clear() method for remove elements
from the dictionary. The popitem() removes the arbitrary element from a dictionary, whereas
the clear() method removes all elements to the whole dictionary.

Iterating Dictionary
A dictionary can be iterated using for loop as given below.

Example 1
# for loop to print all the keys of a dictionary

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. for x in Employee:
3. print(x)

Output:

Name
Age
salary
Company
Example 2
#for loop to print all the values of the dictionary

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. for x in Employee:
3. print(Employee[x])

Output:

John
29
25000
GOOGLE

Example - 3
#for loop to print the values of the dictionary by using values() method.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. for x in Employee.values():
3. print(x)

Output:

John
29
25000
GOOGLE

Example 4
#for loop to print the items of the dictionary by using items() method.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}

2. for x in Employee.items():
3. print(x)

Output:

('Name', 'John')
('Age', 29)
('salary', 25000)
('Company', 'GOOGLE')

Properties of Dictionary keys


1. In the dictionary, we cannot store multiple values for the same keys. If we pass more than
one value for a single key, then the value which is last assigned is considered as the value of
the key.

Consider the following example.

1. Employee={"Name":"John","Age":29,"Salary":25000,"Company":"GOOGLE","Nam
e":"John"}
2. for x,y in Employee.items():
3. print(x,y)

Output:

Name John
Age 29
Salary 25000
Company GOOGLE

2. In python, the key cannot be any mutable object. We can use numbers, strings, or tuples as
the key, but we cannot use any mutable object like the list as the key in the dictionary.

Consider the following example.

1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE",[1


00,201,301]:"Department ID"}
2. for x,y in Employee.items():
3. print(x,y)

Output:

Traceback (most recent call last):


File "dictionary.py", line 1, in
Employee = {"Name": "John", "Age": 29,
"salary":25000,"Company":"GOOGLE",[100,201,301]:"Department ID"}
TypeError: unhashable type: 'list'

Built-in Dictionary functions


The built-in python dictionary methods along with the description are given below.

SN Function Description
1 cmp(dict1, It compares the items of both the dictionary and returns true if the first
dict2) dictionary values are greater than the second dictionary, otherwise it
returns false.

2 len(dict) It is used to calculate the length of the dictionary.

3 str(dict) It converts the dictionary into the printable string representation.

4 type(variable) It is used to print the type of the passed variable.

Built-in Dictionary methods


The built-in python dictionary methods along with the description are given below.

SN Method Description

1 dic.clear() It is used to delete all the items of the dictionary.

2 dict.copy() It returns a shallow copy of the dictionary.

3 dict.fromkeys(iterable, value = Create a new dictionary from the iterable with the
None, /) values equal to value.

4 dict.get(key, default = "None") It is used to get the value specified for the passed key.

5 dict.has_key(key) It returns true if the dictionary contains the specified


key.

6 dict.items() It returns all the key-value pairs as a tuple.

7 dict.keys() It returns all the keys of the dictionary.

8 dict.setdefault(key,default= It is used to set the key to the default value if the key
"None") is not specified in the dictionary

9 dict.update(dict2) It updates the dictionary by adding the key-value pair


of dict2 to this dictionary.

10 dict.values() It returns all the values of the dictionary.

11 len()

12 popItem()

13 pop()
14 count()

15 index()

Python Function
Functions are the most important aspect of an application. A function can be defined
as the organized block of reusable code, which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as a
function. The function contains the set of programming statements enclosed by {}. A
function can be called multiple times to provide reusability and modularity to the
Python program.

The Function helps to programmer to break the program into the smaller part. It
organizes the code very effectively and avoids the repetition of the code. As the
program grows, function makes the program more organized.

Python provide us various inbuilt functions like range() or print(). Although, the user
can create its functions, which can be called user-defined functions.Prime Ministers of

There are mainly two types of functions.

o User-define functions - The user-defined functions are those define by


the user to perform the specific task.
o Built-in functions - The built-in functions are those functions that are pre-
defined in Python.

In this tutorial, we will discuss the user define functions.

Advantage of Functions in Python


There are the following advantages of Python functions.

o Using functions, we can avoid rewriting the same logic/code again and again in
a program.
o We can call Python functions multiple times in a program and anywhere in a
program.
o We can track a large Python program easily when it is divided into multiple
functions.
o Reusability is the main achievement of Python functions.
o However, Function calling is always overhead in a Python program.

Creating a Function
Python provides the def keyword to define the function. The syntax of the define
function is given below.

Syntax:

1. def my_function(parameters):
2. function_block
3. return expression

Let's understand the syntax of functions definition.

o The def keyword, along with the function name is used to define the function.
o The identifier rule must follow the function name.
o A function accepts the parameter (argument), and they can be optional.
o The function block is started with the colon (:), and block statements must be
at the same indentation.
o The return statement is used to return the value. A function can have only
one return

Function Calling
In Python, after the function is created, we can call it from another function. A function
must be defined before the function call; otherwise, the Python interpreter gives an
error. To call the function, use the function name followed by the parentheses.

Consider the following example of a simple example that prints the message "Hello
World".

1. #function definition
2. def hello_world():
3. print("hello world")
4. # function calling
5. hello_world()

Output:
hello world

The return statement


The return statement is used at the end of the function and returns the result of the
function. It terminates the function execution and transfers the result where the
function is called. The return statement cannot be used outside of the function.

Syntax

1. return [expression_list]

It can contain the expression which gets evaluated and value is returned to the caller
function. If the return statement has no expression or does not exist itself in the
function then it returns the None object.

Consider the following example:

Example 1

1. # Defining function
2. def sum():
3. a = 10
4. b = 20
5. c = a+b
6. return c
7. # calling sum() function in print statement
8. print("The sum is:",sum())

Output:

The sum is: 30

In the above code, we have defined the function named sum, and it has a statement c
= a+b, which computes the given values, and the result is returned by the return
statement to the caller function.

Example 2 Creating function without return statement

1. # Defining function
2. def sum():
3. a = 10
4. b = 20
5. c = a+b
6. # calling sum() function in print statement
7. print(sum())

Output:

None

In the above code, we have defined the same function without the return statement
as we can see that the sum() function returned the None object to the caller function.

Arguments in function
The arguments are types of information which can be passed into the function. The
arguments are specified in the parentheses. We can pass any number of arguments,
but they must be separate them with a comma.

Consider the following example, which contains a function that accepts a string as the
argument.

Example 1

1. #defining the function


2. def func (name):
3. print("Hi ",name)
4. #calling the function
5. func("Devansh")

Output:

Hi Devansh

Example 2

1. #Python function to calculate the sum of two variables


2. #defining the function
3. def sum (a,b):
4. return a+b;
5.
6. #taking values from the user
7. a = int(input("Enter a: "))
8. b = int(input("Enter b: "))
9.
10. #printing the sum of a and b
11. print("Sum = ",sum(a,b))

Output:

Enter a: 10
Enter b: 20
Sum = 30

Call by reference in Python


In Python, call by reference means passing the actual value as an argument in the
function. All the functions are called by reference, i.e., all the changes made to the
reference inside the function revert back to the original value referred by the reference.

Example 1 Passing Immutable Object (List)

1. #defining the function


2. def change_list(list1):
3. list1.append(20)
4. list1.append(30)
5. print("list inside function = ",list1)
6.
7. #defining the list
8. list1 = [10,30,40,50]
9.
10. #calling the function
11. change_list(list1)
12. print("list outside function = ",list1)

Output:

list inside function = [10, 30, 40, 50, 20, 30]


list outside function = [10, 30, 40, 50, 20, 30]

Example 2 Passing Mutable Object (String)

1. #defining the function


2. def change_string (str):
3. str = str + " Hows you "
4. print("printing the string inside function :",str)
5.
6. string1 = "Hi I am there"
7.
8. #calling the function
9. change_string(string1)
10.
11. print("printing the string outside function :",string1)

Output:

printing the string inside function : Hi I am there Hows you


printing the string outside function : Hi I am there

Types of arguments
There may be several types of arguments which can be passed at the time of function
call.

1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments

Required Arguments
Till now, we have learned about function calling in Python. However, we can provide
the arguments at the time of the function call. As far as the required arguments are
concerned, these are the arguments which are required to be passed at the time of
function calling with the exact match of their positions in the function call and function
definition. If either of the arguments is not provided in the function call, or the position
of the arguments is changed, the Python interpreter will show the error.

Consider the following example.

Example 1

1. def func(name):
2. message = "Hi "+name
3. return message
4. name = input("Enter the name:")
5. print(func(name))

Output:

Enter the name: John


Hi John

Example 2

1. #the function simple_interest accepts three arguments and returns the simple
interest accordingly
2. def simple_interest(p,t,r):
3. return (p*t*r)/100
4. p = float(input("Enter the principle amount? "))
5. r = float(input("Enter the rate of interest? "))
6. t = float(input("Enter the time in years? "))
7. print("Simple Interest: ",simple_interest(p,r,t))

Output:

Enter the principle amount: 5000


Enter the rate of interest: 5
Enter the time in years: 3
Simple Interest: 750.0

Example 3

1. #the function calculate returns the sum of two arguments a and b


2. def calculate(a,b):
3. return a+b
4. calculate(10) # this causes an error as we are missing a required arguments b.

Output:

TypeError: calculate() missing 1 required positional argument: 'b'

Default Arguments
Python allows us to initialize the arguments at the function definition. If the value of
any of the arguments is not provided at the time of function call, then that argument
can be initialized with the value given in the definition even if the argument is not
specified at the function call.
Example 1

1. def printme(name,age=22):
2. print("My name is",name,"and age is",age)
3. printme(name = "john")

Output:

My name is John and age is 22

Example 2

1. def printme(name,age=22):
2. print("My name is",name,"and age is",age)
3. printme(name = "john") #the variable age is not passed into the function how
ever the default value of age is considered in the function
4. printme(age = 10,name="David") #the value of age is overwritten here, 10 will
be printed as age

Output:

My name is john and age is 22


My name is David and age is 10

Variable-length Arguments (*args)


In large projects, sometimes we may not know the number of arguments to be passed
in advance. In such cases, Python provides us the flexibility to offer the comma-
separated values which are internally treated as tuples at the function call. By using the
variable-length arguments, we can pass any number of arguments.

However, at the function definition, we define the variable-length argument using


the *args (star) as *<variable - name >.

Consider the following example.

Example

1. def printme(*names):
2. print("type of passed argument is ",type(names))
3. print("printing the passed arguments...")
4. for name in names:
5. print(name)
6. printme("john","David","smith","nick")

Output:

type of passed argument is <class 'tuple'>


printing the passed arguments...
john
David
smith
nick

In the above code, we passed *names as variable-length argument. We called the


function and passed values which are treated as tuple internally. The tuple is an iterable
sequence the same as the list. To print the given values, we iterated *arg names using
for loop.

Keyword arguments(**kwargs)
Python allows us to call the function with the keyword arguments. This kind of function
call will enable us to pass the arguments in the random order.

The name of the arguments is treated as the keywords and matched in the function
calling and definition. If the same match is found, the values of the arguments are
copied in the function definition.

Consider the following example.

Example 1

1. #function func is called with the name and message as the keyword argument
s
2. def func(name,message):
3. print("printing the message with",name,"and ",message)
4.
5. #name and message is copied with the values John and hello respectively
6. func(name = "John",message="hello")

Output:

printing the message with John and hello

Example 2 providing the values in different order at the calling


1. #The function simple_interest(p, t, r) is called with the keyword arguments the
order of arguments doesn't matter in this case
2. def simple_interest(p,t,r):
3. return (p*t*r)/100
4. print("Simple Interest: ",simple_interest(t=10,r=10,p=1900))

Output:

Simple Interest: 1900.0

If we provide the different name of arguments at the time of function call, an error will
be thrown.

Consider the following example.

Example 3

1. #The function simple_interest(p, t, r) is called with the keyword arguments.


2. def simple_interest(p,t,r):
3. return (p*t*r)/100
4.
5. # doesn't find the exact match of the name of the arguments (keywords)
6. print("Simple Interest: ",simple_interest(time=10,rate=10,principle=1900))

Output:

TypeError: simple_interest() got an unexpected keyword argument 'time'

The Python allows us to provide the mix of the required arguments and keyword
arguments at the time of function call. However, the required argument must not be
given after the keyword argument, i.e., once the keyword argument is encountered in
the function call, the following arguments must also be the keyword arguments.

Consider the following example.

Example 4

1. def func(name1,message,name2):
2. print("printing the message with",name1,",",message,",and",name2)
3. #the first argument is not the keyword argument
4. func("John",message="hello",name2="David")

Output:
printing the message with John , hello ,and David

The following example will cause an error due to an in-proper mix of keyword and
required arguments being passed in the function call.

Example 5

1. def func(name1,message,name2):
2. print("printing the message with",name1,",",message,",and",name2)
3. func("John",message="hello","David")

Output:

SyntaxError: positional argument follows keyword argument

Python provides the facility to pass the multiple keyword arguments which can be
represented as **kwargs. It is similar as the *args but it stores the argument in the
dictionary format.

This type of arguments is useful when we do not know the number of arguments in
advance.

Consider the following example:

Example 6: Many arguments using Keyword argument

1. def food(**kwargs):
2. print(kwargs)
3. food(a="Apple")
4. food(fruits="Orange", Vagitables="Carrot")

Output:

{'a': 'Apple'}
{'fruits': 'Orange', 'Vagitables': 'Carrot'}

Scope of variables
The scopes of the variables depend upon the location where the variable is being
declared. The variable declared in one part of the program may not be accessible to
the other parts.

In python, the variables are defined with the two types of scopes.

1. Global variables
2. Local variables

The variable defined outside any function is known to have a global scope, whereas
the variable defined inside a function is known to have a local scope.

Consider the following example.

Example 1 Local Variable

1. def print_message():
2. message = "hello !! I am going to print a message." # the variable message i
s local to the function itself
3. print(message)
4. print_message()
5. print(message) # this will cause an error since a local variable cannot be acces
sible here.

Output:

hello !! I am going to print a message.


File "/root/PycharmProjects/PythonTest/Test1.py", line 5, in
print(message)
NameError: name 'message' is not defined

Example 2 Global Variable

1. def calculate(*args):
2. sum=0
3. for arg in args:
4. sum = sum +arg
5. print("The sum is",sum)
6. sum=0
7. calculate(10,20,30) #60 will be printed as the sum
8. print("Value of sum outside the function:",sum) # 0 will be printed Output:

Output:

The sum is 60
Value of sum outside the function: 0

Python Built-in Functions


The Python built-in functions are defined as the functions whose functionality is pre-
defined in Python. The python interpreter has several functions that are always present
for use. These functions are known as Built-in Functions. There are several built-in
functions in Python which are listed below:

Python abs() Function


The python abs() function is used to return the absolute value of a number. It takes
only one argument, a number whose absolute value is to be returned. The argument
can be an integer and floating-point number. If the argument is a complex number,
then, abs() returns its magnitude.

Python abs() Function Example

1. # integer number
2. integer = -20
3. print('Absolute value of -40 is:', abs(integer))
4.
5. # floating number
6. floating = -20.83
7. print('Absolute value of -40.83 is:', abs(floating))

Output:

9.2M
195
OOPs Concepts in Java
Absolute value of -20 is: 20
Absolute value of -20.83 is: 20.83

Python all() Function


The python all() function accepts an iterable object (such as list, dictionary, etc.). It
returns true if all items in passed iterable are true. Otherwise, it returns False. If the
iterable object is empty, the all() function returns True.

Python all() Function Example

1. # all values true


2. k = [1, 3, 4, 6]
3. print(all(k))
4.
5. # all values false
6. k = [0, False]
7. print(all(k))
8.
9. # one false value
10. k = [1, 3, 7, 0]
11. print(all(k))
12.
13. # one true value
14. k = [0, False, 5]
15. print(all(k))
16.
17. # empty iterable
18. k = []
19. print(all(k))

Output:

True
False
False
False
True

Python bin() Function


The python bin() function is used to return the binary representation of a specified
integer. A result always starts with the prefix 0b.

Python bin() Function Example

1. x = 10
2. y = bin(x)
3. print (y)

Output:

0b1010

Python bool()
The python bool() converts a value to boolean(True or False) using the standard truth
testing procedure.

Python bool() Example

1. test1 = []
2. print(test1,'is',bool(test1))
3. test1 = [0]
4. print(test1,'is',bool(test1))
5. test1 = 0.0
6. print(test1,'is',bool(test1))
7. test1 = None
8. print(test1,'is',bool(test1))
9. test1 = True
10. print(test1,'is',bool(test1))
11. test1 = 'Easy string'
12. print(test1,'is',bool(test1))

Output:

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

Python bytes()
The python bytes() in Python is used for returning a bytes object. It is an immutable
version of the bytearray() function.

It can create empty bytes object of the specified size.

Python bytes() Example

1. string = "Hello World."


2. array = bytes(string, 'utf-8')
3. print(array)

Output:

b ' Hello World.'


Python callable() Function
A python callable() function in Python is something that can be called. This built-in
function checks and returns true if the object passed appears to be callable, otherwise
false.

Python callable() Function Example

1. x = 8
2. print(callable(x))

Output:

False

Python compile() Function


The python compile() function takes source code as input and returns a code object
which can later be executed by exec() function.

Python compile() Function Example

1. # compile string source to code


2. code_str = 'x=5\ny=10\nprint("sum =",x+y)'
3. code = compile(code_str, 'sum.py', 'exec')
4. print(type(code))
5. exec(code)
6. exec(x)

Output:

<class 'code'>
sum = 15

Python exec() Function


The python exec() function is used for the dynamic execution of Python program
which can either be a string or object code and it accepts large blocks of code, unlike
the eval() function which only accepts a single expression.
Python exec() Function Example

1. x = 8
2. exec('print(x==8)')
3. exec('print(x+4)')

Output:

True
12

Python sum() Function


As the name says, python sum() function is used to get the sum of numbers of an
iterable, i.e., list.

Python sum() Function Example

1. s = sum([1, 2,4 ])
2. print(s)
3.
4. s = sum([1, 2, 4], 10)
5. print(s)

Output:

7
17

Python any() Function


The python any() function returns true if any item in an iterable is true. Otherwise, it
returns False.

Python any() Function Example

1. l = [4, 3, 2, 0]
2. print(any(l))
3.
4. l = [0, False]
5. print(any(l))
6.
7. l = [0, False, 5]
8. print(any(l))
9.
10. l = []
11. print(any(l))

Output:

True
False
True
False

Python ascii() Function


The python ascii() function returns a string containing a printable representation of an
object and escapes the non-ASCII characters in the string using \x, \u or \U escapes.

Python ascii() Function Example

1. normalText = 'Python is interesting'


2. print(ascii(normalText))
3.
4. otherText = 'Pythön is interesting'
5. print(ascii(otherText))
6.
7. print('Pyth\xf6n is interesting')

Output:

'Python is interesting'
'Pyth\xf6n is interesting'
Pythön is interesting

Python bytearray()
The python bytearray() returns a bytearray object and can convert objects into
bytearray objects, or create an empty bytearray object of the specified size.

Python bytearray() Example


1. string = "Python is a programming language."
2.
3. # string with encoding 'utf-8'
4. arr = bytearray(string, 'utf-8')
5. print(arr)

Output:

bytearray(b'Python is a programming language.')

Python eval() Function


The python eval() function parses the expression passed to it and runs python
expression(code) within the program.

Python eval() Function Example

1. x = 8
2. print(eval('x + 1'))

Output:

Python float()
The python float() function returns a floating-point number from a number or string.

Python float() Example

1. # for integers
2. print(float(9))
3.
4. # for floats
5. print(float(8.19))
6.
7. # for string floats
8. print(float("-24.27"))
9.
10. # for string floats with whitespaces
11. print(float(" -17.19\n"))
12.
13. # string float error
14. print(float("xyz"))

Output:

9.0
8.19
-24.27
-17.19
ValueError: could not convert string to float: 'xyz'

Python format() Function


The python format() function returns a formatted representation of the given value.

Python format() Function Example

1. # d, f and b are a type


2.
3. # integer
4. print(format(123, "d"))
5.
6. # float arguments
7. print(format(123.4567898, "f"))
8.
9. # binary format
10. print(format(12, "b"))

Output:

123
123.456790
1100

Python frozenset()
The python frozenset() function returns an immutable frozenset object initialized with
elements from the given iterable.

Python frozenset() Example


1. # tuple of letters
2. letters = ('m', 'r', 'o', 't', 's')
3.
4. fSet = frozenset(letters)
5. print('Frozen set is:', fSet)
6. print('Empty frozen set is:', frozenset())

Output:

Frozen set is: frozenset({'o', 'm', 's', 'r', 't'})


Empty frozen set is: frozenset()

Python getattr() Function


The python getattr() function returns the value of a named attribute of an object. If it
is not found, it returns the default value.

Python getattr() Function Example

1. class Details:
2. age = 22
3. name = "Phill"
4.
5. details = Details()
6. print('The age is:', getattr(details, "age"))
7. print('The age is:', details.age)

Output:

The age is: 22


The age is: 22

Python globals() Function


The python globals() function returns the dictionary of the current global symbol
table.

A Symbol table is defined as a data structure which contains all the necessary
information about the program. It includes variable names, methods, classes, etc.

Python globals() Function Example


1. age = 22
2.
3. globals()['age'] = 22
4. print('The age is:', age)

Output:

The age is: 22

Python hasattr() Function


The python any() function returns true if any item in an iterable is true, otherwise it
returns False.

Python hasattr() Function Example

1. l = [4, 3, 2, 0]
2. print(any(l))
3.
4. l = [0, False]
5. print(any(l))
6.
7. l = [0, False, 5]
8. print(any(l))
9.
10. l = []
11. print(any(l))

Output:

True
False
True
False

Python iter() Function


The python iter() function is used to return an iterator object. It creates an object
which can be iterated one element at a time.

Python iter() Function Example


1. # list of numbers
2. list = [1,2,3,4,5]
3.
4. listIter = iter(list)
5.
6. # prints '1'
7. print(next(listIter))
8.
9. # prints '2'
10. print(next(listIter))
11.
12. # prints '3'
13. print(next(listIter))
14.
15. # prints '4'
16. print(next(listIter))
17.
18. # prints '5'
19. print(next(listIter))

Output:

1
2
3
4
5

Python len() Function


The python len() function is used to return the length (the number of items) of an
object.

Python len() Function Example

1. strA = 'Python'
2. print(len(strA))

Output:

6
Python list()
The python list() creates a list in python.

Python list() Example

1. # empty list
2. print(list())
3.
4. # string
5. String = 'abcde'
6. print(list(String))
7.
8. # tuple
9. Tuple = (1,2,3,4,5)
10. print(list(Tuple))
11. # list
12. List = [1,2,3,4,5]
13. print(list(List))

Output:

[]
['a', 'b', 'c', 'd', 'e']
[1,2,3,4,5]
[1,2,3,4,5]

Python locals() Function


The python locals() method updates and returns the dictionary of the current local
symbol table.

A Symbol table is defined as a data structure which contains all the necessary
information about the program. It includes variable names, methods, classes, etc.

Python locals() Function Example

1. def localsAbsent():
2. return locals()
3.
4. def localsPresent():
5. present = True
6. return locals()
7.
8. print('localsNotPresent:', localsAbsent())
9. print('localsPresent:', localsPresent())

Output:

localsAbsent: {}
localsPresent: {'present': True}

Python map() Function


The python map() function is used to return a list of results after applying a given
function to each item of an iterable(list, tuple etc.).

Python map() Function Example

1. def calculateAddition(n):
2. return n+n
3.
4. numbers = (1, 2, 3, 4)
5. result = map(calculateAddition, numbers)
6. print(result)
7.
8. # converting map object to set
9. numbersAddition = set(result)
10. print(numbersAddition)

Output:

<map object at 0x7fb04a6bec18>


{8, 2, 4, 6}

Python memoryview() Function


The python memoryview() function returns a memoryview object of the given
argument.

Python memoryview () Function Example


1. #A random bytearray
2. randomByteArray = bytearray('ABC', 'utf-8')
3.
4. mv = memoryview(randomByteArray)
5.
6. # access the memory view's zeroth index
7. print(mv[0])
8.
9. # It create byte from memory view
10. print(bytes(mv[0:2]))
11.
12. # It create list from memory view
13. print(list(mv[0:3]))

Output:

65
b'AB'
[65, 66, 67]

Python object()
The python object() returns an empty object. It is a base for all the classes and holds
the built-in properties and methods which are default for all the classes.

Python object() Example

1. python = object()
2.
3. print(type(python))
4. print(dir(python))

Output:

<class 'object'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
'__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__',
'__str__', '__subclasshook__']
Python open() Function
The python open() function opens the file and returns a corresponding file object.

Python open() Function Example

1. # opens python.text file of the current directory


2. f = open("python.txt")
3. # specifying full path
4. f = open("C:/Python33/README.txt")

Output:

Since the mode is omitted, the file is opened in 'r' mode; opens for reading.

Python chr() Function


Python chr() function is used to get a string representing a character which points to
a Unicode code integer. For example, chr(97) returns the string 'a'. This function takes
an integer argument and throws an error if it exceeds the specified range. The standard
range of the argument is from 0 to 1,114,111.

Python chr() Function Example

1. # Calling function
2. result = chr(102) # It returns string representation of a char
3. result2 = chr(112)
4. # Displaying result
5. print(result)
6. print(result2)
7. # Verify, is it string type?
8. print("is it string type:", type(result) is str)

Output:

ValueError: chr() arg not in range(0x110000)

Python complex()
Python complex() function is used to convert numbers or string into a complex
number. This method takes two optional parameters and returns a complex number.
The first parameter is called a real and second as imaginary parts.

Python complex() Example

1. # Python complex() function example


2. # Calling function
3. a = complex(1) # Passing single parameter
4. b = complex(1,2) # Passing both parameters
5. # Displaying result
6. print(a)
7. print(b)

Output:

(1.5+0j)
(1.5+2.2j)

Python delattr() Function


Python delattr() function is used to delete an attribute from a class. It takes two
parameters, first is an object of the class and second is an attribute which we want to
delete. After deleting the attribute, it no longer available in the class and throws an
error if try to call it using the class object.

Python delattr() Function Example

1. class Student:
2. id = 101
3. name = "Pranshu"
4. email = "pranshu@abc.com"
5. # Declaring function
6. def getinfo(self):
7. print(self.id, self.name, self.email)
8. s = Student()
9. s.getinfo()
10. delattr(Student,'course') # Removing attribute which is not available
11. s.getinfo() # error: throws an error

Output:
101 Pranshu pranshu@abc.com
AttributeError: course

Python dir() Function


Python dir() function returns the list of names in the current local scope. If the object
on which method is called has a method named __dir__(), this method will be called
and must return the list of attributes. It takes a single object type argument.

Python dir() Function Example

1. # Calling function
2. att = dir()
3. # Displaying result
4. print(att)

Output:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__',


'__loader__',
'__name__', '__package__', '__spec__']

Python divmod() Function


Python divmod() function is used to get remainder and quotient of two numbers. This
function takes two numeric arguments and returns a tuple. Both arguments are
required and numeric

Python divmod() Function Example

1. # Python divmod() function example


2. # Calling function
3. result = divmod(10,2)
4. # Displaying result
5. print(result)

Output:

(5, 0)

Python enumerate() Function


Python enumerate() function returns an enumerated object. It takes two parameters,
first is a sequence of elements and the second is the start index of the sequence. We
can get the elements in sequence either through a loop or next() method.

Python enumerate() Function Example

1. # Calling function
2. result = enumerate([1,2,3])
3. # Displaying result
4. print(result)
5. print(list(result))

Output:

<enumerate object at 0x7ff641093d80>


[(0, 1), (1, 2), (2, 3)]

Python dict()
Python dict() function is a constructor which creates a dictionary. Python dictionary
provides three different constructors to create a dictionary:

o If no argument is passed, it creates an empty dictionary.


o If a positional argument is given, a dictionary is created with the same key-value
pairs. Otherwise, pass an iterable object.
o If keyword arguments are given, the keyword arguments and their values are
added to the dictionary created from the positional argument.

Python dict() Example

1. # Calling function
2. result = dict() # returns an empty dictionary
3. result2 = dict(a=1,b=2)
4. # Displaying result
5. print(result)
6. print(result2)

Output:

{}
{'a': 1, 'b': 2}
Python filter() Function
Python filter() function is used to get filtered elements. This function takes two
arguments, first is a function and the second is iterable. The filter function returns a
sequence of those elements of iterable object for which function returns true value.

The first argument can be none, if the function is not available and returns only
elements that are true.

Python filter() Function Example

1. # Python filter() function example


2. def filterdata(x):
3. if x>5:
4. return x
5. # Calling function
6. result = filter(filterdata,(1,2,6))
7. # Displaying result
8. print(list(result))

Output:

[6]

Python hash() Function


Python hash() function is used to get the hash value of an object. Python calculates
the hash value by using the hash algorithm. The hash values are integers and used to
compare dictionary keys during a dictionary lookup. We can hash only the types which
are given below:

Hashable types: * bool * int * long * float * string * Unicode * tuple * code object.

Python hash() Function Example

1. # Calling function
2. result = hash(21) # integer value
3. result2 = hash(22.2) # decimal value
4. # Displaying result
5. print(result)
6. print(result2)

Output:

21
461168601842737174

Python help() Function


Python help() function is used to get help related to the object passed during the call.
It takes an optional parameter and returns help information. If no argument is given,
it shows the Python help console. It internally calls python's help function.

Python help() Function Example

1. # Calling function
2. info = help() # No argument
3. # Displaying result
4. print(info)

Output:

Welcome to Python 3.5's help utility!

Python min() Function


Python min() function is used to get the smallest element from the collection. This
function takes two arguments, first is a collection of elements and second is key, and
returns the smallest element from the collection.

Python min() Function Example

1. # Calling function
2. small = min(2225,325,2025) # returns smallest element
3. small2 = min(1000.25,2025.35,5625.36,10052.50)
4. # Displaying result
5. print(small)
6. print(small2)

Output:
325
1000.25

Python set() Function


In python, a set is a built-in class, and this function is a constructor of this class. It is
used to create a new set using elements passed during the call. It takes an iterable
object as an argument and returns a new set object.

Python set() Function Example

1. # Calling function
2. result = set() # empty set
3. result2 = set('12')
4. result3 = set('javatpoint')
5. # Displaying result
6. print(result)
7. print(result2)
8. print(result3)

Output:

set()
{'1', '2'}
{'a', 'n', 'v', 't', 'j', 'p', 'i', 'o'}

Python hex() Function


Python hex() function is used to generate hex value of an integer argument. It takes
an integer argument and returns an integer converted into a hexadecimal string. In
case, we want to get a hexadecimal value of a float, then use float.hex() function.

Python hex() Function Example

1. # Calling function
2. result = hex(1)
3. # integer value
4. result2 = hex(342)
5. # Displaying result
6. print(result)
7. print(result2)
Output:

0x1
0x156

Python id() Function


Python id() function returns the identity of an object. This is an integer which is
guaranteed to be unique. This function takes an argument as an object and returns a
unique integer number which represents identity. Two objects with non-overlapping
lifetimes may have the same id() value.

Python id() Function Example

1. # Calling function
2. val = id("Javatpoint") # string object
3. val2 = id(1200) # integer object
4. val3 = id([25,336,95,236,92,3225]) # List object
5. # Displaying result
6. print(val)
7. print(val2)
8. print(val3)

Output:

139963782059696
139963805666864
139963781994504

Python setattr() Function


Python setattr() function is used to set a value to the object's attribute. It takes three
arguments, i.e., an object, a string, and an arbitrary value, and returns none. It is helpful
when we want to add a new attribute to an object and set a value to it.

Python setattr() Function Example

1. class Student:
2. id = 0
3. name = ""
4.
5. def __init__(self, id, name):
6. self.id = id
7. self.name = name
8.
9. student = Student(102,"Sohan")
10. print(student.id)
11. print(student.name)
12. #print(student.email) product error
13. setattr(student, 'email','sohan@abc.com') # adding new attribute
14. print(student.email)

Output:

102
Sohan
sohan@abc.com

Python slice() Function


Python slice() function is used to get a slice of elements from the collection of
elements. Python provides two overloaded slice functions. The first function takes a
single argument while the second function takes three arguments and returns a slice
object. This slice object can be used to get a subsection of the collection.

Python slice() Function Example

1. # Calling function
2. result = slice(5) # returns slice object
3. result2 = slice(0,5,3) # returns slice object
4. # Displaying result
5. print(result)
6. print(result2)

Output:

slice(None, 5, None)
slice(0, 5, 3)
Python sorted() Function
Python sorted() function is used to sort elements. By default, it sorts elements in an
ascending order but can be sorted in descending also. It takes four arguments and
returns a collection in sorted order. In the case of a dictionary, it sorts only keys, not
values.

Python sorted() Function Example

1. str = "javatpoint" # declaring string


2. # Calling function
3. sorted1 = sorted(str) # sorting string
4. # Displaying result
5. print(sorted1)

Output:

['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v']

Python next() Function


Python next() function is used to fetch next item from the collection. It takes two
arguments, i.e., an iterator and a default value, and returns an element.

This method calls on iterator and throws an error if no item is present. To avoid the
error, we can set a default value.

Python next() Function Example

1. number = iter([256, 32, 82]) # Creating iterator


2. # Calling function
3. item = next(number)
4. # Displaying result
5. print(item)
6. # second item
7. item = next(number)
8. print(item)
9. # third item
10. item = next(number)
11. print(item)
Output:

256
32
82

Python input() Function


Python input() function is used to get an input from the user. It prompts for the user
input and reads a line. After reading data, it converts it into a string and returns it. It
throws an error EOFError if EOF is read.

Python input() Function Example

1. # Calling function
2. val = input("Enter a value: ")
3. # Displaying result
4. print("You entered:",val)

Output:

Enter a value: 45
You entered: 45

Python int() Function


Python int() function is used to get an integer value. It returns an expression converted
into an integer number. If the argument is a floating-point, the conversion truncates
the number. If the argument is outside the integer range, then it converts the number
into a long type.

If the number is not a number or if a base is given, the number must be a string.

Python int() Function Example

1. # Calling function
2. val = int(10) # integer value
3. val2 = int(10.52) # float value
4. val3 = int('10') # string value
5. # Displaying result
6. print("integer values :",val, val2, val3)
Output:

integer values : 10 10 10

Python isinstance() Function


Python isinstance() function is used to check whether the given object is an instance
of that class. If the object belongs to the class, it returns true. Otherwise returns False.
It also returns true if the class is a subclass.

The isinstance() function takes two arguments, i.e., object and classinfo, and then it
returns either True or False.

Python isinstance() function Example

1. class Student:
2. id = 101
3. name = "John"
4. def __init__(self, id, name):
5. self.id=id
6. self.name=name
7.
8. student = Student(1010,"John")
9. lst = [12,34,5,6,767]
10. # Calling function
11. print(isinstance(student, Student)) # isinstance of Student class
12. print(isinstance(lst, Student))

Output:

True
False

Python oct() Function


Python oct() function is used to get an octal value of an integer number. This method
takes an argument and returns an integer converted into an octal string. It throws an
error TypeError, if argument type is other than an integer.

Python oct() function Example


1. # Calling function
2. val = oct(10)
3. # Displaying result
4. print("Octal value of 10:",val)

Output:

Octal value of 10: 0o12

Python ord() Function


The python ord() function returns an integer representing Unicode code point for the
given Unicode character.

Python ord() function Example

1. # Code point of an integer


2. print(ord('8'))
3.
4. # Code point of an alphabet
5. print(ord('R'))
6.
7. # Code point of a character
8. print(ord('&'))

Output:

56
82
38

Python pow() Function


The python pow() function is used to compute the power of a number. It returns x to
the power of y. If the third argument(z) is given, it returns x to the power of y modulus
z, i.e. (x, y) % z.

Python pow() function Example

1. # positive x, positive y (x**y)


2. print(pow(4, 2))
3.
4. # negative x, positive y
5. print(pow(-4, 2))
6.
7. # positive x, negative y (x**-y)
8. print(pow(4, -2))
9.
10. # negative x, negative y
11. print(pow(-4, -2))

Output:

16
16
0.0625
0.0625

Python print() Function


The python print() function prints the given object to the screen or other standard
output devices.

Python print() function Example

1. print("Python is programming language.")


2.
3. x = 7
4. # Two objects passed
5. print("x =", x)
6.
7. y = x
8. # Three objects passed
9. print('x =', x, '= y')

Output:

Python is programming language.


x = 7
x = 7 = y

Python range() Function


The python range() function returns an immutable sequence of numbers starting from
0 by default, increments by 1 (by default) and ends at a specified number.

Python range() function Example

1. # empty range
2. print(list(range(0)))
3.
4. # using the range(stop)
5. print(list(range(4)))
6.
7. # using the range(start, stop)
8. print(list(range(1,7 )))

Output:

[]
[0, 1, 2, 3]
[1, 2, 3, 4, 5, 6]

Python reversed() Function


The python reversed() function returns the reversed iterator of the given sequence.

Python reversed() function Example

1. # for string
2. String = 'Java'
3. print(list(reversed(String)))
4.
5. # for tuple
6. Tuple = ('J', 'a', 'v', 'a')
7. print(list(reversed(Tuple)))
8.
9. # for range
10. Range = range(8, 12)
11. print(list(reversed(Range)))
12.
13. # for list
14. List = [1, 2, 7, 5]
15. print(list(reversed(List)))

Output:

['a', 'v', 'a', 'J']


['a', 'v', 'a', 'J']
[11, 10, 9, 8]
[5, 7, 2, 1]

Python round() Function


The python round() function rounds off the digits of a number and returns the floating
point number.

Python round() Function Example

1. # for integers
2. print(round(10))
3.
4. # for floating point
5. print(round(10.8))
6.
7. # even choice
8. print(round(6.6))

Output:

10
11
7

Python issubclass() Function


The python issubclass() function returns true if object argument(first argument) is a
subclass of second class(second argument).

Python issubclass() Function Example

1. class Rectangle:
2. def __init__(rectangleType):
3. print('Rectangle is a ', rectangleType)
4.
5. class Square(Rectangle):
6. def __init__(self):
7. Rectangle.__init__('square')
8.
9. print(issubclass(Square, Rectangle))
10. print(issubclass(Square, list))
11. print(issubclass(Square, (list, Rectangle)))
12. print(issubclass(Rectangle, (list, Rectangle)))

Output:

True
False
True
True

Python str
The python str() converts a specified value into a string.

Python str() Function Example

1. str('4')

Output:

'4'

Python tuple() Function


The python tuple() function is used to create a tuple object.

Python tuple() Function Example

1. t1 = tuple()
2. print('t1=', t1)
3.
4. # creating a tuple from a list
5. t2 = tuple([1, 6, 9])
6. print('t2=', t2)
7.
8. # creating a tuple from a string
9. t1 = tuple('Java')
10. print('t1=',t1)
11.
12. # creating a tuple from a dictionary
13. t1 = tuple({4: 'four', 5: 'five'})
14. print('t1=',t1)

Output:

t1= ()
t2= (1, 6, 9)
t1= ('J', 'a', 'v', 'a')
t1= (4, 5)

Python type()
The python type() returns the type of the specified object if a single argument is
passed to the type() built in function. If three arguments are passed, then it returns a
new type object.

Python type() Function Example

1. List = [4, 5]
2. print(type(List))
3.
4. Dict = {4: 'four', 5: 'five'}
5. print(type(Dict))
6.
7. class Python:
8. a=0
9.
10. InstanceOfPython = Python()
11. print(type(InstanceOfPython))

Output:

<class 'list'>
<class 'dict'>
<class '__main__.Python'>

Python vars() function


The python vars() function returns the __dict__ attribute of the given object.

Python vars() Function Example

1. class Python:
2. def __init__(self, x = 7, y = 9):
3. self.x = x
4. self.y = y
5.
6. InstanceOfPython = Python()
7. print(vars(InstanceOfPython))

Output:

{'y': 9, 'x': 7}

Python zip() Function


The python zip() Function returns a zip object, which maps a similar index of multiple
containers. It takes iterables (can be zero or more), makes it an iterator that aggregates
the elements based on iterables passed, and returns an iterator of tuples.

Python zip() Function Example

1. numList = [4,5, 6]
2. strList = ['four', 'five', 'six']
3.
4. # No iterables are passed
5. result = zip()
6.
7. # Converting itertor to list
8. resultList = list(result)
9. print(resultList)
10.
11. # Two iterables are passed
12. result = zip(numList, strList)
13.
14. # Converting itertor to set
15. resultSet = set(result)
16. print(resultSet)

Output:

[]
{(5, 'five'), (4, 'four'), (6, 'six')}

Python Lambda Functions


Python Lambda function is known as the anonymous function that is defined without
a name. Python allows us to not declare the function in the standard manner, i.e., by
using the def keyword. Rather, the anonymous functions are declared by using
the lambda keyword. However, Lambda functions can accept any number of
arguments, but they can return only one value in the form of expression.

The anonymous function contains a small piece of code. It simulates inline functions
of C and C++, but it is not exactly an inline function.

The syntax to define an anonymous function is given below.

Syntax

1. lambda arguments: expression

It can accept any number of arguments and has only one expression. It is useful when
the function objects are required.

OOPs Concepts in Java

Consider the following example of the lambda function.

Example 1

1. # a is an argument and a+10 is an expression which got evaluated and returne


d.
2. x = lambda a:a+10
3. # Here we are printing the function object
4. print(x)
5. print("sum = ",x(20))

Output:

<function <lambda> at 0x0000019E285D16A8>


sum = 30
In the above example, we have defined the lambda a: a+10 anonymous function
where a is an argument and a+10 is an expression. The given expression gets
evaluated and returned the result. The above lambda function is same as the normal
function.

1. def x(a):
2. return a+10
3. print(sum = x(10))

Example 2
Multiple arguments to Lambda function

1. # a and b are the arguments and a*b is the expression which gets evaluated a
nd returned.
2. x = lambda a,b: a*b
3. print("mul = ", x(20,10))
4. <p><strong>Output:</strong></p>
5. <div class="codeblock3"><pre>
6. mul = 200
7. </pre></div>
8. <h2 class="h2">Why use lambda function?</h2>
9. <p>The main role of the lambda function is better described in the scenarios
when we use them anonymously inside another function. In Python, the lamb
da function can be used as an argument to the <strong>higher-
order functions</strong> which accepts other functions as arguments.</p>
10. <p>Consider the following example:</p>
11. <h3 class="h3">Example 1:</h3>
12. <div class="codeblock"><textarea name="code" class="python">
13. #the function table(n) prints the table of n
14. def table(n):
15. return lambda a:a*n # a will contain the iteration variable i and a multiple o
f n is returned at each function call
16. n = int(input("Enter the number:"))
17. b = table(n) #the entered number is passed into the function table. b will cont
ain a lambda function which is called again and again with the iteration variabl
ei
18. for i in range(1,11):
19. print(n,"X",i,"=",b(i)) #the lambda function b is called with the iteration varia
ble i

Output:

Enter the number:10


10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

The lambda function is commonly used with Python built-in functions filter() function
and map() function.

Use lambda function with filter()


The Python built-in filter() function accepts a function and a list as an argument. It
provides an effective way to filter out all elements of the sequence. It returns the new
sequence in which the function evaluates to True.

Consider the following example where we filter out the only odd number from the
given list.

1. #program to filter out the tuple which contains odd numbers


2. lst = (10,22,37,41,100,123,29)
3. oddlist = tuple(filter(lambda x:(x%3 == 0),lst)) # the tuple contains all the item
s of the tuple for which the lambda function evaluates to true
4. print(oddlist)

Output:

(37, 41, 123, 29)

Using lambda function with map()


The map() function in Python accepts a function and a list. It gives a new list which
contains all modified items returned by the function for each item.

Consider the following example of map() function.

1. #program to filter out the list which contains odd numbers


2. lst = (10,20,30,40,50,60)
3. square_list = list(map(lambda x:x**2,lst)) # the tuple contains all the items of t
he list for which the lambda function evaluates to true
4. print(square_tuple)

Output:

(100, 400, 900, 1600, 2500, 3600)

Python File Handling


Till now, we were taking the input from the console and writing it back to the console
to interact with the user.

Sometimes, it is not enough to only display the data on the console. The data to be
displayed may be very large, and only a limited amount of data can be displayed on
the console since the memory is volatile, it is impossible to recover the
programmatically generated data again and again.

The file handling plays an important role when the data needs to be stored
permanently into the file. A file is a named location on disk to store related information.
We can access the stored information (non-volatile) after the program termination.

The file-handling implementation is slightly lengthy or complicated in the other


programming language, but it is easier and shorter in Python.

12.5M
284
Exception Handling in Java - Javatpoint

In Python, files are treated in two modes as text or binary. The file may be in the text
or binary format, and each line of a file is ended with the special character.

Hence, a file operation can be done in the following order.

o Open a file
o Read or write - Performing operation
o Close the file

Opening a file
Python provides an open() function that accepts two arguments, file name and access
mode in which the file is accessed. The function returns a file object which can be used
to perform various operations like reading, writing, etc.

Syntax:

1. file object = open(<file-name>, <access-mode>, <buffering>)

The files can be accessed using various modes like read, write, or append. The
following are the details about the access mode to open a file.

SN Access Description
mode

1 r It opens the file to read-only mode. The file pointer exists at the beginning.
The file is by default open in this mode if no access mode is passed.

2 rb It opens the file to read-only in binary format. The file pointer exists at the
beginning of the file.

3 r+ It opens the file to read and write both. The file pointer exists at the beginning
of the file.

4 rb+ It opens the file to read and write both in binary format. The file pointer exists
at the beginning of the file.

5 w It opens the file to write only. It overwrites the file if previously exists or creates
a new one if no file exists with the same name. The file pointer exists at the
beginning of the file.

6 wb It opens the file to write only in binary format. It overwrites the file if it exists
previously or creates a new one if no file exists. The file pointer exists at the
beginning of the file.

7 w+ It opens the file to write and read both. It is different from r+ in the sense that
it overwrites the previous file if one exists whereas r+ doesn't overwrite the
previously written file. It creates a new file if no file exists. The file pointer exists
at the beginning of the file.

8 wb+ It opens the file to write and read both in binary format. The file pointer exists
at the beginning of the file.
9 a It opens the file in the append mode. The file pointer exists at the end of the
previously written file if exists any. It creates a new file if no file exists with the
same name.

10 ab It opens the file in the append mode in binary format. The pointer exists at the
end of the previously written file. It creates a new file in binary format if no file
exists with the same name.

11 a+ It opens a file to append and read both. The file pointer remains at the end of
the file if a file exists. It creates a new file if no file exists with the same name.

12 ab+ It opens a file to append and read both in binary format. The file pointer
remains at the end of the file.

Let's look at the simple example to open a file named "file.txt" (stored in the same
directory) in read mode and printing its content on the console.

Example

1. #opens the file file.txt in read mode


2. fileptr = open("file.txt","r")
3.
4. if fileptr:
5. print("file is opened successfully")

Output:

<class '_io.TextIOWrapper'>
file is opened successfully

In the above code, we have passed filename as a first argument and opened file in
read mode as we mentioned r as the second argument. The fileptr holds the file
object and if the file is opened successfully, it will execute the print statement

The close() method


Once all the operations are done on the file, we must close it through our Python script
using the close() method. Any unwritten information gets destroyed once
the close() method is called on a file object.

We can perform any operation on the file externally using the file system which is the
currently opened in Python; hence it is good practice to close the file once all the
operations are done.
The syntax to use the close() method is given below.

Syntax

1. fileobject.close()

Consider the following example.

1. # opens the file file.txt in read mode


2. fileptr = open("file.txt","r")
3.
4. if fileptr:
5. print("file is opened successfully")
6.
7. #closes the opened file
8. fileptr.close()

After closing the file, we cannot perform any operation in the file. The file needs to be
properly closed. If any exception occurs while performing some operations in the file
then the program terminates without closing the file.

We should use the following method to overcome such type of problem.

1. try:
2. fileptr = open("file.txt")
3. # perform file operations
4. finally:
5. fileptr.close()

The with statement


The with statement was introduced in python 2.5. The with statement is useful in the
case of manipulating the files. It is used in the scenario where a pair of statements is
to be executed with a block of code in between.

The syntax to open a file using with the statement is given below.

1. with open(<file name>, <access mode>) as <file-pointer>:


2. #statement suite

The advantage of using with statement is that it provides the guarantee to close the
file regardless of how the nested block exits.
It is always suggestible to use the with statement in the case of files because, if the
break, return, or exception occurs in the nested block of code then it automatically
closes the file, we don't need to write the close() function. It doesn't let the file to
corrupt.

Consider the following example.

Example

1. with open("file.txt",'r') as f:
2. content = f.read();
3. print(content)

Writing the file


To write some text to a file, we need to open the file using the open method with one
of the following access modes.

w: It will overwrite the file if any file exists. The file pointer is at the beginning of the
file.

a: It will append the existing file. The file pointer is at the end of the file. It creates a
new file if no file exists.

Consider the following example.

Example

1. # open the file.txt in append mode. Create a new file if no such file exists.
2. fileptr = open("file2.txt", "w")
3.
4. # appending the content to the file
5. fileptr.write('''''Python is the modern day language. It makes things so simple.
6. It is the fastest-growing programing language''')
7.
8. # closing the opened the file
9. fileptr.close()

Output:

File2.txt
Python is the modern-day language. It makes things so simple. It is the
fastest growing programming language.

Snapshot of the file2.txt

We have opened the file in w mode. The file1.txt file doesn't exist, it created a new
file and we have written the content in the file using the write() function.

Example 2

1. #open the file.txt in write mode.


2. fileptr = open("file2.txt","a")
3.
4. #overwriting the content of the file
5. fileptr.write(" Python has an easy syntax and user-friendly interaction.")
6.
7. #closing the opened file
8. fileptr.close()

Output:

Python is the modern day language. It makes things so simple.


It is the fastest growing programing language Python has an easy syntax and
user-friendly interaction.

Snapshot of the file2.txt

We can see that the content of the file is modified. We have opened the file in a mode
and it appended the content in the existing file2.txt.
To read a file using the Python script, the Python provides the read() method.
The read() method reads a string from the file. It can read the data in the text as well
as a binary format.

The syntax of the read() method is given below.

Syntax:

1. fileobj.read(<count>)

Here, the count is the number of bytes to be read from the file starting from the
beginning of the file. If the count is not specified, then it may read the content of the
file until the end.

Consider the following example.

Example

1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r")
3. #stores all the data of the file into the variable content
4. content = fileptr.read(10)
5. # prints the type of the data stored in the file
6. print(type(content))
7. #prints the content of the file
8. print(content)
9. #closes the opened file
10. fileptr.close()

Output:

<class 'str'>
Python is

In the above code, we have read the content of file2.txt by using the read() function.
We have passed count value as ten which means it will read the first ten characters
from the file.

If we use the following line, then it will print all content of the file.

1. content = fileptr.read()
2. print(content)
Output:

Python is the modern-day language. It makes things so simple.


It is the fastest-growing programing language Python has easy an syntax and
user-friendly interaction.

Read file through for loop


We can read the file using for loop. Consider the following example.

1. #open the file.txt in read mode. causes an error if no such file exists.
2. fileptr = open("file2.txt","r");
3. #running a for loop
4. for i in fileptr:
5. print(i) # i contains each line of the file

Output:

Python is the modern day language.

It makes things so simple.

Python has easy syntax and user-friendly interaction.

Read Lines of the file


Python facilitates to read the file line by line by using a function readline() method.
The readline() method reads the lines of the file from the beginning, i.e., if we use the
readline() method two times, then we can get the first two lines of the file.

Consider the following example which contains a function readline() that reads the
first line of our file "file2.txt" containing three lines. Consider the following example.

Example 1: Reading lines using readline() function

1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r");
3. #stores all the data of the file into the variable content
4. content = fileptr.readline()
5. content1 = fileptr.readline()
6. #prints the content of the file
7. print(content)
8. print(content1)
9. #closes the opened file
10. fileptr.close()

Output:

Python is the modern day language.

It makes things so simple.

We called the readline() function two times that's why it read two lines from the file.

Python provides also the readlines() method which is used for the reading lines. It
returns the list of the lines till the end of file(EOF) is reached.

Example 2: Reading Lines Using readlines() function

1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r");
3.
4. #stores all the data of the file into the variable content
5. content = fileptr.readlines()
6.
7. #prints the content of the file
8. print(content)
9.
10. #closes the opened file
11. fileptr.close()

Output:

['Python is the modern day language.\n', 'It makes things so simple.\n',


'Python has easy syntax and user-friendly interaction.']

Creating a new file


The new file can be created by using one of the following access modes with the
function open().

x: it creates a new file with the specified name. It causes an error a file exists with the
same name.

a: It creates a new file with the specified name if no such file exists. It appends the
content to the file if the file already exists with the specified name.
w: It creates a new file with the specified name if no such file exists. It overwrites the
existing file.

Consider the following example.

Example 1

1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","x")
3. print(fileptr)
4. if fileptr:
5. print("File created successfully")

Output:

<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>


File created successfully

File Pointer positions


Python provides the tell() method which is used to print the byte number at which the
file pointer currently exists. Consider the following example.

1. # open the file file2.txt in read mode


2. fileptr = open("file2.txt","r")
3.
4. #initially the filepointer is at 0
5. print("The filepointer is at byte :",fileptr.tell())
6.
7. #reading the content of the file
8. content = fileptr.read();
9.
10. #after the read operation file pointer modifies. tell() returns the location of the
fileptr.
11.
12. print("After reading, the filepointer is at:",fileptr.tell())

Output:

The filepointer is at byte : 0


After reading, the filepointer is at: 117
Modifying file pointer position
In real-world applications, sometimes we need to change the file pointer location
externally since we may need to read or write the content at various locations.

For this purpose, the Python provides us the seek() method which enables us to modify
the file pointer position externally.

The syntax to use the seek() method is given below.

Syntax:

1. <file-ptr>.seek(offset[, from)

The seek() method accepts two parameters:

offset: It refers to the new position of the file pointer within the file.

from: It indicates the reference position from where the bytes are to be moved. If it is
set to 0, the beginning of the file is used as the reference position. If it is set to 1, the
current position of the file pointer is used as the reference position. If it is set to 2, the
end of the file pointer is used as the reference position.

Consider the following example.

Example

1. # open the file file2.txt in read mode


2. fileptr = open("file2.txt","r")
3.
4. #initially the filepointer is at 0
5. print("The filepointer is at byte :",fileptr.tell())
6.
7. #changing the file pointer location to 10.
8. fileptr.seek(10);
9.
10. #tell() returns the location of the fileptr.
11. print("After reading, the filepointer is at:",fileptr.tell())

Output:

The filepointer is at byte : 0


After reading, the filepointer is at: 10
Python OS module
Renaming the file
The Python os module enables interaction with the operating system. The os module
provides the functions that are involved in file processing operations like renaming,
deleting, etc. It provides us the rename() method to rename the specified file to a new
name. The syntax to use the rename() method is given below.

Syntax:

1. rename(current-name, new-name)

The first argument is the current file name and the second argument is the modified
name. We can change the file name bypassing these two arguments.

Example 1:

1. import os
2.
3. #rename file2.txt to file3.txt
4. os.rename("file2.txt","file3.txt")

Output:

The above code renamed current file2.txt to file3.txt

Removing the file


The os module provides the remove() method which is used to remove the specified
file. The syntax to use the remove() method is given below.

1. remove(file-name)

Example 1

1. import os;
2. #deleting the file named file3.txt
3. os.remove("file3.txt")

Creating the new directory


The mkdir() method is used to create the directories in the current working directory.
The syntax to create the new directory is given below.

Syntax:

1. mkdir(directory name)

Example 1

1. import os
2.
3. #creating a new directory with the name new
4. os.mkdir("new")

The getcwd() method


This method returns the current working directory.

The syntax to use the getcwd() method is given below.

Syntax

1. os.getcwd()

Example

1. import os
2. os.getcwd()

Output:

'C:\\Users\\DEVANSH SHARMA'

Changing the current working directory


The chdir() method is used to change the current working directory to a specified
directory.

The syntax to use the chdir() method is given below.

Syntax

1. chdir("new-directory")
Example

1. import os
2. # Changing current directory with the new directiory
3. os.chdir("C:\\Users\\DEVANSH SHARMA\\Documents")
4. #It will display the current working directory
5. os.getcwd()

Output:

'C:\\Users\\DEVANSH SHARMA\\Documents'

Deleting directory
The rmdir() method is used to delete the specified directory.

The syntax to use the rmdir() method is given below.

Syntax

1. os.rmdir(directory name)

Example 1

1. import os
2. #removing the new directory
3. os.rmdir("directory_name")

It will remove the specified directory.

Writing Python output to the files


In Python, there are the requirements to write the output of a Python script to a file.

The check_call() method of module subprocess is used to execute a Python script


and write the output of that script to a file.

The following example contains two python scripts. The script file1.py executes the
script file.py and writes its output to the text file output.txt.

Example

file.py
1. temperatures=[10,-20,-289,100]
2. def c_to_f(c):
3. if c< -273.15:
4. return "That temperature doesn't make sense!"
5. else:
6. f=c*9/5+32
7. return f
8. for t in temperatures:
9. print(c_to_f(t))

file.py

1. import subprocess
2.
3. with open("output.txt", "wb") as f:
4. subprocess.check_call(["python", "file.py"], stdout=f)

The file related methods


The file object provides the following methods to manipulate the files on various
operating systems.

SN Method Description

1 file.close() It closes the opened file. The file once closed, it can't be read or
write anymore.

2 File.fush() It flushes the internal buffer.

3 File.fileno() It returns the file descriptor used by the underlying


implementation to request I/O from the OS.

4 File.isatty() It returns true if the file is connected to a TTY device, otherwise


returns false.

5 File.next() It returns the next line from the file.

6 File.read([size]) It reads the file for the specified size.

7 File.readline([size]) It reads one line from the file and places the file pointer to the
beginning of the new line.
8 File.readlines([sizehint]) It returns a list containing all the lines of the file. It reads the file
until the EOF occurs using readline() function.

9 File.seek(offset[,from) It modifies the position of the file pointer to a specified offset


with the specified reference.

10 File.tell() It returns the current position of the file pointer within the file.

11 File.truncate([size]) It truncates the file to the optional specified size.

12 File.write(str) It writes the specified string to a file

13 File.writelines(seq) It writes a sequence of the strings to a file.

Python Modules
A python module can be defined as a python program file which contains a python
code including python functions, class, or variables. In other words, we can say that
our python code file saved with the extension (.py) is treated as the module. We may
have a runnable code inside the python module.

Modules in Python provides us the flexibility to organize the code in a logical way.

To use the functionality of one module into another, we must have to import the
specific module.

Example
In this example, we will create a module named as file.py which contains a function
func that contains a code to print some message on the console.

Darwin Carpet Pythons on the Balcony

Let's create the module named as file.py.

1. #displayMsg prints a message to the name being passed.


2. def displayMsg(name)
3. print("Hi "+name);

Here, we need to include this module into our main module to call the method
displayMsg() defined in the module named file.

Loading the module in our python code


We need to load the module in our python code to use its functionality. Python
provides two types of statements as defined below.

1. The import statement


2. The from-import statement

The import statement


The import statement is used to import all the functionality of one module into
another. Here, we must notice that we can use the functionality of any python source
file by importing that file as the module into another python source file.

We can import multiple modules with a single import statement, but a module is
loaded once regardless of the number of times, it has been imported into our file.

The syntax to use the import statement is given below.

1. import module1,module2,........ module n

Hence, if we need to call the function displayMsg() defined in the file file.py, we have
to import that file as a module into our module as shown in the example below.

Example:

1. import file;
2. name = input("Enter the name?")
3. file.displayMsg(name)

Output:

Enter the name?John


Hi John

The from-import statement


Instead of importing the whole module into the namespace, python provides the
flexibility to import only the specific attributes of a module. This can be done by using
from? import statement. The syntax to use the from-import statement is given below.

1. from < module-name> import <name 1>, <name 2>..,<name n>

Consider the following module named as calculation which contains three functions
as summation, multiplication, and divide.
calculation.py:

1. #place the code in the calculation.py


2. def summation(a,b):
3. return a+b
4. def multiplication(a,b):
5. return a*b;
6. def divide(a,b):
7. return a/b;

Main.py:

1. from calculation import summation


2. #it will import only the summation() from calculation.py
3. a = int(input("Enter the first number"))
4. b = int(input("Enter the second number"))
5. print("Sum = ",summation(a,b)) #we do not need to specify the module name
while accessing summation()

Output:

Enter the first number10


Enter the second number20
Sum = 30

The from...import statement is always better to use if we know the attributes to be


imported from the module in advance. It doesn't let our code to be heavier. We can
also import all the attributes from a module by using *.

Consider the following syntax.

1. from <module> import *

Renaming a module
Python provides us the flexibility to import some module with a specific name so that
we can use this name to use that module in our python source file.

The syntax to rename a module is given below.

1. import <module-name> as <specific-name>

Example
1. #the module calculation of previous example is imported in this example as ca
l.
2. import calculation as cal;
3. a = int(input("Enter a?"));
4. b = int(input("Enter b?"));
5. print("Sum = ",cal.summation(a,b))

Output:

Enter a?10
Enter b?20
Sum = 30

Using dir() function


The dir() function returns a sorted list of names defined in the passed module. This list
contains all the sub-modules, variables and functions defined in this module.

Consider the following example.

Example

1. import json
2.
3. List = dir(json)
4.
5. print(List)

Output:

['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__',


'__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__',
'__version__',
'_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps',
'encoder', 'load', 'loads', 'scanner']

The reload() function


As we have already stated that, a module is loaded once regardless of the number of
times it is imported into the python source file. However, if you want to reload the
already imported module to re-execute the top-level code, python provides us the
reload() function. The syntax to use the reload() function is given below.
1. reload(<module-name>)

for example, to reload the module calculation defined in the previous example, we
must use the following line of code.

1. reload(calculation)

Scope of variables
In Python, variables are associated with two types of scopes. All the variables defined
in a module contain the global scope unless or until it is defined within a function.

All the variables defined inside a function contain a local scope that is limited to this
function itself. We can not access a local variable globally.

If two variables are defined with the same name with the two different scopes, i.e., local
and global, then the priority will always be given to the local variable.

Consider the following example.

Example

1. name = "john"
2. def print_name(name):
3. print("Hi",name) #prints the name that is local to this function only.
4. name = input("Enter the name?")
5. print_name(name)

Output:

Hi David

Python packages
The packages in python facilitate the developer with the application development
environment by providing a hierarchical directory structure where a package contains
sub-packages, modules, and sub-modules. The packages are used to categorize the
application level code efficiently.

Let's create a package named Employees in your home directory. Consider the
following steps.
1. Create a directory with name Employees on path /home.

2. Create a python source file with name ITEmployees.py on the path


/home/Employees.

ITEmployees.py

1. def getITNames():
2. List = ["John", "David", "Nick", "Martin"]
3. return List;

3. Similarly, create one more python file with name BPOEmployees.py and create a
function getBPONames().

4. Now, the directory Employees which we have created in the first step contains two
python modules. To make this directory a package, we need to include one more file
here, that is __init__.py which contains the import statements of the modules defined
in this directory.

__init__.py

1. from ITEmployees import getITNames


2. from BPOEmployees import getBPONames

5. Now, the directory Employees has become the package containing two python
modules. Here we must notice that we must have to create __init__.py inside a directory
to convert this directory to a package.

6. To use the modules defined inside the package Employees, we must have to import
this in our python source file. Let's create a simple python source file at our home
directory (/home) which uses the modules defined in this package.

Test.py

1. import Employees
2. print(Employees.getNames())

Output:

['John', 'David', 'Nick', 'Martin']

We can have sub-packages inside the packages. We can nest the packages up to any
level depending upon the application requirements.
The following image shows the directory structure of an application Library
management system which contains three sub-packages as Admin, Librarian, and
Student. The sub-packages contain the python modules.

Python Exception
An exception can be defined as an unusual condition in a program resulting in the
interruption in the flow of the program.

Whenever an exception occurs, the program stops the execution, and thus the further
code is not executed. Therefore, an exception is the run-time errors that are unable to
handle to Python script. An exception is a Python object that represents an error

Python provides a way to handle the exception so that the code can be executed
without any interruption. If we do not handle the exception, the interpreter doesn't
execute all the code that exists after the exception.

Python has many built-in exceptions that enable our program to run without
interruption and give the output. These exceptions are given below:

7.2M
140
History of Java

Common Exceptions
Python provides the number of built-in exceptions, but here we are describing the
common standard exceptions. A list of common exceptions that can be thrown from a
standard Python program is given below.
1. ZeroDivisionError: Occurs when a number is divided by zero.
2. NameError: It occurs when a name is not found. It may be local or global.
3. IndentationError: If incorrect indentation is given.
4. IOError: It occurs when Input Output operation fails.
5. EOFError: It occurs when the end of the file is reached, and yet operations are
being performed.

The problem without handling exceptions


As we have already discussed, the exception is an abnormal condition that halts the
execution of the program.

Suppose we have two variables a and b, which take the input from the user and
perform the division of these values. What if the user entered the zero as the
denominator? It will interrupt the program execution and through
a ZeroDivision exception. Let's see the following example.

Example

1. a = int(input("Enter a:"))
2. b = int(input("Enter b:"))
3. c = a/b
4. print("a/b = %d" %c)
5.
6. #other code:
7. print("Hi I am other part of the program")

Output:

Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero

The above program is syntactically correct, but it through the error because of unusual
input. That kind of programming may not be suitable or recommended for the projects
because these projects are required uninterrupted execution. That's why an exception-
handling plays an essential role in handling these unexpected exceptions. We can
handle these exceptions in the following way.
Exception handling in python
The try-expect statement
If the Python program contains suspicious code that may throw the exception, we must
place that code in the try block. The try block must be followed with
the except statement, which contains a block of code that will be executed if there is
some exception in the try block.

Syntax

1. try:
2. #block of code
3.
4. except Exception1:
5. #block of code
6.
7. except Exception2:
8. #block of code
9.
10. #other code

Consider the following example.

Example 1

1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. except:
6. print("Can't divide with zero")

Output:

Enter a:10
Enter b:0
Can't divide with zero

We can also use the else statement with the try-except statement in which, we can
place the code which will be executed in the scenario if no exception occurs in the try
block.

The syntax to use the else statement with the try-except statement is given below.

1. try:
2. #block of code
3.
4. except Exception1:
5. #block of code
6.
7. else:
8. #this code executes if no except block is executed
Consider the following program.

Example 2

1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. print("a/b = %d"%c)
6. # Using Exception with except statement. If we print(Exception) it will return ex
ception class
7. except Exception:
8. print("can't divide by zero")
9. print(Exception)
10. else:
11. print("Hi I am else block")

Output:

Enter a:10
Enter b:0
can't divide by zero
<class 'Exception'>

The except statement with no exception


Python provides the flexibility not to specify the name of exception with the exception
statement.

Consider the following example.

Example

1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b;
5. print("a/b = %d"%c)
6. except:
7. print("can't divide by zero")
8. else:
9. print("Hi I am else block")
The except statement using with exception variable
We can use the exception variable with the except statement. It is used by using
the as keyword. this object will return the cause of the exception. Consider the
following example:

1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. print("a/b = %d"%c)
6. # Using exception object with the except statement
7. except Exception as e:
8. print("can't divide by zero")
9. print(e)
10. else:
11. print("Hi I am else block")

Output:

Enter a:10
Enter b:0
can't divide by zero
division by zero

Points to remember

1. Python facilitates us to not specify the exception with the except statement.
2. We can declare multiple exceptions in the except statement since the try block
may contain the statements which throw the different type of exceptions.
3. We can also specify an else block along with the try-except statement, which
will be executed if no exception is raised in the try block.
4. The statements that don't throw the exception should be placed inside the else
block.

Example

1. try:
2. #this will throw an exception if the file doesn't exist.
3. fileptr = open("file.txt","r")
4. except IOError:
5. print("File not found")
6. else:
7. print("The file opened successfully")
8. fileptr.close()

Output:

File not found

Declaring Multiple Exceptions


The Python allows us to declare the multiple exceptions with the except clause.
Declaring multiple exceptions is useful in the cases where a try block throws multiple
exceptions. The syntax is given below.

Syntax

1. try:
2. #block of code
3.
4. except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>)
5. #block of code
6.
7. else:
8. #block of code

Consider the following example.

1. try:
2. a=10/0;
3. except(ArithmeticError, IOError):
4. print("Arithmetic Exception")
5. else:
6. print("Successfully Done")

Output:

Arithmetic Exception

The try...finally block


Python provides the optional finally statement, which is used with the try statement.
It is executed no matter what exception occurs and used to release the external
resource. The finally block provides a guarantee of the execution.

We can use the finally block with the try block in which we can pace the necessary
code, which must be executed before the try statement throws an exception.

The syntax to use the finally block is given below.

Syntax

1. try:
2. # block of code
3. # this may throw an exception
4. finally:
5. # block of code
6. # this will always be executed

Example

1. try:
2. fileptr = open("file2.txt","r")
3. try:
4. fileptr.write("Hi I am good")
5. finally:
6. fileptr.close()
7. print("file closed")
8. except:
9. print("Error")

Output:

file closed
Error

Raising exceptions
An exception can be raised forcefully by using the raise clause in Python. It is useful in
in that scenario where we need to raise an exception to stop the execution of the
program.

For example, there is a program that requires 2GB memory for execution, and if the
program tries to occupy 2GB of memory, then we can raise an exception to stop the
execution of the program.

The syntax to use the raise statement is given below.

Syntax

1. raise Exception_class,<value>

Points to remember

1. To raise an exception, the raise statement is used. The exception class name
follows it.
2. An exception can be provided with a value that can be given in the parenthesis.
3. To access the value "as" keyword is used. "e" is used as a reference variable
which stores the value of the exception.
4. We can pass the value to an exception to specify the exception type.

Example

1. try:
2. age = int(input("Enter the age:"))
3. if(age<18):
4. raise ValueError
5. else:
6. print("the age is valid")
7. except ValueError:
8. print("The age is not valid")

Output:

Enter the age:17


The age is not valid

Example 2 Raise the exception with message

1. try:
2. num = int(input("Enter a positive integer: "))
3. if(num <= 0):
4. # we can pass the message in the raise statement
5. raise ValueError("That is a negative number!")
6. except ValueError as e:
7. print(e)

Output:

Enter a positive integer: -5


That is a negative number!

Example 3

1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. if b is 0:
5. raise ArithmeticError
6. else:
7. print("a/b = ",a/b)
8. except ArithmeticError:
9. print("The value of b can't be 0")

Output:

Enter a:10
Enter b:0
The value of b can't be 0

Custom Exception
The Python allows us to create our exceptions that can be raised from the program
and caught using the except clause. However, we suggest you read this section after
visiting the Python object and classes.

Consider the following example.

Example

1. class ErrorInCode(Exception):
2. def __init__(self, data):
3. self.data = data
4. def __str__(self):
5. return repr(self.data)
6.
7. try:
8. raise ErrorInCode(2000)
9. except ErrorInCode as ae:
10. print("Received error:", ae.data)

Output:

Received error: 2000

Python Date and time


Python provides the datetime module work with real dates and times. In real-world
applications, we need to work with the date and time. Python enables us to schedule
our Python script to run at a particular timing.

In Python, the date is not a data type, but we can work with the date objects by
importing the module named with datetime, time, and calendar.

In this section of the tutorial, we will discuss how to work with the date and time objects
in Python.

The datetime classes are classified in the six main classes.

Hello Java Program for Beginners

o date - It is a naive ideal date. It consists of the year, month, and day as
attributes.
o time - It is a perfect time, assuming every day has precisely 24*60*60 seconds.
It has hour, minute, second, microsecond, and tzinfo as attributes.
o datetime - It is a grouping of date and time, along with the attributes year,
month, day, hour, minute, second, microsecond, and tzinfo.
o timedelta - It represents the difference between two dates, time or datetime
instances to microsecond resolution.
o tzinfo - It provides time zone information objects.
o timezone - It is included in the new version of Python. It is the class that
implements the tzinfo abstract base class.

Tick
In Python, the time instants are counted since 12 AM, 1st January 1970. The
function time() of the module time returns the total number of ticks spent since 12
AM, 1st January 1970. A tick can be seen as the smallest unit to measure the time.

Consider the following example

1. import time;
2. #prints the number of ticks spent since 12 AM, 1st January 1970
3. print(time.time())

Output:

1585928913.6519969

How to get the current time?


The localtime() functions of the time module are used to get the current time tuple.
Consider the following example.

Example

1. import time;
2.
3. #returns a time tuple
4.
5. print(time.localtime(time.time()))

Output:

time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21,


tm_sec=40, tm_wday=4, tm_yday=94, tm_isdst=0)
Time tuple
The time is treated as the tuple of 9 numbers. Let's look at the members of the time
tuple.

Index Attribute Values

0 Year 4 digit (for example 2018)

1 Month 1 to 12

2 Day 1 to 31

3 Hour 0 to 23

4 Minute 0 to 59

5 Second 0 to 60

6 Day of weak 0 to 6

7 Day of year 1 to 366

8 Daylight savings -1, 0, 1 , or -1

Getting formatted time


The time can be formatted by using the asctime() function of the time module. It
returns the formatted time for the time tuple being passed.

Example

1. import time
2. #returns the formatted time
3.
4. print(time.asctime(time.localtime(time.time())))

Output:

Tue Dec 18 15:31:39 2018


Python sleep time
The sleep() method of time module is used to stop the execution of the script for a
given amount of time. The output will be delayed for the number of seconds provided
as the float.

Consider the following example.

Example

1. import time
2. for i in range(0,5):
3. print(i)
4. #Each element will be printed after 1 second
5. time.sleep(1)

Output:

0
1
2
3
4

The datetime Module


The datetime module enables us to create the custom date objects, perform various
operations on dates like the comparison, etc.

To work with dates as date objects, we have to import the datetime module into the
python source code.

Consider the following example to get the datetime object representation for the
current time.

Example

1. import datetime
2. #returns the current datetime object
3. print(datetime.datetime.now())

Output:

2020-04-04 13:18:35.252578
Creating date objects
We can create the date objects bypassing the desired date in the datetime constructor
for which the date objects are to be created.

Consider the following example.

Example

1. import datetime
2. #returns the datetime object for the specified date
3. print(datetime.datetime(2020,04,04))

Output:

2020-04-04 00:00:00

We can also specify the time along with the date to create the datetime object.
Consider the following example.

Example

1. import datetime
2.
3. #returns the datetime object for the specified time
4.
5. print(datetime.datetime(2020,4,4,1,26,40))

Output:

2020-04-04 01:26:40

In the above code, we have passed in datetime() function year, month, day, hour,
minute, and millisecond attributes in a sequential manner.

Comparison of two dates


We can compare two dates by using the comparison operators like >, >=, <, and <=.

Consider the following example.

Example

1. from datetime import datetime as dt


2. #Compares the time. If the time is in between 8AM and 4PM, then it prints wo
rking hours otherwise it prints fun hours
3. if dt(dt.now().year,dt.now().month,dt.now().day,8)<dt.now()<dt(dt.now().year,d
t.now().month,dt.now().day,16):
4. print("Working hours....")
5. else:
6. print("fun hours")

Output:

fun hours

The calendar module


Python provides a calendar object that contains various methods to work with the
calendars.

Consider the following example to print the calendar for the last month of 2018.

Example

1. import calendar;
2. cal = calendar.month(2020,3)
3. #printing the calendar of December 2018
4. print(cal)

Output:

March 2020
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Printing the calendar of whole year


The prcal() method of calendar module is used to print the calendar of the entire year.
The year of which the calendar is to be printed must be passed into this method.

Example

1. import calendar
2. #printing the calendar of the year 2019
3. s = calendar.prcal(2020)

Output:

Python Regular Expressions


The regular expressions can be defined as the sequence of characters which are used
to search for a pattern in a string. The module re provides the support to use regex in
the python program. The re module throws an exception if there is some error while
using the regular expression.

The re module must be imported to use the regex functionalities in python.

1. import re

Regex Functions
The following regex functions are used in the python.
SN Function Description

1 match This method matches the regex pattern in the string with the optional flag. It
returns true if a match is found in the string otherwise it returns false.

2 search This method returns the match object if there is a match found in the string.

3 findall It returns a list that contains all the matches of a pattern in the string.

4 split Returns a list in which the string has been split in each match.

5 sub Replace one or many matches in the string.

Forming a regular expression


A regular expression can be formed by using the mix of meta-characters, special
sequences, and sets.

Java Try Catch

Meta-Characters
Metacharacter is a character with the specified meaning.

Metacharacter Description Example

[] It represents the set of characters. "[a-z]"

\ It represents the special sequence. "\r"

. It signals that any character is present at some specific place. "Ja.v."

^ It represents the pattern present at the beginning of the string. "^Java"

$ It represents the pattern present at the end of the string. "point"

* It represents zero or more occurrences of a pattern in the string. "hello*"

+ It represents one or more occurrences of a pattern in the string. "hello+"

{} The specified number of occurrences of a pattern the string. "java{2}"

| It represents either this or that character is present. "java|point"


() Capture and group

Special Sequences
Special sequences are the sequences containing \ followed by one of the characters.

Character Description

\A It returns a match if the specified characters are present at the beginning of the
string.

\b It returns a match if the specified characters are present at the beginning or the
end of the string.

\B It returns a match if the specified characters are present at the beginning of the
string but not at the end.

\d It returns a match if the string contains digits [0-9].

\D It returns a match if the string doesn't contain the digits [0-9].

\s It returns a match if the string contains any white space character.

\S It returns a match if the string doesn't contain any white space character.

\w It returns a match if the string contains any word characters.

\W It returns a match if the string doesn't contain any word.

\Z Returns a match if the specified characters are at the end of the string.

Sets
A set is a group of characters given inside a pair of square brackets. It represents the
special meaning.

SN Set Description

1 [arn] Returns a match if the string contains any of the specified characters in the set.

2 [a-n] Returns a match if the string contains any of the characters between a to n.

3 [^arn] Returns a match if the string contains the characters except a, r, and n.
4 [0123] Returns a match if the string contains any of the specified digits.

5 [0-9] Returns a match if the string contains any digit between 0 and 9.

6 [0-5][0- Returns a match if the string contains any digit between 00 and 59.
9]

10 [a-zA-Z] Returns a match if the string contains any alphabet (lower-case or upper-case).

The findall() function


This method returns a list containing a list of all matches of a pattern within the string.
It returns the patterns in the order they are found. If there are no matches, then an
empty list is returned.

Consider the following example.

Example

1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.findall("How", str)
6.
7. print(matches)
8.
9. print(matches)

Output:

['How', 'How']

The match object


The match object contains the information about the search and the output. If there is
no match found, the None object is returned.

Example

1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.search("How", str)
6.
7. print(type(matches))
8.
9. print(matches) #matches is the search object

Output:

<class '_sre.SRE_Match'>
<_sre.SRE_Match object; span=(0, 3), match='How'>

The Match object methods


There are the following methods associated with the Match object.

1. span(): It returns the tuple containing the starting and end position of the
match.
2. string(): It returns a string passed into the function.
3. group(): The part of the string is returned where the match is found.

Example

1. import re
2.
3. str = "How are you. How is everything"
4.
5. matches = re.search("How", str)
6.
7. print(matches.span())
8.
9. print(matches.group())
10.
11. print(matches.string)

Output:

(0, 3)
How
How are you. How is everything
Python OOPs Concepts
Like other general-purpose programming languages, Python is also an object-oriented
language since its beginning. It allows us to develop applications using an Object-
Oriented approach. In Python, we can easily create and use classes and objects.

An object-oriented paradigm is to design the program using classes and objects. The
object is related to real-word entities such as book, house, pencil, etc. The oops
concept focuses on writing the reusable code. It is a widespread technique to solve
the problem by creating objects.

Major principles of object-oriented programming system are given below.

o Class
o Object
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation

Class
The class can be defined as a collection of objects. It is a logical entity that has some
specific attributes and methods. For example: if you have an employee class, then it
should contain an attribute and method, i.e. an email id, name, age, salary, etc.

Features of Java - Javatpoint

Syntax

1. class ClassName:
2. <statement-1>
3. .
4. .
5. <statement-N>

Object
The object is an entity that has state and behavior. It may be any real-world object like
the mouse, keyboard, chair, table, pen, etc.

Everything in Python is an object, and almost everything has attributes and methods.
All functions have a built-in attribute __doc__, which returns the docstring defined in
the function source code.

When we define a class, it needs to create an object to allocate the memory. Consider
the following example.

Example:

1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()

Output:

Toyota 2016

In the above example, we have created the class named car, and it has two attributes
modelname and year. We have created a c1 object to access the class attribute. The c1
object will allocate memory for these values. We will learn more about class and object
in the next tutorial.

Method
The method is a function that is associated with an object. In Python, a method is not
unique to class instances. Any object type can have methods.

Inheritance
Inheritance is the most important aspect of object-oriented programming, which
simulates the real-world concept of inheritance. It specifies that the child object
acquires all the properties and behaviors of the parent object.
By using inheritance, we can create a class which uses all the properties and behavior
of another class. The new class is known as a derived class or child class, and the one
whose properties are acquired is known as a base class or parent class.

It provides the re-usability of the code.

Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means many, and morph
means shape. By polymorphism, we understand that one task can be performed in
different ways. For example - you have a class animal, and all animals speak. But they
speak differently. Here, the "speak" behavior is polymorphic in a sense and depends
on the animal. So, the abstract "animal" concept does not actually "speak", but specific
animals (like dogs and cats) have a concrete implementation of the action "speak".

Encapsulation
Encapsulation is also an essential aspect of object-oriented programming. It is used to
restrict access to methods and variables. In encapsulation, code and data are wrapped
together within a single unit from being modified by accident.

Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly
synonyms because data abstraction is achieved through encapsulation.

Abstraction is used to hide internal details and show only functionalities. Abstracting
something means to give names to things so that the name captures the core of what
a function or a whole program does.

Object-oriented vs. Procedure-oriented Programming


languages
The difference between object-oriented and procedure-oriented programming is
given below:

Index Object-oriented Programming Procedural Programming

1. Object-oriented programming is the Procedural programming uses a list of


problem-solving approach and used where instructions to do computation step by
computation is done by using objects. step.
2. It makes the development and maintenance In procedural programming, It is not easy
easier. to maintain the codes when the project
becomes lengthy.

3. It simulates the real world entity. So real- It doesn't simulate the real world. It works
world problems can be easily solved on step by step instructions divided into
through oops. small parts called functions.

4. It provides data hiding. So it is more secure Procedural language doesn't provide any
than procedural languages. You cannot proper way for data binding, so it is less
access private data from anywhere. secure.

5. Example of object-oriented programming Example of procedural languages are: C,


languages is C++, Java, .Net, Python, C#, Fortran, Pascal, VB etc.
etc.

Python Class and Objects


We have already discussed in previous tutorial, a class is a virtual entity and can be
seen as a blueprint of an object. The class came into existence when it instantiated.
Let's understand it by an example.

Suppose a class is a prototype of a building. A building contains all the details about
the floor, rooms, doors, windows, etc. we can make as many buildings as we want,
based on these details. Hence, the building can be seen as a class, and we can create
as many objects of this class.

On the other hand, the object is the instance of a class. The process of creating an
object can be called instantiation.

In this section of the tutorial, we will discuss creating classes and objects in Python. We
will also discuss how a class attribute is accessed by using the object.

C++ vs Java

Creating classes in Python


In Python, a class can be created by using the keyword class, followed by the class
name. The syntax to create a class is given below.

Syntax

1. class ClassName:
2. #statement_suite
In Python, we must notice that each class is associated with a documentation string
which can be accessed by using <class-name>.__doc__. A class contains a statement
suite including fields, constructor, function, etc. definition.

Consider the following example to create a class Employee which contains two fields
as Employee id, and name.

The class also contains a function display(), which is used to display the information
of the Employee.

Example

1. class Employee:
2. id = 10
3. name = "Devansh"
4. def display (self):
5. print(self.id,self.name)

Here, the self is used as a reference variable, which refers to the current class object.
It is always the first argument in the function definition. However, using self is optional
in the function call.

The self-parameter
The self-parameter refers to the current instance of the class and accesses the class
variables. We can use anything instead of self, but it must be the first parameter of any
function which belongs to the class.

Creating an instance of the class


A class needs to be instantiated if we want to use the class attributes in another class
or method. A class can be instantiated by calling the class using the class name.

The syntax to create the instance of the class is given below.

1. <object-name> = <class-name>(<arguments>)

The following example creates the instance of the class Employee defined in the above
example.

Example
1. class Employee:
2. id = 10
3. name = "John"
4. def display (self):
5. print("ID: %d \nName: %s"%(self.id,self.name))
6. # Creating a emp instance of Employee class
7. emp = Employee()
8. emp.display()

Output:

ID: 10
Name: John

In the above code, we have created the Employee class which has two attributes named
id and name and assigned value to them. We can observe we have passed the self as
parameter in display function. It is used to refer to the same class attribute.

We have created a new instance object named emp. By using it, we can access the
attributes of the class.

Delete the Object


We can delete the properties of the object or object itself by using the del keyword.
Consider the following example.

Example

1. class Employee:
2. id = 10
3. name = "John"
4.
5. def display(self):
6. print("ID: %d \nName: %s" % (self.id, self.name))
7. # Creating a emp instance of Employee class
8.
9. emp = Employee()
10.
11. # Deleting the property of object
12. del emp.id
13. # Deleting the object itself
14. del emp
15. emp.display()

It will through the Attribute error because we have deleted the object emp.

Python Constructor
A constructor is a special type of method (function) which is used to initialize the
instance members of the class.

In C++ or Java, the constructor has the same name as its class, but it treats constructor
differently in Python. It is used to create an object.

Constructors can be of two types.

1. Parameterized Constructor
2. Non-parameterized Constructor

Constructor definition is executed when we create the object of this class. Constructors
also verify that there are enough resources for the object to perform any start-up task.

10 Sec

History of Java

Creating the constructor in python


In Python, the method the __init__() simulates the constructor of the class. This
method is called when the class is instantiated. It accepts the self-keyword as a first
argument which allows accessing the attributes or method of the class.

We can pass any number of arguments at the time of creating the class object,
depending upon the __init__() definition. It is mostly used to initialize the class
attributes. Every class must have a constructor, even if it simply relies on the default
constructor.

Consider the following example to initialize the Employee class attributes.

Example

1. class Employee:
2. def __init__(self, name, id):
3. self.id = id
4. self.name = name
5.
6. def display(self):
7. print("ID: %d \nName: %s" % (self.id, self.name))
8.
9.
10. emp1 = Employee("John", 101)
11. emp2 = Employee("David", 102)
12.
13. # accessing display() method to print employee 1 information
14.
15. emp1.display()
16.
17. # accessing display() method to print employee 2 information
18. emp2.display()

Output:

ID: 101
Name: John
ID: 102
Name: David

Counting the number of objects of a class


The constructor is called automatically when we create the object of the class. Consider
the following example.

Example

1. class Student:
2. count = 0
3. def __init__(self):
4. Student.count = Student.count + 1
5. s1=Student()
6. s2=Student()
7. s3=Student()
8. print("The number of students:",Student.count)

Output:
The number of students: 3

Python Non-Parameterized Constructor


The non-parameterized constructor uses when we do not want to manipulate the value
or the constructor that has only self as an argument. Consider the following example.

Example

1. class Student:
2. # Constructor - non parameterized
3. def __init__(self):
4. print("This is non parametrized constructor")
5. def show(self,name):
6. print("Hello",name)
7. student = Student()
8. student.show("John")

Python Parameterized Constructor


The parameterized constructor has multiple parameters along with the self. Consider
the following example.

Example

1. class Student:
2. # Constructor - parameterized
3. def __init__(self, name):
4. print("This is parametrized constructor")
5. self.name = name
6. def show(self):
7. print("Hello",self.name)
8. student = Student("John")
9. student.show()

Output:

This is parametrized constructor


Hello John

Python Default Constructor


When we do not include the constructor in the class or forget to declare it, then that
becomes the default constructor. It does not perform any task but initializes the
objects. Consider the following example.

Example

1. class Student:
2. roll_num = 101
3. name = "Joseph"
4.
5. def display(self):
6. print(self.roll_num,self.name)
7.
8. st = Student()
9. st.display()

Output:

101 Joseph

More than One Constructor in Single class


Let's have a look at another scenario, what happen if we declare the two same
constructors in the class.

Example

1. class Student:
2. def __init__(self):
3. print("The First Constructor")
4. def __init__(self):
5. print("The second contructor")
6.
7. st = Student()

Output:

The Second Constructor

In the above code, the object st called the second constructor whereas both have the
same configuration. The first method is not accessible by the st object. Internally, the
object of the class will always call the last constructor if the class has multiple
constructors.

Note: The constructor overloading is not allowed in Python.

Python built-in class functions


The built-in functions defined in the class are described in the following table.

SN Function Description

1 getattr(obj,name,default) It is used to access the attribute of the object.

2 setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.

3 delattr(obj, name) It is used to delete a specific attribute.

4 hasattr(obj, name) It returns true if the object contains some specific attribute.

Example
1. class Student:
2. def __init__(self, name, id, age):
3. self.name = name
4. self.id = id
5. self.age = age
6.
7. # creates the object of the class Student
8. s = Student("John", 101, 22)
9.
10. # prints the attribute name of the object s
11. print(getattr(s, 'name'))
12.
13. # reset the value of attribute age to 23
14. setattr(s, "age", 23)
15.
16. # prints the modified value of age
17. print(getattr(s, 'age'))
18.
19. # prints true if the student contains the attribute with name id
20.
21. print(hasattr(s, 'id'))
22. # deletes the attribute age
23. delattr(s, 'age')
24.
25. # this will give an error since the attribute age has been deleted
26. print(s.age)

Output:

John
23
True
AttributeError: 'Student' object has no attribute 'age'

Built-in class attributes


Along with the other attributes, a Python class also contains some built-in class
attributes which provide information about the class.

The built-in class attributes are given in the below table.

SN Attribute Description

1 __dict__ It provides the dictionary containing the information about the class namespace.

2 __doc__ It contains a string which has the class documentation

3 __name__ It is used to access the class name.

4 __module__ It is used to access the module in which, this class is defined.

5 __bases__ It contains a tuple including all base classes.

Example

1. class Student:
2. def __init__(self,name,id,age):
3. self.name = name;
4. self.id = id;
5. self.age = age
6. def display_details(self):
7. print("Name:%s, ID:%d, age:%d"%(self.name,self.id))
8. s = Student("John",101,22)
9. print(s.__doc__)
10. print(s.__dict__)
11. print(s.__module__)

Output:

None
{'name': 'John', 'id': 101, 'age': 22}
__main__

Python Inheritance
Inheritance is an important aspect of the object-oriented paradigm. Inheritance
provides code reusability to the program because we can use an existing class to create
a new class instead of creating it from scratch.

In inheritance, the child class acquires the properties and can access all the data
members and functions defined in the parent class. A child class can also provide its
specific implementation to the functions of the parent class. In this section of the
tutorial, we will discuss inheritance in detail.

In python, a derived class can inherit base class by just mentioning the base in the
bracket after the derived class name. Consider the following syntax to inherit a base
class into the derived class.
Syntax
1. class derived-class(base class):
2. <class-suite>

A class can inherit multiple classes by mentioning all of them inside the bracket.
Consider the following syntax.

Syntax
1. class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
2. <class - suite>

Example 1
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #child class Dog inherits the base class Animal
5. class Dog(Animal):
6. def bark(self):
7. print("dog barking")
8. d = Dog()
9. d.bark()
10. d.speak()

Output:

dog barking
Animal Speaking

Python Multi-Level inheritance


Multi-Level inheritance is possible in python like other object-oriented languages.
Multi-level inheritance is archived when a derived class inherits another derived class.
There is no limit on the number of levels up to which, the multi-level inheritance is
archived in python.
The syntax of multi-level inheritance is given below.

Syntax
1. class class1:
2. <class-suite>
3. class class2(class1):
4. <class suite>
5. class class3(class2):
6. <class suite>
7. .
8. .

Example
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #The child class Dog inherits the base class Animal
5. class Dog(Animal):
6. def bark(self):
7. print("dog barking")
8. #The child class Dogchild inherits another child class Dog
9. class DogChild(Dog):
10. def eat(self):
11. print("Eating bread...")
12. d = DogChild()
13. d.bark()
14. d.speak()
15. d.eat()

Output:

dog barking
Animal Speaking
Eating bread...

Python Multiple inheritance


Python provides us the flexibility to inherit multiple base classes in the child class.

The syntax to perform multiple inheritance is given below.

Syntax
1. class Base1:
2. <class-suite>
3.
4. class Base2:
5. <class-suite>
6. .
7. .
8. .
9. class BaseN:
10. <class-suite>
11.
12. class Derived(Base1, Base2, ...... BaseN):
13. <class-suite>

Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(d.Summation(10,20))
12. print(d.Multiplication(10,20))
13. print(d.Divide(10,20))

Output:

30
200
0.5

The issubclass(sub,sup) method


The issubclass(sub, sup) method is used to check the relationships between the
specified classes. It returns true if the first class is the subclass of the second class, and
false otherwise.

Consider the following example.

Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(issubclass(Derived,Calculation2))
12. print(issubclass(Calculation1,Calculation2))

Output:

True
False

The isinstance (obj, class) method


The isinstance() method is used to check the relationship between the objects and
classes. It returns true if the first parameter, i.e., obj is the instance of the second
parameter, i.e., class.

Consider the following example.

Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(isinstance(d,Derived))

Output:

True

Method Overriding
We can provide some specific implementation of the parent class method in our child
class. When the parent class method is defined in the child class with some specific
implementation, then the concept is called method overriding. We may need to
perform method overriding in the scenario where the different definition of a parent
class method is needed in the child class.

Consider the following example to perform method overriding in python.

Example
1. class Animal:
2. def speak(self):
3. print("speaking")
4. class Dog(Animal):
5. def speak(self):
6. print("Barking")
7. d = Dog()
8. d.speak()

Output:

Barking

Real Life Example of method overriding


1. class Bank:
2. def getroi(self):
3. return 10;
4. class SBI(Bank):
5. def getroi(self):
6. return 7;
7.
8. class ICICI(Bank):
9. def getroi(self):
10. return 8;
11. b1 = Bank()
12. b2 = SBI()
13. b3 = ICICI()
14. print("Bank Rate of interest:",b1.getroi());
15. print("SBI Rate of interest:",b2.getroi());
16. print("ICICI Rate of interest:",b3.getroi());

Output:

Bank Rate of interest: 10


SBI Rate of interest: 7
ICICI Rate of interest: 8

Data abstraction in python


Abstraction is an important aspect of object-oriented programming. In python, we can
also perform data hiding by adding the double underscore (___) as a prefix to the
attribute which is to be hidden. After this, the attribute will not be visible outside of
the class through the object.
Consider the following example.

Example
1. class Employee:
2. __count = 0;
3. def __init__(self):
4. Employee.__count = Employee.__count+1
5. def display(self):
6. print("The number of employees",Employee.__count)
7. emp = Employee()
8. emp2 = Employee()
9. try:
10. print(emp.__count)

finally: Abstraction in Python


Abstraction is used to hide the internal functionality of the function from the users.
The users only interact with the basic implementation of the function, but inner
working is hidden. User is familiar with that "what function does" but they don't
know "how it does."

In simple words, we all use the smartphone and very much familiar with its functions
such as camera, voice-recorder, call-dialing, etc., but we don't know how these
operations are happening in the background. Let's take another example - When we
use the TV remote to increase the volume. We don't know how pressing a key increases
the volume of the TV. We only know to press the "+" button to increase the volume.

That is exactly the abstraction that works in the object-oriented concept.

Why Abstraction is Important?


In Python, an abstraction is used to hide the irrelevant data/class in order to reduce
the complexity. It also enhances the application efficiency. Next, we will learn how we
can achieve abstraction using the Python program.

Features of Java - Javatpoint

Abstraction classes in Python


In Python, abstraction can be achieved by using abstract classes and interfaces.
A class that consists of one or more abstract method is called the abstract class.
Abstract methods do not contain their implementation. Abstract class can be inherited
by the subclass and abstract method gets its definition in the subclass. Abstraction
classes are meant to be the blueprint of the other class. An abstract class can be useful
when we are designing large functions. An abstract class is also helpful to provide the
standard interface for different implementations of components. Python provides
the abc module to use the abstraction in the Python program. Let's see the following
syntax.

Syntax

1. from abc import ABC


2. class ClassName(ABC):

We import the ABC class from the abc module.

Abstract Base Classes


An abstract base class is the common application program of the interface for a set of
subclasses. It can be used by the third-party, which will provide the implementations
such as with plugins. It is also beneficial when we work with the large code-base hard
to remember all the classes.

Working of the Abstract Classes


Unlike the other high-level language, Python doesn't provide the abstract class itself.
We need to import the abc module, which provides the base for defining Abstract Base
classes (ABC). The ABC works by decorating methods of the base class as abstract. It
registers concrete classes as the implementation of the abstract base. We use
the @abstractmethod decorator to define an abstract method or if we don't provide
the definition to the method, it automatically becomes the abstract method. Let's
understand the following example.

Example -

1. # Python program demonstrate


2. # abstract base class work
3. from abc import ABC, abstractmethod
4. class Car(ABC):
5. def mileage(self):
6. pass
7.
8. class Tesla(Car):
9. def mileage(self):
10. print("The mileage is 30kmph")
11. class Suzuki(Car):
12. def mileage(self):
13. print("The mileage is 25kmph ")
14. class Duster(Car):
15. def mileage(self):
16. print("The mileage is 24kmph ")
17.
18. class Renault(Car):
19. def mileage(self):
20. print("The mileage is 27kmph ")
21.
22. # Driver code
23. t= Tesla ()
24. t.mileage()
25.
26. r = Renault()
27. r.mileage()
28.
29. s = Suzuki()
30. s.mileage()
31. d = Duster()
32. d.mileage()

Output:

The mileage is 30kmph


The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph

Explanation -

In the above code, we have imported the abc module to create the abstract base class.
We created the Car class that inherited the ABC class and defined an abstract method
named mileage(). We have then inherited the base class from the three different
subclasses and implemented the abstract method differently. We created the objects
to call the abstract method.
Let's understand another example.

Let's understand another example.

Example -

1. # Python program to define


2. # abstract class
3.
4. from abc import ABC
5.
6. class Polygon(ABC):
7.
8. # abstract method
9. def sides(self):
10. pass
11.
12. class Triangle(Polygon):
13.
14.
15. def sides(self):
16. print("Triangle has 3 sides")
17.
18. class Pentagon(Polygon):
19.
20.
21. def sides(self):
22. print("Pentagon has 5 sides")
23.
24. class Hexagon(Polygon):
25.
26. def sides(self):
27. print("Hexagon has 6 sides")
28.
29. class square(Polygon):
30.
31. def sides(self):
32. print("I have 4 sides")
33.
34. # Driver code
35. t = Triangle()
36. t.sides()
37.
38. s = square()
39. s.sides()
40.
41. p = Pentagon()
42. p.sides()
43.
44. k = Hexagon()
45. K.sides()

Output:

Triangle has 3 sides


Square has 4 sides
Pentagon has 5 sides
Hexagon has 6 sides

Explanation -

In the above code, we have defined the abstract base class named Polygon and we
also defined the abstract method. This base class inherited by the various subclasses.
We implemented the abstract method in each subclass. We created the object of the
subclasses and invoke the sides() method. The hidden implementations for
the sides() method inside the each subclass comes into play. The abstract
method sides() method, defined in the abstract class, is never invoked.

Points to Remember
Below are the points which we should remember about the abstract base class in
Python.

o An Abstract class can contain the both method normal and abstract method.
o An Abstract cannot be instantiated; we cannot create objects for the abstract
class.

Abstraction is essential to hide the core functionality from the users. We have covered
the all the basic concepts of Abstraction in Python.
11. emp.display()

Output:

The number of employees 2


AttributeError: 'Employee' object has no attribute '__count'

Environment Setup
To build the real world applications, connecting with the databases is the necessity for
the programming languages. However, python allows us to connect our application to
the databases like MySQL, SQLite, MongoDB, and many others.

In this section of the tutorial, we will discuss Python - MySQL connectivity, and we will
perform the database operations in python. We will also cover the Python connectivity
with the databases like MongoDB and SQLite later in this tutorial.

Install mysql.connector
To connect the python application with the MySQL database, we must import the
mysql.connector module in the program.

The mysql.connector is not a built-in module that comes with the python installation.
We need to install it to get it working.

11.2M
269
C++ vs Java

Execute the following command to install it using pip installer.

1. > python -m pip install mysql-connector

Or follow the following steps.

1. Click the link:

https://files.pythonhosted.org/packages/8f/6d/fb8ebcbbaee68b172ce3dfd08c7b866
0d09f91d8d5411298bcacbd309f96/mysql-connector-python-8.0.13.tar.gz to
download the source code.

2. Extract the archived file.

3. Open the terminal (CMD for windows) and change the present working directory to
the source code directory.
1. $ cd mysql-connector-python-8.0.13/

4. Run the file named setup.py with python (python3 in case you have also installed
python 2) with the parameter build.

1. $ python setup.py build

5. Run the following command to install the mysql-connector.

1. $ python setup.py install

This will take a bit of time to install mysql-connector for python. We can verify the
installation once the process gets over by importing mysql-connector on the python
shell.

Hence, we have successfully installed mysql-connector for python on our system.

Database Connection
In this section of the tutorial, we will discuss the steps to connect the python
application to the database.

There are the following steps to connect a python application to our database.

1. Import mysql.connector module


2. Create the connection object.
3. Create the cursor object
4. Execute the query

Creating the connection


To create a connection between the MySQL database and the python application, the
connect() method of mysql.connector module is used.

Pass the database details like HostName, username, and the database password in the
method call. The method returns the connection object.

Snake bites man sitting on toilet

The syntax to use the connect() is given below.

1. Connection-Object= mysql.connector.connect(host = <host-


name> , user = <username> , passwd = <password> )

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google")
5.
6. #printing the connection object
7. print(myconn)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7fb142edd780>

Here, we must notice that we can specify the database name in the connect() method
if we want to connect to a specific database.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google", database = "mydb")
5.
6. #printing the connection object
7. print(myconn)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7ff64aa3d7b8>

Creating a cursor object


The cursor object can be defined as an abstraction specified in the Python DB-API 2.0.
It facilitates us to have multiple separate working environments through the same
connection to the database. We can create the cursor object by calling the 'cursor'
function of the connection object. The cursor object is an important aspect of
executing queries to the databases.

The syntax to create the cursor object is given below.

1. <my_cur> = conn.cursor()

Example

1. import mysql.connector
2. #Create the connection object
3. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google", database = "mydb")
4.
5. #printing the connection object
6. print(myconn)
7.
8. #creating the cursor object
9. cur = myconn.cursor()
10.
11. print(cur)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7faa17a15748>


MySQLCursor: (Nothing executed yet)

Creating new databases


In this section of the tutorial, we will create the new database PythonDB.
Getting the list of existing databases
We can get the list of all the databases by using the following MySQL query.

1. > show databases;

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. dbs = cur.execute("show databases")
11. except:
12. myconn.rollback()
13. for x in cur:
14. print(x)
15. myconn.close()

Output:

('EmployeeDB',)
('Test',)
('TestDB',)
('information_schema',)
('javatpoint',)
('javatpoint1',)
('mydb',)
('mysql',)
('performance_schema',)
('testDB',)

Creating the new database


The new database can be created by using the following SQL query.

Prime Ministers of India | List of Prime Minister of India (1947-2020)


1. > create database <database-name>

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #creating a new database
11. cur.execute("create database PythonDB2")
12.
13. #getting the list of all the databases which will now include the new databa
se PythonDB
14. dbs = cur.execute("show databases")
15.
16. except:
17. myconn.rollback()
18.
19. for x in cur:
20. print(x)
21.
22. myconn.close()

Output:

('EmployeeDB',)
('PythonDB',)
('Test',)
('TestDB',)
('anshika',)
('information_schema',)
('javatpoint',)
('javatpoint1',)
('mydb',)
('mydb1',)
('mysql',)
('performance_schema',)
('testDB',)

Creating the table


In this section of the tutorial, we will create the new table Employee. We have to
mention the database name while establishing the connection object.

We can create the new table by using the CREATE TABLE statement of SQL. In our
database PythonDB, the table Employee will have the four columns, i.e., name, id,
salary, and department_id initially.

The following query is used to create the new table Employee.

1. > create table Employee (name varchar(20) not null, id int primary key, salary
float not null, Dept_Id int not null)

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Creating a table with name Employee having four columns i.e., name, id, sa
lary, and department id
11. dbs = cur.execute("create table Employee(name varchar(20) not null, id int(2
0) not null primary key, salary float not null, Dept_id int not null)")
12. except:
13. myconn.rollback()
14.
15. myconn.close()
Now, we may check that the table Employee is present in the database.

HTML Tutorial

Alter Table
Sometimes, we may forget to create some columns, or we may need to update the
table schema. The alter statement used to alter the table schema if required. Here, we
will add the column branch_name to the table Employee. The following SQL query is
used for this purpose.

1. alter table Employee add branch_name varchar(20) not null

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #adding a column branch name to the table Employee
11. cur.execute("alter table Employee add branch_name varchar(20) not null")
12. except:
13. myconn.rollback()
14.
15. myconn.close()

Insert Operation
Adding a record to the table
The INSERT INTO statement is used to add a record to the table. In python, we can
mention the format specifier (%s) in place of values.

We provide the actual values in the form of tuple in the execute() method of the cursor.
Consider the following example.

Example

1. import mysql.connector
2. #Create the connection object
3. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
4. #creating the cursor object
5. cur = myconn.cursor()
6. sql = "insert into Employee(name, id, salary, dept_id, branch_name) values (%s,
%s, %s, %s, %s)"
7.
8. #The row values are provided in the form of tuple
9. val = ("John", 110, 25000.00, 201, "Newyork")
10.
11. try:
12. #inserting the values into the table
13. cur.execute(sql,val)
14.
15. #commit the transaction
16. myconn.commit()
17.
18. except:
19. myconn.rollback()
20.
21. print(cur.rowcount,"record inserted!")
22. myconn.close()

Output:

Snake bites man sitting on toilet


1 record inserted!
Insert multiple rows
We can also insert multiple rows at once using the python script. The multiple rows
are mentioned as the list of various tuples.

Each element of the list is treated as one particular row, whereas each element of the
tuple is treated as one particular column value (attribute).

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8. sql = "insert into Employee(name, id, salary, dept_id, branch_name) values (%s,
%s, %s, %s, %s)"
9. val = [("John", 102, 25000.00, 201, "Newyork"),("David",103,25000.00,202,"Port
of spain"),("Nick",104,90000.00,201,"Newyork")]
10.
11. try:
12. #inserting the values into the table
13. cur.executemany(sql,val)
14.
15. #commit the transaction
16. myconn.commit()
17. print(cur.rowcount,"records inserted!")
18.
19. except:
20. myconn.rollback()
21.
22. myconn.close()

Output:

3 records inserted!
Row ID
In SQL, a particular row is represented by an insertion id which is known as row id. We
can get the last inserted row id by using the attribute lastrowid of the cursor object.

Consider the following example.

Example

1. import mysql.connector
2. #Create the connection object
3. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
4. #creating the cursor object
5. cur = myconn.cursor()
6.
7. sql = "insert into Employee(name, id, salary, dept_id, branch_name) values (%s,
%s, %s, %s, %s)"
8.
9. val = ("Mike",105,28000,202,"Guyana")
10.
11. try:
12. #inserting the values into the table
13. cur.execute(sql,val)
14.
15. #commit the transaction
16. myconn.commit()
17.
18. #getting rowid
19. print(cur.rowcount,"record inserted! id:",cur.lastrowid)
20.
21. except:
22. myconn.rollback()
23.
24. myconn.close()

Output:

1 record inserted! Id: 0


Read Operation
The SELECT statement is used to read the values from the databases. We can restrict
the output of a select query by using various clause in SQL like where, limit, etc.

Python provides the fetchall() method returns the data stored inside the table in the
form of rows. We can iterate the result to get the individual rows.

In this section of the tutorial, we will extract the data from the database by using the
python script. We will also format the output to print it on the console.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select * from Employee")
12.
13. #fetching the rows from the cursor object
14. result = cur.fetchall()
15. #printing the result
16.
17. for x in result:
18. print(x);
19. except:
20. myconn.rollback()
21.
22. myconn.close()

Output:

Snake bites man sitting on toilet


('John', 101, 25000.0, 201, 'Newyork')
('John', 102, 25000.0, 201, 'Newyork')
('David', 103, 25000.0, 202, 'Port of spain')
('Nick', 104, 90000.0, 201, 'Newyork')
('Mike', 105, 28000.0, 202, 'Guyana')

Reading specific columns


We can read the specific columns by mentioning their names instead of using star (*).

In the following example, we will read the name, id, and salary from the Employee table
and print it on the console.

Example

1. import mysql.connector
2. #Create the connection object
3. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
4. #creating the cursor object
5. cur = myconn.cursor()
6. try:
7. #Reading the Employee data
8. cur.execute("select name, id, salary from Employee")
9.
10. #fetching the rows from the cursor object
11. result = cur.fetchall()
12. #printing the result
13. for x in result:
14. print(x);
15. except:
16. myconn.rollback()
17. myconn.close()

Output:

('John', 101, 25000.0)


('John', 102, 25000.0)
('David', 103, 25000.0)
('Nick', 104, 90000.0)
('Mike', 105, 28000.0)
The fetchone() method
The fetchone() method is used to fetch only one row from the table. The fetchone()
method returns the next row of the result-set.

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select name, id, salary from Employee")
12.
13. #fetching the first row from the cursor object
14. result = cur.fetchone()
15.
16. #printing the result
17. print(result)
18.
19. except:
20. myconn.rollback()
21.
22. myconn.close()

Output:

('John', 101, 25000.0)

Formatting the result


We can format the result by iterating over the result produced by the fetchall() or
fetchone() method of cursor object since the result exists as the tuple object which is
not readable.

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10.
11. #Reading the Employee data
12. cur.execute("select name, id, salary from Employee")
13.
14. #fetching the rows from the cursor object
15. result = cur.fetchall()
16.
17. print("Name id Salary");
18. for row in result:
19. print("%s %d %d"%(row[0],row[1],row[2]))
20. except:
21. myconn.rollback()
22.
23. myconn.close()

Output:

Name id Salary
John 101 25000
John 102 25000
David 103 25000
Nick 104 90000
Mike 105 28000
Using where clause
We can restrict the result produced by the select statement by using the where clause.
This will extract only those columns which satisfy the where condition.

Consider the following example.

Example: printing the names that start with j

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select name, id, salary from Employee where name like 'J%'")
12.
13. #fetching the rows from the cursor object
14. result = cur.fetchall()
15.
16. print("Name id Salary");
17.
18. for row in result:
19. print("%s %d %d"%(row[0],row[1],row[2]))
20. except:
21. myconn.rollback()
22.
23. myconn.close()

Output:

Name id Salary
John 101 25000
John 102 25000

Example: printing the names with id = 101, 102, and 103

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select name, id, salary from Employee where id in (101,102,103
)")
12.
13. #fetching the rows from the cursor object
14. result = cur.fetchall()
15.
16. print("Name id Salary");
17.
18. for row in result:
19. print("%s %d %d"%(row[0],row[1],row[2]))
20. except:
21. myconn.rollback()
22.
23. myconn.close()

Output:

Name id Salary
John 101 25000
John 102 25000
David 103 2500

Ordering the result


The ORDER BY clause is used to order the result. Consider the following example.
Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select name, id, salary from Employee order by name")
12.
13. #fetching the rows from the cursor object
14. result = cur.fetchall()
15.
16. print("Name id Salary");
17.
18. for row in result:
19. print("%s %d %d"%(row[0],row[1],row[2]))
20. except:
21. myconn.rollback()
22.
23. myconn.close()

Output:

Name id Salary
David 103 25000
John 101 25000
John 102 25000
Mike 105 28000
Nick 104 90000

Order by DESC
This orders the result in the decreasing order of a particular column.
Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Reading the Employee data
11. cur.execute("select name, id, salary from Employee order by name desc")
12.
13. #fetching the rows from the cursor object
14. result = cur.fetchall()
15.
16. #printing the result
17. print("Name id Salary");
18. for row in result:
19. print("%s %d %d"%(row[0],row[1],row[2]))
20.
21. except:
22. myconn.rollback()
23.
24. myconn.close()

Output:

Name id Salary
Nick 104 90000
Mike 105 28000
John 101 25000
John 102 25000
David 103 25000

Update Operation
The UPDATE-SET statement is used to update any column inside the table. The
following SQL query is used to update a column.

1. > update Employee set name = 'alex' where id = 110

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #updating the name of the employee whose id is 110
11. cur.execute("update Employee set name = 'alex' where id = 110")
12. myconn.commit()
13. except:
14.
15. myconn.rollback()
16.
17. myconn.close()
Delete Operation
The DELETE FROM statement is used to delete a specific record from the table. Here,
we must impose a condition using WHERE clause otherwise all the records from the
table will be removed.

The following SQL query is used to delete the employee detail whose id is 110 from
the table.

History of Java

1. > delete from Employee where id = 110

Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #Deleting the employee details whose id is 110
11. cur.execute("delete from Employee where id = 110")
12. myconn.commit()
13. except:
14.
15. myconn.rollback()
16.
17. myconn.close()

Join Operation
We can combine the columns from two or more tables by using some common column
among them by using the join statement.
We have only one table in our database, let's create one more table Departments with
two columns department_id and department_name.

1. create table Departments (Dept_id int(20) primary key not null, Dept_Name va
rchar(20) not null);

As we have created a new table Departments as shown in the above image. However,
we haven't yet inserted any value inside it.

Let's insert some Departments ids and departments names so that we can map this to
our Employee table.

Features of Java - Javatpoint

1. insert into Departments values (201, "CS");


2. insert into Departments values (202, "IT");

Let's look at the values inserted in each of the tables. Consider the following image.

Now, let's create a python script that joins the two tables on the common column, i.e.,
dept_id.
Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #joining the two tables on departments_id
11. cur.execute("select Employee.id, Employee.name, Employee.salary, Departm
ents.Dept_id, Departments.Dept_Name from Departments join Employee on D
epartments.Dept_id = Employee.Dept_id")
12. print("ID Name Salary Dept_Id Dept_Name")
13. for row in cur:
14. print("%d %s %d %d %s"%(row[0], row[1],row[2],row[3],row[4]))
15.
16. except:
17. myconn.rollback()
18.
19. myconn.close()

Output:

ID Name Salary Dept_Id Dept_Name


101 John 25000 201 CS
102 John 25000 201 CS
103 David 25000 202 IT
104 Nick 90000 201 CS
105 Mike 28000 202 IT

Right Join
Right join shows all the columns of the right-hand side table as we have two tables in
the database PythonDB, i.e., Departments and Employee. We do not have any
Employee in the table who is not working for any department (Employee for which
department id is null). However, to understand the concept of right join let's create the
one.

Execute the following query on the MySQL server.

1. insert into Employee(name, id, salary, branch_name) values ("Alex",108,29900,"


Mumbai");

This will insert an employee Alex who doesn't work for any department (department
id is null).

Now, we have an employee in the Employee table whose department id is not present
in the Departments table. Let's perform the right join on the two tables now.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #joining the two tables on departments_id
11. result = cur.execute("select Employee.id, Employee.name, Employee.salary,
Departments.Dept_id, Departments.Dept_Name from Departments right join E
mployee on Departments.Dept_id = Employee.Dept_id")
12.
13. print("ID Name Salary Dept_Id Dept_Name")
14.
15. for row in cur:
16. print(row[0]," ", row[1]," ",row[2]," ",row[3]," ",row[4])
17.
18.
19.
20. except:
21. myconn.rollback()
22.
23. myconn.close()

Output:

ID Name Salary Dept_Id Dept_Name


101 John 25000.0 201 CS
102 John 25000.0 201 CS
103 David 25000.0 202 IT
104 Nick 90000.0 201 CS
105 Mike 28000.0 202 IT
108 Alex 29900.0 None None

Left Join
The left join covers all the data from the left-hand side table. It has just opposite effect
to the right join. Consider the following example.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. #joining the two tables on departments_id
11. result = cur.execute("select Employee.id, Employee.name, Employee.salary,
Departments.Dept_id, Departments.Dept_Name from Departments left join Em
ployee on Departments.Dept_id = Employee.Dept_id")
12. print("ID Name Salary Dept_Id Dept_Name")
13. for row in cur:
14. print(row[0]," ", row[1]," ",row[2]," ",row[3]," ",row[4])
15.
16.
17.
18. except:
19. myconn.rollback()
20.
21. myconn.close()

Output:

ID Name Salary Dept_Id Dept_Name


101 John 25000.0 201 CS
102 John 25000.0 201 CS
103 David 25000.0 202 IT
104 Nick 90000.0 201 CS
105 Mike 28000.0 202 IT

Performing Transactions
Transactions ensure the data consistency of the database. We have to make sure that
more than one applications must not modify the records while performing the
database operations. The transactions have the following properties.

1. Atomicity
Either the transaction completes, or nothing happens. If a transaction contains
4 queries then all these queries must be executed, or none of them must be
executed.
2. Consistency
The database must be consistent before the transaction starts and the database
must also be consistent after the transaction is completed.
3. Isolation
Intermediate results of a transaction are not visible outside the current
transaction.
4. Durability
Once a transaction was committed, the effects are persistent, even after a
system failure.

Python commit() method


Python provides the commit() method which ensures the changes made to

the database consistently take place.


The syntax to use the commit() method is given below.OOPs Concepts in Java

1. conn.commit() #conn is the connection object

All the operations that modify the records of the database do not take place until the
commit() is called.

Python rollback() method


The rollback() method is used to revert the changes that are done to the database. This
method is useful in the sense that, if some error occurs during the database operations,
we can rollback that transaction to maintain the database consistency.

The syntax to use the rollback() is given below.

1. Conn.rollback()

Closing the connection


We need to close the database connection once we have done all the operations
regarding the database. Python provides the close() method. The syntax to use the
close() method is given below.

1. conn.close()

In the following example, we are deleting all the employees who are working for the
CS department.

Example

1. import mysql.connector
2.
3. #Create the connection object
4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google",database = "PythonDB")
5.
6. #creating the cursor object
7. cur = myconn.cursor()
8.
9. try:
10. cur.execute("delete from Employee where Dept_id = 201")
11. myconn.commit()
12. print("Deleted !")
13. except:
14. print("Can't delete !")
15. myconn.rollback()
16.
17. myconn.close()

Output:

Deleted !

Python Tkinter Tutorial

Tkinter tutorial provides basic and advanced concepts of Python Tkinter. Our Tkinter
tutorial is designed for beginners and professionals.

Python provides the standard library Tkinter for creating the graphical user interface
for desktop based applications.

Developing desktop based applications with python Tkinter is not a complex task. An
empty Tkinter top-level window can be created by using the following steps.

1. import the Tkinter module.


2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's
computer screen.
Example

1. # !/usr/bin/python3
2. from tkinter import *
3. #creating the application main window.
4. top = Tk()
5. #Entering the event main loop
6. top.mainloop()

Output:

Hello Java Program for Beginners

Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to
build the python GUI applications.

SN Widget Description

1 Button The Button is used to add various kinds of buttons to the python
application.

2 Canvas The canvas widget is used to draw the canvas on the window.

3 Checkbutton The Checkbutton is used to display the CheckButton on the window.

4 Entry The entry widget is used to display the single-line text field to the user. It
is commonly used to accept user values.

5 Frame It can be defined as a container to which, another widget can be added


and organized.

6 Label A label is a text used to display some message or information about the
other widgets.

7 ListBox The ListBox widget is used to display a list of options to the user.
8 Menubutton The Menubutton is used to display the menu items to the user.

9 Menu It is used to add menu items to the user.

10 Message The Message widget is used to display the message-box to the user.

11 Radiobutton The Radiobutton is different from a checkbutton. Here, the user is


provided with various options and the user can select only one option
among them.

12 Scale It is used to provide the slider to the user.

13 Scrollbar It provides the scrollbar to the user so that the user can scroll the window
up and down.

14 Text It is different from Entry because it provides a multi-line text field to the
user so that the user can write the text and edit the text inside it.

14 Toplevel It is used to create a separate window container.

15 Spinbox It is an entry widget used to select from options of values.

16 PanedWindow It is like a container widget that contains horizontal or vertical panes.

17 LabelFrame A LabelFrame is a container widget that acts as the container

18 MessageBox This module is used to display the message-box in the desktop based
applications.

Python Tkinter Geometry


The Tkinter geometry specifies the method by using which, the widgets are
represented on display. The python Tkinter provides the following geometry methods.

1. The pack() method


2. The grid() method
3. The place() method

Let's discuss each one of them in detail.

Python Tkinter pack() method


The pack() widget is used to organize widget in the block. The positions widgets added
to the python application using the pack() method can be controlled by using the
various options specified in the method call.

However, the controls are less and widgets are generally added in the less organized
manner.

The syntax to use the pack() is given below.

syntax

1. widget.pack(options)

A list of possible options that can be passed in pack() is given below.

o expand: If the expand is set to true, the widget expands to fill any space.
o Fill: By default, the fill is set to NONE. However, we can set it to X or Y to
determine whether the widget contains any extra space.
o size: it represents the side of the parent to which the widget is to be placed on
the window.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. redbutton = Button(parent, text = "Red", fg = "red")
5. redbutton.pack( side = LEFT)
6. greenbutton = Button(parent, text = "Black", fg = "black")
7. greenbutton.pack( side = RIGHT )
8. bluebutton = Button(parent, text = "Blue", fg = "blue")
9. bluebutton.pack( side = TOP )
10. blackbutton = Button(parent, text = "Green", fg = "red")
11. blackbutton.pack( side = BOTTOM)
12. parent.mainloop()

Output:
Python Tkinter grid() method
The grid() geometry manager organizes the widgets in the tabular form. We can specify
the rows and columns as the options in the method call. We can also specify the
column span (width) or rowspan(height) of a widget.

This is a more organized way to place the widgets to the python application. The syntax
to use the grid() is given below.

Syntax

1. widget.grid(options)

A list of possible options that can be passed inside the grid() method is given below.

o Column
The column number in which the widget is to be placed. The leftmost column
is represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the
column is expanded.
o ipadx,ipady
It represents the number of pixels to pad the widget inside the widget's border.
o padx,pady
It represents the number of pixels to pad the widget outside the widget's
border.
o row
The row number in which the widget is to be placed. The topmost row is
represented by 0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is
expanded.
o Sticky
If the cell is larger than a widget, then sticky is used to specify the position of
the widget inside the cell. It may be the concatenation of the sticky letters
representing the position of the widget. It may be N, E, W, S, NE, NW, NS, EW,
ES.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. name = Label(parent,text = "Name").grid(row = 0, column = 0)
5. e1 = Entry(parent).grid(row = 0, column = 1)
6. password = Label(parent,text = "Password").grid(row = 1, column = 0)
7. e2 = Entry(parent).grid(row = 1, column = 1)
8. submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
9. parent.mainloop()

Output:

Python Tkinter place() method


The place() geometry manager organizes the widgets to the specific x and y
coordinates.

Syntax

1. widget.place(options)

A list of possible options is given below.

o Anchor: It represents the exact position of the widget within the container. The
default value (direction) is NW (the upper left corner)
o bordermode: The default value of the border type is INSIDE that refers to
ignore the parent's inside the border. The other option is OUTSIDE.
o height, width: It refers to the height and width in pixels.
o relheight, relwidth: It is represented as the float between 0.0 and 1.0 indicating
the fraction of the parent's height and width.
o relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in
the horizontal and vertical direction.
o x, y: It refers to the horizontal and vertical offset in the pixels.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. top = Tk()
4. top.geometry("400x250")
5. name = Label(top, text = "Name").place(x = 30,y = 50)
6. email = Label(top, text = "Email").place(x = 30, y = 90)
7. password = Label(top, text = "Password").place(x = 30, y = 130)
8. e1 = Entry(top).place(x = 80, y = 50)
9. e2 = Entry(top).place(x = 80, y = 90)
10. e3 = Entry(top).place(x = 95, y = 130)
11. top.mainloop()

Output:

Python Tkinter Button


The button widget is used to add various types of buttons to the python application.
Python allows us to configure the look of the button according to our requirements.
Various options can be set or reset depending upon the requirements.
We can also associate a method or function with a button which is called when the
button is pressed.

The syntax to use the button widget is given below.

Syntax

1. W = Button(parent, options)

A list of possible options is given below.

Java Try Catch

SN Option Description

1 activebackground It represents the background of the button when the mouse hover the
button.

2 activeforeground It represents the font color of the button when the mouse hover the
button.

3 Bd It represents the border width in pixels.

4 Bg It represents the background color of the button.

5 Command It is set to the function call which is scheduled when the function is
called.

6 Fg Foreground color of the button.

7 Font The font of the button text.

8 Height The height of the button. The height is represented in the number of
text lines for the textual lines or the number of pixels for the images.

10 Highlightcolor The color of the highlight when the button has the focus.

11 Image It is set to the image displayed on the button.

12 justify It illustrates the way by which the multiple text lines are represented.
It is set to LEFT for left justification, RIGHT for the right justification,
and CENTER for the center.

13 Padx Additional padding to the button in the horizontal direction.

14 pady Additional padding to the button in the vertical direction.


15 Relief It represents the type of the border. It can be SUNKEN, RAISED,
GROOVE, and RIDGE.

17 State This option is set to DISABLED to make the button unresponsive. The
ACTIVE represents the active state of the button.

18 Underline Set this option to make the button text underlined.

19 Width The width of the button. It exists as a number of letters for textual
buttons or pixels for image buttons.

20 Wraplength If the value is set to a positive number, the text lines will be wrapped
to fit within this length.

Example

1. #python application to create a simple button


2.
3. from tkinter import *
4.
5.
6. top = Tk()
7.
8. top.geometry("200x100")
9.
10. b = Button(top,text = "Simple")
11.
12. b.pack()
13.
14. top.mainaloop()

Output:
Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x100")
6.
7. def fun():
8. messagebox.showinfo("Hello", "Red Button clicked")
9.
10.
11. b1 = Button(top,text = "Red",command = fun,activeforeground = "red",active
background = "pink",pady=10)
12.
13. b2 = Button(top, text = "Blue",activeforeground = "blue",activebackground =
"pink",pady=10)
14.
15. b3 = Button(top, text = "Green",activeforeground = "green",activebackground
= "pink",pady = 10)
16.
17. b4 = Button(top, text = "Yellow",activeforeground = "yellow",activebackgroun
d = "pink",pady = 10)
18.
19. b1.pack(side = LEFT)
20.
21. b2.pack(side = RIGHT)
22.
23. b3.pack(side = TOP)
24.
25. b4.pack(side = BOTTOM)
26.
27. top.mainloop()

Output:
Python Tkinter Canvas
The canvas widget is used to add the structured graphics to the python application. It
is used to draw the graph and plots to the python application. The syntax to use the
canvas is given below.

Syntax

1. w = canvas(parent, options)

A list of possible options is given below.

SN Option Description

1 bd The represents the border width. The default width is 2.

2 bg It represents the background color of the canvas.

3 confine It is set to make the canvas unscrollable outside the scroll region.

4 cursor The cursor is used as the arrow, circle, dot, etc. on the canvas.

5 height It represents the size of the canvas in the vertical direction.

6 highlightcolor It represents the highlight color when the widget is focused.

7 relief It represents the type of the border. The possible values are SUNKEN,
RAISED, GROOVE, and RIDGE.

8 scrollregion It represents the coordinates specified as the tuple containing the area
of the canvas.

9 width It represents the width of the canvas.


10 xscrollincrement If it is set to a positive value. The canvas is placed only to the multiple
of this value.

11 xscrollcommand If the canvas is scrollable, this attribute should be the .set() method of
the horizontal scrollbar.

12 yscrollincrement Works like xscrollincrement, but governs vertical movement.

13 yscrollcommand If the canvas is scrollable, this attribute should be the .set() method of
the vertical scrollbar.

Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. #creating a simple canvas
8. c = Canvas(top,bg = "pink",height = "200")
9.
10.
11. c.pack()
12.
13. top.mainloop()

Output:

Example: Creating an arc

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. #creating a simple canvas
8. c = Canvas(top,bg = "pink",height = "200",width = 200)
9.
10. arc = c.create_arc((5,10,150,200),start = 0,extent = 150, fill= "white")
11.
12. c.pack()
13.
14. top.mainloop()

Output:

HTML Tutorial

Python Tkinter Checkbutton


The Checkbutton is used to track the user's choices provided to the application. In
other words, we can say that Checkbutton is used to implement the on/off selections.

The Checkbutton can contain the text or images. The Checkbutton is mostly used to
provide many choices to the user among which, the user needs to choose the one. It
generally implements many of many selections.

The syntax to use the checkbutton is given below.

Syntax

1. w = checkbutton(master, options)

A list of possible options is given below.

History of Java
SN Option Description

1 activebackground It represents the background color when the checkbutton is under the
cursor.

2 activeforeground It represents the foreground color of the checkbutton when the


checkbutton is under the cursor.

3 bg The background color of the button.

4 bitmap It displays an image (monochrome) on the button.

5 bd The size of the border around the corner.

6 command It is associated with a function to be called when the state of the


checkbutton is changed.

7 cursor The mouse pointer will be changed to the cursor name when it is over
the checkbutton.

8 disableforeground It is the color which is used to represent the text of a disabled


checkbutton.

9 font It represents the font of the checkbutton.

10 fg The foreground color (text color) of the checkbutton.

11 height It represents the height of the checkbutton (number of lines). The


default height is 1.

12 highlightcolor The color of the focus highlight when the checkbutton is under focus.

13 image The image used to represent the checkbutton.

14 justify This specifies the justification of the text if the text contains multiple
lines.

15 offvalue The associated control variable is set to 0 by default if the button is


unchecked. We can change the state of an unchecked variable to
some other one.

16 onvalue The associated control variable is set to 1 by default if the button is


checked. We can change the state of the checked variable to some
other one.
17 padx The horizontal padding of the checkbutton

18 pady The vertical padding of the checkbutton.

19 relief The type of the border of the checkbutton. By default, it is set to FLAT.

20 selectcolor The color of the checkbutton when it is set. By default, it is red.

21 selectimage The image is shown on the checkbutton when it is set.

22 state It represents the state of the checkbutton. By default, it is set to


normal. We can change it to DISABLED to make the checkbutton
unresponsive. The state of the checkbutton is ACTIVE when it is under
focus.

24 underline It represents the index of the character in the text which is to be


underlined. The indexing starts with zero in the text.

25 variable It represents the associated variable that tracks the state of the
checkbutton.

26 width It represents the width of the checkbutton. It is represented in the


number of characters that are represented in the form of texts.

27 wraplength If this option is set to an integer number, the text will be broken into
the number of pieces.

Methods
The methods that can be called with the Checkbuttons are described in the following
table.

SN Method Description

1 deselect() It is called to turn off the checkbutton.

2 flash() The checkbutton is flashed between the active and normal colors.

3 invoke() This will invoke the method associated with the checkbutton.

4 select() It is called to turn on the checkbutton.

5 toggle() It is used to toggle between the different Checkbuttons.


Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. checkvar1 = IntVar()
8.
9. checkvar2 = IntVar()
10.
11. checkvar3 = IntVar()
12.
13. chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offv
alue = 0, height = 2, width = 10)
14.
15. chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1,
offvalue = 0, height = 2, width = 10)
16.
17. chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1,
offvalue = 0, height = 2, width = 10)
18.
19. chkbtn1.pack()
20.
21. chkbtn2.pack()
22.
23. chkbtn3.pack()
24.
25. top.mainloop()

Output:
Python Tkinter Entry
The Entry widget is used to provde the single line text-box to the user to accept a value
from the user. We can use the Entry widget to accept the text strings from the user. It
can only be used for one line of text from the user. For multiple lines of text, we must
use the text widget.

The syntax to use the Entry widget is given below.

Syntax

1. w = Entry (parent, options)

A list of possible options is given below.

SN Option Description

1 bg The background color of the widget.

2 bd The border width of the widget in pixels.

3 cursor The mouse pointer will be changed to the cursor type set to the
arrow, dot, etc.

4 exportselection The text written inside the entry box will be automatically copied
to the clipboard by default. We can set the exportselection to 0 to
not copy this.

5 fg It represents the color of the text.

6 font It represents the font type of the text.

7 highlightbackground It represents the color to display in the traversal highlight region


when the widget does not have the input focus.

8 highlightcolor It represents the color to use for the traversal highlight rectangle
that is drawn around the widget when it has the input focus.
9 highlightthickness It represents a non-negative value indicating the width of the
highlight rectangle to draw around the outside of the widget when
it has the input focus.

10 insertbackground It represents the color to use as background in the area covered by


the insertion cursor. This color will normally override either the
normal background for the widget.

11 insertborderwidth It represents a non-negative value indicating the width of the 3-D


border to draw around the insertion cursor. The value may have
any of the forms acceptable to Tk_GetPixels.

12 insertofftime It represents a non-negative integer value indicating the number


of milliseconds the insertion cursor should remain "off" in each
blink cycle. If this option is zero, then the cursor doesn't blink: it is
on all the time.

13 insertontime Specifies a non-negative integer value indicating the number of


milliseconds the insertion cursor should remain "on" in each blink
cycle.

14 insertwidth It represents the value indicating the total width of the insertion
cursor. The value may have any of the forms acceptable to
Tk_GetPixels.

15 justify It specifies how the text is organized if the text contains multiple
lines.

16 relief It specifies the type of the border. Its default value is FLAT.

17 selectbackground The background color of the selected text.

18 selectborderwidth The width of the border to display around the selected task.

19 selectforeground The font color of the selected task.

20 show It is used to show the entry text of some other type instead of the
string. For example, the password is typed using stars (*).

21 textvariable It is set to the instance of the StringVar to retrieve the text from the
entry.

22 width The width of the displayed text or image.


23 xscrollcommand The entry widget can be linked to the horizontal scrollbar if we want
the user to enter more text then the actual width of the widget.

Example

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("400x250")
8.
9. name = Label(top, text = "Name").place(x = 30,y = 50)
10.
11. email = Label(top, text = "Email").place(x = 30, y = 90)
12.
13. password = Label(top, text = "Password").place(x = 30, y = 130)
14.
15. sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activefore
ground = "blue").place(x = 30, y = 170)
16.
17. e1 = Entry(top).place(x = 80, y = 50)
18.
19.
20. e2 = Entry(top).place(x = 80, y = 90)
21.
22.
23. e3 = Entry(top).place(x = 95, y = 130)
24.
25. top.mainloop()

Output:

C++ vs Java
Entry widget methods
Python provides various methods to configure the data written inside the widget.
There are the following methods provided by the Entry widget.

SN Method Description

1 delete(first, last = none) It is used to delete the specified characters inside the widget.

2 get() It is used to get the text written inside the widget.

3 icursor(index) It is used to change the insertion cursor position. We can


specify the index of the character before which, the cursor to
be placed.

4 index(index) It is used to place the cursor to the left of the character written
at the specified index.

5 insert(index,s) It is used to insert the specified string before the character


placed at the specified index.

6 select_adjust(index) It includes the selection of the character present at the


specified index.

7 select_clear() It clears the selection if some selection has been done.

8 select_form(index) It sets the anchor index position to the character specified by


the index.

9 select_present() It returns true if some text in the Entry is selected otherwise


returns false.

10 select_range(start,end) It selects the characters to exist between the specified range.

11 select_to(index) It selects all the characters from the beginning to the specified
index.
12 xview(index) It is used to link the entry widget to a horizontal scrollbar.

13 xview_scroll(number,what) It is used to make the entry scrollable horizontally.

Example: A simple calculator

1. import tkinter as tk
2. from functools import partial
3.
4.
5. def call_result(label_result, n1, n2):
6. num1 = (n1.get())
7. num2 = (n2.get())
8. result = int(num1)+int(num2)
9. label_result.config(text="Result = %d" % result)
10. return
11.
12. root = tk.Tk()
13. root.geometry('400x200+100+200')
14.
15. root.title('Calculator')
16.
17. number1 = tk.StringVar()
18. number2 = tk.StringVar()
19.
20. labelNum1 = tk.Label(root, text="A").grid(row=1, column=0)
21.
22. labelNum2 = tk.Label(root, text="B").grid(row=2, column=0)
23.
24. labelResult = tk.Label(root)
25.
26. labelResult.grid(row=7, column=2)
27.
28. entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)
29.
30. entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)
31.
32. call_result = partial(call_result, labelResult, number1, number2)
33.
34. buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=
3, column=0)
35.
36. root.mainloop()

Output:

Python Tkinter Frame


Python Tkinter Frame widget is used to organize the group of widgets. It acts like a
container which can be used to hold the other widgets. The rectangular areas of the
screen are used to organize the widgets to the python application.

The syntax to use the Frame widget is given below.

Syntax

1. w = Frame(parent, options)

A list of possible options is given below.

SN Option Description

1 bd It represents the border width.

2 bg The background color of the widget.

3 cursor The mouse pointer is changed to the cursor type set to different
values like an arrow, dot, etc.

4 height The height of the frame.


5 highlightbackground The color of the background color when it is under focus.

6 highlightcolor The text color when the widget is under focus.

7 highlightthickness It specifies the thickness around the border when the widget is
under the focus.

8 relief It specifies the type of the border. The default value if FLAT.

9 width It represents the width of the widget.

Example

1. from tkinter import *


2.
3. top = Tk()
4. top.geometry("140x100")
5. frame = Frame(top)
6. frame.pack()
7.
8. leftframe = Frame(top)
9. leftframe.pack(side = LEFT)
10.
11. rightframe = Frame(top)
12. rightframe.pack(side = RIGHT)
13.
14. btn1 = Button(frame, text="Submit", fg="red",activebackground = "red")
15. btn1.pack(side = LEFT)
16.
17. btn2 = Button(frame, text="Remove", fg="brown", activebackground = "brow
n")
18. btn2.pack(side = RIGHT)
19.
20. btn3 = Button(rightframe, text="Add", fg="blue", activebackground = "blue")
21. btn3.pack(side = LEFT)
22.
23. btn4 = Button(leftframe, text="Modify", fg="black", activebackground = "whit
e")
24. btn4.pack(side = RIGHT)
25.
26. top.mainloop()

Output:

How to find Nth Highest Salary in SQL

Python Tkinter Label


The Label is used to specify the container box where we can place the text or images.
This widget is used to provide the message to the user about other widgets used in
the python application.

There are the various options which can be specified to configure the text or the part
of the text shown in the Label.

The syntax to use the Label is given below.

Syntax

1. w = Label (master, options)

A list of possible options is given below.

C++ vs Java

SN Option Description

1 anchor It specifies the exact position of the text within the size provided to the
widget. The default value is CENTER, which is used to center the text within
the specified space.

2 bg The background color displayed behind the widget.

3 bitmap It is used to set the bitmap to the graphical object specified so that, the label
can represent the graphics instead of text.
4 bd It represents the width of the border. The default is 2 pixels.

5 cursor The mouse pointer will be changed to the type of the cursor specified, i.e.,
arrow, dot, etc.

6 font The font type of the text written inside the widget.

7 fg The foreground color of the text written inside the widget.

8 height The height of the widget.

9 image The image that is to be shown as the label.

10 justify It is used to represent the orientation of the text if the text contains multiple
lines. It can be set to LEFT for left justification, RIGHT for right justification,
and CENTER for center justification.

11 padx The horizontal padding of the text. The default value is 1.

12 pady The vertical padding of the text. The default value is 1.

13 relief The type of the border. The default value is FLAT.

14 text This is set to the string variable which may contain one or more line of text.

15 textvariable The text written inside the widget is set to the control variable StringVar so
that it can be accessed and changed accordingly.

16 underline We can display a line under the specified letter of the text. Set this option to
the number of the letter under which the line will be displayed.

17 width The width of the widget. It is specified as the number of characters.

18 wraplength Instead of having only one line as the label text, we can break it to the
number of lines where each line has the number of characters specified to
this option.

Example 1

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("400x250")
8.
9. #creating label
10. uname = Label(top, text = "Username").place(x = 30,y = 50)
11.
12. #creating label
13. password = Label(top, text = "Password").place(x = 30, y = 90)
14.
15.
16. sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activefore
ground = "blue").place(x = 30, y = 120)
17.
18. e1 = Entry(top,width = 20).place(x = 100, y = 50)
19.
20.
21. e2 = Entry(top, width = 20).place(x = 100, y = 90)
22.
23.
24. top.mainloop()

Output:

Python Tkinter Listbox


The Listbox widget is used to display the list items to the user. We can place only text
items in the Listbox and all text items contain the same font and color.

The user can choose one or more items from the list depending upon the
configuration.

The syntax to use the Listbox is given below.

1. w = Listbox(parent, options)
A list of possible options is given below.

HTML Tutorial

SN Option Description

1 bg The background color of the widget.

2 bd It represents the size of the border. Default value is 2 pixel.

3 cursor The mouse pointer will look like the cursor type like dot, arrow, etc.

4 font The font type of the Listbox items.

5 fg The color of the text.

6 height It represents the count of the lines shown in the Listbox. The default
value is 10.

7 highlightcolor The color of the Listbox items when the widget is under focus.

8 highlightthickness The thickness of the highlight.

9 relief The type of the border. The default is SUNKEN.

10 selectbackground The background color that is used to display the selected text.

11 selectmode It is used to determine the number of items that can be selected from
the list. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.

12 width It represents the width of the widget in characters.

13 xscrollcommand It is used to let the user scroll the Listbox horizontally.

14 yscrollcommand It is used to let the user scroll the Listbox vertically.

Methods
There are the following methods associated with the Listbox.

SN Method Description

1 activate(index) It is used to select the lines at the specified index.


2 curselection() It returns a tuple containing the line numbers of the selected
element or elements, counting from 0. If nothing is selected,
returns an empty tuple.

3 delete(first, last = None) It is used to delete the lines which exist in the given range.

4 get(first, last = None) It is used to get the list items that exist in the given range.

5 index(i) It is used to place the line with the specified index at the top of
the widget.

6 insert(index, *elements) It is used to insert the new lines with the specified number of
elements before the specified index.

7 nearest(y) It returns the index of the nearest line to the y coordinate of


the Listbox widget.

8 see(index) It is used to adjust the position of the listbox to make the lines
specified by the index visible.

9 size() It returns the number of lines that are present in the Listbox
widget.

10 xview() This is used to make the widget horizontally scrollable.

11 xview_moveto(fraction) It is used to make the listbox horizontally scrollable by the


fraction of width of the longest line present in the listbox.

12 xview_scroll(number, It is used to make the listbox horizontally scrollable by the


what) number of characters specified.

13 yview() It allows the Listbox to be vertically scrollable.

14 yview_moveto(fraction) It is used to make the listbox vertically scrollable by the fraction


of width of the longest line present in the listbox.

15 yview_scroll (number, It is used to make the listbox vertically scrollable by the number
what) of characters specified.

Example 1

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("200x250")
8.
9. lbl = Label(top,text = "A list of favourite countries...")
10.
11. listbox = Listbox(top)
12.
13. listbox.insert(1,"India")
14.
15. listbox.insert(2, "USA")
16.
17. listbox.insert(3, "Japan")
18.
19. listbox.insert(4, "Austrelia")
20.
21. lbl.pack()
22. listbox.pack()
23.
24. top.mainloop()

Output:

Example 2: Deleting the active items from the list

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("200x250")
8.
9. lbl = Label(top,text = "A list of favourite countries...")
10.
11. listbox = Listbox(top)
12.
13. listbox.insert(1,"India")
14.
15. listbox.insert(2, "USA")
16.
17. listbox.insert(3, "Japan")
18.
19. listbox.insert(4, "Austrelia")
20.
21. #this button will delete the selected item from the list
22.
23. btn = Button(top, text = "delete", command = lambda listbox=listbox: listbox.
delete(ANCHOR))
24.
25. lbl.pack()
26.
27.
28. listbox.pack()
29.
30. btn.pack()
31. top.mainloop()

Output:
After pressing the delete button.

Python Tkinter Menubutton


The Menubutton widget can be defined as the drop-down menu that is shown to the
user all the time. It is used to provide the user a option to select the appropriate choice
exist within the application.

The Menubutton is used to implement various types of menus in the python


application. A Menu is associated with the Menubutton that can display the choices of
the Menubutton when clicked by the user.

The syntax to use the python tkinter Menubutton is given below.

Syntax

1. w = Menubutton(Top, options)

A list of various options is given below.

Features of Java - Javatpoint

SN Option Description

1 activebackground The background color of the widget when the widget is under focus.

2 activeforeground The font color of the widget text when the widget is under focus.

3 anchor It specifies the exact position of the widget content when the widget
is assigned more space than needed.

4 bg It specifies the background color of the widget.

5 bitmap It is set to the graphical content which is to be displayed to the


widget.

6 bd It represents the size of the border. The default value is 2 pixels.


7 cursor The mouse pointer will be changed to the cursor type specified when
the widget is under the focus. The possible value of the cursor type
is arrow, or dot etc.

8 direction It direction can be specified so that menu can be displayed to the


specified direction of the button. Use LEFT, RIGHT, or ABOVE to
place the widget accordingly.

9 disabledforeground The text color of the widget when the widget is disabled.

10 fg The normal foreground color of the widget.

11 height The vertical dimension of the Menubutton. It is specified as the


number of lines.

12 highlightcolor The highlight color shown to the widget under focus.

13 image The image displayed on the widget.

14 justify This specified the exact position of the text under the widget when
the text is unable to fill the width of the widget. We can use the LEFT
for the left justification, RIGHT for the right justification, CENTER for
the centre justification.

15 menu It represents the menu specified with the Menubutton.

16 padx The horizontal padding of the widget.

17 pady The vertical padding of the widget.

18 relief This option specifies the type of the border. The default value is
RAISED.

19 state The normal state of the Mousebutton is enabled. We can set it to


DISABLED to make it unresponsive.

20 text The text shown with the widget.

21 textvariable We can set the control variable of string type to the text variable so
that we can control the text of the widget at runtime.

22 underline The text of the widget is not underlined by default but we can set
this option to make the text of the widget underlined.

23 width It represents the width of the widget in characters. The default value
is 20.
24 wraplength We can break the text of the widget in the number of lines so that
the text contains the number of lines not greater than the specified
value.

Example

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("200x250")
8.
9. menubutton = Menubutton(top, text = "Language", relief = FLAT)
10.
11. menubutton.grid()
12.
13. menubutton.menu = Menu(menubutton)
14.
15. menubutton["menu"]=menubutton.menu
16.
17. menubutton.menu.add_checkbutton(label = "Hindi", variable=IntVar())
18.
19. menubutton.menu.add_checkbutton(label = "English", variable = IntVar())
20.
21. menubutton.pack()
22.
23. top.mainloop()

Output:

Python Tkinter Menu


The Menu widget is used to create various types of menus (top level, pull down, and
pop up) in the python application.

The top-level menus are the one which is displayed just under the title bar of the parent
window. We need to create a new instance of the Menu widget and add various
commands to it by using the add() method.

The syntax to use the Menu widget is given below.

Syntax

1. w = Menu(top, options)

A list of possible options is given below.

11.2M
269
C++ vs Java

SN Option Description

1 activebackground The background color of the widget when the widget is under the
focus.

2 activeborderwidth The width of the border of the widget when it is under the mouse.
The default is 1 pixel.

3 activeforeground The font color of the widget when the widget has the focus.

4 bg The background color of the widget.

5 bd The border width of the widget.

6 cursor The mouse pointer is changed to the cursor type when it hovers the
widget. The cursor type can be set to arrow or dot.

7 disabledforeground The font color of the widget when it is disabled.

8 font The font type of the text of the widget.

9 fg The foreground color of the widget.

10 postcommand The postcommand can be set to any of the function which is called
when the mourse hovers the menu.

11 relief The type of the border of the widget. The default type is RAISED.
12 image It is used to display an image on the menu.

13 selectcolor The color used to display the checkbutton or radiobutton when they
are selected.

14 tearoff By default, the choices in the menu start taking place from position
1. If we set the tearoff = 1, then it will start taking place from 0th
position.

15 title Set this option to the title of the window if you want to change the
title of the window.

Methods
The Menu widget contains the following methods.

SN Option Description

1 add_command(options) It is used to add the Menu items to the menu.

2 add_radiobutton(options) This method adds the radiobutton to the menu.

3 add_checkbutton(options) This method is used to add the checkbuttons to the menu.

4 add_cascade(options) It is used to create a hierarchical menu to the parent menu


by associating the given menu to the parent menu.

5 add_seperator() It is used to add the seperator line to the menu.

6 add(type, options) It is used to add the specific menu item to the menu.

7 delete(startindex, It is used to delete the menu items exist in the specified


endindex) range.

8 entryconfig(index, options) It is used to configure a menu item identified by the given


index.

9 index(item) It is used to get the index of the specified menu item.

10 insert_seperator(index) It is used to insert a seperator at the specified index.

11 invoke(index) It is used to invoke the associated with the choice given at


the specified index.
12 type(index) It is used to get the type of the choice specified by the index.

Creating a top level menu


A top-level menu can be created by instantiating the Menu widget and adding the
menu items to the menu.

Example 1

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. def hello():
8. print("hello!")
9.
10. # create a toplevel menu
11. menubar = Menu(root)
12. menubar.add_command(label="Hello!", command=hello)
13. menubar.add_command(label="Quit!", command=top.quit)
14.
15. # display the menu
16. top.config(menu=menubar)
17.
18. top.mainloop()

Output:

Clicking the hello Menubutton will print the hello on the console while clicking the
Quit Menubutton will make an exit from the python application.
Example 2

1. from tkinter import Toplevel, Button, Tk, Menu


2.
3. top = Tk()
4. menubar = Menu(top)
5. file = Menu(menubar, tearoff=0)
6. file.add_command(label="New")
7. file.add_command(label="Open")
8. file.add_command(label="Save")
9. file.add_command(label="Save as...")
10. file.add_command(label="Close")
11.
12. file.add_separator()
13.
14. file.add_command(label="Exit", command=top.quit)
15.
16. menubar.add_cascade(label="File", menu=file)
17. edit = Menu(menubar, tearoff=0)
18. edit.add_command(label="Undo")
19.
20. edit.add_separator()
21.
22. edit.add_command(label="Cut")
23. edit.add_command(label="Copy")
24. edit.add_command(label="Paste")
25. edit.add_command(label="Delete")
26. edit.add_command(label="Select All")
27.
28. menubar.add_cascade(label="Edit", menu=edit)
29. help = Menu(menubar, tearoff=0)
30. help.add_command(label="About")
31. menubar.add_cascade(label="Help", menu=help)
32.
33. top.config(menu=menubar)
34. top.mainloop()
Python Tkinter Message
The Message widget is used to show the message to the user regarding the behaviour
of the python application. The message widget shows the text messages to the user
which can not be edited.

The message text contains more than one line. However, the message can only be
shown in the single font.

The syntax to use the Message widget is given below.

Syntax

1. w = Message(parent, options)

A list of possible options is given below.

How to find Nth Highest Salary in SQL

SN Option Description

1 anchor It is used to decide the exact position of the text within the space provided
to the widget if the widget contains more space than the need of the text.
The default is CENTER.

2 bg The background color of the widget.

3 bitmap It is used to display the graphics on the widget. It can be set to any graphical
or image object.

4 bd It represents the size of the border in the pixel. The default size is 2 pixel.

5 cursor The mouse pointer is changed to the specified cursor type. The cursor type
can be an arrow, dot, etc.

6 font The font type of the widget text.

7 fg The font color of the widget text.

8 height The vertical dimension of the message.

9 image We can set this option to a static image to show that onto the widget.
10 justify This option is used to specify the alignment of multiple line of code with
respect to each other. The possible values can be LEFT (left alignment),
CENTER (default), and RIGHT (right alignment).

11 padx The horizontal padding of the widget.

12 pady The vertical padding of the widget.

13 relief It represents the type of the border. The default type is FLAT.

14 text We can set this option to the string so that the widget can represent the
specified text.

15 textvariable This is used to control the text represented by the widget. The textvariable
can be set to the text that is shown in the widget.

16 underline The default value of this option is -1 that represents no underline. We can
set this option to an existing number to specify that nth letter of the string
will be underlined.

17 width It specifies the horizontal dimension of the widget in the number of


characters (not pixel).

18 wraplength We can wrap the text to the number of lines by setting this option to the
desired number so that each line contains only that number of characters.

Example

1. from tkinter import *


2.
3. top = Tk()
4. top.geometry("100x100")
5. var = StringVar()
6. msg = Message( top, text = "Welcome to Javatpoint")
7.
8. msg.pack()
9. top.mainloop()

Output:
Python Tkinter Message
The Message widget is used to show the message to the user regarding the behaviour
of the python application. The message widget shows the text messages to the user
which can not be edited.

The message text contains more than one line. However, the message can only be
shown in the single font.

The syntax to use the Message widget is given below.

Syntax

1. w = Message(parent, options)

A list of possible options is given below.

10.4M
242
How to find Nth Highest Salary in SQL

SN Option Description

1 anchor It is used to decide the exact position of the text within the space provided
to the widget if the widget contains more space than the need of the text.
The default is CENTER.

2 bg The background color of the widget.

3 bitmap It is used to display the graphics on the widget. It can be set to any graphical
or image object.

4 bd It represents the size of the border in the pixel. The default size is 2 pixel.

5 cursor The mouse pointer is changed to the specified cursor type. The cursor type
can be an arrow, dot, etc.
6 font The font type of the widget text.

7 fg The font color of the widget text.

8 height The vertical dimension of the message.

9 image We can set this option to a static image to show that onto the widget.

10 justify This option is used to specify the alignment of multiple line of code with
respect to each other. The possible values can be LEFT (left alignment),
CENTER (default), and RIGHT (right alignment).

11 padx The horizontal padding of the widget.

12 pady The vertical padding of the widget.

13 relief It represents the type of the border. The default type is FLAT.

14 text We can set this option to the string so that the widget can represent the
specified text.

15 textvariable This is used to control the text represented by the widget. The textvariable
can be set to the text that is shown in the widget.

16 underline The default value of this option is -1 that represents no underline. We can
set this option to an existing number to specify that nth letter of the string
will be underlined.

17 width It specifies the horizontal dimension of the widget in the number of


characters (not pixel).

18 wraplength We can wrap the text to the number of lines by setting this option to the
desired number so that each line contains only that number of characters.

Example

1. from tkinter import *


2.
3. top = Tk()
4. top.geometry("100x100")
5. var = StringVar()
6. msg = Message( top, text = "Welcome to Javatpoint")
7.
8. msg.pack()
9. top.mainloop()

Output:

Python Tkinter Radiobutton


The Radiobutton widget is used to implement one-of-many selection in the python
application. It shows multiple choices to the user out of which, the user can select only
one out of them. We can associate different methods with each of the radiobutton.

We can display the multiple line text or images on the radiobuttons. To keep track the
user's selection the radiobutton, it is associated with a single variable. Each button
displays a single value for that particular variable.

The syntax to use the Radiobutton is given below.

Syntax

1. w = Radiobutton(top, options)

SN Option Description

1 activebackground The background color of the widget when it has the focus.

2 activeforeground The font color of the widget text when it has the focus.

3 anchor It represents the exact position of the text within the widget if the
widget contains more space than the requirement of the text. The
default value is CENTER.
4 bg The background color of the widget.

5 bitmap It is used to display the graphics on the widget. It can be set to any
graphical or image object.

6 borderwidth It represents the size of the border.

7 command This option is set to the procedure which must be called every-time
when the state of the radiobutton is changed.

8 cursor The mouse pointer is changed to the specified cursor type. It can
be set to the arrow, dot, etc.

9 font It represents the font type of the widget text.

10 fg The normal foreground color of the widget text.

11 height The vertical dimension of the widget. It is specified as the number


of lines (not pixel).

12 highlightcolor It represents the color of the focus highlight when the widget has
the focus.

13 highlightbackground The color of the focus highlight when the widget is not having the
focus.

14 image It can be set to an image object if we want to display an image on


the radiobutton instead the text.

15 justify It represents the justification of the multi-line text. It can be set to


CENTER(default), LEFT, or RIGHT.

16 padx The horizontal padding of the widget.

17 pady The vertical padding of the widget.

18 relief The type of the border. The default value is FLAT.

19 selectcolor The color of the radio button when it is selected.

20 selectimage The image to be displayed on the radiobutton when it is selected.

21 state It represents the state of the radio button. The default state of the
Radiobutton is NORMAL. However, we can set this to DISABLED to
make the radiobutton unresponsive.
22 text The text to be displayed on the radiobutton.

23 textvariable It is of String type that represents the text displayed by the widget.

24 underline The default value of this option is -1, however, we can set this
option to the number of character which is to be underlined.

25 value The value of each radiobutton is assigned to the control variable


when it is turned on by the user.

26 variable It is the control variable which is used to keep track of the user's
choices. It is shared among all the radiobuttons.

27 width The horizontal dimension of the widget. It is represented as the


number of characters.

28 wraplength We can wrap the text to the number of lines by setting this option
to the desired number so that each line contains only that number
of characters.

Methods
The radiobutton widget provides the following methods.

7.7M
132
Triggers in SQL (Hindi)

SN Method Description

1 deselect() It is used to turn of the radiobutton.

2 flash() It is used to flash the radiobutton between its active and normal colors few
times.

3 invoke() It is used to call any procedure associated when the state of a Radiobutton is
changed.

4 select() It is used to select the radiobutton.

Example
1. from tkinter import *
2.
3. def selection():
4. selection = "You selected the option " + str(radio.get())
5. label.config(text = selection)
6.
7. top = Tk()
8. top.geometry("300x150")
9. radio = IntVar()
10. lbl = Label(text = "Favourite programming language:")
11. lbl.pack()
12. R1 = Radiobutton(top, text="C", variable=radio, value=1,
13. command=selection)
14. R1.pack( anchor = W )
15.
16. R2 = Radiobutton(top, text="C++", variable=radio, value=2,
17. command=selection)
18. R2.pack( anchor = W )
19.
20. R3 = Radiobutton(top, text="Java", variable=radio, value=3,
21. command=selection)
22. R3.pack( anchor = W)
23.
24. label = Label(top)
25. label.pack()
26. top.mainloop()

Output:
Python Tkinter Scale
The Scale widget is used to implement the graphical slider to the python application
so that the user can slide through the range of values shown on the slider and select
the one among them.

We can control the minimum and maximum values along with the resolution of the
scale. It provides an alternative to the Entry widget when the user is forced to select
only one value from the given range of values.

The syntax to use the Scale widget is given below.

Syntax

1. w = Scale(top, options)

A list of possible options is given below.

Darwin Carpet Pythons on the Balcony

SN Option Description

1 activebackground The background color of the widget when it has the focus.

2 bg The background color of the widget.

3 bd The border size of the widget. The default is 2 pixel.

4 command It is set to the procedure which is called each time when we move
the slider. If the slider is moved rapidly, the callback is done when
it settles.

5 cursor The mouse pointer is changed to the cursor type assigned to this
option. It can be an arrow, dot, etc.

6 digits If the control variable used to control the scale data is of string
type, this option is used to specify the number of digits when the
numeric scale is converted to a string.

7 font The font type of the widget text.

8 fg The foreground color of the text.

9 from_ It is used to represent one end of the widget range.


10 highlightbackground The highlight color when the widget doesn't have the focus.

11 highlighcolor The highlight color when the widget has the focus.

12 label This can be set to some text which can be shown as a label with the
scale. It is shown in the top left corner if the scale is horizontal or
the top right corner if the scale is vertical.

13 length It represents the length of the widget. It represents the X dimension


if the scale is horizontal or y dimension if the scale is vertical.

14 orient It can be set to horizontal or vertical depending upon the type of


the scale.

15 relief It represents the type of the border. The default is FLAT.

16 repeatdelay This option tells the duration up to which the button is to be


pressed before the slider starts moving in that direction repeatedly.
The default is 300 ms.

17 resolution It is set to the smallest change which is to be made to the scale


value.

18 showvalue The value of the scale is shown in the text form by default. We can
set this option to 0 to suppress the label.

19 sliderlength It represents the length of the slider window along the length of
the scale. The default is 30 pixels. However, we can change it to the
appropriate value.

20 state The scale widget is active by default. We can set this to DISABLED
to make it unresponsive.

21 takefocus The focus cycles through the scale widgets by default. We can set
this option to 0 if we don't want this to happen.

22 tickinterval The scale values are displayed on the multiple of the specified tick
interval. The default value of the tickinterval is 0.

23 to It represents a float or integer value that specifies the other end of


the range represented by the scale.

24 troughcolor It represents the color of the through.

25 variable It represents the control variable for the scale.


26 width It represents the width of the through part of the widget.

Methods

SN Method Description

1 get() It is used to get the current value of the scale.

2 set(value) It is used to set the value of the scale.

Example

1. from tkinter import *


2.
3. def select():
4. sel = "Value = " + str(v.get())
5. label.config(text = sel)
6.
7. top = Tk()
8. top.geometry("200x100")
9. v = DoubleVar()
10. scale = Scale( top, variable = v, from_ = 1, to = 50, orient = HORIZONTAL)
11. scale.pack(anchor=CENTER)
12.
13. btn = Button(top, text="Value", command=select)
14. btn.pack(anchor=CENTER)
15.
16. label = Label(top)
17. label.pack()
18.
19. top.mainloop()

Output:
Python Tkinter Scrollbar
The scrollbar widget is used to scroll down the content of the other widgets like listbox,
text, and canvas. However, we can also create the horizontal scrollbars to the Entry
widget.

The syntax to use the Scrollbar widget is given below.

Syntax

1. w = Scrollbar(top, options)

A list of possible options is given below.

SN Option Description

1 activebackground The background color of the widget when it has the focus.

2 bg The background color of the widget.

3 bd The border width of the widget.

4 command It can be set to the procedure associated with the list which can be
called each time when the scrollbar is moved.

5 cursor The mouse pointer is changed to the cursor type set to this option
which can be an arrow, dot, etc.

6 elementborderwidth It represents the border width around the arrow heads and slider.
The default value is -1.

7 Highlightbackground The focus highlighcolor when the widget doesn't have the focus.

8 highlighcolor The focus highlighcolor when the widget has the focus.

9 highlightthickness It represents the thickness of the focus highlight.

10 jump It is used to control the behavior of the scroll jump. If it set to 1,


then the callback is called when the user releases the mouse
button.

11 orient It can be set to HORIZONTAL or VERTICAL depending upon the


orientation of the scrollbar.
12 repeatdelay This option tells the duration up to which the button is to be
pressed before the slider starts moving in that direction repeatedly.
The default is 300 ms.

13 repeatinterval The default value of the repeat interval is 100.

14 takefocus We can tab the focus through this widget by default. We can set
this option to 0 if we don't want this behavior.

15 troughcolor It represents the color of the trough.

16 width It represents the width of the scrollbar.

Methods
The widget provides the following methods.

OOPs Concepts in Java

SN Method Description

1 get() It returns the two numbers a and b which represents the current position of
the scrollbar.

2 set(first, It is used to connect the scrollbar to the other widget w. The yscrollcommand
last) or xscrollcommand of the other widget to this method.

Example

1. from tkinter import *


2.
3. top = Tk()
4. sb = Scrollbar(top)
5. sb.pack(side = RIGHT, fill = Y)
6.
7. mylist = Listbox(top, yscrollcommand = sb.set )
8.
9. for line in range(30):
10. mylist.insert(END, "Number " + str(line))
11.
12. mylist.pack( side = LEFT )
13. sb.config( command = mylist.yview )
14.
15. mainloop()

Output:

Python Tkinter Spinbox


The Spinbox widget is an alternative to the Entry widget. It provides the range of values
to the user, out of which, the user can select the one.

It is used in the case where a user is given some fixed number of values to choose
from.

We can use various options with the Spinbox to decorate the widget. The syntax to
use the Spinbox is given below.

Syntax

1. w = Spinbox(top, options)

A list of possible options is given below.

SN Option Description

1 activebackground The background color of the widget when it has the focus.

2 bg The background color of the widget.

3 bd The border width of the widget.

4 command The associated callback with the widget which is called each time
the state of the widget is called.

5 cursor The mouse pointer is changed to the cursor type assigned to this
option.
6 disabledbackground The background color of the widget when it is disabled.

7 disabledforeground The foreground color of the widget when it is disabled.

8 fg The normal foreground color of the widget.

9 font The font type of the widget content.

10 format This option is used for the format string. It has no default value.

11 from_ It is used to show the starting range of the widget.

12 justify It is used to specify the justification of the multi-line widget content.


The default is LEFT.

13 relief It is used to specify the type of the border. The default is SUNKEN.

14 repeatdelay This option is used to control the button auto repeat. The value is
given in milliseconds.

15 repeatinterval It is similar to repeatdelay. The value is given in milliseconds.

16 state It represents the state of the widget. The default is NORMAL. The
possible values are NORMAL, DISABLED, or "readonly".

17 textvariable It is like a control variable which is used to control the behaviour of


the widget text.

18 to It specify the maximum limit of the widget value. The other is


specified by the from_ option.

19 validate This option controls how the widget value is validated.

20 validatecommand It is associated to the function callback which is used for the


validation of the widget content.

21 values It represents the tuple containing the values for this widget.

22 vcmd It is same as validation command.

23 width It represents the width of the widget.

24 wrap This option wraps up the up and down button the Spinbox.

25 xscrollcommand This options is set to the set() method of scrollbar to make this
widget horizontally scrollable.
Methods
There are the following methods associated with the widget.

SN Option Description

1 delete(startindex, This method is used to delete the characters present at the


endindex) specified range.

2 get(startindex, endindex) It is used to get the characters present in the specified range.

3 identify(x, y) It is used to identify the widget's element within the specified


range.

4 index(index) It is used to get the absolute value of the given index.

5 insert(index, string) This method is used to insert the string at the specified index.

6 invoke(element) It is used to invoke the callback associated with the widget.

Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. spin = Spinbox(top, from_= 0, to = 25)
8.
9. spin.pack()
10.
11. top.mainloop()

Output:
Tkinter PanedWindow
The PanedWindow widget acts like a Container widget which contains one or more
child widgets (panes) arranged horizontally or vertically. The child panes can be resized
by the user, by moving the separator lines known as sashes by using the mouse.

Each pane contains only one widget. The PanedWindow is used to implement the
different layouts in the python applications.

The syntax to use the PanedWindow is given below.

Syntax

1. w= PanedWindow(master, options)

A list of possible options is given below.

Features of Java - Javatpoint

SN Option Description

1 bg It represents the background color of the widget when it


doesn't have the focus.

2 bd It represents the 3D border size of the widget. The default


option specifies that the trough contains no border whereas
the arrowheads and slider contain the 2-pixel border size.

3 borderwidth It represents the border width of the widget. The default is 2


pixel.

4 cursor The mouse pointer is changed to the specified cursor type


when it is over the window.

5 handlepad This option represents the distance between the handle and
the end of the sash. For the horizontal orientation, it is the
distance between the top of the sash and the handle. The
default is 8 pixels.

6 handlesize It represents the size of the handle. The default size is 8 pixels.
However, the handle will always be a square.

7 height It represents the height of the widget. If we do not specify the


height, it will be calculated by the height of the child window.
8 orient The
orient will be
set to
HORIZONTAL

9 relief It represents the type of the border. The default is FLAT.

10 sashpad It represents the padding to be done around each sash. The


default is 0.

11 sashrelief It represents the type of the border around each of the sash.
The default is FLAT.

12 sashwidth It represents the width of the sash. The default is 2 pixels.

13 showhandle It is set to True to display the handles. The default value is


false.

14 Width It represents the width of the widget. If we don't specify the


width of the widget, it will be calculated by the size of the child
widgets.

Methods
There are the following methods that are associated with the PanedWindow.

SN Method Description

1 add(child, options) It is used to add a window to the parent window.

2 get(startindex, This method is used to get the text present at the specified
endindex) range.

3 config(options) It is used to configure the widget with the specified options.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3.
4. def add():
5. a = int(e1.get())
6. b = int(e2.get())
7. leftdata = str(a+b)
8. left.insert(1,leftdata)
9.
10. w1 = PanedWindow()
11. w1.pack(fill = BOTH, expand = 1)
12.
13. left = Entry(w1, bd = 5)
14. w1.add(left)
15.
16. w2 = PanedWindow(w1, orient = VERTICAL)
17. w1.add(w2)
18.
19. e1 = Entry(w2)
20. e2 = Entry(w2)
21.
22. w2.add(e1)
23. w2.add(e2)
24.
25. bottom = Button(w2, text = "Add", command = add)
26. w2.add(bottom)
27.
28. mainloop()

Output:

Tkinter LabelFrame
The LabelFrame widget is used to draw a border around its child widgets. We can also
display the title for the LabelFrame widget. It acts like a container which can be used
to group the number of interrelated widgets such as Radiobuttons.
This widget is a variant of the Frame widget which has all the features of a frame. It
also can display a label.

The syntax to use the LabelFrame widget is given below.

Syntax

1. w = LabelFrame(top, options)

A list of options is given below.

History of Java

SN Option Description

1 bg The background color of the widget.

2 bd It represents the size of the border shown around the indicator. The
default is 2 pixels.

3 Class The default value of the class is LabelFrame.

4 colormap This option is used to specify which colomap is to be used for this
widget. By colormap, we mean the 256 colors that are used to form
the graphics. With this option, we can reuse the colormap of
another window on this widget.

5 container If this is set to true, the LabelFrame becomes the container widget.
The default value is false.

6 cursor It can be set to a cursor type, i.e. arrow, dot, etc. the mouse pointer
is changed to the cursor type when it is over the widget.

7 fg It represents the foreground color of the widget.

8 font It represents the font type of the widget text.

9 height It represents the height of the widget.

10 labelAnchor It represents the exact position of the text within the widget. The
default is NW(north-west)

11 labelwidget It represents the widget to be used for the label. The frame uses
the text for the label if no value specified.
12 highlightbackground The color of the focus highlight border when the widget doesn't
have the focus.

13 highlightcolor The color of the focus highlight when the widget has the focus.

14 highlightthickness The width of the focus highlight border.

15 padx The horizontal padding of the widget.

16 pady The vertical padding of the widget.

17 relief It represents the border style. The default value is GROOVE.

18 text It represents the string containing the label text.

19 width It represents the width of the frame.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3.
4. top = Tk()
5. top.geometry("300x200")
6.
7. labelframe1 = LabelFrame(top, text="Positive Comments")
8. labelframe1.pack(fill="both", expand="yes")
9.
10. toplabel = Label(labelframe1, text="Place to put the positive comments")
11. toplabel.pack()
12.
13. labelframe2 = LabelFrame(top, text = "Negative Comments")
14. labelframe2.pack(fill="both", expand = "yes")
15.
16. bottomlabel = Label(labelframe2,text = "Place to put the negative comments")

17. bottomlabel.pack()
18.
19. top.mainloop()

Output:
Tkinter messagebox
The messagebox module is used to display the message boxes in the python
applications. There are the various functions which are used to display the relevant
messages depending upon the application requirements.

The syntax to use the messagebox is given below.

Syntax

1. messagebox.function_name(title, message [, options])

Parameters

o function_name: It represents an appropriate message box function.


o title: It is a string which is shown as a title of a message box.
o message: It is the string to be displayed as a message on the message box.
o options: There are various options which can be used to configure the message
dialog box.

The two options that can be used are default and parent.

1. default

Hello Java Program for Beginners

The default option is used to mention the types of the default button, i.e. ABORT,
RETRY, or IGNORE in the message box.

2. parent

The parent option specifies the parent window on top of which, the message box is to
be displayed.
There is one of the following functions used to show the appropriate message boxes.
All the functions are used with the same syntax but have the specific functionalities.

1. showinfo()
The showinfo() messagebox is used where we need to show some relevant information
to the user.

Example

1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. from tkinter import messagebox
6.
7. top = Tk()
8.
9. top.geometry("100x100")
10.
11. messagebox.showinfo("information","Information")
12.
13. top.mainloop()

Output:

2. showwarning()
This method is used to display the warning to the user. Consider the following example.

Example
1. # !/usr/bin/python3
2. from tkinter import *
3.
4. from tkinter import messagebox
5.
6. top = Tk()
7. top.geometry("100x100")
8. messagebox.showwarning("warning","Warning")
9.
10. top.mainloop()

Output:

3. showerror()
This method is used to display the error message to the user. Consider the following
example.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.showerror("error","Error")
8. top.mainloop()

Output:
4. askquestion()
This method is used to ask some question to the user which can be answered in yes or
no. Consider the following example.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askquestion("Confirm","Are you sure?")
8. top.mainloop()

Output:

5. askokcancel()
This method is used to confirm the user's action regarding some application activity.
Consider the following example.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askokcancel("Redirect","Redirecting you to www.javatpoint.com")

8. top.mainloop()

Output:

6. askyesno()
This method is used to ask the user about some action to which, the user can answer
in yes or no. Consider the following example.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askyesno("Application","Got It?")
8. top.mainloop()

Output:

7. askretrycancel()
This method is used to ask the user about doing a particular task again or not. Consider
the following example.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askretrycancel("Application","try again?")
8.
9. top.mainloop()

Output:
Python NumPy Tutorial

Our Python NumPy Tutorial provides the basic and advanced concepts of the NumPy.
Our NumPy tutorial is designed for beginners and professionals.

NumPy stands for numeric python which is a python package for the computation and
processing of the multidimensional and single dimensional array elements.

What is NumPy
NumPy stands for numeric python which is a python package for the computation and
processing of the multidimensional and single dimensional array elements.

Travis Oliphant created NumPy package in 2005 by injecting the features of the
ancestor module Numeric into another module Numarray.

It is an extension module of Python which is mostly written in C. It provides various


functions which are capable of performing the numeric computations with a high
speed.

NumPy provides various powerful data structures, implementing multi-dimensional


arrays and matrices. These data structures are used for the optimal computations
regarding arrays and matrices.

In this tutorial, we will go through the numeric python library NumPy.

The need of NumPy


With the revolution of data science, data analysis libraries like NumPy, SciPy, Pandas,
etc. have seen a lot of growth. With a much easier syntax than other programming
languages, python is the first choice language for the data scientist.

NumPy provides a convenient and efficient way to handle the vast amount of data.
NumPy is also very convenient with Matrix multiplication and data reshaping. NumPy
is fast which makes it reasonable to work with a large set of data.
There are the following advantages of using NumPy for data analysis.

1. NumPy performs array-oriented computing.


2. It efficiently implements the multidimensional arrays.
3. It performs scientific computations.
4. It is capable of performing Fourier Transform and reshaping the data stored in
multidimensional arrays.
5. NumPy provides the in-built functions for linear algebra and random number
generation.

Nowadays, NumPy in combination with SciPy and Mat-plotlib is used as the


replacement to MATLAB as Python is more complete and easier programming
language than MATLAB.

NumPy Environment Setup


NumPy doesn't come bundled with Python. We have to install it using the python
pip installer. Execute the following command.

1. $ pip install numpy

It is best practice to install NumPy with the full SciPy stack. The binary distribution of
the SciPy stack is specific to the operating systems.

Windows
On the Windows operating system, The SciPy stack is provided by the Anaconda which
is a free distribution of the Python SciPy package.

It can be downloaded from the official website: https://www.anaconda.com/. It is also


available for Linux and Mac.

The CanoPy also comes with the full SciPy stack which is available as free as well as
commercial license. We can download it by visiting the link:
https://www.enthought.com/products/canopy/

The Python (x, y) is also available free with the full SciPy distribution. Download it by
visiting the link: https://python-xy.github.io/

Linux
In Linux, the different package managers are used to install the SciPy stack. The
package managers are specific to the different distributions of Linux. Let's look at each
one of them.

Ubuntu
Execute the following command on the terminal.

1. $ sudo apt-get install python-numpy


2.
3. $ python-scipy python-matplotlibipythonipythonnotebook python-pandas
4.
5. $ python-sympy python-nose

Redhat
On Redhat, the following commands are executed to install the Python SciPy package
stack.

1. $ sudo yum install numpyscipy python-matplotlibipython


2.
3. $ python-pandas sympy python-nose atlas-devel

To verify the installation, open the Python prompt by executing python command on
the terminal (cmd in the case of windows) and try to import the module NumPy as
shown in the below image. If it doesn't give the error, then it is installed successfully.

NumPy Ndarray
Ndarray is the n-dimensional array object defined in the numpy which stores the
collection of the similar type of elements. In other words, we can define a ndarray as
the collection of the data type (dtype) objects.

The ndarray object can be accessed by using the 0 based indexing. Each element of
the Array object contains the same size in the memory.

Creating a ndarray object


The ndarray object can be created by using the array routine of the numpy module.
For this purpose, we need to import the numpy.

1. >>> a = numpy.array

Consider the below image.

We can also pass a collection object into the array routine to create the equivalent n-
dimensional array. The syntax is given below.

1. >>> numpy.array(object, dtype = None, copy = True, order = None, subok =


False, ndmin = 0)

The parameters are described in the following table.


SN Parameter Description

1 object It represents the collection object. It can be a list, tuple,


dictionary, set, etc.

2 dtype We can change the data type of the array elements by


changing this option to the specified type. The default is none.

3 copy It is optional. By default, it is true which means the object is


copied.

4 order There can be 3 possible values assigned to this option. It can


be C (column order), R (row order), or A (any)

5 subok The returned array will be base class array by default. We can
change this to make the subclasses passes through by setting
this option to true.

6 ndmin It represents the minimum dimensions of the resultant array.

To create an array using the list, use the following syntax.

1. >>> a = numpy.array([1, 2, 3])

To create a multi-dimensional array object, use the following syntax.

1. >>> a = numpy.array([[1, 2, 3], [4, 5, 6]])


To change the data type of the array elements, mention the name of the data type
along with the collection.

1. >>> a = numpy.array([1, 3, 5, 7], complex)

Finding the dimensions of the Array


The ndim function can be used to find the dimensions of the array.

1. >>> import numpy as np


2. >>> arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])
3.
4. >>> print(arr.ndim)
Finding the size of each array element
The itemsize function is used to get the size of each array item. It returns the number
of bytes taken by each array element.

Consider the following example.

Example

1. #finding the size of each item in the array


2. import numpy as np
3. a = np.array([[1,2,3]])
4. print("Each item contains",a.itemsize,"bytes")

Output:

Each item contains 8 bytes.

Finding the data type of each array item


To check the data type of each array item, the dtype function is used. Consider the
following example to check the data type of the array items.

Example

1. #finding the data type of each array item


2. import numpy as np
3. a = np.array([[1,2,3]])
4. print("Each item is of the type",a.dtype)
Output:

Each item is of the type int64

Finding the shape and size of the array


To get the shape and size of the array, the size and shape function associated with the
numpy array is used.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,3,4,5,6,7]])
3. print("Array Size:",a.size)
4. print("Shape:",a.shape)

Output:

Array Size: 7
Shape: (1, 7)

Reshaping the array objects


By the shape of the array, we mean the number of rows and columns of a multi-
dimensional array. However, the numpy module provides us the way to reshape the
array by changing the number of rows and columns of the multi-dimensional array.

The reshape() function associated with the ndarray object is used to reshape the array.
It accepts the two parameters indicating the row and columns of the new shape of the
array.

Let's reshape the array given in the following image.


Example

1. import numpy as np
2. a = np.array([[1,2],[3,4],[5,6]])
3. print("printing the original array..")
4. print(a)
5. a=a.reshape(2,3)
6. print("printing the reshaped array..")
7. print(a)

Output:

printing the original array..


[[1 2]
[3 4]
[5 6]]
printing the reshaped array..
[[1 2 3]
[4 5 6]]

Slicing in the Array


Slicing in the NumPy array is the way to extract a range of elements from an array.
Slicing in the array is performed in the same way as it is performed in the python list.

Consider the following example to print a particular element of the array.

Example

1. import numpy as np
2. a = np.array([[1,2],[3,4],[5,6]])
3. print(a[0,1])
4. print(a[2,0])

Output:

2
5

The above program prints the 2nd element from the 0th index and 0th element from the
2nd index of the array.

Linspace
The linspace() function returns the evenly spaced values over the given interval. The
following example returns the 10 evenly separated values over the given interval 5-15

Example

1. import numpy as np
2. a=np.linspace(5,15,10) #prints 10 values which are evenly spaced over the given inter
val 5-15
3. print(a)

Output:

[ 5. 6.11111111 7.22222222 8.33333333 9.44444444 10.55555556


11.66666667 12.77777778 13.88888889 15. ]

Finding the maximum, minimum, and sum of the array


elements
The NumPy provides the max(), min(), and sum() functions which are used to find the
maximum, minimum, and sum of the array elements respectively.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([1,2,3,10,15,4])
3. print("The array:",a)
4. print("The maximum element:",a.max())
5. print("The minimum element:",a.min())
6. print("The sum of the elements:",a.sum())

Output:

The array: [ 1 2 3 10 15 4]
The maximum element: 15
The minimum element: 1
The sum of the elements: 35

NumPy Array Axis


A NumPy multi-dimensional array is represented by the axis where axis-0 represents
the columns and axis-1 represents the rows. We can mention the axis to perform row-
level or column-level calculations like the addition of row or column elements.

To calculate the maximum element among each column, the minimum element among
each row, and the addition of all the row elements, consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. print("The array:",a)
4. print("The maximum elements of columns:",a.max(axis = 0))
5. print("The minimum element of rows",a.min(axis = 1))
6. print("The sum of all rows",a.sum(axis = 1))

Output:

The array: [[1 2


30]
[10 15 4]]
The maximum elements of columns: [10 15 30]
The minimum element of rows [1 4]
The sum of all rows [33 29]

Finding square root and standard deviation


The sqrt() and std() functions associated with the numpy array are used to find the
square root and standard deviation of the array elements respectively.

Standard deviation means how much each element of the array varies from the mean
value of the numpy array.
Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. print(np.sqrt(a))
4. print(np.std(a))

Output:

[[1. 1.41421356 5.47722558]


[3.16227766 3.87298335 2. ]]
10.044346115546242

Arithmetic operations on the array


The numpy module allows us to perform the arithmetic operations on multi-
dimensional arrays directly.

In the following example, the arithmetic operations are performed on the two multi-
dimensional arrays a and b.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. b = np.array([[1,2,3],[12, 19, 29]])
4. print("Sum of array a and b\n",a+b)
5. print("Product of array a and b\n",a*b)
6. print("Division of array a and b\n",a/b)

Array Concatenation
The numpy provides us with the vertical stacking and horizontal stacking which allows
us to concatenate two multi-dimensional arrays vertically or horizontally.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. b = np.array([[1,2,3],[12, 19, 29]])
4. print("Arrays vertically concatenated\n",np.vstack((a,b)));
5. print("Arrays horizontally concatenated\n",np.hstack((a,b)))

Output:

Arrays vertically concatenated


[[ 1 2 30]
[10 15 4]
[ 1 2 3]
[12 19 29]]
Arrays horizontally concatenated
[[ 1 2 30 1 2 3]
[10 15 4 12 19 29]]

NumPy Datatypes
The NumPy provides a higher range of numeric data types than that provided by the
Python. A list of numeric data types is given in the following table.

SN Data type Description

1 bool_ It represents the boolean value indicating true or false. It is


stored as a byte.

2 int_ It is the default type of integer. It is identical to long type in C


that contains 64 bit or 32-bit integer.

3 intc It is similar to the C integer (c int) as it represents 32 or 64-bit


int.

4 intp It represents the integers which are used for indexing.

5 int8 It is the 8-bit integer identical to a byte. The range of the value
is -128 to 127.

6 int16 It is the 2-byte (16-bit) integer. The range is -32768 to 32767.

7 int32 It is the 4-byte (32-bit) integer. The range is -2147483648 to


2147483647.
8 int64 It is the 8-byte (64-bit) integer. The range is -
9223372036854775808 to 9223372036854775807.

9 uint8 It is the 1-byte (8-bit) unsigned integer.

10 uint16 It is the 2-byte (16-bit) unsigned integer.

11 uint32 It is the 4-byte (32-bit) unsigned integer.

12 uint64 It is the 8 bytes (64-bit) unsigned integer.

13 float_ It is identical to float64.

14 float16 It is the half-precision float. 5 bits are reserved for the exponent.
10 bits are reserved for mantissa, and 1 bit is reserved for the
sign.

15 float32 It is a single precision float. 8 bits are reserved for the exponent,
23 bits are reserved for mantissa, and 1 bit is reserved for the
sign.

16 float64 It is the double precision float. 11 bits are reserved for the
exponent, 52 bits are reserved for mantissa, 1 bit is used for the
sign.

17 complex_ It is identical to complex128.

18 complex64 It is used to represent the complex number where real and


imaginary part shares 32 bits each.

19 complex128 It is used to represent the complex number where real and


imaginary part shares 64 bits each.

NumPy dtype
All the items of a numpy array are data type objects also known as numpy dtypes. A
data type object implements the fixed size of memory corresponding to an array.

We can create a dtype object by using the following syntax.


1. numpy.dtype(object, align, copy)

The constructor accepts the following object.

Object: It represents the object which is to be converted to the data type.

Align: It can be set to any boolean value. If true, then it adds extra padding to make it
equivalent to a C struct.

Copy: It creates another copy of the dtype object.

Example 1

1. import numpy as np
2. d = np.dtype(np.int32)
3. print(d)

Output:

int32

Example 2

1. import numpy as np
2. d = np.int32(i4)
3. print(d)

Output:

int32

Creating a Structured data type


We can create a map-like (dictionary) data type which contains the mapping between
the values. For example, it can contain the mapping between employees and salaries
or the students and the age, etc.

Consider the following example.

Example 1

1. import numpy as np
2. d = np.dtype([('salary',np.float)])
3. print(d)

Output:

[('salary', '<="" pre="">

Example 2

1. import numpy as np
2. d=np.dtype([('salary',np.float)])
3. arr = np.array([(10000.12,),(20000.50,)],dtype=d)
4. print(arr['salary'])

Output:

[(10000.12,) (20000.5 ,)]

Numpy Array Creation


The ndarray object can be constructed by using the following routines.

Numpy.empty
As the name specifies, The empty routine is used to create an uninitialized array of
specified shape and data type.

The syntax is given below.

1. numpy.empty(shape, dtype = float, order = 'C')

It accepts the following parameters.

Difference between JDK, JRE, and JVM

o Shape: The desired shape of the specified array.


o dtype: The data type of the array items. The default is the float.
o Order: The default order is the c-style row-major order. It can be set to F for FORTRAN-
style column-major order.

Example

1. import numpy as np
2. arr = np.empty((3,2), dtype = int)
3. print(arr)

Output:

[[ 140482883954664 36917984]
[ 140482883954648 140482883954648]
[6497921830368665435 172026472699604272]]

NumPy.Zeros
This routine is used to create the numpy array with the specified shape where each
numpy array item is initialized to 0.

The syntax is given below.

1. numpy.zeros(shape, dtype = float, order = 'C')

It accepts the following parameters.

o Shape: The desired shape of the specified array.


o dtype: The data type of the array items. The default is the float.
o Order: The default order is the c-style row-major order. It can be set to F for FORTRAN-
style column-major order.

Example

1. import numpy as np
2. arr = np.zeros((3,2), dtype = int)
3. print(arr)

Output:

[[0 0]
[0 0]
[0 0]]

NumPy.ones
This routine is used to create the numpy array with the specified shape where each
numpy array item is initialized to 1.

The syntax to use this module is given below.


1. numpy.ones(shape, dtype = none, order = 'C')

It accepts the following parameters.

o Shape: The desired shape of the specified array.


o dtype: The data type of the array items.
o Order: The default order is the c-style row-major order. It can be set to F for FORTRAN-
style column-major order.

Example

1. import numpy as np
2. arr = np.ones((3,2), dtype = int)
3. print(arr)

Output:

[[1 1]
[1 1]
[1 1]]

Numpy array from existing data


NumPy provides us the way to create an array by using the existing data.

numpy.asarray
This routine is used to create an array by using the existing data in the form of lists, or
tuples. This routine is useful in the scenario where we need to convert a python
sequence into the numpy array object.

The syntax to use the asarray() routine is given below.

1. numpy.asarray(sequence, dtype = None, order = None)

It accepts the following parameters.

1. sequence: It is the python sequence which is to be converted into the python array.
2. dtype: It is the data type of each item of the array.
3. order: It can be set to C or F. The default is C.
Example: creating numpy array using the list

1. import numpy as np
2. l=[1,2,3,4,5,6,7]
3. a = np.asarray(l);
4. print(type(a))
5. print(a)

Output:

<class 'numpy.ndarray'>
[1 2 3 4 5 6 7]

Example: creating a numpy array using Tuple

1. import numpy as np
2. l=(1,2,3,4,5,6,7)
3. a = np.asarray(l);
4. print(type(a))
5. print(a)

Output:

<class 'numpy.ndarray'>
[1 2 3 4 5 6 7]

Example: creating a numpy array using more than one list

1. import numpy as np
2. l=[[1,2,3,4,5,6,7],[8,9]]
3. a = np.asarray(l);
4. print(type(a))
5. print(a)

Output:

<class 'numpy.ndarray'>
[list([1, 2, 3, 4, 5, 6, 7]) list([8, 9])]

numpy.frombuffer
This function is used to create an array by using the specified buffer. The syntax to use
this buffer is given below.
1. numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

It accepts the following parameters.

o buffer: It represents an object that exposes a buffer interface.


o dtype: It represents the data type of the returned data type array. The default value is
0.
o count: It represents the length of the returned ndarray. The default value is -1.
o offset: It represents the starting position to read from. The default value is 0.

Example

1. import numpy as np
2. l = b'hello world'
3. print(type(l))
4. a = np.frombuffer(l, dtype = "S1")
5. print(a)
6. print(type(a))

Output:

<class 'bytes'>
[b'h' b'e' b'l' b'l' b'o' b' ' b'w' b'o' b'r' b'l' b'd']
<class 'numpy.ndarray'>

numpy.fromiter
This routine is used to create a ndarray by using an iterable object. It returns a one-
dimensional ndarray object.

The syntax is given below.

1. numpy.fromiter(iterable, dtype, count = - 1)

It accepts the following parameters.

1. Iterable: It represents an iterable object.


2. dtype: It represents the data type of the resultant array items.
3. count: It represents the number of items to read from the buffer in the array.

Example
1. import numpy as np
2. list = [0,2,4,6]
3. it = iter(list)
4. x = np.fromiter(it, dtype = float)
5. print(x)
6. print(type(x))

Output:

[0. 2. 4. 6.]
<class 'numpy.ndarray'>

Numpy Arrays within the numerical range


This section of the tutorial illustrates how the numpy arrays can be created using some
given specified range.

Numpy.arrange
It creates an array by using the evenly spaced values over the given interval. The syntax
to use the function is given below.

1. numpy.arrange(start, stop, step, dtype)

It accepts the following parameters.

1. start: The starting of an interval. The default is 0.


2. stop: represents the value at which the interval ends excluding this value.
3. step: The number by which the interval values change.
4. dtype: the data type of the numpy array items.

group

C++ vs Java

Example

1. import numpy as np
2. arr = np.arange(0,10,2,float)
3. print(arr)
Output:

[0. 2. 4. 6. 8.]

Example

1. import numpy as np
2. arr = np.arange(10,100,5,int)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [10 15 20 25 30 35 40 45 50 55 60 65 70


75 80 85 90 95]

NumPy.linspace
It is similar to the arrange function. However, it doesn?t allow us to specify the step
size in the syntax.

Instead of that, it only returns evenly separated values over a specified period. The
system implicitly calculates the step size.

The syntax is given below.

1. numpy.linspace(start, stop, num, endpoint, retstep, dtype)

It accepts the following parameters.

1. start: It represents the starting value of the interval.


2. stop: It represents the stopping value of the interval.
3. num: The amount of evenly spaced samples over the interval to be generated. The
default is 50.
4. endpoint: Its true value indicates that the stopping value is included in the interval.
5. rettstep: This has to be a boolean value. Represents the steps and samples between
the consecutive numbers.
6. dtype: It represents the data type of the array items.

Example

1. import numpy as np
2. arr = np.linspace(10, 20, 5)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [10. 12.5 15. 17.5 20.]

Example

1. import numpy as np
2. arr = np.linspace(10, 20, 5, endpoint = False)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [10. 12. 14. 16. 18.]

numpy.logspace
It creates an array by using the numbers that are evenly separated on a log scale.

The syntax is given below.

1. numpy.logspace(start, stop, num, endpoint, base, dtype)

It accepts the following parameters.

1. start: It represents the starting value of the interval in the base.


2. stop: It represents the stopping value of the interval in the base.
3. num: The number of values between the range.
4. endpoint: It is a boolean type value. It makes the value represented by stop as the last
value of the interval.
5. base: It represents the base of the log space.
6. dtype: It represents the data type of the array items.

Example

1. import numpy as np
2. arr = np.logspace(10, 20, num = 5, endpoint = True)
3. print("The array over the given range is ",arr)

Output:
The array over the given range is [1.00000000e+10 3.16227766e+12
1.00000000e+15 3.16227766e+17
1.00000000e+20]

Example

1. import numpy as np
2. arr = np.logspace(10, 20, num = 5,base = 2, endpoint = True)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [1.02400000e+03 5.79261875e+03


3.27680000e+04 1.85363800e+05
1.04857600e+06]

NumPy Broadcasting
In Mathematical operations, we may need to consider the arrays of different shapes.
NumPy can perform such operations where the array of different shapes are involved.

For example, if we consider the matrix multiplication operation, if the shape of the two
matrices is the same then this operation will be easily performed. However, we may
also need to operate if the shape is not similar.

Consider the following example to multiply two arrays.

Example

1. import numpy as np
2. a = np.array([1,2,3,4,5,6,7])
3. b = np.array([2,4,6,8,10,12,14])
4. c = a*b;
5. print(c)

Output:

Prime Ministers of India | List of Prime Minister of India (1947-2020)


[ 2 8 18 32 50 72 98]

However, in the above example, if we consider arrays of different shapes, we will get
the errors as shown below.

Example
1. import numpy as np
2. a = np.array([1,2,3,4,5,6,7])
3. b = np.array([2,4,6,8,10,12,14,19])
4. c = a*b;
5. print(c)

Output:

ValueError: operands could not be broadcast together with shapes (7,) (8,)

In the above example, we can see that the shapes of the two arrays are not similar and
therefore they cannot be multiplied together. NumPy can perform such operation by
using the concept of broadcasting.

In broadcasting, the smaller array is broadcast to the larger array to make their shapes
compatible with each other.

Broadcasting Rules
Broadcasting is possible if the following cases are satisfied.

1. The smaller dimension array can be appended with '1' in its shape.
2. Size of each output dimension is the maximum of the input sizes in the dimension.
3. An input can be used in the calculation if its size in a particular dimension matches the
output size or its value is exactly 1.
4. If the input size is 1, then the first data entry is used for the calculation along the
dimension.

Broadcasting can be applied to the arrays if the following rules are satisfied.

1. All the input arrays have the same shape.


2. Arrays have the same number of dimensions, and the length of each dimension is either
a common length or 1.
3. Array with the fewer dimension can be appended with '1' in its shape.

Let's see an example of broadcasting.

Example

1. import numpy as np
2. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
3. b = np.array([2,4,6,8])
4. print("\nprinting array a..")
5. print(a)
6. print("\nprinting array b..")
7. print(b)
8. print("\nAdding arrays a and b ..")
9. c = a + b;
10. print(c)

Output:

printing array a..


[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]

printing array b..


[2 4 6 8]

Adding arrays a and b ..


[[ 3 6 9 12]
[ 4 8 11 14]
[12 24 45 11]]

NumPy Array Iteration


NumPy provides an iterator object, i.e., nditer which can be used to iterate over the
given array using python standard Iterator interface.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
3. print("Printing array:")
4. print(a);
5. print("Iterating over the array:")
6. for x in np.nditer(a):
7. print(x,end=' ')

Output:

Printing array:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Iterating over the array:
1 2 3 4 2 4 5 6 10 20 39 3

Order of the iteration doesn't follow any special ordering like row-major or column-
order. However, it is intended to match the memory layout of the array.

Let's iterate over the transpose of the array given in the above example.

Example

1. import numpy as np
2. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
3. print("Printing the array:")
4. print(a)
5. print("Printing the transpose of the array:")
6. at = a.T
7. print(at)
8.
9. #this will be same as previous
10. for x in np.nditer(at):
11. print(print("Iterating over the array:")
12. for x in np.nditer(a):
13. print(x,end=' ')

Output:

Printing the array:


[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Printing the transpose of the array:
[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
1 2 3 4 2 4 5 6 10 20 39 3
Order of Iteration
As we know, there are two ways of storing values into the numpy arrays:

1. F-style order
2. C-style order

Let's see an example of how the numpy Iterator treats the specific orders (F or C).

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
4.
5. print("\nPrinting the array:\n")
6.
7. print(a)
8.
9. print("\nPrinting the transpose of the array:\n")
10. at = a.T
11.
12. print(at)
13.
14. print("\nIterating over the transposed array\n")
15.
16. for x in np.nditer(at):
17. print(x, end= ' ')
18.
19. print("\nSorting the transposed array in C-style:\n")
20.
21. c = at.copy(order = 'C')
22.
23. print(c)
24.
25. print("\nIterating over the C-style array:\n")
26. for x in np.nditer(c):
27. print(x,end=' ')
28.
29.
30. d = at.copy(order = 'F')
31.
32. print(d)
33. print("Iterating over the F-style array:\n")
34. for x in np.nditer(d):
35. print(x,end=' ')

Output:

Printing the array:

[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]

Printing the transpose of the array:

[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]

Iterating over the transposed array

1 2 3 4 2 4 5 6 10 20 39 3
Sorting the transposed array in C-style:

[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]

Iterating over the C-style array:

1 2 10 2 4 20 3 5 39 4 6 3 [[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
Iterating over the F-style array:

1 2 3 4 2 4 5 6 10 20 39 3

We can mention the order 'C' or 'F' while defining the Iterator object itself. Consider
the following example.

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
4.
5. print("\nPrinting the array:\n")
6.
7. print(a)
8.
9. print("\nPrinting the transpose of the array:\n")
10. at = a.T
11.
12. print(at)
13.
14. print("\nIterating over the transposed array\n")
15.
16. for x in np.nditer(at):
17. print(x, end= ' ')
18.
19. print("\nSorting the transposed array in C-style:\n")
20.
21. print("\nIterating over the C-style array:\n")
22. for x in np.nditer(at, order = 'C'):
23. print(x,end=' ')

Output:

Iterating over the transposed array

1 2 3 4 2 4 5 6 10 20 39 3
Sorting the transposed array in C-style:

Iterating over the C-style array:

1 2 10 2 4 20 3 5 39 4 6 3

Array Values Modification


We can not modify the array elements during the iteration since the op-flag associated
with the Iterator object is set to readonly.

However, we can set this flag to readwrite or write only to modify the array values.
Consider the following example.
Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[2,4,5,6],[10,20,39,3]])
4.
5. print("\nPrinting the original array:\n")
6.
7. print(a)
8.
9. print("\nIterating over the modified array\n")
10.
11. for x in np.nditer(a, op_flags = ['readwrite']):
12. x[...] = 3 * x;
13. print(x,end = ' ')

Output:

Printing the original array:

[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]

Iterating over the modified array

3 6 9 12 6 12 15 18 30 60 117 9

NumPy Bitwise Operators


Numpy provides the following bitwise operators.

SN Operator Description

1 bitwise_and It is used to calculate the bitwise and operation between the


corresponding array elements.

2 bitwise_or It is used to calculate the bitwise or operation between the


corresponding array elements.
3 invert It is used to calculate the bitwise not the operation of the array
elements.

4 left_shift It is used to shift the bits of the binary representation of the


elements to the left.

5 right_shift It is used to shift the bits of the binary representation of the


elements to the right.

bitwise_and Operation
The NumPy provides the bitwise_and() function which is used to calculate the
bitwise_and operation of the two operands.

The bitwise and operation is performed on the corresponding bits of the binary
representation of the operands. If both the corresponding bit in the operands is set to
1, then only the resultant bit in the AND result will be set to 1 otherwise it will be set
to 0.

Example

1. import numpy as np
2.
3. a = 10
4. b = 12
5.
6. print("binary representation of a:",bin(a))
7. print("binary representation of b:",bin(b))
8. print("Bitwise-and of a and b: ",np.bitwise_and(a,b))

Output:

binary representation of a: 0b1010


binary representation of b: 0b1100
Bitwise-and of a and b: 8

AND Truth Table


The output of the AND result of the two bits is 1 if and only if both the bits are 1
otherwise it will be 0.
A B AND (A, B)

0 0 0

0 1 0

1 0 0

1 1 1

bitwise_or Operator
The NumPy provides the bitwise_or() function which is used to calculate the bitwise or
operation of the two operands.

The bitwise or operation is performed on the corresponding bits of the binary


representation of the operands. If one of the corresponding bit in the operands is set
to 1 then the resultant bit in the OR result will be set to 1; otherwise it will be set to 0.

Example

1. import numpy as np
2.
3. a = 50
4. b = 90
5. print("binary representation of a:",bin(a))
6. print("binary representation of b:",bin(b))
7. print("Bitwise-or of a and b: ",np.bitwise_or(a,b))

Output:

binary representation of a: 0b110010


binary representation of b: 0b1011010
Bitwise-or of a and b: 122

Or truth table
The output of the OR result of the two bits is 1 if one of the bits are 1 otherwise it will
be 0.
A B Or (A, B)

0 0 0

0 1 1

1 0 1

1 1 1

Invert operation
It is used to calculate the bitwise not the operation of the given operand. The 2's
complement is returned if the signed integer is passed in the function.

Consider the following example.

Example

1. import numpy as np
2.
3. arr = np.array([20],dtype = np.uint8)
4. print("Binary representation:",np.binary_repr(20,8))
5.
6. print(np.invert(arr))
7.
8. print("Binary representation: ", np.binary_repr(235,8))

Output:

Binary representation: 00010100


[235]
Binary representation: 11101011

It shifts the bits in the binary representation of the operand to the left by the specified
position. An equal number of 0s are appended from the right. Consider the following
example.

Example
1. import numpy as np
2.
3. print("left shift of 20 by 3 bits",np.left_shift(20, 3))
4.
5. print("Binary representation of 20 in 8 bits",np.binary_repr(20, 8))
6.
7. print("Binary representation of 160 in 8 bits",np.binary_repr(160,8))

Output:

left shift of 20 by 3 bits 160


Binary representation of 20 in 8 bits 00010100
Binary representation of 160 in 8 bits 10100000

Right Shift Operation


It shifts the bits in the binary representation of the operand to the right by the specified
position. An equal number of 0s are appended from the left. Consider the following
example.

Example

1. import numpy as np
2.
3. print("left shift of 20 by 3 bits",np.right_shift(20, 3))
4.
5. print("Binary representation of 20 in 8 bits",np.binary_repr(20, 8))
6.
7. print("Binary representation of 160 in 8 bits",np.binary_repr(160,8))

Output:

left shift of 20 by 3 bits 2


Binary representation of 20 in 8 bits 00010100
Binary representation of 160 in 8 bits 10100000

NumPy String Functions


NumPy contains the following functions for the operations on the arrays of dtype
string.
SN Function Description

1 add() It is used to concatenate the corresponding array elements


(strings).

2 multiply() It returns the multiple copies of the specified string, i.e., if a string
'hello' is multiplied by 3 then, a string 'hello hello' is returned.

3 center() It returns the copy of the string where the original string is
centered with the left and right padding filled with the specified
number of fill characters.

4 capitalize() It returns a copy of the original string in which the first letter of
the original string is converted to the Upper Case.

5 title() It returns the title cased version of the string, i.e., the first letter
of each word of the string is converted into the upper case.

6 lower() It returns a copy of the string in which all the letters are
converted into the lower case.

7 upper() It returns a copy of the string in which all the letters are
converted into the upper case.

9 split() It returns a list of words in the string.

9 splitlines() It returns the list of lines in the string, breaking at line


boundaries.

10 strip() Returns a copy of the string with the leading and trailing white
spaces removed.

11 join() It returns a string which is the concatenation of all the strings


specified in the given sequence.

12 replace() It returns a copy of the string by replacing all occurrences of a


particular substring with the specified one.
13 decode() It is used to decode the specified string element-wise using the
specified codec.

14 encode() It is used to encode the decoded string element-wise.

numpy.char.add() method example

1. import numpy as np
2. print("Concatenating two string arrays:")
3. print(np.char.add(['welcome','Hi'], [' to Javatpoint', ' read python'] ))

Output:

Concatenating two string arrays:


['welcome to Javatpoint' 'Hi read python']

numpy.char.multiply() method example

1. import numpy as np
2. print("Printing a string multiple times:")
3. print(np.char.multiply("hello ",3))

Output:

Printing a string multiple times:


hello hello hello

numpy.char.center() method example

1. import numpy as np
2. print("Padding the string through left and right with the fill char *");
3. #np.char.center(string, width, fillchar)
4. print(np.char.center("Javatpoint", 20, '*'))

Output:

Padding the string through left and right with the fill char *
*****Javatpoint*****

numpy.char.capitalize() method example

1. import numpy as np
2. print("Capitalizing the string using capitalize()...")
3. print(np.char.capitalize("welcome to javatpoint"))

Output:

Capitalizing the string using capitalize()...


Welcome to javatpoint

numpy.char.title() method example

1. import numpy as np
2. print("Converting string into title cased version...")
3. print(np.char.title("welcome to javatpoint"))

Output:

Converting string into title cased version...


Welcome To Javatpoint

numpy.char.lower() method example

1. import numpy as np
2. print("Converting all the characters of the string into lowercase...")
3. print(np.char.lower("WELCOME TO JAVATPOINT"))

Output:

Converting all the characters of the string into lowercase...


welcome to javatpoint

numpy.char.upper() method example

1. import numpy as np
2. print("Converting all the characters of the string into uppercase...")
3. print(np.char.upper("Welcome To Javatpoint"))

Output:

Converting all the characters of the string into uppercase...


WELCOME TO JAVATPOINT

numpy.char.split() method example

1. import numpy as np
2. print("Splitting the String word by word..")
3. print(np.char.split("Welcome To Javatpoint"),sep = " ")

Output:

Splitting the String word by word..


['Welcome', 'To', 'Javatpoint']

numpy.char.splitlines() method example

1. import numpy as np
2. print("Splitting the String line by line..")
3. print(np.char.splitlines("Welcome\nTo\nJavatpoint"))

Output:

Splitting the String line by line..


['Welcome', 'To', 'Javatpoint']

numpy.char.strip() method example

1. import numpy as np
2. str = " welcome to javatpoint "
3. print("Original String:",str)
4. print("Removing the leading and trailing whitespaces from the string")
5. print(np.char.strip(str))

Output:

Original String: welcome to javatpoint


Removing the leading and trailing whitespaces from the string
welcome to javatpoint

numpy.char.join() method example

1. import numpy as np
2. print(np.char.join(':','HM'))

Output:

H:M

numpy.char.replace() method example

1. import numpy as np
2. str = "Welcome to Javatpoint"
3. print("Original String:",str)
4. print("Modified String:",end=" ")
5. print(np.char.replace(str, "Welcome to","www."))

Output:

Original String: Welcome to Javatpoint


Modified String: www. Javatpoint

numpy.char.encode() and decode() method example

1. import numpy as np
2. enstr = np.char.encode("welcome to javatpoint", 'cp500')
3. dstr =np.char.decode(enstr, 'cp500')
4. print(enstr)
5. print(dstr)

Output:

b'\xa6\x85\x93\x83\x96\x94\x85@\xa3\x96@\x91\x81\xa5\x81\xa3\x97\x96\x89\x9
5\xa3'
welcome to javatpoint

NumPy Mathematical Functions


Numpy contains a large number of mathematical functions which can be used to
perform various mathematical operations. The mathematical functions include
trigonometric functions, arithmetic functions, and functions for handling complex
numbers. Let's discuss the mathematical functions.

Trigonometric functions
Numpy contains the trigonometric functions which are used to calculate the sine,
cosine, and tangent of the different angles in radian.

The sin, cos, and tan functions return the trigonometric ratio for the specified angles.
Consider the following example.

Example

1. import numpy as np
2. arr = np.array([0, 30, 60, 90, 120, 150, 180])
3. print("\nThe sin value of the angles",end = " ")
4. print(np.sin(arr * np.pi/180))
5. print("\nThe cosine value of the angles",end = " ")
6. print(np.cos(arr * np.pi/180))
7. print("\nThe tangent value of the angles",end = " ")
8. print(np.tan(arr * np.pi/180))

Output:

History of Java
The sin value of the angles
[0.00000000e+00 5.00000000e-01 8.66025404e-01 1.00000000e+00
8.66025404e-01 5.00000000e-01 1.22464680e-16]

The cosine value of the angles


[ 1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17
-5.00000000e-01 -8.66025404e-01 -1.00000000e+00]

The tangent value of the angles [ 0.00000000e+00 5.77350269e-01


1.73205081e+00 1.63312394e+16
-1.73205081e+00 -5.77350269e-01 -1.22464680e-16]

On the other hand, arcsin(), arccos(), and arctan() functions return the trigonometric
inverse of the specified angles.

The numpy.degrees() function can be used to verify the result of these trigonometric
functions. Consider the following example.

Example

1. import numpy as np
2. arr = np.array([0, 30, 60, 90])
3. print("printing the sin values of different angles")
4.
5. sinval = np.sin(arr*np.pi/180)
6.
7. print(sinval)
8. print("printing the inverse of the sin")
9. cosec = np.arcsin(sinval)
10.
11. print(cosec)
12.
13. print("printing the values in degrees")
14. print(np.degrees(cosec))
15.
16. print("\nprinting the cos values of different angles")
17. cosval = np.cos(arr*np.pi/180)
18.
19. print(cosval)
20. print("printing the inverse of the cos")
21. sec = np.arccos(cosval)
22. print(sec)
23.
24. print("\nprinting the values in degrees")
25. print(np.degrees(sec))
26.
27. print("\nprinting the tan values of different angles")
28. tanval = np.tan(arr*np.pi/180)
29.
30. print(tanval)
31. print("printing the inverse of the tan")
32. cot = np.arctan(tanval)
33. print(cot)
34.
35. print("\nprinting the values in degrees")
36. print(np.degrees(cot))

Output:

printing the sin values of different angles


[0. 0.5 0.8660254 1. ]
printing the inverse of the sin
[0. 0.52359878 1.04719755 1.57079633]
printing the values in degrees
[ 0. 30. 60. 90.]

printing the cos values of different angles


[1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17]
printing the inverse of the cos
[0. 0.52359878 1.04719755 1.57079633]

printing the values in degrees


[ 0. 30. 60. 90.]

printing the tan values of different angles


[0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16]
printing the inverse of the tan
[0. 0.52359878 1.04719755 1.57079633]

printing the values in degrees


[ 0. 30. 60. 90.]

Rounding Functions
The numpy provides various functions that can be used to truncate the value of a
decimal float number rounded to a particular precision of decimal numbers. Let's
discuss the rounding functions.

The numpy.around() function


This function returns a decimal value rounded to a desired position of the decimal. The
syntax of the function is given below.

1. numpy.around(num, decimals)

It accepts the following parameters.

SN Parameter Description

1 num It is the input number.

2 decimals It is the number of decimals which to which the number is to


be rounded. The default value is 0. If this value is negative, then
the decimal will be moved to the left.

Consider the following example.

Example

1. import numpy as np
2. arr = np.array([12.202, 90.23120, 123.020, 23.202])
3. print("printing the original array values:",end = " ")
4. print(arr)
5. print("Array values rounded off to 2 decimal position",np.around(arr, 2))
6. print("Array values rounded off to -1 decimal position",np.around(arr, -1))

Output:

printing the original array values: [ 12.202 90.2312 123.02 23.202 ]


Array values rounded off to 2 decimal position [ 12.2 90.23 123.02 23.2
]
Array values rounded off to -2 decimal position [ 10. 90. 120. 20.]
The numpy.floor() function
This function is used to return the floor value of the input data which is the largest
integer not greater than the input value. Consider the following example.

Example

1. import numpy as np
2. arr = np.array([12.202, 90.23120, 123.020, 23.202])
3. print(np.floor(arr))

Output:

[ 12. 90. 123. 23.]

The numpy.ceil() function


This function is used to return the ceiling value of the array values which is the smallest
integer value greater than the array element. Consider the following example.

Example

1. import numpy as np
2. arr = np.array([12.202, 90.23120, 123.020, 23.202])
3. print(np.ceil(arr))

Output:

[ 13. 91. 124. 24.]

Numpy statistical functions


Numpy provides various statistical functions which are used to perform some statistical
data analysis. In this section of the tutorial, we will discuss the statistical functions
provided by the numpy.

Finding the minimum and maximum elements from the


array
The numpy.amin() and numpy.amax() functions are used to find the minimum and
maximum of the array elements along the specified axis respectively.
Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[2,10,20],[80,43,31],[22,43,10]])
4.
5. print("The original array:\n")
6. print(a)
7.
8.
9. print("\nThe minimum element among the array:",np.amin(a))
10. print("The maximum element among the array:",np.amax(a))
11.
12. print("\nThe minimum element among the rows of array",np.amin(a,0))
13. print("The maximum element among the rows of array",np.amax(a,0))
14.
15. print("\nThe minimum element among the columns of array",np.amin(a,1))
16. print("The maximum element among the columns of array",np.amax(a,1))

Output:

Java Try Catch


The original array:

[[ 2 10 20]
[80 43 31]
[22 43 10]]

The minimum element among the array: 2


The maximum element among the array: 80

The minimum element among the rows of array [ 2 10 10]


The maximum element among the rows of array [80 43 31]

The minimum element among the columns of array [ 2 31 10]


The maximum element among the columns of array [20 80 43]

numpy.ptp() function
The name of the function numpy.ptp() is derived from the name peak-to-peak. It is
used to return the range of values along an axis. Consider the following example.
Example

1. import numpy as np
2.
3. a = np.array([[2,10,20],[80,43,31],[22,43,10]])
4.
5. print("Original array:\n",a)
6.
7. print("\nptp value along axis 1:",np.ptp(a,1))
8.
9. print("ptp value along axis 0:",np.ptp(a,0))

Output:

Original array:
[[ 2 10 20]
[80 43 31]
[22 43 10]]

ptp value along axis 1: [18 49 33]


ptp value along axis 0: [78 33 21]

numpy.percentile() function
The syntax to use the function is given below.

1. numpy.percentile(input, q, axis)

It accepts the following parameters.

1. input: It is the input array.


2. q: It is the percentile (1-100) which is calculated of the array element.
3. axis: It is the axis along which the percentile is to be calculated.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[2,10,20],[80,43,31],[22,43,10]])
4.
5. print("Array:\n",a)
6.
7. print("\nPercentile along axis 0",np.percentile(a, 10,0))
8.
9. print("Percentile along axis 1",np.percentile(a, 10, 1))

Output:

Array:
[[ 2 10 20]
[80 43 31]
[22 43 10]]

Percentile along axis 0 [ 6. 16.6 12. ]


Percentile along axis 1 [ 3.6 33.4 12.4]

Calculating median, mean, and average of array items


The numpy.median() function:
Median is defined as the value that is used to separate the higher range of data sample
with a lower range of data sample. The function numpy.median() is used to calculate
the median of the multi-dimensional or one-dimensional arrays.

The numpy.mean() function:


The mean can be calculated by adding all the items of the arrays dividing by the
number of array elements. We can also mention the axis along which the mean can be
calculated.

The numpy.average() function:


The numpy.average() function is used to find the weighted average along the axis of
the multi-dimensional arrays where their weights are given in another array.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3],[4,5,6],[7,8,9]])
4.
5. print("Array:\n",a)
6.
7. print("\nMedian of array along axis 0:",np.median(a,0))
8. print("Mean of array along axis 0:",np.mean(a,0))
9. print("Average of array along axis 1:",np.average(a,1))

NumPy Sorting and Searching


Numpy provides a variety of functions for sorting and searching. There are various
sorting algorithms like quicksort, merge sort and heapsort which is implemented using
the numpy.sort() function.

The kind of the sorting algorithm to be used in the sort operation must be mentioned
in the function call.

Let's discuss the sorting algorithm which is implemented in numpy.sort()

SN Algorithm Worst case complexity

1 Quick Sort O (n ^ 2)

2 Merge Sort O (n * log(n))

3 Heap Sort O (n * log(n))

The syntax to use the numpy.sort() function is given below.

1. numpy.sort(a, axis, kind, order)

It accepts the following parameters.

SN Parameter Description

1 input It represents the input array which is to be sorted.

2 axis It represents the axis along which the array is to be sorted. If


the axis is not mentioned, then the sorting is done along the
last available axis.
3 kind It represents the type of sorting algorithm which is to be used
while sorting. The default is quick sort.

4 order It represents the filed according to which the array is to be


sorted in the case if the array contains the fields.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[10,2,3],[4,5,6],[7,8,9]])
4.
5. print("Sorting along the columns:")
6. print(np.sort(a))
7.
8. print("Sorting along the rows:")
9. print(np.sort(a, 0))
10.
11. data_type = np.dtype([('name', 'S10'),('marks',int)])
12.
13. arr = np.array([('Mukesh',200),('John',251)],dtype = data_type)
14.
15. print("Sorting data ordered by name")
16.
17. print(np.sort(arr,order = 'name'))

Output:

Sorting along the columns:


[[ 2 3 10]
[ 4 5 6]
[ 7 8 9]]
Sorting along the rows:
[[ 4 2 3]
[ 7 5 6]
[10 8 9]]
Sorting data ordered by name
[(b'John', 251) (b'Mukesh', 200)]

numpy.argsort() function
This function is used to perform an indirect sort on an input array that is, it returns an
array of indices of data which is used to construct the array of sorted data.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([90, 29, 89, 12])
4.
5. print("Original array:\n",a)
6.
7. sort_ind = np.argsort(a)
8.
9. print("Printing indices of sorted data\n",sort_ind)
10.
11. sort_a = a[sort_ind]
12.
13. print("printing sorted array")
14.
15. for i in sort_ind:
16. print(a[i],end = " ")

Output:

Original array:
[90 29 89 12]
Printing indices of sorted data
[3 1 2 0]
printing sorted array
12 29 89 90

numpy.lexsort() function
This function is used to sort the array using the sequence of keys indirectly. This
function performs similarly to the numpy.argsort() which returns the array of indices
of sorted data.

Consider the following example.

Example
1. import numpy as np
2.
3. a = np.array(['a','b','c','d','e'])
4.
5. b = np.array([12, 90, 380, 12, 211])
6.
7. ind = np.lexsort((a,b))
8.
9. print("printing indices of sorted data")
10.
11. print(ind)
12.
13. print("using the indices to sort the array")
14.
15. for i in ind:
16. print(a[i],b[i])

Output:

printing indices of sorted data


[0 3 1 4 2]
using the indices to sort the array
a 12
d 12
b 90
e 211
c 380

numpy.nonzero() function
This function is used to find the location of the non-zero elements from the array.

Consider the following example.

Example

1. import numpy as np
2.
3. b = np.array([12, 90, 380, 12, 211])
4.
5. print("printing original array",b)
6.
7. print("printing location of the non-zero elements")
8.
9. print(b.nonzero())

Output:

printing original array [ 12 90 380 12 211]


printing location of the non-zero elements
(array([0, 1, 2, 3, 4]),)

numpy.where() function
This function is used to return the indices of all the elements which satisfies a particular
condition.

Consider the following example.

Example

1. import numpy as np
2.
3. b = np.array([12, 90, 380, 12, 211])
4.
5. print(np.where(b>12))
6.
7. c = np.array([[20, 24],[21, 23]])
8.
9. print(np.where(c>20))

Output:

(array([1, 2, 4]),)
(array([0, 1, 1]), array([1, 0, 1]))

NumPy Copies and Views


The copy of an input array is physically stored at some other location and the content
stored at that particular location is returned which is the copy of the input array
whereas the different view of the same memory location is returned in the case of view.

In this section of the tutorial, we will consider the way by which, the different copies
and views are generated from some memory location.
Array Assignment
The assignment of a numpy array to another array doesn't make the direct copy of the
original array, instead, it makes another array with the same content and same id. It
represents the reference to the original array. Changes made on this reference are also
reflected in the original array.

The id() function returns the universal identifier of the array similar to the pointer in C.

SQL CREATE TABLE

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]])
4.
5. print("Original Array:\n",a)
6.
7. print("\nID of array a:",id(a))
8.
9. b = a
10.
11. print("\nmaking copy of the array a")
12.
13. print("\nID of b:",id(b))
14.
15. b.shape = 4,3;
16.
17. print("\nChanges on b also reflect to a:")
18. print(a)

Output:

Original Array:
[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

ID of array a: 139663602288640
making copy of the array a

ID of b: 139663602288640

Changes on b also reflect to a:


[[ 1 2 3]
[ 4 9 0]
[ 2 3 1]
[ 2 3 19]]

ndarray.view() method
The ndarray.view() method returns the new array object which contains the same
content as the original array does. Since it is a new array object, changes made on this
object do not reflect the original array.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]])
4.
5. print("Original Array:\n",a)
6.
7. print("\nID of array a:",id(a))
8.
9. b = a.view()
10.
11. print("\nID of b:",id(b))
12.
13. print("\nprinting the view b")
14. print(b)
15.
16. b.shape = 4,3;
17.
18. print("\nChanges made to the view b do not reflect a")
19. print("\nOriginal array \n",a)
20. print("\nview\n",b)

Output:
Original Array:
[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

ID of array a: 140280414447456

ID of b: 140280287000656

printing the view b


[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

Changes made to the view b do not reflect a

Original array
[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

view
[[ 1 2 3]
[ 4 9 0]
[ 2 3 1]
[ 2 3 19]]

ndarray.copy() method
It returns the deep copy of the original array which doesn't share any memory with the
original array. The modification made to the deep copy of the original array doesn't
reflect the original array.

Consider the following example.

Example

1. import numpy as np
2.
3. a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]])
4.
5. print("Original Array:\n",a)
6.
7. print("\nID of array a:",id(a))
8.
9. b = a.copy()
10.
11. print("\nID of b:",id(b))
12.
13. print("\nprinting the deep copy b")
14. print(b)
15.
16. b.shape = 4,3;
17.
18. print("\nChanges made to the copy b do not reflect a")
19. print("\nOriginal array \n",a)
20. print("\nCopy\n",b)

Output:

Original Array:
[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

ID of array a: 139895697586176

ID of b: 139895570139296

printing the deep copy b


[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

Changes made to the copy b do not reflect a

Original array
[[ 1 2 3 4]
[ 9 0 2 3]
[ 1 2 3 19]]

Copy
[[ 1 2 3]
[ 4 9 0]
[ 2 3 1]
[ 2 3 19]]

NumPy Matrix Library


NumPy contains a matrix library, i.e. numpy.matlib which is used to configure matrices
instead of ndarray objects.

numpy.matlib.empty() function
This function is used to return a new matrix with the uninitialized entries. The syntax
to use this function is given below.

1. numpy.matlib.empty(shape, dtype, order)


It accepts the following parameter.

1. shape: It is the tuple defining the shape of the matrix.


2. dtype: It is the data type of the matrix.
3. order: It is the insertion order of the matrix, i.e. C or F.

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.empty((3,3)))

Output:

[[6.90262230e-310 6.90262230e-310 6.90262304e-310]


[6.90262304e-310 6.90261674e-310 6.90261552e-310]
[6.90261326e-310 6.90262311e-310 3.95252517e-322]]

numpy.matlib.zeros() function
This function is used to create the matrix where the entries are initialized to zero.

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.zeros((4,3)))

Output:

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
numpy.matlib.ones() function
This function returns a matrix with all the elements initialized to 1.

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.ones((2,2)))

Output:

[[1. 1.]
[1. 1.]]

numpy.matlib.eye() function
This function returns a matrix with the diagonal elements initialized to 1 and zero
elsewhere. The syntax to use this function is given below.

1. numpy.matlib.eye(n, m, k, dtype)

It accepts the following parameters.

1. n: It represents the number of rows in the resulting matrix.


2. m: It represents the number of columns, defaults to n.
3. k: It is the index of diagonal.
4. dtype: It is the data type of the output

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.eye(n = 3, M = 3, k = 0, dtype = int))

Output:

[[1 0 0]
[0 1 0]
[0 0 1]]

numpy.matlib.identity() function
This function is used to return an identity matrix of the given size. An identity matrix is
the one with diagonal elements initializes to 1 and all other elements to zero.

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.identity(5, dtype = int))

Output:

[[1 0 0 0 0]
[0 1 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]

numpy.matlib.rand() function
This function is used to generate a matrix where all the entries are initialized with
random values.

Consider the following example.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.rand(3,3))

Output:

[[0.86201511 0.86980769 0.06704884]


[0.80531086 0.53814098 0.84394673]
[0.85653048 0.8146121 0.35744405]]

NumPy Linear Algebra


Numpy provides the following functions to perform the different algebraic calculations
on the input data.

SN Function Definition

1 dot() It is used to calculate the dot product of two arrays.

2 vdot() It is used to calculate the dot product of two vectors.

3 inner() It is used to calculate the inner product of two arrays.

4 matmul() It is used to calculate the matrix multiplication of two arrays.

5 det() It is used to calculate the determinant of a matrix.

6 solve() It is used to solve the linear matrix equation.

7 inv() It is used to calculate the multiplicative inverse of the matrix.

numpy.dot() function
This function is used to return the dot product of the two matrices. It is similar to the
matrix multiplication. Consider the following example.

Example

1. import numpy as np
2. a = np.array([[100,200],[23,12]])
3. b = np.array([[10,20],[12,21]])
4. dot = np.dot(a,b)
5. print(dot)

Output:

[[3400 6200]
[ 374 712]]

The dot product is calculated as:

[100 * 10 + 200 * 12, 100 * 20 + 200 * 21] [23*10+12*12, 23*20 + 12*21]

numpy.vdot() function
This function is used to calculate the dot product of two vectors. It can be defined as
the sum of the product of corresponding elements of multi-dimensional arrays.

Triggers in SQL (Hindi)

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[100,200],[23,12]])
3. b = np.array([[10,20],[12,21]])
4. vdot = np.vdot(a,b)
5. print(vdot)

Output:

5528

np.vdot(a,b) = 100 *10 + 200 * 20 + 23 * 12 + 12 * 21 = 5528

numpy.inner() function
This function returns the sum of the product of inner elements of the one-dimensional
array. For n-dimensional arrays, it returns the sum of the product of elements over the
last axis.

Consider the following example.

Example
1. import numpy as np
2. a = np.array([1,2,3,4,5,6])
3. b = np.array([23,23,12,2,1,2])
4. inner = np.inner(a,b)
5. print(inner)

Output:

130

numpy.matmul() function
It is used to return the multiplication of the two matrices. It gives an error if the shape
of both matrices is not aligned for multiplication. Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,3],[4,5,6],[7,8,9]])
3. b = np.array([[23,23,12],[2,1,2],[7,8,9]])
4. mul = np.matmul(a,b)
5. print(mul)

numpy determinant
The determinant of the matrix can be calculated using the diagonal elements. The
determinant of following 2 X 2 matrix

A B
C D

can be calculated as AD - BC.

The numpy.linalg.det() function is used to calculate the determinant of the matrix.


Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2],[3,4]])
3. print(np.linalg.det(a))
Output:

-2.0000000000000004

numpy.linalg.solve() function
This function is used to solve a quadratic equation where values can be given in the
form of the matrix.

The following linear equations

1. 3X + 2 Y + Z = 10
2. X + Y + Z = 5

can be represented by using three matrices as:

1. 3 2 1
2. 1 1 1
3. X
4. Y
5. Z and
6. 10
7. 5.

The two matrices can be passed into the numpy.solve() function given as follows.

Example

1. import numpy as np
2. a = np.array([[1,2],[3,4]])
3. b = np.array([[1,2],[3,4]])
4. print(np.linalg.solve(a, b))

Output:

[[1. 0.]
[0. 1.]]

numpy.linalg.inv() function
This function is used to calculate the multiplicative inverse of the input matrix. Consider
the following example.
Example

1. import numpy as np
2. a = np.array([[1,2],[3,4]])
3. print("Original array:\n",a)
4. b = np.linalg.inv(a)
5. print("Inverse:\n",b)

Output:

Original array:
[[1 2]
[3 4]]
Inverse:
[[-2. 1. ]
[ 1.5 -0.5]]

NumPy Matrix Multiplication in Python


Multiplication of matrix is an operation which produces a single matrix by taking two
matrices as input and multiplying rows of the first matrix to the column of the second
matrix. Note that we have to ensure that the number of rows in the first matrix should
be equal to the number of columns in the second matrix.
In Python, the process of matrix multiplication using NumPy is known
as vectorization. The main objective of vectorization is to remove or reduce the for
loops which we were using explicitly. By reducing 'for' loops from programs gives
faster computation. The build-in package NumPy is used for manipulation and array-
processing.

These are three methods through which we can perform numpy matrix multiplication.

1. First is the use of multiply() function, which perform element-wise multiplication of the
matrix.
2. Second is the use of matmul() function, which performs the matrix product of two
arrays.
3. Last is the use of the dot() function, which performs dot product of two arrays.

Example 1: Element-wise matrix multiplication

1. import numpy as np
2. array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3)
3. array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3)
4. result=np.multiply(array1,array2)
5. result

In the above code

OOPs Concepts in Java

o We have imported numpy with alias name np.


o We have created an array1 and array2 using numpy.array() function with dimension 3.
o We have created a variable result and assigned the returned value of np.multiply()
function.
o We have passed both the array array1 and array2 in np.multiply().
o Lastly, we tried to print the value of the result.

In the output, a three-dimensional matrix has been shown whose elements are the
result of the element-wise multiplication of both array1 and array2 elements.

Output:

array([[[ 9, 16, 21],


[24, 25, 24],
[21, 16, 9]]])

Example 2: Matrix product

1. import numpy as np
2. array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3)
3. array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3)
4. result=np.matmul(array1,array2)
5. result

Output:

array([[[ 30, 24, 18],


[ 84, 69, 54],
[138, 114, 90]]])

In the above code

o We have imported numpy with alias name np.


o We have created array1 and array2 using numpy.array() function with dimension 3.
o We have created a variable result and assigned the returned value of the np.matmul()
function.
o We have passed both the array array1 and array2 in np.matmul().
o Lastly, we tried to print the value of the result.

In the output, a three-dimensional matrix has been shown whose elements are the
product of both array1 and array2 elements.

Example 3: Dot product


These are the following specifications for numpy.dot:

o When both a and b are 1-D (one dimensional) arrays-> Inner product of two vectors
(without complex conjugation)
o When both a and b are 2-D (two dimensional) arrays -> Matrix multiplication
o When either a or b is 0-D (also known as a scalar) -> Multiply by using
numpy.multiply(a, b) or a * b.
o When a is an N-D array and b is a 1-D array -> Sum product over the last axis of a and
b.
o When a is an N-D array and b is an M-D array provided that M>=2 -> Sum product
over the last axis of a and the second-to-last axis of b:
Also, dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

1. import numpy as np
2. array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3)
3. array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3)
4. result=np.dot(array1,array2)
5. result

In the above code

o We have imported numpy with alias name np.


o We have created array1 and array2 using numpy.array() function with dimension 3.
o We have created a variable result and assigned the returned value of the np.dot()
function.
o We have passed both the array array1 and array2 in np.dot().
o Lastly, we tried to print the value of the result.
In the output, a three-dimensional matrix has been shown whose elements are the dot
product of both array1 and array2 elements.

Output:

array([[[[ 30, 24, 18]],


[[ 84, 69, 54]],
[[138, 114, 90]]]])

numpy.array() in Python
The homogeneous multidimensional array is the main object of NumPy. It is basically
a table of elements which are all of the same type and indexed by a tuple of positive
integers. The dimensions are called axis in NumPy.

The NumPy's array class is known as ndarray or alias array. The numpy.array is not
the same as the standard Python library class array.array. The array.array handles only
one-dimensional arrays and provides less functionality.

Syntax

1. numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=


0)

Parameters
There are the following parameters in numpy.array() function.

1) object: array_like

Any object, which exposes an array interface whose __array__ method returns any nested
sequence or an array.

2) dtype : optional data-type

This parameter is used to define the desired parameter for the array element. If we do not
define the data type, then it will determine the type as the minimum type which will require
to hold the object in the sequence. This parameter is used only for upcasting the array.

3) copy: bool(optional)

If we set copy equals to true, the object is copied else the copy will be made when an object
is a nested sequence, or a copy is needed to satisfy any of the other requirements such as
dtype, order, etc.
4) order : {'K', 'A', 'C', 'F'}, optional

The order parameter specifies the memory layout of the array. When the object is not an
array, the newly created array will be in C order (row head or row-major) unless 'F' is
specified. When F is specified, it will be in Fortran order (column head or column-major).
When the object is an array, it holds the following order.

order no copy copy=True

'K' Unchanged F and C order preserved.

'A' Unchanged When the input is F and not C then F order otherwise C order

'C' C order C order

'F' F order F order

When copy=False or the copy is made for the other reason, the result will be the same
as copy= True with some exceptions for A. The default order is 'K'.

5) subok : bool(optional)

When subok=True, then sub-classes will pass-through; otherwise, the returned array
will force to be a base-class array (default).

6) ndmin : int(optional)

This parameter specifies the minimum number of dimensions which the resulting array
should have. Users can be prepended to the shape as needed to meet this
requirement.

Returns
The numpy.array() method returns an ndarray. The ndarray is an array object which
satisfies the specified requirements.

Example 1: numpy.array()

1. import numpy as np
2. arr=np.array([1,2,3])
3. arr
Output:

array([1, 2, 3])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by np.array()
function.
o In the array() function, we have passed only the elements, not axis.
o Lastly, we have tried to print the value of arr.

In the output, an array has been shown.

Example 2:

1. import numpy as np
2. arr=np.array([1,2.,3.])
3. arr

Output:

array([1., 2., 3.])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by np.array()
function.
o In the array() function, we have passed elements of different type such as integer, float,
etc.
o Lastly, we have tried to print the value of arr.

In the output, an array has been displayed containing elements in such type which
require minimum memory to hold the object in the sequence.

Example 3: More than one dimensions

1. import numpy as np
2. arr=np.array([[1,2.,3.],[4.,5.,7]])
3. arr
Output:

array([[1., 2., 3.],


[4., 5., 7.]])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by np.array()
function.
o In the array() function, we have passed the number of elements in different square
brackets.
o Lastly, we have tried to print the value of arr.

In the output, a multi-dimensional array has been shown.

Example 4: Minimum dimensions: 2

1. import numpy as np
2. arr=np.array([1,2.,3.],ndmin=2)
3. arr

Output:

array([[1., 2., 3.]])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by np.array()
function.
o In the array() function, we have passed the number of elements in a square bracket and
the dimension to create a ndarray.
o Lastly, we have tried to print the value of arr.

In the output, a two-dimensional array has been shown.

Example 5: Type provided

1. import numpy as np
2. arr=np.array([12,45.,3.],dtype=complex)
3. arr

Output:

array([12.+0.j, 45.+0.j, 3.+0.j])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by the np.array()
function.
o In the array() function, we have passed the elements in the square bracket and set the
dtype to complex.
o Lastly, we have tried to print the value of arr.

In the output, the values of the 'arr' elements have been shown in the form of complex
numbers.

Example 6: Creating an array from sub-classes

1. import numpy as np
2. arr=np.array(np.mat('1 2;3 4'))
3. arr
4. arr=np.array(np.mat('1 2;3 4'),subok=True)
5. arr

Output:

array([[1, 2],
[3, 4]])
matrix([[1, 2],
[3, 4]])

In the above code

o We have imported numpy with alias name np.


o We have declared the 'arr' variable and assigned the value returned by the np.array()
function.
o In the array() function, we have passed the elements in the form of the matrix using
np.mat() function and set the subok=True.
o Lastly, we have tried to print the value of arr.
In the output, a multi-dimensional array has been shown.

numpy.concatenate() in Python
The concatenate() function is a function from the NumPy package. This function
essentially combines NumPy arrays together. This function is basically used for joining
two or more arrays of the same shape along a specified axis. There are the following
things which are essential to keep in mind:

1. NumPy's concatenate() is not like a traditional database join. It is like stacking NumPy
arrays.
2. This function can operate both vertically and horizontally. This means we can
concatenate arrays together horizontally or vertically.

The concatenate() function is usually written as np.concatenate(), but we can also write
it as numpy.concatenate(). It depends on the way of importing the numpy package,
either import numpy as np or import numpy, respectively.

Syntax

1. numpy.concatenate((a1, a2, ...), axis)

Parameters
1) (a1, a2, ...)
This parameter defines the sequence of arrays. Here, a1, a2, a3 ... are the arrays which
have the same shape, except in the dimension corresponding to the axis.

2) axis : int(optional)

This parameter defines the axis along which the array will be joined. By default, its
value is 0.

Result
It will return a ndarray containing the elements of both the arrays.

Example 1: numpy.concatenate()

1. import numpy as np
2. x=np.array([[1,2],[3,4]])
3. y=np.array([[12,30]])
4. z=np.concatenate((x,y))
5. z

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using np.array() function.
o Then, we have created another array 'y' using the same np.array() function.
o We have declared the variable 'z' and assigned the returned value of np.concatenate()
function.
o We have passed the array 'x' and 'y' in the function.
o Lastly, we tried to print the value of 'z'.

In the output, values of both the arrays, i.e., 'x' and 'y' shown as per the axis=0.

Output:

array([[ 1, 2],
[ 3, 4],
[12, 30]])

Example 2: numpy.concatenate() with axis=0

1. import numpy as np
2. x=np.array([[1,2],[3,4]])
3. y=np.array([[12,30]])
4. z=np.concatenate((x,y), axis=0)
5. z

Output:

array([[ 1, 2],
[ 3, 4],
[12, 30]])

Example 3: numpy.concatenate() with axis=1

1. import numpy as np
2. x=np.array([[1,2],[3,4]])
3. y=np.array([[12,30]])
4. z=np.concatenate((x,y.T), axis=1)
5. z

Output:

array([[ 1, 2, 12],
[ 3, 4, 30]])

In the above example, the '.T' used to change the rows into columns and columns into
rows.

Example 4: numpy.concatenate() with axis=None

1. import numpy as np
2. x=np.array([[1,2],[3,4]])
3. y=np.array([[12,30]])
4. z=np.concatenate((x,y), axis=None)
5. z

Output:

array([ 1, 2, 3, 4, 12, 30])

In the above examples, we have used np.concatenate() function. This function is not
preserved masking of MaskedArray inputs. There is the following way through which
we can concatenate the arrays that can preserve masking of MaskedArray inputs.

Example 5: np.ma.concatenate()
1. import numpy as np
2. x=np.ma.arange(3)
3. y=np.arange(3,6)
4. x[1]=np.ma.masked
5. x
6. y
7. z1=np.concatenate([x,y])
8. z2=np.ma.concatenate([x,y])
9. z1
10. z2

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using np.ma.arrange() function.
o Then, we have created another array 'y' using the same np.ma.arrange() function.
o We have declared the variable 'z1' and assigned the returned value of np.concatenate()
function.
o We have declared variable 'z2' and assigned the returned value of np.ma.concatenate()
function.
o Lastly, we tried to print the value of 'z1' and 'z2'.

In the output, values of both the arrays 'z1' and 'z2' have preserved the masking of
MaskedArray input.

Output:

masked_array(data=[0, --, 2],


mask=[False, True, False],
fill_value=999999)
array([3, 4, 5])
masked_array(data=[0, 1, 2, 3, 4, 5],
mask=False,
fill_value=999999)
masked_array(data=[0, --, 2, 3, 4, 5],
mask=[False, True, False, False, False, False],
fill_value=999999)

numpy.append() in Python
The numpy.append() function is available in NumPy package. As the name suggests,
append means adding something. The numpy.append() function is used to add or
append new values to an existing numpy array. This function adds the new values at
the end of the array.

The numpy append() function is used to merge two arrays. It returns a new array, and
the original array remains unchanged.

Syntax

1. numpy.append(arr, values, axis=None)

Parameters
There are the following parameters of the append() function:

1) arr: array_like

This is a ndarray. The new values are appended to a copy of this array. This parameter
is required and plays an important role in numpy.append() function.

2) values: array_like

This parameter defines the values which are appended to a copy of a ndarray. One
thing is to be noticed here that these values must be of the correct shape as the
original ndarray, excluding the axis. If the axis is not defined, then the values can be in
any shape and will flatten before use.

3) axis: int(optional)

This parameter defines the axis along which values are appended. When the axis is not
given to them, both ndarray and values are flattened before use.

Returns
This function returns a copy of ndarray with values appended to the axis.

Example 1: np.append()

1. import numpy as np
2. a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
3. b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
4. c=np.append(a,b)
5. c

Output:
array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11, 21, 31, 42, 52, 62,
73, 83,
93])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o Then we have created another array 'b' using the same np.array() function.
o We have declared the variable 'c' and assigned the returned value of np.append()
function.
o We have passed the array 'a' and 'b' in the function.
o Lastly, we tried to print the value of arr.

In the output, values of both arrays, i.e., 'a' and 'b', have been shown in the flattened
form, and the original array remained same.

Example 2: np.append({a1,a2,...}, axis=0)

1. import numpy as np
2. a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
3. b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
4. c=np.append(a,b,axis=0)
5. c

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o Then we have created another array 'b' using the same np.array() function.
o We have declared the variable 'c' and assigned the returned value of np.append()
function.
o We have passed the array 'a' and 'b' in the function, and we have also passed the axis
as 0.
o Lastly, we tried to print the value of arr.

In the output, values of both arrays, i.e., 'a' and 'b', have been shown vertically in a
single array, and the original array remained the same.

Output:
array([[ 10, 20, 30],
[ 40, 50, 60],
[ 70, 80, 90],
[11, 21, 31],
[42, 52, 62],
[73, 83, 93]])

Example 3: np.append({a1,a2,...}, axis=1)

1. import numpy as np
2. a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
3. b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]])
4. c=np.append(a,b,axis=1)
5. c

Output:

array([[ 10, 20, 30, 11, 21, 31],


[ 40, 50, 60, 42, 52, 62],
[ 70, 80, 90, 73, 83, 93]])

numpy.reshape() in Python
The numpy.reshape() function is available in NumPy package. As the name suggests,
reshape means 'changes in shape'. The numpy.reshape() function helps us to get a new
shape to an array without changing its data.

Sometimes, we need to reshape the data from wide to long. So in this situation, we
have to reshape the array using reshape() function.

Syntax

1. numpy.reshape(arr, new_shape, order='C')

Parameters
There are the following parameters of reshape() function:

1) arr: array_like

This is a ndarray. This is the source array which we want to reshape. This parameter is
essential and plays a vital role in numpy.reshape() function.

2) new_shape: int or tuple of ints


The shape in which we want to convert our original array should be compatible with
the original array. If an integer, the result will be a 1-D array of that length. One shape
dimension can be -1. Here, the value is approximated by the length of the array and
the remaining dimensions.

3) order: {'C', 'F', 'A'}, optional

These indexes order parameter plays a crucial role in reshape() function. These index
orders are used to read the elements of source array and place the elements into the
reshaped array using this index order.

1. The index order 'C' means to read/write the elements which are using a C-like index
order where the last axis index is changing fastest, back to the first axis index changing
slowest.
2. The index order 'F' means to read/write the elements which are using the Fortran-like
index order, where the last axis index changing slowest and the first axis index changing
fastest.
3. The 'C' and 'F' order take no amount of the memory layout of the underlying array and
only refer to the order of indexing.
4. The index order 'A' means to read/write the elements in Fortran-like index order, when
arr is contiguous in memory, otherwise use C-like order.

Returns
This function returns a ndarray. It is a new view object if possible; otherwise, it will be
a copy. There is no guarantee of the memory layout of the returned array.

Example 1: C-like index ordering

1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (4,3))
4. x
5. y

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arrange() function.
o We have declared the variable 'y' and assigned the returned value of the np.reshape()
function.
o We have passed the array 'x' and the shape in the function.
o Lastly, we tried to print the value of arr.

In the output, the array has been represented as three rows and four columns.

Example 2: Equivalent to C ravel then C reshape

1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(np.ravel(x),(3,4))
4. x
5. y

The ravel() function is used for creating a contiguous flattened array. A one-
dimensional array that contains the elements of the input, is returned. A copy is made
only when it is needed.

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

Example 3: Fortran-like index ordering

1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (4, 3), order='F')
4. x
5. y

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arrange() function.
o We have declared the variable 'y' and assigned the returned value of np.reshape()
function.
o We have passed the array 'x' and the shape and Fortran-like index order in the function.
o Lastly, we tried to print the value of arr.

In the output, the array has been represented as four rows and three columns.

Example 4: Fortran-like index ordering

1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(np.ravel(x, order='F'), (4, 3), order='F')
4. x
5. y

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])

Example 5: The unspecified value is inferred to be 2

1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (2, -1))
4. x
5. y

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arrange() function.
o We have declared the variable 'y' and assigned the returned value of the np.reshape()
function.
o We have passed the array 'x' and the shape (unspecified value) in the function.
o Lastly, we tried to print the value of arr.

In the output, the array has been represented as two rows and five columns.

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])

numpy.sum() in Python
The numpy.sum() function is available in the NumPy package of Python. This function
is used to compute the sum of all elements, the sum of each row, and the sum of each
column of a given array.

Essentially, this sum ups the elements of an array, takes the elements within a ndarray,
and adds them together. It is also possible to add rows and column elements of an
array. The output will be in the form of an array object.
Syntax
There is the following syntax of numpy.sum() function:

1. numpy.sum(arr, axis=None, dtype=None, out=None, keepdims=<no value>, i


nitial=<no value>)

Parameters
1) arr: array_like

This is a ndarray. This is the source array whose elements we want to sum. This
parameter is essential and plays a vital role in numpy.sum() function.

2) axis: int or None or tuple of ints(optional)

This parameter defines the axis along which a sum is performed. The default axis is
None, which will sum all the elements of the array. When the axis is negative, it counts
from the last to the first axis. In version 1.7.0, a sum is performed on all axis specified
in the tuple instead of a single axis or all axis as before when an axis is a tuple of ints.

3) dtype: dtype(optional)
This parameter defines the type of the accumulator and the returned array in which
the elements are summed. By default, the dtype of arr is used unless arr has an integer
dtype of less precision than the default platform integer. In such a case, when arr is
signed, then the platform integer is used, and when arr is unsigned, then an unsigned
integer of the same precision as the platform integer is used.

4) out: ndarray(optional)

This parameter defines the alternative output array in which the result will be placed.
This resulting array must have the same shape as the expected output. The type of
output values will be cast, when necessary.

5) keepdims: bool(option)

This parameter defines a Boolean value. When this parameter is set to True, the axis
which is reduced is left in the result as dimensions with size one. With the help of this
option, the result will be broadcast correctly against the input array. The keepdims will
not be passed to the sum method of sub-classes of a ndarray, when the default value
is passed, but not in case of non-default value. If the sub-class method does not
implement keepdims, then any exception can be raised.

6) initial: scalar

This parameter defines the starting value for the sum.

Returns
This function returns an array of the same shape as arr with the specified axis removed.
When arr is a 0-d array, or when the axis is None, a scalar is returned. A reference
to out is returned, when an array output is specified.

Example 1: numpy.array()

1. import numpy as np
2. a=np.array([0.4,0.5])
3. b=np.sum(a)
4. b

Output:

0.9

In the above code


o We have imported numpy with alias name 'np'.
o We have created an array 'a' using np.array() function.
o We have declared variable 'b' and assigned the returned value of np.sum() function.
o We have passed the array 'a' in the function.
o Lastly, we tried to print the value of b.

In the output, the sum of all the elements of the array has been shown.

Example 2:

1. import numpy as np
2. a=np.array([0.4,0.5,0.9,6.1])
3. x=np.sum(a, dtype=np.int32)
4. x

Output:

In the above code

o We have imported numpy with alias name 'np'.


o We have created an array 'a' using np.array() function.
o We have declared variable 'x' and assigned the returned value of np.sum() function.
o We have passed the array 'a' and data type of int32 in the function.
o Lastly, we tried to print the value of x.

In the output, the sum only of integer numbers, not floating-point values has been
displayed.

Example 3:

1. import numpy as np
2. a=np.array([[1,4],[3,5]])
3. b=np.sum(a)
4. b

In the above code

Output:
13

Example 4:

1. import numpy as np
2. a=np.array([[1,4],[3,5]])
3. b=np.sum(a,axis=0)
4. b

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o We have declared variable 'b' and assigned the returned value of np.sum() function.
o We have passed the array 'a' and axis=0 in the function.
o Lastly, we tried to print the value of b.

In the output, the sum of the column elements has been calculated accordingly.

Output:

array([4, 9])

Example 5:

1. import numpy as np
2. a=np.array([[1,4],[3,5]])
3. b=np.sum(a,axis=1)
4. b

Output:

array([5, 8])

Example 6:

1. import numpy as np
2. b=np.sum([15], initial=8)
3. b

Output:
23

In the above code

o We have imported numpy with alias name np.


o We have declared variable 'b' and assigned the returned value of np.sum() function.
o We have passed the number of elements and initial value in the function.
o Lastly, we tried to print the value of b.

In the output, the initial value has been added to the last element in the sequence of
elements and then performed the sum of all the elements.

numpy.random() in Python
The random is a module present in the NumPy library. This module contains the
functions which are used for generating random numbers. This module contains some
simple random data generation methods, some permutation and distribution
functions, and random generator functions.

All the functions in a random module are as follows:

Simple random data


There are the following functions of simple random data:

1) p.random.rand(d0, d1, ..., dn)

This function of random module is used to generate random numbers or values in a


given shape.

Example:

1. import numpy as np
2. a=np.random.rand(5,2)
3. a

Output:

array([[0.74710182, 0.13306399],
[0.01463718, 0.47618842],
[0.98980426, 0.48390004],
[0.58661785, 0.62895758],
[0.38432729, 0.90384119]])
2) np.random.randn(d0, d1, ..., dn)

This function of random module return a sample from the "standard normal"
distribution.

Example:

1. import numpy as np
2. a=np.random.randn(2,2)
3. a

Output:

array([[ 1.43327469, -0.02019121],


[ 1.54626422, 1.05831067]])
b=np.random.randn()
b
-0.3080190768904835

3) np.random.randint(low[, high, size, dtype])

This function of random module is used to generate random integers from


inclusive(low) to exclusive(high).

Example:

1. import numpy as np
2. a=np.random.randint(3, size=10)
3. a

Output:

array([1, 1, 1, 2, 0, 0, 0, 0, 0, 0])

4) np.random.random_integers(low[, high, size])

This function of random module is used to generate random integers number of type
np.int between low and high.

Example:

1. import numpy as np
2. a=np.random.random_integers(3)
3. a
4. b=type(np.random.random_integers(3))
5. b
6. c=np.random.random_integers(5, size=(3,2))
7. c

Output:

2
<type 'numpy.int32'>
array([[1, 1],
[2, 5],
[1, 3]])

5) np.random.random_sample([size])

This function of random module is used to generate random floats number in the half-
open interval [0.0, 1.0).

Example:

1. import numpy as np
2. a=np.random.random_sample()
3. a
4. b=type(np.random.random_sample())
5. b
6. c=np.random.random_sample((5,))
7. c

Output:

0.09250360565571492
<type 'float'>
array([0.34665418, 0.47027209, 0.75944969, 0.37991244, 0.14159746])

6) np.random.random([size])

This function of random module is used to generate random floats number in the half-
open interval [0.0, 1.0).

Example:

1. import numpy as np
2. a=np.random.random()
3. a
4. b=type(np.random.random())
5. b
6. c=np.random.random((5,))
7. c

Output:

0.008786953974334155
<type 'float'>
array([0.05530122, 0.59133394, 0.17258794, 0.6912388 , 0.33412534])

7) np.random.ranf([size])

This function of random module is used to generate random floats number in the half-
open interval [0.0, 1.0).

Example:

1. import numpy as np
2. a=np.random.ranf()
3. a
4. b=type(np.random.ranf())
5. b
6. c=np.random.ranf((5,))
7. c

Output:

0.2907792098474542
<type 'float'>
array([0.34084881, 0.07268237, 0.38161256, 0.46494681, 0.88071377])

8) np.random.sample([size])

This function of random module is used to generate random floats number in the half-
open interval [0.0, 1.0).

Example:

1. import numpy as np
2. a=np.random.sample()
3. a
4. b=type(np.random.sample())
5. b
6. c=np.random.sample((5,))
7. c

Output:

0.012298209913766511
<type 'float'>
array([0.71878544, 0.11486169, 0.38189074, 0.14303308, 0.07217287])

9) np.random.choice(a[, size, replace, p])

This function of random module is used to generate random sample from a given 1-D
array.

Example:

1. import numpy as np
2. a=np.random.choice(5,3)
3. a
4. b=np.random.choice(5,3, p=[0.2, 0.1, 0.4, 0.2, 0.1])
5. b

Output:

array([0, 3, 4])
array([2, 2, 2], dtype=int64)

10) np.random.bytes(length)

This function of random module is used to generate random bytes.

Example:

1. import numpy as np
2. a=np.random.bytes(7)
3. a

Output:

'nQ\x08\x83\xf9\xde\x8a'

Permutations
There are the following functions of permutations:

1) np.random.shuffle()
This function is used for modifying a sequence in-place by shuffling its contents.

Example:

1. import numpy as np
2. a=np.arange(12)
3. a
4. np.random.shuffle(a)
5. a

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])


array([10, 3, 2, 4, 5, 8, 0, 9, 1, 11, 7, 6])

2) np.random.permutation()

This function permute a sequence randomly or return a permuted range.

Example:

1. import numpy as np
2. a=np.random.permutation(12)
3. a

Output:

array([ 8, 7, 3, 11, 6, 0, 9, 10, 2, 5, 4, 1])

Distributions
There are the following functions of permutations:

1) beta(a, b[, size])

This function is used to draw samples from a Beta distribution.

Example:

1. def setup(self):
2. self.dist = dist.beta
3. self.cargs = []
4. self.ckwd = dict(alpha=2, beta=3)
5. self.np_rand_fxn = numpy.random.beta
6. self.np_args = [2, 3]
7. self.np_kwds = dict()

2) binomial(n, p[, size])

This function is used to draw sample from a binomial distribution.

Example:

1. import numpy as np
2. n, p = 10, .6
3. s1= np.random.binomial(n, p, 10)
4. s1

Output:

array([6, 7, 7, 9, 3, 7, 8, 6, 6, 4])

3) chisquare(df[, size])

This function is used to draw sample from a binomial distribution.

Example:

1. import numpy as np
2. np.random.chisquare(2,4)
3. sum(np.random.binomial(9, 0.1, 20000) == 0)/20000.

Output:

array([6, 7, 7, 9, 3, 7, 8, 6, 6, 4])

4) dirichlet(alpha[, size])

This function is used to draw a sample from the Dirichlet distribution.

Example:

1. Import numpy as np
2. import matplotlib.pyplot as plt
3. s1 = np.random.dirichlet((10, 5, 3), 20).transpose()
4. plt.barh(range(20), s1[0])
5. plt.barh(range(20), s1[1], left=s1[0], color='g')
6. plt.barh(range(20), s1[2], left=s1[0]+s1[1], color='r')
7. plt.title("Lengths of Strings")
8. plt.show()

Output:

5) exponential([scale, size])

This function is used to draw sample from an exponential distribution.

Example:

1. def __init__(self, sourceid, targetid):


2. self.__type = 'Transaction'
3. self.id = uuid4()
4. self.source = sourceid
5. self.target = targetid
6. self.date = self._datetime.date(start=2015, end=2019)
7. self.time = self._datetime.time()
8.
9. if random() < 0.05:
10. self.amount = self._numbers.between(100000, 1000000)
11. self.amount = npr.exponential(10)
12.
13. if random() < 0.15:
14. self.currency = self._business.currency_iso_code()
15. else:
16. self.currency = None

6) f(dfnum, dfden[, size])

This function is used to draw sample from an F distribution.

Example:

1. import numpy as np
2. dfno= 1.
3. dfden = 48.
4. s1 = np.random.f(dfno, dfden, 10)
5. np.sort(s1)

Output:

array([0.00264041, 0.04725478, 0.07140803, 0.19526217, 0.23979 ,


0.24023478, 0.63141254, 0.95316446, 1.40281789, 1.68327507])

7) gamma(shape[, scale, size])

This function is used to draw sample from a Gamma distribution

Example:

1. import numpy as np
2. shape, scale = 2., 2.
3. s1 = np.random.gamma(shape, scale, 1000)
4. import matplotlib.pyplot as plt
5. import scipy.special as spss
6. count, bins, ignored = plt.hist(s1, 50, density=True)
7. a = bins**(shape-1)*(np.exp(-bins/scale) /
8. (spss.gamma(shape)*scale**shape))
9. plt.plot(bins, a, linewidth=2, color='r')
10. plt.show()

8) geometric(p[, size])

This function is used to draw sample from a geometric distribution.

Example:

1. import numpy as np
2. a = np.random.geometric(p=0.35, size=10000)
3. (a == 1).sum() / 1000

Output:

3.

9) gumbel([loc, scale, size])


This function is used to draw sample from a Gumble distribution.

Example:

1. import numpy as np
2. lov, scale = 0, 0.2
3. s1 = np.random.gumbel(loc, scale, 1000)
4. import matplotlib.pyplot as plt
5. count, bins, ignored = plt.hist(s1, 30, density=True)
6. plt.plot(bins, (1/beta)*np.exp(-(bins - loc)/beta)* np.exp( -np.exp( -
(bins - loc) /beta) ),linewidth=2, color='r')
7. plt.show()

Output:

10) hypergeometric(ngood, nbad, nsample[, size])


This function is used to draw sample from a Hypergeometric distribution.

Example:

1. import numpy as np
2. good, bad, samp = 100, 2, 10
3. s1 = np.random.hypergeometric(good, bad, samp, 1000)
4. plt.hist(s1)
5. plt.show()

Output:

(array([ 13., 0., 0., 0., 0., 163., 0., 0., 0., 824.]), array([
8. , 8.2, 8.4, 8.6, 8.8, 9. , 9.2, 9.4, 9.6, 9.8, 10. ]), <a list
of 10 Patch objects>)

11) laplace([loc, scale, size])


This function is used to draw sample from the Laplace or double exponential
distribution with specified location and scale.

Example:

1. import numpy as np
2. location, scale = 0., 2.
3. s = np.random.laplace(location, scale, 10)
4. s

Output:

array([-2.77127948, -1.46401453, -0.03723516, -1.61223942, 2.29590691,


1.74297722, 1.49438411, 0.30325513, -0.15948891, -4.99669747])

12) logistic([loc, scale, size])

This function is used to draw sample from logistic distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. location, scale = 10, 1
4. s1 = np.random.logistic(location, scale, 10000)
5. count, bins, ignored = plt.hist(s1, bins=50)
6. count
7. bins
8. ignored
9. plt.show()

Output:

array([1.000e+00, 1.000e+00, 1.000e+00, 0.000e+00, 1.000e+00, 1.000e+00,


1.000e+00, 5.000e+00, 7.000e+00, 1.100e+01, 1.800e+01, 3.500e+01,
5.300e+01, 6.700e+01, 1.150e+02, 1.780e+02, 2.300e+02, 3.680e+02,
4.910e+02, 6.400e+02, 8.250e+02, 9.100e+02, 9.750e+02, 1.039e+03,
9.280e+02, 8.040e+02, 6.530e+02, 5.240e+02, 3.380e+02, 2.470e+02,
1.650e+02, 1.150e+02, 8.500e+01, 6.400e+01, 3.300e+01, 1.600e+01,
2.400e+01, 1.400e+01, 4.000e+00, 5.000e+00, 2.000e+00, 2.000e+00,
1.000e+00, 1.000e+00, 0.000e+00, 1.000e+00, 0.000e+00, 0.000e+00,
0.000e+00, 1.000e+00])
array([ 0.50643911, 0.91891814, 1.33139717, 1.7438762 , 2.15635523,
2.56883427, 2.9813133 , 3.39379233, 3.80627136, 4.2187504 ,
4.63122943, 5.04370846, 5.45618749, 5.86866652, 6.28114556,
6.69362459, 7.10610362, 7.51858265, 7.93106169, 8.34354072,
8.75601975, 9.16849878, 9.58097781, 9.99345685, 10.40593588,
10.81841491, 11.23089394, 11.64337298, 12.05585201, 12.46833104,
12.88081007, 13.2932891 , 13.70576814, 14.11824717, 14.5307262 ,
14.94320523, 15.35568427, 15.7681633 , 16.18064233, 16.59312136,
17.00560039, 17.41807943, 17.83055846, 18.24303749, 18.65551652,
19.06799556, 19.48047459, 19.89295362, 20.30543265, 20.71791168,
21.13039072])
<a list of 50 Patch objects>

13) lognormal([mean, sigma, size])

This function is used to draw sample from a log-normal distribution.

Example:

1. import numpy as np
2. mu, sigma = 2., 1.
3. s1 = np.random.lognormal(mu, sigma, 1000)
4. import matplotlib.pyplot as plt
5. count, bins, ignored = plt.hist(s1, 100, density=True, align='mid')
6. a = np.linspace(min(bins), max(bins), 10000)
7. pdf = (np.exp(-
(np.log(a) - mu)**2 / (2 * sigma**2))/ (a * sigma * np.sqrt(2 * np.pi)))
8. plt.plot(a, pdf, linewidth=2, color='r')
9. plt.axis('tight')
10. plt.show()

Output:

14) logseries(p[, size])

This function is used to draw sample from a logarithmic distribution.

Example:

1. import numpy as np
2. x = .6
3. s1 = np.random.logseries(x, 10000)
4. count, bins, ignored = plt.hist(s1)
5. def logseries(k, p):
6. return -p**k/(k*log(1-p))
7. plt.plot(bins, logseries(bins, x)*count.max()/logseries(bins, a).max(), 'r')
8. plt.show()

Output:

15) multinomial(n, pvals[, size])

This function is used to draw sample from a multinomial distribution.

Example:

1. import numpy as np
2. np.random.multinomial(20, [1/6.]*6, size=1)

Output:
array([[4, 2, 5, 5, 3, 1]])

16) multivariate_normal(mean, cov[, size, ...)

This function is used to draw sample from a multivariate normal distribution.

Example:

1. import numpy as np
2. mean = (1, 2)
3. coveriance = [[1, 0], [0, 100]]
4. import matplotlib.pyplot as plt
5. a, b = np.random.multivariate_normal(mean, coveriance, 5000).T
6. plt.plot(a, b, 'x')
7. plt.axis('equal'023
8. 030
9. )
10. plt.show()

Output:
17) negative_binomial(n, p[, size])

This function is used to draw sample from a negative binomial distribution.

Example:

1. import numpy as np
2. s1 = np.random.negative_binomial(1, 0.1, 100000)
3. for i in range(1, 11):
4. probability = sum(s1<i) / 100000.
5. print i, "wells drilled, probability of one success =", probability

Output:

1 wells drilled, probability of one success = 0


2 wells drilled, probability of one success = 0
3 wells drilled, probability of one success = 0
4 wells drilled, probability of one success = 0
5 wells drilled, probability of one success = 0
6 wells drilled, probability of one success = 0
7 wells drilled, probability of one success = 0
8 wells drilled, probability of one success = 0
9 wells drilled, probability of one success = 0
10 wells drilled, probability of one success = 0

18) noncentral_chisquare(df, nonc[, size])

This function is used to draw sample from a noncentral chi-square distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. val = plt.hist(np.random.noncentral_chisquare(3, 25, 100000), bins=200, norme
d=True)
4. plt.show()

Output:
19) normal([loc, scale, size])

This function is used to draw sample from a normal distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. mu, sigma = 0, 0.2 # mean and standard deviation
4. s1 = np.random.normal(mu, sigma, 1000)
5. abs(mu - np.mean(s1)) < 0.01
6. abs(sigma - np.std(s1, ddof=1)) < 0.01
7. count, bins, ignored = plt.hist(s1, 30, density=True)
8. plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ), l
inewidth=2, color='r')
9. plt.show()

Output:
20) pareto(a[, size])

This function is used to draw samples from a Lomax or Pareto II with specified shape.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. b, m1 = 3., 2. # shape and mode
4. s1 = (np.random.pareto(b, 1000) + 1) * m1
5. count, bins, _ = plt.hist(s1, 100, density=True)
6. fit = b*m**b / bins**(b+1)
7. plt.plot(bins, max(count)*fit/max(fit), linewidth=2, color='r')
8. plt.show()

Output:
21) power(a[, size])

This function is used to draw samples in [0, 1] from a power distribution with positive
exponent a-1.

Example:

1. import numpy as np
2. x = 5. # shape
3. samples = 1000
4. s1 = np.random.power(x, samples)
5. import matplotlib.pyplot as plt
6. count, bins, ignored = plt.hist(s1, bins=30)
7. a = np.linspace(0, 1, 100)
8. b = x*a**(x-1.)
9. density_b = samples*np.diff(bins)[0]*b
10. plt.plot(a, density_b)
11. plt.show()

Output:

22) rayleigh([scale, size])

This function is used to draw sample from a Rayleigh distribution.

Example:

1. val = hist(np.random.rayleigh(3, 100000), bins=200, density=True)


2. meanval = 1
3. modeval = np.sqrt(2 / np.pi) * meanval
4. s1 = np.random.rayleigh(modeval, 1000000)
5. 100.*sum(s1>3)/1000000.

Output:
0.087300000000000003

23) standard_cauchy([size])

This function is used to draw sample from a standard Cauchy distribution with
mode=0.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. s1 = np.random.standard_cauchy(1000000)
4. s1 = s1[(s1>-25) & (s1<25)] # truncate distribution so it plots well
5. plt.hist(s1, bins=100)
6. plt.show()

Output:
24) standard_exponential([size])

This function is used to draw sample from a standard exponential distribution.

Example:

1. import numpy as np
2. n = np.random.standard_exponential((2, 7000))

Output:

array([[0.53857931, 0.181262 , 0.20478701, ..., 3.66232881, 1.83882709,


1.77963295],
[0.65163973, 1.40001955, 0.7525986 , ..., 0.76516523, 0.8400617 ,
0.88551011]])

25) standard_gamma([size])

This function is used to draw sample from a standard Gamma distribution.


Example:

1. import numpy as np
2. shape, scale = 2., 1.
3. s1 = np.random.standard_gamma(shape, 1000000)
4. import matplotlib.pyplot as plt
5. import scipy.special as sps
6. count1, bins1, ignored1 = plt.hist(s, 50, density=True)
7. y = bins1**(shape-1) * ((np.exp(-
bins1/scale))/ (sps.gamma(shape) * scale**shape))
8. plt.plot(bins1, y, linewidth=2, color='r')
9. plt.show()

Output:

26) standard_normal([size])
This function is used to draw sample from a standard Normal distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. s1= np.random.standard_normal(8000)
4. s1
5. q = np.random.standard_normal(size=(3, 4, 2))
6. q

Output:

array([-3.14907597, 0.95366265, -1.20100026, ..., 3.47180222,


0.9608679 , 0.0774319 ])
array([[[ 1.55635461, -1.29541713],
[-1.50534663, -0.02829194],
[ 1.03949348, -0.26128132],
[ 1.51921798, 0.82136178]],

[[-0.4011052 , -0.52458858],
[-1.31803814, 0.37415379],
[-0.67077365, 0.97447018],
[-0.20212115, 0.67840888]],

[[ 1.86183474, 0.19946562],
[-0.07376021, 0.84599701],
[-0.84341386, 0.32081667],
[-3.32016062, -1.19029818]]])

27) standard_t(df[, size])

This function is used to draw sample from a standard Student's distribution with df
degree of freedom.

Example:

1. intake = np.array([5260., 5470, 5640, 6180, 6390, 6515, 6805, 7515,8230,8770])

2. s1 = np.random.standard_t(10, size=100000)
3. np.mean(intake)
4. intake.std(ddof=1)
5. t = (np.mean(intake)-7725)/(intake.std(ddof=1)/np.sqrt(len(intake)))
6. h = plt.hist(s1, bins=100, density=True)
7. np.sum(s1<t) / float(len(s1))
8. plt.show()
Output:

6677.5
1174.1101831694598
0.00864

28) triangular(left, mode, right[, size])

This function is used to draw sample from a triangular distribution over the interval.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. h = plt.hist(np.random.triangular(-4, 0, 8, 1000000), bins=300,density=True)
4. plt.show()

Output:
29) uniform([low, high, size])

This function is used to draw sample from a uniform distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. s1 = np.random.uniform(-1,0,1000)
4. np.all(s1 >= -1)
5. np.all(s1 < 0)
6. count, bins, ignored = plt.hist(s1, 15, density=True)
7. plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
8. plt.show()

Output:
30) vonmises(m1, m2[, size])

This function is used to draw sample from a von Mises distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. m1, m2 = 0.0, 4.0
4. s1 = np.random.vonmises(m1, m2, 1000)
5. from scipy.special import i0
6. plt.hist(s1, 50, density=True)
7. x = np.linspace(-np.pi, np.pi, num=51)
8. y = np.exp(m2*np.cos(x-m1))/(2*np.pi*i0(m2))
9. plt.plot(x, y, linewidth=2, color='r')
10. plt.show()
Output:

31) wald(mean, scale[, size])

This function is used to draw sample from a Wald, or inverse Gaussian distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. h = plt.hist(np.random.wald(3, 3, 100000), bins=250, density=True)
4. plt.show()

Output:
32) weibull(a[, size])

This function is used to draw sample from a Weibull distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. from scipy import special
4. x=2.0
5. s=np.random.weibull(x, 1000)
6. a = np.arange(1, 100.)/50.
7. def weib(x, n, a):
8. return (a/n)*(x/n)**np.exp(-(x/n)**a)
9. count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
10. a= np.arange(1,100.)/50.
11. scale = count.max()/weib(x, 1., 5.).max()
12. scale = count.max()/weib(a, 1., 5.).max()
13. plt.plot(x, weib(x, 1., 5.)*scale)
14. plt.show()

Output:

33) zipf(a[, size])

This function is used to draw sample from a Zipf distribution.

Example:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. from scipy import special
4. x=2.0
5. s=np.random.zipf(x, 1000)
6. count, bins, ignored = plt.hist(s[s<50], 50, density=True)
7. a = np.arange(1., 50.)
8. b= a**(-x) / special.zetac(x)
9. plt.plot(a, b/max(b), linewidth=2, color='r')
10. plt.show()

Output:

numpy.zeros() in Python
The numpy.zeros() function is one of the most significant functions which is used in
machine learning programs widely. This function is used to generate an array
containing zeros.

The numpy.zeros() function provide a new array of given shape and type, which is filled
with zeros.
Syntax

1. numpy.zeros(shape, dtype=float, order='C'

Parameters
shape: int or tuple of ints

This parameter is used to define the dimensions of the array. This parameter is used
for the shape in which we want to create an array, such as (3,2) or 2.

dtype: data-type(optional)

This parameter is used to define the desired data-type for the array. By default, the
data-type is numpy.float64. This parameter is not essential for defining.

order: {'C','F'}(optional)

This parameter is used to define the order in which we want to store data in memory
either row-major(C-style) or column-major(Fortran-style)

Return
This function returns a ndarray. The output array is the array with specified shape,
dtype, order, and contains zeros.

Example 1: numpy.zeros() without dtype and order

1. import numpy as np
2. a=np.zeros(6)
3. a

Output:

array([0., 0., 0., 0., 0., 0.])

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'a' and assigned the returned value of np.zeros()
function.
o We have passed an integer value in the function.
o Lastly, we tried to print the value of 'a'.

In the output, an array with floating-point integers(zeros) has been shown.

Example 2: numpy.zeros() without order

1. import numpy as np
2. a=np.zeros((6,), dtype=int)
3. a

Output:

array([0, 0, 0, 0, 0, 0])

Example 3: numpy.zeros() with shape

1. import numpy as np
2. a=np.zeros((6,2))
3. a

Output:

array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])

In the above code


o We have imported numpy with alias name np.
o We have declared the variable 'a' and assigned the returned value of np.zeros()
function.
o We have passed the shape for the array elements.
o Lastly, we tried to print the value of 'a'.

In the output, an array of given shape has been shown.

Example 4: numpy.zeros() with the shape

1. Import numpy as np
2. s1=(3,2)
3. a=np.zeros(s1)
4. a

Output:

array([[0., 0.],
[0., 0.],
[0., 0.]])

Example 5: numpy.zeros() with custom dtype

1. Import numpy as np
2. a=np.zeros((3,), dtype=[('x', 'i4'), ('y', 'i4')])
3. a

Output:

array([(0, 0), (0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'a' and assigned the returned value of np.zeros()
function.
o We have passed the shape and custom data type in the function.
o Lastly, we tried to print the value of 'a'.

In the output, an array contains zeros with custom data-type has been shown.
numpy.log() in Python
The numpy.log() is a mathematical function that is used to calculate the natural
logarithm of x(x belongs to all the input array elements). It is the inverse of the
exponential function as well as an element-wise natural logarithm. The natural
logarithm log is the reverse of the exponential function, so that log(exp(x))=x. The
logarithm in base e is the natural logarithm.

Syntax

1. numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dty


pe=None, subok=True[, signature, extobj]) = <ufunc 'log'>

Parameters
x: array_like

This parameter defines the input value for the numpy.log() function.

out: ndarray, None, or tuple of ndarray and None(optional)

This parameter is used to define the location in which the result is stored. If we define
this parameter, it must have a shape similar to the input broadcast; otherwise, a freshly-
allocated array is returned. A tuple has a length equal to the number of outputs.

where: array_like(optional)

It is a condition that is broadcast over the input. At this location, where the condition
is True, the out array will be set to the ufunc(universal function) result; otherwise, it will
retain its original value.

casting: {'no','equiv','safe','same_kind','unsafe'}(optional)

This parameter controls the kind of data casting that may occur. The 'no' means the
data types should not be cast at all. The 'equiv' means only byte-order changes are
allowed. The 'safe' means the only cast, which can allow the preserved value. The
'same_kind' means only safe casts or casts within a kind. The 'unsafe' means any data
conversions may be done.

order: {'K', 'C', 'F', 'A'}(optional)

This parameter specifies the calculation iteration order/ memory layout of the output
array. By default, the order will be K. The order 'C' means the output should be C-
contiguous. The order 'F' means F-contiguous, and 'A' means F-contiguous if the
inputs are F-contiguous and if inputs are in C-contiguous, then 'A' means C-
contiguous. 'K' means to match the element ordering of the inputs(as closely as
possible).

dtype: data-type(optional)

It overrides the dtype of the calculation and output arrays.

subok: bool(optional)

By default, this parameter is set to true. If we set it to false, the output will always be a
strict array, not a subtype.

signature

This argument allows us to provide a specific signature to the 1-d loop 'for', used in
the underlying calculation.

extobj

This parameter is a list of length 1, 2, or 3 specifying the ufunc buffer-size, the error
mode integer, and the error callback function.

Returns
This function returns a ndarray that contains the natural logarithmic value of x, which
belongs to all elements of the input array.

Example 1:

1. import numpy as np
2. a=np.array([2, 4, 6, 3**8])
3. a
4. b=np.log(a)
5. b
6. c=np.log2(a)
7. c
8. d=np.log10(a)
9. d

Output:

array([ 2, 4, 6, 6561])
array([0.69314718, 1.38629436, 1.79175947, 8.78889831])
array([ 1. , 2. , 2.5849625 , 12.67970001])
array([0.30103 , 0.60205999, 0.77815125, 3.81697004])

In the above mentioned code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o We have declared variable b, c, and, d and assigned the returned value of np.log(),
np.log2(), and np.log10() functions respectively.
o We have passed the array 'a' in all the functions.
o Lastly, we tried to print the value of b, c, and d.

In the output, a ndarray has been shown, contains the log, log2, and log10 values of
all the elements of the source array.

Example 2:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. arr = [2, 2.2, 2.4, 2.6,2.8, 3]
4. result1=np.log(arr)
5. result2=np.log2(arr)
6. result3=np.log10(arr)
7. plt.plot(arr,arr, color='blue', marker="*")
8. plt.plot(result1,arr, color='green', marker="o")
9. plt.plot(result2,arr, color='red', marker="*")
10. plt.plot(result3,arr, color='black', marker="*")
11. plt.show()

Output:
In the above code

o We have imported numpy with alias name np.


o We have also imported matplotlib.pyplot with alias name plt.
o Next, we have created an array 'arr' using np.array() function.
o After that we declared variable result1, result2, result3 and assigned the returned values
of np.log(), np.log2(), and np.log10() functions respectively.
o We have passed the array 'arr' in all the functions.
o Lastly, we tried to plot the values of 'arr', result1, result2, and result3.

In the output, a graph with four straight lines with different colors has been shown.

Example 3:

1. import numpy as np
2. x=np.log([2, np.e, np.e**3, 0])
3. x

Output:

__main__:1: RuntimeWarning: divide by zero encountered in log


array([0.69314718, 1. , 3. , -inf])

In the above code

o Firstly, we have imported numpy with alias name np.


o We have declared the variable 'x' and assigned the returned value of np.log() functions.
o We have passed different values in the function, such as integer value, np.e, and
np.e**2.
o Lastly, we tried to print the value of 'x'.

In the output, a ndarray has been shown, contains the log values of the elements of
the source array.

numpy.where() in Python
The NumPy module provides a function numpy.where() for selecting elements based
on a condition. It returns elements chosen from a or b depending on the condition.

For example, if all arguments -> condition, a & b are passed in numpy.where() then it
will return elements selected from a & b depending on values in bool array yielded by
the condition.

If only the condition is provided, this function is a shorthand to the function np.asarray
(condition).nonzero(). Although nonzero should be preferred directly, as it behaves
correctly for subclasses.

Syntax:

1. numpy.where(condition[, x, y])

Parameters:
These are the following parameters in numpy.where() function:

condition: array_like, bool

If this parameter set to True, yield x otherwise yield y.


x, y: array_like:

This parameter defines the values from which to choose. The x, y, and condition need
to be broadcastable to some shape.

Returns:
This function returns the array with elements from x where the condition is True and
elements from y elsewhere.

Example 1: np.where()

1. import numpy as np
2. a=np.arange(12)
3. b=np.where(a<6,a,5*a)
4. b

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arange() function.
o We have declared the variable 'b' and assigned the returned value of np.where()
function.
o We have passed the array 'a' in the function.
o Lastly, we tried to print the value of b.

In the output, the values ranging from 0 to 5 remain the same as per the condition,
and the other values have been multiplied with 5.

Output:

array([ 0, 1, 2, 3, 4, 5, 30, 35, 40, 45, 50, 55])

Example 2: For multidimensional array

1. import numpy as np
2. a=np.arange(12)
3. b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]])
4. b

Output:
array([[1, 8],
[3, 4]])

Example 3: Broadcasting x, y, and condition

1. import numpy as np
2. x, y = np.ogrid[:3, :4]
3. a=np.where(x > y, x, 10 + y)
4. a

Output:

array([[10, 11, 12, 13],


[ 1, 11, 12, 13],
[ 2, 2, 12, 13]])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arange() function.
o We declared the variable 'b' and assigned the returned value of np.where() function.
o We have passed a multidimensional array of boolean as a condition and x and y as an
integer arrays.
o Lastly, we tried to print the value of b.

In the output, the x value has been compared to y value if it satisfied the condition,
then it will be printed x value otherwise, it will print y value, which has passed as an
argument in the where() function.

Example 4: Broadcasting specific value

1. x=np.array([[0,1,2],[0,2,5],[0,4,8]])
2. y=np.where(x<4,x,-2)
3. y

Output:

array([[ 0, 1, 2],
[ 0, 2, -2],
[ 0, -2, -2]])

numpy.argsort() in Python
The NumPy module provides a function argsort(), returns the indices which would sort
an array.

The NumPy module provides a function for performing an indirect sort along with the
given axis with the help of the algorithm specified by the keyword. This function
returns an array of indices of the same shape as 'a', which would sort the array.

Syntax

1. numpy.argsort(a, axis=-1, kind=None, order=None)

Parameters
These are the following parameters in numpy.argsort() function:

a: array_like

This parameter defines the source array which we want to sort.

axis: int or None(optional)

This parameter defines the axis along which the sorting is performed. By default, the
axis is -1. If we set this parameter to None, the flattened array is used.

kind: {'quicksort','mergesort','heapsort','stable'}(optional)

This parameter defines the sorting algorithm. By default, the algorithm is quicksort.
Both mergesort and stable are using time sort under the covers. The actual
implementation will vary with the data type. The mergesort option is retained for
backward compatibility.

order: str or list of str(optional)

If 'a' is an array with defined fields, this argument specifies which fields to compare
first, second, etc. The single field can be specified as a string, and not all fields need to
be specified. But unspecified fields will still use, in the order in which they come up in
the dtype, to break the ties.

Returns: index_array: ndarray, int


This function returns an array of indices which sort 'a' along with the specified axis. If
'a' is 1-D, a[index_array] yields a sorted 'a'. More generally, np.take_along_axis(arr1,
index_array, axis=axis) always yields the sorted 'a', irrespective of dimensionality.

Example 1: np.argsort()
1. import numpy as np
2. a=np.array([456,11,63])
3. a
4. b=np.argsort(a)
5. b

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o We have declared the variable 'b' and assigned the returned value of np.argsort()
function.
o We have passed the array 'a' in the function.
o Lastly, we tried to print the value of b.

In the output, a ndarray has been shown that contains the indices(indicate the position
of the element for the sorted array) and dtype.

Output:

array([456, 11, 63])


array([1, 2, 0], dtype=int64)

Example 2: For 2-D array( sorts along first axis (down))

1. import numpy as np
2. a = np.array([[0, 5], [3, 2]])
3. indices = np.argsort(a, axis=0)
4. indices

Output:

array([[0, 1],
[1, 0]], dtype=int64)

Example 3: For 2-D array(alternative of axis=0)

1. import numpy as np
2. a = np.array([[0, 5], [3, 2]])
3. indices = np.argsort(a, axis=0)
4. indices
5. np.take_along_axis(a, indices, axis=0)

In the above code

o We have imported numpy with alias name np.


o We have created a 2-D array 'a' using np.array() function.
o We have declared variable indices and assigned the returned value of np.argsort()
function.
o We have passed the 2-D array 'a' and axis as 0.
o Next, we used the take_along_axis() function and passed the source array, indices, and
axis.
o This function has returned the sorted 2-D array.

In the output, a 2-D array with sorted elements has been shown.

Output:

array([[0, 2],
[3, 5]])

Example 4: For 2-D array( sorts along last axis (across))

1. import numpy as np
2. a = np.array([[0, 5], [3, 2]])
3. indices = np.argsort(a, axis=1)
4. indices

Output:

array([[0, 1],
[1, 0]], dtype=int64)

Example 5: For 2-D array(alternative of axis=1)

1. import numpy as np
2. a = np.array([[0, 5], [3, 2]])
3. indices = np.argsort(a, axis=1)
4. indices
5. np.take_along_axis(a, indices, axis=1)

Output:
array([[0, 2],
[3, 5]])

Example 6: For N-D array

1. import numpy as np
2. a = np.array([[0, 5], [3, 2]])
3. indices = np.unravel_index(np.argsort(a, axis=None), a.shape)
4. indices
5. a[indices] # same as np.sort(a, axis=None)

Output:

(array([0, 1, 1, 0], dtype=int64), array([0, 1, 0, 1], dtype=int64))


array([0, 2, 3, 5])

In the above code

o We have imported numpy with alias name np.


o We have created a 2-D array 'a' using np.array() function.
o We have declared a variable 'indices' and assigned the returned value of
np.unravel_index() function.
o We have passed the np.argsort() function and shape of the array 'a'.
o We have passed the 2-D array 'a' and axis as 1 in argsort() function.
o Next, we tried to print the value of indices and a[indices].

In the output, an N-D array with sorted elements has been shown.

Example 7: Sorting with keys

1. import numpy as np
2. a= np.array([(0, 5), (3, 2)], dtype=[('x', '<i4'), ('y', '<i4')])
3. a
4. b=np.argsort(a, order=('x','y'))
5. b
6. c=np.argsort(a, order=('y','x'))
7. c

Output:

array([(0, 5), (3, 2)], dtype=[('x', '<i4'), ('y', '<i4')])


array([0, 1], dtype=int64)
array([1, 0], dtype=int64)

In the above code

o We have imported numpy with alias name np.


o We have created a 2-D array 'a' using np.array() function with dtype=[('x', '<i4'), ('y',
'<i4')].
o We have declared the variables 'b' and 'c' and assigned the returned value of
np.argsort() function.
o We have passed the array 'a' and order as an argument in the function.
o Lastly, we tried to print the value of 'b' and 'c'.

In the output, a sorted array has been shown with dtype=[('x', '<i4'), ('y', '<i4')]

numpy.transpose() in Python
The numpy.transpose() function is one of the most important functions in matrix
multiplication. This function permutes or reserves the dimension of the given array and
returns the modified array.

The numpy.transpose() function changes the row elements into column elements and
the column elements into row elements. The output of this function is a modified array
of the original one.

Syntax

1. numpy.transpose(arr, axis=None)

Parameters
arr: array_like

It is an ndarray. It is the source array whose elements we want to transpose. This


parameter is essential and plays a vital role in numpy.transpose() function.

axis: List of ints()

If we didn't specify the axis, then by default, it reverses the dimensions otherwise
permute the axis according to the given values.

Return
This function returns a ndarray. The output array is the source array, with its axis
permuted. A view is returned whenever possible.

Example 1: numpy.transpose()

1. import numpy as np
2. a= np.arange(6).reshape((2,3))
3. a
4. b=np.transpose(a)
5. b

Output:

array([[0, 1, 2],
[3, 4, 5]])
array([[0, 3],
[1, 4],
[2, 5]])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arange() function and gave a shape using
reshape() function.
o We have declared the variable 'b' and assigned the returned value of np.transpose()
function.
o We have passed the array 'a' in the function.
o Lastly, we tried to print the value of b.

In the output, the transposed array of the original array has been shown.

Example 2: numpy.transpose() with axis

1. import numpy as np
2. a= np.array([[1, 2], [4, 5], [7, 8]])
3. a
4. b=np.transpose(a, (1,0))
5. b

Output:

array([[1, 2],
[4, 5],
[7, 8]])
array([[1, 4, 7],
[2, 5, 8]])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.array() function.
o We have declared the variable 'b' and assigned the returned value of np.transpose()
function.
o We have passed the array 'a' and the axis in the function.
o Lastly, we tried to print the value of b.

In the output, the transposed array of the original array has been shown.

Example 3: Reposition elements using numpy.transpose()

1. import numpy as np
2. a=np.ones((12,32,123,64))
3. b=np.transpose(a,(1,3,0,2)).shape
4. b
5. c=np.transpose(a,(0,3,1,2)).shape
6. c

Output:

(32L, 64L, 12L, 123L)


(12L, 64L, 32L, 123L)

o We have imported numpy with alias name np.


o We have created an array 'a' using np.ones() function.
o We have declared the variable 'b' and 'c' and assigned the returned value of
np.transpose() function.
o We have passed the array 'a' and the positions of the array elements in the function.
o Lastly, we tried to print the value of b and c.

In the output, an array has been shown whose elements are located at the defined
position in the array.

numpy.mean() in Python
The sum of elements, along with an axis divided by the number of elements, is known
as arithmetic mean. The numpy.mean() function is used to compute the arithmetic
mean along the specified axis.

This function returns the average of the array elements. By default, the average is taken
on the flattened array. Else on the specified axis, float 64 is intermediate as well as
return values are used for integer inputs

Syntax

1. numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)

Parameters
These are the following parameters in numpy.mean() function:

a: array_like

This parameter defines the source array containing elements whose mean is desired.
In such a case where 'a' is not an array, a conversion is attempted.

axis: None, int or tuple of ints(optional)

This parameter defines the axis along which the means are computed. By default, the
mean is computed of the flattened array. In version 1.7.0, if this is a tuple of ints, the
mean is performed over multiple axes, instead of a single axis or all the axes as before.

dtype: data-type(optional)

This parameter is used to define the data-type used in computing the mean. For
integer inputs, the default is float64, and for floating-point inputs, it is the same as the
input dtype.

out: ndarray(optional)

This parameter defines an alternative output array in which the result will be placed.
The shape of the resulting array should be the same as the shape of the expected
output. The type of output values will cast when necessary.

keepdims: bool(optional)

When the value is true, the reduced axis is left as dimensions with size one in the
output/result. Also, the result broadcasts correctly against the input array. When the
default value is set, the keepdims does not pass via the mean method of sub-classes
of ndarray, but any non-default value will surely pass. In case the sub-class method
does not implement keepdims, then an exception will surely rise.

Return
If we set the 'out' parameter to None, this function returns a new array containing the
mean values. Otherwise, it will return the reference to the output array.

Example 1:

1. import numpy as np
2. a = np.array([[1, 2], [3, 4]])
3. b=np.mean(a)
4. b
5. x = np.array([[5, 6], [7, 34]])
6. y=np.mean(x)
7. y

Output:

2.5
13.0

In the above code

o We have imported numpy with alias name np.


o We have created two arrays 'a' and 'x' using np.array() function.
o We have declared the variable 'b' and 'y' and assigned the return value of np.zeros()
function.
o We have passed arrays 'a' and 'x' in the function.
o Lastly, we tried to print the value of 'b' and 'y'.

Example 2:

1. import numpy as np
2. a = np.array([[2, 4], [3, 5]])
3. b=np.mean(a,axis=0)
4. c=np.mean(a,axis=1)
5. b
6. c
Output:

array([2.5, 4.5])
array([3., 4.])

Example 3:
In single precision, mean can be inaccurate:

1. import numpy as np
2. a = np.zeros((2, 512*512), dtype=np.float32)
3. a[0, :] = 23.0
4. a[1, :] = 32.0
5. c=np.mean(a)
6. c

Output:

27.5

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.zeros() function with dtype float32.
o We have set the value of all the elements of 1st row to 23.0 and 2nd row 32.0.
o We have passed the array 'a' in the function and assigned the return value of the
np.mean() function.
o Lastly, we tried to print the value of 'c'.

In the output, it shows the mean of array 'a'.

Example 4:
Computing the mean in float64 is more accurate:

1. import numpy as np
2. a[0, :] = 2.0
3. a[1, :] = 0.2
4. c=np.mean(a)
5. c
6. d=np.mean(a, dtype=np.float64)
7. d

Output:

1.0999985
1.1000000014901161

numpy.unique() in Python
The numpy module of Python provides a function for finding unique elements in a
numpy array. The numpy.unique() function finds the unique elements of an array and
returns these unique elements as a sorted array. Apart from the unique elements, there
are some optional outputs also, which are as follows:

o The output can be the indices of the input array which give the unique values
o The output can be the indices of the unique array which reconstruct the input array
o The output can be an array of the number of times each unique value comes in the
input array.

Syntax

1. numpy.unique(a, return_index=False, return_inverse=False, return_counts=Fals


e, axis=None)

Parameters
These are the following parameters in numpy.mean() function:

a: array_like

This parameter defines the source array containing elements whose unique values are
desired. The array will be flattened if it is not 1-D array.

Return_index: bool(optional)

If this parameter is set True, the function will return the indices of the input array(along
the specified axis if provided or in the flattened array), which results in the unique array.

return_inverse: bool(optional)

If this parameter is set True, the function will also return the indices of the input
array(along the specified axis if provided or in the flattened array), which can be used
to reconstruct the input array.
Return_counts: bool(optional)

If this parameter is set True, the function will return the number of times each unique
item appeared in the input array 'a'.

axis: int or None(optional)

This parameter defines the axis to operate on. If this parameter is not set, then the
array 'a' will be flattened. If this parameter is an integer, then the subarrays indexed by
the given axis will be flattened and treated as an element of a 1-D array with the
dimension of the given axis. Structured arrays or object arrays that contain objects are
not supported if the axis 'kwarg' is used.

Returns
This function returns four types of outputs which are as follows:

unique: ndarray

In this output, a ndarray will be shown that contain sorted unique values.

unique_indices: ndarray(optional)

In this output, a ndarray will be shown that contains the indices of the first occurrences
of the unique values in the original array. This output is only provided if return_index
is True.

unique_inverse: ndarray(optional)

In this output, a ndarray will be shown that contains the indices to reconstruct the
original array from the unique array. This output is only provided if return_inverse is
True.

unique_counts: ndarray(optional)

In this output, a ndarray will be shown that contains the number of times each of the
unique values comes up in the original array. This output is only provided if
return_counts is True.

Example 1:

1. import numpy as np
2. a=np.unique([1,2,3,4,3,6,2,4])
3. a
Output:

array([1, 2, 3, 4, 6])

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'a' and assigned the returned value of np.unique()
function.
o We have passed the number of elements in the function.
o Lastly, we tried to print the value of 'a'.

In the output, a ndarray has been shown, which contains unique elements.

Example 2:

1. a=np.array([[1,2,2,3,9],[1,4,3,5,8]])
2. a
3. b=np.unique(a)
4. b

Output:

array([[1, 2, 2, 3, 9],
[1, 4, 3, 5, 8]])
array([1, 2, 3, 4, 5, 8, 9])

Example 3:

1. import numpy as np
2. a = np.array([[1, 1, 0], [1, 1, 0], [2, 3, 4],[5, 9, 8],[2, 3, 4]])
3. a
4. b=np.unique(a, axis=0)
5. b

Output:

array([[1, 1, 0],
[1, 1, 0],
[2, 3, 4],
[5, 9, 8],
[2, 3, 4]])
array([[1, 1, 0],
[2, 3, 4],
[5, 9, 8]])
In the above code

o We have imported numpy with alias name np.


o We have created a multi-dimensional array 'a'.
o We have declared the variable 'b' and assigned the returned value of np.unique()
function.
o We have passed the multi-dimensional array 'a' and axis as 0 in the function.
o Lastly, we tried to print the value of 'b'.

In the output, a ndarray has been shown that contains unique rows of the source array
'a'.

Example 4:

1. import numpy as np
2. a = np.array([[1, 1, 0], [1, 1, 0], [2, 2, 4],[5, 5, 8],[2, 2, 4]])
3. a
4. b=np.unique(a, axis=1)
5. b

Output:

array([[1, 1, 0],
[1, 1, 0],
[2, 2, 4],
[5, 5, 8],
[2, 2, 4]])
array([[0, 1],
[0, 1],
[4, 2],
[8, 5],
[4, 2]])
Note: When we set axis as 1 then this function returns the unique columns from the
source array.

Example 5: Use return_index

1. import numpy as np
2. a = np.array(['d', 'b', 'b', 'z', 'a'])
3. result, indices=np.unique(a,return_index=True)
4. result
5. indices
6. a[indices]
Output:

array(['a', 'b', 'd', 'z'], dtype='|S1')


array([4, 1, 0, 3], dtype=int64)
array(['a', 'b', 'd', 'z'], dtype='|S1')

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a'.
o We have declared the variable 'result' and 'indices' and assigned the returned value of
np.unique() function.
o We have passed the array 'a' and set return_index to True in the function.
o Lastly, we tried to print the value of 'result', 'indices', and array elements, which
indicates the indices('a [indices]').

In the output, a ndarray has been shown that contains the indices of the original array
that give unique values.

Example 6: Use return_inverse


We can reconstruct the input array from the unique values in the following way:

1. import numpy as np
2. a = np.array([1, 2, 6, 4, 5, 3, 2])
3. result, indices=np.unique(a,return_inverse=True)
4. result
5. indices
6. a[indices]

Output:

array([1, 2, 3, 4, 5, 6])
array([0, 1, 5, 3, 4, 2, 1], dtype=int64)
array([1, 2, 3, 4, 5, 6, 2])

numpy.ndarray.tolist() in Python
The numpy module provides a function numpy.ndarray.tolist(), used to convert the
data elements of an array into a list. This function returns the array as an a.ndim- levels
deep nested list of Python scalars.
In simple words, this function returns a copy of the array elements as a Python list. The
elements are converted to the nearest compatible built-in Python type through the
item function. When 'a.ndim' is 0, then the depth of the list is 0, and it will be a simple
Python scalar, not any list.

Syntax

1. ndarray.tolist()

Parameters
This function has no arguments or parameters.

Returns: y: object, or list of object, or list of object


This function returns the possibly nested list of array elements.
Note
We can re-create the array via a=np.array(a.tolist()), however it can sometimes lose
precision.

Example 1:
If we will use a.tolist() for a 1D array then it will be almost the same as list(a), except
that tolist converts numpy scalars to Python scalars.

1. import numpy as np
2. a = np.uint32([6, 2])
3. a
4. a_list=list(a)
5. a_list
6. type(a_list[0])
7. a_tolist=a.tolist()
8. a_tolist
9. type(a_tolist[0])

Output:

array([6, 2], dtype=uint32)


[6, 2]
<type 'numpy.uint32'>
[6L, 2L]
<type 'long'>

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.uint32() function.
o We have declared the variable 'a_list' and assigned the returned value of
the list() function.
o We tried to print the value of 'a', 'a_list', and type of a_list.
o We have declared the variable a_tolist and assigned the returned value
of ndarray.tolist().
o Lastly, we tried to print the type and the value of 'a_tolist'.

In the output, it shows a list and the type whose elements are transformed from the
source array.

Example 2:
For a 2-dimensional array, tolist is applied recursively.

1. import numpy as np
2. a = np.array([[11, 21], [31, 41]])
3. b=a.tolist()
4. a
5. b

Output:

array([[11, 21],
[31, 41]])
[[11, 21], [31, 41]]

In the above code

o We have imported numpy with alias name np.


o We have created a 2-dimensional array 'a' using the np.array() function.
o We have declared the variable 'b' and assigned the returned value
of a.tolist() function.
o Lastly, we tried to print the value of 'b'.

In the output, it shows a list whose elements are transformed from the source array.

Example 3:

1. import numpy as np
2. x = np.array(5)
3. list(x)
4. y=x.tolist()
5. y

Output:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
TypeError: iteration over a 0-d array
5

numpy.dot() in Python
The numpy module of Python provides a function to perform the dot product of two
arrays.

o If both the arrays 'a' and 'b' are 1-dimensional arrays, the dot() function performs the
inner product of vectors (without complex conjugation).
o If both the arrays 'a' and 'b' are 2-dimensional arrays, the dot() function performs the
matrix multiplication. But for matrix multiplication use of matmul or 'a' @ 'b' is
preferred.
o If either 'a' or 'b' is 0-dimensional (scalar), the dot() function performs multiplication.
Also, the use of numpy.multiply(a, b) or a *b method is preferred.
o If 'a' is an N-dimensional array and 'b' is a 1-dimensional array, then the dot() function
performs the sum-product over the last axis of a and b.
o If 'a' is an M-dimensional array and 'b' is an N-dimensional array (where N>=2), then
the dot() function performs the sum-product over the last axis of 'a' and the second-
to-last axis of 'b':

1. dot(a, b)[i,j,k,n] = sum(a[i,j,:] * b[k,:,n])

Syntax

1. numpy.dot(a, b, out=None)

Parameters
a: array_like

This parameter defines the first array.

b: array_like

This parameter defines the second array.

out: ndarray(optional)

It is an output argument. It should have the exact kind which would be returned in the
case when it was not used. Particularly, it should meet the performance feature, i.e., it
must contain the right type, i.e., it must be C-contiguous, and its dtype must be the
dtype that would be returned for dot(a,b). Thus, if it does not meet these specified
conditions, it raises an exception.

Returns
This function returns the dot product of 'a' and 'b'. This function returns a scalar if 'a'
and 'b' are both scalars or 1-dimensional; otherwise, it returns an array. If 'out' is given,
then it is returned.

Raises
The ValueError occurs when the last dimension of 'a' is not having the same size as
the second-to-last dimension of 'b'.

Example 1:

1. import numpy as np
2. a=np.dot(6,12)
3. a

Output:

72

Example 2:

1. import numpy as np
2. a=np.dot([2j, 3j], [5j, 8j])
3. a

Output:

(-34+0j)

Example 3:

1. import numpy as np
2. a = [[1, 2], [4, 1]]
3. b = [[4, 11], [2, 3]]
4. c=np.dot(a, b)
5. c

Output:

array([[ 8, 17],
[18, 47]])

In the above code


o We have imported numpy with alias name np.
o We have created two 2-dimensional arrays 'a' and 'b'.
o We have declared the variable 'c' and assigned the returned value of np.dot() function.

Lastly, we tried to print the value of 'c'.


o

In the output, it shows the matrix product as an array.

Example 4:

1. import numpy as np
2. x = np.arange(3*4*5*6).reshape((3,4,5,6))
3. y = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
4. p=np.dot(a, b)[2,3,2,1,2,2]
5. q=sum(a[2,3,2,:] * b[1,2,:,2])
6. p
7. q

Output:

499128
499128

In the above code

o We have imported numpy with alias name np.


o We have created two arrays 'a' and 'b' using np.arange() function and change the
shape of both the arrays using reshape() function.
o We have declared the variable 'c' and assigned the returned value of np.dot() function
o Lastly, we tried to print the 'c' value.

In the output, it shows the matrix product as an array.

numpy.loadtxt() in Python
The numpy module of Python provides a function to load data from a text file. The
numpy module provides loadtxt() function to be a fast reader for simple text files.
Note: In the text file, each row must have the same number of values.

Syntax

1. numpy.loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, c


onverters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)

Parameters
These are the following parameter in numpy.loadtxt() function:

fname: file, str, or pathlib.Path

This parameter defines the file, filename, or generator to read. Firstly, we will
decompose the file, if the filename extension is .gz and .bz2. After that the generators
will return byte strings for Python 3k.

dtype: data-type(optional)

This parameter defines the data type for the resulting array, and by default, the data
type will be the float. The resulting array will be 1-dimensional when it is a structured
data-type. Each row is interpreted as an array element, and the number of columns
used must match with the number of fields in the data-type.

comments: str or sequence(optional)

This parameter defines the characters or list of characters used for indicating the start
of the comment. By default, it will be '#'.

delimiter: str(optional)

This parameter defines the string used for separating values. By default, it will be any
whitespace.

converters: dict(optional)

This parameter defines a dictionary mapping column number to a function that will
convert the mapped column to the float. When column() is a date string
then converters={0:datestr2num}. This parameter is also used to provide a default
value for missing data as converters= {3: lambda s: float(s.strip() or 0)}.

skiprows: int(optional)

This parameter is used to skip the first 'skiprows', and by default, it will be 0.
usecols: int or sequence(optional)

This parameter defines the columns to read, with 0 being the first. For example,
usecols=(0, 3, 5) will extract the 1st, 4th, and 5th column. By default, its value is None,
which results in all columns being read. In the new version, we can use an integer
instead of a tuple if we want to read a single column.

unpack: bool(optional)

If this parameter is set to true, then the returned array is transposed, so that arguments
may be unpacked using x, y, z =loadtxt(...). The arrays are returned for each field
when using it with the structured data-type. By default, it will be set to False.

ndim: int(optional)

The returned array will have 'ndmin' dimensions. Otherwise, it will squeeze the mono-
dimensional axis. Legal values: 0 (default), 1 or 2.

Returns: out(ndarray)
It reads data from the text file in the form of a ndarray.

Example 1:

1. import numpy as np
2. from io import StringIO
3. c = StringIO(u"0 1\n2 3")
4. c
5. np.loadtxt(c)

Output:

<_io.StringIO object at 0x000000000A4C3E48>


array([[0., 1.],
[2., 3.]])

In the above code

o We have imported numpy with alias name np.


o We have also imported StringIO from io.
o We have declared the variable 'c' and assigned the returned value of the StringIO()
function.
o We have passed the unicode data in the function.
o Lastly, we tried to print the return value of np.loadtxt() in which we passed the file or
filename.

In the output, it shows the content of the file in the form of ndarray.

Example 2:

1. import numpy as np
2. from io import StringIO
3. d = StringIO(u"M 21 72\nF 35 58")
4. np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),'formats': ('S1', 'i4', 'f4')})

Output:

array([('M', 21, 72.), ('F', 35, 58.)], dtype=[('gender', 'S1'), ('age',


'<i4'), ('weight', '<f4')])

Example 3:

1. import numpy as np
2. from io import StringIO
3. c = StringIO(u"1,3,2\n3,5,4")
4. x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
5. x
6. y

Output:

array([1., 3.])
array([2., 4.])

In the above code

o We have imported numpy with alias name np.


o We have also imported StringIO from io.
o We have declared the variable 'c' and assigned the returned value of the StringIO()
function.
o We have passed the unicode data in the function.
o Lastly, we tried to print the return value of np.loadtxt in which we passed the file or
filename, set delimiter, usecols, and unpack to True.
In the output, it displays the content of the file has been shown in the form of ndarray.

numpy.clip() in Python
For clipping the values in an array, the numpy module of Python provides a function
called numpy.clip(). In the clip() function, we will pass the interval, and the values
which are outside the interval will be clipped for the interval edges.

If we specify an interval of [1, 2] then the values smaller than 1 become 1 and larger
than 2 is 2. This function is similar to numpy.maximum(x_min, numpy.maximum(x,
x_max)). But it is faster than np.maximum(). In numpy.clip(), there is no need to
perform check for ensuring x_min < x_max.

Syntax:

1. numpy.clip(a, a_min, a_max, out=None)

Parameters:
x: array_like

This parameter defines the source array whose elements we want to clip.

x_min: None, scalar, or array_like

This parameter defines the minimum value for clipping values. On the lower interval
edge, clipping is not required.

x_max: None, scalar, or array_like

This parameter defines the maximum value for clipping values. On the upper interval
edge, clipping is not required. The three arrays are broadcasted for matching their
shapes with x_min and x_max arrays. This will be done only when x_min and x_max are
array_like.

out: ndaaray(optional)

This parameter defines the ndarray in which the result will be stored. For in-place
clipping, this can be an input array. The data type of this 'out' arrays have the right
shape for holding the output.

Returns
clip_arr: ndarray
This function returns an array that contains the elements of 'x' but the values which are
less than the x_min, they get replaced with x_min, and those which are greater
than x_max, they get replaced with x_max.

Example 1:

1. import numpy as np
2. x= np.arange(12)
3. y=np.clip(x, 3, 10)
4. y

Output:

array([ 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 10])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using arange() function.
o We have declared the variable 'y' and assigned the returned value of clip() function.
o We have passed the array 'x', x_min, and x_max value in the function
o Lastly, we tried to print the value of 'y'.

In the output, a ndarray is shown, which contains elements ranging from 3 to 10.

Example 2:

1. import numpy as np
2. a = np.arange(12)
3. np.clip(a, 3, 9, out=a)
4. a

Output:

array([3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 9, 9])
array([3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 9, 9])

Example 3:

1. import numpy as np
2. a = np.arange(12)
3. np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4, 5, 6], 8)
Output:

array([3, 4, 3, 3, 4, 5, 6, 7, 8, 8, 8, 8])

numpy.ndarray.flatten() in Python
In Python, for some cases, we need a one-dimensional array rather than a 2-D or multi-
dimensional array. For this purpose, the numpy module provides a function
called numpy.ndarray.flatten(), which returns a copy of the array in one dimensional
rather than in 2-D or a multi-dimensional array.

Syntax

1. ndarray.flatten(order='C')

Parameters:
order: {'C', 'F', 'A', 'K'}(optional)

If we set the order parameter to 'C', it means that the array gets flattened in row-major
order. If 'F' is set, the array gets flattened in column-major order. The array is flattened
in column-major order only when 'a' is Fortran contiguous in memory, and when we
set the order parameter to 'A'. The last order is 'K', which flatten the array in same order
in which the elements occurred in the memory. By default, this parameter is set to 'C'.

Returns:
y: ndarray

This function returns a copy of the source array, which gets flattened into one-
dimensional.

Example 1:

1. import numpy as np
2. a = np.array([[1,4,7], [2,5,8],[3,6,9]])
3. b=a.flatten()
4. b

Output:

array([1, 4, 7, 2, 5, 8, 3, 6, 9])
In the above code

o We have imported numpy with alias name np.


o We have created a multi-dimensional array 'a' using array() function.
o We have declared the variable 'b' and assigned the returned value of flatten() function.
o Lastly, we tried to print the value of 'b'.

In the output, it shows a ndarray, which contains elements of the multi-dimensional


array into 1-D.

Example 2:

1. import numpy as np
2. a = np.array([[1,4,7], [2,5,8],[3,6,9]])
3. b=a.flatten('C')
4. b

Output:

array([1, 4, 7, 2, 5, 8, 3, 6, 9])

In the above code

o We have imported numpy with alias name np.


o We have created a multi-dimensional array 'a' using array() function.
o We have declared the variable 'b' and assigned the returned value of flatten() function.
o We have used 'C' order in the function.
o Lastly, we tried to print the value of 'b'.

In the output, it shows a ndarray, which contains elements of the multi-dimensional


array into 1-D.

Example 3:

1. import numpy as np
2. a = np.array([[1,4,7], [2,5,8],[3,6,9]])
3. b=a.flatten('F')
4. b

Output:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Example 4:

1. import numpy as np
2. a = np.array([[1,4,7], [2,5,8],[3,6,9]])
3. b=a.flatten('A')
4. b

Output:

array([1, 4, 7, 2, 5, 8, 3, 6, 9])

Example 5:

1. import numpy as np
2. a = np.array([[1,4,7], [2,5,8],[3,6,9]])
3. b=a.flatten('K')
4. b

Output:

array([1, 4, 7, 2, 5, 8, 3, 6, 9])

numpy.meshgrid() in Python
The numpy module of Python provides meshgrid() function for creating a rectangular
grid with the help of the given 1-D arrays that represent the Matrix
indexing or Cartesian indexing. MATLAB somewhat inspires the meshgrid() function.
From the coordinate vectors, the meshgrid() function returns the coordinate matrices.
In the above figure, the x-axis is ranging from -5 to 5, and the y-axis is ranging from -
5 to 5. So, there is a total of 121 points marked in the figure, each with x-coordinate
and y-coordinate. For any line parallel to the x-axis, the x-coordinates of the marked
points are -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, and 5 respectively. On the other hand, for any
line parallel to the y-axis, the y-coordinates of the marked points from bottom to top
are -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, and 5 respectively.

Syntax

1. numpy.meshgrid(*xi, **kwargs)

Parameters
x1, x2,…, xn : array_like

This parameter defines the 1-dimensional array, which represents the coordinates of a
grid.

indexing: {'xy', 'ij'}(optional)

This is an optional argument which defines the Cartesian 'xy'(by default) or matrix ('ij')
indexing of output.

sparse: bool(optional)

This parameter is also optional. If we need a sparse grid for conserving memory, we
have to set this parameter to True. By default, it is set to False.
copy: bool(optional)

The aim of this optional argument is that it returns a copy of the original array for
conserving memory. By default, it is set to False.

If both sparse and copy parameters are set to False, then it will return non-contiguous
arrays. In addition, more than one element of a broadcast array can refer to a single
memory location. If we need to write into the arrays, then we have to make copies first.

Returns
X1, X2, …, Xn

The coordinate length from the coordinate vector is returned from this function.

Example 1:

1. import numpy as np
2. na, nb = (5, 3)
3. a = np.linspace(1, 2, na)
4. b = np.linspace(1, 2, nb)
5. xa, xb = np.meshgrid(a, b)
6. xa
7. xb

Output:

array([[1. , 1.25, 1.5 , 1.75, 2. ],


[1. , 1.25, 1.5 , 1.75, 2. ],
[1. , 1.25, 1.5 , 1.75, 2. ]])
array([[1. , 1. , 1. , 1. , 1. ],
[1.5, 1.5, 1.5, 1.5, 1.5],
[2. , 2. , 2. , 2. , 2. ]])

In the above code

o We have imported numpy with alias name np.


o We have created two variables, i.e., na and nb, and assigned the values 5 and 3,
respectively.
o We have created two arrays, i.e., a and b using linspace() function.
o After that, we have declared the variables 'xa' and 'xb' and assigned the returned value
of meshgrid()
o We have passed both the arrays 'a' and 'b' in the function
o Lastly, we tried to print the value of 'xa' and 'xb'.

In the output, two arrays have been shown which contain the coordinate length from
the coordinate vectors.

Example 2:

1. import numpy as np
2. na, nb = (5, 3)
3. a = np.linspace(1, 2, na)
4. b = np.linspace(1, 2, nb)
5. xa, xb = np.meshgrid(a, b, sparse=True)
6. xa
7. xb

Output:

array([[1. , 1.25, 1.5 , 1.75, 2. ]])


array([[1. ],
[1.5],
[2. ]])

Example 3:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. a = np.arange(-10, 10, 0.1)
4. b = np.arange(-10, 10, 0.1)
5. xa, xb = np.meshgrid(a, b, sparse=True)
6. z = np.sin(xa**2 + xb**2) / (xa**2 + xb**2)
7. h = plt.contourf(a,b,z)
8. plt.show()

Output:
In the above code

o We have imported numpy with alias name np.


o We have imported matplotlib.pyplot as plt.
o We have created two arrays, i.e., a and b using np.arange() function.
o After that, we have declared the variables 'xa' and 'xb' and assigned the returned value
of meshgrid()
o We have passed both the arrays 'a' and 'b' in the function.
o After that, we have declared a variable z and assigned the return value of np.sine()
function.
o Lastly, we tried to draw contour lines and filled contours by using the plt.contourf()

In the output, contour lines have been plotted.

Example 4:
1. import numpy as np
2. import matplotlib.pyplot as plt
3. a = np.linspace(-5, 5, 5)
4. b = np.linspace(-5, 5, 11)
5. random_data = np.random.random((11, 5))
6. xa, xb = np.meshgrid(a, b)
7. plt.contourf(xa, xb, random_data, cmap = 'jet')
8. plt.colorbar()
9. plt.show()

Output:

Example 5:

1. import numpy as np
2. import matplotlib.pyplot as plt
3. a = np.linspace(-5, 5, 5)
4. b = np.linspace(-5, 5, 11)
5. random_data = np.random.random((11, 5))
6. xa, xb = np.meshgrid(a, b)
7. sine = (np.sin(xa**2 + xb**2))/(xa**2 + xb**2)
8. plt.contourf(xa, xb, sine, cmap = 'jet')
9. plt.colorbar()
10. plt.show()

Output:

numpy standard deviation


The numpy module of Python provides a function called numpy.std(), used to
compute the standard deviation along the specified axis. This function returns the
standard deviation of the array elements. The square root of the average square
deviation (computed from the mean), is known as the standard deviation. By default,
the standard deviation is calculated for the flattened array. With the help of
the x.sum()/N, the average square deviation is normally calculated, and here,
N=len(x).

Standard Deviation=sqrt(mean(abs(x-x.mean( ))**2

Syntax:

1. numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class


numpy._globals._NoValue>)

Parameters
a: array_like

This parameter defines the source array whose elements standard deviation is
calculated.

axis: None, int, or tuple of ints(optional)

It is the axis along which the standard deviation is calculated. The standard deviation
of the flattened array is computed by default. If it is a tuple of ints, performs standard
deviation over multiple axis instead of a single axis or all axis as before.

dtype : data_type(optional)

This parameter defines the data type, which is used in computing the standard
deviation. By default, the data type is float64 for integer type arrays, and, for float types
array, it will be the same as the array type.

out : ndarray(optional)

This parameter defines the alternative output array in which the result is to be placed.
This alternative ndarray has the same shape as the expected output. But we cast the
type when necessary.

ddof : int(optional)

This parameter defines the Delta Degrees of Freedom. The N-ddof divisor is used in
calculations, where N is the number of elements. By default, the value of this parameter
is set to 0.

keepdims : bool(optional)
It is optional, whose value, when true, will leave the reduced axis as dimensions with
size one in the resultant. When it passes the default value, it will allow the non-default
values to pass via the mean method of sub-classes of ndarray, but the keepdims will
not pass. Also, the output or the result will broadcast against the input array correctly.

Returns
This function will return a new array that contains the standard deviation. If we do not
set the 'out' parameter to None, it returns the output array's reference.

Example 1:

1. a=np.array([[1,4,7,10],[2,5,8,11]])
2. b=np.std(a)
3. b

Output:

3.391164991562634

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' via array() function.
o We have declared the variable 'b' and assigned the returned value of std() function.
o We have passed the array 'a' in the function
o Lastly, we tried to print the value of 'b'.

In the output, an array containing standard deviation has been shown.

Example 2:

1. a=np.array([[1,4,7,10],[2,5,8,11]])
2. b=np.std(a, axis=0)
3. b

Output:

array([0.5, 0.5, 0.5, 0.5])

Example 3:
1. a=np.array([[1,4,7,10],[2,5,8,11]])
2. b=np.std(a, axis=1)
3. b

Output:

array([3.35410197, 3.35410197])

Example 4:

1. import numpy as np
2. a = np.zeros((2, 512*512), dtype=np.float32)
3. a[1, :] = 1.0
4. a[0, :] = 0.1
5. b=np.std(a)
6. b

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.zeros() function with data type np.float32.
o We have assigned the value 0.1 to the elements of the 1st row and 1.0 to the elements
of the second row.
o We have passed the array 'a' in the function
o Lastly, we tried to print the value of 'b'.

In the output, the standard deviation has been shown, which can be inaccurate.

Output:

0.45000008

Example 5:

1. import numpy as np
2. a = np.zeros((2, 512*512), dtype=np.float32)
3. a[1, :] = 1.0
4. a[0, :] = 0.1
5. b=np.std(a ,dtype=np.float64))
6. b
Output:

0.4499999992549418

numpy.argmax in Python
In many cases, where the size of the array is too large, it takes too much time to find
the maximum elements from them. For this purpose, the numpy module of Python
provides a function called numpy.argmax(). This function returns indices of the
maximum values are returned along with the specified axis.

Syntax:

1. numpy.argmax(a, axis=None, out=None)

Parameters
x: array_like

This parameter defines the source array whose maximum value we want to know.
axis: int(optional)

This parameter defines the axis along which the index is present, and by default, it is
into the flattened array.

out: array(optional)

This parameter defines the ndarray in which the result is going to be inserted. This will
be of the same type and shape, which is appropriate for storing the result

Returns
This parameter defines a ndarray, which contains the indices of the array. The shape is
the same as x.shape with the dimension along the axis removed.

Example 1:

1. Import numpy as np
2. x = np.arange(20).reshape(4,5) + 7
3. x
4. y=np.argmax(a)
5. y

Output:

array([[ 7, 8, 9, 10, 11],


[12, 13, 14, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]])
19

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using np.arange() function with the shape of four rows
and five columns.
o We have added 7 in each element of the array also.
o We have declared the variable 'y' and assigned the returned value
of np.argmax() function.
o We have passed the array 'x' in the function.
o Lastly, we tried to print the value of 'y'.

In the output, it shows the indices of the maximum element in the array.
Example 2:

1. Import numpy as np
2. x = np.arange(20).reshape(4,5) + 7
3. y=np.argmax(x, axis=0)
4. z=np.argmax(x, axis=1)
5. y
6. z

Output:

array([3, 3, 3, 3, 3], dtype=int64)


array([4, 4, 4, 4], dtype=int64)

Example 3:

1. Import numpy as np
2. x = np.arange(20).reshape(4,5) + 7
3. indices = np.unravel_index(np.argmax(x, axis=None), x.shape)
4. indices
5. x[indices]

Output:

(3, 4)
26

Example 4:

1. import numpy as np
2. a = np.array([[5,2,1], [3,7,9],[0, 4, 6]])
3. index_arr = np.argmax(a, axis=-1)
4. index_arr
5. # Same as np.max(a, axis=-1, keepdims=True)
6. result = np.take_along_axis(a, np.expand_dims(index_arr, axis=-1), axis=-1)
7. result1
8. # Same as np.max(a, axis=-1)
9. result = np.take_along_axis(a, np.expand_dims(index_arr, axis=-1), axis=-
1).squeeze(axis=-1)
10. result2
Output:

array([[0],
[2],
[2]])
array([5, 9, 6])

In the above code

o We have imported numpy with alias name np.


o We have created a multi-dimensional array 'a' using np.array() function.
o We have declared the variable 'index_arr' and assigned the returned value
of np.argmax() function.
o We have passed the array 'a' and the axis in the function.
o We tried to print the value of 'index_arr'.
o In the end, we tried to fetch the maximum value of the array with the help of two
different ways, which are quite similar to np.argmax().

In the output, it shows indices of the maximum elements in the array and the values
which are present on that indices.

numpy.diff() in Python
The numpy module of Python provides a function called numpy.diff for calculating
the nth discrete difference along the given axis. If 'x' is the input array, then the first
difference is given by out[i]=x[i+1]-a[i]. We can calculate the higher difference by using
diff recursively. The numpy module of Python provides a function called numpy.diff for
calculating the nth discrete difference along the given axis. If 'x' is the input array, then
the first difference is given by out[i]=x[i+1]-a[i]. We can calculate the higher difference
by using diff recursively.

Syntax

1. numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)

Parameters
x: array_like

This parameter defines the source array whose elements nth discrete deference are
those which we want to calculate.
n: int(optional)

This parameter defines the number of times the values are differenced. If it is 0, then
the source array is returned as it is.

append, prepend: array_like(optional)

This parameter defines a ndarray, which defines the values going to append or
prepend to 'x', along the axis before computing differences.

Returns:
This function returns a ndarray containing nth differences having the same shape
as 'x,' and the dimension is smaller from n. The type of difference between any two
elements of 'x' is the type of the output.

Example 1:

1. import numpy as np
2. arr = np.array([0, 1, 2], dtype=np.uint8)
3. arr
4. b=np.diff(arr)
5. b
6. arr[2,...] - arr[1,...] - arr[0,...]

Output:

array([0, 1, 2], dtype=uint8)


array([1, 1], dtype=uint8)
1

In the above code

o We have imported numpy with alias name np.


o We have created an array 'arr' using np.array() function with the dtype 'uint8'.
o We have declared the variable 'b' and assigned the returned value of
the np.diff() function.
o We have passed the array 'arr' in the function.
o Lastly, we tried to print the value of 'b' and the difference between elements.

In the output, it shows the discrete differences of elements.


Example 2:

1. import numpy as np
2. x = np.array([11, 21, 41, 71, 1, 12, 33, 2])
3. y = np.diff(x)
4. x
5. y

Output:

array([11, 21, 41, 71, 1, 12, 33, 2])


array([ 10, 20, 30, -70, 11, 21, -31])

Example 3:

1. import numpy as np
2. x = np.array([[11, 21, 41], [71, 1, 12], [33, 2, 13]])
3. y = np.diff(x, axis=0)
4. y
5. z = np.diff(x, axis=1)
6. z

Output:

array([[ 60, -20, -29],


[-38, 1, 1]])
array([[ 10, 20],
[-70, 11],
[-31, 11]])

Example 4:

1. import numpy as np
2. x = np.arange('1997-10-01', '1997-12-16', dtype=np.datetime64)
3. y = np.diff(x)
4. y

Output:

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1], dtype='timedelta64[D]')

In the above code


o We have imported numpy with alias name np.
o We have created an array of dates 'x' using np.arange() function with the
dtype 'datetime64'.
o We have declared the variable 'y' and assigned the returned value of
the np.diff() function.
o We have passed the array 'x' in the function.
o Lastly, we tried to print the value of 'y'.

In the output, it shows the discrete differences between dates.

numpy.empty() in Python
The numpy module of Python provides a function called numpy.empty(). This
function is used to create an array without initializing the entries of given shape and
type.

Just like numpy.zeros(), the numpy.empty() function doesn't set the array values to
zero, and it is quite faster than the numpy.zeros(). This function requires the user to
set all the values in the array manually and should be used with caution.

Syntax

1. numpy.empty(shape, dtype=float, order='C')

Parameters:
shape: int or tuple of ints

This parameter defines the shape of the empty array, such as (3, 2) or (3, 3).

dtype: data-type(optional)

This parameter defines the data type, which is desired for the output array.

order: {'C', 'F'}(optional)

This parameter defines the order in which the multi-dimensional array is going to be
stored either in row-major or column-major. By default, the order parameter is set
to 'C'.

Returns:
This function returns the array of uninitialized data that have the shape, dtype, and
order defined in the function.

Example 1:

1. import numpy as np
2. x = np.empty([3, 2])
3. x

Output:

array([[7.56544226e-316, 2.07617768e-316],
[2.02322570e-316, 1.93432036e-316],
[1.93431918e-316, 1.93431799e-316]])

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'x' and assigned the returned value of
the np.empty() function.
o We have passed the shape in the function.
o Lastly, we tried to print the value of 'x' and the difference between elements.

Example 2:

1. import numpy as np
2. x = np.empty([3, 3], dtype=float)
3. x

Output:

array([[ 2.94197848e+120, -2.70534020e+252, -4.25371363e+003],


[ 1.44429964e-088, 3.12897830e-053, 1.11313317e+253],
[-2.28920735e+294, -5.11507284e+039, 0.00000000e+000]])

Example 3:

1. import numpy as np
2. x = np.empty([3, 3], dtype=float, order='C')
3. x

Output:

array([[ 2.94197848e+120, -2.70534020e+252, -4.25371363e+003],


[ 1.44429964e-088, 3.12897830e-053, 1.11313317e+253],
[-2.28920735e+294, -5.11507284e+039, 0.00000000e+000]])

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'x' and assigned the returned value of
the np.empty() function.
o We have passed the shape, data-type, and order in the function.
o Lastly, we tried to print the value of 'x' and the difference between elements.

In the output, it shows an array of uninitialized values of defined shape, data type, and
order.

Example 4:

1. import numpy as np
2. x = np.empty([3, 3], dtype=float, order='F')
3. x

Output:

array([[ 2.94197848e+120, 1.44429964e-088, -2.28920735e+294],


[-2.70534020e+252, 3.12897830e-053, -5.11507284e+039],
[-4.25371363e+003, 1.11313317e+253, 0.00000000e+000]])

numpy.histogram() in Python
The numpy module of Python provides a function called numpy.histogram(). This
function represents the frequency of the number of values that are compared with a
set of values ranges. This function is similar to the hist() function
of matplotlib.pyplot.

In simple words, this function is used to compute the histogram of the set of data.

Syntax:

1. numpy.histogram(x, bins=10, range=None, normed=None, weights=None, de


nsity=None)

Parameters:
x: array_like
This parameter defines a flattened array over which the histogram is computed.

bins: int or sequence of str or scalars(optional)

If this parameter is defined as an integer, then in the given range, it defines the number
of equal-width bins. Otherwise, an array of bin edges which monotonically increased
is defined. It also includes the rightmost edge, which allows for non-uniform bin
widths. The latest version of numpy allows us to set bin parameters as a string, which
defines a method for calculating optimal bin width.

range : (float, float)(optional)

This parameter defines the lower-upper ranges of the bins. By default, the range
is (x.min(), x.max()). The values are ignored, which are outside the range. The ranges
of the first element should be equal to or less than the second element.

normed : bool(optional)

This parameter is the same as the density argument, but it can give the wrong output
for unequal bin widths.

weights : array_like(optional)

This parameter defines an array which contains weights and has the same shape as 'x'.

density : bool(optional)

If it is set to True, will result in the number of samples in every bin. If its value is False,
the density function will result in the value of the probability density function in the
bin.

Returns:
hist: array

The density function returns the values of the histogram.

edge_bin: an array of float dtype

This function returns the bin edges (length(hist+1)).

Example 1:

1. import numpy as np
2. a=np.histogram([1, 5, 2], bins=[0, 1, 2, 3])
3. a

Output:

(array([0, 1, 1], dtype=int64), array([0, 1, 2, 3]))

In the above code

o We have imported numpy with alias name np.


o We have declared the variable 'a' and assigned the returned value
of np.histogram() function.
o We have passed an array and the value of the bin in the function.
o Lastly, we tried to print the value of 'a'.

In the output, it shows a ndarray that contain the values of the histogram.

Example 2:

1. import numpy as np
2. x=np.histogram(np.arange(6), bins=np.arange(7), density=True)
3. x

Output:

(array([0.16666667, 0.16666667, 0.16666667, 0.16666667, 0.16666667,


0.16666667]), array([0, 1, 2, 3, 4, 5, 6]))

Example 3:

1. import numpy as np
2. x=np.histogram([[1, 3, 1], [1, 3, 1]], bins=[0,1,2,3])
3. x

Output:

(array([0, 4, 2], dtype=int64), array([0, 1, 2, 3]))

Example 4:

1. import numpy as np
2. a = np.arange(8)
3. hist, bin_edges = np.histogram(a, density=True)
4. hist
5. bin_edges

Output:

array([0.17857143, 0.17857143, 0.17857143, 0. , 0.17857143,


0.17857143, 0. , 0.17857143, 0.17857143, 0.17857143])
array([0. , 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3, 7. ])

Example 5:

1. import numpy as np
2. a = np.arange(8)
3. hist, bin_edges = np.histogram(a, density=True)
4. hist
5. hist.sum()
6. np.sum(hist * np.diff(bin_edges))

Output:

array([0.17857143, 0.17857143, 0.17857143, 0. , 0.17857143,


0.17857143, 0. , 0.17857143, 0.17857143, 0.17857143])
1.4285714285714288
1.0

In the above code

o We have imported numpy with alias name np.


o We have created an array 'a' using np.arange() function.
o We have declared variables 'hist' and 'bin_edges' and then assigned the returned
value of np.histogram() function.
o We have passed the array 'a' and set 'density' to True in the function.
o We tried to print the value of 'hist'.
o And lastly, we tried to calculate the sum of histogram values
using hist.sum() and np.sum() in which we passed histogram values and edges of the
bin.

In the output, it shows a ndarray that contains the values of the histogram and the
sum of histogram values.

numpy.sort in Python
In some cases, we require a sorted array for computation. For this purpose, the numpy
module of Python provides a function called numpy.sort(). This function gives a
sorted copy of the source array or input array.

Syntax:

1. numpy.sort(a, axis=-1, kind='quicksort', order=None)

Parameters:
x: array_like

This parameter defines the source array, which is going to be sorted.

axis: int or None(optional)

This parameter defines the axis along which the sorting is performed. If this parameter
is None, the array will be flattened before sorting, and by default, this parameter is set
to -1, which sorts the array along the last axis.
kind: {quicksort, heapsort, mergesort}(optional)

This parameter is used to define the sorting algorithm, and by default, the sorting is
performed using 'quicksort'.

order: str or list of str(optional)

When an array is defined with fields, its order defines the fields for making a
comparison in first, second, etc. Only the single field can be specified as a string, and
not necessarily for all fields. However, the unspecified fields will still be used, in the
order in which they come up in the dtype, to break the ties.

Returns:
This function returns a sorted copy of the source array, which will have the same shape
and type as a source array.

Example 1:

1. import numpy as np
2. x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
3. x
4. y=np.sort(x)
5. y

Output:

array([[ 1, 4, 2, 3],
[ 9, 13, 61, 1],
[43, 24, 88, 22]])
array([[ 1, 2, 3, 4],
[ 1, 9, 13, 61],
[22, 24, 43, 88]])

In the above code

o We have imported numpy with alias name np.


o We have created a multi-dimensional array 'x' using np.array() function.
o We have declared the variable 'y' and assigned the returned value
of np.sort() function.
o We have passed the input array 'x' in the function.
o Lastly, we tried to print the value of 'y'.
In the output, it shows a sorted copy of the source array of the same type and shape.

Example 2:

1. import numpy as np
2. x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
3. x
4. y=np.sort(x, axis=None)
5. y

Output:

array([[ 1, 4, 2, 3],
[ 9, 13, 61, 1],
[43, 24, 88, 22]])
array([ 1, 1, 2, 3, 4, 9, 13, 22, 24, 43, 61, 88])

Example 3:

1. import numpy as np
2. x=np.array([[1,4,2,3],[9,13,61,1],[43,24,88,22]])
3. x
4. y=np.sort(x,axis=0)
5. y
6. z=np.sort(x,axis=1)
7. z

Output:

array([[ 1, 4, 2, 1],
[ 9, 13, 61, 3],
[43, 24, 88, 22]])
array([[ 1, 2, 3, 4],
[ 1, 9, 13, 61],
[22, 24, 43, 88]])

Example 4:

1. import numpy as np
2. dtype = [('name', 'S10'), ('height', float), ('age', int),('gender','S10')]
3. values = [('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'),('Vaishali', 5.2, 30, 'F')]
4. x=np.array(values, dtype=dtype)
5. x
6. y=np.sort(x, order='age')
7. y
8. z=np.sort(x, order=['age','height'])
9. z

Output:

array([('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'), ('Vaishali', 5.2,
30, 'F')],dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'),
('gender', 'S10')])
array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2,
30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'),
('gender', 'S10')])
array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2,
30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'),
('gender', 'S10')])

In the above code

o We have imported numpy with alias name np.


o We have defined the fields and values for the structured array.
o We have created a structured array 'x' by passing dtype and values in
the np.array() function.
o We have declared the variables 'y' and 'z', and assigned the returned value
of np.sort() function.
o We have passed the input array 'x' and order in the function.
o Lastly, we tried to print the value of 'y' and 'z'.

In the output, it shows a sorted copy of the structured array with a defined order.

numpy.average() in Python
The numpy module of Python provides a function called numpy.average(), used for
calculating the weighted average along the specified axis.

Syntax:

1. numpy.average(a, axis=None, weights=None, returned=False)

Parameters:
x: array_like
This parameter defines the source array whose element's average we want to calculate.
The conversion will be attempted if 'x' is an array.

axis: int or None or tuple of ints(optional)

This parameter defines the axis along which the average is going to be calculated. By
default, the axis is set to None, which will calculate the average of all the elements of
the source array. Counts start from the ending to the starting axis when the value of
the axis is negative.

weights : array_like(optional)

This parameter defines an array containing weights associated with the array values.
Each value of array elements together makes the average according to its associated
weight. The weighted array can be one-dimensional or of the same shape as the input
array. When there is no weight associated with the array element, the weight will be
treated as 1 for all elements.

returned: bool(optional)

By default, this parameter is set to False. If we set it as True, then a tuple of average
and sum_of_weights, is returned. If it is False, the average is returned. The weighted
sum is equivalent to the number of elements if there are no values for weights.

Returns:
retval, [sum_of_weights]: array_type or double

This function returns either the average or both the average and the sum_of_weights
that depend on the returned parameter.

Raises:
ZeroDivisionError

This error is raised when all weights along the axis are set to zero.

TypeError

This error is raised when the length of the weighted array is not the same as the shape
of the input array.

Example 1:

1. import numpy as np
2. data = list(range(1,6))
3. output=np.average(data)
4. data
5. output

Output:

[1, 2, 3, 4, 5]
3.0

In the above code:

o We have imported numpy with alias name np.


o We have created a list of elements 'data'.
o We have declared the variable 'output' and assigned the returned value
of average() function.
o We have passed the list 'data' in the function.
o Lastly, we tried to print the 'data' and 'output'

In the output, it shows the average of the list elements.

Example 2:

1. import numpy as np
2. output=np.average(range(1,16), weights=range(15,0,-1))
3. output

Output:

5.666666666666667

Example 3:

1. import numpy as np
2. data=np.arange(12).reshape((4,3))
3. output = np.average(data, axis=1, weights=[1./4, 3./4, 5./4])
4. data
5. output

Output:

array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
array([ 1.44444444, 4.44444444, 7.44444444, 10.44444444])

In the above code:

o We have imported numpy with alias name np.


o We have created an array 'data' using arange() and np.reshape() function.
o We have declared the variable 'output' and assigned the returned value
of average() function.
o We have passed the array 'data', set axis to 1, and weighted array in the function.
o Lastly, we tried to print the 'data' and 'output'

In the output, it shows the average of each column elements in the array.

Example 4:

1. import numpy as np
2. data=np.arange(12).reshape((4,3))
3. data
4. np.average(data, weights=[1./4, 3./4, 5./4])

Output:

array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line
406, in average
"Axis must be specified when shapes of data and weights."
TypeError: Axis must be specified when shapes of data and weight

numpy.pad() in Python
The numpy module of Python provides a function called numpy.pad() to perform
padding in the array. This function has several required and optional parameters.

Syntax:

1. numpy.pad(array, pad_width, mode='constant', **kwargs)


Parameters:
array: array_like

This is the source array which we want to pad.

pad_width: int, sequence, or array_like

This parameter defines the number of values that are padded to the edges of each
axis. The unique pad widths for each axis are defined as (before_1, after_1), (before_2,
after_2), ... (before_N, after_N)). For each axis, ((before, after),) will be treated as same
as before and after pad. For all axes, the int, or (pad,) is a shortcut to before = after =
pad width.

mode: str or function(optional)

This parameter has one of the following string values:

'constant'(Default)

If we assign a constant value to the mode parameter, padding will be done with a
constant value.

'edge'

It is the edge value of the array. The padding will be done with this edge value.

'linear_ramp'

This value is used to perform padding with the linear ramp between the edge value
and the end value.

'maximum'

This parameter value performs padding by using the max value of a vector part or all,
along each axis.

'mean'

This parameter value performs padding via the mean value of a vector part or all, along
each axis.

'median'
This parameter value performs padding via the median value of a vector part or all,
along each axis.

'minimum'

This parameter value performs padding via the min value of a vector part or all, along
each axis.

'reflect'

This value pads the array via vector reflection, which is mirrored on the starting and
ending vector values, along each axis.

'symmetric'

This value is used to pad the array via vector reflection, which is mirrored along the
edge of the array.

'wrap'

This value is used to perform padding of the array via the wrap of the vector along the
axis. The starting values are used for padding the end, and the ending values pad the
beginning.

'empty'

This value is used to pad the array with undefined values.

stat_length: int or sequence(optional)

This parameter is used in 'maximum', 'minimum', 'mean', 'median'. It defines the


number of values at each edge axis, used for calculating the static value.

constant_values: scalar or sequence(optional)

This parameter is used in 'constant'. It defines the values for setting the padded values
to each axis.

end_values: scalar or sequence(optional)

This parameter is used in 'linear_ramp'. It defines the values which are used for the last
value of the linear_ramp and will form the edge of the padded array.

reflect_type: even or odd(optional)


This parameter is used in 'symmetric' and 'reflect'. By default, the reflect_type is 'even'
with an unaltered reflection around the edge value. By subtracting the reflected values
from two times the edge value, the array's extended part is created for the 'odd' style.

Returns:
pad: ndarray

This function returns the padded array of rank equal to the array, whose shape increase
according to pad_width.

Example 1:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3, 2), 'constant', constant_values=(6, 4))
4. y

Output:

array([6, 6, 6, 1, 3, 2, 5, 4, 4, 4])

In the above code

o We have imported numpy with alias name np.


o We have created a list of values x.
o We have declared the variable y and assigned the returned value of np.pad() function.
o We have passed the list x, pad_width, set the mode
to constant and constant_values in the function.
o Lastly, we tried to print the value of y.

In the output, it shows a ndarray padded with the defined size and values.

Example 2:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3, 2), 'edge')
4. y

Output:
array([1, 1, 1, 1, 3, 2, 5, 4, 4, 4])

Example 3:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3, 2), 'linear_ramp', end_values=(-4, 5))
4. y

Output:

array([-4, -2, 0, 1, 3, 2, 5, 4, 4, 5])

Example 4:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3,), 'maximum')
4. y

Output:

array([5, 5, 5, 1, 3, 2, 5, 4, 5, 5, 5])

Example 5:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3,), 'mean')
4. y

Output:

array([3, 3, 3, 1, 3, 2, 5, 4, 3, 3, 3])

Example 6:

1. import numpy as np
2. x = [1, 3, 2, 5, 4]
3. y = np.pad(x, (3,), 'median')
4. y

Output:
array([3, 3, 3, 1, 3, 2, 5, 4, 3, 3, 3])

Example 7:

1. import numpy as np
2. a = [[1, 2], [3, 4]]
3. y = np.pad(x, (3,), 'minimum')
4. y

Output:

array([[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[3, 3, 3, 3, 4, 3, 3],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1]])

Example 8:

1. import numpy as np
2. def pad_with(vector, pad_width, iaxis, kwargs):
3. padding_value = kwargs.get('padder', 10)
4. vector[:pad_width[0]] = padding_value
5. vector[-pad_width[1]:] = padding_value
6. x = np.arange(6)
7. x = x.reshape((3, 2))
8. y = np.pad(x, 3, pad_with)
9. y

Output:

array([41, 31, 21, 11, 21, 31, 41, 51, 41, 31])

In the above code

o We have imported numpy with alias name np.


o We have created a function pad_with with vector, pad_width, iaxis, and kwargs.
o We have declared the variable pad_value to get padding values from
the get() function.
o We have passed the padding values to the part of the vector.
o We have created an array x using np.arange() function and changed the shape using
the reshape() function.
o We have declared a variable y and assigned the returned value of the np.pad() function.
o We have passed the list x and pad_width in the function
o Lastly, we tried to print the value of y.

In the output, it shows a ndarray padded with the defined size and values.

Example 9:

1. import numpy as np
2. import numpy as np
3. def pad_with(vector, pad_width, iaxis, kwargs):
4. padding_value = kwargs.get('padder', 10)
5. vector[:pad_width[0]] = padding_value
6. vector[-pad_width[1]:] = padding_value
7. x = np.arange(6)
8. x = x.reshape((3, 2))
9. np.pad(x, 3, pad_with)

Output:

array([[10, 10, 10, 10, 10, 10, 10, 10],


[10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 0, 1, 10, 10, 10],
[10, 10, 10, 2, 3, 10, 10, 10],
[10, 10, 10, 4, 5, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10]])

Example 10:

1. import numpy as np
2. import numpy as np
3. def pad_with(vector, pad_width, iaxis, kwargs):
4. ... pad_value = kwargs.get('padder', 10)
5. ... vector[:pad_width[0]] = pad_value
6. ... vector[-pad_width[1]:] = pad_value
7. x = np.arange(6)
8. x = x.reshape((3, 2))
9. np.pad(x, 3, pad_with, padder=100)

Output:

array([[100, 100, 100, 100, 100, 100, 100, 100],


[100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 0, 1, 100, 100, 100],
[100, 100, 100, 2, 3, 100, 100, 100],
[100, 100, 100, 4, 5, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100, 100]])

numpy.ravel() in Python
The numpy module of Python provides a function called numpy.ravel, which is used to
change a 2-dimensional array or a multi-dimensional array into a contiguous flattened
array. The returned array has the same data type as the source array or input array. If
the input array is a masked array, the returned array will also be a masked array.

Syntax:

1. numpy.ravel(x, order='C')

Parameters:
x: array_like

This parameter defines the input array, which we want to change in a contiguous
flattened array. The array elements are read in the order specified by the order
parameter and packed as a 1-D array.

order: {'C','F', 'A', 'K'}(optional)

If we set the order parameter to 'C', it means that the array gets flattened in row-major
order. If 'F' is set, the array gets flattened in column-major order. The array is flattened
in column-major order only when 'A' is Fortran contiguous in memory, and when we
set the order parameter to 'A'. The last order is 'K', which flatten the array in same order
in which the elements occurred in the memory. By default, this parameter is set to 'C'.

Returns:
This function returns a contiguous flatten array with the same data type as an input
array and has shape equal to (x.size).
Example 1:

1. import numpy as np
2. x = np.array([[1, 3, 5], [11, 35, 56]])
3. y=np.ravel(x)
4. y

Output:

array([ 1, 3, 5, 11, 35, 56])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using np.array() function.
o We have declared variable y and assigned the returned value of np.ravel() function.
o We have passed the array 'x' in the function.
o Lastly, we tried to print the value of y.

In the output, the values of the array are shown in a contiguous flattened array.

Example 2:

1. import numpy as np
2. x = np.array([[1, 3, 5], [11, 35, 56]])
3. y = np.ravel(x, order='F')
4. z = np.ravel(x, order='C')
5. p = np.ravel(x, order='A')
6. q = np.ravel(x, order='K')
7. y
8. z
9. p
10. q

Output:

array([ 1, 11, 3, 35, 5, 56])


array([ 1, 3, 5, 11, 35, 56])
array([ 1, 3, 5, 11, 35, 56])
array([ 1, 3, 5, 11, 35, 56])
Example 3:

1. import numpy as np
2. x = np.arange(12).reshape(3,2,2).swapaxes(1,2)
3. x
4. y=np.ravel(a, order='C')
5. y
6. z=np.ravel(a, order='K')
7. z
8. q=np.ravel(a, order='A')
9. q

Output:

array([[[ 0, 2],
[ 1, 3]],

[[ 4, 6],
[ 5, 7]],

[[ 8, 10],
[ 9, 11]]])
array([ 0, 2, 1, 3, 4, 6, 5, 7, 8, 10, 9, 11])
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
array([ 0, 2, 1, 3, 4, 6, 5, 7, 8, 10, 9, 11])

In the above code

o We have imported numpy with alias name np.


o We have created an array 'x' using np.arange() function.
o We have changed its shape and swapped the axis using
the reshape() and np.swapaxes() function.
o We have declared the variables y, z, and q and assigned the returned value of np.ravel()
function.
o We have passed the array 'x' and order C, K, and A in the function.
o Lastly, we tried to print the value of y.

In the output, the values of the array are shown in a contiguous flattened array.

numpy.save() in Python
The numpy module of Python provides a function called numpy.save() to save an array
into a binary file in .npy format. In many of the cases, we require data in binary format
to manipulate it.

Syntax:

1. numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameters:
file: str, file, or pathlib.path

This parameter defines the file or filename in which the data is going to be saved. If
this parameter is an object of a file, the filename will be unchanged. If
the file parameter is a path or a string, the .npy extension will be added to the file
name, and it will be done when it doesn't have one.

allow_pickle : bool(optional)

History of Java

This parameter is used to allow the saving of objects into the pickle. The security and
the probability are the reason for disallowing pickles.

fix_imports : bool(optional)

If fix_imports is set to True, then pickle does the mapping of the new Python3 names
to the old module names, which are used in Python2. This makes the pickle data stream
readable with Python2.

Example 1:

1. import numpy as np
2. from tempfile import TemporaryFile
3. out_file = TemporaryFile()
4. x=np.arange(15)
5. np.save(out_file, x)
6. _=out_file.seek(0) # Only needed here to simulate closing & reopening file
7. np.load(outfile)

Output:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])


In the above code:

o We have imported numpy with alias name np.


o We have also imported TemporaryFile from tempfile.
o We have created an object out_file of TemporaryFile.
o We have created an array 'x' using arange() function.
o We have saved the array's elements as binary in npy file using np.save() function.
o We have passed the array 'x' and filename in the function.
o We have closed and reopened the file using seek(0) function.
o Lastly, we tried to load the out_file.

In the output, an array has been shown which contain elements present in
the out_file.npy.

Example 2:

1. import numpy as np
2. from tempfile import TemporaryFile
3. outfile = TemporaryFile()
4. x=np.arange(15)
5. np.save(outfile, x, allow_pickle=False)
6. _=outfile.seek(0) # Only needed here to simulate closing & reopening file
7. np.load(outfile)

Output:

array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])

Numpy arccos() method


This function is used to calculate the inverse cos of the array elements.

Syntax

1. numpy.arccos(array, out)

Parameters
1. array: these are the array elements of which, the inverse cos values are to be calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing the inverse cos for all the array elements, x.

Example

1. import numpy as np
2.
3. arr = [0, 0.3, -1]
4. print ("Input array : \n", arr)
5.
6. arccos_val = np.arccos(arr)
7. print ("\nInverse cos values : \n", arccos_val)

Output:

Input array :
[0, 0.3, -1]

Inverse cos values :


[1.57079633 1.26610367 3.14159265]

Numpy arcsin() method


This function is used to calculate the inverse sin of the array elements.

Syntax

1. numpy.arcsin(array, out)

Parameters

1. array: these are the array elements of which, the inverse sin values are to be calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing the inverse sin for all the array elements, x.
Example

1. import numpy as np
2. import math
3.
4. arr = [0, 0.3, -1]
5. print ("Input array : \n", arr)
6.
7. arcsine = np.arcsin(arr)
8. print ("\nInverse Sine values : \n", arcsine)

Output:

Input array :
[0, 0.3, -1]

Inverse Sine values :


[ 0. 0.30469265 -1.57079633]

Numpy arctan() method


This function is used to calculate the inverse tangent of the array elements.

Syntax

1. numpy.arcttan(array, out)

Parameters

1. array: these are the array elements of which, the inverse tangent values are to be
calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing the inverse tangent values for all the array elements, x.

Example

1. import numpy as np
2.
3. arr = [0, 0.3, -1]
4. print ("Input array : \n", arr)
5.
6. arctan_val = np.arctan(arr)
7. print ("\nInverse tan values : \n", arctan_val)

Output:

Input array :
[0, 0.3, -1]

Inverse tan values :


[ 0. 0.29145679 -0.78539816]

Numpy degrees() method


This function is used to convert the angles from radian to degrees.

Syntax

1. numpy.degrees(array, out)

Parameters

1. array: these are the angles whose degree values are to be calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing equivalent degree angles of the radians given in the input
array.

Example
1. import numpy as np
2. import math
3.
4. arr = [0, math.pi/2, math.pi/4, math.pi/6 ]
5. print ("Input array : \n", arr)
6.
7. degval = np.degrees(arr)
8. print ("\n Degree value : \n", degval)

Output:

Input array :
[0, 1.5707963267948966, 0.7853981633974483, 0.5235987755982988]

Degree value :
[ 0. 90. 45. 30.]

Numpy tan() method


This function is used to calculate the trigonometric tangent for all the elements of the
array passed as the argument.

Syntax

1. numpy.tan(array[,out] )

Parameters

1. array: Array elements whose tangent values are to be calculated.


2. out: shape of the output array.

Return
An array with trigonometric tangent sins are returned.

Example

1. import numpy as np
2. import math
3. arr = np.array([0, math.pi/4, 3*math.pi/2, math.pi/6])
4. print("Input Array:",arr)
5. print("tan Array:",end=" ")
6. tanarr = np.tan(arr)
7. print(tanarr)

Output:

Input Array: [0. 0.78539816 4.71238898 0.52359878]


tan Array: [0.00000000e+00 1.00000000e+00 5.44374645e+15 5.77350

Numpy deg2rad() method


This function is used to convert the angles from degree to radian.

Syntax

1. numpy.deg2rad(array, out)

Parameters

1. array: these are the angles whose degree values are to be calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing equivalent degree angles to the radian given in the input
array.

Example

1. import numpy as np
2. import math
3.
4. arr = [0, math.pi/2, math.pi/4, math.pi/6 ]
5. print ("Input array : \n", arr)
6.
7. radval = np.deg2rad(arr)
8. print ("\n Radian value : \n", radval)

Output:
Input array :
[0, 1.5707963267948966, 0.7853981633974483, 0.5235987755982988]

Radian value :
[0. 0.02741557 0.01370778 0.00913852]

Numpy hypot() method


This function is used to calculate the hypotenuse for the right angled triangle. The
hypotenuse of the right angle is calculated as:

1. h = sqrt(b**2 + p**2)

where b and p are the base and perpendicular of the right angled triangle.

Each value of h is copied into the output array.

Syntax

1. numpy.hypot(array1, array2 out)

Parameters

1. array1: It is the input array of bases of the given right angled triangles.
2. array2: It is the input array of perpendiculars of the given right angled triangles.
3. Out: It is the shape of the output array.

Return
It returns an array containing the hypotenuse of the given right angled triangles.

Example 1

1. import numpy as np
2.
3. base = [10,2,5,50]
4. per= [3,10,23,6]
5.
6. print("Input base array:",base)
7. print("Input perpendicular array:",per)
8.
9. hyp = np.hypot(base,per)
10.
11. print("hypotenuse ",hyp)

Output:

Input base array: [10, 2, 5, 50]


Input perpendicular array: [3, 10, 23, 6]
hypotenuse [10.44030651 10.19803903 23.53720459 50.35871325]

Example 2

1. import numpy as np
2.
3. base = np.random.rand(3,4)
4. per= np.random.rand(3, 4)
5.
6. print("Input base array:",base)
7. print("Input perpendicular array:",per)
8.
9. hyp = np.hypot(base,per)
10.
11. print("hypotenuse ",hyp)

Output:

Input base array: [[0.38040712 0.8629511 0.5018026 0.08201071]


[0.77464952 0.68392036 0.10326945 0.69031164]
[0.35380139 0.58803283 0.19791751 0.48756617]]

Input perpendicular array: [[0.50466085 0.38053395 0.96808322 0.69790711]


[0.77802303 0.5486305 0.34418331 0.69201998]
[0.28038631 0.35850426 0.90459831 0.13383838]]

hypotenuse [[0.63197481 0.94312814 1.09040862 0.70270911]


[1.09790788 0.87677961 0.35934208 0.97745681]
[0.45143318 0.68870017 0.92599646 0.5056021 ]]

Numpy rad2deg() method


This function is used to convert the angles from radians to degrees.

Syntax

1. numpy.rad2deg(array, out)
Parameters

1. array: these are the angles whose degree values are to be calculated.
2. Out: It is the shape of the output array.

Return
It returns an array containing equivalent degree angles of the radians given in the input
array.

Example

1. import numpy as np
2. import math
3.
4. arr = [0, math.pi/2, math.pi/4, math.pi/6 ]
5. print ("Input array : \n", arr)
6.
7. degval = np.rad2deg(arr)
8. print ("\n Degree value : \n", degval)

Output:

Input array :
[0, 1.5707963267948966, 0.7853981633974483, 0.5235987755982988]

Degree value :
[ 0. 90. 45. 30.]

Numpy radians() method


This function is used to convert the angles from degrees to radians.

Syntax

1. numpy.radians(array, out)

Parameters

1. array: these are the angles whose radians values are to be calculated.
2. Out: It is the shape of the output array.
Return
It returns an array containing equivalent radians angles of the degrees given in the
input array.

Example

1. import numpy as np
2.
3. arr = [0, 30, 60, 90 ]
4. print ("Input array : \n", arr)
5.
6. radval = np.radians(arr)
7. print ("\n Radian value : \n", radval)

Output:

Input array :
[0, 30, 60, 90]

Radian value :
[0. 0.52359878 1.04719755 1.57079633]

Numpy arcsinh()
This function is used to calculate the hyperbolic inverse sine of the array elements.

Syntax

1. numpy.arcsinh(array[,out] )

Parameters

1. array: Array elements (in radians) whose hyperbolic inverse sine values are to be
calculated.
2. out: shape of the output array.

Return
An array containing hyperbolic inverse sine values are returned.

Example
1. import numpy as np
2. import math
3.
4. arr = np.array([0, math.pi/4, 3*math.pi/2, math.pi/6])
5.
6. print("Input Array:",arr)
7. print("tanh Array:",end=" ")
8.
9. arcsinharr = np.arcsinh(arr)
10.
11. print(arcsinharr)

Output:

Input Array: [0. 0.78539816 4.71238898 0.52359878]


tanh Array: [0. 0.72122549 2.25441459 0.50221899]

Numpy arctanh()
This function is used to calculate the hyperbolic inverse tangent of the array elements.

Syntax

1. numpy.arctanh(array[,out] )

Parameters

1. array: Array elements (in radians) whose hyperbolic inverse tangent values are to be
calculated.
2. out: shape of the output array.

Return
An array containing hyperbolic inverse tangent values are returned.

Example

1. import numpy as np
2. import math
3.
4. arr = np.array([0,0.2, 0.5, 0.3])
5.
6. print("Input Array:",arr)
7. print("arctanh Array:",end=" ")
8.
9. arctanharr = np.arctanh(arr)
10.
11. print(arctanharr)

Output:

Input Array: [0. 0.2 0.5 0.3]

arctanh Array: [0. 0.20273255 0.54930614 0.3095196 ]

Numpy ceil()
This function returns the ceil value of the input array elements. The floor of a number
x is i if i is the smallest integer such that, i>=x.

Syntax

1. numpy.ceil(array)

Parameters

1. array: Array elements whose ceil values are to be calculated.

Return
An array containing the ceil values is returned.

Example

1. import numpy as np
2.
3. arr = [0.23, 0.09, 1.2, 1.24, 9.99]
4.
5. print("Input array:",arr)
6.
7. r_arr = np.ceil(arr)
8.
9. print("Output array:",r_arr)
10.
11. arr2 = [145.23, 0.12, 12.34, 123]
12.
13. r_arr2=np.ceil(arr2)
14.
15. print("Input array:",arr2)
16.
17. print("Output array:",r_arr2)

Output:

Input array: [0.23, 0.09, 1.2, 1.24, 9.99]


Output array: [ 1. 1. 2. 2. 10.]
Input array: [145.23, 0.12, 12.34, 123]
Output array: [146. 1. 13. 123.]

Numpy fix()
This function is used to round the array values to the nearest integers towards zero.

Syntax

1. numpy.fix(array,b = None)

Parameters

1. array: Array elements which are to be rounded.


2. b: it is an Ndarray which is optional.

Return
An array containing the rounded values is returned.

Example

1. import numpy as np
2.
3. arr = [0.23, 0.09, 1.2, 1.24, 9.99]
4.
5. print("Input array:",arr)
6.
7. r_arr = np.fix(arr)
8.
9. print("Output array:",r_arr)

Output:

Input array: [0.23, 0.09, 1.2, 1.24, 9.99]


Output array: [0. 0. 1. 1. 9.]

Numpy floor()
This function returns the floor value of the input array elements. The floor of a number
x is i if i is the largest integer such that, i<=x.

Syntax

1. numpy.floor(array)

Parameters

1. array: Array elements whose floor values are to be calculated.

Return
An array containing the floor values is returned.

Example

1. import numpy as np
2.
3. arr = [0.23, 0.09, 1.2, 1.24, 9.99]
4.
5. print("Input array:",arr)
6.
7. r_arr = np.floor(arr)
8.
9. print("Output array:",r_arr)
10.
11. arr2 = [145.23, 0.12, 12.34, 123]
12.
13. r_arr2=np.floor(arr2)
14.
15. print("Input array:",arr2)
16.
17. print("Output array:",r_arr2)

Output:

Input array: [0.23, 0.09, 1.2, 1.24, 9.99]


Output array: [0. 0. 1. 1. 9.]
Input array: [145.23, 0.12, 12.34, 123]
Output array: [145. 0. 12. 123.]

Numpy rint()
This function is used to round the array elements to the nearest integer.

Syntax

1. numpy.rint(array)

Parameters

1. array: Array elements whose nearest integers are to be calculated.

Return
An array containing the rounded values is returned.

Example

1. import numpy as np
2.
3. arr = [0.23, 0.09, 1.2, 1.24, 9.99]
4.
5. print("Input array:",arr)
6.
7. r_arr = np.rint(arr)
8.
9. print("Output array:",r_arr)

Output:

Input array: [0.23, 0.09, 1.2, 1.24, 9.99]


Output array: [ 0. 0. 1. 1. 10.]

Numpy tanh()
This function is used to calculate the hyperbolic tangent for all the elements of the
array passed as the argument.

Syntax

1. numpy.tanh(array[,out] )

Parameters

1. array: Array elements whose tangent values are to be calculated (in radians).
2. out: shape of the output array.

Return
An array with trigonometric tangent sins are returned.

Example

1. import numpy as np
2. import math
3. arr = np.array([0, math.pi/4, 3*math.pi/2, math.pi/6])
4. print("Input Array:",arr)
5. print("tanh Array:",end=" ")
6. tanharr = np.tanh(arr)
7. print(tanharr)

Output:

Input Array: [0. 0.78539816 4.71238898 0.52359878]


tanh Array: [0. 0.6557942 0.99983861 0.48047278]

Numpy trunc()
This function returns the truncated value of the input array elements. The truncated
value t of input value x is the nearest integer which is closer to zero than x.

The fractional part of the input number is discarded by this function.

Syntax

1. numpy.trunc(array)

Parameters

1. array: Array elements whose truncated values are to be returned.

Return
An array containing the truncated values are to be returned.

Example

1. import numpy as np
2.
3. arr = [0.23, 0.09, 1.2, 1.24, 9.99]
4.
5. print("Input array:",arr)
6.
7. r_arr = np.trunc(arr)
8.
9. print("Output array:",r_arr)
10.
11. arr2 = [145.23, 0.12, 12.34, 123]
12.
13. r_arr2=np.trunc(arr2)
14.
15. print("Input array:",arr2)
16.
17. print("Output array:",r_arr2)

Output:

Input array: [0.23, 0.09, 1.2, 1.24, 9.99]


Output array: [0. 0. 1. 1. 9.]
Input array: [145.23, 0.12, 12.34, 123]
Output array: [145. 0. 12. 123.]

numpy.matlib.empty()
This function is used to return a new matrix with the uninitialized entries.

Syntax

1. numpy.matlib.empty(shape,dtype,order)

Parameters
It accepts the following parameters.

1. shape: It is the Tuple defining the shape of the matrix.


2. dtype: It is the data type of the matrix.
3. order: It is the insertion order of the matrix.

Return
A matrix with uninitialized entries is returned.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.empty((3,3)))

Output:

[[6.94892251e-310 2.29200848e-316 0.00000000e+000]


[0.00000000e+000 2.37151510e-322 2.37151510e-322]
[0.00000000e+000 6.94889962e-310 0.00000000e+000]]

Example: initializing integer values

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.empty((3,3),int))

Output:

[[140584865515528 35760528 0]
[ 0 0 0]
[ 0 0 0]]

Example: specifying Insertion order

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.empty((3,3),int,'C'))

Output:

[[140437489977352 22202768 0]
[ 0 0 0]
[ 0 0 0]]

numpy.matlib.eye()
This function returns a matrix with the diagonal elements initialized to 1 and zero
elsewhere.

Syntax

1. numpy.matlib.eye(n, m, k, dtype)

Parameters
It accepts the following parameters.

1. n: It represents the number of rows in the resulting matrix.


2. m: It represents the number columns in the resulting matrix.
3. k: It is the index of the diagonal.
4. dtype: It is the data type of the output.

Return
A matrix with uninitialized entries is returned.
Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.eye(n=3,m=3,k=0,dtype=int))

Output:

HTML Tutorial
[[1 0 0]
[0 1 0]
[0 0 1]]

Example: Initializing float values

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.eye(n=3,m=3,k=0,dtype=float))

Output:

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

numpy.matlib.identity()
This function is used to return an identity matrix of the given size. An identity matrix is
the one with diagonal elements initializes to 1 and all other elements to zero.

Syntax

1. numpy.matlib.ones(size,dtype)

Parameters
It accepts the following parameters.

1. shape: It is the number of rows and columns in the resulting identity matrix.
2. dtype: It is the data type of the identity matrix.

Return
It returns an identity matrix of the specified size and specified data type.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.identity(4))

Output:

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

Example: Identity matrix with integer values

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.identity(4,int))

Output:

[[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]]

numpy.matlib.ones()
This function is used to return a new matrix with the values initialized to ones.

Syntax

1. numpy.matlib.ones(shape,dtype,order)
Parameters
It accepts the following parameters.

1. shape: It is the Tuple defining the shape of the matrix.


2. dtype: It is the data type of the matrix.
3. order: It is the insertion order of the matrix.

Return
A matrix is returned with all the entries initialized to 1.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.ones((3,3)))

Output:

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Example: initializing integer values

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.ones((3,3),int))

Output:

[[1 1 1]
[1 1 1]
[1 1 1]]

Example: specifying Insertion order

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.ones((3,3),int,'C'))

Output:

[[1 1 1]
[1 1 1]
[1 1 1]]

numpy.matlib.zeros()
This function is used to return a new matrix with the values initialized to zeros.

Syntax

1. numpy.matlib.zeros(shape,dtype,order)

Parameters
It accepts the following parameters.

1. shape: It is the Tuple defining the shape of the matrix.


2. dtype: It is the data type of the matrix.
3. order: It is the insertion order of the matrix.

Return
A matrix with uninitialized entries is returned.

Example

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.zeros((3,3)))

Output:

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

Example: initializing integer values

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.zeros((3,3),int))

Output:

[[0 0 0]
[0 0 0]
[0 0 0]]

Example: specifying Insertion order

1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.zeros((3,3),int,'C'))

Output:

[[0 0 0]
[0 0 0]
[0 0 0]]

numpy.arrange()
It creates an array by using the evenly spaced values over the given interval. The
interval mentioned is half opened i.e. [Start, Stop]).

Syntax

1. numpy.arrange(start, stop, step, dtype)

Parameters
It accepts the following parameters.
1. start: The starting of an interval. The default is 0.
2. stop: represents the value at which the interval ends excluding this value.
3. step: The number by which the interval values change.
4. dtype: the data type of the numpy array items.

Return
An array within the specified range is returned.

Example 1

1. import numpy as np
2. arr = np.arange(0,10,2,float)
3. print(arr)

Output:

[0. 2. 4. 6. 8.]

Example 2

1. import numpy as np
2. arr = np.arange(10,100,5,int)
3. print("The array over the given range is ",arr

Output:

The array over the given range is [10 15 20 25 30 35 40 45 50

numpy.asarray()
This function is used to create an array by using the existing data in the form of lists,
or tuples. This function is useful in the scenario where we need to convert a python
sequence into the numpy array object.

Syntax

1. numpy.asarray(sequence, dtype = None, order = None)

Parameters
It accepts the following parameters.

1. shape: It is the Tuple defining the shape of the matrix.


2. dtype: It is the data type each item of the array
3. order: It is the insertion order of the array. The default is C.

Return
An array with the equivalent values to the sequence is returned.

Example

1. import numpy as np
2.
3. l=[1,2,3,4,5,6,7]
4.
5. a = np.asarray(l);
6.
7. print(type(a))
8.
9. print(a)

Output:

[1 2 3 4 5 6 7]

Example: Creating a numpy array from the Tuple

1. import numpy as np
2. l=(1,2,3,4,5,6,7)
3. a = np.asarray(l);
4. print(type(a))
5. print(a)

Output:

[1 2 3 4 5 6 7]

Example: creating a numpy array using more than one list


1. import numpy as np
2. l=[[1,2,3,4,5,6,7],[8,9]]
3. a = np.asarray(l);
4. print(type(a))
5. print(a)

Output:

[list([1, 2, 3, 4, 5, 6, 7]) list([8, 9])]

numpy.frombuffer()
This function is used to create an array by using the specified buffer.

Syntax

1. numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

Parameters
It accepts the following parameters.

1. buffer: It represents an object that exposes a buffer interface.


2. dtype: It represents the data type of the returned data type array. The default value is
0.
3. count: It represents the length of the returned ndarray. The default value is -1.
4. offset: It represents the starting position to read from. The default value is 0.

Return
The array version of the buffer is returned.

Example

1. import numpy as np
2. l = b'hello world'
3. print(type(l))
4. a = np.frombuffer(l, dtype = "S1")
5. print(a)
6. print(type(a))

Output:

[b'h' b'e' b'l' b'l' b'o' b' ' b'w' b'o' b'r' b'l' b'd']

numpy.fromiter()
This function is used to create a ndarray by using an iterable object. It returns a one-
dimensional ndarray object.

Syntax

1. numpy.fromiter(iterable, dtype, count = - 1)

Parameters
It accepts the following parameters.

1. Iterable: It represents an iterable object.


2. dtype: It represents the data type of the resultant array items.
3. count: It represents the number of items to read from the buffer in the array.

Return
An array created by using the iterable object is returned.

Example

1. import numpy as np
2. list = [0,2,4,6]
3. it = iter(list)
4. x = np.fromiter(it, dtype = float)
5. print(x)
6. print(type(x))

Output:

[0. 2. 4. 6.]
numpy.linspace()
It is similar to the arrange function. However, it doesn?t allow us to specify the step
size in the syntax.

Instead of that, it only returns evenly separated values over a specified period. The
system implicitly calculates the step size.

Syntax

1. numpy.linspace(start, stop, num, endpoint, retstep, dtype)

Parameters
It accepts the following parameters.

1. start: It represents the starting value of the interval.


2. stop:It represents the stopping value of the interval.
3. num: The amount of evenly spaced samples over the interval to be generated. The
default is 50.
4. endpoint: Its true value indicates that the stopping value is included in the interval.
5. rettstep: This has to be a boolean value. Represents the steps and samples between
the consecutive numbers.
6. dtype: It represents the data type of the array items.

Return
An array within the specified range is returned.

Example 1

1. import numpy as np
2. arr = np.linspace(10, 20, 5)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [10. 12.5 15. 17.5 20.]

Example 2
1. import numpy as np
2. arr = np.linspace(10, 20, 5, endpoint = False)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [10. 12. 14. 16. 18.]

numpy.logspace()
It creates an array by using the numbers that are evenly separated on a log scale.

Syntax

1. numpy.logspace(start, stop, num, endpoint, base, dtype)

Parameters
It accepts the following parameters.

1. start: It represents the starting value of the interval in the base.


2. stop:It represents the stopping value of the interval in the base.
3. num:The number of values between the range.
4. endpoint:It is a boolean type value. It makes the value represented by stop as the last
value of the interval.
5. base:It represents the base of the log space.
6. dtype:It represents the data type of the array items.

Return
An array within the specified range is returned.

Example 1

1. import numpy as np
2. arr = np.logspace(10, 20, num = 5, endpoint = True)
3. print("The array over the given range is ",arr)

Output:
The array over the given range is [1.00000000e+10 3.16227766e+12
1.00000000e+15 3.16227766e+17 1.00000000e+20]

Example 2

1. import numpy as np
2. arr = np.logspace(10, 20, num = 5,base = 2, endpoint = True)
3. print("The array over the given range is ",arr)

Output:

The array over the given range is [1.02400000e+03 5.79261875e+03


3.27680000e+04 1.85363800e+05
1.04857600e+06]

Python Pandas Tutorial


Python Pandas is defined as an open-source library that provides high-performance
data manipulation in Python. This tutorial is designed for both beginners and
professionals.

It is used for data analysis in Python and developed by Wes McKinney in 2008. Our
Tutorial provides all the basic and advanced concepts of Python Pandas, such as
Numpy, Data operation and Time Series

Python Pandas Introduction


Pandas is defined as an open-source library that provides high-performance data
manipulation in Python. The name of Pandas is derived from the word Panel Data,
which means an Econometrics from Multidimensional data. It is used for data
analysis in Python and developed by Wes McKinney in 2008.

Data analysis requires lots of processing, such as restructuring, cleaning or merging,


etc. There are different tools are available for fast data processing, such as Numpy,
Scipy, Cython, and Panda. But we prefer Pandas because working with Pandas is fast,
simple and more expressive than other tool 308

Pandas is built on top of the Numpy package, means Numpy is required for operating
the Pandas.

Before Pandas, Python was capable for data preparation, but it only provided limited
support for data analysis. So, Pandas came into the picture and enhanced the
capabilities of data analysis. It can perform five significant steps required for
processing and analysis of data irrespective of the origin of the data, i.e., load,
manipulate, prepare, model, and analyze.

Key Features of Pandas


o It has a fast and efficient DataFrame object with the default and customized indexing.
o Used for reshaping and pivoting of the data sets.
o Group by data for aggregations and transformations.
o It is used for data alignment and integration of the missing data.
o Provide the functionality of Time Series.
o Process a variety of data sets in different formats like matrix data, tabular
heterogeneous, time series.
o Handle multiple operations of the data sets such as subsetting, slicing, filtering,
groupBy, re-ordering, and re-shaping.
o It integrates with the other libraries such as SciPy, and scikit-learn.
o Provides fast performance, and If you want to speed it, even more, you can use
the Cython.

Benefits of Pandas
The benefits of pandas over using other language are as follows:

o Data Representation: It represents the data in a form that is suited for data analysis
through its DataFrame and Series.
o Clear code: The clear API of the Pandas allows you to focus on the core part of the
code. So, it provides clear and concise code for the user.

Python Pandas Data Structure


The Pandas provides two data structures for processing the data,
i.e., Series and DataFrame, which are discussed below:

1) Series
It is defined as a one-dimensional array that is capable of storing various data types.
The row labels of series are called the index. We can easily convert the list, tuple, and
dictionary into series using "series' method. A Series cannot contain multiple columns.
It has one parameter:
Data: It can be any list, dictionary, or scalar value.

Creating Series from Array:

Before creating a Series, Firstly, we have to import the numpy module and then use
array() function in the program.

1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Explanation: In this code, firstly, we have imported the pandas and numpy library
with the pd and np alias. Then, we have taken a variable named "info" that consist of
an array of some values. We have called the info variable through a Series method
and defined it in an "a" variable. The Series has printed by calling the print(a) method.

Python Pandas DataFrame


It is a widely used data structure of pandas and works with a two-dimensional array
with labeled axes (rows and columns). DataFrame is defined as a standard way to store
data and has two different indexes, i.e., row index and column index. It consists of the
following properties:

o The columns can be heterogeneous types like int, bool, and so on.
o It can be seen as a dictionary of Series structure where both the rows and columns are
indexed. It is denoted as "columns" in case of columns and "index" in case of rows.

Create a DataFrame using List:

We can easily create a DataFrame in Pandas using list.

1. import pandas as pd
2. # a list of strings
3. x = ['Python', 'Pandas']
4.
5. # Calling DataFrame constructor on list
6. df = pd.DataFrame(x)
7. print(df)

Output

0
0 Python
1 Pandas

Explanation: In this code, we have defined a variable named "x" that consist of string
values. The DataFrame constructor is being called on a list to print the values.

Python Pandas Series


The Pandas Series can be defined as a one-dimensional array that is capable of storing
various data types. We can easily convert the list, tuple, and dictionary into series using
"series' method. The row labels of series are called the index. A Series cannot contain
multiple columns. It has the following parameter:

o data: It can be any list, dictionary, or scalar value.


o index: The value of the index should be unique and hashable. It must be of the same
length as data. If we do not pass any index, default np.arrange(n) will be used.
o dtype: It refers to the data type of series.
o copy: It is used for copying the data.

Creating a Series:
We can create a Series in two ways:

1. Create an empty Series


2. Create a Series using inputs.

Create an Empty Series:


We can easily create an empty series in Pandas which means it will not have any value.

The syntax that is used for creating an Empty Series:


1. <series object> = pandas.Series()

The below example creates an Empty Series type object that has no values and having
default datatype, i.e., float64.

Example

1. import pandas as pd
2. x = pd.Series()
3. print (x)

Output

Series([], dtype: float64)

Creating a Series using inputs:


We can create Series by using various inputs:

o Array
o Dict
o Scalar value

Creating Series from Array:

Before creating a Series, firstly, we have to import the numpy module and then use
array() function in the program. If the data is ndarray, then the passed index must be
of the same length.

If we do not pass an index, then by default index of range(n) is being passed where n
defines the length of an array, i.e., [0,1,2,....range(len(array))-1].

Example

1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Create a Series from dict

We can also create a Series from dict. If the dictionary object is being passed as an
input and the index is not specified, then the dictionary keys are taken in a sorted
order to construct the index.

If index is passed, then values correspond to a particular label in the index will be
extracted from the dictionary.

1. #import the pandas library


2. import pandas as pd
3. import numpy as np
4. info = {'x' : 0., 'y' : 1., 'z' : 2.}
5. a = pd.Series(info)
6. print (a)

Output

x 0.0
y 1.0
z 2.0
dtype: float64

Create a Series using Scalar:

If we take the scalar values, then the index must be provided. The scalar value will be
repeated for matching the length of the index.

1. #import pandas library


2. import pandas as pd
3. import numpy as np
4. x = pd.Series(4, index=[0, 1, 2, 3])
5. print (x)

Output

0 4
1 4
2 4
3 4
dtype: int64

Accessing data from series with Position:


Once you create the Series type object, you can access its indexes, data, and even
individual elements.

The data in the Series can be accessed similar to that in the ndarray.

1. import pandas as pd
2. x = pd.Series([1,2,3],index = ['a','b','c'])
3. #retrieve the first element
4. print (x[0])

Output

Series object attributes


The Series attribute is defined as any information related to the Series object such as
size, datatype. etc. Below are some of the attributes that you can use to get the
information about the Series object:

Attributes Description

Series.index Defines the index of the Series.

Series.shape It returns a tuple of shape of the data.

Series.dtype It returns the data type of the data.

Series.size It returns the size of the data.

Series.empty It returns True if Series object is empty, otherwise returns false.

Series.hasnans It returns True if there are any NaN values, otherwise returns false.

Series.nbytes It returns the number of bytes in the data.


Series.ndim It returns the number of dimensions in the data.

Series.itemsize It returns the size of the datatype of item.

Retrieving Index array and data array of a series object


We can retrieve the index array and data array of an existing Series object by using the
attributes index and values.

1. import numpy as np
2. import pandas as pd
3. x=pd.Series(data=[2,4,6,8])
4. y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c'])
5. print(x.index)
6. print(x.values)
7. print(y.index)
8. print(y.values)

Output

RangeIndex(start=0, stop=4, step=1)


[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]

Retrieving Types (dtype) and Size of Type (itemsize)


You can use attribute dtype with Series object as <objectname> dtype for retrieving
the data type of an individual element of a series object, you can use
the itemsize attribute to show the number of bytes allocated to each data item.

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.dtype)
7. print(a.itemsize)
8. print(b.dtype)
9. print(b.itemsize)
Output

int64
8
float64
8

Retrieving Shape
The shape of the Series object defines total number of elements including missing or
empty values(NaN).

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. print(a.shape)
6. print(b.shape)

Output

(4,)
(3,)

Retrieving Dimension, Size and Number of bytes:

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.ndim, b.ndim)
7. print(a.size, b.size)
8. print(a.nbytes, b.nbytes)

Output

1 1
4 3
32 24

Checking Emptiness and Presence of NaNs


To check the Series object is empty, you can use the empty attribute. Similarly, to
check if a series object contains some NaN values or not, you can use
the hasans attribute.

Example

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,np.NaN])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. c=pd.Series()
6. print(a.empty,b.empty,c.empty)
7. print(a.hasnans,b.hasnans,c.hasnans)
8. print(len(a),len(b))
9. print(a.count( ),b.count( ))

Output

False False True


True False False
4 3
3 3

Series Functions
There are some functions used in Series which are as follows:

Functions Description

Pandas Series.map() Map the values from two series that have a common column.

Pandas Series.std() Calculate the standard deviation of the given set of numbers, DataFrame,
column, and rows.

Pandas Series.to_frame() Convert the series object to the dataframe.

Pandas Returns a Series that contain counts of unique values.


Series.value_counts()
Pandas Series.map()
The main task of map() is used to map the values from two series that have a common
column. To map the two Series, the last column of the first Series should be the same
as the index column of the second series, and the values should be unique.

Syntax

1. Series.map(arg, na_action=None)

Parameters

o arg: function, dict, or Series.


It refers to the mapping correspondence.
o na_action: {None, 'ignore'}, Default value None. If ignore, it returns null values,
without passing it to the mapping correspondence.

Returns
It returns the Pandas Series with the same index as a caller.

Example

1. import pandas as pd
2. import numpy as np
3. a = pd.Series(['Java', 'C', 'C++', np.nan])
4. a.map({'Java': 'Core'})

Output

0 Core
1 NaN
2 NaN
3 NaN
dtype: object

Example2

1. import pandas as pd
2. import numpy as np
3. a = pd.Series(['Java', 'C', 'C++', np.nan])
4. a.map({'Java': 'Core'})
5. a.map('I like {}'.format, na_action='ignore')

Output

0 I like Java
1 I like C
2 I like C++
3 I like nan
dtype: object

Example3

1. import pandas as pd
2. import numpy as np
3. a = pd.Series(['Java', 'C', 'C++', np.nan])
4. a.map({'Java': 'Core'})
5. a.map('I like {}'.format)
6. a.map('I like {}'.format, na_action='ignore')

Output

0 I like Java
1 I like C
2 I like C++
3 NaN
dtype: object

Pandas Series.std()
The Pandas std() is defined as a function for calculating the standard deviation of the
given set of numbers, DataFrame, column, and rows. In respect to calculate the
standard deviation, we need to import the package named "statistics" for the
calculation of median.

The standard deviation is normalized by N-1 by default and can be changed using
the ddof argument.

Syntax:

1. Series.std(axis=None, skipna=None, level=None, ddof=1, numeric_only=None


, **kwargs)

Parameters:
o axis: {index (0), columns (1)}
o skipna: It excludes all the NA/null values. If NA is present in an entire
row/column, the result will be NA.
o level: It counts along with a particular level, and collapsing into a scalar if the
axis is a MultiIndex (hierarchical).
o ddof: Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
where N represents the number of elements.
o numeric_only: boolean, default value None
It includes only float, int, boolean columns. If it is None, it will attempt to use
everything, so use only numeric data.
It is not implemented for a Series.

Returns:
It returns Series or DataFrame if the level is specified.

Example1:

1. import pandas as pd
2. # calculate standard deviation
3. import numpy as np
4. print(np.std([4,7,2,1,6,3]))
5. print(np.std([6,9,15,2,-17,15,4]))

Output

Difference between JDK, JRE, and JVM


2.1147629234082532
10.077252622027656

Example2:

1. import pandas as pd
2. import numpy as np
3.
4. #Create a DataFrame
5. info = {
6. 'Name':['Parker','Smith','John','William'],
7. 'sub1_Marks':[52,38,42,37],
8. 'sub2_Marks':[41,35,29,36]}
9. data = pd.DataFrame(info)
10. data
11. # standard deviation of the dataframe
12. data.std()

Output

sub1_Marks 6.849574
sub2_Marks 4.924429
dtype: float64

Pandas Series.to_frame()
Series is defined as a type of list that can hold an integer, string, double values, etc. It
returns an object in the form of a list that has an index starting from 0 to n where n
represents the length of values in Series.

The main difference between Series and Data Frame is that Series can only contain a
single list with a particular index, whereas the DataFrame is a combination of
more than one series that can analyze the data.

The Pandas Series.to_frame() function is used to convert the series object to the
DataFrame.

Syntax

1. Series.to_frame(name=None)

Parameters
name: Refers to the object. Its Default value is None. If it has one value, the passed
name will be substituted for the series name.

Returns
It returns DataFrame representation of Series.

Example1

1. s = pd.Series(["a", "b", "c"],


2. name="vals")
3. s.to_frame()
Output

vals
0 a
1 b
2 c

Example2

1. import pandas as pd
2. import matplotlib.pyplot as plt
3. emp = ['Parker', 'John', 'Smith', 'William']
4. id = [102, 107, 109, 114]
5. emp_series = pd.Series(emp)
6. id_series = pd.Series(id)
7. frame = { 'Emp': emp_series, 'ID': id_series }
8. result = pd.DataFrame(frame)
9. print(result)

Output

Emp ID
0 Parker 102
1 John 107
2 Smith 109
3 William 114

Pandas Series.unique()
While working with the DataFrame in Pandas, you need to find the unique elements
present in the column. For doing this, we have to use the unique() method to extract
the unique values from the columns. The Pandas library in Python can easily help us to
find unique data.

The unique values present in the columns are returned in order of its occurrence. This
does not sort the order of its appearance. In addition, this method is based on
the hash-table.

It is significantly faster than numpy.unique() method and also includes null values.

Syntax:

1. pandas.unique(values)
Parameters:
values: It refers to a 1d array-like object that consists of an array value.

Returns:
This method returns a numpy.ndarray or ExtensionArray object and can be:

o index: Returns when user passes index as an input.


o Categorical: Returns when user passes a Categorical dtype as an input.
o ndarray: Returns when user passes a ndarray/Series as an input.

Example 1:

1. import pandas as pd
2. pd.unique(pd.Series([2, 1, 3, 3]))
3. pd.unique(pd.Series([pd.Timestamp('20160101'),
4. pd.Timestamp('20160101')]))

Output:

array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]')

Example 2: The below example extracts the unique timestamp from the Index:

1. import pandas as pd
2. import numpy as np
3. pd.unique(pd.Index([pd.Timestamp('20160101', tz='US/Eastern'),
4. pd.Timestamp('20160101', tz='US/Eastern')]))

Output:

DatetimeIndex(['2016-01-01 00:00:00-05:00'], dtype='datetime64[ns,


US/Eastern]'

Pandas Series.value_counts()
The value_counts() function returns a Series that contain counts of unique values. It
returns an object that will be in descending order so that its first element will be the
most frequently-occurred element.

By default, it excludes NA values.


Syntax

1. Series.value_counts(normalize=False, sort=True, ascending=False, bins=None,


dropna=True)

Parameters

o normalize: If it is true, then the returned object will contain the relative
frequencies of the unique values.
o sort: It sort by the values.
o ascending: It sort in the ascending order.
o bins: Rather than counting the values, it groups them into the half-open bins
that provide convenience for the pd.cut, which only works with numeric data.
o dropna: It does not include counts of NaN.

Returns
It returns the counted series.

Example1

1. import pandas as pd
2. import numpy as np
3. index = pd.Index([2, 1, 1, np.nan, 3])
4. index.value_counts()

Output

1.0 2
3.0 1
2.0 1
dtype: int64

Example2

1. import pandas as pd
2. import numpy as np
3. index = pd.Index([2, 1, 1, np.nan, 3])
4. a = pd.Series([2, 1, 1, np.nan, 3])
5. a.value_counts(normalize=True)
Output

1.0 0.50
3.0 0.25
2.0 0.25
dtype: float64

Example3

1. import pandas as pd
2. index = pd.Index([1, 3, 2, 2, 1, np.nan])
3. index.value_counts()
4. a = pd.Series([1, 3, 2, 2, 1, np.nan])
5. a.value_counts(bins=2)

Output

(0.997, 2.0] 4
(2.0, 3.0] 1
dtype: int64

Example4

1. import pandas as pd
2. index = pd.Index([1, 3, 2, 2, 1, np.nan])
3. index.value_counts()
4. a = pd.Series([1, 3, 2, 2, 1, np.nan])
5. a.value_counts(dropna=False)

Output

2.0 2
1.0 2
NaN 1
3.0 1
dtype: int64

Python Pandas DataFrame


Pandas DataFrame is a widely used data structure which works with a two-dimensional
array with labeled axes (rows and columns). DataFrame is defined as a standard way
to store data that has two different indexes, i.e., row index and column index. It
consists of the following properties:

o The columns can be heterogeneous types like int, bool, and so on.
o It can be seen as a dictionary of Series structure where both the rows and
columns are indexed. It is denoted as "columns" in case of columns and "index"
in case of rows.

Parameter & Description:


data: It consists of different forms like ndarray, series, map, constants, lists, array.

index: The Default np.arrange(n) index is used for the row labels if no index is passed.

columns: The default syntax is np.arrange(n) for the column labels. It shows only true
if no index is passed.

10.8M
256
How to find Nth Highest Salary in SQL

dtype: It refers to the data type of each column.

copy(): It is used for copying the data.

Create a DataFrame
We can create a DataFrame using following ways:
o dict
o Lists
o Numpy ndarrrays
o Series

Create an empty DataFrame

The below code shows how to create an empty DataFrame in Pandas:

1. # importing the pandas library


2. import pandas as pd
3. df = pd.DataFrame()
4. print (df)

Output

Empty DataFrame
Columns: []
Index: []

Explanation: In the above code, first of all, we have imported the pandas library with
the alias pd and then defined a variable named as df that consists an empty
DataFrame. Finally, we have printed it by passing the df into the print.

Create a DataFrame using List:


We can easily create a DataFrame in Pandas using list.

1. # importing the pandas library


2. import pandas as pd
3. # a list of strings
4. x = ['Python', 'Pandas']
5.
6. # Calling DataFrame constructor on list
7. df = pd.DataFrame(x)
8. print(df)

Output

0
0 Python
1 Pandas
Explanation: In the above code, we have defined a variable named "x" that consist of
string values. The DataFrame constructor is being called for a list to print the values.

Create a DataFrame from Dict of ndarrays/ Lists

1. # importing the pandas library


2. import pandas as pd
3. info = {'ID' :[101, 102, 103],'Department' :['B.Sc','B.Tech','M.Tech',]}
4. df = pd.DataFrame(info)
5. print (df)

Output

ID Department
0 101 B.Sc
1 102 B.Tech
2 103 M.Tech

Explanation: In the above code, we have defined a dictionary named "info" that
consist list of ID and Department. For printing the values, we have to call the info
dictionary through a variable called df and pass it as an argument in print().

Create a DataFrame from Dict of Series:

1. # importing the pandas library


2. import pandas as pd
3.
4. info = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']),
5. 'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}
6.
7. d1 = pd.DataFrame(info)
8. print (d1)

Output

one two
a 1.0 1
b 2.0 2
c 3.0 3
d 4.0 4
e 5.0 5
f 6.0 6
g NaN 7
h NaN 8
Explanation: In the above code, a dictionary named "info" consists of two Series with
its respective index. For printing the values, we have to call the info dictionary through
a variable called d1 and pass it as an argument in print().

Column Selection
We can select any column from the DataFrame. Here is the code that demonstrates
how to select a column from the DataFrame.

1. # importing the pandas library


2. import pandas as pd
3.
4. info = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']),
5. 'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}
6.
7. d1 = pd.DataFrame(info)
8. print (d1 ['one'])

Output

a 1.0
b 2.0
c 3.0
d 4.0
e 5.0
f 6.0
g NaN
h NaN
Name: one, dtype: float64

Explanation: In the above code, a dictionary named "info" consists of two Series with
its respective index. Later, we have called the info dictionary through a
variable d1 and selected the "one" Series from the DataFrame by passing it into
the print().

Column Addition
We can also add any new column to an existing DataFrame. The below code
demonstrates how to add any new column to an existing DataFrame:

1. # importing the pandas library


2. import pandas as pd
3.
4. info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),
5. 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
6.
7. df = pd.DataFrame(info)
8.
9. # Add a new column to an existing DataFrame object
10.
11. print ("Add new column by passing series")
12. df['three']=pd.Series([20,40,60],index=['a','b','c'])
13. print (df)
14.
15. print ("Add new column using existing DataFrame columns")
16. df['four']=df['one']+df['three']
17.
18. print (df)

Output

Add new column by passing series


one two three
a 1.0 1 20.0
b 2.0 2 40.0
c 3.0 3 60.0
d 4.0 4 NaN
e 5.0 5 NaN
f NaN 6 NaN

Add new column using existing DataFrame columns


one two three four
a 1.0 1 20.0 21.0
b 2.0 2 40.0 42.0
c 3.0 3 60.0 63.0
d 4.0 4 NaN NaN
e 5.0 5 NaN NaN
f NaN 6 NaN NaN

Explanation: In the above code, a dictionary named as f consists two Series with its
respective index. Later, we have called the info dictionary through a variable df.

To add a new column to an existing DataFrame object, we have passed a new series
that contain some values concerning its index and printed its result using print().

We can add the new columns using the existing DataFrame. The "four" column has
been added that stores the result of the addition of the two columns,
i.e., one and three.

Column Deletion:
We can also delete any column from the existing DataFrame. This code helps to
demonstrate how the column can be deleted from an existing DataFrame:

1. # importing the pandas library


2. import pandas as pd
3.
4. info = {'one' : pd.Series([1, 2], index= ['a', 'b']),
5. 'two' : pd.Series([1, 2, 3], index=['a', 'b', 'c'])}
6.
7. df = pd.DataFrame(info)
8. print ("The DataFrame:")
9. print (df)
10.
11. # using del function
12. print ("Delete the first column:")
13. del df['one']
14. print (df)
15. # using pop function
16. print ("Delete the another column:")
17. df.pop('two')
18. print (df)

Output

The DataFrame:
one two
a 1.0 1
b 2.0 2
c NaN 3

Delete the first column:


two
a 1
b 2
c 3

Delete the another column:


Empty DataFrame
Columns: []
Index: [a, b, c]

Explanation:
In the above code, the df variable is responsible for calling the info dictionary and
print the entire values of the dictionary. We can use the delete or pop function to
delete the columns from the DataFrame.

In the first case, we have used the delete function to delete the "one" column from
the DataFrame whereas in the second case, we have used the pop function to remove
the "two" column from the DataFrame.

Row Selection, Addition, and Deletion


Row Selection:
We can easily select, add, or delete any row at anytime. First of all, we will understand
the row selection. Let's see how we can select a row using different ways that are as
follows:

Selection by Label:

We can select any row by passing the row label to a loc function.

1. # importing the pandas library


2. import pandas as pd
3.
4. info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),
5. 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
6.
7. df = pd.DataFrame(info)
8. print (df.loc['b'])

Output

one 2.0
two 2.0
Name: b, dtype: float64

Explanation: In the above code, a dictionary named as info that consists


two Series with its respective index.

For selecting a row, we have passed the row label to a loc function.

Selection by integer location:


The rows can also be selected by passing the integer location to an iloc function.

1. # importing the pandas library


2. import pandas as pd
3. info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),
4. 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
5. df = pd.DataFrame(info)
6. print (df.iloc[3])

Output

one 4.0
two 4.0
Name: d, dtype: float64

Explanation: Explanation: In the above code, we have defined a dictionary named


as info that consists two Series with its respective index.

For selecting a row, we have passed the integer location to an iloc function.

Slice Rows

It is another method to select multiple rows using ':' operator.

1. # importing the pandas library


2. import pandas as pd
3. info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),
4. 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
5. df = pd.DataFrame(info)
6. print (df[2:5])

Output

one two
c 3.0 3
d 4.0 4
e 5.0 5

Explanation: In the above code, we have defined a range from 2:5 for the selection of
row and then printed its values on the console.

Addition of rows:

We can easily add new rows to the DataFrame using append function. It adds the new
rows at the end.
1. # importing the pandas library
2. import pandas as pd
3. d = pd.DataFrame([[7, 8], [9, 10]], columns = ['x','y'])
4. d2 = pd.DataFrame([[11, 12], [13, 14]], columns = ['x','y'])
5. d = d.append(d2)
6. print (d)

Output

x y
0 7 8
1 9 10
0 11 12
1 13 14

Explanation: In the above code, we have defined two separate lists that contains some
rows and columns. These columns have been added using the append function and
then result is displayed on the console.

Deletion of rows:

We can delete or drop any rows from a DataFrame using the index label. If in case, the
label is duplicate then multiple rows will be deleted.

1. # importing the pandas library


2. import pandas as pd
3.
4. a_info = pd.DataFrame([[4, 5], [6, 7]], columns = ['x','y'])
5. b_info = pd.DataFrame([[8, 9], [10, 11]], columns = ['x','y'])
6.
7. a_info = a_info.append(b_info)
8.
9. # Drop rows with label 0
10. a_info = a_info.drop(0)

Output

x y
1 6 7
1 10 11

Explanation: In the above code, we have defined two separate lists that contains some
rows and columns.
Here, we have defined the index label of a row that needs to be deleted from the list.

DataFrame Functions
There are lots of functions used in DataFrame which are as follows:

Functions Description

Pandas DataFrame.append() Add the rows of other dataframe to the end of the given
dataframe.

Pandas DataFrame.apply() Allows the user to pass a function and apply it to every single
value of the Pandas series.

Pandas DataFrame.assign() Add new column into a dataframe.

Pandas DataFrame.astype() Cast the Pandas object to a specified dtype.astype() function.

Pandas DataFrame.concat() Perform concatenation operation along an axis in the


DataFrame.

Pandas DataFrame.count() Count the number of non-NA cells for each column or row.

Pandas DataFrame.describe() Calculate some statistical data like percentile, mean and std of
the numerical values of the Series or DataFrame.

Pandas Remove duplicate values from the DataFrame.


DataFrame.drop_duplicates()

Pandas DataFrame.groupby() Split the data into various groups.

Pandas DataFrame.head() Returns the first n rows for the object based on position.

Pandas DataFrame.hist() Divide the values within a numerical variable into "bins".

Pandas DataFrame.iterrows() Iterate over the rows as (index, series) pairs.

Pandas DataFrame.mean() Return the mean of the values for the requested axis.

Pandas DataFrame.melt() Unpivots the DataFrame from a wide format to a long format.

Pandas DataFrame.merge() Merge the two datasets together into one.

Pandas DataFrame.pivot_table() Aggregate data with calculations such as Sum, Count,


Average, Max, and Min.
Pandas DataFrame.query() Filter the dataframe.

Pandas DataFrame.sample() Select the rows and columns from the dataframe randomly.

Pandas DataFrame.shift() Shift column or subtract the column value with the previous
row value from the dataframe.

Pandas DataFrame.sort() Sort the dataframe.

Pandas DataFrame.sum() Return the sum of the values for the requested axis by the
user.

Pandas DataFrame.to_excel() Export the dataframe to the excel file.

Pandas DataFrame.transpose() Transpose the index and columns of the dataframe.

Pandas DataFrame.where() Check the dataframe for one or more conditions

Pandas DataFrame.append()
The Pandas append() function is used to add the rows of other dataframe to the end
of the given dataframe, returning a new dataframe object. The new columns and the
new cells are inserted into the original DataFrame that are populated with NaN value.

Syntax:

1. DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=No


ne)

Parameters:

o other: DataFrame or Series/dict-like object, or a list of these


It refers to the data to be appended.
o ignore_index: If it is true, it does not use the index labels.
o verify_integrity: If it is true, it raises ValueError on creating an index with
duplicates.
o sort: It sorts the columns if the columns of self and other are not aligned. The
default sorting is deprecated, and it will change to not-sorting in a future
version of pandas. We pass sort=True Explicitly for silence the warning and the
sort, whereas we pass sort=False Explicitly for silence the warning and not the
sort.

Returns:
It returns the appended DataFrame as an output.

Example1:

1. import pandas as pd
2. # Create first Dataframe using dictionary
3. info1 = pd.DataFrame({"x":[25,15,12,19],
4. "y":[47, 24, 17, 29]})
5. # Create second Dataframe using dictionary
6. Info2 = pd.DataFrame({"x":[25, 15, 12],
7. "y":[47, 24, 17],
8. "z":[38, 12, 45]})
9. # append info2 at end in info1
10. info.append(info2, ignore_index = True)

Output

x y z
0 25 47 NaN
1 15 24 NaN
2 12 17 NaN
3 19 29 NaN
4 25 47 38.0
5 15 24 12.0
6 12 17 45.0

Example2:

1. import pandas as pd
2. # Create first Dataframe using dictionary
3. info1 = info = pd.DataFrame({"x":[15, 25, 37, 42],
4. "y":[24, 38, 18, 45]})
5. # Create second Dataframe using dictionary
6. info2 = pd.DataFrame({"x":[15, 25, 37],
7. "y":[24, 38, 45]})
8. # print value of info1
9. print(info1, "\n")
10. # print values of info2
11. info2
12. # append info2 at the end of info1 dataframe
13. info1.append(df2)
14. # Continuous index value will maintained
15. # across rows in the new appended data frame.
16. info.append(info2, ignore_index = True)

Output

x y
0 15 24
1 25 38
2 37 18
3 42 45
4 15 24
5 25 38
6 37 45

Pandas DataFrame.apply()
The Pandas apply() function allows the user to pass a function and apply it to every
single value of the Pandas series. This function improves the capabilities of the panda's
library because it helps to segregate data according to the conditions required. So that
it can be efficiently used for data science and machine learning.

The objects that are to be passed to function are Series objects whose index is either
the DataFrame's index, i.e., axis=0 or the DataFrame's columns, i.e., axis=1. By default,
the result_type=None and the final return type is inferred from the return type of the
applied function. Otherwise, it depends on the result_type argument.

Syntax:

1. DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, res


ult_type=None, args=(), **kwds)

Parameters:

o func: It is a function that is to be applied to each column or row.


o axis: {0 or 'index', 1 or 'columns'}, default value 0
It is an axis along which the function is applied. It can have two values:
o 0 or 'index': It applies the function to each of the columns.
o 1 or 'columns': It applies the function to each of the rows.
o broadcast: It is an optional parameter that returns the boolean values.
Only relevant for aggregation functions:
False or None: It returns a Series whose length will be the length of the index
or the number of columns based on the axis parameter.
True: The results will be broadcasted to the original shape of the frame; the
original index and columns will be retained.
o raw: bool, default value False
False: It passes each row or column as a Series to the function.
True: The passed function will receive a ndarray objects. If you are applying a
NumPy reduction function, it will achieve better performance.
o reduce: bool or None, default value None
It tries to apply the reduction procedures. If the DataFrame is empty,
the apply will use the reduce to determine whether the result should be a
Series or a DataFrame.
By default, reduce=None, the apply's return value will be guessed by
calling func on an empty Series (note: All the exceptions that are to be raised
by func will be ignored while guessing). If reduce=True, Series will always be
returned, whereas reduce=False, Always the DataFrame will be returned.
o result_type: {'expand', 'reduce', 'broadcast', None}, default value None
These only act when axis=1 (columns):
'expand': It defines the list-like results that will be turned into columns.
'reduce': It is the opposite of 'expand'. If possible, it returns a Series rather than
expanding list-like results.
'broadcast': It broadcast the results to the original shape of the DataFrame, the
original index, and the columns will be retained.
The default value None depends on the return value of the applied function ,
i.e., list-like results returned as a Series of those.
If apply returns a Series, it expands to the columns.
o args: It is a positional argument that is to be passed to func in addition to the
array/series.
o **kwds: It is an optional keyword argument, which is used to pass as keywords
arguments to func.

Returns:
It returns the result of applying func along the given axis of the DataFrame.

Example:

1. info = pd.DataFrame([[2, 7]] * 4, columns=['P', 'Q'])


2. info.apply(np.sqrt)
3. info.apply(np.sum, axis=0)
4. info.apply(np.sum, axis=1)
5. info.apply(lambda x: [1, 2], axis=1)
6. info.apply(lambda x: [1, 2], axis=1, result_type='expand')
7. info.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1)
8. info.apply(lambda x: [1, 2], axis=1, result_type='broadcast')
9. info

Output

Difference between JDK, JRE, and JVM


A B
0 2 7
1 2 7
2 2 7
3 2 7

Pandas DataFrame.aggregate()
The main task of DataFrame.aggregate() function is to apply some aggregation to one
or more column. Most frequently used aggregations are:

sum: It is used to return the sum of the values for the requested axis.

min: It is used to return the minimum of the values for the requested axis.

max: It is used to return the maximum values for the requested axis.

Syntax:

1. DataFrame.aggregate(func, axis=0, *args, **kwargs)

Parameters:
func: It refers callable, string, dictionary, or list of string/callables.
It is used for aggregating the data. For a function, it must either work when passed to
a DataFrame or DataFrame.apply(). For a DataFrame, it can pass a dict, if the keys are
the column names.

axis: (default 0): It refers to 0 or 'index', 1 or 'columns'

0 or 'index': It is an apply function for each column.

1 or 'columns': It is an apply function for each row.

*args: It is a positional argument that is to be passed to func.

**kwargs: It is a keyword argument that is to be passed to the func.

Returns:
It returns the scalar, Series or DataFrame.

scalar: It is being used when Series.agg is called with the single function.

Series: It is being used when DataFrame.agg is called for the single function.

DataFrame: It is being used when DataFrame.agg is called for the several functions.

Example:

1. import pandas as pd
2. import numpy as np
3. info=pd.DataFrame([[1,5,7],[10,12,15],[18,21,24],[np.nan,np.nan,np.nan]],colum
ns=['X','Y','Z'])
4. info.agg(['sum','min'])

Output:

X Y Z
sum 29.0 38.0 46.0
min 1.0 5.0 7.0

Example2:

1. import pandas as pd
2. import numpy as np
3. info=pd.DataFrame([[1,5,7],[10,12,15],[18,21,24],[np.nan,np.nan,np.nan]],colum
ns=['X','Y','Z'])
4. df.agg({'A' : ['sum', 'min'], 'B' : ['min', 'max']})

Output:

X Y
max NaN 21.0
min 1.0 12.0
sum 29.0 NaN

Pandas DataFrame.assign()
The assign() method is also responsible for adding a new column into a DataFrame.

If we re-assign an existing column, then its value will be overwritten.

Signature

1. DataFrame.assign(**kwargs)

Parameters

o kwargs: keywords are the column names. These keywords are assigned to the
new column if the values are callable. If the values are not callable, they are
simply assigned.

Returns
It returns a new DataFrame with the addition of the new columns.

Example 1:

1. import pandas as pd
2. # Create an empty dataframe
3. info = pd.DataFrame()
4.
5. # Create a column
6. info['ID'] = [101, 102, 103]
7.
8. # View the dataframe
9. info
10. # Assign a new column to dataframe called 'age'
11. info.assign(Name = ['Smith', 'Parker', 'John'])

Output

ID Name
0 101 Smith
1 102 Parker
2 103 John

Example 2:

1. import pandas as pd
2. # Create a dataframe
3. info = pd.DataFrame({'temp_c': [17.0, 25.0]},
4. # Create an index that consist some values
5. index=['Canada', 'Australia'])
6. # View the dataframe
7. info
8. info.assign(temp_f=lambda x: x.temp_c * 7 / 2 + 24)
9. info.assign(temp_f=lambda x: x['temp_c'] * 6 / 2 + 21,
10. temp_k=lambda x: (x['temp_f'] + 342.27) * 6 / 4)

Output

temp_c temp_f temp_k


Canada 17.0 72.0 621.405
Australia 25.0 96.0 657.405

Pandas DataFrame.astype()
The astype() method is generally used for casting the pandas object to a
specified dtype.astype() function. It can also convert any suitable existing column to
a categorical type.

It comes into use when we want to case a particular column data type to another data
type. We can also use the input to Python dictionary to change more than one column
type at once. In the dictionary, the key label corresponds to the column name, and the
values label corresponds to the new data types that we want to be in the columns.

Syntax

1. DataFrame.astype(dtype, copy=True, errors='raise', **kwargs)


Parameters
dtype: It uses numpy.dtype or the Python type for casting the entire pandas object to
the same type. It can also use {col: dtype, ?} alternatively where col refers to the column
label, and dtype is a numpy.dtype or Python type for casting one or more of the
DataFrame's columns to column-specific types.

copy: If copy=True, it returns a copy. Be careful when setting copy= False because
changes to values may propagate to other pandas objects.

errors: For provided dtype, it controls the raising of exceptions on the invalid data.

o raise: It allows the exception that is to be raised.


o ignore: It ignores the exception. It returns the original object on error.

kwargs: It is a keyword argument that is to be passed on to the constructor.

Returns
casted: It returns the same type as a caller.

Example

1. import pandas as pd
2. a = {'col1': [1, 2], 'col2': [3, 4]}
3. info = pd.DataFrame(data=a)
4. info.dtypes
5. # We convert it into 'int64' type.
6. info.astype('int64').dtypes
7. info.astype({'col1': 'int64'}).dtypes
8. x = pd.Series([1, 2], dtype='int64')
9. x.astype('category')
10. cat_dtype = pd.api.types.CategoricalDtype(
11. categories=[2, 1], ordered=True)
12. x.astype(cat_dtype)
13. x1 = pd.Series([1,2])
14. x2 = x1.astype('int64', copy=False)
15. x2[0] = 10
16. x1 # note that x1[0] has changed too

Output
0 12
1 2
dtype: int64

Pandas DataFrame.count()
The Pandas count() is defined as a method that is used to count the number of non-
NA cells for each column or row. It is also suitable to work with the non-floating data.

Syntax:

1. DataFrame.count(axis=0, level=None, numeric_only=False)

Parameters:

o axis: {0 or 'index', 1 or 'columns'}, default value 0


0 or 'index' is used for row-wise, whereas 1 or 'columns' is used for column-
wise.
o level: int or str
It is an optional parameter. If an axis is hierarchical, it counts along with the
particular level and collapsing into the DataFrame.
o numeric_only: bool, default value False
It only includes int, float, or Boolean data.

Returns:
It returns the count of Series or DataFrame if the level is specified.

Example 1: The below example demonstrates the working of the count().

1. import pandas as pd
2. import numpy as np
3. info = pd.DataFrame({"Person":["Parker", "Smith", "William", "John"],
4. "Age": [27., 29, np.nan, 32]
5. info.count()

Output

Person 5
Age 3
dtype: int64
Example 2: If we want to count for each of the row, we can use the axis parameter.
The below code demonstrates the working of the axis parameter.

1. import pandas as pd
2. import numpy as np
3. info = pd.DataFrame({"Person":["Parker", "Smith", "William", "John"],
4. "Age": [27., 29, np.nan, 32]
5. info.count(axis='columns')

Output

0 2
1 2
2 1
3 2
dtype: int64

Pandas DataFrame.cut()
The cut() method is invoked when you need to segment and sort the data values into
bins. It is used to convert a continuous variable to a categorical variable. It can also
segregate an array of elements into separate bins. The method only works for the one-
dimensional array-like objects.

If we have a large set of scalar data and perform some statistical analysis on it, we can
use the cut() method.

Syntax:

1. pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, includ


e_lowest=False, duplicates='raise')

Parameters:
x: It generally refers to an array as an input that is to be bin. The array should be a
one-dimensional array.

bins: It refers to an int, sequence of scalars, or IntervalIndex values that define the
bin edges for the segmentation. Most of the time, we have numerical data on a very
large scale. So, we can group the values into bins to easily perform descriptive statistics
as a generalization of patterns in data. The criteria for binning the data into groups are
as follows:
o int: It defines the number of equal-width bins that are in the range of x. We can
also extend the range of x by .1% on both sides to include the minimum and
maximum values of x.
o sequence of scalars: It mainly defines the bin edges that are allowed for non-
uniform width.
o IntervalIndex: It refers to an exact bin that is to be used in the function. It
should be noted that the IntervalIndex for bins must be non-overlapping.
o right: It consists of a boolean value that checks whether the bins include the
rightmost edge or not. Its default value is True, and it is ignored when bins is
an
o labels: It is an optional parameter that mainly refers to an array or a boolean
value. Its main task is to specify the labels for the returned The length of the
labels must be the same as the resulting bins. If we set its value to False, it
returns only integer indicator of the bins. This argument is ignored if bins is an
IntervalIndex.
o retbins: It refers to a boolean value that checks whether to return the bins or
not. It is often useful when bins are provided as a scalar value. The default value
of retbins is False.
o precision: It is used to store and display the bins labels. It consists of an integer
value that has the default value 3.
o include_lowest: It consists of a boolean value that is used to check whether the
first interval should be left-inclusive or not.
o duplicates: It is an optional parameter that decides whether to raise a
ValueError or drop duplicate values if the bin edges are not unique.

Returns:
This method returns two objects as output which are as follows:

1. out: It mainly refers to a Categorical, Series, or ndarray that is an array-like


object which represents the respective bin for each value of These objects
depend on the value of labels. The possible values than can be returned are as
follows:
o True: It is a default value that returns a Series or a Categorical variable.
The values stored in these objects are Interval data type.
o sequence of scalars: It also returns a Series or a Categorical variable.
The values that are stored in these objects are the type of the sequence.
o False: The false value returns an ndarray of integers.
2. bins: It mainly refers to a ndarray

Example1: The below example segments the numbers into bins:

1. import pandas as pd
2. import numpy as np
3. info_nums = pd.DataFrame({'num': np.random.randint(1, 50, 11)})
4. print(info_nums)
5. info_nums['num_bins'] = pd.cut(x=df_nums['num'], bins=[1, 25, 50])
6. print(info_nums)
7. print(info_nums['num_bins'].unique())

Output:

num
0 48
1 36
2 7
3 2
4 25
5 2
6 13
7 5
8 7
9 25
10 10
num num_bins
0 48 (1.0, 25.0]
1 36 (1.0, 25.0]
2 7 (1.0, 25.0]
3 2 (1.0, 25.0]
4 25 NaN
5 2 (1.0, 25.0]
6 13 (1.0, 25.0]
7 5 (1.0, 25.0]
8 7 (1.0, 25.0]
9 25 (1.0, 25.0]
10 10 NaN
[(1.0, 25.0], NaN]
Categories (1, interval[int64]): [(1, 25]]

Example2: The below example shows how to add labels to bins:

1. import pandas as pd
2. import numpy as np
3. info_nums = pd.DataFrame({'num': np.random.randint(1, 10, 7)})
4. print(info_nums)
5. info_nums['nums_labels'] = pd.cut(x=info_nums['num'], bins=[1, 7, 10], labels=
['Lows', 'Highs'], right=False)
6. print(info_nums)
7. print(info_nums['nums_labels'].unique())

Output:

num
0 9
1 9
2 4
3 9
4 4
5 7
6 2
num nums_labels
0 9 Highs
1 9 Highs
2 4 Lows
3 9 Highs
4 4 Lows
5 7 Highs
6 2 Lows
[Highs, Lows]
Categories (2, object): [Lows < Highs]

Pandas DataFrame.describe()
The describe() method is used for calculating some statistical data like percentile,
mean and std of the numerical values of the Series or DataFrame. It analyzes both
numeric and object series and also the DataFrame column sets of mixed data types.

Syntax

1. DataFrame.describe(percentiles=None, include=None, exclude=None)

Parameters

o percentile: It is an optional parameter which is a list like data type of numbers


that should fall between 0 and 1. Its default value is [.25, .5, .75], which returns
the 25th, 50th, and 75th percentiles.
o include: It is also an optional parameter that includes the list of the data types
while describing the DataFrame. Its default value is None.
o exclude: It is also an optional parameter that exclude the list of data types while
describing DataFrame. Its default value is None.

Returns
It returns the statistical summary of the Series and DataFrame.

Example1

1. import pandas as pd
2. import numpy as np
3. a1 = pd.Series([1, 2, 3])
4. a1.describe()

Output

count 3.0
mean 2.0
std 1.0
min 1.0
25% 1.5
50% 2.0
75% 2.5
max 3.0
dtype: float64

Example2

1. import pandas as pd
2. import numpy as np
3. a1 = pd.Series(['p', 'q', 'q', 'r'])
4. a1.describe()

Output

count 4
unique 3
top q
freq 2
dtype: object

Example3

1. import pandas as pd
2. import numpy as np
3. a1 = pd.Series([1, 2, 3])
4. a1.describe()
5. a1 = pd.Series(['p', 'q', 'q', 'r'])
6. a1.describe()
7. info = pd.DataFrame({'categorical': pd.Categorical(['s','t','u']),
8. 'numeric': [1, 2, 3],
9. 'object': ['p', 'q', 'r']
10. })
11. info.describe(include=[np.number])
12. info.describe(include=[np.object])
13. info.describe(include=['category'])

Output

categorical
count 3
unique 3
top u
freq 1

Example4

1. import pandas as pd
2. import numpy as np
3. a1 = pd.Series([1, 2, 3])
4. a1.describe()
5. a1 = pd.Series(['p', 'q', 'q', 'r'])
6. a1.describe()
7. info = pd.DataFrame({'categorical': pd.Categorical(['s','t','u']),
8. 'numeric': [1, 2, 3],
9. 'object': ['p', 'q', 'r']
10. })
11. info.describe()
12. info.describe(include='all')
13. info.numeric.describe()
14. info.describe(include=[np.number])
15. info.describe(include=[np.object])
16. info.describe(include=['category'])
17. info.describe(exclude=[np.number])
18. info.describe(exclude=[np.object])
Output

categorical numeric
count 3 3.0
unique 3 NaN
top u NaN
freq 1 NaN
mean NaN 2.0
std NaN 1.0
min NaN 1.0
25% NaN 1.5
50% NaN 2.0
75% NaN 2.5
max NaN 3.0

Pandas DataFrame.drop_duplicates()
The drop_duplicates() function performs common data cleaning task that deals with
duplicate values in the DataFrame. This method helps in removing duplicate values
from the DataFrame.

Syntax

1. DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)

Parameters

o subset: It takes a column or the list of column labels. It considers only certain
columns for identifying duplicates. Default value None.
o keep: It is used to control how to consider duplicate values. It has three distinct
values that are as follows:
o first: It drops the duplicate values except for the first occurrence.
o last: It drops the duplicate values except for the last occurrence.
o False: It drops all the duplicates.
o inplace: Returns the boolean value. Default value is False.

If it is true, it removes the rows with duplicate values.

Return
Depending on the arguments passed, it returns the DataFrame with the removal of
duplicate rows.

Example
1. import pandas as pd
2. emp = {"Name": ["Parker", "Smith", "William", "Parker"],
3. "Age": [21, 32, 29, 21]}
4. info = pd.DataFrame(emp)
5. print(info)

Output

History of Java
Name Age
0 Parker 21
1 Smith 32
2 William 29
3 Parker 21

1. import pandas as pd
2. emp = {"Name": ["Parker", "Smith", "William", "Parker"],
3. "Age": [21, 32, 29, 21]}
4. info = pd.DataFrame(emp)
5. info = info.drop_duplicates()
6. print(info)

Output

Name Age
0 Parker 21
1 Smith 32
2 William 29

Pandas DataFrame.groupby()
In Pandas, groupby() function allows us to rearrange the data by utilizing them on
real-world data sets. Its primary task is to split the data into various groups. These
groups are categorized based on some criteria. The objects can be divided from any
of their axes.

Syntax:

1. DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True,


group_keys=True, squeeze=False, **kwargs)

This operation consists of the following steps for aggregating/grouping the data:
o Splitting datasets
o Analyzing data
o Aggregating or combining data

Note: The result of Groupby operation is not a DataFrame, but dict of DataFrame
objects.

Split data into groups


There are multiple ways to split any object into the group which are as follows:

o obj.groupby('key')
o obj.groupby(['key1','key2'])
o obj.groupby(key,axis=1)

We can also add some functionality to each subset. The following operations can be
performed on the applied functionality:

10.8M

218

HTML Tutorial

o Aggregation: Computes summary statistic.


o Transformation: It performs some group-specific operation.
o Filtration: It filters the data by discarding it with some condition.

Aggregations
It is defined as a function that returns a single aggregated value for each of the groups.
We can perform several aggregation operations on the grouped data when
the groupby object is created.

Example

1. # import the pandas library


2. import pandas as pd
3. import numpy as np
4. data = {'Name': ['Parker', 'Smith', 'John', 'William'],
5. 'Percentage': [82, 98, 91, 87],
6. 'Course': ['B.Sc','B.Ed','M.Phill','BA']}
7. df = pd.DataFrame(data)
8.
9. grouped = df.groupby('Course')
10. print(grouped['Percentage'].agg(np.mean))

Output

Course
B.Ed 98
B.Sc 82
BA 87
M.Phill 91
Name: Percentage, dtype: int64

Transformations
It is an operation on a group or column that performs some group-specific
computation and returns an object that is indexed with the same size as of the group
size.

Example

1. # import the pandas library


2. import pandas as pd
3. import numpy as np
4.
5. data = {'Name': ['Parker', 'Smith', 'John', 'William'],
6. 'Percentage': [82, 98, 91, 87],
7. 'Course': ['B.Sc','B.Ed','M.Phill','BA']}
8. df = pd.DataFrame(data)
9.
10. grouped = df.groupby('Course')
11. Percentage = lambda x: (x - x.mean()) / x.std()*10
12. print(grouped.transform(Percentage))

Output

Percentage
0 NaN
1 NaN
2 NaN
3 NaN

Filtration
The filter() function filters the data by defining some criteria and returns the subset of
data.

Example

1. # import the pandas library


2. import pandas as pd
3. import numpy as np
4.
5. data = {'Name': ['Parker', 'Smith', 'John', 'William'],
6. 'Percentage': [82, 98, 91, 87],
7. 'Course': ['B.Sc','B.Ed','M.Phill','BA']}
8. df = pd.DataFrame(data)
9.
10. grouped = df.groupby('Course')
11. print (df.groupby('Course').filter(lambda x: len(x) >= 1))

Output

Name Percentage Course


0 Parker 82 B.Sc
1 Smith 98 B.Ed
2 John 91 M.Phill
3 William 87 BA

Parameters of Groupby:

o by: mapping, function, str, or iterable

Its main task is to determine the groups in the groupby. If we use by as a function, it
is called on each value of the object's index. If in case a dict or Series is passed, then
the Series or dict VALUES will be used to determine the groups.

If a ndarray is passed, then the values are used as-is determine the groups.

We can also pass the label or list of labels to group by the columns in the self.

o axis: {0 or 'index', 1 or 'columns'}, default value 0


o level: int, level name, or sequence of such, default value None.

It is used when the axis is a MultiIndex (hierarchical), so, it will group by a particular
level or levels.
o as_index: bool, default True
It returns the object with group labels as the index for the aggregated output.
o sort: bool, default True
It is used to sort the group keys. Get better performance by turning this off.

Note: It does not influence the order of observations within each group. The
Groupby preserves the order of rows within each group.
o group_keys: bool, default value True

When we call it, it adds the group keys to the index for identifying the pieces.

o observed: bool, default value False


It will be used only if any of the groupers are the Categoricals. If the value is True,
then it will show only the observed values for categorical groupers. Otherwise, it
will show all of its values.
o **kwargs
It is an optional parameter that only accepts the keyword argument 'mutated'
that is passed to groupby.

Returns
It returns the DataFrameGroupBy or SeriesGroupBy. The return value depends
on the calling object that consists of information about the groups.

Example

1. import pandas as pd
2. info = pd.DataFrame({'Name': ['Parker', 'Smith','John', 'William'],'Percentage': [
92., 98., 89., 86.]})
3. info

Output
Example

1. # import the pandas library


2. import pandas as pd
3.
4. data = {'Name': ['Parker', 'Smith', 'John', 'William'],
5. 'Percentage': [82, 98, 91, 87],}
6. info = pd.DataFrame(data)
7.
8. print (info)

Pandas DataFrame.head()
The head() returns the first n rows for the object based on position. If your object has
the right type of data in it, it is useful for quick testing. This method is used for
returning top n (by default value 5) rows of a data frame or series.

Syntax

1. DataFrame.head(n=5)

Parameters
n: It refers to an integer value that returns the number of rows.

Return
It returns the DataFrame with top n rows.

Example1
1. info = pd.DataFrame({'language':['C', 'C++', 'Python', 'Java','PHP']})
2. info.head()
3. info.head(3)

Output

language
0 C
1 C++
2 Python

Example 2
We have a csv file "aa.csv" that have the following dataset.

1. Name Hire Date Salary Leaves Remaining


2. 0 John Idle 03/15/14 50000.0 10
3. 1 Smith Gilliam 06/01/15 65000.0 8
4. 2 Parker Chapman 05/12/14 45000.0 10
5. 3 Jones Palin 11/01/13 70000.0 3
6. 4 Terry Gilliam 08/12/14 48000.0 7

By using the head() in the below example, we showed only top 2 rows from the dataset.

1. # importing pandas module


2. import pandas as pd
3. # making data frame
4. data = pd.read_csv("aa.csv")
5. # calling head() method
6. # storing in new variable
7. data_top = data.head(2)
8. # display
9. data_top

Name Hire Date Salary Leaves Remaining


0 John Idle 03/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 8

Pandas DataFrame.hist()
The hist() function is defined as a quick way to understand the distribution of certain
numerical variables from the dataset. It divides the values within a numerical variable
into "bins". It counts the number of examinations that fall into each of the bin. These
bins are responsible for a rapid and intuitive sense of the distribution of the values
within a variable by visualizing bins.

We can create a histogram by using the DataFrame.hist() method, which is a wrapper


for the matplotlib pyplot API.

It is also a useful tool that quickly access the probability distribution.

Syntax

1. DataFrame.hist(data, column=None, by=None, grid=True, xlabelsize=None, xr


ot=None, ylabelsize=None, yrot=None, ax=None, sharex=False, sharey=False,
figsize=None, layout=None, bins=10, **kwds)

Parameters

o data: A DataFrame.
It is a pandas DataFrame object that holds the data.
o column: Refers to a string or sequence.
If it is passed, it will be used to limit the data to a subset of columns.
o by: It is an optional parameter. If it is passed, then it will be used to form the histogram
for independent groups.
o grid: It is also an optional parameter. Used for showing the axis grid lines. Default value
True.
o xlabelsize: Refers to the integer value. Default value None. Used for specifying the
changes in the x-axis label size.
o xrot: Refers to float value. Used for rotating the x-axis labels. Default value None.
o ylabelsize: Refers to an integer value. Used for specifying the changes in the y-axis
label size.
o yrot: Refers to the float value. Used for rotating the y-axis labels. Default value None.
o ax: Matplotlib axes object.
It defines the axis on which we need to plot the histogram. Default value None.
o sharex: Refers to the boolean value. Default value True, if ax is None else False. In the
case of subplots, if value is True, it shares the x-axis and sets some of the x-axis labels
to invisible. Its Default value is True.
If the ax is none, it returns False if an ax is passed in.
Note: Passing true in both an ax and sharex, it will alter all x-axis labels for all the
subplots.
o sharey: Default value False. In the case of subplots is True, it shares the y-axis and sets
some y-axis labels to invisible.
o figsize: Refers to the size in inches for the figure to create. By default, it uses the value
in matplotlib.rcParams.
o layout: It is an optional parameter. It returns the tuple of (rows, columns) for the layout
of the histograms.
o bins: Default value 10. It refers to the number of histogram bins that are to be used. If
an integer value is given, then it returns the calculated value of bins +1 bin edges.
o **kwds: Refers to all the other plotting keyword arguments that are to be passed to
matplotlib.pyplot.hist().

Returns
It returns the matplotlib.AxesSubplot or numpy.ndarray.

Example1

1. import pandas as pd
2. info = pd.DataFrame({
3. 'length': [2, 1.7, 3.6, 2.4, 1],
4. 'width': [4.2, 2.6, 1.6, 5.1, 2.9]
5. })
6. hist = info.hist(bins=4)

Output
Pandas DataFrame.iterrows()
If you want to loop over the DataFrame for performing some operations on each of
the rows then you can use iterrows() function in Pandas.

Pandas use three functions for iterating over the rows of the DataFrame, i.e., iterrows(),
iteritems() and itertuples().

Iterate rows with Pandas iterrows:


The iterrows () is responsible for loop through each row of the DataFrame. It returns
an iterator that contains index and data of each row as a Series.

We have the next function to see the content of the iterator.

This function returns each index value along with a series that contain the data in each
row.

o iterrows() - used for iterating over the rows as (index, series) pairs.
o iteritems() - used for iterating over the (key, value) pairs.
o itertuples() - used for iterating over the rows as namedtuples.

Yields:

o index: Returns the index of the row and a tuple for the MultiIndex.
o data: Returns the data of the row as a Series.
o it: Returns a generator that iterates over the rows of the frame.
Example1

1. import pandas as pd
2. import numpy as np
3.
4. info = pd.DataFrame(np.random.randn(4,2),columns = ['col1','col2'])
5. for row_index,row in info.iterrows():
6. print (row_index,row)

Output

0 name John
degree B.Tech
score 90
Name: 0, dtype: object

1 name Smith
degree B.Com
score 40
Name: 1, dtype: object

2 name Alexander
degree M.Com
score 80
Name: 2, dtype: object

3 name William
degree M.Tech
score 98
Name: 3, dtype: object

Example2

1. # importing pandas module


2. import pandas as pd
3.
4. # making data frame from csv file
5. data = pd.read_csv("aa.csv")
6.
7. for i, j in data.iterrows():
8. print(i, j)
9. print()

Output

0 Name Hire Date Salary Leaves


Remaining 0 John Idle 03/15/14 50...
Name: 0, dtype: object
1 Name Hire Date Salary Leaves
Remaining 1 Smith Gilliam 06/01/15 65000...
Name: 1, dtype: object

2 Name Hire Date Salary Leaves


Remaining 2 Parker Chapman 05/12/14 45000.0 ...
Name: 2, dtype: object

3 Name Hire Date Salary Leaves


Remaining 3 Jones Palin 11/01/13 700...
Name: 3, dtype: object

4 Name Hire Date Salary Leaves


Remaining 4 Terry Gilliam 08/12/14 4800...
Name: 4, dtype: object

5 Name Hire Date Salary Leaves


Remaining 5 Michael Palin 05/23/13 66000...
Name: 5, dtype: object

Pandas DataFrame.join()
When we want to concatenate our DataFrames, we can add them with each other by
stacking them either vertically or side by side. Another method to combine these
DataFrames is to use columns in each dataset that contain common values. The
method of combining the DataFrame using common fields is called "joining". The
method that we use for combining the DataFrame is a join() method. The columns
that contain common values are called "join key".

The join() method is often useful when one DataFrame is a lookup table that contains
additional data added into the other DataFrame. It is a convenient method that can
combine the columns of two differently-indexed DataFrames into a single DataFrame.

Identifying join keys


To determine the appropriate join keys, first, we have to define required fields that are
shared between the DataFrames. Both the DataFrames consist of the columns that
have the same name and also contain the same data.

Inner joins
Inner join can be defined as the most commonly used join. Basically, its main task is to
combine the two DataFrames based on a join key and returns a new DataFrame. The
returned DataFrame consists of only selected rows that have matching values in both
of the original DataFrame.

Left joins
If we want to add some information into the DataFrame without losing any of the data,
we can simply do it through a different type of join called a "left outer join" or "left
join".

Like an inner join, left join also uses the join keys to combine two DataFrames, but
unlike inner join, it returns all of the rows from the left DataFrame, even those rows
whose join keys do not include the values in the right DataFrame.

Syntax:

1. DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False)

Parameters:
other: It refers to the DataFrame or Series.

In this case, the index should be similar to one of the columns. If we pass a Series, the
named attribute has to be set for using it as the column name in the resulting joined
DataFrame.

on: It is an optional parameter that refers to array-like or str values.

It refers to a column or index level name in the caller to join on the index. Otherwise,
it joins index-on-index. If multiple values are present, then the other DataFrame must
have MultiIndex. It is like an Excel VLOOKUP operation that can pass an array as the
join key if it is not already contained within the calling DataFrame.

how: It refers to 'left', 'right', 'outer', 'inner' values that mainly work on how to handle
the operation of the two objects. The default value of how is left.

o left: It uses a calling frame's index or column if the parameter on is specified.


o right: It uses the other index.
o outer: It is used to form a union of calling frame's index or column if parameter on is
specified with other's index, and also sort it lexicographically.
o inner: It is used to form an intersection of calling frame's index or column if
parameter on is specified with other's index. So, due to this, it preserves the order of
the calling object.

lsuffix: It refers to a string object that has the default value ''. It uses the Suffix from
the left frame's overlapping columns.
rsuffix: It refers to a string value, that has the default value ''. It uses the Suffix from
the right frame's overlapping columns.

sort: It consists of a boolean value that sorts the resulting DataFrame lexicographically
by the join key. If we pass False value, then the order of the join key mainly depends
on the join type, i.e., how.

Example: The below example shows the working of join() function.

1. import pandas as pd
2. info = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
3. 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
4. x = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
5. 'B': ['B0', 'B1', 'B2']})
6. info.join(x, lsuffix='_caller', rsuffix='_x')
7. info.set_index('key').join(x.set_index('key'))
8. info.join(x.set_index('key'), on='key')

Output:

key A B
0 K0 A0 B0
1 K1 A1 B1
2 K2 A2 B2
3 K3 A3 NaN
4 K4 A4 NaN
5 K5 A5 NaN

Example2: The below example joins the two MultiIndexes:

1. import pandas as pd
2. leftindex = pd.MultiIndex.from_product([list('xyz'), list('pq'), [1, 2]],
3. names=['xyz', 'pq', 'num'])
4. left = pd.DataFrame({'value': range(12)}, index=leftindex)
5. left

Output:

value
xyz pq num
x p 1 0
2 1
q 1 2
2 3
y p 1 4
2 5
q 1 6
2 7
z p 1 8
2 9
q 1 10
2 11

Pandas DataFrame.mean()
The mean() function is used to return the mean of the values for the requested axis. If
we apply this method on a Series object, then it returns a scalar value, which is the
mean value of all the observations in the dataframe.

If we apply this method on a DataFrame object, then it returns a Series object which
contains mean of values over the specified axis.

Syntax

1. DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None,


**kwargs)

Parameters

o axis: {index (0), columns (1)}.


This refers to the axis for a function that is to be applied.
o skipna: It excludes all the null values when computing result.
o level: It counts along with a particular level and collapsing into a Series if the axis is a
MultiIndex (hierarchical),
o numeric_only: It includes only int, float, boolean columns. If None, it will attempt to
use everything, then use only numeric data. Not implemented for Series.

Returns
It returns the mean of the Series or DataFrame if the level is specified.

Example

1. # importing pandas as pd
2. import pandas as pd
3. # Creating the dataframe
4. info = pd.DataFrame({"A":[8, 2, 7, 12, 6],
5. "B":[26, 19, 7, 5, 9],
6. "C":[10, 11, 15, 4, 3],
7. "D":[16, 24, 14, 22, 1]})
8. # Print the dataframe
9. info
10. # If axis = 0 is not specified, then
11. # by default method return the mean over
12. # the index axis
13. info.mean(axis = 0)

Output

A 7.0
B 13.2
C 8.6
D 15.4
dtype: float64

Example2

1. # importing pandas as pd
2. import pandas as pd
3. # Creating the dataframe
4. info = pd.DataFrame({"A":[5, 2, 6, 4, None],
5. "B":[12, 19, None, 8, 21],
6. "C":[15, 26, 11, None, 3],
7. "D":[14, 17, 29, 16, 23]})
8. # while finding mean, it skip null values
9. info.mean(axis = 1, skipna = True)

Output

0 11.500000
1 16.000000
2 15.333333
3 9.333333
4 15.666667
dtype: float64

Pandas melt()
The Pandas.melt() function is used to unpivot the DataFrame from a wide format to a
long format.
Its main task is to massage a DataFrame into a format where some columns are
identifier variables and remaining columns are considered as measured variables, are
unpivoted to the row axis. It leaves just two non-identifier columns, variable and value.

Syntax

1. pandas.melt(frame, id_vars=None, value_vars=None,


2. var_name=None, value_name='value', col_level=None)

Parameters

o frame: It refers to the DataFrame.


o id_vars[tuple, list, or ndarray, optional]: It refers to the columns to use as identifier
variables.
o value_vars[tuple, list, or ndarray, optional]: Refers to columns to unpivot. If it is not
specified, use all columns that are not set as id_vars.
o var_name[scalar]: Refers to a name to use for the 'variable' column. If it is None, it
uses frame.columns.name or 'variable'.
o value_name[scalar, default 'value']: Refers to a name to use for the 'value' column.
o col_level[int or string, optional]: It will use this level to melt if the columns are
MultiIndex.

Returns
It returns the unpivoted DataFrame as the output.

Example

1. # importing pandas as pd
2. import pandas as pd
3. # creating a dataframe
4. info = pd.DataFrame({'Name': {0: 'Parker', 1: 'Smith', 2: 'John'},
5. 'Language': {0: 'Python', 1: 'Java', 2: 'C++'},
6. 'Age': {0: 22, 1: 30, 2: 26}})
7.
8. # Name is id_vars and Course is value_vars
9. pd.melt(info, id_vars =['Name'], value_vars =['Language'])
10. info
Output

Name

Example2

1. import pandas as pd
2. info = pd.DataFrame({'A': {0: 'p', 1: 'q', 2: 'r'},
3. 'B': {0: 40, 1: 55, 2: 25},
4. 'C': {0: 56, 1: 62, 2: 42}})
5. pd.melt(info, id_vars=['A'], value_vars=['C'])
6. pd.melt(info, id_vars=['A'], value_vars=['B', 'C'])
7. pd.melt(info, id_vars=['A'], value_vars=['C'],
8. var_name='myVarname', value_name='myValname')
9.

Pandas DataFrame.merge()
Pandas merge() is defined as the process of bringing the two datasets together into
one and aligning the rows based on the common attributes or columns. It is an entry
point for all standard database join operations between DataFrame objects:

Syntax:

1. pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,


2. left_index=False, right_index=False, sort=True)

Parameters:

o right: DataFrame or named Series


It is an object which merges with the DataFrame.
o how: {'left', 'right', 'outer', 'inner'}, default 'inner'
Type of merge to be performed.
o left: It use only keys from the left frame, similar to a SQL left outer join; preserve
key order.
o right: It use only keys from the right frame, similar to a SQL right outer join;
preserve key order.
o outer: It used the union of keys from both frames, similar to a SQL full outer
join; sort keys lexicographically.
o inner: It use the intersection of keys from both frames, similar to a SQL inner
join; preserve the order of the left keys.
o on: label or list
It is a column or index level names to join on. It must be found in both the left and
right DataFrames. If on is None and not merging on indexes, then this defaults to the
intersection of the columns in both DataFrames.
left_on: label or list, or array-like
It is a column or index level names from the left DataFrame to use as a key. It can be
an array with length equal to the length of the DataFrame.
o right_on: label or list, or array-like
It is a column or index level names from the right DataFrame to use as keys. It can be
an array with length equal to the length of the DataFrame.
o left_index : bool, default False
It uses the index from the left DataFrame as the join key(s), If true. In the case of
MultiIndex (hierarchical), many keys in the other DataFrame (either the index or some
columns) should match the number of levels.
o right_index : bool, default False
It uses the index from the right DataFrame as the join key. It has the same usage as the
left_index.
o sort: bool, default False
If True, it sorts the join keys in lexicographical order in the result DataFrame. Otherwise,
the order of the join keys depends on the join type (how keyword).
o suffixes: tuple of the (str, str), default ('_x', '_y')
It suffixes to apply to overlap the column names in the left and right DataFrame,
respectively. The columns use (False, False) values to raise an exception on overlapping.
o copy: bool, default True
If True, it returns a copy of the DataFrame.
Otherwise, It can avoid the copy.
o indicator: bool or str, default False
If True, It adds a column to output DataFrame "_merge" with information on the source
of each row. If it is a string, a column with information on the source of each row will
be added to output DataFrame, and the column will be named value of a string. The
information column is defined as a categorical-type and it takes value of:
o "left_only" for the observations whose merge key appears only in 'left' of the
DataFrame, whereas,
o "right_only" is defined for observations in which merge key appears only in
'right' of the DataFrame,
o "both" if the observation's merge key is found in both of them.
o validate: str, optional
If it is specified, it checks the merge type that is given below:
o "one_to_one" or "1:1": It checks if merge keys are unique in both the left and
right datasets.
o "one_to_many" or "1:m": It checks if merge keys are unique in only the left
dataset.
o "many_to_one" or "m:1": It checks if merge keys are unique in only the right
dataset.
o "many_to_many" or "m:m": It is allowed, but does not result in checks.

Example1: Merge two DataFrames on a key

1. # import the pandas library


2. import pandas as pd
3. left = pd.DataFrame({
4. 'id':[1,2,3,4],
5. 'Name': ['John', 'Parker', 'Smith', 'Parker'],
6. 'subject_id':['sub1','sub2','sub4','sub6']})
7. right = pd.DataFrame({
8. 'id':[1,2,3,4],
9. 'Name': ['William', 'Albert', 'Tony', 'Allen'],
10. 'subject_id':['sub2','sub4','sub3','sub6']})
11. print (left)
12. print (right)

Output
id Name subject_id

0 1 John sub1
1 2 Parker sub2
2 3 Smith sub4
3 4 Parker sub6

id Name subject_id

0 1 William sub2
1 2 Albert sub4
2 3 Tony sub3
3 4 Allen sub6

Example2: Merge two DataFrames on multiple keys:

1. import pandas as pd
2. left = pd.DataFrame({
3. 'id':[1,2,3,4,5],
4. 'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
5. 'subject_id':['sub1','sub2','sub4','sub6','sub5']})
6. right = pd.DataFrame({
7. 'id':[1,2,3,4,5],
8. 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
9. 'subject_id':['sub2','sub4','sub3','sub6','sub5']})
10. print pd.merge(left,right,on='id')

Output

id Name_x subject_id_x Name_y subject_id_y


0 1 John sub1 William sub2
1 2 Parker sub2 Albert sub4
2 3 Smith sub4 Tony sub3
3 4 Parker sub6 Allen sub6

Pandas DataFrame.pivot_table()
The Pandas pivot_table() is used to calculate, aggregate, and summarize your data. It
is defined as a powerful tool that aggregates data with calculations such as Sum,
Count, Average, Max, and Min.

It also allows the user to sort and filter your data when the pivot table has been created.

Parameters:

o data: A DataFrame.
o values: It is an optional parameter and refers the column to aggregate.
o index: It refers to the column, Grouper, and array.

If we pass an array, it must be of the same length as data.

o columns: Refers to column, Grouper, and array

If we pass an array, it must be of the same length as data.

Difference between JDK, JRE, and JVM

o aggfunc: function, list of functions, dict, default numpy.mean


If we pass the list of functions, the resulting pivot table will have hierarchical columns
whose top level are the function names.
If we pass a dict, the key is referred to as a column to aggregate, and value is function
or list of functions.
o fill_value[scalar, default None]: It replaces the missing values with a value.
o margins[boolean, default False]: It add all the row / columns (e.g. for subtotal / grand
totals)
o dropna[boolean, default True] : It drops the columns whose entries are all NaN.
o margins_name[string, default 'All'] : It refers to the name of the row/column that
will contain the totals when margins are True.

Returns:
It returns a DataFrame as the output.

Example:

1. # importing pandas as pd
2. import pandas as pd
3. import numpy as np
4.
5. # create dataframe
6. info = pd.DataFrame({'P': ['Smith', 'John', 'William', 'Parker'],
7. 'Q': ['Python', 'C', 'C++', 'Java'],
8. 'R': [19, 24, 22, 25]})
9. info
10. table = pd.pivot_table(info, index =['P', 'Q'])
11. table

Output

P Q R
John C 24
Parker Java 25
Smith Python 19
William C 22

Pandas DataFrame.query()
For analyzing the data, we need a lot of filtering operations. Pandas provide a query()
method to filter the DataFrame.

It offers a simple way of making the selection and also capable of simplifying the task
of index-based selection.

Syntax

1. DataFrame.query(expr, inplace=False, **kwargs)

Parameters

o expr: Refers to an expression in string form to filter data.


o inplace: If the value is True, it makes the changes in the original DataFrame.
o kwargs: Refers to the other keyword arguments.

Return
It returns a DataFrame that results from the query expression.

Note: This method only works if the column name doesn't have any empty spaces.
You can replace the spaces in column names with '_'

Example1

1. info = pd.DataFrame({'X': range(1, 6),


2. 'Y': range(10, 0, -2),
3. 'Z Z': range(10, 5, -1)})
4. info
5. info.query('X > Y')
6. info[info.X > info.Y]
7. info[info.Y == info['Z Z']]

Output

X Y Z Z
0 1 10 10

Pandas DataFrame.rename()
The main task of the Pandas rename() function is to rename any index, column, or
row. This method is useful for renaming some selected columns because we have to
specify the information only for those columns that we want to rename.

It mainly alters the axes labels based on some of the mapping (dict or Series) or the
arbitrary function. The function must be unique and should range from 1 to -1. The
labels will be left, if it is not contained in a dict or Series. If you list some extra labels,
it will throw an error.

Syntax:

1. DataFrame.rename(mapper=None, index=None, columns=None, axis=None,


copy=True, inplace=False, level=None, errors='ignore')

Parameters:

o mapper: It is a dict-like or function transformation that is to be applied to a particular


axis label. We can use either mapper or axis to specify the axis targeted with mapper,
index, and
o index: It is an alternative of specifying the axis (mapper, axis =0 is equivalent to
the index=mapper).
o columns: It is an alternative to specify an axis (mapper, axis =1 is equivalent to
the columns=mapper).
o axis: It refers to an int or str value that defines the axis targeted with the mapper. It
can be either the axis name ('index', 'columns') or the number.
o copy: It refers to a boolean value that copies the underlying data. The default value of
the copy is True.
o inplace: It refers to a boolean value and checks whether to return the new DataFrame
or not. If it is true, it makes the changes in the original DataFrame. The default value of
the inplace is True.
o level: It refers to an int or level name values that specify the level, if DataFrame has a
multiple level index. The default value of the level is None.
o errors: It refers to ignore, raise If we specify raise value, it raises a KeyError if any of
the labels are not found in the selected axis.

Returns:
It returns the DataFrame with renamed axis labels.

Example 1: The below example renames a single column:

1. import pandas as pd
2. # Define a dictionary containing information of employees
3. info = {'name': ['Parker', 'Smith', 'William', 'Robert'],
4. 'age': [38, 47, 44, 34],
5. 'language': ['Java', 'Python', 'JavaScript', 'Python']}
6. # Convert dictionary into DataFrame
7. info_pd = pd.DataFrame(info)
8. # Before renaming columns
9. print(info_pd)
10. info_pd.rename(columns = {'name':'Name'}, inplace = True)
11. # After renaming columns
12. print("\nAfter modifying first column:\n", info_pd.columns

Output:

name age language


0 Parker 38 Java
1 Smith 47 Python
2 William 44 JavaScript
3 Robert 34 Python
After modifying first column:
Index(['Name', 'age', 'language'], dtype='object')

Example2: The below example renames the multiple columns:

1. import pandas as pd
2. # Define a dictionary containing information of employees
3. info = {'name': ['Parker', 'Smith', 'William', 'Robert'],
4. 'age': [38, 47, 44, 34],
5. 'language': ['Java', 'Python', 'JavaScript', 'Python']}
6. # Convert dictionary into DataFrame
7. info_pd = pd.DataFrame(info)
8. # Before renaming columns
9. print(info_pd)
10. info_pd.rename(columns = {'name':'Name', 'age':'Age', 'language':'Language'}, inplace
= True)
11. # After renaming columns
12. print(info_pd.columns)

Output:

name age language


0 Parker 38 Java
1 Smith 47 Python
2 William 44 JavaScript
3 Robert 34 Python
Index(['Name', 'Age', 'Language'], dtype='object')

Example3: The below example renames indexes of a particular column:

1. import pandas as pd
2. data = {'Name': ['Smith', 'Parker', 'William'], 'Emp_ID': [101, 102, 103], 'Language': ['Pyt
hon', 'Java', 'JavaScript']}
3. info1 = pd.DataFrame(data)
4. print('DataFrame:\n', info1)
5. info2 = info.rename(index={0: '#0', 1: '#1', 2: '#2'})
6. print('Renamed Indexes:\n', info2)

Output:

DataFrame:
Name Emp_ID Language
0 Smith 101 Python
1 Parker 102 Java
2 William 103 JavaScript
Renamed Indexes:
Name Emp_ID Language
#0 Smith 101 Python
#1 Parker 102 Java
#2 William 103 JavaScript

Pandas Dataframe.sample()
The Pandas sample() is used to select the rows and columns from the DataFrame
randomly. If we want to build a model from an extensive dataset, we have to randomly
choose a smaller sample of the data that is done through a function sample.
Syntax

1. DataFrame.sample(n=None, frac=None, replace=False, weights=None, rando


m_state=None, axis=None)

Parameters

o n: It is an optional parameter that consists of an integer value and defines the number
of random rows generated.
o frac: It is also an optional parameter that consists of float values and returns float
value * length of data frame values. It cannot be used with a parameter n.
o replace: It consists of boolean value. If it is true, it returns a sample with replacement.
The default value of the replace is false.
o weights: It is also an optional parameter that consists of str or ndarray-like. Default
value "None" that results in equal probability weighting.
If a Series is being passed; it will align with the target object on the index. The index
values in weights that are not found in the sampled object will be ignored, and index
values in the sampled object not in weights will be assigned zero weights.
If a DataFrame is being passed when axis =0; it will accept the name of a column.
If the weights are Series; then, the weights must be of the same length as axis being
sampled.
If the weights are not equal to 1; it will be normalized to the sum of 1.
The missing value in the weights column is considered as zero.
Infinite values are not allowed in the weights column.
o random_state: It is also an optional parameter that consists of an integer or
numpy.random.RandomState. If the value is int, it seeds for the random number
generator or numpy RandomState object.
o axis: It is also an optional parameter that consists of integer or string value. 0 or 'row'
and 1 or 'column'.

Returns
It returns a new object of the same type as a caller that contains n items randomly
sampled from the caller object.

Example1

1. import pandas as pd
2. info = pd.DataFrame({'data1': [2, 4, 8, 0],
3. 'data2': [2, 0, 0, 0],
4. 'data3': [10, 2, 1, 8]},
5. index=['John', 'Parker', 'Smith', 'William'])
6. info
7. info['data1'].sample(n=3, random_state=1)
8. info.sample(frac=0.5, replace=True, random_state=1)
9. info.sample(n=2, weights='data3', random_state=1)

Output

data1 data2 data3


John 2 2 10
William 0 0 8

Example2
In this example, we take a csv file and extract random rows from the DataFrame by
using a sample.

The csv file named as aa that contains the following dataset:

Let's write a code that extract the random rows from the above dataset:

1. # importing pandas package


2. import pandas as pd
3. # define data frame from csv file
4. data = pd.read_csv("aa.csv")
5. # randomly select one row
6. row1 = data.sample(n = 1)
7. # display row
8. row1
9. # randomly select another row
10. row2 = data.sample(n = 2)
11. # display row
12. row2

Output

Name Hire Date Salary Leaves Remaining


2 Parker Chapman 02/21/14 45000.0 10
5 Michael Palin 06/28/13 66000.0 8

Pandas DataFrame.shift()
If you want to shift your column or subtract the column value with the previous row
value from the DataFrame, you can do it by using the shift() function. It consists of a
scalar parameter called period, which is responsible for showing the number of shifts
to be made over the desired axis. It is also capable of dealing with time-series data.

Syntax:

1. DataFrame.shift(periods=1, freq=None, axis=0)

Parameters:

o periods: It consists of an integer value that can be positive or negative. It defines the
number of periods to move.
o freq: It can be used with DateOffset, tseries module, str or time rule (e.g., 'EOM').
o axis: 0 is used for shifting the index, whereas 1 is used for shifting the column.
o fill_value: Used for filling newly missing values.

Returns
It returns a shifted copy of DataFrame.

Example1: The below example demonstrates the working of the shift().

1. import pandas as pd
2. info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18],
3. 'b_data': [26, 37, 41, 35, 45],
4. 'c_data': [22, 19, 11, 25, 16]})
5. info.shift(periods=2)

Output

a_data b_data c_data


0 NaN NaN NaN
1 NaN NaN NaN
2 45.0 26.0 22.0
3 28.0 37.0 19.0
4 39.0 41.0 11.0

Example2: The example shows how to fill the missing values in the DataFrame using
the fill_value.

1. import pandas as pd
2. info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18],
3. 'b_data': [26, 38, 41, 35, 45],
4. 'c_data': [22, 19, 11, 25, 16]})
5. info.shift(periods=2)
6. info.shift(periods=2,axis=1,fill_value= 70)

Output

a_data b_data c_data


0 70 70 45
1 70 70 28
2 70 70 39
3 70 70 32
4 70 70 18

Pandas DataFrame.sort()
We can efficiently perform sorting in the DataFrame through different kinds:

o By label
o By Actual value

Before explaining these two kinds of sorting, first we have to take the dataset for
demonstration:

1. import pandas as pd
2. import numpy as np
3.
4. info=pd.DataFrame(np.random.randn(10,2),index=[1,3,7,2,4,5,9,8,0,6],columns=['col2',
'col1'])
5. print(info)

Output

col2 col1
1 -0.456763 -0.931156
3 0.242766 -0.793590
7 1.133803 0.454363
2 -0.843520 -0.938268
4 -0.018571 -0.315972
5 -1.951544 -1.300100
9 -0.711499 0.031491
8 1.648080 0.695637
0 2.576250 -0.625171
6 -0.301717 0.879970

In the above DataFrame, the labels and the values are unsorted. So, let's see how it can
be sorted:

o By label

The DataFrame can be sorted by using the sort_index() method. It can be done by
passing the axis arguments and the order of sorting. The sorting is done on row labels
in ascending order by default.

Example

1. import pandas as pd
2. import numpy as np
3. info=pd.DataFrame(np.random.randn(10,2),index=[1,2,5,4,8,7,9,3,0,6],columns
= ['col4','col3'])
4. info2=info.sort_index()
5. print(info2)

Output

col4 col3
0 0.698346 1.897573
1 1.247655 -1.208908
2 -0.469820 -0.546918
3 -0.793445 0.362020
4 -1.184855 -1.596489
5 1.500156 -0.397635
6 -1.239635 -0.255545
7 1.110986 -0.681728
8 -1.797474 0.108840
9 0.063048 1.512421
o Order of Sorting

The order of sorting can be controlled by passing the Boolean value to the ascending
parameter.

Example:

1. import pandas as pd
2. import numpy as np
3. info= pd.DataFrame(np.random.randn(10,2),index=[1,4,7,2,5,3,0,8,9,6],columns
= ['col4','col5'])
4.
5. info_2 = info.sort_index(ascending=False)
6. print(info)

Output

col4 col5
1 0.664336 -1.846533
4 -0.456203 -1.255311
7 0.537063 -0.774384
2 -1.937455 0.257315
5 0.331764 -0.741020
3 -0.082334 0.304390
0 -0.983810 -0.711582
8 0.208479 -1.234640
9 0.656063 0.122720
6 0.347990 -0.410401

o Sort the Columns:

We can sort the columns labels by passing the axis argument respected to its values 0
or 1. By default, the axis=0, it sort by row.

Example:

1. import pandas as pd
2. import numpy as np
3.
4. info = pd.DataFrame(np.random.randn(10,2),index=[1,4,8,2,0,6,7,5,3,9],columns = ['col
4','col7'])
5. info_2=info.sort_index(axis=1)
6. print(info_2)

Output
col4 col7
1 -0.509367 -1.609514
4 -0.516731 0.397375
8 -0.201157 -0.009864
2 1.440567 1.058436
0 0.955486 -0.009777
6 -1.211133 0.415147
7 0.095644 0.531727
5 -0.881241 -0.871342
3 0.206327 -1.154724
9 1.418127 0.146788

By Actual Value
It is another kind through which sorting can be performed in the DataFrame. Like index
sorting, sort_values() is a method for sorting by the values.

It also provides a feature in which we can specify the column name of the DataFrame
with which values are to be sorted. It is done by passing the 'by' argument.

Example:

1. import pandas as pd
2. import numpy as np
3. info = pd.DataFrame({'col1':[7,1,8,3],'col2':[8,12,4,9]})
4. info_2 = info.sort_values(by='col2')
5. print(info_2)

Output

col1 col2
2 8 4
0 7 8
3 3 9
1 1 12

In the above output, observe that the values are sorted in col2 only, and the
respective col1 value and row index will alter along with col2. Thus, they look
unsorted.

Parameters

o columns: Before Sorting, you have to pass an object or the column names.
o ascending: A Boolean value is passed that is responsible for sorting in the ascending
order. Its default value is True.
o axis: 0 or index; 1 or 'columns'. The default value is 0. It decides whether you sort by
index or columns.
o inplace: A Boolean value is passed. The default value is false. It will modify any other
views on this object and does not create a new instance while sorting the DataFrame.
o kind: 'heapsort', 'mergesort', 'quicksort'. It is an optional parameter that is to be applied
only when you sort a single column or labels.
o na_position: 'first', 'last'. The 'first' puts NaNs at the beginning, while the 'last' puts
NaNs at the end. Default option last.

Pandas DataFrame.sum()
Pandas DataFrame.sum() function is used to return the sum of the values for the
requested axis by the user. If the input value is an index axis, then it will add all the
values in a column and works same for all the columns. It returns a series that contains
the sum of all the values in each column.

It is also capable of skipping the missing values in the DataFrame while calculating the
sum in the DataFrame.

Syntax:

1. DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None,


min_count=0, **kwargs)

Parameters

o axis: {index (0), columns (1)}

0 or 'index' is used for row-wise, whereas 1 or 'columns' is used for column-wise.

o skipna: bool, default True

It is used to exclude all the null values.

o level: int or level name, default None

It counts along a particular level and collapsing into a series, if the axis is a multiindex.

o numeric_only: bool, default value None

It includes only int, float, and boolean columns. If it is None, it will attempt to use
everything, so numeric data should be used.

o min_count: int, default value 0


It refers to the required number of valid values to perform any operation. If it is fewer
than the min_count non-NA values are present, then the result will be NaN.

o **kwargs: It is an optional parameter that is to be passed to a function.

Returns:
It returns the sum of Series or DataFrame if a level is specified.

Example1:

1. import pandas as pd
2. # default min_count = 0
3. pd.Series([]).sum()
4. # Passed min_count = 1, then sum of an empty series will be NaN
5. pd.Series([]).sum(min_count = 1)

Output

0.0
nan

Example2:

1. import pandas as pd
2. # making a dict of list
3. info = {'Name': ['Parker', 'Smith', 'William'],
4. 'age' : [32, 28, 39]}
5. data = pd.DataFrame(info)
6. # sum of all salary stored in 'total'
7. data['total'] = data['age'].sum()
8. print(data)

Output

Name age total


0 Parker 32 99
1 Smith 28 99
2 William 39 99

Pandas DataFrame.to_excel()
We can export the DataFrame to the excel file by using the to_excel() function.
To write a single object to the excel file, we have to specify the target file name. If we
want to write to multiple sheets, we need to create an ExcelWriter object with target
filename and also need to specify the sheet in the file in which we have to write.

The multiple sheets can also be written by specifying the unique sheet_name. It is
necessary to save the changes for all the data written to the file.

Note: If we create an ExcelWriter object with a file name that already exists, it will
erase the content of the existing file.

Syntax

1. DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format


=None, columns=None, header=True, index=True, index_label=None, startro
w=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='i
nf', verbose=True, freeze_panes=None)

Parameters

o excel_writer: A file path or existing ExcelWriter.


o sheet_name: It refers to the name of the sheet that contains the DataFrame.
o na_repr: Missing Data representation.
o float_format: It is an optional parameter that formats the string for floating-point
numbers.
o columns: Refers the column to write.
o header: It writes out the column names. If a list of the string is given, it is assumed to
be the aliases for the column names.
o index: It writes the index.
o index_label: Refers to the column label for the index column. If it is not specified, and
the header and index are True, then the index names are used. If DataFrame uses
MultiIndex, a sequence should be given.
o startrow: Default value 0. It refers to the upper left cell row to dump the DataFrame.
o startcol: Default value 0. It refers to the upper left cell column to dump the DataFrame.
o engine: It is an optional parameter that writes the engine to use, openpyxl, or
xlsxwriter.
o merge_cells: It returns the boolean value and its default value is True. It writes
MultiIndex and Hierarchical rows as the merged cells.
o encoding: It is an optional parameter that encodes the resulting excel file. It is only
necessary for the xlwt.
o inf_rep: It is also an optional parameter and its default value is inf. It usually represents
infinity.
o verbose: It returns a boolean value. It's default value is True.
It is used to display more information in the error logs.
o freeze_panes: It is also an optional parameter that specifies the one based
bottommost row and rightmost column that is to be frozen.

Example

1. import pandas as pd
2. # create dataframe
3. info_marks = pd.DataFrame({'name': ['Parker', 'Smith', 'William', 'Terry'],
4. 'Maths': [78, 84, 67, 72],
5. 'Science': [89, 92, 61, 77],
6. 'English': [72, 75, 64, 82]})
7.
8. # render dataframe as html
9. writer = pd.ExcelWriter('output.xlsx')
10. info_marks.to_excel(writer)
11. writer.save()
12. print('DataFrame is written successfully to the Excel File.')

Output

DataFrame is written successfully to the Excel file

Pandas DataFrame.transform
We can define Pandas DataFrame as a two-dimensional size-mutable, heterogeneous
tabular data structure with some labeled axes (rows and columns). Performing the
arithmetic operations will align both row and column labels. It can be considered as a
dict-like container for Series objects.

The main task of Pandas DataFrame.transform() function is to self produce a


DataFrame with its transformed values and it has the same axis length as self.

Syntax:

1. DataFrame.transform(func, axis=0, *args, **kwargs)

Parameters :
func : It is a function that is used for transforming the data.

axis : Refers to 0 or 'index', 1 or 'columns', default value 0.

*args: It is a positional arguments that is to be passed to a func.

**kwargs : It is a keyword arguments that is to be passed to a func.

Returns:
It returns the DataFrame that must have same length as self.

Example 1 : Use DataFrame.transform() function to add 10 to each element in the


dataframe.

1. # importing pandas as pd
2. importpandas as pd
3.
4. # Creating the DataFrame
5. info =pd.DataFrame({"P":[8, 2, 9, None, 3],
6. "Q":[4, 14, 12, 22, None],
7. "R":[2, 5, 7, 16, 13],
8. "S":[16, 10, None, 19, 18]})
9.
10. # Create the index
11. index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row']
12.
13. # Set the index
14. info.index =index_
15.
16. # Print the DataFrame
17. print(info)

Output:

P Q R S
A_Row 8.0 4.0 2.0 16.0
B_Row 2.0 14.0 5.0 10.0
C_Row 9.0 12.0 7.0 NaN
D_RowNaN 22.0 16.0 19.0
E_Row 3.0NaN 13.0 18.0

Example 2 : Use DataFrame.transform() function to find the square root and the result
of euler's number raised to each element of the dataframe.

1. # importing pandas as pd
2. importpandas as pd
3.
4. # Creating the DataFrame
5. info =pd.DataFrame({"P":[8, 2, 9, None, 3],
6. "Q":[4, 14, 12, 22, None],
7. "R":[2, 5, 7, 16, 13],
8. "S":[16, 10, None, 19, 18]})
9.
10. # Create the index
11. index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row']
12.
13. # Set the index
14. info.index =index_
15.
16. # Print the DataFrame
17. print(info)

Output:

P Q R S
A_Row 88.0 14.0 12.0 16.0
B_Row 12.0 14.0 15.0 10.0
C_Row 19.0 22.0 17.0 NaN
D_RowNaN 21.0 16.0 19.0
E_Row 13.0NaN 13.0 18.0
Pandas DataFrame.transpose()
The transpose() function helps to transpose the index and columns of the dataframe.
It reflects DataFrame over its main diagonal by writing the rows as columns and vice-
versa.

Syntax

1. DataFrame.transpose(*args, **kwargs)

Parameters
copy: If its value is True, then the underlying data is being copied. Otherwise, by
default, no copy is made, if possible.

*args, **kwargs: Both are additional keywords that do not affect, but has an
acceptance that provide compatibility with a numpy.

Returns
It returns the transposed DataFrame.

Example1

1. # importing pandas as pd
2. import pandas as pd
3. # Creating the DataFrame
4. info = pd.DataFrame({'Weight':[27, 44, 38, 10, 67],
5. 'Name':['William', 'John', 'Smith', 'Parker', 'Jones'],
6. 'Age':[22, 17, 19, 24, 27]})
7. # Create the index
8. index_ = pd.date_range('2010-10-04 06:15', periods = 5, freq ='H')
9. # Set the index
10. info.index = index_
11. # Print the DataFrame
12. print(info)
13. # return the transpose
14. result = info.transpose()
15. # Print the result
16. print(result)
Output

Weight Name Age


2010-10-04 06:15:00 27 William 22
2010-10-04 07:15:00 44 John 7
2010-10-04 08:15:00 38 Smith 19
2010-10-04 09:15:00 10 Parker 24
2010-10-04 10:15:00 67 Jones 27
2010-10-04 06:15:00 2010-10-04 07:15:00 2010-10-04 08:15:00 \
Weight 27 44 38
Name William John Smith
Age 22 7 19

2010-10-04 09:15:00 2010-10-04 10:15:00


Weight 10 67
Name Parker Jones
Age 24 27

Example2

1. # importing pandas as pd
2. import pandas as pd
3. # Creating the DataFrame
4. info = pd.DataFrame({"A":[8, 2, 7, None, 6],
5. "B":[4, 3, None, 9, 2],
6. "C":[17, 42, 35, 18, 24],
7. "D":[15, 18, None, 11, 12]})
8. # Create the index
9. index_ = ['Row1', 'Row2', 'Row3', 'Row4', 'Row5']
10. # Set the index
11. info.index = index_
12. # Print the DataFrame
13. print(info)
14. # return the transpose
15. result = info.transpose()
16. # Print the result
17. print(result)

Output

A B C D
Row_1 8.0 4.0 17 15.0
Row_2 2.0 3.0 42 18.0
Row_3 7.0 NaN 35 NaN
Row_4 NaN 9.0 18 11.0
Row_5 6.0 2.0 24 12.0
Row1 Row2 Row3 Row4 Row5
A 8.0 2.0 7.0 NaN 6.0
B 4.0 3.0 NaN 9.0 2.0
C 17.0 42.0 35.0 18.0 24.0
D 15.0 18.0 NaN 11.0 12.0

Pandas DataFrame.where()
The main task of the where() method is to check the data frame for one or more
conditions and return the result accordingly. By default, if the rows are not satisfying
the condition, it is filled with NaN value.

Syntax

1. DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, err


ors='raise', try_cast=False, raise_on_error=None)

Parameters

o cond: It refers to one or more conditions to check the data frame.


o other: It replaces the rows that do not satisfy the condition with the user-defined
object; the default value is NaN.
o inplace: Returns the boolean value. If the value is true, it makes the changes in the
dataframe itself.
o axis: An axis to check( row or columns).

Returns

Example1

1. import pandas as pd
2. import numpy as np
3. a = pd.Series(range(5))
4. a.where(a > 0)
5. a.mask(a > 0)
6. a.where(a > 1, 10)
7. info = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
8. info
9. b = info % 3 == 0
10. info.where(b, -info)
11. info.where(b, -info) == np.where(b, info, -info)
12. info.where(b, -info) == info.mask(~b, -info)

Output

A B
0 True True
1 True True
2 True True
3 True True
4 True True

Add a column to DataFrame Columns


We can add a new column to an existing DataFrame using different ways. For the
demonstration, first, we have to write a code to read the existing file, which consists of
some columns in a DataFrame.

1. import pandas as pd
2. aa = pd.read_csv("aa.csv")
3. aa.head()

The above code read the existing csv file and shows the data values column as the
output.

Output

Name Hire Date Salary Leaves Remaining

0 John Idle 03/15/14 50000.0 10

1 Smith Gilliam 06/01/15 65000.0 8

2 Parker Chapman 05/12/14 45000.0 10

3 Jones Palin 11/01/13 70000.0 3

4 Terry Gilliam 08/12/14 48000.0 7

5 Michael Palin 05/23/13 66000.0 8


Add new columns to a DataFrame using [] operator
If we want to add any new column at the end of the table, we have to use
the [] operator. Let's add a new column named "Age" into "aa" csv file.

1. import pandas as pd
2. aa = pd.read_csv("aa.csv")
3. aa["Age"] = "24"
4. aa.head()

This code adds a column "Age" at the end of the aa csv file. So, the new table after
adding a column will look like this:

Name Hire Date Salary Leaves Remaining Age


0 John Idle 03/15/14 50000.0 10 24
1 Smith Gilliam 06/01/15 65000.0 8 24
2 Parker Chapman 05/12/14 45000.0 10 24
3 Jones Palin 11/01/13 70000.0 3 24
4 Terry Gilliam 08/12/14 48000.0 7 24
5 Michael Palin 05/23/13 66000.0 8 24

In the above code, Age value has defined the universal value that means its value is
common to all the rows. If we specify a column name that does not exist, Pandas will
throw an error.

For ex:

1. aa["Designation"]

In the above code, Pandas will throw an error because the Designation column does
not exist.

But if we assign a value to that column, Pandas will generate a new column
automatically at the end of the table.

Add new columns in a DataFrame using insert()


We can also add a new column at any position in an existing DataFrame using a
method name insert.

For the demonstration, first, we have to write a code to read the existing file that
consists of some columns in a DataFrame.

1. import pandas as pd
2. aa = pd.read_csv("aa.csv")
3. aa.head()s

The above code read the existing csv file and shown the data values column in the
output.

Output

Name Hire Date Salary Leaves Remaining


0 John Idle 03/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 8
2 Parker Chapman 05/12/14 45000.0 10
3 Jones Palin 11/01/13 70000.0 3
4 Terry Gilliam 08/12/14 48000.0 7
5 Michael Palin 05/23/13 66000.0 8

Let's add a new column name "Department" into an existing "aa" csv file
using insert method.

1. import pandas as pd
2. aa = pd.read_csv("aa.csv")
3. aa.insert(2, column = "Department", value = "B.Sc")
4. aa.head()

Output

Name Hire Date Department Salary Leaves Remaining


0 John Idle 03/15/14 B.Sc 50000.0 10
1 Smith Gilliam 06/01/15 B.Sc 65000.0 8
2 Parker Chapman 05/12/14 B.Sc 45000.0 10
3 Jones Palin 11/01/13 B.Sc 70000.0 3
4 Terry Gilliam 08/12/14 B.Sc 48000.0 7
5 Michael Palin 05/23/13 B.Sc 66000.0 8

Convert Pandas DataFrame to Numpy array


For performing some high-level mathematical functions, we can convert Pandas
DataFrame to numpy arrays. It uses the DataFrame.to_numpy() function.

The DataFrame.to_numpy() function is applied on the DataFrame that returns the


numpy ndarray.

Syntax

1. DataFrame.to_numpy(dtype=None, copy=False)

Parameters
o dtype: It is an optional parameter that pass the dtype to numpy.asarray().
o copy: It returns the boolean value that has the default value False.

It ensures that the returned value is not a view on another array.

Returns
It returns the numpy.ndarray as an output.

Example1

1. import pandas as pd
2. pd.DataFrame({"P": [2, 3], "Q": [4, 5]}).to_numpy()
3. info = pd.DataFrame({"P": [2, 3], "Q": [4.0, 5.8]})
4. info.to_numpy()
5. info['R'] = pd.date_range('2000', periods=2)
6. info.to_numpy()

Output

array([[2, 4.0, Timestamp('2000-01-01 00:00:00')],


[3, 5.8, Timestamp('2000-01-02 00:00:00')]], dtype=object)

Example2

1. import pandas as pd
2. #initializing the dataframe
3. info = pd.DataFrame([[17, 62, 35],[25, 36, 54],[42, 20, 15],[48, 62, 76]],
4. columns=['x', 'y', 'z'])
5. print('DataFrame\n----------\n', info)
6. #convert the dataframe to a numpy array
7. arr = info.to_numpy()
8. print('\nNumpy Array\n----------\n', arr)

Output

DataFrame
----------
x y z
0 17 62 35
1 25 36 54
2 42 20 15
3 48 62 76
Numpy Array
----------
[[17 62 35]
[25 36 54]
[42 20 15]
[48 62 76]]

Convert Pandas DataFrame to CSV


The Pandas to_csv() function is used to convert the DataFrame into CSV data. To write
the CSV data into a file, we can simply pass a file object to the function. Otherwise, the
CSV data is returned in a string format.

Syntax:

1. DataFrame.to_csv(path_or_buf=None, sep=', ', na_rep='', float_format=None, c


olumns=None, header=True, index=True, index_label=None, mode='w', enco
ding=None, compression='infer', quoting=None, quotechar='"', line_terminat
or=None, chunksize=None, date_format=None, doublequote=True, escapech
ar=None, decimal='.')

Parameters:
path_or_buf: It refers to str or file handle. Basically, it defines the path of file or
object. The default value is None and if None value is passed, then it returns a string
value.

If we pass a file object, it should be opened with newline =" and disable the universal
newlines.

sep: It refers to a string value and consists a string of length 1. It's default value
is comma(,).

na_rep: It refers to a string value that represents null or missing values. The empty
string is the default value.

float_format: It also consists a string value that is responsible for formatting a string
for the floating-point numbers.

columns: It is an optional parameter that refers to a sequence to specify the columns


that need to be included in the CSV output.

header: It generally consists a boolean value or a list of string. If its value is set to False,
then the column names are not written in the output. It's default value is True.
If we pass a list of string as an input, it generally writes the column names in the output.
The length of the list of the file should be same as number of columns being written
in the CSV file.

index: If the value is set to True, index is included in the CSV data. Otherwise, the index
value is not written in CSV output.

index_label: It consists a str value or a sequence that is used to specify the column
name for index. It's default value is None.

mode: It refers a string value that is used for writing mode. It's default value is w.

encoding: It is an optional parameter consisting a string value that represents an


encoding used in the output file. The default value of encoding is UTF-8.

compression: It refers a str value that compress the mode among the following
values{'infer', 'gzip', 'bz2', 'zip', 'xz', None}. It detects the compression from the
extensions: '.gz', '.bz2', '.zip' or '.xz' if infer and path_or_buf is a path-like,
otherwise no compression occurs.

quoting: It is an optional parameter that is defined as a constant from the csv


module. Its default value is csv.QUOTE_MINIMAL. If you set a float_format then the
floating value is converted to strings and csv.QUOTE_NONNUMERIC is treated as a
non-numeric value.

quotechar: It refers to a str value of length 1. It is a character that is used to quote the
fields.

line_terminator: It is an optional parameter that refers to a string value. Its main task
is to terminate the line. It is a newline character that is to be used in the output file. Its
default value is set to os.linesep that mainly depends on the OS. An individual method
is called to define the operating system ('n' for linux, 'rn' for 'Windows').

chunksize: It consists None or integer value and define the rows to write at the
current time.

date_format: It consists str value and used to format a string for the datetime objects.
The default value of date_format is None.

doublequote: It consists a boolean value that have the default value True. It is mainly
used for controlling the quote of quotechar inside the field.

escapechar: It consists a string value of length 1. Basically, it is a character that is used


to escape sep and quotechar. The default value of escapechar is None.
decimal: It consists a string value that identify a character as decimal separator. Ex:
use ',' for European data.

Returns:
It returns str or None value. If a parameter value named as path_or_buf is None, it
returns the resulting csv format as a string. Otherwise, it returns None.

Example1: The below example convert a DataFrame to CSV String:

1. import pandas as pd
2. data = {'Name': ['Smith', 'Parker'], 'ID': [101, 102], 'Language': ['Python', 'JavaScript']}
3. info = pd.DataFrame(data)
4. print('DataFrame Values:\n', info)
5. # default CSV
6. csv_data = info.to_csv()
7. print('\nCSV String Values:\n', csv_data)

Output:

DataFrame Values:
Name ID Language
0 Smith 101 Python
1 Parker 102 JavaScript

CSV String Values:


,Name,ID,Language
0 ,Smith,101,Python
1 ,Parker,102,JavaScript

Example2: The below example shows Null or missing Data Representation in the CSV
Output file:

Example:

1. import pandas as pd
2. data = {'Name': ['Smith', 'Parker'], 'ID': [101, pd.NaT], 'Language': [pd.NaT, 'JavaScript']
}
3. info = pd.DataFrame(data)
4. print('DataFrame Values:\n', info)
5. csv_data = info.to_csv()
6. print('\nCSV String Values:\n', csv_data)
7. csv_data = info.to_csv(na_rep="None")
8. print('CSV String with Null Data Values:\n', csv_data)
Output:

DataFrame Values:
Name ID Language
0 Smith 101 NaT
1 Parker NaT JavaScript

CSV String Values:


,Name,ID,Language
0,Smith,101,
1,Parker,,JavaScript

CSV String with Null Data Values:


,Name,ID,Language
0,Smith,101,None
1,Parker,None,JavaScript

Example3: The below example specify the delimiter for the CSV output.

1. import pandas as pd
2. data = {'Name': ['Smith', 'Parker'], 'ID': [101, pd.NaT], 'Language': [Python, 'JavaScript']
}
3. info = pd.DataFrame(data)
4. print('DataFrame:\n', info)
5. csv_data = info.to_csv(sep='|')
6. print(csv_data)

Output:

DataFrame:
Name ID Language
0 Smith 101 Python
1 Parker NaT JavaScript
|Name|ID|Language
0|Smith|101|Python
1|Parker||JavaScript

Python Pandas Reading Files


Reading from CSV File
A csv stands for Comma Separated Values, which is defined as a simple file format that
uses specific structuring to arrange tabular data. It stores tabular data such as
spreadsheet or database in plain text and has a common format for data interchange.
The csv file is opened into the excel file, and the rows and columns data define the
standard format.
Reading the csv file into a pandas DataFrame is quick and straight forward. We don't
require to write several lines of code to open, analyze, and read the csv file in pandas.
Instead, we can perform these operations in a single line, and it stores the data in
DataFrame.

For reading the Pandas files, firstly we have to load data from file formats into a
DataFrame. You need only a single line to load your data in code.

1. Name,Hire Date,Salary,Leaves Remaining


2. John Idle,08/15/14,50000.00,10
3. Smith Gilliam,04/07/15,65000.00,6
4. Parker Chapman,02/21/14,45000.00,7
5. Jones Palin,10/14/13,70000.00,3
6. Terry Gilliam,07/22/14,48000.00,9
7. Michael Palin,06/28/13,66000.00,8
df = pd.read_csv('a.csv')

Code

1. import pandas
2. df = pandas.read_csv('hrdata.csv')
3. print(df)

In the above, the three lines of code are enough to read the file, and only one of them
is doing the actual work, i.e., pandas.read_csv().

Output:

Name Hire Date Salary Leaves


Remaining
0 John Idle 08/15/14 50000.0 10
1 Smith Gilliam 04/07/15 65000.0 8
2 Parker Chapman 02/21/14 45000.0 10
3 Jones Palin 10/14/13 70000.0 3
4 Terry Gilliam 07/22/14 48000.0 7
5 Michael Palin 06/28/13 66000.0 8

However, the pandas are also using the zero-based integer indices in the DataFrame;
we didn't tell it what our index should be.

Reading from JSON


If you have any JSON file, Pandas can easily read it through a single line of code.

1. df =pd.read_json('hrdata.json')
It allowed indexes to work through nesting.

Pandas convert a list of lists into a DataFrame and also define the column names
separately. A JSON parser is responsible for converting a JSON text into another
representation that must accept all the texts according to the JSON grammar. It can
also accept non JSON forms or extensions.

We have to import the JSON file before reading.

1. import pandas as pd
2. data = pd.read_json('hrdata.json')
3. print(data)

Output:

Name Hire Date Salary Leaves


Remaining
0 John Idle 08/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 6
2 Parker Chapman 05/12/14 45000.0 7
3 Jones Palin 11/01/13 70000.0 3

4 Terry Gilliam 08/12/14 48000.0 9


5 Michael Palin 05/23/13 66000.0 8

Reading from the SQL database


For reading a file from the SQL, first, you need to establish a connection using the
Python library and then pass the query to pandas. Here, we use SQLite for
demonstration.

Firstly, we have to install pysqlite3 and run this command into the terminal:

1. pip install pysqlite3

sqlite3 is used to establish a connection to the database, and then we can use it to
generate a DataFrame through SELECT query.

For establishing a connection to the SQLite database file:

1. import sqlite3
2. con = sqlite3.connect("database.db")
A table called information is present in the SQLite database, and the index of the
column called "index". We can read data from the information table by passing
the SELECT query and the con.

1. df = pd.read_sql_query("SELECT * FROM information", con)

Output:

Index E_id Designation


0 46 M.Com
1 47 B.Com
2 48 B.Com

Pandas Concatenation
Pandas is capable of combining Series, DataFrame, and Panel objects through different
kinds of set logic for the indexes and the relational algebra functionality.

The concat() function is responsible for performing concatenation operation along an


axis in the DataFrame.

Syntax:

1. pd.concat(objs,axis=0,join='outer',join_axes=None,
2. ignore_index=False)

Parameters:

o objs: It is a sequence or mapping of series or DataFrame objects.


If we pass a dict in the DataFrame, then the sorted keys will be used as
the keys<.strong> argument, and the values will be selected in that case. If any
non-objects are present, then it will be dropped unless they are all none, and in
this case, a ValueError will be raised.
o axis: It is an axis to concatenate along.
o join: Responsible for handling indexes on another axis.
o join_axes: A list of index objects. Instead of performing the inner or outer set
logic, specific indexes use for the other (n-1) axis.
o ignore_index: bool, default value False
It does not use the index values on the concatenation axis, if true. The resulting
axis will be labeled as 0, ..., n - 1.
Returns
A series is returned when we concatenate all the Series along the axis (axis=0).
In case if objs contains at least one DataFrame, it returns a DataFrame.

Example1:

1. import pandas as pd
2. a_data = pd.Series(['p', 'q'])
3. b_data = pd.Series(['r', 's'])
4. pd.concat([a_data, b_data])

Example2: In the above example, we can reset the existing index by using
the ignore_index parameter. The below code demonstrates the working
of ignore_index.

1. import pandas as pd
2. a_data = pd.Series(['p', 'q'])
3. b_data = pd.Series(['r', 's'])
4. pd.concat([a_data, b_data], ignore_index=True)

Output

Example 3: We can add a hierarchical index at the outermost level of the data
by using the keys parameter.

1. import pandas as pd
2. a_data = pd.Series(['p', 'q'])
3. b_data = pd.Series(['r', 's'])
4. pd.concat([a_data, b_data], keys=['a_data', 'b_data'])
Output

Example 4: We can label the index keys by using the names parameter. The
below code shows the working of names parameter.

1. import pandas as pd
2. a_data = pd.Series(['p', 'q'])
3. b_data = pd.Series(['r', 's'])
4. pd.concat([a_data, b_data], keys=['a_data', 'b_data'])
5. pd.concat([a_data, b_data], keys=['a_data', 'b_data'],
6. names=['Series name', 'Row ID'])

Output

Concatenation using append


The append method is defined as a useful shortcut to concatenate the Series
and DataFrame.

Example:

1. import pandas as pd
2. one = pd.DataFrame({
3. 'Name': ['Parker', 'Smith', 'Allen', 'John', 'Parker'],
4. 'subject_id':['sub1','sub2','sub4','sub6','sub5'],
5. 'Marks_scored':[98,90,87,69,78]},
6. index=[1,2,3,4,5])
7. two = pd.DataFrame({
8. 'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
9. 'subject_id':['sub2','sub4','sub3','sub6','sub5'],
10. 'Marks_scored':[89,80,79,97,88]},
11. index=[1,2,3,4,5])
12. print (one.append(two))

Output

Python Pandas Data operations


In Pandas, there are different useful data operations for DataFrame, which are as
follows :

Row and column selection

We can select any row and column of the DataFrame by passing the name of the rows
and column. When you select it from the DataFrame, it becomes one-dimensional and
considered as Series.

Filter Data

C++ vs Java

We can filter the data by providing some of the boolean expression in DataFrame.

Note: If we want to pass the boolean results into a DataFrame, then it shows all the
results.

Null values

A Null value can occur when no data is being provided to the items. The various
columns may contain no values which are usually represented as NaN. In Pandas,
several useful functions are available for detecting, removing, and replacing the null
values in Data Frame. These functions are as follows:

isnull(): The main task of isnull() is to return the true value if any row has null values.

notnull(): It is opposite of isnull() function and it returns true values for not null value.
dropna(): This method analyzes and drops the rows/columns of null values.

fillna(): It allows the user to replace the NaN values with some other values.

replace(): It is a very rich function that replaces a string, regex, series, dictionary, etc.

interpolate(): It is a very powerful function that fills null values in the DataFrame or
series.

String operation

A set of a string function is available in Pandas to operate on string data and ignore
the missing/NaN values. There are different string operation that can be performed
using .str. option. These functions are as follows:

lower(): It converts any strings of the series or index into lowercase letters.

upper(): It converts any string of the series or index into uppercase letters.

strip(): This function helps to strip the whitespaces including a new line from each
string in the Series/index.

split(' '): It is a function that splits the string with the given pattern.

cat(sep=' '): It concatenates series/index elements with a given separator.

contains(pattern): It returns True if a substring is present in the element, else False.

replace(a,b): It replaces the value a with the value b.

repeat(value): It repeats each element with a specified number of times.

count(pattern): It returns the count of the appearance of a pattern in each element.

startswith(pattern): It returns True if all the elements in the series starts with a
pattern.

endswith(pattern): It returns True if all the elements in the series ends with a pattern.

find(pattern): It is used to return the first occurrence of the pattern.

findall(pattern): It returns a list of all the occurrence of the pattern.

swapcase: It is used to swap the case lower/upper.


islower(): It returns True if all the characters in the string of the Series/Index are in
lowercase. Otherwise, it returns False.

isupper(): It returns True if all the characters in the string of the Series/Index are in
uppercase. Otherwise, it returns False.

isnumeric(): It returns True if all the characters in the string of the Series/Index are
numeric. Otherwise, it returns False.

Count Values

This operation is used to count the total number of occurrences using 'value_counts()'
option.

Plots

Pandas plots the graph with the matplotlib library. The .plot() method allows you to
plot the graph of your data.

.plot() function plots index against every column.

You can also pass the arguments into the plot() function to draw a specific column.

Data processing
Most of the time of data analysis and modeling is spent on data preparation and
processing i.e., loading, cleaning and rearranging the data, etc. Further, because of
Python libraries, Pandas give us high performance, flexible, and high-level environment
for processing the data. Various functionalities are available for pandas to process the
data effectively.

Hierarchical indexing

For enhancing the capabilities of Data Processing, we have to use some indexing that
helps to sort the data based on the labels. So, Hierarchical indexing is comes into the
picture and defined as an essential feature of pandas that helps us to use the multiple
index levels.

Creating multiple index

In Hierarchical indexing, we have to create multiple indexes for the data. This example
creates a series with multiple indexes.

Example:
1. import pandas as pd
2. info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27],
3. index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'],
4. ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']])
5. data

Output:

aobj1 11
obj2 14
obj3 17
obj4 24
bobj1 19
obj2 32
obj3 34
obj4 27
dtype: int64

We have taken two level of index here i.e. (a, b) and (obj1,..., obj4) and can see the
index by using 'index' command.

1. info.index

Output:

MultiIndex(levels=[['x', 'y'], ['obj1', 'obj2', 'obj3', 'obj4']],


labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])

Partial indexing
Partial indexing can be defined as a way to choose the particular index from a
hierarchical indexing.

Below code is extracting 'b' from the data,

1. import pandas as pd
2. info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27],
3. index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'],
4. ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']])
5. info['b']

Output:

obj1 19
obj2 32
obj3 34
obj4 27
dtype: int64

Further, the data can also be extracted based on inner level i.e. 'obj'. The below result
defines two available values for 'obj2' in the Series.

1. info[:, 'obj2']

Output:

x 14
y 32
dtype: int64

Unstack the data


Unstack means to change the row header to the column header. The row index will
change to the column index, therefore the Series will become the DataFrame. Below
are the example of unstacking the data.

Example:

1. import pandas as pd
2. info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27],
3. index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'],
4. ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']])
5. # unstack on first level i.e. x, y
6. #note that data row-labels are x and y
7. data.unstack(0)

Output:

ab
obj1 11 19
obj2 14 32
obj3 17 34
obj4 24 27
# unstack based on second level i.e. 'obj'
info.unstack(1)

Output:

obj1 obj2 obj3 obj4


a 11 14 17 24
b 19 32 34 27

'stack()' operation is used to convert the column index to row index. In above code,
we can convert 'obj' as column index into row index using 'stack' operation.
1. import pandas as pd
2. info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27],
3. index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'],
4. ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']])
5. # unstack on first level i.e. x, y
6. #note that data row-labels are x and y
7. data.unstack(0)
8. d.stack()

Output:

aobj1 11
obj2 14
obj3 17
obj4 24
bobj1 19
obj2 32
obj3 34
obj4 27
dtype: int64

Column indexing
Remember that, since, column-indexing requires two dimensional data, the column
indexing is possible only for DataFrame(not for Series). Let's create new DataFrame for
demonstrating the columns with multiple index,

1. import numpy as np
2. info = pd.DataFrame(np.arange(12).reshape(4, 3),
3. index = [['a', 'a', 'b', 'b'], ['one', 'two', 'three', 'four']],
4. columns = [['num1', 'num2', 'num3'], ['x', 'y', 'x']] ... )
5. info

Output:

num1 num2 num3


x y x
a one0 1 2
two3 4 5
b three 6 7 8
four 9 10 11
1. # display row index
2. info.index

Output:
MultiIndex(levels=[['x', 'y'], ['four', 'one', 'three', 'two']], labels=[[0,
0, 1, 1], [1, 3, 2, 0]])
1. # display column index
2. info.columns

Output:

MultiIndex(levels=[['num1', 'num2', 'num3'], ['green', 'red']], labels=[[0,


1, 2], [1, 0, 1]])

Swap and sort level


We can easily swap the index level by using 'swaplevel' command, which takes input
as two level-numbers.

1. import numpy as np
2. info = pd.DataFrame(np.arange(12).reshape(4, 3),
3. index = [['a', 'a', 'b', 'b'], ['one', 'two', 'three', 'four']],
4. columns = [['num1', 'num2', 'num3'], ['x', 'y', 'x']] ... )
5. info.swaplevel('key1', 'key2')
6. nnum1 num2 num3
7. p x y x
8. key2 key1
9. onea 0 1 2
10. twoa 3 4 5
11. three b 6 7 8
12. four b 9 10 11

We can sort the labels by using 'sort_index' command. The data will be sorted by
'key2' names i.e. key2 that is arranged alphabetically.

1. info.sort_index(level='key2')
2. nnum1 num2 num3
3. p x y x
4. key1 key2
5. bfour 9 10 11
6. aone 0 1 2
7. bthree 6 7 8
8. atwo 3 4 5

Pandas DataFrame.corr()
The main task of the DataFrame.corr() method is to find the pairwise correlation of
all the columns in the DataFrame. If any null value is present, it will automatically be
excluded.

It also ignores non-numeric data type columns from the DataFrame.

Syntax

1. DataFrame.count(axis=0, level=None, numeric_only=False)

Parameters
method:

o pearson: standard correlation coefficient.


o kendall: Kendall Tau correlation coefficient.
o spearman: Spearman rank correlation.
o callable: callable with input two 1d ndarrays that returns a float value.

min_periods: It is an optional parameter that requires a minimum number of


observations per pair of columns to return a valid result. Currently it is only available
for the Pearson and Spearman correlation.

OOPs Concepts in Java

Returns
It returns a DataFrame correlation matrix.

Example

1. >>>defhistogram_intersection(x, y):
2. ... a = np.minimum(x, y).sum().round(decimals=1)
3. ... return a
4. >>>info = pd.DataFrame([(.6, .2), (.4, .7), (.3, .5), (.5, .2)],
5. ... columns=['Pen', 'Pencil'])
6. >>>info.corr(method=histogram_intersection)

Output:

Pen Pencil
Pen 1.0 1.1
Pencil 1.1 1.0
Pandas DataFrame.dropna()
If your dataset consists of null values, we can use the dropna() function to analyze and
drop the rows/columns in the dataset.

Syntax:

1. DataFrameName.dropna(axis=0, how='any', thresh=None, subset=None, inpla


ce=False)

Parameters:

o axis : {0 or 'index', 1 or 'columns'}, default value 0


It takes int or string values for rows/columns. The input can be 0 and 1 for the integers
and index or columns for the string.
o 0, or 'index': Drop the rows which contain missing values.
o 1, or 'columns': Drop the columns which contain the missing value.
o how :
It determines if row or column is removed from DataFrame when we have at least one
NA or all NA.
It takes a string value of only two kinds ('any' or 'all').
o any: It drops the row/column if any value is null.
o all: It drops only if all values are null.
o thresh:
It takes integer value that defines the minimum amount of NA values to drop.
o subset:
It is an array that limits the dropping process to passed rows/columns through the list.
o inplace:
It returns a boolean value that makes the changes in data frame itself if it is True.

Returns
It returns the DataFrame from which NA entries has been dropped.

For Demonstration, first, we are taking a csv file that will drop any column from the
dataset.

1. import pandas as pd
2. aa = pd.read_csv("aa.csv")
3. aa.head()

Output

C++ vs Java

Name Hire Date Salary Leaves Remaining

0 John Idle 03/15/14 50000.0 10

1 Smith Gilliam 06/01/15 65000.0 8

2 Parker Chapman 05/12/14 45000.0 10

3 Jones Palin 11/01/13 70000.0 3

4 Terry Gilliam 08/12/14 48000.0 7

5 Michael Palin 05/23/13 66000.0 8

Code:

1. # importing pandas module


2. import pandas as pd
3. # making data frame from csv file
4. info = pd.read_csv("aa.csv")
5. # making a copy of old data frame
6. copy = pd.read_csv("aa.csv")
7.
8. # creating value with all null values in new data frame
9. copy["Null Column"]= None
10.
11. # checking if column is inserted properly
12. print(info.columns.values, "\n", copy.columns.values)
13.
14. # comparing values before dropping null column
15. print("\nColumn number before dropping Null column\n",
16. len(info.dtypes), len(copy.dtypes))
17.
18. # dropping column with all null values
19. copy.dropna(axis = 1, how ='all', inplace = True)
20.
21. # comparing values after dropping null column
22. print("\nColumn number after dropping Null column\n",
23. len(info.dtypes), len(info.dtypes))

Output

[' Name Hire Date Salary Leaves Remaining']


[' Name Hire Date Salary Leaves Remaining'
'Null Column']

Column number before dropping Null column


1 2

Column number after dropping Null column


1 1

Pandas DataFrame.fillna()
We can use the fillna() function to fill the null values in the dataset.

Syntax:

1. DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit


=None, downcast=None, **kwargs)

Parameters:

o value: It is a value that is used to fill the null values, alternately a Series/dict/DataFrame.
o method: A method that is used to fill the null values in the reindexed Series.
o axis: It takes int or string value for rows/columns. Axis along which we need to fill
missing values.
o inplace: If it is True, it fills values at an empty place.
o limit: It is an integer value that specifies the maximum number of consecutive
forward/backward NaN value fills.
o downcast: It takes a dict that specifies what to downcast like Float64 to int64.
Returns:
It returns an object in which the missing values are being filled.

Example1:

1. import pandas as pd
2. # Create a dataframe
3. info = pd.DataFrame(data={'x':[10,20,30,40,50,None]})
4. print(info)
5. # Fill null value to dataframe using 'inplace'
6. info.fillna(value=0, inplace=True)
7. print(info)

Output

x
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 NaN
x
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 0.0

Example2:
The below code is responsible for filling the DataFrame that consist some NaN values.

1. import pandas as pd
2. # Create a dataframe
3. info = pd.DataFrame([[np.nan,np.nan, 20, 0],
4. [1, np.nan, 4, 1],
5. [np.nan, np.nan, np.nan, 5],
6. [np.nan, 20, np.nan, 2]],
7. columns=list('ABCD'))
8. info

Output
A B C D
0 NaN NaN 20.0 0
1 1.0 NaN 4.0 1
2 NaN NaN NaN 5
3 NaN 20.0 NaN 2

Example3:
In below code, we have used the fillna function to fill in some of the NaN values only.

1. info = pd.DataFrame([[np.nan,np.nan, 20, 0],


2. [1, np.nan, 4, 1],
3. [np.nan, np.nan, np.nan, 5],
4. [np.nan, 20, np.nan, 2]],
5. columns=list('ABCD'))
6. info
7. info.fillna(0)
8. info.fillna(method='ffill')
9. values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
10. info.fillna(value=values)
11. info.fillna(value=values, limit=1)

Output

A B C D
0 0.0 1.0 20.0 0
1 1.0 NaN 4.0 1
2 NaN NaN 2.0 5
3 NaN 20.0 NaN 2

Pandas DataFrame.replace()
Pandas replace() is a very rich function that is used to replace a string, regex,
dictionary, list, and series from the DataFrame. The values of the DataFrame can be
replaced with other values dynamically. It is capable of working with the Python
regex(regular expression).

It differs from updating with .loc or .iloc, which requires you to specify a location
where you want to update with some value.

Syntax:

1. DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None,


regex=False, method='pad', axis=None)
Parameters:

o to_replace: Defines a pattern that we are trying to replace in dataframe.


o value: It is a value that is used to fill holes in the DataFrame (e.g., 0), alternately a dict
of values that specify which value to use for each column (columns not in the dict will
not be filled).
It also allow such objects of regular expressions, strings, and lists or dicts, etc.
o inplace: If it is True, then it replaces in place.

Note: It will also modify any other views on this object (e.g., a column from a
DataFrame). Returns the caller if this is True.
o limit: It defines the maximum size gap to forward or backward fill.
o regex: It checks whether to interpret to_replace and/or value as regular expressions. If
it is True, then to_replace must be a string. Otherwise, to_replace must be None
because this parameter will be interpreted as a regular expression or a list, dict, or array
of regular expressions.
o method: It is a method to use for replacement when to_replace is a list.

Returns: It returns a DataFrame object after the replacement.

Example1:

1. import pandas as pd
2. info = pd.DataFrame({'Language known': ['Python', 'Android', 'C', 'Android', 'Python', '
C++', 'C']},
3. index=['Parker', 'Smith', 'John', 'William', 'Dean', 'Christina', 'Cornelia'])
4. print(info)
5. dictionary = {"Python": 1, "Android": 2, "C": 3, "Android": 4, "C++": 5}
6. info1 = info.replace({"Language known": dictionary})
7. print("\n\n")
8. print(info1)

Output

Language known
Parker Python
Smith Android
John C
William Android
Dean Python
Christina C++
Cornelia C
Language known
Parker 1
Smith 4
John 3
William 4
Dean 1
Christina 5
Cornelia 3

Example2:
The below example replaces a value with another in a DataFrame.

1. import pandas as pd
2. info = pd.DataFrame({
3. 'name':['Parker','Smith','John'],
4. 'age':[27,34,31],
5. 'city':['US','Belgium','London']
6. })
7. info.replace([29],38)

Output

name age City


0 Parker 27 US
1 Smith 34 Belgium
2 John 38 London

Example3:
The below example replaces the values from a dict:

1. import pandas as pd
2. info = pd.DataFrame({
3. 'name':['Parker','Smith','John'],
4. 'age':[27,34,31],
5. 'city':['US','Belgium','London']
6. })
7. info.replace({
8. 34:29,
9. 'Smith':'William'
10. })
Output

name age City


0 Parker 27 US
1 William 29 Belgium
2 John 31 London

Example4:
The below example replaces the values from regex:

1. import pandas as pd
2. info = pd.DataFrame({
3. 'name':['Parker','Smith','John'],
4. 'age':[27,34,31],
5. 'city':['US','Belgium','London']
6. })
7. info.replace('Sm.+','Ela',regex=True)

Output

name age City


0 Parker 27 US
1 Ela 34 Belgium
2 John 31 London

Pandas DataFrame.iloc[]
The DataFrame.iloc[] is used when the index label of the DataFrame is other than
numeric series of 0,1,2,....,n or in the case when the user does not know the index label.

We can extract the rows by using an imaginary index position which is not visible in
the DataFrame. It is an integer- based position(from 0 to length-1 of the axis), but may
also be used with the boolean array.

The allowed inputs for .loc[] are:

o Integer value, e.g. 7.


o List or array of integers, e.g [2, 5, 6].
o Slice object with ints, e.g., 1:9.
o boolean array.
o A callable function with one argument that can be the calling Series or the DataFrame.
It returns valid outputs for indexing.
It can raise the IndexError if we request the index is out-of-bounds, except slice
indexers, which allow the out-of-bounds indexing.

Syntax:

1. pandas.DataFrame.iloc[]

Parameters:
None

Returns:
It returns the DataFrame or the Series.

Example:

1. import pandas as pd
2. a = [{'p': 2, 'q': 4, 'r': 6, 's': 8},
3. {'a': 200, 'b': 400, 'c': 600, 'd': 800},
4. {'p': 2000, 'q': 4000, 'r': 6000, 's': 8000 }]
5. info = pd.DataFrame(mydict)
6. type(info.iloc[0])
7. <class 'pandas.core.series.Series'>
8. info.iloc[0]

Output:

a1
b2
c 3
d4
Name: 0, dtype: int64

Pandas DataFrame.isin()
The main task of the DataFrame.isin() method is to select the rows having a particular
(or multiple) values in a particular column.

Syntax

1. DataFrame.isin(values)

Parameter
values : It can be DataFrame, Series, Iterable, or dict and returns a boolean value.

It returns a true value if all the labels match. If it consists of a Series, then it will be the
index.

If it consists of a dict, then the keys must be the column names and must be matched.

If it consists of a DataFrame, then both the index and column labels must be matched.

Example1:

1. import pandas as pd
2. #initializing dataframe
3. info = pd.DataFrame({'x': [1, 2], 'y': [3, 7]})
4. #check if the values of info are in the range(1,6)
5. p = info.isin(range(1,8))
6. print('DataFrame\n-----------\n',info)
7. print('\nDataFrame.isin(range(1,6))\n-----------\n',p)

Output:

DataFrame
-----------
xy
0 1 3
1 2 7

DataFrame.isin(range(1,6))
-----------
xy
0 TrueTrue
1 TrueTrue

Example2:

1. import pandas as pd
2. data = pd.DataFrame({
3. 'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'],
4. 'Name': ['Parker', 'Smith', 'Jones', 'Terry', 'Palin'],
5. 'Occupation': ['Tester', 'Developer', 'Statistician',
6. 'Tester', 'Developer'],
7. 'Date Of Join': ['2019-01-17', '2019-01-26', '2019-01-29', '2019-02-02',
8. '2019-02-11'],
9. 'Age': [29, 22, 25, 38, 27]})
10.
11. print("\nUseisin operator\n")
12. print(data.loc[data['Occupation'].isin(['Tester','Developer'])])
13. print("\nMultiple Conditions\n")
14. print(data.loc[(data['Occupation'] == 'Tester') |
15. (data['Name'] == 'John') &
16. (data['Age'] < 27)])

Output:

Use isin operator

EmpCodeNameOccupation Date Of Join Age


0 Emp001 Parker Tester 2019-01-17 29
1 Emp002 Smith Developer 2019-01-26 22
3 Emp004 Terry Tester 2019-02-02 38
4 Emp005 Palin Developer 2019-02-11 27

Multiple Conditions

EmpCode Name Occupation Date Of Join Age


0 Emp001 Parker Tester 2019-01-17 29
3 Emp004 Terry Tester 2019-02-02 38

Pandas DataFrame.loc[]
The DataFrame.loc[] is used to retrieve the group of rows and columns by labels or a
boolean array in the DataFrame. It takes only index labels, and if it exists in the caller
DataFrame, it returns the rows, columns, or DataFrame.

The DataFrame.loc[] is a label based but may use with the boolean array.

The allowed inputs for .loc[] are:

o Single label, e.g.,7 or a. Here, 7 is interpreted as the label of the index.


o List or array of labels, e.g. ['x', 'y', 'z'].
o Slice object with labels, e.g. 'x':'f'.
o A boolean array of the same length. e.g. [True, True, False].
o callable function with one argument.

Syntax

1. pandas.DataFrame.loc[]

Parameters
None

Returns
It returns Scalar, Series or DataFrame.

Example
# importing pandas as pd

1. import pandas as pd
2. # Creating the DataFrame
3. info = pd.DataFrame({'Age':[32, 41, 44, 38, 33],
4. 'Name':['Phill', 'William', 'Terry', 'Smith', 'Parker']})
5. # Create the index
6. index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']
7.
8. # Set the index
9. info.index = index_
10.
11. # return the value
12. final = info.loc['Row_2', 'Name']
13.
14. # Print the result
15. print(final)

Output:

William

Example2:

1. # importing pandas as pd
2. import pandas as pd
3. # Creating the DataFrame
4. info = pd.DataFrame({"P":[28, 17, 14, 42, None],
5. "Q":[15, 23, None, 15, 12],
6. "R":[11, 23, 16, 32, 42],
7. "S":[41, None, 34, 25, 18]})
8. # Create the index
9. index_ = ['A', 'B', 'C', 'D', 'E']
10. # Set the index
11. info.index = index_
12. # Print the DataFrame
13. print(info)

Output:

P Q R S
A 28.0 15.0 11 41.0
B 17.0 23.0 23 NaN
C 14.0 NaN 16 34.0
D 42.0 15.0 32 25.0
E NaN 12.0 42 18.0

Now, we have to use DataFrame.loc attribute to return the values present in the
DataFrame.

1. # return the values


2. result = info.loc[:, ['P', 'S']]
3. # Print the result
4. print(result)

Output:

P S
A 28.0 41.0
B 17.0 NaN
C14.0 34.0
D 42.0 25.0
ENaN 18.0

Pandas loc vs. iloc


The Pandas offers .loc[] and .iloc[] methods for data slicing. Data Slicing generally
refers to inspect your data sets. These two methods belong to the index selection
method that is used to set an identifier for each row of the data set. The indexing can
take specific labels, and these labels can either be an integer or any other value
specified by the user.

The .loc[] method is used to retrieve the group of rows and columns by labels or a
boolean array present in the DataFrame. It takes only index labels, and if it exists in the
caller DataFrame, it returns the rows, columns, or DataFrame. It is a label-based
method but may be used with the boolean array.
Whereas, the .iloc[] method is used when the index label of the DataFrame is other
than numeric series of 0,1,2,....,n, or in the case when the user does not know the index
label.

There are some differences between the above methods, which are given below:

1. The .loc[] method is a label based method that means it takes names or labels of the
index when taking the slices, whereas .iloc[] method is based on the index's position.
It behaves like a regular slicing where we just have to indicate the positional index
number and simply get the appropriate slice.
2. The .loc[] method includes the last element of the table whereas .iloc[] method does
not include the last element.
3. The .loc[] method is a name-based indexing, whereas the .iloc[] method
is positional based indexing.
4. The arguments of .iloc[] can be:
o list of rows and columns
o range of rows and columns
o single row and column

Whereas, the arguments of .loc[] can be:

o row label
o list of row label
5. The .loc[] method indexer can perform the boolean selection by passing the boolean
series, but in the case of .iloc[]method, we cannot pass a boolean series.

Pandas Cheat Sheet


Pandas can be used as the most important Python package for Data Science. It helps
to provide a lot of functions that deal with the data in easier way. It's fast, flexible, and
expressive data structures are designed to make real-world data analysis.

Pandas Cheat Sheet is a quick guide through the basics of Pandas that you will need
to get started on wrangling your data with Python. If you want to begin your data
science journey with Pandas, you can use it as a handy reference to deal with the data
easily.

This cheat sheet will guide through the basics of the Pandas library from the data
structure to I/O, selection, sorting and ranking, etc.
Key and Imports
We use following shorthand in the cheat sheet:

o df: Refers to any Pandas Dataframe object.


o s: Refers to any Pandas Series object. You can use the following imports to get started:

Importing Data

o pd.read_csv(filename) : It read the data from CSV file.


o pd.read_table(filename) : It is used to read the data from delimited text file.
o pd.read_excel(filename) : It read the data from an Excel file.
o pd.read_sql(query,connection _object) : It read the data from a SQL table/database.
o pd.read_json(json _string) : It read the data from a JSON formatted string, URL or file.
o pd.read_html(url) : It parses an html URL, string or the file and extract the tables to a
list of dataframes.
o pd.read_clipboard() : It takes the contents of clipboard and passes it to the
read_table() function.
o pd.DataFrame(dict) : From the dict, keys for the columns names, values for the data
as lists.

Exporting data

o df.to_csv(filename): It writes to a CSV file.


o df.to_excel(filename): It writes to an Excel file.
o df.to_sql(table_name, connection_object): It writes to a SQL table.
o df.to_json(filename) : It write to a file in JSON format.

Create Test objects


It is useful for testing the code segments.

o pd.DataFrame(np.random.rand(7,18)): Refers to 18 columns and 7 rows of random


floats.
o pd.Series(my_list): It creates a Series from an iterable my_list.
o df.index= pd.date_range('1940/1/20', periods=df.shape[0]): It adds the date
index.
Viewing/Inspecting Data

o df.head(n): It returns first n rows of the DataFrame.


o df.tail(n): It returns last n rows of the DataFrame.
o df.shape: It returns number of rows and columns.
o df.info(): It returns index, Datatype, and memory information.
o s.value_counts(dropna=False): It views unique values and counts.
o df.apply(pd.Series.value_counts): It refers to the unique values and counts for all the
columns.

Selection

o df[col1]: It returns column with the label col as Series.


o df[[col1, col2]]: It returns columns as a new DataFrame.
o s.iloc[0]: It select by the position.
o s.loc['index_one']: It select by the index.
o df.iloc[0,:]: It returns first row.
o df.iloc[0,0]: It returns the first element of first column.

Data cleaning

o df.columns = ['a','b','c']: It rename the columns.


o pd.isnull(): It checks for the null values and returns the Boolean array.
o pd.notnull(): It is opposite of pd.isnull().
o df.dropna(): It drops all the rows that contain the null values.
o df.dropna(axis= 1): It drops all the columns that contain null values.
o df.dropna(axis=1,thresh=n): It drops all the rows that have less than n non null
values.
o df.fillna(x): It replaces all null values with x.
o s.fillna(s.mean()): It replaces all the null values with the mean(the mean can be
replaced with almost any function from the statistics module).
o s.astype(float): It converts the datatype of series to float.
o s.replace(1, 'one'): It replaces all the values equal to 1 with 'one'.
o s.replace([1,3],[ 'one', 'three']):It replaces all 1 with 'one' and 3 with 'three'.
o df.rename(columns=lambda x: x+1):It rename mass of the columns.
o df.rename(columns={'old_name': 'new_ name'}): It consist selective renaming.
o df.set_index('column_one'): Used for changing the index.
o df.rename(index=lambda x: x+1): It rename mass of the index.

Filter, Sort, and Groupby

o df[df[col] > 0.5]: Returns the rows where column col is greater than 0.5
o df[(df[col] > 0.5) & (df[col] < 0.7)] : Returns the rows where 0.7 > col > 0.5
o df.sort_values(col1) :It sorts the values by col1 in ascending order.
o df.sort_values(col2,ascending=False) :It sorts the values by col2 in descending order.
o df.sort_values([col1,col2],ascending=[True,False]) :It sort the values by col1 in
ascending order and col2 in descending order.
o df.groupby(col1): Returns a groupby object for the values from one column.
o df.groupby([col1,col2]) :Returns a groupby object for values from multiple columns.
o df.groupby(col1)[col2]) :Returns mean of the values in col2, grouped by the values
in col1.
o df.pivot_table(index=col1,values=[col2,col3],aggfunc=mean) :It creates the pivot
table that groups by col1 and calculate mean of col2 and col3.
o df.groupby(col1).agg(np.mean) :It calculates the average across all the columns for
every unique col1 group.
o df.apply(np.mean) :Its task is to apply the function np.mean() across each column.
o nf.apply(np.max,axis=1) :Its task is to apply the function np.max() across each row.

Join/Combine

o df1.append(df2): Its task is to add the rows in df1 to the end of df2(columns should
be identical).
o pd.concat([df1, df2], axis=1): Its task is to add the columns in df1 to the end of
df2(rows should be identical).
o df1.join(df2,on=col1,how='inner'): SQL-style join the columns in df1 with the
columns on df2 where the rows for col have identical values, 'how' can be of 'left',
'right', 'outer', 'inner'.

Statistics
The statistics functions can be applied to a Series, which are as follows:
o df.describe(): It returns the summary statistics for the numerical columns.
o df.mean() : It returns the mean of all the columns.
o df.corr(): It returns the correlation between the columns in the dataframe.
o df.count(): It returns the count of all the non-null values in each dataframe column.
o df.max(): It returns the highest value from each of the columns.
o df.min(): It returns the lowest value from each of the columns.
o df.median(): It returns the median from each of the columns.
o df.std(): It returns the standard deviation from each of the columns.

Pandas Index
Pandas Index is defined as a vital tool that selects particular rows and columns of data
from a DataFrame. Its task is to organize the data and to provide fast accessing of data.
It can also be called a Subset Selection.

The values are in bold font in the index, and the individual value of the index is called
a label.

If we want to compare the data accessing time with and without indexing, we can
use %%timeit for comparing the time required for various access-operations.

We can also define an index like an address through which any data can be accessed
across the Series or DataFrame. A DataFrame is a combination of three different
components, the index, columns, and the data.

Java Try Catch

Axis and axes


An axis is defined as a common terminology that refers to rows and columns, whereas
axes are collection of these rows and columns.

Creating index
First, we have to take a csv file that consist some data used for indexing.

1. # importing pandas package


2. import pandas as pd
3. data = pd.read_csv("aa.csv")
4. data
Output:

Name Hire Date Salary Leaves Remaining


0 John Idle 03/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 8
2 Parker Chapman 05/12/14 45000.0 10
3 Jones Palin 11/01/13 70000.0 3
4 Terry Gilliam 08/12/14 48000.0 7
5 Michael Palin 05/23/13 66000.0 8

Example1

1. # importing pandas package


2. import pandas as pd
3. # making data frame from csv file
4. info = pd.read_csv("aa.csv", index_col ="Name")
5. # retrieving multiple columns by indexing operator
6. a = info[["Hire Date", "Salary"]]
7. print(a)

Output:

Name Hire Date Salary


0 John Idle 03/15/14 50000.0
1 Smith Gilliam 06/01/15 65000.0
2 Parker Chapman 05/12/14 45000.0
3 Jones Palin 11/01/13 70000.0
4 Terry Gilliam 08/12/14 48000.0
5 Michael Palin 05/23/13 66000.0

Example2:

1. # importing pandas package


2. importpandas as pd
3.
4. # making data frame from csv file
5. info =pd.read_csv("aa.csv", index_col ="Name")
6.
7. # retrieving columns by indexing operator
8. a =info["Salary"]
9. print(a)

Output:

Name Salary
0 John Idle 50000.0
1 Smith Gilliam 65000.0
2 Parker Chapman 45000.0
3 Jones Palin 70000.0
4 Terry Gilliam 48000.0
5 Michael Palin 66000.0

Set index
The 'set_index' is used to set the DataFrame index using existing columns. An index
can replace the existing index and can also expand the existing index.

It set a list, Series or DataFrame as the index of the DataFrame.

1. info = pd.DataFrame({'Name': ['Parker', 'Terry', 'Smith', 'William'],


2. 'Year': [2011, 2009, 2014, 2010],
3. 'Leaves': [10, 15, 9, 4]})
4. info
5. info.set_index('Name')
6. info.set_index(['year', 'Name'])
7. info.set_index([pd.Index([1, 2, 3, 4]), 'year'])
8. a = pd.Series([1, 2, 3, 4])
9. info.set_index([a, a**2])

Output:

Name Year Leaves


1 1 Parker 2011 10
2 4 Terry 2009 15
3 9 Smith 2014 9
4 16 William 2010 4

Multiple Index
We can also have multiple indexes in the data.

Example1:

1. import pandas as pd
2. import numpy as np
3. pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]],
4. codes=[[0, -1, 1, 2, 3, 4]])

Output:

MultiIndex(levels=[[nan, None, NaT, 128, 2]],


codes=[[0, -1, 1, 2, 3, 4]])
Reset index
We can also reset the index using the 'reset_index' command. Let's look at the 'cm'
DataFrame again.

Example:

1. info = pd.DataFrame([('William', 'C'),


2. ('Smith', 'Java'),
3. ('Parker', 'Python'),
4. ('Phill', np.nan)],
5. index=[1, 2, 3, 4],
6. columns=('name', 'Language'))
7. info
8. info.reset_index()

Output:

index name Language


0 1 William C
1 2 Smith Java
2 3 Parker Python
3 4 Phill NaN

Multiple Index
Multiple indexing is defined as a very essential indexing because it deals with the data
analysis and manipulation, especially for working with higher dimensional data. It also
enables to store and manipulate data with the arbitrary number of dimensions in lower
dimensional data structures like Series and DataFrame.

It is the hierarchical analogue of the standard index object which is used to store the
axis labels in pandas objects. It can also be defined as an array of tuples where each
tuple is unique. It can be created from a list of arrays, an array of tuples, and a crossed
set of iterables.

Example:

1. arrays = [['it', 'it', 'of', 'of', 'for', 'for', 'then', 'then'],


2. ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
3. tuples = list(zip(*arrays))
4. tuples

Output:
Difference between JDK, JRE, and JVM
[('it', 'one'),
('it', 'two'),
('of', 'one'),
('of', 'two'),
('for', 'one'),
('for', 'two'),
('then', 'one'),
('then', 'two')]

Example2:

1. arrays = [['it', 'it', 'of', 'of', 'for', 'for', 'then', 'then'],


2. ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
3. tuples = list(zip(*arrays))
4. index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

Output:

MultiIndex([('bar', 'one'),
[('it', 'one'),
('it', 'two'),
('of', 'one'),
('of', 'two'),
('for', 'one'),
('for', 'two'),
('then', 'one'),
('then', 'two')]
names=['first', 'second'])

Example3:

1. import pandas as pd
2. import numpy as np
3. pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]],
4. codes=[[0, -1, 1, 2, 3, 4]])

Output:

MultiIndex(levels=[[nan, None, NaT, 128, 2]],


codes=[[0, -1, 1, 2, 3, 4]])

Reindex
The main task of the Pandas reindex is to conform DataFrame to a new index with
optional filling logic and to place NA/NaN in that location where the values are not
present in the previous index. It returns a new object unless the new index is produced
as an equivalent to the current one, and the value of copy becomes False.
Reindexing is used to change the index of the rows and columns of the DataFrame.
We can reindex the single or multiple rows by using the reindex() method. Default
values in the new index are assigned NaN if it is not present in the DataFrame.

Syntax:

1. DataFrame.reindex(labels=None, index=None, columns=None, axis=None, me


thod=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=N
one)

Parameters:
labels: It is an optional parameter that refers to the new labels or the index to conform
to the axis that is specified by the 'axis'.

index, columns : It is also an optional parameter that refers to the new labels or the
index. It generally prefers an index object for avoiding the duplicate data.

axis : It is also an optional parameter that targets the axis and can be either the axis
name or the numbers.

method: It is also an optional parameter that is to be used for filling the holes in the
reindexed DataFrame. It can only be applied to the DataFrame or Series with a
monotonically increasing/decreasing order.

None: It is a default value that does not fill the gaps.

pad / ffill: It is used to propagate the last valid observation forward to the next valid
observation.

backfill / bfill: To fill the gap, It uses the next valid observation.

nearest: To fill the gap, it uses the next valid observation.

copy: Its default value is True and returns a new object as a boolean value, even if the
passed indexes are the same.

level : It is used to broadcast across the level, and match index values on the passed
MultiIndex level.

fill_value : Its default value is np.NaN and used to fill existing missing (NaN) values. It
needs any new element for successful DataFrame alignment, with this value before
computation.
limit : It defines the maximum number of consecutive elements that are to be forward
or backward fill.

tolerance : It is also an optional parameter that determines the maximum distance


between original and new labels for inexact matches. At the matching locations, the
values of the index should most satisfy the equation abs(index[indexer] ? target) <=
tolerance.

Returns :
It returns reindexed DataFrame.

Example 1:
The below example shows the working of reindex() function to reindex the dataframe.
In the new index,default values are assigned NaN in the new index that does not have
corresponding records in the DataFrame.

Note: We can use fill_value for filling the missing values.


1. import pandas as pd
2.
3. # Create dataframe
4. info = pd.DataFrame({"P":[4, 7, 1, 8, 9],
5. "Q":[6, 8, 10, 15, 11],
6. "R":[17, 13, 12, 16, 14],
7. "S":[15, 19, 7, 21, 9]},
8. index =["Parker", "William", "Smith", "Terry", "Phill"])
9.
10. # Print dataframe
11. info

Output:

A B D E
Parker NaN NaN NaN NaN
William NaN NaN NaN NaN
Smith NaN NaN NaN NaN
Terry NaN NaN NaN NaN
Phill NaN NaN NaN NaN

Now, we can use the dataframe.reindex() function to reindex the dataframe.

1. # reindexing with new index values


2. info.reindex(["A", "B", "C", "D", "E"])
Output:

P Q R S
A NaN NaN NaN NaN
B NaN NaN NaN NaN
C NaN NaN NaN NaN
D NaN NaN NaN NaN
E NaN NaN NaN NaN

Notice that the new indexes are populated with NaN values. We can fill in the missing
values using the fill_value parameter.

1. # filling the missing values by 100


2. info.reindex(["A", "B", "C", "D", "E"], fill_value =100)

Output:

P Q R S
A 100 100 100 100
B 100 100 100 100
C 100 100 100 100
D 100 100 100 100
E 100 100 100 100

Example 2:

This example shows the working of reindex() function to reindex the column axis.

1. # importing pandas as pd
2. importpandas as pd
3.
4. # Creating the first dataframe
5. info1 =pd.DataFrame({"A":[1, 5, 3, 4, 2],
6. "B":[3, 2, 4, 3, 4],
7. "C":[2, 2, 7, 3, 4],
8. "D":[4, 3, 6, 12, 7]})
9. # reindexing the column axis with
10. # old and new index values
11. info.reindex(columns =["A", "B", "D", "E"])

Output:

A B D E
Parker NaN NaN NaN NaN
William NaN NaN NaN NaN
Smith NaN NaN NaN NaN
Terry NaN NaN NaN NaN
Phill NaN NaN NaN NaN

Notice that NaN values are present in the new columns after reindexing, we can use
the argument fill_value to the function for removing the NaN values.

1. # reindex the columns


2. # fill the missing values by 25
3. info.reindex(columns =["A", "B", "D", "E"], fill_value =37)

Output:

A B D E
Parker 37 37 37 37
William 37 37 37 37
Smith 37 37 37 37
Terry 37 37 37 37
Phill 37 37 37 37

Reset Index
The Reset index of the DataFrame is used to reset the index by using the 'reset_index'
command. If the DataFrame has a MultiIndex, this method can remove one or more
levels.

Syntax:

1. DataFrame.reset_index(self, level=None, drop=False, inplace=False, col_level=


0, col_fill='')

Parameters:

level : Refers to int, str, tuple, or list, default value None

It is used to remove the given levels from the index and also removes all levels by
default.

drop : Refers to Boolean value, default value False

It resets the index to the default integer index.

inplace : Refers to Boolean value, default value False

It is used to modify the DataFrame in place and does not require to create a new object.

col_level : Refers to int or str, default value 0

It determines level the labels are inserted if the column have multiple labels
col_fill : Refers to an object, default value ''

It determines how the other levels are named if the columns have multiple level.

Example1:

1. info = pd.DataFrame([('William', 'C'),


2. ('Smith', 'Java'),
3. ('Parker', 'Python'),
4. ('Phill', np.nan)],
5. index=[1, 2, 3, 4],
6. columns=('name', 'Language'))
7. info
8. info.reset_index()

Output:

index name Language


0 1 William C
1 2 Smith Java
2 3 Parker Python
3 4 Phill NaN

Set Index
Pandas set index() is used to set a List, Series or DataFrame as index of a Data Frame.
We can set the index column while making a data frame. But sometimes a data frame
is made from two or more data frames and then index can be changed using this
method.

Syntax:

1. DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verif


y_integrity=False)

Parameters:

o keys: Refers to label or array-like or list of labels/arrays

It can be either a single column key, a single array of the same length as the calling
DataFrame, or also a list that contains an arbitrary combination of column keys and
arrays.
o drop: Returns the boolean value, default value is True. Used to delete the columns that
are to be used as the new index.
o append: Returns the boolean value, default value is False.

It checks whether append the columns to an existing index.

o inplace: Returns the boolean value, default value False.

It is used to modify the DataFrame in place. We don't need to create a new object.

o verify_integrity: Returns the boolean value, default value False.

It checks the new index for duplicate values. Otherwise, it will defer the check until
necessary. It also set it to False that will improve the performance of this method.

Returns:

It will change the row labels as the output.

Example1:
This example shows how to set the index:

1. import pandas as pd
2. info = pd.DataFrame({'Name': ['William', 'Phill', 'Parker', 'Smith'],
3. 'Age': [32, 38, 41, 36],
4. 'id': [105, 132, 134, 127]})
5. info

Output:

Name Age id
0 William 32 105
1 Phill 38 132
2 Parker 41 134
3 Smith 36 127

Now, we have to set the index to create the 'month' column:

1. info.set_index('month')

Output:

Age id
Name
William 32 105
Phill 38 132
Parker 41 134
Smith 36 127

Example2:
Create a MultiIndex using columns 'Age' and 'Name':

1. info.set_index(['Age', 'Name'])

Output:

Name id
Age
32 William 105
38 Phill 132
41 Parker 134
36 Smith 127

Example3:
It creates a MultiIndex using an Index and a column:

1. info.set_index([pd.Index([1, 2, 3, 4]), 'Name'])

Output:

Age id
Name
1 William 32 105
2 Phill 38 132
3 Parker 41 134
4 Smith 36 127

Example4:
Create a MultiIndex using two Series:

1. a = pd.Series([1, 2, 3, 4])
2. info.set_index([a, a**2])

Output:

Name Age id
1 1 William 32 105
2 4 Phill 38 132
3 9 Parker 41 134
4 16 Smith 36 127
Pandas NumPy
Numerical Python (Numpy) is defined as a Python package used for performing the
various numerical computations and processing of the multidimensional and single-
dimensional array elements. The calculations using Numpy arrays are faster than the
normal Python array.

This package is created by the Travis Oliphant in 2005 by adding the functionalities
of the ancestor module Numeric into another module Numarray. It is also capable of
handling a vast amount of data and convenient with Matrix multiplication and data
reshaping.

NumPy is mostly written in C language, and it is an extension module of Python.

Pandas are built over numpy array; therefore, numpy helps us to use pandas more
effectively.

How to find Nth Highest Salary in SQL

Creating Arrays

The main task of arrays is to store multiple values in a single variable. It defines the
multidimensional arrays that can be easily handled in numpy as shown in the below
examples:

Example

1. # import the "array" for demonstrating array operations


2. import array
3. # initializing an array with array values and signed integers
4. arr = array.array('l', [2, 4, 6, 8, 10, 12])
5. # print the original array
6. print ("New created array: ",end="")
7. for l in range (0,5):
8. print (arr[l], end=" ")
9. print ("\r")

Output:

New created array: 2 4 6 8 10

Boolean indexing
Boolean indexing is defined as a vital tool of numpy, which is frequently used in
pandas. Its main task is to use the actual values of the data in the DataFrame. We can
filter the data in the boolean indexing in different ways that are as follows:

o Access the DataFrame with a boolean index.


o Apply the boolean mask to the DataFrame.
o Masking the data based on column value.
o Masking the data based on the index value.

Example1

This example shows how to access the DataFrame with a boolean index:

1. # importing pandas as pd
2. import pandas as pd
3. # dictionary of lists
4. dict = {'name':["Smith", "William", "Phill", "Parker"],
5. 'age': ["28", "39", "34", "36"]}
6. info = pd.DataFrame(dict, index = [True, True, False, True])
7. print(info)

Output:

name age
True Smith 28
True William 39
False Phill 34
True Parker 36

Example2

This example shows how to access the DataFrame with a boolean index by using .loc[]

1. # importing pandas as pd
2. import pandas as pd
3. # dictionary of lists
4. dict = {'name':["Smith", "William", "Phill", "Parker"],
5. 'age': ["28", "39", "34", "36"]}
6. info = pd.DataFrame(dict, index = [True, True, False, True])
7. # accessing a dataframe using .loc[] function
8. print(info.loc[True])
Output:

name age
True Smith 28
True William 39
True Parker 36

Reshaping arrays
Reshaping arrays are used to reshape the array without changing its data.

Syntax

1. numpy.reshape(a, newshape, order='C')

Parameters

o a: Defines an array that is to be reshaped.


o newshape: Defines the new shape that should be compatible with the original shape.
For the integer value, the result will be a 1-D array of that length. The one shape
dimension can be -1.
o order: It is an optional parameter that read the elements by using the index order, and
place the elements into the reshaped array with the help of index order.

Returns:

It returns the reshaped array.

Example

1. import numpy as np
2. arr = np.arange(16)
3. print("The Original array is: \n", arr)
4. # shape array with 2 rows and 8 columns
5. arr = np.arange(16).reshape(2, 8)
6. print("\nreshapedarray: \n", arr)
7. # shape array with 2 rows and 8 columns
8. arr = np.arange(16).reshape(8 ,2)
9. print("\nreshaped array: \n", arr)

Output:

The Original array is:


[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
reshaped array:
[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]]

reshaped array:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]]

Concatenating the data


NumPy's concatenate data is used to concatenate two arrays either row-wise or
column-wise. It can take two or more arrays of the same shape and it concatenates
row-wise as a default type i.e. axis=0.

Example1:

1. # import numpy
2. import numpy as np
3. arr1 = np.arange(9)
4. arr1
5. arr2d_1 = array.reshape((3,3))
6. arr2d_1
7. arr2d_1 = np.arange(10,19).reshape(3,3)
8. arr2d_1
9.
10.
11. # concatenate 2 numpy arrays: row-wise
12. np.concatenate((arr2d_1, arr2d_2))

Output:

array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])

Example2:
1. import pandas as pd
2.
3. one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'],'id':[108,119,127]},index
=['A','B','C'])
4. two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'],
5. 'id':[102,125,112]},
6. index=['A','B','C'])
7. print(pd.concat([one,two]))

Output:

Name id
A Parker 108
B Phill 119
C Smith 127
A Terry 102
B Jones 125
C John 112

Example3:

1. import pandas as pd
2. one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'],'id':[108,119,127]},index=['A','B','
C'])
3. two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'],
4. 'id':[102,125,112]},
5. index=['A','B','C'])
6. print(pd.concat([one,two],keys=['x','y']))

Output:

Name id
x A Parker 108
B Phill119
C Smith 127
y A Terry 102
B Jones 125
C John 112

Pandas vs. NumPy


What is Pandas?
Pandas is defined as an open-source library that provides high-performance data
manipulation in Python. It is built on top of the NumPy package, which
means Numpy is required for operating the Pandas. The name of Pandas is derived
from the word Panel Data, which means an Econometrics from Multidimensional
data. It is used for data analysis in Python and developed by Wes McKinney in 2008.

Before Pandas, Python was capable for data preparation, but it only provided limited
support for data analysis. So, Pandas came into the picture and enhanced the
capabilities of data analysis. It can perform five significant steps required for
processing and analysis of data irrespective of the origin of the data, i.e., load,
manipulate, prepare, model, and analyze.

What is NumPy?
NumPy is mostly written in C language, and it is an extension module of Python. It is
defined as a Python package used for performing the various numerical computations
and processing of the multidimensional and single-dimensional array elements. The
calculations using Numpy arrays are faster than the normal Python array.

The NumPy package is created by the Travis Oliphant in 2005 by adding the
functionalities of the ancestor module Numeric into another module Numarray. It is
also capable of handling a vast amount of data and convenient with Matrix
multiplication and data reshaping.

12.9M

247

Difference between JDK, JRE, and JVM

Both the Pandas and NumPy can be seen as an essential library for any scientific
computation, including machine learning due to their intuitive syntax and high-
performance matrix computation capabilities. These two libraries are also best suited
for data science applications.

Difference between Pandas and NumPy:


There are some differences between Pandas and NumPy that is listed below:

o The Pandas module mainly works with the tabular data, whereas the NumPy module
works with the numerical data.
o The Pandas provides some sets of powerful tools like DataFrame and Series that
mainly used for analyzing the data, whereas in NumPy module offers a powerful object
called Array.
o Instacart, SendGrid, and Sighten are some of the famous companies that work on
the Pandas module, whereas NumPy is used by SweepSouth.
o The Pandas covered the broader application because it is mentioned in 73 company
stacks and 46 developer stacks, whereas in NumPy, 62 company stacks
and 32 developer stacks are being mentioned.
o The performance of NumPy is better than the NumPy for 50K rows or less.
o The performance of Pandas is better than the NumPy for 500K rows or more. Between
50K to 500K rows, performance depends on the kind of operation.
o NumPy library provides objects for multi-dimensional arrays, whereas Pandas is
capable of offering an in-memory 2d table object called DataFrame.
o NumPy consumes less memory as compared to Pandas.
o Indexing of the Series objects is quite slow as compared to NumPy arrays.

The below table shows the comparison chart between the Pandas and NumPy:

Basis for Pandas NumPy


Comparison

Works with Pandas module works with the tabular NumPy module works with numerical
data. data.

Powerful Tools Pandas has powerful tools like Series, NumPy has a powerful tool
DataFrame etc. like Arrays.

Organizational Pandas is used in popular organizations NumPy is used in the popular


usage like Instacart, SendGrid, and Sighten. organization like SweepSouth.

Performance Pandas has a better performance for 500K NumPy has a better performance
rows or more. for 50K rows or less.

Memory Pandas consume large memory as NumPy consumes less memory as


Utilization compared to NumPy. compared to Pandas.

Industrial Pandas is mentioned in 73 company stacks NumPy is mentioned in 62 company


Coverage and 46 developer stacks. stacks and 32 developer stacks.

Objects Pandas provides 2d table object NumPy provides a multi-


called DataFrame. dimensional array.
Pandas Time Series
The Time series data is defined as an important source for information that provides a
strategy that is used in various businesses. From a conventional finance industry to the
education industry, it consist of a lot of details about the time.

Time series forecasting is the machine learning modeling that deals with the Time
Series data for predicting future values through Time Series modeling.

The Pandas have extensive capabilities and features that work with the time series data
for all the domains. By using the NumPy datetime64 and timedelta64 dtypes. The
Pandas has consolidated different features from other python libraries
like scikits.timeseries as well as created a tremendous amount of new functionality
for manipulating the time series data.

For example, pandas support to parse the time-series information from various sources
and formats.

Importing Packages and Data


Before starting, you have to import some packages that will make use of numpy,
pandas, matplotlib, and seaborn.

You can attach the images to be plotted in the Jupyter Notebook, by


adding %matplotlib inline to the code and can also switch to Seaborn defaults by
using sns.set():

1. # import packages
2. import numpy as np
3. import pandas as pd
4. import matplotlib.pyplot as plt
5. import seaborn as sns
6. %matplotlib inline
7. sns.set()

Date and time


The Pandas provide the number of functionalities for dates, times, deltas, and
timespans. It is mainly used for data science applications.

Native dates and times:


We have two native date and time available that reside in datetime module. We can
also perform lots of useful functionalities on date and time by using
the dateutil function. You can also parse the dates from a variety of string formats:

Example1:

1. import pandas as pd
2. # Create the dates with frequency
3. info = pd.date_range('5/4/2013', periods = 8, freq ='S')
4. info

Output:

DatetimeIndex(['2013-05-04 00:00:00', '2013-05-04 00:00:01',


'2013-05-04 00:00:02', '2013-05-04 00:00:03',
'2013-05-04 00:00:04', '2013-05-04 00:00:05',
'2013-05-04 00:00:06', '2013-05-04 00:00:07'],
dtype='datetime64[ns]', freq='S')

Example1:

1. import pandas as pd
2. # Create the Timestamp
3. p = pd.Timestamp('2018-12-12 06:25:18')
4. # Create the DateOffset
5. do = pd.tseries.offsets.DateOffset(n = 2)
6. # Print the Timestamp
7. print(p)
8. # Print the DateOffset
9. print(do)

Output:

2018-12-12 06:25:18
<2 * DateOffsets>

Pandas Datetime
The Pandas can provide the features to work with time-series data for all domains. It
also consolidates a large number of features from other Python libraries like
scikits.timeseries by using the NumPy datetime64 and timedelta64 dtypes. It provides
new functionalities for manipulating the time series data.
The time series tools are most useful for data science applications and deals with other
packages used in Python.

Example1:

1. import pandas as pd
2. # Create the dates with frequency
3. info = pd.date_range('5/4/2013', periods = 8, freq ='S')
4. info

Output:

DatetimeIndex(['2013-05-04 00:00:00', '2013-05-04 00:00:01',


'2013-05-04 00:00:02', '2013-05-04 00:00:03',
'2013-05-04 00:00:04', '2013-05-04 00:00:05',
'2013-05-04 00:00:06', '2013-05-04 00:00:07'],
dtype='datetime64[ns]', freq='S')

Example2:

1. info = pd.DataFrame({'year': [2014, 2012],


2. 'month': [5, 7],
3. 'day': [20, 17]})
4. pd.to_datetime(info)
5. 0 2014-05-20
6. 1 2012-07-17
7. dtype: datetime64[ns]

You can pass errors='ignore' if the date does not meet the timestamp. It will return the
original input without raising any exception.

If you pass errors='coerce', it will force an out-of-bounds date to NaT.

1. import pandas as pd
2. pd.to_datetime('18000706', format='%Y%m%d', errors='ignore')
3. datetime.datetime(1800, 7, 6, 0, 0)
4. pd.to_datetime('18000706', format='%Y%m%d', errors='coerce')

Output:

Timestamp('1800-07-06 00:00:00')

Example3:
1. import pandas as pd
2. dmy = pd.date_range('2017-06-04', periods=5, freq='S')
3. dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00',
'2017-06-04 00:00:01',
'2017-06-04 00:00:02',
'2017-06-04 00:00:03',
'2017-06-04 00:00:04'],
dtype='datetime64[ns]', freq='S')

Example4:

1. import pandas as pd
2. dmy = dmy.tz_localize('UTC')
3. dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00+00:00', '2017-06-04 00:00:01+00:00',


'2017-06-04 00:00:02+00:00',
'2017-06-04 00:00:03+00:00',
'2017-06-04 00:00:04+00:00'],
dtype='datetime64[ns, UTC]', freq='S')

Example5:

1. import pandas as pd
2. dmy = pd.date_range('2017-06-04', periods=5, freq='S')
3. dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00', '2017-06-04 00:00:01',


'2017-06-04 00:00:02', '2017-06-04 00:00:03',
'2017-06-04 00:00:04'],
dtype='datetime64[ns]', freq='S')

Pandas Time Offset


The time series tools are most useful for data science applications and deals with other
packages used in Python. The time offset performs various operations on time, i.e.,
adding and subtracting.
The offset specifies a set of dates that conform to the DateOffset. We can create the
DateOffsets to move the dates forward to valid dates.

If the date is not valid, we can use the rollback and rollforward methods for rolling the
date to its nearest valid date before or after the date. The pseudo-code of time offsets
are as follows:

Syntax:

1. class pandas.tseries.offsets.DateOffset(n=1, normalize=False, **kwds)

def __add__(date):

date = rollback(date). It returns nothing if the date is valid + <n number of periods>.

date = rollforward(date)

When we create a date offset for a negative number of periods, the date will be rolling
forward.

Parameters:
n: Refers to int, default value is 1.

It is the number of time periods that represents the offsets.

normalize: Refers to a boolean value, default value False.

**kwds

It is an optional parameter that adds or replaces the offset value.

The parameters used for adding to the offset are as follows:

o years
o months
o weeks
o days
o hours
o minutes
o seconds
o microseconds
o nanoseconds

The parameters used for replacing the offset value are as follows:

o year
o month
o day
o weekday
o hour
o minute
o second
o microsecond
o nanosecond

Example:

1. import pandas as pd
2. # Create the Timestamp
3. p = pd.Timestamp('2018-12-12 06:25:18')
4. # Create the DateOffset
5. do = pd.tseries.offsets.DateOffset(n = 2)
6. # Print the Timestamp
7. print(p)
8. # Print the DateOffset
9. print(do)

Output:

2018-12-12 06:25:18
<2 * DateOffsets>

Example2:

1. import pandas as pd
2. # Create the Timestamp
3. p = pd.Timestamp('2018-12-12 06:25:18')
4. # Create the DateOffset
5. do = pd.tseries.offsets.DateOffset(n = 2)
6. # Add the dateoffset to given timestamp
7. new_timestamp = p + do
8. # Print updated timestamp
9. print(new_timestamp)

Output:

Timestamp('2018-12-14 06:25:18')

Pandas Time Periods


The Time Periods represent the time span, e.g., days, years, quarter or month, etc. It is
defined as a class that allows us to convert the frequency to the periods.

Generating periods and frequency conversion


We can generate the period by using 'Period' command with frequency 'M'. If we use
'asfreq' operation with 'start' operation, the date will print '01' whereas if we use the
'end' option, the date will print '31'.

Example:

1. import pandas as pd
2. x = pd.Period('2014', freq='S')
3. x.asfreq('D', 'start')

Output:

Period('2014-01-01', 'D')

Example:

1. import pandas as pd
2. x = pd.Period('2014', freq='S')
3. x.asfreq('D', 'end')

Output:

Period('2014-01-31', 'D')

Period arithmetic
Period arithmetic is used to perform various arithmetic operation on periods. All the
operations will be performed on the basis of 'freq'.
1. import pandas as pd
2. x = pd.Period('2014', freq='Q')
3. x

Output:

Period('2014', 'Q-DEC')

Example:

1. import pandas as pd
2. x = pd.Period('2014', freq='Q')
3. x + 1

Output:

Period('2015', 'Q-DEC')

Creating period range


We can create the range of period by using the 'period_range' command.

1. import pandas as pd
2. p = pd.period_range('2012', '2017', freq='A')
3. p

Output:

PeriodIndex(['2012-01-02', '2012-01-03', '2012-01-04', '2012-01-05',


'2012-01-06', '2012-01-09', '2012-01-10', '2012-01-11',
'2012-01-12', '2012-01-13',
'2016-12-20', '2016-12-21', '2016-12-22', '2016-12-23',
'2016-12-26', '2016-12-27', '2016-12-28', '2016-12-29',
'2016-12-30', '2017-01-02'],
dtype='period[B]', length=1306, freq='B')

Converting string-dates to period


If we want to Convert the string-dates to period, first we need to convert the string to
date format and then we can convert the dates into the periods.

1. # dates as string
2. p = ['2012-06-05', '2011-07-09', '2012-04-06']
3. # convert string to date format
4. x = pd.to_datetime(p)
5. x

Output:

DatetimeIndex(['2012-06-05', '2011-07-09', '2012-04-06'],


dtype='datetime64[ns]', freq=None)

Convert periods to timestamps


If we convert periods back to timestamps, we can simply do it by using 'to_timestamp'
command.

1. import pandas as pd
2. prd
3. prd.to_timestamp()

Output:

DatetimeIndex(['2017-04-02', '2016-04-06', '2016-05-08'],


dtype='datetime64[ns]

Convert string to date


In today's time, it is a tedious task to analyze datasets with dates and times. Because
of different lengths in months, distributions of the weekdays and weekends, leap years,
and the time zones are the things that needs to consider according to our context. So,
for this reason, Python has defined a new data type especially for dates and times
called datetime.

However, in many datasets, Strings are used to represent the dates. So, in this topic,
you'll learn about converting date strings to the datetime format and see how these
powerful set of tools helps to work effectively with complicated time series data.

The challenge behind this scenario is how the date strings are expressed. For example,
'Wednesday, June 6, 2018' can also be shown as '6/6/18' and '06-06-2018'. All these
formats define the same date, but the code represents to convert each of them is
slightly different.

1. fromdatetime import datetime


2.
3. # Define dates as the strings
4. dmy_str1 = 'Wednesday, July 14, 2018'
5. dmy_str2 = '14/7/17'
6. dmy_str3 = '14-07-2017'
7.
8. # Define dates as the datetime objects
9. dmy_dt1 = datetime.strptime(date_str1, '%A, %B %d, %Y')
10. dmy_dt2 = datetime.strptime(date_str2, '%m/%d/%y')
11. dmy_dt3 = datetime.strptime(date_str3, '%m-%d-%Y')
12.
13. #Print the converted dates
14. print(dmy_dt1)
15. print(dmy_dt2)
16. print(dmy_dt3)

Output:

HTML Tutorial
2017-07-14 00:00:00
2017-07-14 00:00:00
2018-07-14 00:00:00

Converting the date string column


This conversion shows how to convert whole column of date strings from the dataset
to datetime format.

From now on, you have to work with the DataFrame called eth that contains the
historical data on ether, and also a cryptocurrency whose blockchain is produced by
the Ethereum platform. The dataset consists the following columns:

o date: Defines the actual date, daily at 00:00 UTC.


o txVolume: It refers an unadjusted measure of the total value in US dollars, in outputs
on the blockchain.
o txCount: It defines number of transactions performed on public blockchain.
o marketCap: Refers to the unit price in US dollars multiplied by the number of units in
circulation.
o price: Refers an opening price in US dollars at 00:00 UTC.
o generatedCoins: Refers the number of new coins.
o exchangeVolume: Refers the actual volume which is measured by US dollars, at
exchanges like GDAX and Bitfinex.

Pandas Plot
It is used to make plots of DataFrame using matplotlib / pylab. Every plot kind has a
corresponding method on the DataFrame.plot accessor: df.plot(kind='line') that are
generally equivalent to the df.plot.line().

Syntax:

1. DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, share


x=None, sharey=False, layout=None, figsize=None, use_index=True, title=No
ne, grid=None, legend=True, style=None, logx=False, logy=False, loglog=Fals
e, xticks=None, yticks=None, xlim=None, ylim=None, rot=None, fontsize=No
ne, colormap=None, table=False, yerr=None, xerr=None, secondary_y=False,
sort_columns=False, **kwds)

Parameters:
data: DataFrame

x: Refers to label or position, default value None

y: Refers to label, position or list of label, positions, default value None

HTML Tutorial

It allows the plotting of one column versus another.

kind: str

o 'line': line plot (default)


o 'bar': vertical bar plot
o 'barh': horizontal bar plot
o 'hist': histogram
o 'box': boxplot
o 'kde': Kernel Density Estimation plot
o 'density': same as 'kde'
o 'area': area plot
o 'pie': pie plot
o 'scatter': scatter plot
o 'hexbin': hexbin plot

ax: matplotlib axes object, default None


subplots: boolean, default False

Make separate subplots for each column

sharex: It returns the boolean value and default value True if the ax is None else
returns False.

If the subplots =True, it shares the x-axis and set some x-axis labels to the invisible;

Its default value is True if ax is None; otherwise, if an ax is passed, it returns false. If you
pass True on both an ax and shareax, it will alter all the x-axis labels.

sharey: It also returns a boolean value that default value False.

If the subplots= True, it shares the y-axis and set some y-axis to the labels to invisible.

layout: It is an optional parameter that refers to the tuple for the layout of subplots.

figsize: Refers to a tuple (width, height) in inches.

use_index: It returns the boolean value; default value True.

It uses the index as ticks for the x-axis.

title: Refers to a string or list that defines a title for the plot. If we pass a string, it will
print string at the top of the figure. If we pass a list and subplots as True, it will print
each item in the list in the corresponding subplot.

grid: Returns the boolean value, the default value is None. It defines the axis grid lines.

legend: Returns the False/True/'reverse' and place the legend on axis subplots.

style: Returns the list or dict. It defines the matplotlib line style per column.

logx: Returns the boolean value; the default value is False.

It generally uses a log scale on the x-axis.

logy: Returns the boolean value; the default value is False.

It generally uses log scaling on the y-axis.

loglog: Returns the boolean value; the default value is False.

It uses log scaling on both x and y axes


xticks: Refers to a sequence that consists of values to use for the xticks.

yticks: Refers to a sequence that consists of values to use for the yticks.

xlim: It consists 2-tuple/list.

ylim: It consists 2-tuple/list

rot: Refers to an integer value; the default value None

It generally Rotates for ticks (xticks for vertical, yticks for horizontal plots)

fontsize: Refers to an integer value; the default value is None.

Its main task is to specify the font size for xticks and yticks.

colormap: Refers to str or matplotlib colormap object, default value is None.

It provides colormap to select colors. If a value is a string, it loads colormap with that
name from matplotlib.

colorbar: It is an optional parameter that returns a boolean value.

If the value is True, it plots the colorbar (only relevant for 'scatter' and 'hexbin' plots)

position: Refers to float value.

Its main task is to specify the relative alignments for the bar plot layout. Its value ranges
from 0 (left/bottom-end) to 1 (right/top-end). The default value is 0.5 (center).

table: Returns the boolean value, Series or DataFrame, default value False

If the value is True, it draws a table using the data in the DataFrame.

If we pass a Series or DataFrame, it will pass data to draw a table.

yerr: Refers to the DataFrame, Series, array-like, dict, and str.

xerr: It is the same type as yerr.

stacked: Returns the boolean value; the default value is False in line and

bar plots, and True in area plot. If the value is True, it creates a stacked plot.

sort_columns: Returns the boolean value; the default value is False


It sorts column names to determine plot ordering

secondary_y: Returns the boolean value or sequence; the default value is False.

It checks whether to plot on the secondary y-axis. If a list/tuple, it plots the columns of
list /tuple on the secondary y-axis

mark_right: Returns the boolean value; the default value is True.

It is used when using a secondary_y axis, automatically mark the column labels with
"(right)" in the legend

'**kwds': It is an optional parameter that refers to the options to pass to the matplotlib
plotting method.

Example:

1. # import libraries
2. import matplotlib.pyplot as plt
3. import pandas as pd
4. import numpy as np
5. p = pd.Series(np.random.randn(2000), index = pd.date_range(
6. '2/2/2000', periods = 2000))
7. p = ts.cumsum()
8. p.plot()
9. plt.show()

Output:
Python Pandas interview questions

A list of top frequently asked Python Pandas Interview Questions and answers are
given below.

1) Define the Pandas/Python pandas?


Pandas is defined as an open-source library that provides high-performance data
manipulation in Python. The name of Pandas is derived from the word Panel Data,
which means an Econometrics from Multidimensional data. It can be used for data
analysis in Python and developed by Wes McKinney in 2008. It can perform five
significant steps that are required for processing and analysis of data irrespective of
the origin of the data, i.e., load, manipulate, prepare, model, and analyze.

2) Mention the different types of Data Structures in Pandas?


Pandas provide two data structures, which are supported by the pandas
library, Series, and DataFrames. Both of these data structures are built on top of the
NumPy.

A Series is a one-dimensional data structure in pandas, whereas the DataFrame is the


two-dimensional data structure in pandas.

456.1K

Jamie Lee Curtis proudly reveals youngest child is transgender

3) Define Series in Pandas?


A Series is defined as a one-dimensional array that is capable of storing various data
types. The row labels of series are called the index. By using a 'series' method, we can
easily convert the list, tuple, and dictionary into series. A Series cannot contain multiple
columns.

4) How can we calculate the standard deviation from the Series?


The Pandas std() is defined as a function for calculating the standard deviation of the
given set of numbers, DataFrame, column, and rows.

Series.std(axis=None, skipna=None, level=None, ddof=1, numeric_only=None,


**kwargs)

5) Define DataFrame in Pandas?


A DataFrame is a widely used data structure of pandas and works with a two-
dimensional array with labeled axes (rows and columns) DataFrame is defined as a
standard way to store data and has two different indexes, i.e., row index and column
index. It consists of the following properties:

o The columns can be heterogeneous types like int and bool.


o It can be seen as a dictionary of Series structure where both the rows and columns are
indexed. It is denoted as "columns" in the case of columns and "index" in case of rows.

6) What are the significant features of the pandas Library?


The key features of the panda's library are as follows:

o Memory Efficient
o Data Alignment
o Reshaping
o Merge and join
o Time Series

7) Explain Reindexing in pandas?


Reindexing is used to conform DataFrame to a new index with optional filling logic. It
places NA/NaN in that location where the values are not present in the previous index.
It returns a new object unless the new index is produced as equivalent to the current
one, and the value of copy becomes False. It is used to change the index of the rows
and columns of the DataFrame.

8) What is the name of Pandas library tools used to create a


scatter plot matrix?
Scatter_matrix

9) Define the different ways a DataFrame can be created in


pandas?
We can create a DataFrame using following ways:

o Lists
o Dict of ndarrays

Example-1: Create a DataFrame using List:

1. import pandas as pd
2. # a list of strings
3. a = ['Python', 'Pandas']
4. # Calling DataFrame constructor on list
5. info = pd.DataFrame(a)
6. print(info)

Output:

0
0 Python
1 Pandas

Example-2: Create a DataFrame from dict of ndarrays:

1. import pandas as pd
2. info = {'ID' :[101, 102, 103],'Department' :['B.Sc','B.Tech','M.Tech',]}
3. info = pd.DataFrame(info)
4. print (info)

Output:

ID Department
0 101 B.Sc
1 102 B.Tech
2 103 M.Tech

10) Explain Categorical data in Pandas?


A Categorical data is defined as a Pandas data type that corresponds to a categorical
variable in statistics. A categorical variable is generally used to take a limited and
usually fixed number of possible values. Examples: gender, country affiliation, blood
type, social class, observation time, or rating via Likert scales. All values of categorical
data are either in categories or np.nan.

This data type is useful in the following cases:

o It is useful for a string variable that consists of only a few different values. If we want
to save some memory, we can convert a string variable to a categorical variable.
o It is useful for the lexical order of a variable that is not the same as the logical order
(?one?, ?two?, ?three?) By converting into a categorical and specify an order on the
categories, sorting and min/max is responsible for using the logical order instead of
the lexical order.
o It is useful as a signal to other Python libraries because this column should be treated
as a categorical variable.

11) How will you create a series from dict in Pandas?


A Series is defined as a one-dimensional array that is capable of storing various data
types.

We can create a Pandas Series from Dictionary:

Create a Series from dict:

We can also create a Series from dict. If the dictionary object is being passed as an
input and the index is not specified, then the dictionary keys are taken in a sorted order
to construct the index.
If index is passed, then values correspond to a particular label in the index will be
extracted from the dictionary.

1. import pandas as pd
2. import numpy as np
3. info = {'x' : 0., 'y' : 1., 'z' : 2.}
4. a = pd.Series(info)
5. print (a)

Output:

x 0.0
y 1.0
z 2.0
dtype: float64

12) How can we create a copy of the series in Pandas?


We can create the copy of series by using the following syntax:

pandas.Series.copy
Series.copy(deep=True)

The above statements make a deep copy that includes a copy of the data and the
indices. If we set the value of deep to False, it will neither copy the indices nor the
data.

Note: If we set deep=True, the data will be copied, and the actual python objects will
not be copied recursively, only the reference to the object will be copied.

13) How will you create an empty DataFrame in Pandas?


A DataFrame is a widely used data structure of pandas and works with a two-
dimensional array with labeled axes (rows and columns) It is defined as a standard way
to store data and has two different indexes, i.e., row index and column index.

Create an empty DataFrame:

The below code shows how to create an empty DataFrame in Pandas:

1. # importing the pandas library


2. import pandas as pd
3. info = pd.DataFrame()
4. print (info)

Output:

Empty DataFrame
Columns: []
Index: []

14) How will you add a column to a pandas DataFrame?


We can add any new column to an existing DataFrame. The below code demonstrates
how to add any new column to an existing DataFrame:

1. # importing the pandas library


2. import pandas as pd
3. info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),
4. 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
5.
6. info = pd.DataFrame(info)
7.
8. # Add a new column to an existing DataFrame object
9.
10. print ("Add new column by passing series")
11. info['three']=pd.Series([20,40,60],index=['a','b','c'])
12. print (info)
13. print ("Add new column using existing DataFrame columns")
14. info['four']=info['one']+info['three']
15. print (info)

Output:

Add new column by passing series


one two three
a 1.0 1 20.0
b 2.0 2 40.0
c 3.0 3 60.0
d 4.0 4 NaN
e 5.0 5 NaN
f NaN 6 NaN

Add new column using existing DataFrame columns


one two three four
a 1.0 1 20.0 21.0
b 2.0 2 40.0 42.0
c 3.0 3 60.0 63.0
d 4.0 4 NaN NaN
e 5.0 5 NaN NaN
f NaN 6 NaN NaN

15) How to add an Index, row, or column to a Pandas


DataFrame?
Adding an Index to a DataFrame

Pandas allow adding the inputs to the index argument if you create a DataFrame. It
will make sure that you have the desired index. If you don?t specify inputs, the
DataFrame contains, by default, a numerically valued index that starts with 0 and ends
on the last row of the DataFrame.

Adding Rows to a DataFrame

We can use .loc, iloc, and ix to insert the rows in the DataFrame.

o The loc basically works for the labels of our index. It can be understood as if we insert
in loc[4], which means we are looking for that values of DataFrame that have an index
labeled 4.
o The iloc basically works for the positions in the index. It can be understood as if we
insert in iloc[4], which means we are looking for the values of DataFrame that are
present at index '4`.
o The ix is a complex case because if the index is integer-based, we pass a label to ix.
The ix[4] means that we are looking in the DataFrame for those values that have an
index labeled 4. However, if the index is not only integer-based, ix will deal with the
positions as iloc.

Adding Columns to a DataFrame

If we want to add the column to the DataFrame, we can easily follow the same
procedure as adding an index to the DataFrame by using loc or iloc.

16) How to Delete Indices, Rows or Columns From a Pandas


Data Frame?
Deleting an Index from Your DataFrame
If you want to remove the index from the DataFrame, you should have to do the
following:

Reset the index of DataFrame.

Executing del df.index.name to remove the index name.

Remove duplicate index values by resetting the index and drop the duplicate values
from the index column.

Remove an index with a row.

Deleting a Column from Your DataFrame

You can use the drop() method for deleting a column from the DataFrame.

The axis argument that is passed to the drop() method is either 0 if it indicates the
rows and 1 if it drops the columns.

You can pass the argument inplace and set it to True to delete the column without
reassign the DataFrame.

You can also delete the duplicate values from the column by using the
drop_duplicates() method.

Removing a Row from Your DataFrame

By using df.drop_duplicates(), we can remove duplicate rows from the DataFrame.

You can use the drop() method to specify the index of the rows that we want to
remove from the DataFrame.

17) How to Rename the Index or Columns of a Pandas


DataFrame?
You can use the .rename method to give different values to the columns or the index
values of DataFrame.

18) How to iterate over a Pandas DataFrame?


You can iterate over the rows of the DataFrame by using for loop in combination with
an iterrows() call on the DataFrame.

19) How to get the items of series A not present in series B?


We can remove items present in p2 from p1 using isin() method.

1. import pandas as pd
2. p1 = pd.Series([2, 4, 6, 8, 10])
3. p2 = pd.Series([8, 10, 12, 14, 16])
4. p1[~p1.isin(p2)]

Solution

0 2
1 4
2 6
dtype: int64

20) How to get the items not common to both series A and series
B?
We get all the items of p1 and p2 not common to both using below example:

1. import pandas as pd
2. import numpy as np
3. p1 = pd.Series([2, 4, 6, 8, 10])
4. p2 = pd.Series([8, 10, 12, 14, 16])
5. p1[~p1.isin(p2)]
6. p_u = pd.Series(np.union1d(p1, p2)) # union
7. p_i = pd.Series(np.intersect1d(p1, p2)) # intersect
8. p_u[~p_u.isin(p_i)]

Output:

0 2
1 4
2 6
5 12
6 14
7 16
dtype: int64
21) How to get the minimum, 25th percentile, median, 75th, and
max of a numeric series?
We can compute the minimum, 25th percentile, median, 75th, and maximum of p as
below example:

1. import pandas as pd
2. import numpy as np
3. p = pd.Series(np.random.normal(14, 6, 22))
4. state = np.random.RandomState(120)
5. p = pd.Series(state.normal(14, 6, 22))
6. np.percentile(p, q=[0, 25, 50, 75, 100])

Output:

array([ 4.61498692, 12.15572753, 14.67780756, 17.58054104, 33.24975515])

22) How to get frequency counts of unique items of a series?


We can calculate the frequency counts of each unique value p as below example:

1. import pandas as pd
2. import numpy as np
3. p= pd.Series(np.take(list('pqrstu'), np.random.randint(6, size=17)))
4. p = pd.Series(np.take(list('pqrstu'), np.random.randint(6, size=17)))
5. p.value_counts()

Output:

s 4
r 4
q 3
p 3
u 3

23) How to convert a numpy array to a dataframe of given


shape?
We can reshape the series p into a dataframe with 6 rows and 2 columns as below
example:

1. import pandas as pd
2. import numpy as np
3. p = pd.Series(np.random.randint(1, 7, 35))
4. # Input
5. p = pd.Series(np.random.randint(1, 7, 35))
6. info = pd.DataFrame(p.values.reshape(7,5))
7. print(info)

Output:

0 1 2 3 4
0 3 2 5 5 1
1 3 2 5 5 5
2 1 3 1 2 6
3 1 1 1 2 2
4 3 5 3 3 3
5 2 5 3 6 4
6 3 6 6 6 5

24) How can we convert a Series to DataFrame?


The Pandas Series.to_frame() function is used to convert the series object to the
DataFrame.

1. Series.to_frame(name=None)

name: Refers to the object. Its Default value is None. If it has one value, the passed
name will be substituted for the series name.

1. s = pd.Series(["a", "b", "c"],


2. name="vals")
3. s.to_frame()

Output:

vals
0 a
1 b
2 c

25) What is Pandas NumPy array?


Numerical Python (Numpy) is defined as a Python package used for performing the
various numerical computations and processing of the multidimensional and single-
dimensional array elements. The calculations using Numpy arrays are faster than the
normal Python array.

26) How can we convert DataFrame into a NumPy array?


For performing some high-level mathematical functions, we can convert Pandas
DataFrame to numpy arrays. It uses the DataFrame.to_numpy() function.

The DataFrame.to_numpy() function is applied to the DataFrame that returns the


numpy ndarray.

1. DataFrame.to_numpy(dtype=None, copy=False)

27) How can we convert DataFrame into an excel file?


We can export the DataFrame to the excel file by using the to_excel() function.

To write a single object to the excel file, we have to specify the target file name. If we
want to write to multiple sheets, we need to create an ExcelWriter object with target
filename and also need to specify the sheet in the file in which we have to write.

28) How can we sort the DataFrame?


We can efficiently perform sorting in the DataFrame through different kinds:

o By label
o By Actual value

By label

The DataFrame can be sorted by using the sort_index() method. It can be done by
passing the axis arguments and the order of sorting. The sorting is done on row labels
in ascending order by default.

By Actual Value

It is another kind through which sorting can be performed in the DataFrame. Like index
sorting, sort_values() is a method for sorting the values.
It also provides a feature in which we can specify the column name of the DataFrame
with which values are to be sorted. It is done by passing the 'by' argument.

29) What is Time Series in Pandas?


The Time series data is defined as an essential source for information that provides a
strategy that is used in various businesses. From a conventional finance industry to the
education industry, it consists of a lot of details about the time.

Time series forecasting is the machine learning modeling that deals with the Time
Series data for predicting future values through Time Series modeling.

30) What is Time Offset?


The offset specifies a set of dates that conform to the DateOffset. We can create the
DateOffsets to move the dates forward to valid dates.

31) Define Time Periods?


The Time Periods represent the time span, e.g., days, years, quarter or month, etc. It is
defined as a class that allows us to convert the frequency to the periods.

32) How to convert String to date?


The below code demonstrates how to convert the string to date:

1. fromdatetime import datetime


2.
3. # Define dates as the strings
4. dmy_str1 = 'Wednesday, July 14, 2018'
5. dmy_str2 = '14/7/17'
6. dmy_str3 = '14-07-2017'
7.
8. # Define dates as the datetime objects
9. dmy_dt1 = datetime.strptime(date_str1, '%A, %B %d, %Y')
10. dmy_dt2 = datetime.strptime(date_str2, '%m/%d/%y')
11. dmy_dt3 = datetime.strptime(date_str3, '%m-%d-%Y')
12.
13. #Print the converted dates
14. print(dmy_dt1)
15. print(dmy_dt2)
16. print(dmy_dt3)

Output:

2017-07-14 00:00:00
2017-07-14 00:00:00
2018-07-14 00:00:00

33) What is Data Aggregation?


The main task of Data Aggregation is to apply some aggregation to one or more
columns. It uses the following:

o sum: It is used to return the sum of the values for the requested axis.
o min: It is used to return a minimum of the values for the requested axis.
o max: It is used to return a maximum values for the requested axis.

34) What is Pandas Index?


Pandas Index is defined as a vital tool that selects particular rows and columns of data
from a DataFrame. Its task is to organize the data and to provide fast accessing of data.
It can also be called a Subset Selection.

35) Define Multiple Indexing?


Multiple indexing is defined as essential indexing because it deals with data analysis
and manipulation, especially for working with higher dimensional data. It also enables
us to store and manipulate data with the arbitrary number of dimensions in lower-
dimensional data structures like Series and DataFrame.
36) Define ReIndexing?
Reindexing is used to change the index of the rows and columns of the DataFrame.
We can reindex the single or multiple rows by using the reindex() method. Default
values in the new index are assigned NaN if it is not present in the DataFrame.

DataFrame.reindex(labels=None, index=None, columns=None, axis=None,


method=None, copy=True, level=None, fill_value=nan, limit=None,
tolerance=None)

37) How to Set the index?


We can set the index column while making a data frame. But sometimes, a data frame
is made from two or more data frames, and then the index can be changed using this
method.

38) How to Reset the index?


The Reset index of the DataFrame is used to reset the index by using the 'reset_index'
command. If the DataFrame has a MultiIndex, this method can remove one or more
levels.

39) Describe Data Operations in Pandas?


In Pandas, there are different useful data operations for DataFrame, which are as
follows:

o Row and column selection

We can select any row and column of the DataFrame by passing the name of the rows
and columns. When you select it from the DataFrame, it becomes one-dimensional
and considered as Series.

o Filter Data

We can filter the data by providing some of the boolean expressions in DataFrame.

o Null values
A Null value occurs when no data is provided to the items. The various columns may
contain no values, which are usually represented as NaN.

40) Define GroupBy in Pandas?


In Pandas, groupby() function allows us to rearrange the data by utilizing them on
real-world data sets. Its primary task is to split the data into various groups. These
groups are categorized based on some criteria. The objects can be divided from any
of their axes.

DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True,


group_keys=True, squeeze=False, **kwargs)

Matplotlib (Python Plotting Library)

Human minds are more adaptive for the visual representation of data rather than
textual data. We can easily understand things when they are visualized. It is better to
represent the data through the graph where we can analyze the data more efficiently
and make the specific decision according to data analysis. Before learning the
matplotlib, we need to understand data visualization and why data visualization is
important.

Data Visualization
Graphics provides an excellent approach for exploring the data, which is essential for
presenting results. Data visualization is a new term. It expresses the idea that involves
more than just representing data in the graphical form (instead of using textual form).

This can be very helpful when discovering and getting to know a dataset and can help
with classifying patterns, corrupt data, outliers, and much more. With a little domain
knowledge, data visualizations can be used to express and demonstrate key
relationships in plots and charts. The static does indeed focus on quantitative
description and estimations of data. It provides an important set of tools for gaining a
qualitative understanding.

There are five key plots that are used for data visualization.

There are five phases which are essential to make the decision for the organization:
o Visualize: We analyze the raw data, which means it makes complex data more
accessible, understandable, and more usable. Tabular data representation is used
where the user will look up a specific measurement, while the chart of several types is
used to show patterns or relationships in the data for one or more variables.
o Analysis: Data analysis is defined as cleaning, inspecting, transforming, and modeling
data to derive useful information. Whenever we make a decision for the business or in
daily life, is by past experience. What will happen to choose a particular decision, it
is nothing but analyzing our past. That may be affected in the future, so the proper
analysis is necessary for better decisions for any business or organization.
o Document Insight: Document insight is the process where the useful data or
information is organized in the document in the standard format.
o Transform Data Set: Standard data is used to make the decision more effectively.

Why need data visualization?


Data visualization can perform below tasks:

o It identifies areas that need improvement and attention.


o It clarifies the factors.
o It helps to understand which product to place where.
o Predict sales volumes.

Benefit of Data Visualization


Here are some benefits of the data visualization, which helps to make an effective
decision for the organizations or business:

1. Building ways of absorbing information

Data visualization allows users to receive vast amounts of information regarding


operational and business conditions. It helps decision-makers to see the relationship
between multi-dimensional data sets. It offers new ways to analyses data through the
use of maps, fever charts, and other rich graphical representations.

Visual data discovery is more likely to find the information that the organization needs
and then end up with being more productive than other competitive companies.

2. Visualize relationship and patterns in Businesses

The crucial advantage of data visualization is that it is essential to find the correlation
between operating conditions and business performance in today's highly competitive
business environment.
The ability to make these types of correlations enables the executives to identify the
root cause of the problem and act quickly to resolve it.

Suppose a food company is looking their monthly customer data, and the data is
presented with bar charts, which shows that the company's score has dropped by five
points in the previous months in that particular region; the data suggest that there's a
problem with customer satisfaction in this area.

3. Take action on the emerging trends faster

Data visualization allows the decision-maker to grasp shifts in customer behavior and
market conditions across multiple data sets more efficiently.

Having an idea about the customer's sentiments and other data discloses an emerging
opportunity for the company to act on new business opportunities ahead of their
competitor.

4. Geological based Visualization

Geo-spatial visualization is occurred due to many websites providing web-services,


attracting visitor's interest. These types of websites are required to take benefit of
location-specific information, which is already present in the customer details.

Matplotlib is a Python library which is defined as a multi-platform data visualization


library built on Numpy array. It can be used in python scripts, shell, web application,
and other graphical user interface toolkit.

The John D. Hunter originally conceived the matplotlib in 2002. It has an active
development community and is distributed under a BSD-style license. Its first version
was released in 2003, and the latest version 3.1.1 is released on 1 July 2019.
Matplotlib 2.0.x supports Python versions 2.7 to 3.6 till 23 June 2007. Python3 support
started with Matplotlib 1.2. Matplotlib 1.4 is the last version that supports Python 2.6.

There are various toolkits available that are used to enhance the functionality of
the matplotlib. Some of these tools are downloaded separately, others can be shifted
with the matplotlib source code but have external dependencies.

o Bashmap: It is a map plotting toolkit with several map projections, coastlines, and
political boundaries.
o Cartopy: It is a mapping library consisting of object-oriented map projection
definitions, and arbitrary point, line, polygon, and image transformation abilities.
o Excel tools: Matplotlib provides the facility to utilities for exchanging data with
Microsoft Excel.
o Mplot3d: It is used for 3D plots.
o Natgrid: It is an interface to the Natgrid library for irregular gridding of the spaced
data.

Matplotlib Architecture
There are three different layers in the architecture of the matplotlib which are the
following:

o Backend Layer
o Artist layer
o Scripting layer

Backend layer

The backend layer is the bottom layer of the figure, which consists of the
implementation of the various functions that are necessary for plotting. There are three
essential classes from the backend layer FigureCanvas(The surface on which the figure
will be drawn), Renderer(The class that takes care of the drawing on the surface),
and Event(It handle the mouse and keyboard events).

Artist Layer

The artist layer is the second layer in the architecture. It is responsible for the various
plotting functions, like axis, which coordinates on how to use the renderer on the figure
canvas.

Scripting layer
The scripting layer is the topmost layer on which most of our code will run. The
methods in the scripting layer, almost automatically take care of the other layers, and
all we need to care about is the current state (figure & subplot).

The General Concept of Matplotlib


A Matplotlib figure can be categorized into various parts as below:

Figure: It is a whole figure which may hold one or more axes (plots). We can think of
a Figure as a canvas that holds plots.

Axes: A Figure can contain several Axes. It consists of two or three (in the case of 3D)
Axis objects. Each Axes is comprised of a title, an x-label, and a y-label.

Axis: Axises are the number of line like objects and responsible for generating the
graph limits.

Artist: An artist is the all which we see on the graph like Text objects, Line2D objects,
and collection objects. Most Artists are tied to Axes.

Installing Matplotlib
Before start working with the Matplotlib or its plotting functions first, it needs to be
installed. The installation of matplotlib is dependent on the distribution that is installed
on your computer. These installation methods are following:

Use the Anaconda distribution of Python

The easiest way to install Matplotlib is to download the Anaconda distribution of


Python. Matplotlib is pre-installed in the anaconda distribution No further installation
steps are necessary.

o Visit the official site of Anaconda and click on the Download Button

o Choose download according to your Python interpreter configuration.


Install Matplotlib using with Anaconda Prompt

Matplotlib can be installed using with the Anaconda Prompt by typing command. To
install matplotlib, open Anaconda Prompt and type the following command:

1. conda install matplotlib

Install Matplotlib with pip

The python package manager pip is also used to install matplotlib. Open the command
prompt window, and type the following command:

1. pip install matplotlib


Verify the Installation
To verify that matplotlib is installed properly or not, type the following command
includes calling .__version __ in the terminal.

1. import matplotlib
2. matplotlib.__version__
3. '3.1.1'

Basic Example of plotting Graph


Here is the basic example of generating a simple graph; the program is following:

1. from matplotlib import pyplot as plt


2. #ploting our canvas
3. plt.plot([1,2,3],[4,5,1])
4. #display the graph
5. plt.show()

Output:

It takes only three lines to plot a simple graph using the Python matplotlib. We can
add titles, labels to our chart which are created by Python matplotlib library to make it
more meaningful. The example is the following:

1. from matplotlib import pyplot as plt


2.
3. x = [5, 2, 7]
4. y = [1, 10, 4]
5. plt.plot(x, y)
6. plt.title('Line graph')
7. plt.ylabel('Y axis')
8. plt.xlabel('X axis')
9. plt.show()

Output:

The graph is more understandable from the previous graph.

Working with Pyplot


The matplotlib.pyplot is the collection command style functions that make matplotlib
feel like working with MATLAB. The pyplot functions are used to make some changes
to figure such as create a figure, creates a plotting area in a figure, plots some lines in
a plotting area, decorates the plot including labels, etc.

It is good to use when we want to plot something quickly without instantiating any
figure or Axes.

While working with matplotlib.pyplot, some states are stored across function calls so
that it keeps track of the things like current figure and plotting area, and these plotting
functions are directed to the current axes.

The pyplot module provide the plot() function which is frequently use to plot a graph.
Let's have a look on the simple example:

1. from matplotlib import pyplot as plt


2. plt.plot([1,2,3,4,5])
3. plt.ylabel("y axis")
4. plt.xlabel('x axis')
5. plt.show()

Output:

In the above program, it plots the graph x-axis ranges from 0-4 and the y-axis from 1-
5. If we provide a single list to the plot(), matplotlib assumes it is a sequence of y values,
and automatically generates the x values. Since we know that python index starts at 0,
the default x vector has the same length as y but starts at 0. Hence the x data are [0, 1,
2, 3, 4].

We can pass the arbitrary number of arguments to the plot(). For example, to plot x
versus y, we can do this following way:

1. from matplotlib import pyplot as plt


2. plt.plot([1,2,3,4,5],[1,4,9,16,25])
3. plt.ylabel("y axis")
4. plt.xlabel('x axis')
5. plt.show()

Output:
Formatting the style of the plot

There is an optional third argument, which is a format string that indicates the color
and line type of the plot. The default format string is 'b-'which is the solid blue as you
can observe in the above plotted graph. Let's consider the following example where
we plot the graph with the red circle.

1. from matplotlib import pyplot as plt


2. plt.plot([1, 2, 3, 4,5], [1, 4, 9, 16,25], 'ro')
3. plt.axis([0, 6, 0, 20])
4. plt.show()

Output:

Example format String


'b' Using for the blue marker with default shape.

'ro' Red circle

'-g' Green solid line

'--' A dashed line with the default color

'^k:' Black triangle up markers connected by a dotted line

The matplotlib supports the following color abbreviation:

Character Color

'b' Blue

'g' Green

'r' Red

'c' Cyan

'm' Magenta

'y' Yellow

'k' Black

'w' White

Plotting with categorical variables


Matplotlib allows us to pass categorical variables directly to many plotting functions:
consider the following example

1. from matplotlib import pyplot


2. names = ['Abhishek', 'Himanshu', 'Devansh']
3. marks= [87,50,98]
4.
5. plt.figure(figsize=(9,3))
6.
7. plt.subplot(131)
8. plt.bar(names, marks)
9. plt.subplot(132)
10. plt.scatter(names, marks)
11. plt.subplot(133)
12. plt.plot(names, marks)
13. plt.suptitle('Categorical Plotting')
14. plt.show()

Output:

In the above program, we have plotted the categorical graph using


the subplot() function. Let's a have a look on the subplot() function.

What is subplot()
The Matplotlib subplot() function is defined as to plot two or more plots in one figure.
We can use this method to separate two graphs which plotted in the same axis
Matplotlib supports all kinds of subplots, including 2x1 vertical, 2x1 horizontal, or a
2x2 grid.

It accepts the three arguments: they are nrows, ncols, and index. It denote the
number of rows, number of columns and the index.

The subplot() function can be called in the following way:


1. subplot(nrows,ncols,index,**kwargs)
2. subplot(pos,**kwargs)
3. subplot(ax)

Parameters:

o *args:

Three separate integers or three-digit integer describes the position of the subplot. If
the three integers are nrows, ncols, and index in order, the subplot will take the index
position on a grid with nrows row and ncol column.

The argument pos are a three-digit integer, where the first digit is denoted the number
of rows, the second digit denoted the number of columns, and the third represents
the index of the subplot. For example, subplot (1, 3, 2) is the same as the subplot
(132).

Note: Passed integer must be less than 10.


o **kwargs

The subplot() function also accepts the keyword arguments for the returned axes base
class.

Consider the following example:

Creating different types of graph


1. Line graph
The line graph is one of charts which shows information as a series of the line. The
graph is plotted by the plot() function. The line graph is simple to plot; let's consider
the following example:

1. from matplotlib import pyplot as plt


2.
3. x = [4,8,9]
4. y = [10,12,15]
5.
6. plt.plot(x,y)
7.
8. plt.title("Line graph")
9. plt.ylabel('Y axis')
10. plt.xlabel('X axis')
11. plt.show()

Output:

We can customize the graph by importing the style module. The style module will be
built into a matplotlib installation. It contains the various functions to make the plot
more attractive. In the below program, we are using the style module:

1. from matplotlib import pyplot as plt


2. from matplotlib import style
3.
4. style.use('ggplot')
5. x = [16, 8, 10]
6. y = [8, 16, 6]
7. x2 = [8, 15, 11]
8. y2 = [6, 15, 7]
9. plt.plot(x, y, 'r', label='line one', linewidth=5)
10. plt.plot(x2, y2, 'm', label='line two', linewidth=5)
11. plt.title('Epic Info')
12. fig = plt.figure()
13. plt.ylabel('Y axis')
14. plt.xlabel('X axis')
15. plt.legend()
16. plt.grid(True, color='k')
17. plt.show()
Output:

In Matplotlib, the figure (an instance of class plt.Figure) can be supposed of as a single
container that consists of all the objects denoting axes, graphics, text, and labels.

Example-3

1. import numpy as np
2. import matplotlib.pyplot as plt
3.
4. fig = plt.figure()
5. ax = plt.axes()
6.
7. x = np.linspace(0, 10, 1000)
8. ax.plot(x, np.sin(x))

Output:
The matplotlib provides the fill_between() function which is used to fill area around
the lines based on the user defined logic.

Example-4

1. import numpy as np
2. import matplotlib.pyplot as plt
3.
4. fig = plt.figure()
5. ax = plt.axes()
6.
7. x = np.linspace(0, 10, 1000)
8. ax.plot(x, np.sin(x))
9.
10.
11. import matplotlib.pyplot as plt
12. import numpy as np
13.
14. x = np.arange(0.0, 2, 0.01)
15. y1 = np.sin(2 * np.pi * x)
16. y2 = 1.2 * np.sin(4 * np.pi * x)
17. fig, ax = plt.subplots(1, sharex=True)
18. ax.plot(x, y1, x, y2, color='black')
19. ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='blue', interpolate=True)
20. ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)
21. ax.set_title('fill between where')
Output:

2. Bar graphs
Bar graphs are one of the most common types of graphs and are used to show data
associated with the categorical variables. Matplotlib provides a bar() to make bar
graphs which accepts arguments such as: categorical variables, their value and color.

1. from matplotlib import pyplot as plt


2. players = ['Virat','Rohit','Shikhar','Hardik']
3. runs = [51,87,45,67]
4. plt.bar(players,runs,color = 'green')
5. plt.title('Score Card')
6. plt.xlabel('Players')
7. plt.ylabel('Runs')
8. plt.show()

Output:
Another function barh() is used to make horizontal bar graphs. It
accepts xerr or yerr as arguments (in case of vertical graphs) to depict the variance in
our data as follows:

1. from matplotlib import pyplot as plt


2. players = ['Virat','Rohit','Shikhar','Hardik']
3. runs = [51,87,45,67]
4. plt.barh(players,runs, color = 'green')
5. plt.title('Score Card')
6. plt.xlabel('Players')
7. plt.ylabel('Runs')
8. plt.show()

Output:
Let's have a look on the other example using the style() function:

1. from matplotlib import pyplot as plt


2. from matplotlib import style
3.
4. style.use('ggplot')
5.
6. x = [5,8,10]
7. y = [12,16,6]
8.
9. x2 = [6,9,11]
10. y2 = [7,15,7]
11.
12.
13. plt.bar(x, y, color = 'y', align='center')
14. plt.bar(x2, y2, color='c', align='center')
15.
16. plt.title('Information')
17.
18. plt.ylabel('Y axis')
19. plt.xlabel('X axis')

Output:
Similarly to vertical stack, the bar graph together by using the bottom argument and
define the bar graph, which we want to stack below and its value.

1. from matplotlib import pyplot as plt


2. import numpy as np
3.
4. countries = ['USA', 'India', 'China', 'Russia', 'Germany']
5. bronzes = np.array([38, 17, 26, 19, 15])
6. silvers = np.array([37, 23, 18, 18, 10])
7. golds = np.array([46, 27, 26, 19, 17])
8. ind = [x for x, _ in enumerate(countries)]
9.
10. plt.bar(ind, golds, width=0.5, label='golds', color='gold', bottom=silvers+bronzes)
11. plt.bar(ind, silvers, width=0.5, label='silvers', color='silver', bottom=bronzes)
12. plt.bar(ind, bronzes, width=0.5, label='bronzes', color='#CD853F')
13.
14. plt.xticks(ind, countries)
15. plt.ylabel("Medals")
16. plt.xlabel("Countries")
17. plt.legend(loc="upper right")
18. plt.title("2019 Olympics Top Scorers")

Output:
3. Pie Chart
A pie chart is a circular graph that is broken down in the segment or slices of pie. It is
generally used to represent the percentage or proportional data where each slice of
pie represents a particular category. Let's have a look at the below example:

1. from matplotlib import pyplot as plt


2.
3. # Pie chart, where the slices will be ordered and plotted counter-clockwise:
4. Players = 'Rohit', 'Virat', 'Shikhar', 'Yuvraj'
5. Runs = [45, 30, 15, 10]
6. explode = (0.1, 0, 0, 0) # it "explode" the 1st slice
7.
8. fig1, ax1 = plt.subplots()
9. ax1.pie(Runs, explode=explode, labels=Players, autopct='%1.1f%%',
10. shadow=True, startangle=90)
11. ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
12.
13. plt.show()

Output:
4. Histogram
First, we need to understand the difference between the bar graph and histogram. A
histogram is used for the distribution, whereas a bar chart is used to compare different
entities. A histogram is a type of bar plot that shows the frequency of a number of
values compared to a set of values ranges.

For example we take the data of the different age group of the people and plot a
histogram with respect to the bin. Now, bin represents the range of values that are
divided into series of intervals. Bins are generally created of the same size.

1. from matplotlib import pyplot as plt


2. from matplotlib import pyplot as plt
3. population_age = [21,53,60,49,25,27,30,42,40,1,2,102,95,8,15,105,70,65,55,70,7
5,60,52,44,43,42,45]
4. bins = [0,10,20,30,40,50,60,70,80,90,100]
5. plt.hist(population_age, bins, histtype='bar', rwidth=0.8)
6. plt.xlabel('age groups')
7. plt.ylabel('Number of people')
8. plt.title('Histogram')
9. plt.show()

Output:
Let's consider the another example of plotting histogram:

1. from matplotlib import pyplot as plt


2. # Importing Numpy Library
3. import numpy as np
4. plt.style.use('fivethirtyeight')
5.
6. mu = 50
7. sigma = 7
8. x = np.random.normal(mu, sigma, size=200)
9. fig, ax = plt.subplots()
10.
11. ax.hist(x, 20)
12. ax.set_title('Historgram')
13. ax.set_xlabel('bin range')
14. ax.set_ylabel('frequency')
15.
16. fig.tight_layout()
17. plt.show()

Output:
5. Scatter plot
The scatter plots are mostly used for comparing variables when we need to define how
much one variable is affected by another variable. The data is displayed as a collection
of points. Each point has the value of one variable, which defines the position on the
horizontal axes, and the value of other variable represents the position on the vertical
axis.

Let's consider the following simple example:

Example-1:

1. from matplotlib import pyplot as plt


2. from matplotlib import style
3. style.use('ggplot')
4.
5. x = [5,7,10]
6. y = [18,10,6]
7.
8. x2 = [6,9,11]
9. y2 = [7,14,17]
10.
11. plt.scatter(x, y)
12.
13. plt.scatter(x2, y2, color='g')
14.
15. plt.title('Epic Info')
16. plt.ylabel('Y axis')
17. plt.xlabel('X axis')
18.
19. plt.show()

Output:

Example-2

1. import matplotlib.pyplot as plt


2. x = [2, 2.5, 3, 3.5, 4.5, 4.7, 5.0]
3. y = [7.5, 8, 8.5, 9, 9.5, 10, 10.5]
4.
5. x1 = [9, 8.5, 9, 9.5, 10, 10.5, 12]
6. y1 = [3, 3.5, 4.7, 4, 4.5, 5, 5.2]
7. plt.scatter(x, y, label='high income low saving', color='g')
8. plt.scatter(x1, y1, label='low income high savings', color='r')
9. plt.xlabel('saving*100')
10. plt.ylabel('income*1000')
11. plt.title('Scatter Plot')
12. plt.legend()
13. plt.show()

Output:
6. 3D graph plot
Matplotlib was initially developed with only two-dimension plot. Its 1.0 release was
built with some of three-dimensional plotting utilities on top of two-dimension display,
and the result is a convenient set of tools for 3D data visualization.

Three-dimension plots can be created by importing the mplot3d toolkit, include with
the main Matplotlib installation:

1. from mpl_toolkits import mplot3d

When this module is imported in the program, three-dimension axes can be created
by passing the keyword projection='3d' to any of the normal axes creation routines:

Let's see the simple 3D plot

Example-1:

1. from mpltoolkits import mplot3d


2. import numpy as np
3. import matplotlib.pyplot as plt
4. fig = plt.figure()
5. ax = plt.axes(projection='3d')_

Output:
Example-2:

1. from mpl_toolkits import mplot3d


2. import numpy as np
3. import matplotlib.pyplot as plt
4.
5. height = np.array([100,110,87,85,65,80,96,75,42,59,54,63,95,71,86])
6. weight = np.array([105,123,84,85,78,95,69,42,87,91,63,83,75,41,80])
7.
8.
9. scatter(height,weight)
10.
11. fig = plt.figure()
12. ax = plt.axes(projection='3d')
13. # This is used to plot 3D scatter
14. ax.scatter3D(height,weight)
15. plt.title("3D Scatter Plot")
16. plt.xlabel("Height")
17. plt.ylabel("Weight")
18. plt.title("3D Scatter Plot")
19. plt.xlabel("Height")
20. plt.ylabel("Weight")
21.
22. plt.show()

Output:
Note: We can use the plot3D () to plot simple 3D line graph.

Example-3

1. import matplotlib as mpl


2. from mpl_toolkits.mplot3d import Axes3D
3. import numpy as np
4. import matplotlib.pyplot as plt
5.
6. mpl.rcParams['legend.fontsize'] = 10
7.
8. fig = plt.figure()
9. ax = fig.gca(projection='3d')
10. theta1 = np.linspace(-4 * np.pi, 4 * np.pi, 100)
11. z = np.linspace(-2, 2, 100)
12. r = z**2 + 1
13. x = r * np.sin(theta1)
14. y = r * np.cos(theta1)
15. ax.plot3D(x, y, z, label='parametric curve', color = 'red')
16. ax.legend()
17.
18. plt.show()

Output:
Important functions of Matplotlib

Functions Description

plot(x-axis value, y-axis-values) It is used to plot a simple line graph with x-axis value against the y-
axis values. show() It is used to display the graph.

title("string") It is used to set the title of the plotted graph as specified by the
string.

xlabel("string") It is used to set the label for the x-axis as specified by the string.

ylabel("string") It is used to set the label for y-axis as specified by the string.

figure() It is used to control a figure level attributes.

subplots(nrows,ncol,index) It is used to add a subplot to recent figure.

subtitle("string") It adds a common title to the plotted graph specified by the string.

subplots(nrows,ncols,figsize) It provides the simple way to create subplot, in a single call and
returns a tuple of a figure and number of axes.

set_title("string") It is an axes level method which is used to set the title of the subplots.

bar(categorical variables, values, It is used to create a vertical bar graph.


color)
barh(categorical variables, values, It is used to create horizontal bar graphs.
color)

legend(loc) It is used to make a legend of the graph.

xtricks(index, categorical variables) It is used to set or get the current tick locations labels of the x-axis.

pie(value, categorical variables) It is used to create a pie chart.

hist(value, number of bins) It is used to create a histogram.

xlim(start value, end value) It is used to set the limit of values of the x-axis.

ylim(start value, end value) It is used to set the limit of values of the y-axis.

scatter(x-axis values, y-axis values) It is used to plots a scatter plot with x-axis value against the y-axis
values.

axes() It is used to add axes to the recent figure.

set_xlabel("string") It is an axes level method which is used to set the x-label of the plot
specified as a string.

set_ylabel("string") It is used to set the y-label of the plot specified as a string.

scatter3D(x-axis values, y-axis It is used to plot a three-dimension scatter plot with x- axis value
values) against the y-axis.

plot3D(x-axis values, y-axis values) It is used to plots a three-dimension line graph with x- axis values
against y-axis values.

In this tutorial, we have learned about the matplotlib (Python Library), where we
covered the brief introduction of data visualization and how data visualization is
essential to make a decision for the organization. We have plotted the different types
of graphs for the graphical representation of the data.

Django Tutorial
Django Tutorial provides basic and advanced concepts of Django. Our Django Tutorial
is designed for beginners and professionals both.

Django is a Web Application Framework which is used to develop web applications.

Our Django Tutorial includes all topics of Django such as introduction, features,
installation, environment setup, admin interface, cookie, form validation, Model,
Template Engine, Migration, MVT etc. All the topics are explained in detail so that
reader can get enought knowledge of Django.

Introduction
Django is a web application framework written in Python programming language. It is
based on MVT (Model View Template) design pattern. The Django is very demanding
due to its rapid development feature. It takes less time to build application after
collecting client requirement.

60.9K

Four qualities that can help you become a developer fast - My personal story - from a dude to coder

This framework uses a famous tag line:The web framework for perfectionists with
deadlines.

By using Django, we can build web applications in very less time. Django is designed
in such a manner that it handles much of configure things automatically, so we can
focus on application development only.

History
Django was design and developed by Lawrence journal world in 2003 and publicly
released under BSD license in July 2005. Currently, DSF (Django Software Foundation)
maintains its development and release cycle.
Django was released on 21, July 2005. Its current stable version is 2.0.3 which was
released on 6 March, 2018.

Django Version History

Version Date Description

0.90 16 Nov
2005

0.91 11 Jan 2006 magic removal

0.96 23 Mar 2007 newforms, testing tools

1.0 3 Sep 2008 API stability, decoupled admin, unicode

1.1 29 Jul 2009 Aggregates, transaction based tests

1.2 17 May Multiple db connections, CSRF, model validation


2010

1.3 23 Mar 2011 Timezones, in browser testing, app templates.

1.5 26 Feb 2013 Python 3 Support, configurable user model

1.6 6 Nov 2013 Dedicated to Malcolm Tredinnick, db transaction management, connection


pooling.

1.7 2 Sep 2014 Migrations, application loading and configuration.

1.8 LTS 2 Sep 2014 Migrations, application loading and configuration.

1.8 LTS 1 Apr 2015 Native support for multiple template engines.Supported until at least April 2018

1.9 1 Dec 2015 Automatic password validation. New styling for admin interface.

1.10 1 Aug 2016 Full text search for PostgreSQL. New-style middleware.
1.11 LTS 1.11 LTS Last version to support Python 2.7.Supported until at least April 2020

2.0 Dec 2017 First Python 3-only release, Simplified URL routing syntax, Mobile friendly
admin.

Popularity
Django is widely accepted and used by various well-known sites such as:

o Instagram
o Mozilla
o Disqus
o Pinterest
o Bitbucket
o The Washington Times

Features of Django
o Rapid Development
o Secure
o Scalable
o Fully loaded
o Versatile
o Open Source
o Vast and Supported Community

Rapid Development
Django was designed with the intention to make a framework which takes less time to
build web application. The project implementation phase is a very time taken but
Django creates it rapidly.

Secure
Django takes security seriously and helps developers to avoid many common security
mistakes, such as SQL injection, cross-site scripting, cross-site request forgery etc. Its
user authentication system provides a secure way to manage user accounts and
passwords.

Scalable
Django is scalable in nature and has ability to quickly and flexibly switch from small to
large scale application project.

Fully loaded
Django includes various helping task modules and libraries which can be used to
handle common Web development tasks. Django takes care of user authentication,
content administration, site maps, RSS feeds etc.

Versatile
Django is versatile in nature which allows it to build applications for different-different
domains. Now a days, Companies are using Django to build various types of
applications like: content management systems, social networks sites or scientific
computing platforms etc.

Open Source
Django is an open source web application framework. It is publicly available without
cost. It can be downloaded with source code from the public repository. Open source
reduces the total cost of the application development.

Vast and Supported Community


Django is an one of the most popular web framework. It has widely supportive
community and channels to share and connect.

Django Installation
To install Django, first visit to django official site
(https://www.djangoproject.com) and download django by clicking on the
download section. Here, we will see various options to download The Django.

Django requires pip to start installation. Pip is a package manager system which is
used to install and manage packages written in python. For Python 3.4 and higher
versions pip3 is used to manage packages.

In this tutorial, we are installing Django in Ubuntu operating system.


The complete installation process is described below. Before installing make sure pip
is installed in local system.

Hello Java Program for Beginners

Here, we are installing Django using pip3, the installation command is given below.

1. $ pip3 install django==2.0.3

Verify Django Installation


After installing Django, we need to varify the installation. Open terminal and
write python3 and press enter. It will display python shell where we can verify django
installation.

Look at the Django version displayed by the print method of the python. Well, Django
is installed successfuly. Now, we can build Django web applications.

Django Project
In the previous topic, we have installed Django successfully. Now, we will learn step by
step process to create a Django application.

To create a Django project, we can use the following command. projectname is the
name of Django application.

1. $ django-admin startproject projectname

Django Project Example


Here, we are creating a project djangpapp in the current directory.

1. $ django-admin startproject djangpapp

Locate into the Project


Now, move to the project by changing the directory. The Directory can be changed by
using the following command.

10 Sec

Features of Java - Javatpoint

1. cd djangpapp
To see all the files and subfolders of django project, we can use tree command to view
the tree structure of the application. This is a utility command, if it is not present, can
be downloaded via apt-get install tree command.

A Django project contains the following packages and files. The outer directory is just
a container for the application. We can rename it further.

o manage.py: It is a command-line utility which allows us to interact with the project in


various ways and also used to manage an application that we will see later on in this
tutorial.
o A directory (djangpapp) located inside, is the actual application package name. Its
name is the Python package name which we'll need to use to import module inside the
application.
o __init__.py: It is an empty file that tells to the Python that this directory should be
considered as a Python package.
o settings.py: This file is used to configure application settings such as database
connection, static files linking etc.
o urls.py: This file contains the listed URLs of the application. In this file, we can mention
the URLs and corresponding actions to perform the task and display the view.
o wsgi.py: It is an entry-point for WSGI-compatible web servers to serve Django project.

Initially, this project is a default draft which contains all the required files and folders.

Running the Django Project


Django project has a built-in development server which is used to run application
instantly without any external web server. It means we don't need of Apache or another
web server to run the application in development mode.
To run the application, we can use the following command.

1. $ python3 manage.py runserver

Look server has started and can be accessed at localhost with port 8000. Let's access
it using the browser, it looks like the below.

The application is running successfully. Now, we can customize it according to our


requirement and can develop a customized web application.
Django Configuration with Apache Web Server
Django uses its built-in development server to run the web application. To start this
server, we can use python manage.py runserver command.

This command starts the server which runs on port 8000 and can be accessed at
browser by entering localhost:8000. It shows a welcome page of the application.

And at browser, it can be accessed as below.


But if we want to run our application by using apache server rather than built-in
development server, we need to configure apache2.conf file located at
/etc/apache directory. Add the following code into this file.

C++ vs Java

// apache2.conf

1. WSGIScriptAlias / /var/www/html/django7/django7/wsgi.py
2. WSGIPythonPath /var/www/html/django7/
3.
4. <Directory /var/www/html/django7>
5. <Files wsgi.py>
6. Require all granted
7. </Files>
8. </Directory>

After adding these lines, restart apache server by using the service apache2
restart command and then type localhost to the browser's address bar. This time,
project will run on apache server rather than a built-in server. See, it shows the home
page of the application.

Django Virtual Environment Setup


The virtual environment is an environment which is used by Django to execute an
application. It is recommended to create and execute a Django application in a
separate environment. Python provides a tool virtualenv to create an isolated Python
environment. We will use this tool to create a virtual environment for our Django
application.

To set up a virtual environment, use the following steps.

1. Install Package

First, install python3-venv package by using the following command.

1. $ apt-get install python3-venv

2. Create a Directory

1. $ mkdir djangoenv

After it, change directory to the newly created directory by using the cd djangoenv.

3. Create Virtual Environment

1. $ python3 -m venv djangoenv

4. Activate Virtual Environment

After creating a virtual environment, activate it by using the following command.


1. $ source djangoenv/bin/activate

Till here, the virtual environment has started. Now, we can use it to create Django
application.

Install Django
Install Django in the virtual environment. To install Django, use the following
command.

1. $ pip install django

Django has installed successfully. Now we can create a new project and build new
applications in the separate environment.

Django Admin Interface


Django provides a built-in admin module which can be used to perform CRUD
operations on the models. It reads metadata from the model to provide a quick
interface where the user can manage the content of the application.

This is a built-in module and designed to perform admin related tasks to the user.

Let's see how to activate and use Django's admin module (interface).
The admin app (django.contrib.admin) is enabled by default and already added into
INSTALLED_APPS section of the settings file.

To access it at browser use '/admin/' at a local machine like localhost:8000/admin/


and it shows the following output:

It prompts for login credentials if no password is created yet, use the following
command to create a user.

Create an Admin User


1. $ python3 managen.py createsuperuser

Now start development server and access admin login.

1. $ python3 manage.py runserver


Provide created username and password and login.

After login successfully, it shows the following interface.

It is a Django Admin Dashboard. Here, we can add and update the registered models.
The model registration process will be discussed in further chapters.

Django App
In the previous topics, we have seen a procedure to create a Django project. Now, in
this topic, we will create app inside the created project.

Django application consists of project and app, it also generates an automatic base
directory for the app, so we can focus on writing code (business logic) rather than
creating app directories.

The difference between a project and app is, a project is a collection of configuration
files and apps whereas the app is a web application which is written to perform
business logic.

Creating an App
To create an app, we can use the following command.

1. $ python3 manage.py startapp appname

Django App Example

1. $ python3 manage.py startapp myapp

See the directory structure of the created app, it contains the migrations folder to
store migration files and model to write business logic.

Initially, all the files are empty, no code is available but we can use these to implement
business logic on the basis of the MVC design pattern.

To run this application, we need to make some significant changes which display hello
world message on the browser.

Open views.py file in any text editor and write the given code to it and do the same
for urls.py file too.

// views.py
1. from django.shortcuts import render
2.
3. # Create your views here.
4. from django.http import HttpResponse
5.
6. def hello(request):
7. return HttpResponse("<h2>Hello, Welcome to Django!</h2>")

// urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4.
5. urlpatterns = [
6. path('admin/', admin.site.urls),
7. path('hello/', views.hello),
8. ]

We have made changes in two files of the application. Now, let's run the it by using
the following command. This command will start the server at port 8000.

Run the Application

1. $ python3 manage.py runserver

Open any web browser and enter the URL localhost:8000/hello. It will show the
output given below.
Django MVT
The MVT (Model View Template) is a software design pattern. It is a collection of three
important components Model View and Template. The Model helps to handle
database. It is a data access layer which handles the data.

The Template is a presentation layer which handles User Interface part completely. The
View is used to execute the business logic and interact with a model to carry data and
renders a template.

Although Django follows MVC pattern but maintains it?s own conventions. So, control
is handled by the framework itself.

There is no separate controller and complete application is based on Model View and
Template. That?s why it is called MVT application.

C++ vs Java

See the following graph that shows the MVT based control flow.
Here, a user requests for a resource to the Django, Django works as a controller and
check to the available resource in URL.

If URL maps, a view is called that interact with model and template, it renders a
template.

Django responds back to the user and sends a template as a response.

Django Model
In Django, a model is a class which is used to contain essential fields and methods.
Each model class maps to a single table in the database.

Django Model is a subclass of django.db.models.Model and each field of the model


class represents a database field (column).

Django provides us a database-abstraction API which allows us to create, retrieve,


update and delete a record from the mapped table.

Model is defined in Models.py file. This file can contain multiple models.

Let's see an example here, we are creating a model Employee which has two
fields first_name and last_name.

1. from django.db import models


2.
3. class Employee(models.Model):
4. first_name = models.CharField(max_length=30)
5. last_name = models.CharField(max_length=30)
The first_name and last_name fields are specified as class attributes and each
attribute maps to a database column.

This model will create a table into the database that looks like below.

1. CREATE TABLE appname_employee (


2. "id" INT NOT NULL PRIMARY KEY,
3. "first_name" varchar(30) NOT NULL,
4. "last_name" varchar(30) NOT NULL
5. );

The created table contains an auto-created id field. The name of the table is a
combination of app name and model name that can be changed further.

Register / Use Model


After creating a model, register model into the INSTALLED_APPS inside settings.py.

For example,

1. INSTALLED_APPS = [
2. #...
3. 'appname',
4. #...
5. ]

Django Model Fields


The fields defined inside the Model class are the columns name of the mapped table.
The fields name should not be python reserve words like clean, save or delete etc.

Django provides various built-in fields types.

Field Name Class Particular

AutoField class AutoField(**options) It An IntegerField that automatically


increments.

BigAutoField class BigAutoField(**options) It is a 64-bit integer, much like an


AutoField except that it is guaranteed to
fit numbers from 1 to
9223372036854775807.

BigIntegerField class BigIntegerField(**options) It is a 64-bit integer, much like an


IntegerField except that it is guaranteed
to fit numbers from -
9223372036854775808 to
9223372036854775807.

BinaryField class BinaryField(**options) A field to store raw binary data.

BooleanField class BooleanField(**options) A true/false field. The default form


widget for this field is a CheckboxInput.

CharField class DateField(auto_now=False, It is a date, represented in Python by a


auto_now_add=False, **options) datetime.date instance.

DateTimeField class DateTimeField(auto_now=False, It is a date, represented in Python by a


auto_now_add=False, **options) datetime.date instance.

DateTimeField class DateTimeField(auto_now=False, It is used for date and time, represented


auto_now_add=False, **options) in Python by a datetime.datetime
instance.

DecimalField class DecimalField(max_digits=None, It is a fixed-precision decimal number,


decimal_places=None, **options) represented in Python by a Decimal
instance.

DurationField class DurationField(**options) A field for storing periods of time.

EmailField class EmailField(max_length=254, It is a CharField that checks that the


**options) value is a valid email address.

FileField class FileField(upload_to=None, It is a file-upload field.


max_length=100, **options)

FloatField class FloatField(**options) It is a floating-point number


represented in Python by a float
instance.
ImageField class ImageField(upload_to=None, It inherits all attributes and methods
height_field=None, width_field=None, from FileField, but also validates that
max_length=100, **options) the uploaded object is a valid image.

IntegerField class IntegerField(**options) It is an integer field. Values from -


2147483648 to 2147483647 are safe in
all databases supported by Django.

NullBooleanField class NullBooleanField(**options) Like a BooleanField, but allows NULL as


one of the options.

PositiveIntegerField class PositiveIntegerField(**options) Like an IntegerField, but must be either


positive or zero (0). Values from 0 to
2147483647 are safe in all databases
supported by Django.

SmallIntegerField class SmallIntegerField(**options) It is like an IntegerField, but only allows


values under a certain (database-
dependent) point.

TextField class TextField(**options) A large text field. The default form


widget for this field is a Textarea.

TimeField class TimeField(auto_now=False, A time, represented in Python by a


auto_now_add=False, **options) datetime.time instance.

Django Model Fields Example

1. first_name = models.CharField(max_length=50) # for creating varchar column

2. release_date = models.DateField() # for creating date column


3. num_stars = models.IntegerField() # for creating integer column

Field Options
Each field requires some arguments that are used to set column attributes. For
example, CharField requires mac_length to specify varchar database.

Common arguments available to all field types. All are optional.


Field Particulars
Options

Null Django will store empty values as NULL in the database.

Blank It is used to allowed field to be blank.

Choices An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.

Default The default value for the field. This can be a value or a callable object.

help_text Extra "help" text to be displayed with the form widget. It's useful for documentation even
if your field isn't used on a form.

primary_key This field is the primary key for the model.

Unique This field must be unique throughout the table.

Django Model Example


We created a model Student that contains the following code in models.py file.

//models.py

1. class Student(models.Model):
2. first_name = models.CharField(max_length=20)
3. last_name = models.CharField(max_length=30)
4. contact = models.IntegerField()
5. email = models.EmailField(max_length=50)
6. age = models.IntegerField()

After that apply migration by using the following command.

1. python3 manage.py makemigrations myapp

It will create a table myapp_student. The table structure looks like the below.
Django Views
A view is a place where we put our business logic of the application. The view is a
python function which is used to perform some business logic and return a response
to the user. This response can be the HTML contents of a Web page, or a redirect, or
a 404 error.

All the view function are created inside the views.py file of the Django app.

Django View Simple Example


//views.py

1. import datetime
2. # Create your views here.
3. from django.http import HttpResponse
4. def index(request):
5. now = datetime.datetime.now()
6. html = "<html><body><h3>Now time is %s.</h3></body></html>" % now
7. return HttpResponse(html) # rendering the template in HttpResponse

Let's step through the code.

OOPs Concepts in Java


First, we will import DateTime library that provides a method to get current date and
time and HttpResponse class.

Next, we define a view function index that takes HTTP request and respond back.

View calls when gets mapped with URL in urls.py. For example

1. path('index/', views.index),

Output:

Returning Errors
Django provides various built-in error classes that are the subclass
of HttpResponse and use to show error message as HTTP response. Some classes are
listed below.

Class Description

class It is used to designate that a page hasn't been modified since the user's
HttpResponseNotModified last request (status code 304).

class It acts just like HttpResponse but uses a 400 status code.
HttpResponseBadRequest

class HttpResponseNotFound It acts just like HttpResponse but uses a 404 status code.

class It acts just like HttpResponse but uses a 410 status code.
HttpResponseNotAllowed
HttpResponseServerError It acts just like HttpResponse but uses a 500 status code.

Django View Example


// views.py

1. from django.shortcuts import render


2. # Create your views here.
3. from django.http import HttpResponse, HttpResponseNotFound
4. def index(request):
5. a=1
6. if a:
7. return HttpResponseNotFound('<h1>Page not found</h1>')
8. else:
9. return HttpResponse('<h1>Page was found</h1>') # rendering the tem
plate in HttpResponse

Output:

Django View HTTP Decorators


HTTP Decorators are used to restrict access to view based on the request method.

These decorators are listed in django.views.decorators.http and return a


django.http.HttpResponseNotAllowed if the conditions are not met.

Syntax

require_http_methods(request_method_list)
Django Http Decorator Example
//views.py

1. from django.shortcuts import render


2. # Create your views here.
3. from django.http import HttpResponse, HttpResponseNotFound
4. from django.views.decorators.http import require_http_methods
5. @require_http_methods(["GET"])
6. def show(request):
7. return HttpResponse('<h1>This is Http GET request.</h1>')

This method will execute only if the request is an HTTP GET request.

//urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. path('show/', views.show),
8. ]

Output:

Django Templates
Django provides a convenient way to generate dynamic HTML pages by using its
template system.

A template consists of static parts of the desired HTML output as well as some special
syntax describing how dynamic content will be inserted.

Why Django Template?


In HTML file, we can't write python code because the code is only interpreted by
python interpreter not the browser. We know that HTML is a static markup language,
while Python is a dynamic programming language.

Django template engine is used to separate the design from the python code and
allows us to build dynamic web pages.

Django Template Configuration


To configure the template system, we have to provide some entries in settings.py file.

1. TEMPLATES = [
2. {
3. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
4. 'DIRS': [os.path.join(BASE_DIR,'templates')],
5. 'APP_DIRS': True,
6. 'OPTIONS': {
7. 'context_processors': [
8. 'django.template.context_processors.debug',
9. 'django.template.context_processors.request',
10. 'django.contrib.auth.context_processors.auth',
11. 'django.contrib.messages.context_processors.messages',
12. ],
13. },
14. },
15. ]

Here, we mentioned that our template directory name is templates. By default,


DjangoTemplates looks for a templates subdirectory in each of the INSTALLED_APPS.

Django Template Simple Example


First, create a directory templates inside the project app as we did below.
After that create a template index.html inside the created folder.

Our template index.html contains the following code.

// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. </head>
7. <body>
8. <h2>Welcome to Django!!!</h2>
9. </body>
10. </html>

Loading Template
To load the template, call get_template() method as we did below and pass template
name.

//views.py

1. from django.shortcuts import render


2. #importing loading from django template
3. from django.template import loader
4. # Create your views here.
5. from django.http import HttpResponse
6. def index(request):
7. template = loader.get_template('index.html') # getting our template
8. return HttpResponse(template.render()) # rendering the template in HttpRespo
nse

Set a URL to access the template from the browser.

//urls.py

1. path('index/', views.index),

Register app inside the INSTALLED_APPS

1. INSTALLED_APPS = [
2. 'django.contrib.admin',
3. 'django.contrib.auth',
4. 'django.contrib.contenttypes',
5. 'django.contrib.sessions',
6. 'django.contrib.messages',
7. 'django.contrib.staticfiles',
8. 'myapp'
9. ]
Run Server
Execute the following command and access the template by
entering localhost:8000/index at the browser.

1. $ python3 manage.py runserver

Django Template Language


Django template uses its own syntax to deal with variable, tags, expressions etc. A
template is rendered with a context which is used to get value at a web page. See the
examples.

Variables
Variables associated with a context can be accessed by {{}} (double curly braces). For
example, a variable name value is rahul. Then the following statement will replace
name with its value.

1. My name is {{name}}.
2. My name is rahul

Django Variable Example


//views.py

1. from django.shortcuts import render


2. #importing loading from django template
3. from django.template import loader
4. # Create your views here.
5. from django.http import HttpResponse
6. def index(request):
7. template = loader.get_template('index.html') # getting our template
8. name = {
9. 'student':'rahul'
10. }
11. return HttpResponse(template.render(name)) # rendering the template
in HttpResponse

//index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. </head>
7. <body>
8. <h2>Welcome to Django!!!</h2>
9. <h3>My Name is: {{ student }}</h3>
10. </body>
11. </html>

Output:
Tags
In a template, Tags provide arbitrary logic in the rendering process. For example, a tag
can output content, serve as a control structure e.g. an "if" statement or a "for" loop,
grab content from a database etc.

Tags are surrounded by {% %} braces. For example.

1. {% csrf_token %}
2.
3. {% if user.is_authenticated %}
4. Hello, {{ user.username }}.
5. {% endif %}

Django URL Mapping


Well, till here, we have learned to create a model, view, and template. Now, we will
learn about the routing of application.

Since Django is a web application framework, it gets user requests by URL locater and
responds back. To handle URL, django.urls module is used by the framework.

Let's open the file urls.py of the project and see the what it looks like:

// urls.py

1. from django.contrib import admin


2. from django.urls import path
3.
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. ]

See, Django already has mentioned a URL here for the admin. The path function takes
the first argument as a route of string or regex type.

The view argument is a view function which is used to return a response (template) to
the user.

The django.urls module contains various


functions, path(route,view,kwargs,name) is one of those which is used to map the
URL and call the specified view.

Django URL Functions


Here, we are giving some commonly used functions for URL handling and mapping.

Name Description Example

path(route, view, kwargs=None, It returns an element for inclusion in path('index/', views.index,


name=None) urlpatterns. name='main-view')

re_path(route, view, It returns an element for inclusion in re_path(r'^index/$',


kwargs=None, name=None) urlpatterns. views.index,
name='index'),

include(module, It is a function that takes a full Python


namespace=None) import path to another URLconf module
that should be "included" in this place.

register_converter(converter, It is used for registering a converter for


type_name) use in path() routes.

Let's see an example that gets user request and map that route to call specified view
function. Have a look at the steps.

1. Create a function hello in the views.py file. This function will be mapped from the
url.py file.

// views.py
1. from django.shortcuts import render
2. # Create your views here.
3. from django.http import HttpResponse, HttpResponseNotFound
4. from django.views.decorators.http import require_http_methods
5. @require_http_methods(["GET"])
6. def hello(request):
7. return HttpResponse('<h1>This is Http GET request.</h1>')

// urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. path('hello/', views.hello),
8. ]

Now, start the server and enter localhost:8000/hello to the browser. This URL will be
mapped into the list of URLs and then call the corresponding function from the views
file.

In this example, hello will be mapped and call hello function from the views file. It is
called URL mapping.

Django Static Files Handling


In a web application, apart from business logic and data handling, we also need to
handle and manage static resources like CSS, JavaScript, images etc.

It is important to manage these resources so that it does not affect our application
performance.

Django deals with it very efficiently and provides a convenient manner to use
resources.

The django.contrib.staticfiles module helps to manage them.


Django Static (CSS, JavaScript, images)
Configuration
1. Include the django.contrib.staticfiles in INSTALLED_APPS.

1. INSTALLED_APPS = [
2. 'django.contrib.admin',
3. 'django.contrib.auth',
4. 'django.contrib.contenttypes',
5. 'django.contrib.sessions',
6. 'django.contrib.messages',
7. 'django.contrib.staticfiles',
8. 'myapp'
9. ]

2. Define STATIC_URL in settings.py file as given below.

1. STATIC_URL = '/static/'

3. Load static files in the templates by using the below expression.

1. {% load static %}

4. Store all images, JavaScript, CSS files in a static folder of the application. First create
a directory static, store the files inside it.

Our project structure looks like this.


Django Image Loading Example
To load an image in a template file, use the code given below.

// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. {% load static %}
7. </head>
8. <body>
9. <img src="{% static '/wallpaper.jpeg' %}" alt="My image" height="300px" wid
th="700px"/>
10. </body>
11. </html>

//urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. ]

//views.py

1. def index(request):
2. return render(request,'index.html')

Run the server by using python manage.py runserver command.

After that access the template by localhost:8000/index URL, and it will produce the
following output to the browser.

Django Loading JavaScript


To load JavaScript file, just add the following line of code in index.html file.

1. {% load static %}
2. <script src="{% static '/js/script.js' %}"

// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. {% load static %}
7. <script src="{% static '/js/script.js' %}" type="text/javascript"></script>
8. </head>
9. <body>
10. </body>
11. </html>

// script.js

1. alert("Hello, Welcome to Javatpoint");

Now, our project structure looks like this:

Run the server by using python manage.py runserver command.

After that access the template by localhost:8000/index URL, and it will produce the
following output to the browser.
Django Loading CSS Example
To, load CSS file, use the following code in index.html file.

1. {% load static %}
2. <link href="{% static 'css/style.css' %}" rel="stylesheet">

After that create a directory CSS and file style.css which contains the following code.

// style.css

1. h1{
2. color:red;
3. }

Our project structure looks like this:


// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. {% load static %}
7. <link href="{% static 'css/style.css' %}" rel="stylesheet">
8. </head>
9. <body>
10. <h1>Welcome to Javatpoint</h1>
11. </body>
12. </html>

Run the server by using python manage.py runserver command.

After that access the template by entering localhost:8000/index URL, and it will
produce the following output to the browser.
Well, in this topic, we have learned the process of managing static files efficiently.

Django Model Form


It is a class which is used to create an HTML form by using the Model. It is an efficient
way to create a form without writing HTML code.

Django automatically does it for us to reduce the application development time. For
example, suppose we have a model containing various fields, we don't need to repeat
the fields in the form file.

For this reason, Django provides a helper class which allows us to create a Form class
from a Django model.

Let's see an example.

Django ModelForm Example


First, create a model that contains fields name and other metadata. It can be used to
create a table in database and dynamic HTML form.

// model.py

1. from __future__ import unicode_literals


2. from django.db import models
3.
4. class Student(models.Model):
5. first_name = models.CharField(max_length=20)
6. last_name = models.CharField(max_length=30)
7. class Meta:
8. db_table = "student"

This file contains a class that inherits ModelForm and mention the model name for
which HTML form is created.

// form.py

1. from django import forms


2. from myapp.models import Student
3.
4. class EmpForm(forms.ModelForm):
5. class Meta:
6. model = Student
7. fields = "__all__"

Write a view function to load the ModelForm from forms.py.

//views.py

1. from django.shortcuts import render


2. from myapp.form import StuForm
3.
4. def index(request):
5. stu = EmpForm()
6. return render(request,"index.html",{'form':stu})

//urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. ]

And finally, create a index.html file that contains the following code.
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. </head>
7. <body>
8. <form method="POST" class="post-form">
9. {% csrf_token %}
10. {{ form.as_p }}
11. <button type="submit" class="save btn btn-default">Save</button>
12. </form>
13. </body>
14. </html>

Run Server
Run the server by using python manage.py runserver command.

After that access the template by localhost:8000/index URL, and it will produce the
following output to the browser.

Output:

Well, an HTML form is created automatically. This is a feature of Django.

Django Forms
Django provides a Form class which is used to create HTML forms. It describes a form
and how it works and appears.

It is similar to the ModelForm class that creates a form by using the Model, but it does
not require the Model.

Each field of the form class map to the HTML form <input> element and each one is
a class itself, it manages form data and performs validation while submitting the form.

Lets see an example, in which we are creating some fields too.

1. from django import forms


2. class StudentForm(forms.Form):
3. firstname = forms.CharField(label="Enter first name",max_length=50)
4. lastname = forms.CharField(label="Enter last name", max_length = 100)

A StudentForm is created that contains two fields of CharField type. Charfield is a class
and used to create an HTML text input component in the form.

The label is used to set HTML label of the component and max_length sets length of
an input value.

When rendered, it produces the following HTML to the browser.

1. <label for="id_firstname">Enter first name:</label>


2. <input type="text" name="firstname" required maxlength="50" id="id_firstname" />

3. <label for="id_lastname">Enter last name:</label> <input type="text" name


="lastname" required maxlength="100" id="id_lastname" />
Note: Django Form does not include <form> tags, or a submit button. We'll have to
provide those ourselves in the template.

Commonly used fields and their details are given in the below table.

Name Class HTML Input Empty value

BooleanField class BooleanField(**kwargs) CheckboxInput False

CharField class CharField(**kwargs) TextInput Whatever you've given as


empty_value.
ChoiceField class ChoiceField(**kwargs) Select '' (an empty string)

DateField class DateField(**kwargs) DateInput None

DateTimeField class DateTimeInput None


DateTimeField(**kwargs)

DecimalField class DecimalField(**kwargs) NumberInput None

EmailField class EmailField(**kwargs) EmailInput '' (an empty string)

FileField class FileField(**kwargs) ClearableFileInput None

ImageField class ImageField(**kwargs) ClearableFileInput None

Let's see a complete example to create an HTML form with the help of Django Form
class.

Building a Form in Django


Suppose we want to create a form to get Student information, use the following code.

1. from django import forms


2. class StudentForm(forms.Form):
3. firstname = forms.CharField(label="Enter first name",max_length=50)
4. lastname = forms.CharField(label="Enter last name", max_length = 100)

Put this code into the forms.py file.

Instantiating Form in Django


Now, we need to instantiate the form in views.py file. See, the below code.

// views.py

1. from django.shortcuts import render


2. from myapp.form import StudentForm
3.
4. def index(request):
5. student = StudentForm()
6. return render(request,"index.html",{'form':student})

Passing the context of form into index template that looks like this:

// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. </head>
7. <body>
8. <form method="POST" class="post-form">
9. {% csrf_token %}
10. {{ form.as_p }}
11. <button type="submit" class="save btn btn-default">Save</button>
12. </form>
13. </body>
14. </html>

Provide the URL in urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. ]

Run Server and access the form at browser by localhost:8000/index, and it will
produce the following output.
There are other output options though for the <label>/<input> pairs:

o {{ form.as_table }} will render them as table cells wrapped in <tr> tags


o {{ form.as_p }} will render them wrapped in <p> tags
o {{ form.as_ul }} will render them wrapped in <li> tags

Note: that we'll have to provide the surrounding <table> or <ul> elements yourself.

Django Form Validation


Django provides built-in methods to validate form data automatically. Django forms
submit only if it contains CSRF tokens. It uses uses a clean and easy approach to
validate data.

The is_valid() method is used to perform validation for each field of the form, it is
defined in Django Form class. It returns True if data is valid and place all data into a
cleaned_data attribute.

Let's see an example that takes user input and validate input as well.

Django Validation Example


This example contains the following files and code.

// models.py

1. from django.db import models


2. class Employee(models.Model):
3. eid = models.CharField(max_length=20)
4. ename = models.CharField(max_length=100)
5. econtact = models.CharField(max_length=15)
6. class Meta:
7. db_table = "employee"

Now, create a form which contains the below code.

// forms.py

1. from django import forms


2. from myapp.models import Employee
3.
4. class EmployeeForm(forms.ModelForm):
5. class Meta:
6. model = Employee
7. fields = "__all__"

Instantiate the form


Instantiate the form, check whether request is post or not. It validate the data by
using is_valid() method.

//views.py

1. def emp(request):
2. if request.method == "POST":
3. form = EmployeeForm(request.POST)
4. if form.is_valid():
5. try:
6. return redirect('/')
7. except:
8. pass
9. else:
10. form = EmployeeForm()
11. return render(request,'index.html',{'form':form})

Index template that shows form and errors.

// index.html

1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <title>Index</title>
6. </head>
7. <body>
8. <form method="POST" class="post-form" enctype="multipart/form-data">
9. {% csrf_token %}
10. {{ form.as_p }}
11. <button type="submit" class="save btn btn-default">Save</button>
12. </form>
13. </body>
14. </html>

Start server and access the form.

It validates each field and throws errors if any validation fails.


Django File Upload
File upload to the server using Django is a very easy task. Django provides built-in
library and methods that help to upload a file to the server.

The forms.FileField() method is used to create a file input and submit the file to the
server. While working with files, make sure the HTML form tag
contains enctype="multipart/form-data" property.

Let's see an example of uploading a file to the server. This example contains the
following files.

Template (index.html)

Triggers in SQL (Hindi)

It will create an HTML form which contains a file input component.

1. <body>
2. <form method="POST" class="post-form" enctype="multipart/form-data">
3. {% csrf_token %}
4. {{ form.as_p }}
5. <button type="submit" class="save btn btn-default">Save</button>
6. </form>
7. </body>

Form (forms.py)

1. from django import forms


2. class StudentForm(forms.Form):
3. firstname = forms.CharField(label="Enter first name",max_length=50)
4. lastname = forms.CharField(label="Enter last name", max_length = 10)
5. email = forms.EmailField(label="Enter Email")
6. file = forms.FileField() # for creating file input

View (views.py)

Here, one extra parameter request.FILES is required in the constructor. This argument
contains the uploaded file instance.

1. from django.shortcuts import render


2. from django.http import HttpResponse
3. from myapp.functions.functions import handle_uploaded_file
4. from myapp.form import StudentForm
5. def index(request):
6. if request.method == 'POST':
7. student = StudentForm(request.POST, request.FILES)
8. if student.is_valid():
9. handle_uploaded_file(request.FILES['file'])
10. return HttpResponse("File uploaded successfuly")
11. else:
12. student = StudentForm()
13. return render(request,"index.html",{'form':student})

Specify URL (urls.py)

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. ]

Upload Script (functions.py)

This function is used to read the uploaded file and store at provided location. Put this
code into the functions.py file. But first create this file into the project.

1. def handle_uploaded_file(f):
2. with open('myapp/static/upload/'+f.name, 'wb+') as destination:
3. for chunk in f.chunks():
4. destination.write(chunk)

Now, create a directory upload to store the uploaded file. Our project structure looks
like below.

Initially, this directory is empty. so, let's upload a file to it and later on it will contain
the uploaded file.

Start Server

1. python manage.py runserver

Output
Submit this form and see the upload folder. Now, it contains the uploaded file.
Django Database
Connectivity
The settings.py file contains all the project settings along with database connection
details. By default, Django works with SQLite, database and allows configuring for
other databases as well.

Database connectivity requires all the connection details such as database name, user
credentials, hostname drive name etc.

To connect with MySQL, django.db.backends.mysql driver is used to establishing a


connection between application and database. Let's see an example.

We need to provide all connection details in the settings file. The settings.py file of our
project contains the following code for the database.

1. DATABASES = {
2. 'default': {
3. 'ENGINE': 'django.db.backends.mysql',
4. 'NAME': 'djangoApp',
5. 'USER':'root',
6. 'PASSWORD':'mysql',
7. 'HOST':'localhost',
8. 'PORT':'3306'
9. }
10. }

After providing details, check the connection using the migrate command.

1. $ python3 manage.py migrate

This command will create tables for admin, auth, contenttypes, and sessions. See the
example.

Now, access to the MySQL database and see the database from the list of databases.
The created database contains the following tables.
Note: It throws an error if database connectivity fails:
django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost'
(using password: YES)")

Migrating Model
Well, till here, we have learned to connect Django application to the MySQL database.
Next, we will see how to create a table using the model.

Each Django's model is mapped to a table in the database. So after creating a model,
we need to migrate it. Let's see an example.

Suppose, we have a model class Employee in the models.py file that contains the
following code.

// models.py

1. from django.db import models


2. class Employee(models.Model):
3. eid = models.CharField(max_length=20)
4. ename = models.CharField(max_length=100)
5. econtact = models.CharField(max_length=15)
6. class Meta:
7. db_table = "employee"
Django first creates a migration file that contains the details of table structure. To
create migration use the following command.

1. $ python3 manage.py makemigrations

The created migration file is located into migrations folder and contains the following
code.

1. from django.db import migrations, models


2. class Migration(migrations.Migration):
3. initial = True
4. dependencies = [
5. ]
6. operations = [
7. migrations.CreateModel(
8. name='Employee',
9. fields=[
10. ('id', models.AutoField(auto_created=True, primary_key=True, serialize=Fals
e, verbose_name='ID')),
11. ('eid', models.CharField(max_length=20)),
12. ('ename', models.CharField(max_length=100)),
13. ('econtact', models.CharField(max_length=15)),
14. ],
15. options={
16. 'db_table': 'employee',
17. },
18. ),
19. ]

Now, migrate to reflect the changes into the database.


1. $ python3 manage.py migrate

Check the database again, now it contains the employee table.

See, a table is present in the database. Well, we have successfully established a


connection between our Django application and MySQL database.

Django Database Migrations


Migration is a way of applying changes that we have made to a model, into the
database schema. Django creates a migration file inside the migration folder for each
model to create the table schema, and each table is mapped to the model of which
migration is created.

Django provides the various commands that are used to perform migration related
tasks. After creating a model, we can use these commands.

o makemigrations : It is used to create a migration file that contains code for the tabled
schema of a model.
o migrate : It creates table according to the schema defined in the migration file.
o sqlmigrate : It is used to show a raw SQL query of the applied migration.
o showmigrations : It lists out all the migrations and their status.

Suppose, we have a model as given below and contains the following attributes.

Model
//models.py

1. from django.db import models


2. class Employee(models.Model):
3. eid = models.CharField(max_length=20)
4. ename = models.CharField(max_length=100)
5. econtact = models.CharField(max_length=15)
6. class Meta:
7. db_table = "employee"

To create a migration for this model, use the following command. It will create a
migration file inside the migration folder.

1. $ python3 manage.py makemigrations

This migration file contains the code in which a Migration class is created that contains
the name and fields of employee table.

Migrations
// 0001_initial.py

1. from django.db import migrations, models


2. class Migration(migrations.Migration):
3. initial = True
4. dependencies = [
5. ]
6. operations = [
7. migrations.CreateModel(
8. name='Employee',
9. fields=[
10. ('id', models.AutoField(auto_created=True, primary_key=True, serialize=Fals
e, verbose_name='ID')),
11. ('eid', models.CharField(max_length=20)),
12. ('ename', models.CharField(max_length=100)),
13. ('econtact', models.CharField(max_length=15)),
14. ],
15. options={
16. 'db_table': 'employee',
17. },
18. ),
19. ]

After creating a migration, migrate it so that it reflects the database permanently. The
migrate command is given below.

1. $ python3 manage.py migrate

Apart from creating a migration, we can see raw SQL query executing behind the
applied migration. The sqlmigrate app-name migration-name is used to get raw
SQL query. See an example.

1. $ python3 manage.py migrate


And showmigrations command is used to show applied migrations. See the example.

If no app-name is provided, it shows all migrations applied to the project.

1. $ python3 manage.py showmigrations

We can get app-specific migrations by specifying app-name, see the example.

1. $ python3 manage.py showmigrations myapp


Django Middleware
In Django, middleware is a lightweight plugin that processes during request and
response execution. Middleware is used to perform a function in the application. The
functions can be a security, session, csrf protection, authentication etc.

Django provides various built-in middleware and also allows us to write our own
middleware. See, settings.py file of Django project that contains various middleware,
that is used to provides functionalities to the application. For example, Security
Middleware is used to maintain the security of the application.

// settings.py

1. MIDDLEWARE = [
2. 'django.middleware.security.SecurityMiddleware',
3. 'django.contrib.sessions.middleware.SessionMiddleware',
4. 'django.middleware.common.CommonMiddleware',
5. 'django.middleware.csrf.CsrfViewMiddleware',
6. 'django.contrib.auth.middleware.AuthenticationMiddleware',
7. 'django.contrib.messages.middleware.MessageMiddleware',
8. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
9. ]

Creating Own Middleware


middleware is a class that takes an argument get_response and returns a response.

1. class FirstMiddleware:
2. def __init__(self, get_response):
3. self.get_response = get_response
4.
5. def __call__(self, request):
6. response = self.get_response(request)
7. return response

__init__(get_response)

It must accept the get_response argument because Django initializes middleware with
only it. It calls only once whereas __call__ executes for each request.
Activating Middleware
To activate middleware, add it to the MIDDLEWARE list of the settings.py file.

1. MIDDLEWARE = [
2. 'django.middleware.security.SecurityMiddleware',
3. 'django.contrib.sessions.middleware.SessionMiddleware',
4. 'django.middleware.common.CommonMiddleware',
5. 'django.middleware.csrf.CsrfViewMiddleware',
6. 'django.contrib.auth.middleware.AuthenticationMiddleware',
7. 'django.contrib.messages.middleware.MessageMiddleware',
8. 'django.middleware.clickjacking.XframeOptionsMiddleware',
9. 'add new created middleware here'
10. ]

A Django project does not require middleware, the MIDDLEWARE list can be empty
but recommended that have at least a CommonMiddleware.

Middleware Order and Layering


Middleware applies in the order it is defined in MIDDLEWARE list and each middleware
class is a layer. The MIDDLEWARE list is like an onion so each request passes through
from top to bottom and response is in reverse order (bottom to up).

Other Middleware Methods


Apart from request and response, we can add three more methods to add more
features to our middleware.

process_view(request, view_func, view_args, view_kwargs )

It takes HttpRequest object, function object, list of arguments passed to the view or a
dictionary of arguments respectively.

This method executes just before the calling of view. It returns either None or
HttpResponse, if it returns an HttpResponse, it stops processing and return the result.

process_template_response(request,response)

It takes two arguments first is a reference of HttpRequest and second is HttpResponse


object. This method is called just after the view finished execution.
It returns a response object which implements the render method.

process_exception(request, exception)

This method takes two arguments, first is HttpRequest object and second is Exception
class object that is raised by the view function.

This method returns either None or HttpResponse object. If it returns a response, the
middleware will be applied and the result returns to the browser. Otherwise, the
exception is handle by default handling system.

Django Request and Response


The client-server architecture includes two major components request and response.
The Django framework uses client-server architecture to implement web applications.

When a client requests for a resource, a HttpRequest object is created and correspond
view function is called that returns HttpResponse object.

To handle request and response, Django provides HttpRequest and HttpResponse


classes. Each class has it?s own attributes and methods.

Let's have a look at the HttpRequest class.

Django HttpRequest
This class is defined in the django.http module and used to handle the client request.
Following are the attributes of this class.

Django HttpRequest Attributes

Attribute Description

HttpRequest.scheme A string representing the scheme of the request (HTTP or HTTPs usually).

HttpRequest.body It returns the raw HTTP request body as a byte string.

HttpRequest.path It returns the full path to the requested page does not include the scheme
or domain.
HttpRequest.path_info It shows path info portion of the path.

HttpRequest.method It shows the HTTP method used in the request.

HttpRequest.encoding It shows the current encoding used to decode form submission data.

HttpRequest.content_type It shows the MIME type of the request, parsed from the CONTENT_TYPE
header.

HttpRequest.content_params It returns a dictionary of key/value parameters included in the


CONTENT_TYPE header.

HttpRequest.GET It returns a dictionary-like object containing all given HTTP GET


parameters.

HttpRequest.POST It is a dictionary-like object containing all given HTTP POST parameters.

HttpRequest.COOKIES It returns all cookies available.

HttpRequest.FILES It contains all uploaded files.

HttpRequest.META It shows all available Http headers.

HttpRequest.resolver_match It contains an instance of ResolverMatch representing the resolved URL.

And the following table contains the methods of HttpRequest class.

Django HttpRequest Methods

Attribute Description

HttpRequest.get_host() It returns the original host of the request.

HttpRequest.get_port() It returns the originating port of the request.


HttpRequest.get_full_path() It returns the path, plus an appended query string, if
applicable.

HttpRequest.build_absolute_uri (location) It returns the absolute URI form of location.

HttpRequest.get_signed_cookie (key, It returns a cookie value for a signed cookie, or raises


default=RAISE_ERROR, salt='', max_age=None) a django.core.signing.BadSignature exception if the
signature is no longer valid.

HttpRequest.is_secure() It returns True if the request is secure; that is, if it was


made with HTTPS.

HttpRequest.is_ajax() It returns True if the request was made via an


XMLHttpRequest.

Django HttpRequest Example


// views.py

1. def methodinfo(request):
2. return HttpResponse("Http request is: "+request.method)

// urls.py

1. path('info',views.methodinfo)

Start the server and get access to the browser. It shows the request method name at
the browser.

Output:
Django HttpResponse
This class is a part of django.http module. It is responsible for generating response
corresponds to the request and back to the client.

This class contains various attributes and methods that are given below.

Django HttpResponse Attributes

Attribute Description

HttpResponse.content A bytestring representing the content, encoded from a string if necessary.

HttpResponse.charset It is a string denoting the charset in which the response will be encoded.

HttpResponse.status_code It is an HTTP status code for the response.

HttpResponse.reason_phrase The HTTP reason phrase for the response.

HttpResponse.streaming It is false by default.

HttpResponse.closed It is True if the response has been closed.

Django HttpResponse Methods

Method Description

HttpResponse.__init__(content='', content_type=None, It is used to instantiate an HttpResponse object


status=200, reason=None, charset=None) with the given page content and content type.

HttpResponse.__setitem__(header, value) It is used to set the given header name to the


given value.

HttpResponse.__delitem__(header) It deletes the header with the given name.

HttpResponse.__getitem__(header) It returns the value for the given header name.


HttpResponse.has_header(header) It returns either True or False based on a case-
insensitive check for a header with the provided
name.

HttpResponse.setdefault(header, value) It is used to set default header.

HttpResponse.write(content) It is used to create response object of file-like


object.

HttpResponse.flush() It is used to flush the response object.

HttpResponse.tell() This method makes an HttpResponse instance a


file-like object.

HttpResponse.getvalue() It is used to get the value of


HttpResponse.content.

HttpResponse.readable() This method is used to create stream-like object


of HttpResponse class.

HttpResponse.seekable() It is used to make response object seekable.

We can use these methods and attributes to handle the response in the Django
application.

Django Exceptions
An exception is an abnormal event that leads to program failure. To deal with this
situation, Django uses its own exception classes and supports all core Python
exceptions as well.

Django core exceptions classes are defined in django.core.exceptions module. This


module contains the following classes.

Django Exception Classes

Exception Description
AppRegistryNotReady It is raised when attempting to use models before the app loading process.

ObjectDoesNotExist The base class for DoesNotExist exceptions.

EmptyResultSet If a query does not return any result, this exception is raised.

FieldDoesNotExist It raises when the requested field does not exist.

MultipleObjectsReturned This exception is raised by a query if only one object is expected, but multiple
objects are returned.

SuspiciousOperation This exception is raised when a user has performed an operation that should
be considered suspicious from a security perspective.

PermissionDenied It is raised when a user does not have permission to perform the action
requested.

ViewDoesNotExist It is raised by django.urls when a requested view does not exist.

MiddlewareNotUsed It is raised when a middleware is not used in the server configuration.

ImproperlyConfigured The ImproperlyConfigured exception is raised when Django is somehow


improperly configured.

FieldError It is raised when there is a problem with a model field.

ValidationError It is raised when data validation fails form or model field validation.

Django URL Resolver Exceptions


These exceptions are defined in django.urls module.

Exception Description

Resolver404 This exception raised when the path passed to resolve() function does not map to a
view.
NoReverseMatch It is raised when a matching URL in your URLconf cannot be identified based on the
parameters supplied.

Django Database Exceptions


The following exceptions are defined in django.db module.

Exception Description

DatabaseError It occurs when the database is not available.

IntegrityError It occurs when an insertion query executes.

DataError It raises when data related issues come into the database.

Django Http Exceptions


The following exceptions are defined in django.http module.

Exception Description

UnreadablePostError It is raised when a user cancels an upload.

Django Transaction Exceptions


The transaction exceptions are defined in django.db.transaction.

Exception Description

TransactionManagementError It is raised for any and all problems related to database transactions.

Django Exception Example


Suppose, we want to get employee record where id = 12, our view function will look
below. It raises a DoesNotExist exception if data not found. This is Django's built-in
exception.

// views.py

1. def getdata(request):
2. data = Employee.objects.get(id=12)
3. return HttpResponse(data)

// urls.py

1. path('get',views.getdata)

It shows the following exception because no record is available at id 12.

Output:

We can handle it by using try and except, now let's handle this exception.

// Views.py

1. def getdata(request):
2. try:
3. data = Employee.objects.get(id=12)
4. except ObjectDoesNotExist:
5. return HttpResponse("Exception: Data not found")
6. return HttpResponse(data);

Output:
Django Session
A session is a mechanism to store information on the server side during the interaction
with the web application.

In Django, by default session stores in the database and also allows file-based and
cache based sessions. It is implemented via a piece of middleware and can be enabled
by using the following code.

Put django.contrib.sessions.middleware.SessionMiddleware in MIDDLEWARE


and django.contrib.sessions in INSTALLED_APPS of settings.py file.

To set and get the session in views, we can use request.session and can set multiple
times too.

The class backends.base.SessionBase is a base class of all session objects. It contains


the following standard methods.

Method Description

__getitem__(key) It is used to get session value.

__setitem__(key, value) It is used to set session value.

__delitem__(key) It is used to delete session object.

__contains__(key) It checks whether the container contains the particular session object or not.
get(key, default=None) It is used to get session value of the specified key.

Let's see an example in which we will set and get session values. Two functions are
defined in the views.py file.

Django Session Example


The first function is used to set and the second is used to get session values.

//views.py

1. from django.shortcuts import render


2. from django.http import HttpResponse
3.
4. def setsession(request):
5. request.session['sname'] = 'irfan'
6. request.session['semail'] = 'irfan.sssit@gmail.com'
7. return HttpResponse("session is set")
8. def getsession(request):
9. studentname = request.session['sname']
10. studentemail = request.session['semail']
11. return HttpResponse(studentname+" "+studentemail);

Url mapping to call both the functions.

// urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. path('ssession',views.setsession),
8. path('gsession',views.getsession)
9. ]

Run Server
1. $ python3 manage.py runserver

And set the session by using localhost:8000/ssession

The session has been set, to check it, use localhost:8000/gsession

Django Cookie
A cookie is a small piece of information which is stored in the client browser. It is used
to store user's data in a file permanently (or for the specified time).

Cookie has its expiry date and time and removes automatically when gets expire.
Django provides built-in methods to set and fetch cookie.

The set_cookie() method is used to set a cookie and get() method is used to get the
cookie.

The request.COOKIES['key'] array can also be used to get cookie values.

History of Java

Django Cookie Example


In views.py, two functions setcookie() and getcookie() are used to set and get cookie
respectively

// views.py

1. from django.shortcuts import render


2. from django.http import HttpResponse
3.
4. def setcookie(request):
5. response = HttpResponse("Cookie Set")
6. response.set_cookie('java-tutorial', 'javatpoint.com')
7. return response
8. def getcookie(request):
9. tutorial = request.COOKIES['java-tutorial']
10. return HttpResponse("java tutorials @: "+ tutorial);

And URLs specified to access these functions.

// urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from myapp import views
4. urlpatterns = [
5. path('admin/', admin.site.urls),
6. path('index/', views.index),
7. path('scookie',views.setcookie),
8. path('gcookie',views.getcookie)
9. ]

Start Server

1. $ python3 manage.py runserver

After starting the server, set cookie by using localhost:8000/scookie URL. It shows
the following output to the browser.
And get a cookie by using localhost:8000/gcookie URL. It shows the set cookie to the
browser.

Create CSV with Django


Django uses Python's built-in CSV library to create Dynamic CSV (Comma Separated
Values) file. We can use this library in our project's view file.

Let's see an example, here we have a Django project to which we are implementing
this feature. A view function getfile() is created.

Django CSV Example


In this example, we are creating CSV using static data.

// Views.py

1. import csv
2.
3. def getfile(request):
4. response = HttpResponse(content_type='text/csv')
5. response['Content-Disposition'] = 'attachment; filename="file.csv"'
6. writer = csv.writer(response)
7. writer.writerow(['1001', 'John', 'Domil', 'CA'])
8. writer.writerow(['1002', 'Amit', 'Mukharji', 'LA', '"Testing"'])
9. return response

// urls.py

Provide url for the function.

1. path('csv',views.getfile)

While executing to the browser, it renders a CSV file. See the example.

Apart from static data, we can get CSV from the database too. See, the following
example in which we are getting data from the table by using the Employee model.

Dynamic CSV using Database


// views.py

1. from myapp.models import Employee import csv


2. def getfile(request):
3. response = HttpResponse(content_type='text/csv')
4. response['Content-Disposition'] = 'attachment; filename="file.csv"'
5. employees = Employee.objects.all()
6. writer = csv.writer(response)
7. for employee in employees:
8. writer.writerow([employee.eid,employee.ename,employee.econtact])
9. return response

Output:

Save the file and open into the text editor that contains the following data.

This data is retrieved from the table employee, a snapshot of the table is shown below.
Well, we have seen that this library is very useful to create a dynamic CSV file. Now,
implement it into Django project when required.

Django PDF
Here, we will learn how to design and generate PDF file using Django view. To generate
PDF, we will use ReportLab Python PDF library that creates customized dynamic PDF.

It is an open source library and can be downloaded easily by using the following
command in Ubuntu.

1. $ pip install reportlab

After installing, we can import it by import keyword in the view file.

Below is a simple PDF example, in which we are outputting a string message "Hello
form javatpoint". This library provides a canvas and tools that are used to generate
customized PDF. See the example.

// views.py

1. from reportlab.pdfgen import canvas


2. from django.http import HttpResponse
3.
4. def getpdf(request):
5. response = HttpResponse(content_type='application/pdf')
6. response['Content-Disposition'] = 'attachment; filename="file.pdf"'
7. p = canvas.Canvas(response)
8. p.setFont("Times-Roman", 55)
9. p.drawString(100,700, "Hello, Javatpoint.")
10. p.showPage()
11. p.save()
12. return response

First, provide MIME (content) type as application/pdf, so that output generates as PDF
rather than HTML,

Set Content-Disposition in which provide header as attachment and output file name.

Pass response argument to the canvas and drawstring to write the string after that
apply to the save() method and return response.

// urls.py

1. path('pdf',views.getpdf)

Set the above code in urls.py to call view function.

Run server and access this view on the browser that creates a pdf file. See the examples.

Output:

A PDF file is generated and ready to download. Download the file and open it, it shows
the string message that we wrote.
Apart from it, this library contains the lots of other methods to design and generate
PDF dynamically.

Django Image Upload | How to Upload Image


with Django
Image uploading is one of the main features of any modern web-applications. It allows
the user to upload the image or picture on the server. Fortunately, Django provides
the simple procedure of working with the images especially uploading the images or
pictures. Using this, we can create a beautiful web application where users can upload
images with captions.

In this tutorial, we will discuss how the upload the image in a Django application.
Before we are going further, make sure that you have a basic knowledge of Django. If
you haven't, visit our Django tutorial.

Upload images to Django


Most of the web applications deal with the file or images, Django provides the two
model fields that allow the user to upload the images and files. These fields
are FileField and ImageField; basically ImageField is a specialized version
of FileField that uses Pillow to confirm that file is an image. Let's see the following
example of creating models.

models.py

8.5M

169

Java Try Catch


1. from django.db import models
2.
3. class UploadImage(models.Model):
4. caption = models.CharField(max_length=200)
5. image = models.ImageField(upload_to='images')
6.
7. def __str__(self):
8. return self.caption

We create the UploadImage model that consists of two fields


- caption and image. The image field will work as Django's file storage API. This API
provides the facility to store and retrieve the files and also read and write them.
The upload_to parameter specifies the file location where the image will be stored.

We don't need to create the media directory manually; it will be automatically created
when we upload an image.

First of all, we need to install the pillow library, which helps work with the images. We
can install it using the following pip command.

1. pip install Pillow

Now add the following settings to the settings.py file of your project.

settings.py

1. # Base url to serve media files


2. MEDIA_URL = '/media/'
3.
4. # Path where media is stored
5. MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
o MEDIA_URL - It will serve the media files.
o MEDIA_ROOT - It specifies the path of the root where file will be stored.

In the next step, we should add the following configuration in urls.py file.

Urls.py

1. from django.contrib import admin


2. from django.urls import path
3. from django.urls.conf import include
4. from django.conf import settings
5. from django.conf.urls.static import static
6.
7. urlpatterns = [
8. path('admin/', admin.site.urls),
9. path('', include(('sampleapp.urls'), namespace='sampleapp'))
10.
11. ]
12. if settings.DEBUG:
13. urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDI
A_ROOT)

All required configurations are done; we are ready to run the following commands.

1. python manage.py makemigations


2. python manage.py migrate

After running this command, we are set to create a form that will take the image as
input from the user. Let's create a form.

forms.py

1. from django.db import models


2. from django.forms import fields
3. from .models import UploadImage
4. from django import forms
5.
6.
7. class UserImage(forms.ModelForm):
8. class meta:
9. # To specify the model to be used to create form
10. models = UploadImage
11. # It includes all the fields of model
12. fields = '__all__'

The advantage of creating the Django model form is that it can handle the form
verification without declaring it explicitly in the script. It will also create the form fields
on the page according to the model fields mentioned in the model.py file.
To display this form, we need to create the image_form.html file under the template
folder.

template/image.form.html

1. {% extends 'base.html' %}
2.
3. {% block content %}
4. <form method="post" enctype="multipart/form-data">
5. {% csrf_token %}
6. {{ form.as_p }}
7. <button type="submit">Upload</button>
8. </form>
9.
10. {% if img_obj %}
11. <h3>Succesfully uploaded : {{img_obj.caption}}</h3>
12. <img src="{{ img_obj.image.url}}" alt="connect" style="max-height:300px">
13. {% endif %}
14.
15. {% endblock content %}
Note - We use the enctype = "multipart/form-data" which allows files to be sent
through a POST. Without enctype, the user cannot send the file through a POST
request. It is necessary to include in the form to work with the files or image.

Now we will create the view to handle the requests.

View.py

1. from django.shortcuts import redirect, render


2. from sampleapp.forms import UserImageForm
3. from .models import UploadImage
4.
5. def image_request(request):
6. if request.method == 'POST':
7. form = UserImageForm(request.POST, request.FILES)
8. if form.is_valid():
9. form.save()
10.
11. # Getting the current instance object to display in the template
12. img_object = form.instance
13.
14. return render(request, 'image_form.html', {'form': form, 'img_obj': img_object}
)
15. else:
16. form = UserImageForm()
17.
18. return render(request, 'image_form.html', {'form': form})

The above view is simple and handling images the same as a normal form. When we
upload the image, the post request will be generated. The form is automatically
validated and saved in the media folder. One point to be noted, we get the image
object using form.instance and it will be used to display the image to web page.

Let's create the URL for this view.

sampleapp/urls.py

1. from django.urls import path


2. from .views import image_request
3.
4. app_name = 'sampleapp'
5. urlpatterns = [
6. path('', image_request, name = "image-request")

We get the following form after running the local host server.

Output:
This file will be stored in the media/images that we have mentioned in the model
field.

Conclusion
In this tutorial, we have discussed how to upload images using Django. Django
provides the simple interface to perform image or file uploading. We just need to do
some configuration and define an ImageField in the model form's field. We can also
upload any file (.xml, .pdf, .word, .etc) by following the same procedure, but we will
need to convert ImageField to FileField in the model's field.

Django ORM Queries | How to work with the


ORM Queries
In this tutorial, we will have the complete discussion over the Django ORM queries and
how we can use them to manipulate the data.

Django ORM is one of the best tools of Django and plays very essential role to perform
database related tasks. It provides abstractions with the database, in a mostly database
agnostic way.

Django ORM consists of ease of use abstraction. It keeps "Simple things easy and
hard things possible."

Here we will have the detailed explanation of each ORM queries and also see their
associated SQL queries.

Creating Table in Database using Model


First, we will create a sample database using the Django model that consists of some
data and we will run the query on that database.

model.py

1. # Create your models here.


2.
3. class Student(models.Model):
4. username = models.CharField(max_length=20)
5. first_name = models.CharField(max_length=30)
6. last_name = models.CharField(max_length=30)
7. mobile = models.CharField(max_length=10)
8. email = models.EmailField()
9.
10. def __str__(self):
11. return "%s %s" % (self.first_name, self.last_name)
And then run the following command.

1. python manage.py makemigrations


2. python manage.py migrate

We are set to run the query.

How to get all records from table(Model)


We have a model called Student. To get all records from model, we will use
the Student.objects.all(). To do so, open the Django shell to run the query.

1. >>> from sampleapp.models import Student


2. >>> queryset = Student.objects.all()
3. >>> queryset
4. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpita Shar
ma>, <Student: Prince Sharma>, <Student: Megha Bhardwaj>, <Student: Akash Mish
ra>]>

You might be wonder how Django ORM makes our queries executed or what the
corresponding query of the code we are writing. It is quite simple to get the SQL query,
we need to use the str() and pass the queryset object along with query.

Corresponding SQL Query

1. >>> str(queryset.query)
2. 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "sampleapp_stu
dent"."first_name", "sampleapp_student"."last_name", "sampleapp_student"."mobile",
"sampleapp_student"."email" FROM "sampleapp_student"'

How to add record to table(Model)


We will use the Student.objects.create() and pass the fields along with its value as
argument. Let's see the below example.
1. >>> queryset = Student.objects.create(username = 'rahul20', first_name = 'Ra
hul', last_name = 'Shakya', mobile = '77777', email = 'rahul@gmail.com')
2. >>> queryset.save()

Note that, we need to use the .save() method on the query object to save the newly
created record in table, otherwise it will not show in database.

Retrieving Single Objects from QuerySets


Suppose we need a specific object from a queryset to matching the result. We can do
this using the get() method. The get() returns the single object directly. Let's see the
following example.

Example -

1. >>> from sampleapp.models import Student


2. >>> queryset = Student.objects.get(pk = 1)
3. >>> queryset
4. <Student: Ritesh Tiwari>

Example - 2

1. >>> queryset = Student.objects.get(mobile = 22222)


2. >>> queryset
3. <Student: Yash Sharma>

As we can see in both examples, we get the single object not a queryset of a single
object. If there is no result then match the query, get() will raise
a DoesNotExist exception. On the other hand, if there is multiple field matches, it will
raise the MultipleObjectReturned, which is an attribute of the model class itself.

Filtering the Records


In the earlier example, the QuerySet returned by all() describes the all record in the
database table. But sometimes, we need to select the subset of complete set of object
and it can be done by adding the filter conditions.

In the below example, we will fetch the data which first name starts with the R.

1. >>> queryset = Student.objects.filter(first_name__startswith = 'R')


2. >>> queryset
3. <QuerySet [<Student: Ritesh Tiwari>, <Student: Rahul Shakya>]>
4. >>> str(queryset.query)
5. 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "samplea
pp_student"."first_name", "sampleapp_student"."last_name", "sampleapp_stud
ent"."mobile", "sampleapp_student"."email" FROM "sampleapp_student" WHE
RE "sampleapp_student"."first_name" LIKE R% ESCAPE \'\\\''
Note - The difference between get() and filter() method is that, the filter() method
returns the queryset of object(s) where get() method returns single object.

Using exclude() Method


It returns a new QuerySet containing objects that do not match the given lookup
parameter. In other words, it excluded the records according the lookup condition.
Let's understand the following example.

1. >>> queryset = Student.objects.exclude(first_name__startswith = 'R')


2. >>> queryset

Output:

, , , , ]>

How to make OR queries in Django ORM?


The OR operation is performed when we need the record filtering with two or more
conditions. In the below example, we will get the student whose first_name starts with
'A' and last_name starts with 'M'.

Django allows us to do this in two ways.

o queryset_1 |queryset_2
o filter(Q(<condition_1>) | Q(<condition_2>

1. >>> queryset = Student.objects.filter(first_name__startswith = 'R') | Student.ob


jects.filter(last_name__startswith = 'S')
2. >>> queryset
3. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpi
ta Sharma>, <Student: Prince Sharma>, <Student: Rahul Shakya>]>

We get the student details those first name starts with 'A' and last name starts with 'S'.

Let's the SQL query of corresponding OR operator.


1. >>> str(queryset.query)
2. 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "sampleapp_stu
dent"."first_name", "sampleapp_student"."last_name", "sampleapp_student"."mobile",
"sampleapp_student"."email" FROM "sampleapp_student" WHERE ("sampleapp_stude
nt"."first_name" LIKE R% ESCAPE \'\\\' OR "sampleapp_student"."last_name" LIKE S% E
SCAPE \'\\\')'

How to make AND queries in Django ORM?


The AND operation is performed when we need the record matching with two or more
conditions. In the below example, we will get the student whose first_name starts
with 'P' and last_name starts with 'S'.

Django allows us to do this in three ways.

o queryset_1 & queryset_2


o filter(<condition_1>, <condition_2>)
o filter(Q(condition_1) & Q(condition_2))

1. >>> queryset = Student.objects.filter(first_name__startswith = 'P') & Student.o


bjects.filter(last_name__startswith = 'S')
2. >>> queryset
3. <QuerySet [<Student: Prince Sharma>]>

Only one object satisfied the given conditions.

We can also use the following query.

1. queryset2 = User.objects.filter( first_name__startswith='A', last_name__startswit


h='S' )

Or

1. queryset3 = User.objects.filter(Q(first_name__startswith='R') & Q(last_name__st


artswith='D') )

All queries will give the same result.

Creating Multiple Object in One Shot


Sometimes we want create multiple objects in one shot. Suppose we want to create
new objects at once and we don't want to run the multiple queries to the database.
Django ORM provides the bulk_create to create multiple objects in one way.

1. >>> Student.objects.all().count()
2. 7

Let's create the multiple records in one query.

1. Student.objects.bulk_create([Student(first_name = 'Jai', last_name = 'Shah', mo


bile = '88888', email = 'shah@reddif.com'),Student(first_name = 'Tarak', last_n
ame = 'Mehta', mobile = '9999', email = 'tarak@reddif.com'), Student(first_na
me = 'SuryaKumar', last_name = 'Yadav', mobile = '00000', email = 'yadav@re
ddif.com')])
2. [<Student: Jai Shah>, <Student: Tarak Mehta>, <Student: SuryaKumar Yadav>]

Now, our database table will update. The bulk_create takes a list of unsaved objects.

1. >>> Student.objects.all().count()
2. 10

Limiting QuerySets
We can set the limit on the queryset using the Python list's slicing syntax. This is
equivalent operation of SQL's LIMIT and OFFSET clauses. Let's see the following
query.

1. >>> Student.objects.all()[:4]
2. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpita Shar
ma>, <Student: Prince Sharma>]>

Below query will return first record to fifth record.

1. >>> Student.objects.all()[1:6]
2. <QuerySet [<Student: Yash Sharma>, <Student: Arpita Sharma>, <Student: Prince Sh
arma>, <Student: Megha Bhardwaj>, <Student: Akash Mishra>]>

Negative indexing is not supported. However, we can use the step in QuerySets.

1. >>> Student.objects.all()[:10:2]
2. [<Student: Ritesh Tiwari>, <Student: Arpita Sharma>, <Student: Megha Bhardwaj>, <
Student: Rahul Shakya>, <Student: Tarak Mehta>]

To fetching the single record, we can do the following operation.

1. >>> Student.objects.all()[0]
2. <Student: Ritesh Tiwari>

How to order a QuerySets in ascending or descending


order?
Django provides the order_by method for ordering the queryset. This method takes
the field name which we want to Order (ascending and descending) the result. Let's
see the following example.

Example - Ascending order

1. >>> from sampleapp.models import Student


2. >>> Student.objects.all().order_by('mobile')
3. <QuerySet [<Student: SuryaKumar Yadav>, <Student: Ritesh Tiwari>, <Studen
t: Yash Sharma>, <Student: Arpita Sharma>, <Student: Prince Sharma>, <Stud
ent: Megha Bhardwaj>, <Student: Akash Mishra>, <Student: Rahul Shakya>, <
Student: Jai Shah>, <Student: Tarak Mehta>]>

For descending order, we will use the Not '-' before the query field.

1. >>> from sampleapp.models import Student


2. >>> Student.objects.all().order_by('-mobile')
3. <QuerySet [<Student: Tarak Mehta>, <Student: Jai Shah>, <Student: Rahul Sh
akya>, <Student: Akash Mishra>, <Student: Megha Bhardwaj>, <Student: Prin
ce Sharma>, <Student: Arpita Sharma>, <Student: Yash Sharma>, <Student: R
itesh Tiwari>, <Student: SuryaKumar Yadav>]>

We can also pass the multiple fields in the order_by function.

1. >>> Student.objects.all().order_by('first_name','-mobile')
2. <QuerySet [<Student: Akash Mishra>, <Student: Arpita Sharma>, <Student: Jai Shah
>, <Student: Megha Bhardwaj>, <Student: Prince Sharma>, <Student:
3. Rahul Shakya>, <Student: Ritesh Tiwari>, <Student: SuryaKumar Yadav>, <Stu
dent: Tarak Mehta>, <Student: Yash Sharma>]>
How to order on a field from a related model (with foreign key)?
Now, we will learn how we can order the data in the relation model. We create another
model called Teacher which is a related model of Student model.

Models

1. class Teacher(models.Model):
2. teacher_name = models.CharField(max_length=200)
3.
4. def __str__(self):
5. return f'{self.teacher_name}'
6.
7. class Student(models.Model):
8. username = models.CharField(max_length=20)
9. first_name = models.CharField(max_length=30)
10. last_name = models.CharField(max_length=30)
11. mobile = models.CharField(max_length=10)
12. email = models.EmailField()
13. teacher_name = models.ForeignKey(Teacher, blank = True, null = True, on_
delete= models.CASCADE)

We have added teachers name and each teacher is associated with the student. Now
we want to order Student by teacher_name inside each teacher_name by the
Student. We can do as follows.

1. >>> Student.objects.all().order_by('teacher__id', 'first_name')


2. <QuerySet [<Student: Prince Sharma>, <Student: Ritesh Tiwari>, <Student: SuryaKu
mar Yadav>, <Student: Tarak Mehta>, <Student: Arpita Sharma>, <Student: Megha B
hardwaj>, <Student: Jai Shah>, <Student: Rahul Shakya>, <Student: Yash Sharma>,
<Student: Akash Mishra>]>

Important Field Lookups


Query field lookups are nothing but a condition which specifies same as the
SQL WHERE clause. They are stated as keyword arguments to the QuerySet methods
such as filter(), exclude(), and get().

Example -
1. Student.objects.filter(first_name__startswith = 'Ritesh')
2. <QuerySet [<Student: Ritesh Tiwari>]>

This is same as the following SQL query

1. Select * from Student where first_name = "Ritesh"

Let's understand some important lookups.

o exact

It returns the exact result according to the search.

1. >>> Student.objects.get(first_name__exact = 'Arpita')


2. <Student: Arpita Sharma>

Lookup should be used after the __ double underscore. We can use the case-
insensitive version called iexact.

o contains

It is used to case-sensitive test. Let's see the following example.

1. >>> from sampleapp.models import Student


2. >>> Student.objects.filter(last_name__contains = 'Shar')
3. <QuerySet [<Student: Yash Sharma>, <Student: Arpita Sharma>, <Student: Pr
ince Sharma>]>

If we translate the SQL query then it will look like as below.

1. SELECT ... WHERE last_name LIKE '%Shar%';

There is also case-incentive version called icontains.

How to perform join operations in Django


The SQL join combines data or rows from two or more tables based on a common field
between them. We can perform join operation in many ways. Let's understand the
following example.

1. >>> q = Student.objects.select_related('teacher')
2. >>>q
3. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpi
ta Sharma>, <Student: Prince Sharma>, <Student: Megha Bhardwaj>, <Stude
nt: Akash Mishra>, <Student: Rahul Shakya>, <Student: Jai Shah>, <Student:
Tarak Mehta>, <Student: SuryaKumar Yadav>]>
4. >>>print(q.query)
5. SELECT "sampleapp_student"."id", "sampleapp_student"."username", "samplea
pp_student"."first_name", "sampleapp_student"."last_name", "sampleapp_stud
ent"."mobile", "sampleapp_student"."email", "sampleapp_student"."teacher_id"
, "sampleapp_teacher"."id", "sampleapp_teacher"."teacher_name" FROM "sam
pleapp_student" LEFT OUTER JOIN "sampleapp_teacher" ON ("sampleapp_stu
dent"."teacher_id" = "sampleapp_teacher"."id")

How to group record in Django ORM?


Django ORM provides the grouping facility using the aggregation functions
like Max, Min, Avg, and Sum. Sometimes we need to get the aggregate values from
the objects. Let's understand the following example.

1. >>> from django.db.models import Avg, Max, Min, Sum, Count


2. >>> Student.objects.all().aggregate(Avg('id'))
3. {'id__avg': 5.5}
4. >>> Student.objects.all().aggregate(Min('id'))
5. {'id__min': 1}
6. >>> Student.objects.all().aggregate(Max('id'))
7. {'id__max': 10}
8. >>> Student.objects.all().aggregate(Sum('id'))
9. {'id__sum': 55}

How to perform truncate like operation using Django


ORM?
Truncate in SQL means clear the table data for future use. Django doesn't provide the
built-in methods to truncate the table, but we can use the delete() method to get the
similar result. Let's understand the following example.

1. >>> Student.objects.all().count()
2. 10
3. >>> Student.objects.all().delete()
4. (10, {'sampleapp.Student': 10})
5. >>> Student.objects.all().count()
6. 0
7. >>> Student.objects.all()
8. <QuerySet []>

If you want to delete the individual object instance, you need to call delete() method
on the individual instances of that model. We have called the delete() method on
model so it deleted the entire data.

How to get union of Data


Union means getting the record which are common in both query sets. Let's see how
we can do this.

1. >>> q1 = Student.objects.filter(id__gte = 15)


2. >>> q1
3. <QuerySet [<Student: Megha Bhardwaj>, <Student: Akash Mishra>]>
4. >>> q2 = Student.objects.filter(id__lte = 15)
5. >>> q2
6. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpita Shar
ma>, <Student: Prince Sharma>, <Student: Megha Bhardwaj>]>
7. >>> q1.union(q2)
8. <QuerySet [<Student: Ritesh Tiwari>, <Student: Yash Sharma>, <Student: Arpita Shar
ma>, <Student: Prince Sharma>, <Student: Megha Bhardwaj>, <Student: Akash Mish
ra>]>

What is difference between null=True and


blank=True?
In Django, we use null and blank often, by default their values are False. Both of these
value work at field level where we want to keep a field null or blank. Both values seem
similar but they are different in use.

If null=True means the field value is set as NULL i.e. no data. It is basically for the
database column value.

1. date = models.DateTimeField(null=True)

The blank = True specifies whether field is required in forms.


1. title = models.CharField(blank=True) // title can be kept blank. In the database
("") will be stored.

If we set null=True blank=True, means that the field is optional in all circumstances.

1. teacher = models.ForeignKey(null=True, blank=True) // The exception is Char


Fields() and TextFields(), which in Django are never saved as ?→NULL. Blank val
ues are stored in the DB as an empty string ('').

Conclusion
In this tutorial, we have learned some important ORM queries. Django ORM is a
powerful tool and one of the key pillars of Django. Django comes with the built-in
database called SQLite. And we have described the ORM queries which acts same as
the SQL queries.

Django Form Widget | Creating forms using


various widgets
In this tutorial, we will learn how we can apply the various widgets in the form. Django
forms come with various attributes that enhance the appearance of the form. There
are many built-in classes, but we will cover the most commonly used form widgets.
These widgets will help to create more interactive form for any site,

So let's get started.

Creating Projects
All we start with is creating the Django project and app. Then we set the basic
configuration of the newly created app. There are three ways to create the form in
Django - Using Model Form, Using the Form class, and simple HTML form. We will use
the Form class.

Creating Form
Creating a forms.py file in sampleapp under the Hello project

7.8M

135

Triggers in SQL (Hindi)


Hello>sampleapp>forms.py

Example -

1. class SampleForrm(forms.Form):
2. first_name = forms.CharField()
3. last_name = forms.CharField()

To render this form, we need to create the view and template that will used to display
the form to the screen.

views.py

1. from django.shortcuts import redirect, render


2. from .forms import SampleForm
3. def newform(request):
4. form = SampleForm(request.POST or None)
5. return render(request, 'sampleform.html', {'form': form})

Base.html

1. <!doctype html>
2. <html lang="en">
3. <head>
4. <!-- Required meta tags -->
5. <meta charset="utf-8">
6. <meta name="viewport" content="width=device-width, initial-scale=1">
7.
8. <!-- Bootstrap CSS -->
9. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstra
p.min.css" rel="stylesheet" integrity="sha384-
EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmL
ASjC" crossorigin="anonymous">
10. <title>Contact Us</title>
11. </head>
12. <body style = "background-color: #63eb8e">
13. <div class = 'col-md-8'>
14. {% if messages %}
15. <ul>
16. {% for message in messages %}
17. <div class = 'alert alert-{{message.tags}}'>
18. {{ message }}
19. </div>
20. {% endfor %}
21. </ul>
22. {% endif %}
23. </div>
24. {% block content %}
25. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstra
p.bundle.min.js" integrity="sha384-
MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxV
XM" crossorigin="anonymous"></script>
26. {% endblock content %}
27. </body>
28. </html>
29. </textarea></div>
30. <p>In template/sampleform.html file -</p>
31. <div class="codeblock"><textarea name="code" class="xml">
32. {% extends 'base.html' %}
33. {% block content %}
34. <div class = "container-md" >
35. <h1 class = "display-5"> Contact Us </h1>
36. <hr style = "border-top: 3px double #8c8b8b">
37. <form method="post" style = "margin-top: 35px; margin-bottom: 50px">
38. {% csrf_token %}
39. {{ form.as_p }}
40. <button type="submit">Submit</button>
41. </form>
42. </div>
43. {% endblock %}

To display the form we will run the following command.

1. python manage.py runserver

Click on the http://127.0.0.1:8000/


Custom form with fields Widgets
We create a two form-field using the CharField() field that takes in a text input. We
can override the default widget of each field for various uses. The CharField() has a
default widget TextInput which is the equivalent of rendering HTML code <input type
= "text">.

charField() with Teaxarea widget

We add comment field in existing form with the Textarea widget.

Example -

1. class SampleForm(forms.Form):
2. first_name = forms.CharField()
3. last_name = forms.CharField()
4. comment = forms.CharField(widget=forms.Textarea)

Output:
The Textarea widget is equivalent to <textarea>....</textarea> tag, which is a multi-
line text input.

CharField() with Textarea widget attributes

We can also modify the height of Textarea, specify the 'row' attribute in the widget.

Example -

1. class SampleForm(forms.Form):
2. first_name = forms.CharField()
3. last_name = forms.CharField()
4. comment = forms.CharField(widget=forms.Textarea(attrs={'rows':3}))

Output:
Note - The default number of rows of Textarea is 10.

EmailField
The EmailField is used to take the email input from the user. Its default input
is EmailInput and renders as <input type = "email"> in plain HTML.

This field consist of default email validator that requires a @ symbol within the input
for it to be considered valid.

Let's see the following example.

1. class SampleForm(forms.Form):
2. email = forms.EmailField()

Output:

BooleanField
As its name suggests, it takes the Boolean input from the user who is either true or
false. The default value is False and shows the unchecked checkbox in HTML form. Let's
see the following example.

Example -

1. class SampleForm(forms.Form):
2. email = forms.EmailField()
3. agree = forms.BooleanField()

Output:
Custom Form with DataField()
The DateField() takes the value in the date format such as 2021-08-01. The default
input is DateInput but it seems like CharField. Let's see the following example.

Example -

1. class SampleForm(forms.Form):
2. name = forms.CharField()
3. date_of_birth = forms.DateField()

Output:

When we hit the submit button, it shows the "Enter a valid date" warning because we
have provides the wrong format date value.
DateField() with NumberInput widget attribute

To get the number value through the dropdown, we can use the DecimalField(). The
default widget is NumberInput. Let's understand the following example.

Example -

1. from django.forms.widgets import NumberInput


2. class SampleForm(forms.Form):
3. name = forms.CharField()
4. date_of_birth = forms.DateField(widget = NumberInput(attrs={'type':'date'}))

Output:

DateField() with SelectDateWidget widget

Django also provide the facility to select the date manually using
the SelectDateWidget which display three drop-down menus for month, day, and
year. Let's understand the following example.
Example -

1. BIRTH_YEAR_CHOICES = ['1980', '1981', '1982', '1983', '1984', '1985']


2. lass SampleForm(forms.Form):
3. name = forms.CharField()
4. date_of_birth = forms.DateField(widget = SelectDateWidget(years=BIRTH_YEAR_CH
OICES))

Output:

Custom Form with DecimalField()


To get the number value through the dropdown, we can use the DecimalField(). The
default widget is NumberInput. Let's understand the following example.

Example -

1. class SampleForm(forms.Form):
2. name = forms.CharField()
3. value = forms.DecimalField()

Output:
Custom Form with ChoiceField()
The ChoiceField() allows us to choose any one or more options from the given
choices. Let's understand the following example.

Example -

1. Choice_value = [('1', 'First'), ('2', 'Second'), ('3', 'Third')]


2. class SampleForm(forms.Form):
3. name = forms.CharField()
4. rank = forms.ChoiceField(choices=Choice_value)

Output:

ChoiceField() with the Select Widget

The Select widget deals with the choices. They present the list of options to choose
from list of list of choices. The Select widget is equivalent to

1. Choice_value = [('1', 'First'), ('2', 'Second'), ('3', 'Third')]


2. class SampleForm(forms.Form):
3. name = forms.CharField()
4. rank = forms.ChoiceField(widget = forms.RadioSelect, choices=Choice_value)

Output:

Core Arguments
We define the core arguments which will be same for the all fields. Below are the some
important arguments.

required (Boolean)
This argument signifies that if the fields are required for form submission. It takes a
Boolean (True or false) and highlights the asterisk mark next to the field. By default,
the argument is assigned for each value is true.

Example -

1. class SampleForm(forms.Form):
2. name = forms.CharField(required=False)
3. email = forms.EmailField(required=True)

Output:
max_length and min_length
The max_length is assigned to get the maximum length of value and the min_length is
assigned to get the minimum length of value. Let's understand the following value.

Example -

1. class SampleForm(forms.Form):
2. message = forms.CharField(max_length=10)

Output:

Label (String)
We can create the custom label of the field using the label argument in the field. Let's
understand the following example.

Example -
1. class SampleForm(forms.Form):
2. message = forms.CharField(max_length=100, label="Please write a message for us"
)

Output:

Initial (String) for CharField()


To add pre-loaded information to input, use the initial argument. Let's understand the
following example.

Example -

Initial (Boolean) for BooleanField()

We pass the initial=True to the BooleanField() to set the default clicked as the
checkbox. Let's understand the following example.

Example -
Initial (Datetime) for DateField()

We can also initiate the default value of the DatefField(). First, we need to
import datetime at the top of the file. It will set the current date. Let's understand the
following example.

Example -

MultipleChoiceField
This field is used to show pre-define multiple choice options. User can select the option
from the predefined choices. Let's understand the following example.

Example -

MultipleChoiceField() with CheckboxSelectMultiple widget


We can add the multiple choice option in the form of checkbox. Let's understand the
following example.

Example -

ModelChoiceField()
Django provides the facility to use the Django model form. To do so, we need to import
the model in the forms.py file and use the ModelChoiceField() and specify
the queryset in the form field. Let's understand the following example.

Sending Django Contact Form


Here, we will render the Django tradition contact form. Let's see the following example.

Example -

1. GENDER_CHOICES = [
2. ('male', 'Male'),
3. ('female', 'Female'),
4. ('other', 'Other'),
5. ]
6. class SampleForm(forms.Form):
7. first_name = forms.CharField()
8. last_name = forms.CharField()
9. email = forms.EmailField()
10. address = forms.CharField()
11. date_of_birth = forms.DateField(widget=NumberInput(attrs={'type': 'date'}))

12. gender = forms.ChoiceField(choices=GENDER_CHOICES)


Note - We use the crispy form framework and import the crispy_form_tag to the
form template format files.

Example -

1. from django.core.mail import send_mail, BadHeaderError


2. from django.shortcuts import redirect, render
3. from .forms import SampleForm
4. def newform(request):
5. if request.method == "POST":
6. form = SampleForm(request.POST)
7. if form.is_valid():
8. subject = "Contact"
9. body = {
10. 'first_name': form.cleaned_data['first_name'],
11. 'last_name': form.cleaned_data['last_name'],
12. 'email': form.cleaned_data['email'],
13. 'address':form.cleaned_data['address'],
14. 'gender':form.cleaned_data['gender'],
15. 'date_of_birth':form.cleaned_data['date_of_birth'],
16. }
17. message = "\n".join(body.values())
18. try:
19. send_mail(subject, message, 'admin@example.com', ['admin@exam
ple.com'])
20. except BadHeaderError:
21. return HttpResponse('Invalid header found.')
22. return redirect ("main:homepage")
23. form = SampleForm()
24. return render(request, 'sampleform.html', {'form': form})

We have imported the send_mail, BadHeaderError, and HttpResponse at the top of


the file.

Then, we checked the form is generated the post request and checked form is valid.
We have defined the subject and message before trying to send an email to the
specified email address in the send_mail method.

We need to require the EMAIL_BACKED in the setting.py file.


1. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Conclusion
This tutorial has described the important Django forms fields and how we can make it
more user-friendly using the widget. These widgets allow us to create more interactive
forms and save from the hard coding.

Ruby on Rails vs. Django


Ruby on Rails and Django are two leading web frameworks popularly used in web
development. This tutorial discusses which framework is more suitable for the project
and defines the advantages and disadvantages.

These frameworks share lots of similarities, but each has unique features on its way.
Both are written in the dynamic programming language and are object-oriented. Here
is the question, which one is the best framework for your project?

Certainly, the best way to choose is according to the project environment and how
they can meet the features of each software. We will explain both frameworks' features
in detail.

What is Ruby on Rails?


Ruby and Rail is an open-source web framework written on the Ruby programming
language under MIT license. The first version was released 16 years ago. The latest
version is Rails 6.0.3.4, which was released on 7 October 2020. It is a server-side
framework that follows the MVC (Model View Controller).

Ruby on rails is popular of its "convention over configuration," which means that
Rails provide many defaults features that we can modify later. For example, it decides
the folder structure automatically (where to put your controller or view template).

Ruby on Rails also provides some excellent integration for scheduling background jobs
(Active job), an ORM (Object Relationship Mapper), a wrapper for sending e-mails
(ActionMaker). It also comes with the file stores (AWS S3, Google Cloud Storage, and
others) called ActiveStorage.

Despite having many defaults for database, web service, and file storage, we don't
need to go through each step.

Advantages of Ruby and Rails


Ruby on Rails is a quality, scalable, secure, and popular web development framework.
It provides many features that make it famous among developers. The following are
the advantages of Ruby on Rails.

Ruby on Rails is a quality, scalable, secure, and popular web development framework.
It provides many features that make it famous among developers. The following are
the advantages of Ruby on Rails.

o Cost Effective

Ruby on Rails is an open-source framework means 100% free and runs on Linux which
is also an open-source framework. There are many features available, so developers
don't need to write complex code and it save the plenty of developers' time.

We can create great quality website easily without spending hard earned money.

o Built on Model-View-Controller (MVC) Architecture

A web application has three interconnected layers of architecture. The developers write
the code that work on the application data. It centralizes the business logic of the
application and the rules to manipulate the data. It includes HTML, PDF, XML, RSS, and
other formats. The controller acts as bridge between models and views.

o Secure

Ruby on Rail follows the secure development lifecycle, and some security measures are
built within the framework. It has a huge community that is actively working to spot
and patch new vulnerabilities.

o Performance

It provides excellent performance, and it optimizes the code very efficiently. A web
application can have lots of computation or data handling so that Rails could slow
down the application. However, an effective code optimization technique can make
the application smooth.

o Flexibility

Web applications use the frontend and backend capabilities of Rails. We can create
easily, but the application needs to communicate to the server to load the web page.

o Large Community
It has a vast community worldwide, and it is one of the most popular programming
languages on Github. The community is head toward improving code and serving
others on their projects.

If you are stuck anywhere while coding, you can find the solution over the forum. There
is a chance someone else has already built something similar before or is willing to
help you fix any issue.

Disadvantages of Ruby on Rails


Every coin has two sides. Along with some excellent advantages Ruby on Rails also
comes with the few demerits. Below are some important disadvantages that you
should keep in mind while choosing it for your project.

o Run Time Speed and Performance

It is the most frequent argument against Ruby on Rails. The other frameworks (nodes
and Django) are indeed faster than the RoR. It is a slow runtime speed, which makes
them harder to scale web applications.

o Huge cost of wrong decision in development

If we choose the wrong architecture during the initial stage of the project, we might
cost more in Rails than in another framework. An inexperienced team might make
unobvious mistakes that will erode our application's application. In RoR, all
components are tightly coupled and depend on each other.

o Hard to create APIs

It might be problematic to create APIs with Ruby on Rails. It has varying quality and
standard of documentation and a slow runtime speed. There is also a lack of structured
information so that users won't find the solution of complex problem

o Lack of Proper Documentation

Ruby on Rails doesn't have proper documentation. Good documentation plays an


excellent role in finding a common solution. Since reading the documentation is not
necessary for experienced developers, they need to study the code.

What is Django?
Django is a high-level web framework that is written in Python. It was released in 2005
and developed by the Django Software Foundation.
This framework is battery-included and used for developing rich applications. It follows
the MVT architecture for rapid web development. Many technology giants such as
Mozilla, Instagram, Bitbucket, Pinterest, etc., are using the Django framework.

It comes with the built-in database support SQLite. The primary aim of the Django is
to provide a fast development environment, which helps developers create an
application in a shorter period

Advantages of Django
Django is the most popular framework nowadays because of its excellent advantages.
Below are some interesting advantages of Django.

o Provides Rapid Development

Rapid development means the developer doesn't need to know about the backend
details to make a fully functioning website. It cuts off the complex coding to build
something from scratch. Django provides the built-in admin panel, so we don't need
to make it from scratch, and it also provides the built-in support of SQLite. So we don't
need to write typical SQL queries. Instead, we can use the Django models.

o Excellent Documentation

It is one of the best features of Django, which motivates to learn it. It offers the best
documentation as compared to other available technologies. As a result, users can
easily find the solution of their problems. It also helps to utilize the technology.

o SEO Optimized

SEO (Search engine optimization) is key factor in website ranking. Django comes with
the SEO optimize feature that helps to make the web pages browser and user friendly.

o High Scalability

Scalability term refers to what level of technology gets implemented. Many tech giants
use Django. A famous photo-sharing platform Instagram uses Django. Django
provides a system that can handle complex data without any system failure.

Disadvantages of Django
Despite being written in Python, Django has some disadvantages. Let's understand the
following demerits of Django.

o ORM system lacks features


Django ORM provides the facility to work with the several databases and perform
queries in pythonic way. But it lacks some essential features, which are available in the
other ORM systems.

o Components are depended on each other

Most web framework keeps their component loosely coupled so that developer can
modify independently. Unfortunately, components are tightly coupled in Django that
force developers to install all of them simultaneously.

o Bad at handling multiple requests at same time

As we know, Django is a trending and modern framework, but it cannot handle the
multiple requests at the same time. Many frameworks including Java combines these
processes to cut down development time.

Difference between Ruby on Rails and Django


Below are the main difference between Django and RoR.

Parameters Ruby on Rails Django

Language RoR is an open-source framework written Django is an open-source framework


in Ruby. written in Python.

Architecture It follows the Model View Controller (MVC). It follows the Model View Template
(MVT).

Usage It is used to develop database backend It is used to develop rich and complex
application and metaprogramming. database-driven website.

Speed and RoR is slightly faster than Django because Django Rest Framework provides the
Performance it has the advantage of a rich repository of excellent performance boost.
excellent libraries.

Stability RoR provides a convenient approach for Django practices a more conventional
the reuse of code and minimizes the approach by sticking to any proven
dependencies. method.
Syntax RoR's syntax is much flexible as compared Its syntax is easy to read and debug.
to Django, and sometimes it can create
problems and make it harder to pass a
project to other team.

Documentation of Documentation of Ruby on Rails is less Django comes with detailed and well-
Framework effective than Django. structured documentation. Anyone
learn Django's concept in detail using
its documentation.

Community It has huge community of developers. It has a small community of


developers.

Static files It serves the static files as they are It provided the built-in configuration
configured. for static files.

Web Servers It uses the NGINX, Apache, and WEBrick as The main servers of Django are NGIX
the prominent servers. using WSGI, Apache, Gunicorn, and
Cherokee.

Conclusion
We have had a detailed explanation of the Django and Ruby on Rails frameworks, and
both are top of their category. Nevertheless, there are few specific areas where one
supersedes the other.

Suppose we require a rich web application that includes high-quality features, then
Django is the most appropriate framework for us. However, if we are looking for a
quick launch and then work on the detail of the websites, then Ruby and Rails is best.
So, first of all, understand the project requirement and then move on to the
appropriate frameworks.

select_related and prefetch_related in Django


Django ORM stands for Object Relationship Mapper and it is used to work with the
database, and we can store the data in a Pythonic way. This tutorial will give you a
detailed explanation about one of the important concepts of Django ORM Query
optimization - select_related and pre_fetch related.

What is select_related in Django?


When we fetch any object from the database, it returns the new queryset consists of
that object not a particular object. It will create the new queryset of all related objects
on access time. This is not good in all cases.

Before moving the select_related operation, we should add below logging to the
setting.py file to see all SQL queries which are run behind in the Django CRM.

Merge the following snippet with the LOGGING field in your settings.py:

1. LOGGING = {
2. 'version': 1,
3. 'filters': {
4. 'require_debug_true': {
5. '()': 'django.utils.log.RequireDebugTrue',
6. }
7. },
8. 'handlers': {
9. 'console': {
10. 'level': 'DEBUG',
11. 'filters': ['require_debug_true'],
12. 'class': 'logging.StreamHandler',
13. }
14. },
15. 'loggers': {
16. 'django.db.backends': {
17. 'level': 'DEBUG',
18. 'handlers': ['console'],
19. }
20. }
21. }

Model.py

We create two models, a Movie and Director. The movie model has three fields,
including movie_title, release_year, and director foreign key.

1. from django.db import models


2.
3. class Director(models.Model):
4. name = models.CharField(max_length=20)
5.
6. def __str__(self):
7. return self.name
8.
9. class Movie(models.Model):
10. movie_title = models.CharField(max_length=150)
11. release_year = models.IntegerField()
12. director = models.ForeignKey(Director, on_delete = models.CASCADE, max_length
=100)
13.
14. def __str__(self):
15. return self.name

Now we will run the following commands to make these tables in the database.

1. python manage.py makemigrations


2. python manage.py migrate

We will open the Django shell and create objects in order to store the date in tables.

1. python manage.py shell

Create the director objects in the Django shell.

1. >>> from sampleapp.models import Director, Movie


2. >>> director1 = Director.objects.create(name = "Steven Spiellberg")
3. >>> director1.save()
4. >>> director2 = Director.objects.create(name = "Christopher Nolan")
5. >>> director2.save()
6. >>> director3 = Director.objects.create(name = "Alan Taylor")
7. >>> director3.save()

We get the queryset of directors using following commands.

1. >>> Director.objects.all()
2. (0.000) SELECT "sampleapp_director"."id", "sampleapp_director"."name" FROM "sampl
eapp_director" LIMIT 21; args=()
3. <QuerySet [<Director: Steven Spiellberg>, <Director: Christopher Nolan>, <Di
rector: Alan Taylor>]>
We can see the corresponding SQL query which is running internally.

Now we will create objects of the movie model.

1. movie1 = Movie.objects.create(name = "Super 8", release_year = '2011', direct


or = director1)
2. (0.141) INSERT INTO "sampleapp_movie" ("movie_title", "release_year", "director_id")
VALUES ('Super 8', 2011, 1); args=['Super 8', 2011, 1]
3. >>> movie1.save()
4. (0.172) UPDATE "sampleapp_movie" SET "movie_title" = 'Super 8', "release_year" = 20
11, "director_id" = 1 WHERE "sampleapp_movie"."id" = 1; args=('Super 8', 2011, 1, 1)

Above output displays the SQL queries of movie object. Same as we created the three
movies objects.

1. >>> from sampleapp.models import Director, Movie


2. >>> Movie.objects.all()
3. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sa
mpleapp_movie"."release_year", "sampleapp_movie"."director_id" FROM "sam
pleapp_movie" LIMIT 21; args=()
4.
5. <QuerySet [<Movie: Super 8>, <Movie: Ready Player One>, <Movie: Munich>
, <Movie: The Terminal>, <Movie: Game of Thrones>, <Movie: The Terminal>,
<Movie: Super 8>]>

The Movie table has the foreign key relationship with the Director table. So we can use
the following Query to fetch data

1. >>> dir = Movie.objects.get(id = 2)


2. (0.016) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sampleap
p_movie"."release_year", "sampleapp_movie"."director_id" FROM "sampleapp_movie"
WHERE "sampleapp_movie"."id" = 2 LIMIT 21; args=(2,)
3. >>> dir.director.name
4. (0.000) SELECT "sampleapp_director"."id", "sampleapp_director"."name" FROM "sampl
eapp_director" WHERE "sampleapp_director"."id" = 2 LIMIT 21; args=(2,)
5. 'Christopher Nolan'

As seen in the above code, we need to run a separate query to fetch the director name
using the Movie object.
These separate queries for the related objects decrease the performance of an
application. Suppose we have 1000 movies, and we have to create a list of movies with
the author's name.

Each time we access the foreign key, we need to make another query to retrieve the
value. So, we will end up running 1001queries to fetch the list of the books.

To overcome this problem, Django provides the select_related(), which will reduce the
1001 queries to 1.

Select Related
The select_related performs the inner join operations in the table. In the below
example, movie_id is inner joined with the director.id.

Example -

1. >>> movie = Movie.objects.select_related('director').all()

Let's create a query that fetches all movies with the name of the director in 1 query.

Open the Django shell and type the following query.

1. >>>movies=Movie.objects.select_related('director').annotate(name=F('directo
r__name')).values('id', 'movie_title', 'name')
2. >>> movies
3. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sa
mpleapp_director"."name" AS "name" FROM "sampleapp_movie" INNER JOIN
"sampleapp_director" ON ("sampleapp_movie"."director_id" = "sampleapp_dir
ector"."id") LIMIT 21; args=()
4. <QuerySet [{'id': 1, 'movie_title': 'Super 8', 'name': 'Steven Spiellberg'}, {'id': 2, 'movie_
title': 'Ready Player One', 'name': 'Christopher Nolan'}, {'id': 3, 'movie_title': 'Munich', 'n
ame': 'Alan Taylor'}, {'id': 4, 'movie_title': 'The Terminal', 'name': 'Steven Spiellberg'}, {'i
d': 5, 'movie_title': 'Game of Thrones', 'name': 'Alan Taylor'}, {'id': 6, 'movie_title': 'The T
erminal', 'name': 'Alan Taylor'}, {'id': 7, 'movie_title': 'Super 8', 'name': 'Steven Spiellber
g'}, {'id': 8, 'movie_title': 'Udan', 'name': 'Alan Taylor'}]>

As we can see in the output, only one join query has been called to fetch all movies
with the associate director. It is a big improvement for an application.

Difference Between select_related() and all()


o Without select_related

1. >>> Movie.objects.select_related('director').all()
2.
3. (0.0) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sam
pleapp_movie"."release_year", "sampleapp_movie"."director_id", "sampleapp_d
irector"."id", "sampleapp_director"."name" FROM "sampleapp_movie" INNER J
OIN "sampleapp_director" ON ("sampleapp_movie"."director_id" = "sampleap
p_director"."id") LIMIT 21; args=()
4. (1.0)
5. <QuerySet [<Movie: Super 8>, <Movie: Ready Player One>, <Movie: Munic
h>, <Movie: The Terminal>, <Movie: Game of Thrones>, <Movie: The Termi
nal>, <Movie: Super 8>, <Movie: Udan>]>
o With select_related

Now we will run the query with the all() method.

1. from sampleapp.models import Director, Movie


2. >>> Movie.objects.all()
3. (0.000)
4. SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sampleapp_movi
e"."release_year", "sampleapp_movie"."director_id" FROM "sampleapp_movie" LIMIT 2
1; args=()
5. <QuerySet [<Movie: Super 8>, <Movie: Ready Player One>, <Movie: Munic
h>, <Movie: The Terminal>, <Movie: Game of Thrones>, <Movie: The Termi
nal>, <Movie: Super 8>, <Movie: Udan>]>

We get the same data from the both queries but there is a difference in the query
lookups.

Let's have a look on another scenario where we want to get the director name of first
movie.

o Without select_related

1. >>> Movie.objects.all()[0].director
2. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sampleap
p_movie"."release_year", "sampleapp_movie"."director_id" FROM "sampleapp_movie"
LIMIT 1; args=()
3. (0.000) SELECT "sampleapp_director"."id", "sampleapp_director"."name" FROM
"sampleapp_director" WHERE "sampleapp_director"."id" = 1 LIMIT 21; args=(1
,)
4. <Director: Steven Spiellberg>

We can observe that there are two SQL queries fired to fetch the director name. The
first query fetches the movie name, and the second query fetches the associated
director name. It may cause a lot of redundancy in the application. Let's see how we
can do the same using a single query.

o With select_related

1. >>> Movie.objects.select_related('director').all()[0].director
2. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sampleap
p_movie"."release_year", "sampleapp_movie"."director_id", "sampleapp_director"."id",
"sampleapp_director"."name" FROM "sampleapp_movie" INNER JOIN "sampleapp_dir
ector" ON ("sampleapp_movie"."director_id" = "sampleapp_director"."id") LIMIT 1; arg
s=()
3. <Director: Steven Spiellberg>

The select_related is only limited to foreign key relationship. If there is many to many
relationships then we cannot use the select_related. In that case, we can use
the prefech_related.

Prefetch Related
The prefetch_related can be used with the many to many relationships to improve
performance by reducing the number of queries. Let's understand the following
example.

1. >>> movies = Movie.objects.prefetch_related('publisher')


2. >>> movies
3. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sa
mpleapp_movie"."release_year", "sampleapp_movie"."director_id" FROM "sam
pleapp_movie" LIMIT 21; args=()
4. (0.000) SELECT ("sampleapp_movie_publisher"."movie_id") AS "_prefetch_related_val_
movie_id", "sampleapp_director"."id", "sampleapp_director"."name" FROM "sampleap
p_director" INNER JOIN "sampleapp_movie_publisher" ON ("sampleapp_director"."id"
= "sampleapp_movie_publisher"."director_id") WHERE "sampleapp_movie_publisher".
"movie_id" IN (1, 2, 3, 4, 5, 6, 7, 8); args=(1, 2, 3, 4, 5, 6, 7, 8)
5. <QuerySet [<Movie: Super 8>, <Movie: Ready Player One>, <Movie: Munic
h>, <Movie: The Terminal>, <Movie: Game of Thrones>, <Movie: The Termi
nal>, <Movie: Super 8>, <Movie: Udan>]>

When we try to get the movie with the publisher, it runs the two SQL queries in the
background

1. movie = Movie.objects.prefetch_related('publisher').all()[0].publisher
2. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sampleap
p_movie"."release_year", "sampleapp_movie"."director_id" FROM "sampleapp_movie"
LIMIT 1; args=()
3. (0.000) SELECT ("sampleapp_movie_publisher"."movie_id") AS "_prefetch_relate
d_val_movie_id", "sampleapp_director"."id", "sampleapp_director"."name" FRO
M "sampleapp_director" INNER JOIN "sampleapp_movie_publisher" ON ("sam
pleapp_director"."id" = "sampleapp_movie_publisher"."director_id") WHERE "s
ampleapp_movie_publisher"."movie_id" IN (1); args=(1,)

We can solve the problem using the prefetch_related(). Let's understand the
following example.

1. >>> movie = Movie.objects.prefetch_related('publisher').values('id', 'movie_titl


e', 'publisher')
2. >>> movie
3. (0.000) SELECT "sampleapp_movie"."id", "sampleapp_movie"."movie_title", "sa
mpleapp_movie_publisher"."director_id" FROM "sampleapp_movie" LEFT OUTE
R JOIN "sampleapp_movie_publisher" ON ("sampleapp_movie"."id" = "sample
app_movie_publisher"."movie_id") LIMIT 21; args=()
4. <QuerySet [{'id': 1, 'movie_title': 'Super 8', 'publisher': None}, {'id': 2, 'movie_title': 'Re
ady Player One', 'publisher': None}, {'id': 3, 'movie_title': 'Munich', 'publisher': None}, {'i
d': 4, 'movie_title': 'The Terminal', 'publisher': None}, {'id': 5,
5. 'movie_title': 'Game of Thrones', 'publisher': None}, {'id': 6, 'movie_title': 'The Te
rminal', 'publisher': 1}, {'id': 6, 'movie_title': 'The Terminal', 'publisher': 2}, {'id': 6,
'movie_title': 'The Terminal', 'publisher': 3}, {'id': 7, 'movie_title': 'Super 8', 'publ
isher': 1}, {'id': 7, 'movie_title': 'Super 8', 'publisher': 2}, {'id': 8, 'movie_title': 'Uda
n', 'publisher': 1}, {'id': 8, 'movie_title': 'Udan', 'publisher': 2}]>

Conclusion
So far, we have seen how select_related and prefetch_related efficiently reduced the
queries overhead in Django. The select_related is used with the foreign key
relationship, and it performs the INNER join operations with the associate table.

On the other hand, prefech_related is used with the many to many relationships.
Select_related obtains all data simultaneously through multi-table join Association
query and improves performance by reducing database queries.

But in many to many relationships, it is not a good choice to solve them using the
JOINS because it will be a very long and tedious process. It will be ended up very time-
consuming and SQL statements. To solve such a problem, we will
use prefetch_related.

Django Shortcuts
Django shortcuts module is a collection of helper functions that are generally used in
view function/classes. There are many shortcuts available in
module django.shortcuts. In other words, these function /classes introduce controlled
coupling for convenience's sake.

render()
It combines a given template with a dictionary and returns the HttpResponse object
with that rendered text. Following is the syntax of the render() function.

Syntax -

1. render(request, template_name, context=None, content_type=None, status=N


one, using=None)

Parameters -

Below are the parameters of render() function.

o request - It is used to generate the response.


o template_name - It takes the template names and display the template contents.

Optional Parameters -

o context - It represents the dictionary of values to add to the template context.


o content_type - The MIME type to use for the resulting document. Default to
'text/html'.
o status - It shows the status code for the response. Defaults to 200.
o using - The name of a template engine to use for loading the template.

Example - In the following example, we render the template newapp/index.html.

1. from django.shortcuts import render


2. def new_view(request):
3. # View code here...
4. return render(request, 'newapp/index.html', {
5. 'foo': 'bar',
6. }, content_type='application/xhtml+xml')

It is equivalent to the below code.

1. def new_view(request):
2. # View code here...
3. t = loader.get_template('newapp/index.html')
4. c = {'foo': 'bar'}
5. return HttpResponse(t.render(c, request), content_type='application/xhtml
+xml')

redirect()
The redirect() function is used to redirect to the specific URL. It returns
an HttpResponseRedirect to the appropriate URL for the argument passed. Let's see
the following syntax.

Syntax -

1. redirect(to, *args, permanent=False, **kwargs)

Parameters -

o A model: The model's get_absolute_url() function will be called.


o A view name with arguments: urls.reverse() will be used to reverse-resolve the name.
o A URL will be used as-is for the redirect location.

Example -

1. def blog_view(request, post_id):


2. blog = Post.objects.get(pk=post_id)
3. return redirect(blog)
4. # equivalent to: return HttpResponseRedirect(blog.get_absolute_url())
5.
6. def blog_view(request, post_id):
7. return redirect('blog_details', id=post_id)
8. # equivalent to: return HttpResponseRedirect(reverse('blog_details', args=(post_id,
)))
9.
10. def relative_url_view(request):
11. return redirect('/blogs/archive/')
12. # equivalent to: return HttpResponseRedirect('/blogs/archive/')

By default, the redirect() returns a temporary redirect. However, we can returns the
permanent redirect if set to True.

1. def my_view(request):
2. obj = MyModel.objects.get(...)
3. return redirect(obj, permanent=True)

get_object_or_404()
It returns the DoesNotExist exception if the searched object is not found. On the other
hand, get() method raise Http404.

Parameters

o Klass - A Model class, a Manager, or a QuerySet instance from which to get the object.
o **kwargs - Lookup parameters, which should be in the format accepted
by get() and filter().

Let's understand the below example.

Example -

1. from django.shortcuts import get_object_or_404


2. def my_view(request):
3. obj = get_object_or_404(MyModel, pk=1)

It is equivalent to:
1. from django.http import Http404
2. def my_view(request):
3. try:
4. obj = MyModel.objects.get(pk=1)
5. except MyModel.DoesNotExist:
6. raise Http404("No MyModel matches the given query.")

get_list_or_404()
It returns the results of filter() on a given model manager cast to a list, raising Http404
if the resulting list is empty. The syntax is same as get_object_or_404.

Parameters

o klass - A Model, Manager or QuerySet instance from which to get the list.
o **kwargs - Lookup parameters, which should be in the format accepted
by get() and filter().

Let's understand the following example.

Example -

1. from django.shortcuts import get_list_or_404


2. def my_view(request):
3. my_objects = get_list_or_404(MyModel, published=True)

It is equivalent to:

1. from django.http import Http404


2. def my_view(request):
3. my_objects = list(MyModel.objects.filter(published=True))
4. if not my_objects:
5. raise Http404("No MyModel matches the given query.")

We have discussed a few important shortcuts that provide control over the objects.
These shortcuts also allow handling the potential errors effectively.

How to connect MySQL to Django


The Database is the essential component of the web application to store and organize
the data. Whenever we develop an application/website, we need to choose a suitable
database that makes it more interactive.

Django comes with a built-in SQLite database. However, we can use the various
databases in Django. Below are the lists of databases that Django supports.

o PostgreSQL
o MariaDB
o MySQL
o Oracle
o SQLite

There are also numbers of database backends provided by third parties. Django
middleware allows us to communicate with the database. In this tutorial, we will learn
how we can connect the MySQL database to our Django application.

Prerequisites
o MySQL server 5.7+ must be installed
o Python 3.0+ must be installed

We assume that you have already installed the MySQL server on your local computer.
If you haven't installed then download it from MySQL official website.

Implementation
We use the following steps to establish the connection between Django and MySQL.

Step - 1: Create a virtual environment and setting up the Django project

First we will create the virtual environment and install Django in it. We skip this process
as it lengthens the tutorial. We create the new project using the following command.

1. django-admin startproject MyProject .

The period (.) at the end of command indicates that we are creating the project in the
working directory. If the period is not provided, the project will be created in the new
directory named MyProject and inside that directory contains our actual django files.

Now start the server using the below command.


1. python manage.py runserver

The terminal will display the link http://127.0.0.1:8000, visit this link

Step - 2 Create new Database

We can create the Database using the two ways - MySQL Workbench and MySQL shell.
MySQL Workbench is a GUI tool for the MySQL database that provides features like
SQL development, data modeling and server administration. We will use the MySQL
shell, which is more recommended for learning purposes.

o Connect MySQL Server

o Create database using SQL queries

Use the create database my_database query. It will create the new database.
We can check the databases through the show databases query.

1. mysql> show databases;


1. +--------------------------------------+
2. | Database |
3. +--------------------------------------+
4. | information_schema |
5. | my_database |
6. | mysql |
7. | performance_schema |
8. | sys |
9. +---------------------------------------+
10. 5 rows in set (0.05 sec)

It shows the all available database in our MySQL server.

Step - 3: Update the settings.py

Once we are done creating the Database, we have to update the database section of
the settings.py file with the following setting configuration.

1. DATABASES = {
2. 'default': {
3. 'ENGINE': 'django.db.backends.mysql',
4. 'NAME': 'my_database',
5. 'USER': 'root',
6. 'PASSWORD': 'your_password',
7. 'HOST': '127.0.0.1',
8. 'PORT': '3306',
9. 'OPTIONS': {
10. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
11. }
12. }
13. }

Let's understand what we have done above.

o First, we have replaced the 'django.db.backends.sqlite3' to 'django.db.backends.mysql'.


This is basically indicating we shift SQLite to MySQL database.
o NAME indicates the name of the database we want to use.
o USER is the MYSQL username that has access to the Database and acts as a database
administrator.
o PASSWORD is the password of the database. It will be created at the time of MySQL
installation.
o 'HOST' is '127.0.0.1' and 'PORT' '3306' points out that the MySQL
databaseis hosted with the hostname '0.0.1' and listens to the specific port
number is 3306.
o In the last line we use SET sql_mode = 'STATIC_TRANS_TABLES' which is used to
handle the invalid or missing values from being stored in the database by INSERT and
UPDATE statements.

Step - 4 Install mysqlclient package

Before installing the mysqlclient package, let's understand what mysqlclient is and why
we use. The mysqlclient is the Python interface to MySQL that allows Python project
to connect to the MySQL server.

So it is necessary to install mysqlclient package to establish the connection between


the MySQL and Django. To install, use the following command in the working directory.

1. pip install mysqlclient

Step - 5 Run the migrate command


Now we are ready to migrate or create tables in the newly created database. In this
final step, we will run the migrate command and it will create the exiting tables in
the my_database database.

1. python manage.py migrate

After running this command Django will automatically create the necessary tables such
as auth_group, auth_user, auth_permission, etc. It will also create the tables which
are defined in the models.py file.

mysql> use my_database;

Database changed

mysql> show tables;

1. +-------------------------------------------------------+
2. | Tables_in_my_database |
3. +-------------------------------------------------------+
4. | auth_group |
5. | auth_group_permissions |
6. | auth_permission |
7. | auth_user |
8. | auth_user_groups |
9. | auth_user_user_permissions |
10. | django_admin_log |
11. | django_content_type |
12. | django_migrations |
13. | django_session |
14. | myweatherapp_profile |
15. +---------------------------------------------------------+
16. 11 rows in set (0.05 sec)
Conclusion
In this tutorial, we have discussed how we can use the MySQL database in Django.
Though Django comes with the built-in database SQLite, sometimes it doesn't fulfill
the requirements. So we can connect with the various databases.

How to use F() Expression


In this tutorial, we will learn about the query expression, the F expression, and how to
use it in the QuerySet. Let's have a brief introduction to Query Expressions.

What is query expression?


Query expression signifies the value or a computational that can be a part of an update,
create, filter, order by, annotation or aggregate. If an expression returns the outputs in
Boolean, it may be used directly in filters. Django provides many built-in expressions
that help us to write queries. Query expression can be nested or combined.

Class F() Expression


An F() object specifies the value of a model field, or it refers to the model field values
directly in the database. Let's have a simple example, A Student class with a fees field,
and we want to increase the 20 percent fees of the student.

It can be done as below.


SQL CREATE TABLE

1. student = Student.objects.all()
2. for stu in student:
3. stu.fees *= 1.2

We can do this in the single query with the use of F() expression.

1. from django.db.models import F


2.
3. Student.objects.update(fees=F('fees') * 1.2)

We can also use the following method.

1. student = Student.objects.get(pk=1)
2. student.price = F('fees') * 1.2
3. student.save()

But take care with this kind of assignment. The F() object persist after saving the model.

1. student.fees # fees = Decimal('10.00')


2. student.fees = F('fees') + 1
3. student.save() # fees = Decimal('11.00')
4. student.name = 'What the F()'
5. student.save() # fees = Decimal('12.00')

After the update of the field, that particular field will contain an instance
of django.db.models.expression.CombinedExpression, instead of actual result. We
can access the result immediately as follows.

1. student.fees= F('fees') + 1
2. product.save()
3. print(student.fees) # <CombinedExpression: F(price) + Value(1)>
4. product.refresh_from_db()
5. print(student.fees)

We can also use the annotation of the data.

1. from django.db.models import ExpressionWrapper, DecimalField


2. Product.objects.all().annotate(
3. value_in_stock=ExpressionWrapper(
4. F('fees') * F('rollno'), output_field=DecimalField()
5. )
6. )

Since fees is a DecimalField and rollno is an IntergerField, we need to wrap the


expression inside an ExpressionWrapper object.

It can be used to filter data as well.

1. Student.objects.filter(fees__gte=F(2000))

The F() expression can offer the following advantages.

o It helps us to get the database, and restrict Python to access database.


o It helps to reduce the number of queries for specific operation.

Avoiding race condition using F()


The F() expression also provides additional features like updating a field's value avoids
a race condition.

Let's understand it in a better way - If two Python threads execute the code, the one
thread could retrieve, increment, and save a field's value after the other threads have
retrieved it from the database. The value that is saved by the thread will be based on
the original value. The process of the first thread will be used.

Every time the database is involved in updating the field, the process will become more
robust. The F() expression will update the field based on the value of the field in the
database when the save() or update() is executed.

Using F() to sort null values


We can sort the null values using the F() and nulls_first or nulls_last keyword
argument to asc() or desc() to control the ordering of a field's null values.

Let's see the below example to sort the students who haven't been paid fees after the
new semester.

1. from django.db.models import F


2. Student.objects.order_by(F('fees_paid').desc(nulls_last=True))

Func() Expression
Func() expression are involved in the database function such as COALESCE and LOWER
or aggregates like SUM. We can use them directly.

Example -

1. from django.db.models import F, Func


2. queryset.annotate(field_lower=Func(F('field'), function='LOWER'))

Aggregate Expressions
The Aggregation expression is an important component of a Func() expression. It
informs the query that a GROUP BY clause is required. All aggregates function inherits
function inherits from Aggregate().

Example -

1. from django.db.models import Count


2. Student.objects.annotate(
3. managers_required=(Count('num_employees') /8 ) + Count('num_managers
'))

Value() Expression
A Value() object represents the smallest component of an expression. We can
represent the integer, Boolean, or string values within the expression using
the Value().

The Value() Expression is rarely used directly. When we write the expression F('field')
+ 1, Django implicitly wraps the 1 in the Value(), allowing simple values to be used in
the more complex expression.

The value argument automatically includes the value in the expression, and these
values can be 1, True, or None. Django converts these Python values into their
corresponding database type. The output_field argument must be a model field
instance such as IntegerField() or BooleanField().

Python Flask Tutorial


Flask Tutorial provides the basic and advanced concepts of the Python Flask
framework. Our Flask tutorial is designed for beginners and professionals.

Flask is a web framework that provides libraries to build lightweight web applications
in python. It is developed by Armin Ronacher who leads an international group of
python enthusiasts (POCCO).

What is Flask?
Flask is a web framework that provides libraries to build lightweight web applications
in python. It is developed by Armin Ronacher who leads an international group of
python enthusiasts (POCCO). It is based on WSGI toolkit and jinja2 template engine.
Flask is considered as a micro framework.

What is WSGI?
It is an acronym for web server gateway interface which is a standard for python web
application development. It is considered as the specification for the universal interface
between the web server and web application.

HTML Tutorial

What is Jinja2?
Jinja2 is a web template engine which combines a template with a certain data source
to render the dynamic web pages.

Flask Environment Setup


To install flask on the system, we need to have python 2.7 or higher installed on our
system. However, we suggest using python 3 for the development in the flask.

Install virtual environment (virtualenv)


virtualenv is considered as the virtual python environment builder which is used to
create the multiple python virtual environment side by side. It can be installed by using
the following command.

1. $ pip install virtualenv

Once it is installed, we can create the new virtual environment into a folder as given
below.

1. $ mkdir new
2. $ cd new
3. $ virtualenv venv

To activate the corresponding environment, use the following command on the Linux
operating system.

1. $ venv/bin/activate

On windows, use the following command.

1. $ venv\scripts\activate

We can now install the flask by using the following command.

1. $ pip install flask

However, we can install the flask using the above command without creating the virtual
environment.

To test the flask installation, open python on the command line and type python to
open the python shell. Try to import the package flask.
We have successfully installed the flask since we do not get the error in the process.
Now, we can continue with the tutorial.

First Flask application


In this section of the tutorial, we will build our first python website built using the Flask
framework. In this process, open any text editor of your choice as we are using the
sublime text editor in this tutorial.

Write the following lines of code and save to a file named as script.py.

1. from flask import Flask


2.
3. app = Flask(__name__) #creating the Flask class object
4.
5. @app.route('/') #decorator drfines the
6. def home():
7. return "hello, this is our first flask website";
8.
9. if __name__ =='__main__':
10. app.run(debug = True)
Let's run this python code on the command line and check the result.

Since it is a web application, therefore it is to be run to on the browser at


http://localhost:5000.
To build the python web application, we need to import the Flask module. An object
of the Flask class is considered as the WSGI application.

We need to pass the name of the current module, i.e. __name__ as the argument into
the Flask constructor.

The route() function of the Flask class defines the URL mapping of the associated
function. The syntax is given below.

1. app.route(rule, options)

It accepts the following parameters.

1. rule: It represents the URL binding with the function.


2. options: It represents the list of parameters to be associated with the rule object

As we can see here, the / URL is bound to the main function which is responsible for
returning the server response. It can return a string to be printed on the browser's
window or we can use the HTML template to return the HTML file as a response from
the server.

Finally, the run method of the Flask class is used to run the flask application on the
local development server.

The syntax is given below.

1. app.run(host, port, debug, options)

SN Option Description

1 host The default hostname is 127.0.0.1, i.e. localhost.

2 port The port number to which the server is listening to. The default port number is 5000.

3 debug The default is false. It provides debug information if it is set to true.

4 options It contains the information to be forwarded to the server.

Flask App routing


App routing is used to map the specific URL with the associated function that is
intended to perform some task. It is used to access some particular page like Flask
Tutorial in the web application.

In our first application, the URL ('/') is associated with the home function that returns
a particular string displayed on the web page.

In other words, we can say that if we visit the particular URL mapped to some particular
function, the output of that function is rendered on the browser's screen.

Consider the following example.

Example

1. from flask import Flask


2. app = Flask(__name__)
3.
4. @app.route('/home')
5. def home():
6. return "hello, welcome to our website";
7.
8. if __name__ =="__main__":
9. app.run(debug = True)

Flask facilitates us to add the variable part to the URL by using the section. We can
reuse the variable by adding that as a parameter into the view function. Consider the
following example.

Example

1. from flask import Flask


2. app = Flask(__name__)
3.
4. @app.route('/home/<name>')
5. def home(name):
6. return "hello,"+name;
7.
8. if __name__ =="__main__":
9. app.run(debug = True)

It will produce the following result on the web browser.


The converter can also be used in the URL to map the specified variable to the
particular data type. For example, we can provide the integers or float like age or salary
respectively.

Consider the following example.

Example

1. from flask import Flask


2. app = Flask(__name__)
3.
4. @app.route('/home/<int:age>')
5. def home(age):
6. return "Age = %d"%age;
7.
8. if __name__ =="__main__":
9. app.run(debug = True)
The following converters are used to convert the default string type to the associated
data type.

1. string: default
2. int: used to convert the string to the integer
3. float: used to convert the string to the float.
4. path: It can accept the slashes given in the URL.

The add_url_rule() function


There is one more approach to perform routing for the flask web application that can
be done by using the add_url() function of the Flask class. The syntax to use this
function is given below.

1. add_url_rule(<url rule>, <endpoint>, <view function>)

This function is mainly used in the case if the view function is not given and we need
to connect a view function to an endpoint externally by using this function.

Consider the following example.

Example

1. from flask import Flask


2. app = Flask(__name__)
3.
4. def about():
5. return "This is about page";
6.
7. app.add_url_rule("/about","about",about)
8.
9. if __name__ =="__main__":
10. app.run(debug = True)

Flask URL Building


The url_for() function is used to build a URL to the specific function dynamically. The
first argument is the name of the specified function, and then we can pass any number
of keyword argument corresponding to the variable part of the URL.

This function is useful in the sense that we can avoid hard-coding the URLs into the
templates by dynamically building them using this function.

Consider the following python flask script.

Example
1. from flask import *
2.
3. app = Flask(__name__)
4.
5. @app.route('/admin')
6. def admin():
7. return 'admin'
8.
9. @app.route('/librarion')
10. def librarion():
11. return 'librarion'
12.
13. @app.route('/student')
14. def student():
15. return 'student'
16.
17. @app.route('/user/<name>')
18. def user(name):
19. if name == 'admin':
20. return redirect(url_for('admin'))
21. if name == 'librarion':
22. return redirect(url_for('librarion'))
23. if name == 'student':
24. return redirect(url_for('student'))
25. if __name__ =='__main__':
26. app.run(debug = True)

The above script simulates the library management system which can be used by the
three types of users, i.e., admin, librarion, and student. There is a specific function
named user() which recognizes the user the redirect the user to the exact function
which contains the implementation for this particular function.

Hello Java Program for Beginners


For example, the URL http://localhost:5000/user/admin is redirected to the URL
http://localhost:5000/admin, the URL localhost:5000/user/librarion, is redirected to the
URL http://localhost:5000/librarion, the URL http://localhost:5000/user/student is
redirected to the URL http://localhost/student.

Benefits of the Dynamic URL Building


1. It avoids hard coding of the URLs.
2. We can change the URLs dynamically instead of remembering the manually changed
hard-coded URLs.
3. URL building handles the escaping of special characters and Unicode data
transparently.
4. The generated paths are always absolute, avoiding unexpected behavior of relative
paths in browsers.
5. If your application is placed outside the URL root, for example, in /myapplication
instead of /, url_for() properly handles that for you.

Flask HTTP methods


HTTP is the hypertext transfer protocol which is considered as the foundation of the
data transfer in the world wide web. All web frameworks including flask need to
provide several HTTP methods for data communication.

The methods are given in the following table.


SN Method Description

1 GET It is the most common method which can be used to send data in the unencrypted
form to the server.

2 HEAD It is similar to the GET but used without the response body.

3 POST It is used to send the form data to the server. The server does not cache the data
transmitted using the post method.

4 PUT It is used to replace all the current representation of the target resource with the
uploaded content.

5 DELETE It is used to delete all the current representation of the target resource specified in the
URL.

We can specify which HTTP method to be used to handle the requests in the route()
function of the Flask class. By default, the requests are handled by the GET() method.

POST Method
To handle the POST requests at the server, let us first create a form to get some data
at the client side from the user, and we will try to access this data on the server by
using the POST request.

login.html

1. <html>
2. <body>
3. <form action = "http://localhost:5000/login" method = "post">
4. <table>
5. <tr><td>Name</td>
6. <td><input type ="text" name ="uname"></td></tr>
7. <tr><td>Password</td>
8. <td><input type ="password" name ="pass"></td></tr>
9. <tr><td><input type = "submit"></td></tr>
10. </table>
11. </form>
12. </body>
13. </html>

Now, Enter the following code into the script named post_example.py.

post_example.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/login',methods = ['POST'])
5. def login():
6. uname=request.form['uname']
7. passwrd=request.form['pass']
8. if uname=="ayush" and passwrd=="google":
9. return "Welcome %s" %uname
10.
11. if __name__ == '__main__':
12. app.run(debug = True)

Now, start the development server by running the script using python post_exmple.py
and open login.html on the web browser as shown in the following image.

Give the required input and click Submit, we will get the following result.
Hence, the form data is sent to the development server by using the post method.

GET Method
Let's consider the same example for the Get method. However, there are some changes
in the data retrieval syntax on the server side. First, create a form as login.html.

login.html

1. <html>
2. <body>
3. <form action = "http://localhost:5000/login" method = "get">
4. <table>
5. <tr><td>Name</td>
6. <td><input type ="text" name ="uname"></td></tr>
7. <tr><td>Password</td>
8. <td><input type ="password" name ="pass"></td></tr>
9. <tr><td><input type = "submit"></td></tr>
10. </table>
11. </form>
12. </body>
13. </html>

Now, create the following python script as get_example.py.


get_example.py

1. from flask import *


2. app = Flask(__name__)
3.
4.
5. @app.route('/login',methods = ['GET'])
6. def login():
7. uname=request.args.get('uname')
8. passwrd=request.args.get('pass')
9. if uname=="ayush" and passwrd=="google":
10. return "Welcome %s" %uname
11.
12. if __name__ == '__main__':
13. app.run(debug = True)

Now, open the HTML file, login.html on the web browser and give the required input.

Now, click the submit button.


As we can check the result. The data sent using the get() method is retrieved on the
development server.

The data is obtained by using the following line of code.

1. uname = request.args.get('uname')

Here, the args is a dictionary object which contains the list of pairs of form parameter
and its corresponding value.

In the above image, we can also check the URL which also contains the data sent with
the request to the server. This is an important difference between the GET requests
and the POST requests as the data sent to the server is not shown in the URL on the
browser in the POST requests.

Flask Templates
In the previous examples, we have returned the simple string as the response from the
view function. Although, flask facilitates us to return the response in the form of HTML
templates. In this section of the tutorial, we will go through the ways using which we
can return the HTML response from the web applications.

Example
The following flask script contains a view function, i.e., the message() which is
associated with the URL '/'. Instead of returning a simple plain string as a message, it
returns a message with <h1> tag attached to it using HTML.

script.py
1. from flask import *
2. app = Flask(__name__)
3. @app.route('/')
4. def message():
5. return "<html><body><h1>Hi, welcome to the website</h1></body></
html>"
6. if __name__ == '__main__':
7. app.run(debug = True)

Rendering external HTML files


Flask facilitates us to render the external HTML file instead of hardcoding the HTML in
the view function. Here, we can take advantage of the jinja2 template engine on which
the flask is based.

Flask provides us the render_template() function which can be used to render the
external HTML file to be returned as the response from the view function.

Consider the following example.

Example
To render an HTML file from the view function, let's first create an HTML file named as
message.html.

message.html
1. <html>
2. <head>
3. <title>Message</title>
4. </head>
5. <body>
6. <h1>hi, welcome to the website </h1>
7. </body>
8. </html>

script.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/')
5. def message():
6. return render_template('message.html')
7. if __name__ == '__main__':
8. app.run(debug = True)

Here, we must create the folder templates inside the application directory and save
the HTML templates referenced in the flask script in that directory.

In our case, the path of our script file script.py is E:\flask whereas the path of the
HTML template is E:\flask\templates.

Application Directory

o script.py
o templates
o message.html
Delimiters
Jinga 2 template engine provides some delimiters which can be used in the HTML to
make it capable of dynamic data representation. The template system provides some
HTML syntax which are placeholders for variables and expressions that are replaced by
their actual values when the template is rendered.

The jinga2 template engine provides the following delimiters to escape from the
HTML.

o {% ... %} for statements


o {{ ... }} for expressions to print to the template output
o {# ... #} for the comments that are not included in the template output
o # ... ## for line statements

Example
Consider the following example where the variable part of the URL is shown in the
HTML script using the {{ ... }} delimiter.

message.html

1. <html>
2. <head>
3. <title>Message</title>
4. </head>
5. <body>
6. <h1>hi, {{ name }}</h1>
7. </body>
8. </html>

script.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/user/<uname>')
5. def message(uname):
6. return render_template('message.html',name=uname)
7. if __name__ == '__main__':
8. app.run(debug = True)

The variable part of the URL http://localhost:5000/user/admin is shown in the HTML


script using the {{name}} placeholder.

Embedding Python statements in HTML


Due to the fact that HTML is a mark-up language and purely used for the designing
purpose, sometimes, in the web applications, we may need to execute the statements
for the general-purpose computations. For this purpose, Flask facilitates us the
delimiter {%...%} which can be used to embed the simple python statements into the
HTML.

Example
In the following example, we will print the table of a number specified in the URL, i.e.,
the URL http://localhost:5000/table/10 will print the table of 10 on the browser's
window.

Here, we must notice that the for-loop statement is enclosed inside {%...%} delimiter,
whereas, the loop variable and the number is enclosed inside {{ ... }} placeholders.

script.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/table/<int:num>')
5. def table(num):
6. return render_template('print-table.html',n=num)
7. if __name__ == '__main__':
8. app.run(debug = True)

print-table.py

1. <html>
2. <head>
3. <title>print table</title>
4. </head>
5. <body>
6. <h2> printing table of {{n}}</h2>
7. {% for i in range(1,11): %}
8. <h3>{{n}} X {{i}} = {{n * i}} </h3>
9. {% endfor %}
10. </body>
11. </html>
Referring Static files in HTML
The static files such as CSS or JavaScript file enhance the display of an HTML web page.
A web server is configured to serve such files from the static folder in the package or
the next to the module. The static files are available at the path /static of the
application.

Example
In the following example, the flask script returns the HTML file,
i.e., message.html which is styled using the stylesheet style.css. The flask template
system finds the static CSS file under static/css directory. Hence the style.css is saved
at the specified path.

script.py
1. from flask import *
2. app = Flask(__name__)
3.
4. @app.route('/')
5. def message():
6. return render_template('message.html')
7. if __name__ == '__main__':
8. app.run(debug = True)

message.html

1. <html>
2. <head>
3. <title>Message</title>
4. <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
5. </head>
6.
7. <body>
8. <h1>hi, welcome to the website</h1>
9. </body>
10. </html>

style.css

1. body {
2. background-color: powderblue;
3. }
4. h1 {
5. color: blue;
6. }
7. p {
8. color: red;
9. }
Flask Request Object
In the client-server architecture, the request object contains all the data that is sent
from the client to the server. As we have already discussed in the tutorial, we can
retrieve the data at the server side using the HTTP methods.

In this section of the tutorial, we will discuss the Request object and its important
attributes that are given in the following table.

SN Attribute Description

1 Form It is the dictionary object which contains the key-value pair of form parameters and
their values.

2 args It is parsed from the URL. It is the part of the URL which is specified in the URL after
question mark (?).

3 Cookies It is the dictionary object containing cookie names and the values. It is saved at the
client-side to track the user session.
4 files It contains the data related to the uploaded file.

5 method It is the current request method (get or post).

Form data retrieval on the template


In the following example, the / URL renders a web page customer.html that contains a
form which is used to take the customer details as the input from the customer.

The data filled in this form is posted to the /success URL which triggers the print_data()
function. The print_data() function collects all the data from the request object and
renders the result_data.html file which shows all the data on the web page.

C++ vs Java

The application contains three files, i.e., script.py, customer.html, and result_data.html.

script.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/')
5. def customer():
6. return render_template('customer.html')
7.
8. @app.route('/success',methods = ['POST', 'GET'])
9. def print_data():
10. if request.method == 'POST':
11. result = request.form
12. return render_template("result_data.html",result = result)
13.
14. if __name__ == '__main__':
15. app.run(debug = True)

customer.html

1. <html>
2. <body>
3. <h3>Register the customer, fill the following form.</h3>
4. <form action = "http://localhost:5000/success" method = "POST">
5. <p>Name <input type = "text" name = "name" /></p>
6. <p>Email <input type = "email" name = "email" /></p>
7. <p>Contact <input type = "text" name = "contact" /></p>
8. <p>Pin code <input type ="text" name = "pin" /></p>
9. <p><input type = "submit" value = "submit" /></p>
10. </form>
11. </body>
12. </html>

result_data.html

1. <!doctype html>
2. <html>
3. <body>
4. <p><strong>Thanks for the registration. Confirm your details</strong></p>
5. <table border = 1>
6. {% for key, value in result.items() %}
7. <tr>
8. <th> {{ key }} </th>
9. <td> {{ value }} </td>
10. </tr>
11. {% endfor %}
12. </table>
13. </body>
14. </html>

To run this application, we must first run the script.py file using the command python
script.py. This will start the development server on localhost:5000 which can be
accessed on the browser as given below.
Now, hit the submit button. It will transfer to the /success URL and shows the data
entered at the client.
Flask Cookies
The cookies are stored in the form of text files on the client's machine. Cookies are
used to track the user's activities on the web and reflect some suggestions according
to the user's choices to enhance the user's experience.

Cookies are set by the server on the client's machine which will be associated with the
client's request to that particular server in all future transactions until the lifetime of
the cookie expires or it is deleted by the specific web page on the server.

In flask, the cookies are associated with the Request object as the dictionary object of
all the cookie variables and their values transmitted by the client. Flask facilitates us to
specify the expiry time, path, and the domain name of the website.

1. response.setCookie(<title>, <content>, <expiry time>)

In Flask, the cookies are set on the response object by using the set_cookie() method
on the response object. The response object can be formed by using the
make_response() method in the view function.

In addition, we can read the cookies stored on the client's machine using the get()
method of the cookies attribute associated with the Request object.

1. request.cookies.get(<title>)

A simple example to set a cookie with the title 'foo' and the content 'bar' is given
below.

Example

1. from flask import *


2.
3. app = Flask(__name__)
4.
5. @app.route('/cookie')
6. def cookie():
7. res = make_response("<h1>cookie is set</h1>")
8. res.set_cookie('foo','bar')
9. return res
10.
11. if __name__ == '__main__':
12. app.run(debug = True)

The above python script can be used to set the cookie with the name 'foo' and the
content 'bar' on the browser for the website localhost:5000.

Run this python script using the command python script.py and check the result on
the browser.

We can track the cookie details in the content settings of the browser as given in below
image.
Login application in Flask
Here, we will create a login application in the flask where a login page (login.html) is
shown to the user which prompts to enter the email and password. If the password is
"jtp", then the application will redirect the user to the success page (success.html)
where the message and a link to the profile (profile.html) is given otherwise it will
redirect the user to the error page.

The controller python flask script (login.py) controls the behaviour of the application.
It contains the view functions for the various cases. The email of the user is stored on
the browser in the form of the cookie. If the password entered by the user is "jtp", then
the application stores the email id of the user on the browser as the cookie which is
later read in the profile page to show some message to the user.

The application contains the following python and HTML script. The directory structure
of the application is given below.
login.py

1. from flask import *


2.
3. app = Flask(__name__)
4.
5. @app.route('/error')
6. def error():
7. return "<p><strong>Enter correct password</strong></p>"
8.
9. @app.route('/')
10. def login():
11. return render_template("login.html")
12.
13. @app.route('/success',methods = ['POST'])
14. def success():
15. if request.method == "POST":
16. email = request.form['email']
17. password = request.form['pass']
18.
19. if password=="jtp":
20. resp = make_response(render_template('success.html'))
21. resp.set_cookie('email',email)
22. return resp
23. else:
24. return redirect(url_for('error'))
25.
26. @app.route('/viewprofile')
27. def profile():
28. email = request.cookies.get('email')
29. resp = make_response(render_template('profile.html',name = email))
30. return resp
31.
32. if __name__ == "__main__":
33. app.run(debug = True)

login.html

1. <html>
2. <head>
3. <title>login</title>
4. </head>
5. <body>
6. <form method = "post" action = "http://localhost:5000/success">
7. <table>
8. <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>
9. <tr><td>Password</td><td><input type = 'password' name = 'pass'
></td></tr>
10. <tr><td><input type = "submit" value = "Submit"></td></tr>
11. </table>
12. </form>
13. </body>
14. </html>

success.html

1. <html>
2. <head>
3. <title>success</title>
4. </head>
5. <body>
6. <h2>Login successful</h2>
7. <a href="/viewprofile">View Profile</a>
8. </body>
9. </html>

profile.html

1. <html>
2. <head>
3. <title>profile</title>
4. </head>
5. <body>
6. <h3>Hi, {{name}}</h3>
7. </body>
8. </html>

Execution
Run the python script by using the command python login.py and visit localhost:5000/
on the browser as given in the following snapshots.

Click Submit. It will show the success message and provide a link to the profile.html.
Click on view profile. It will read the cookie set as a response from the browser and
display the following message.

Flask Session
The concept of a session is very much similar to that of a cookie. However, the session
data is stored on the server.

The session can be defined as the duration for which a user logs into the server and
logs out. The data which is used to track this session is stored into the temporary
directory on the server.

The session data is stored on the top of cookies and signed by the server
cryptographically.
In the flask, a session object is used to track the session data which is a dictionary
object that contains a key-value pair of the session variables and their associated
values.

C++ vs Java

The following syntax is used to set the session variable to a specific value on the server.

1. Session[<variable-name>] = <value>

To remove a session variable, use the pop() method on the session object and mention
the variable to be removed.

1. session.pop(<variable-name>, none)

Let's see a simple example to understand how can we set and get the session variable.

Example

1. from flask import *


2. app = Flask(__name__)
3. app.secret_key = "abc"
4.
5. @app.route('/')
6. def home():
7. res = make_response("<h4>session variable is set, <a href='/get'>Get Varia
ble</a></h4>")
8. session['response']='session#1'
9. return res;
10.
11. @app.route('/get')
12. def getVariable():
13. if 'response' in session:
14. s = session['response'];
15. return render_template('getsession.html',name = s)
16.
17. if __name__ == '__main__':
18. app.run(debug = True)

getsession.html
1. <html>
2. <head>
3. <title>getting the session</title>
4. </head>
5. <body>
6. <p>The session is set with value: <strong>{{name}}</strong></p>
7. </body>
8. </html>

Login Application in the flask using Session


Here, we will create a login application in the flask where the following home page is
shown to the user.
If we click the view_profile directly without login, then it will show some warning as
we can't visit the profile directly without login.

If we visit the login page then, the application shows the login page to the user where
the user is prompted to enter the email id and password. Here, our application
redirects the user to success page for any random valid email id and password as given
in the below image.
On submit, the user is redirected to the success.html as given in the below image.

Now, if we visit the profile page, it will show the message with the session name as the
session variable 'email' is set to its value.
As we now logged in to the application, we can now view the user's profile without
logging in to the application as we have already logged in to the application. To test
this, go back to the home page and click the link view_profile, we will get the result as
shown in the above image.

The application facilitates us to log out the session, for this purpose, go back to the
home page click on the logout button. It will destroy all the session variables set for
this particular server.

Now, the user has to login again with the email and password to view the profile on
the application.

This is a simple login application built using python flask that implements the session.
Here, the flask script login.py acts like the main controller which contains the view
functions (home(), login(), success(), logout(), and profile()) which are associated with
the URL mappings (/, /login, /success, /logout, /profile) respectively.

login.py

1. from flask import *


2. app = Flask(__name__)
3. app.secret_key = "ayush"
4.
5. @app.route('/')
6. def home():
7. return render_template("home.html")
8.
9. @app.route('/login')
10. def login():
11. return render_template("login.html")
12.
13. @app.route('/success',methods = ["POST"])
14. def success():
15. if request.method == "POST":
16. session['email']=request.form['email']
17. return render_template('success.html')
18.
19. @app.route('/logout')
20. def logout():
21. if 'email' in session:
22. session.pop('email',None)
23. return render_template('logout.html');
24. else:
25. return '<p>user already logged out</p>'
26.
27. @app.route('/profile')
28. def profile():
29. if 'email' in session:
30. email = session['email']
31. return render_template('profile.html',name=email)
32. else:
33. return '<p>Please login first</p>'
34.
35. if __name__ == '__main__':
36. app.run(debug = True)

home.html

1. <html>
2. <head>
3. <title>home</title>
4. </head>
5. <body>
6. <h3>Welcome to the website</h3>
7. <a href = "/login">login</a><br>
8. <a href = "/profile">view profile</a><br>
9. <a href = "/logout">Log out</a><br>
10. </body>
11. </html>

login.html

1. <html>
2. <head>
3. <title>login</title>
4. </head>
5. <body>
6. <form method = "post" action = "http://localhost:5000/success">
7. <table>
8. <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>
9. <tr><td>Password</td><td><input type = 'password' name = 'pass'
></td></tr>
10. <tr><td><input type = "submit" value = "Submit"></td></tr>
11. </table>
12. </form>
13. </body>
14. </html>

success.html

1. <html>
2. <head>
3. <title>success</title>
4. </head>
5. <body>
6. <h2>Login successful</h2>
7. <a href="/profile">View Profile</a>
8. </body>
9. </html>

logout.html

1. <html>
2. <head>
3. <title>logout</title>
4. </head>
5.
6. <body>
7. <p>logout successful, click <a href="/login">here</a> to login again</p>
8. </body>
9.
10. </html>

Flask File Uploading


File uploading is the process of transmitting the binary or normal files to the server.
Flask facilitates us to upload the files easily. All we need to have an HTML form with
the encryption set to multipart/form-data.

The server-side flask script fetches the file from the request object using request.files[]
Object. On successfully uploading the file, it is saved to the desired location on the
server.

The uploaded file is saved to the temporary directory of the server for a while before
it is saved to some desired location. The name of the destination file can be obtained
using the following syntax.

1. name = request.files['file'].filename

However, we can mention the path of the folder where the file is to be uploaded to
the server and the maximum size of the uploaded file. This all can be done in the
configuration settings of the flask object.
SN Syntax Description

1 app.config['UPLOAD_FOLDER'] It is used to mention the upload folder.

2 app.config['MAX_CONTENT- It is used to mention the maximum size of the file to be


PATH'] uploaded.

Consider the following example to upload a file from the local file system to the server.

Example
In this example, we will provide a file selector(file_upload_form.html) to the user where
the user can select a file from the file system and submit it to the server.

At the server side, the file is fetched using the request.files['file'] object and saved to
the location on the server.

Since we are using the development server on the same device, hence the file will be
uploaded to the directory from where the flask script upload.py is executed.

upload.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/')
5. def upload():
6. return render_template("file_upload_form.html")
7.
8. @app.route('/success', methods = ['POST'])
9. def success():
10. if request.method == 'POST':
11. f = request.files['file']
12. f.save(f.filename)
13. return render_template("success.html", name = f.filename)
14.
15. if __name__ == '__main__':
16. app.run(debug = True)
file_upload_form.html

1. <html>
2. <head>
3. <title>upload</title>
4. </head>
5. <body>
6. <form action = "/success" method = "post" enctype="multipart/form-data">
7. <input type="file" name="file" />
8. <input type = "submit" value="Upload">
9. </form>
10. </body>
11. </html>

success.html

1. <html>
2. <head>
3. <title>success</title>
4. </head>
5. <body>
6. <p>File uploaded successfully</p>
7. <p>File Name: {{name}}</p>
8. </body>
9. </html>

An HTML form is shown to the user so that the user can browse the file system for the
file which will be uploaded to the development server.
Here, the user has chosen a file named as galexy.jpg which will be uploaded to the
server.

The below snapshot is generated for the URL localhost:5000/success. On successfully


uploading the file, a success message is shown to the user with the name of the
uploaded file.
We can confirm this by checking into the directory where the upload.py is located as
given in the below image.

Flask Redirect and Errors


Flask class provides the redirect() function which redirects the user to some specified
URL with the specified status code.

An HTTP status code is a response from the server to the request of the browser. When
we visit a website, a request is sent to the server, and the server then responds to the
browser's request with a three-digit code: the HTTP status code. This status code also
represents the error.
The syntax to use the redirect() function is given below.

1. Flask.redirect(<location>,<status-code>, <response> )

It accepts the following parameters.

Prime Ministers of India | List of Prime Minister of India (1947-2020)

SN Parameter Description

1 location It is the URL where the response will be redirected.

2 status code It is the status code that is sent to the browser's header along with the response
from the server.

3 response It is the instance of the response that is used in the project for future requirements.

Consider the following example where the user is redirected to the success page with
the HTTP status code 302 (found) on the successful login otherwise; the user reverts
to this page only.

Example
login.py

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/')
5. def home ():
6. return render_template("home.html")
7.
8. @app.route('/login')
9. def login():
10. return render_template("login.html");
11.
12. @app.route('/validate', methods = ["POST"])
13. def validate():
14. if request.method == 'POST' and request.form['pass'] == 'jtp':
15. return redirect(url_for("success"))
16. return redirect(url_for("login"))
17.
18. @app.route('/success')
19. def success():
20. return "logged in successfully"
21.
22. if __name__ == '__main__':
23. app.run(debug = True)

home.html

1. <html>
2. <head>
3. <title>home</title>
4. </head>
5. <body>
6. <h3>Welcome to the website</h3>
7. <a href = "/login">login</a><br>
8. </html>

login.html

1. <html>
2. <head>
3. <title>login</title>
4. </head>
5. <body>
6. <form method = "post" action = "http://localhost:5000/validate">
7. <table>
8. <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>
9. <tr><td>Password</td><td><input type = 'password' name = 'pass'
></td></tr>
10. <tr><td><input type = "submit" value = "Submit"></td></tr>
11. </table>
12. </form>
13. </body>
14. </html>
In the above example, the URL '/' contains a link to the login page as shown in the
below image.

The login page contains shown in the below image prompts the user to enter the email
and password, and the submit button redirects the user to URL /validate.

In this case, as I have entered a random password not equal to 'jtp' therefore, the user
reverts to this page (login page) only.

However, the user is redirected to the URL /success only if the password entered by
the user to 'jtp'. The URL http://localhost:5000/success (generated on the successful
login) is shown in the below image.
Standard HTTP Codes
The following HTTP codes are standardized.

o HTTP_300_MULTIPLE_CHOICES
o HTTP_301_MOVED_PERMANENTLY
o HTTP_302_FOUND
o HTTP_303_SEE_OTHER
o HTTP_304_NOT_MODIFIED
o HTTP_305_USE_PROXY
o HTTP_306_RESERVED
o HTTP_307_TEMPORARY_REDIRECT

The default status code is HTTP_302_FOUND.

The abort() function


The abort() function is used to handle the cases where the errors are involved in the
requests from the client side, such as bad requests, unauthorized access and many
more. However, the error code is to be mentioned due to which the error occurred.

The syntax to use the abort() function is given below.

1. Flask.abort(code)

We can mention the following error codes depending upon the specified errors.
o 400: for bad requests
o 401: for unauthorized access
o 403: for forbidden
o 404: for not found
o 406: for not acceptable
o 415: for unsupported media types
o 429: for too many requests

Let's modify the script login.py from the above example and use the abort() function
with the error code 401 (for unauthorized access) in the case of any random password
entered by the user.

Example

1. from flask import *


2. app = Flask(__name__)
3.
4. @app.route('/')
5. def home ():
6. return render_template("home.html")
7.
8. @app.route('/login')
9. def login():
10. return render_template("login.html");
11.
12. @app.route('/validate', methods = ["POST"])
13. def validate():
14. if request.method == 'POST' and request.form['pass'] == 'jtp':
15. return redirect(url_for("success"))
16. else:
17. abort(401)
18.
19. @app.route('/success')
20. def success():
21. return "logged in successfully"
22.
23. if __name__ == '__main__':
24. app.run(debug = True)

It will generate the following result in the case of the wrong password.

Here, we have used the error code 401 since the user has requested the unauthorized
access to the resource. We can change it to any code depending upon the error case.

Flask Flashing
In the web applications, there are scenarios where the developer might need to flash
the messages to provide feedback to the users for the behavior of the application in
different cases.

Flask provides the flash() method, in the same way, the client-side scripting language
like JavaScript uses the alerts or the python GUI framework Tkinter uses the dialogue
box or the message box.

The flash() method is used to generate informative messages in the flask. It creates a
message in one view and renders it to a template view function called next.

In other words, the flash() method of the flask module passes the message to the next
request which is an HTML template. The syntax to use the flash() method is given
below.

History of Java

1. flash(message, category)
It accepts the following parameters.

o message: it is the message to be flashed to the user.


o Category: It is an optional parameter. Which may represent any error, information, or
warning.

The messages are generated in the flask script using the flash() method of flask
module. These messages need to be extracted in the template from the session. For
this purpose, the method get_flashed_messages() is called in the HTML template.

The syntax to use this method is given below.

1. get_flashed_messages(with_categories, category_filter)

It accepts the following parameters.

o with_categories: This parameter is optional and used if the messages have the
category.
o category_filter: This parameter is also optional. It is useful to display only the specified
messages.

Example
The example contains the flask and HTML scripts for server and client-side scripting.

The python script flashes the messages and redirects the user to the different HTML
script depending upon the successful and unsuccessful login of the user.

flashing.py

1. from flask import *


2. app = Flask(__name__)
3. app.secret_key = "abc"
4.
5. @app.route('/index')
6. def home():
7. return render_template("index.html")
8.
9. @app.route('/login',methods = ["GET","POST"])
10. def login():
11. error = None;
12. if request.method == "POST":
13. if request.form['pass'] != 'jtp':
14. error = "invalid password"
15. else:
16. flash("you are successfuly logged in")
17. return redirect(url_for('home'))
18. return render_template('login.html',error=error)
19.
20.
21. if __name__ == '__main__':
22. app.run(debug = True)

index.html

1. <html>
2. <head>
3. <title>home</title>
4. </head>
5. <body>
6. {% with messages = get_flashed_messages() %}
7. {% if messages %}
8. {% for message in messages %}
9. <p>{{ message }}</p>
10. {% endfor %}
11. {% endif %}
12. {% endwith %}
13. <h3>Welcome to the website</h3>
14. <a href = "{{ url_for('login') }}">login</a>
15. </body>
16. </html>

login.html

1. <html>
2. <head>
3. <title>login</title>
4. </head>
5. <body>
6. {% if error %}
7. <p><strong>Error</strong>: {{error}}</p>
8. {% endif %}
9.
10. <form method = "post" action = "/login">
11. <table>
12. <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>
13. <tr><td>Password</td><td><input type = 'password' name = 'pass'
></td></tr>
14. <tr><td><input type = "submit" value = "Submit"></td></tr>
15. </table>
16. </form>
17. </body>
18. </html>

The URL /index shows the following template (index.html) which contains the code for
flashing the message (if any). The link login redirects the user to the URL /login.

The following page shows the template login.html which prompts the user to enter
the email id and password. Here, if the user enters any random password other than
"jtp", it can not be able to login to the application.
The following URL illustrates the first case where the user has entered the wrong
password. The script login.py generates an error message as "invalid password" and
redirects the user to this page itself.

The following URL illustrates the second case where the user has entered the correct
password as "jtp" and therefore the script flashing.py flashes the success message and
redirect the user to the URL http:localhost:5000/index.
Flask-Mail Extension
Considering the fact that flask is a micro framework, it has its limitations in providing
the facilities to the developer. Although, there are several extensions to the flask like
Mail, WTF, SQLite, SQLAlchemy, etc. which facilitates the developer to provide some
basic facilities to the user.

In this section of the tutorial, we will study one of the most common extensions to the
flask, i.e., Flask-Mail.

A web application must be capable of sending emails to users. The flask-mail extension
provides the simple interface for the developer and the email server to send the email
through the web application.

For this purpose, we must install the flask-mail extension using the pip installer.

10 Sec

Hello Java Program for Beginners

pip install Flask-Mail


The application must configure the flask-mail for the following parameters.

SN Parameter Description
1 MAIL_SERVER It represents the name or IP address of the email server. The default
is localhost.

2 MAIL_PORT It represents the port number of the server. Default port number is
25.

3 MAIL_USE_TLS It is used to enable or disable the transport security layer description.


The default is false.

4 MAIL_USE_SSL It is used to enable or disable the secure socket layer description.


The default value is false.

5 MAIL_DEBUG It is used to provide the debug support to the mail application. The
default value is None.

6 MAIL_USERNAME It represents the user name of the sender. The default value is None.

7 MAIL_PASSWORD It represents the password of the server email id. The default value
is None.

8 MAIL_DEFAULT_SENDER It is used to set the default sender id for the multiple emails. The
default value is None.

9 MAIL_MAX_EMAILS It is used to set the maximum number of email to be sent. The


default value is None.

10 MAIL_SUPPRESS_SEND Sending the mail is suppressed if the app.testing is set to the true.

11 MAIL_ASCII_ATTACHMENTS If it is set to true, attached file names are converted to ASCII. The
default is False.

Flask-Mail Module Classes


There are several classes which are important to send emails using the python flask
web application.

Message class
The Message class binds the email message into the one simple Message class instance
so that the important methods like attach() can be called on this instance. The syntax
to instantiate the Message class is given below.

1. Flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, reply-


to, date, charset, extra-headers, mail-options, rcpt_options)

There are the following methods that can be called on the Message class object.

o attach(filename, content_type, data, disposition): this method is used to send the


attachments with the message. This method accepts the name of the file, MIME type
of file, raw file data, and the content disposition.
o add_recipient(): It is used to add the recipient to the message.

Mail Class
Mail class object is used to send the email. The Mail class is instantiated by passing the
application object to the Mail class constructor as given below.

1. Flask-mail.Mail(app=None)

The Mail class contains the following methods.

o send(): It sends the message object content to the recipient.


o connect(): It is used to open the connection with mail host.
o send_message(): It is used to send the message object.

Process of sending email using flask web application


There are the following steps involved in sending an email using the flask web
application.

Step 1: import the required module like flask-mail, flask using from-import statement.

1. from flask import *


2. from flask-mail import *

Step 2: Configure the Flask Mail.

1. app.config['MAIL_SERVER']='smtp.gmail.com'
2. app.config['MAIL_PORT']=465
3. app.config['MAIL_USERNAME'] = 'admin@gmail.com'
4. app.config['MAIL_PASSWORD'] = '******'
5. app.config['MAIL_USE_TLS'] = False
6. app.config['MAIL_USE_SSL'] = True

Step 3: Instantiate the Mail class.

1. mail = Mail(app)

Step 4: Instantiate the Message class with the desired attributes in the function
mapped by some URL rule.

1. @app.route('/')
2. def index():
3. msg = Message('subject', sender = 'admin@gmail.com', recipients=['userna
me@gmail.com'])
4. msg.body = 'hi, this is the mail sent by using the flask web application'
5. return "Mail Sent, Please check the mail id"

Example
The following example contains a python script where we send the email to the given
email id.

Mailer.py

1. from flask import *


2. from flask-mail import *
3.
4. app = Flask(__name__)
5.
6. #Flask mail configuration
7. app.config['MAIL_SERVER']='smtp.gmail.com'
8. app.config['MAIL_PORT']=465
9. app.config['MAIL_USERNAME'] = 'admin@gmail.com'
10. app.config['MAIL_PASSWORD'] = '******'
11. app.config['MAIL_USE_TLS'] = False
12. app.config['MAIL_USE_SSL'] = True
13.
14. #instantiate the Mail class
15. mail = Mail(app)
16.
17. #configure the Message class object and send the mail from a URL
18. @app.route('/')
19. def index():
20. msg = Message('subject', sender = 'admin@gmail.com', recipients=['username@g
mail.com'])
21. msg.body = 'hi, this is the mail sent by using the flask web application'
22. return "Mail Sent, Please check the mail id"
23.
24. if __name__ == '__main__':
25. app.run(debug = True)

Our Python web application attempts to log in to the email_id mentioned in the script.
This attempt can be blocked if you have not allowed access to the less secure
application to your Google account. In this case, visit the
link https://www.google.com/settings/security/lesssecureapps and allow access for the
less secure app.

Email verification in flask using OTP


In the modern web applications, sometimes the email is verified using the one time
password generated randomly by the program. In this example, we will create a python
script that accepts the user's email id as the input from the user and send an email
containing the automatically (randomly) generated (4-digit) one-time password.

For the successful verification of the email-id, the user is required to enter the otp sent
to the mentioned email id. If the OTP entered by the user is matched with the randomly
generated OTP, then the email-id is successfully verified, and the success message is
shown to the user otherwise the verification is failed, and the failure message is shown
to the user.

In the below example, A flask script Mailer.py acts as a controller with the functions
verify() and validate() associated with the URL /verify and /validate respectively. These
functions also render the HTML templates to accept the input from the user and show
the result depending upon the email verification.

Mailer.py

1. from flask import *


2. from flask_mail import *
3. from random import *
4.
5. app = Flask(__name__)
6. mail = Mail(app)
7.
8. app.config["MAIL_SERVER"]='smtp.gmail.com'
9. app.config["MAIL_PORT"] = 465
10. app.config["MAIL_USERNAME"] = 'username@gmail.com'
11. app.config['MAIL_PASSWORD'] = '*************'
12. app.config['MAIL_USE_TLS'] = False
13. app.config['MAIL_USE_SSL'] = True
14.
15. mail = Mail(app)
16. otp = randint(000000,999999)
17.
18. @app.route('/')
19. def index():
20. return render_template("index.html")
21.
22. @app.route('/verify',methods = ["POST"])
23. def verify():
24. email = request.form["email"]
25.
26. msg = Message('OTP',sender = 'username@gmail.com', recipients = [email])
27. msg.body = str(otp)
28. mail.send(msg)
29. return render_template('verify.html')
30.
31. @app.route('/validate',methods=["POST"])
32. def validate():
33. user_otp = request.form['otp']
34. if otp == int(user_otp):
35. return "<h3>Email verified successfully</h3>"
36. return "<h3>failure</h3>"
37.
38. if __name__ == '__main__':
39. app.run(debug = True)
index.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>index</title>
5. </head>
6. <body>
7. <form action = "http://localhost:5000/verify" method = "POST">
8. Email: <input type="email" name="email">
9. <input type = "submit" value="Submit">
10. </form>
11. </body>
12. </html>

verify.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>OTP Verification</title>
5. </head>
6.
7. <body>
8.
9. <form action = "/validate" method="post">
10.
11. <h4> One-
time password has been sent to the email id. Please check the email for the ve
rification.</h4>
12.
13. Enter OTP: <input type="text" name="otp">
14.
15. <input type="submit" value="Submit">
16.
17. </form>
18. </body>
19. </html>
The following template prompts the user to enter the email_id and password. The
script Mailer.py will send an email containing the one-time password to the email id
entered by the user.

Now, the user is prompted to enter the OTP sent to the specified email.

The function validate() matches the OTP entered by the user with the one which was
randomly generated and mailed to the user's email id. In this case, OTP is matched;
therefore the user will get the success message as shown in the below image.
Bulk Emails
In the above example, the script sends only one email to the user for the email
verification. In web applications, there are cases where we need to send multiple emails
or the bulk of emails in a single connection.

In that case, we can make use of the python with the statement which closes the
connection object automatically once all the emails are sent.

Example

1. from flask import *


2. from flask_mail import *
3.
4. app = Flask(__name__)
5.
6. app.config["MAIL_SERVER"]='smtp.gmail.com'
7. app.config["MAIL_PORT"] = 465
8. app.config["MAIL_USERNAME"] = 'admin@gmail.com'
9. app.config['MAIL_PASSWORD'] = '******'
10. app.config['MAIL_USE_TLS'] = False
11. app.config['MAIL_USE_SSL'] = True
12.
13. users = [{'name':'john','email':'john@gmail.com'},{'name':'Ayush','email':'ayush
@javatpoint.com'},{'name':'david','email':'david@gmail.com'}]
14.
15. mail = Mail(app)
16.
17. @app.route("/")
18. def index():
19. with mail.connect() as con:
20. for user in users:
21. message = "hello %s" %user['name']
22. msgs = Message(recipients=[user['email']],body = message, subject = 'hello',
sender = 'david@gmail.com')
23. con.send(msgs)
24. return "Sent"
25. if __name__ == "__main__":
26. app.run(debug = True)

Adding attachments with the mail


Flask facilitates us to send the attachment with the mail. For this purpose, we need to
open the resource using the open_resource() method of Flask class. Then, we can use
the python-with statement to attach the resource with the message. The with
statement closes the resource automatically when the work is done.

The following syntax is used to send the attachments with the mail.

1. with app.open_resource("image.png") as fp:


2. msg.attach("image.png", "image/png", fp.read())

The flask script which sends the attachment with the mail is given below.

mailer_attach.py

1. from flask import *


2. from flask_mail import *
3.
4. app = Flask(__name__)
5.
6. app.config["MAIL_SERVER"]='smtp.gmail.com'
7. app.config["MAIL_PORT"] = 465
8. app.config["MAIL_USERNAME"] = 'admin@gmail.com'
9. app.config['MAIL_PASSWORD'] = '********'
10. app.config['MAIL_USE_TLS'] = False
11. app.config['MAIL_USE_SSL'] = True
12.
13. mail = Mail(app)
14.
15. @app.route("/")
16. def index():
17. msg = Message(subject = "hello", body = "hello", sender = "admin@gmail.c
om", recipients = ["ayush@javatpoint.com"])
18. with app.open_resource("/home/javatpoint/Desktop/galexy.jpg") as fp:
19. msg.attach("galexy.jpg","image/png",fp.read())
20. mail.send(msg)
21. return "sent"
22.
23. if __name__ == "__main__":
24. app.run(debug = True)

Flask SQLite
Flask can make use of the SQLite3 module of the python to create the database web
applications. In this section of the tutorial, we will create a CRUD (create - read - update
- delete) application.

Since we have given a detailed overview of how a python application can interact with
the SQLite database, to revise the concept, please visit the link: Python SQLite.

CRUD Application in flask


Here, we will manage the employee information in the SQLite database using a flask
script to which the admin can interact. For this purpose, database employee.db
contains the related tables whereas the table Employees contains information about
the employees.

First, let us create a database employee.DB and the table Employees in SQLite using
the following python script.

EmoloyeeDB.py

1. import sqlite3
2.
3. con = sqlite3.connect("employee.db")
4. print("Database opened successfully")
5.
6. con.execute("create table Employees (id INTEGER PRIMARY KEY AUTOINCREMENT, n
ame TEXT NOT NULL, email TEXT UNIQUE NOT NULL, address TEXT NOT NULL)")
7.
8. print("Table created successfully")
9.
10. con.close()

To build a CRUD application in the flask, we must focus on the view functions (to take
the input) and the controllers (to save that data into the database).

Let us look at the view function: index() which is associated with the URL (/). It renders
a template index.html.

1. @app.route("/")
2. def index():
3. return render_template("index.html");

The following HTML template (index.html) is considered as the home page of our
application. It provides the links using which we can add, view, and delete the data
stored in the database.

index.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>home</title>
5. </head>
6. <body>
7. <h2>Hi, welcome to the website</h2>
8. <a href="/add">Add Employee</a><br><br>
9. <a href ="/view">List Records</a><br><br>
10. <a href="/delete">Delete Record</a><br><br>
11. </body>
12. </html>
The view function add() which is associated with the URL (/add) renders the
template add.html given below. It provides the form to enter the employee
information.

add.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Add Employee</title>
5. </head>
6. <body>
7. <h2>Employee Information</h2>
8. <form action = "/savedetails" method="post">
9. <table>
10. <tr><td>Name</td><td><input type="text" name="name"></td></tr>
11. <tr><td>Email</td><td><input type="email" name="email"></td></t
r>
12. <tr><td>Address</td><td><input type="text" name="address"></td></tr>
13. <tr><td><input type="submit" value="Submit"></td></tr>
14. </table>
15. </form>
16. </body>
17. </html>

All the details entered by the Admin is posted to the URL /savedetails which is
associated with the function saveDetails(). The function saveDetails() is given below
which contains the code for extracting the data entered by the admin and save that
data into the table Employees.

It also generates the message depending upon the cases in which the data is
successfully inserted, or some error occurred.

1. @app.route("/savedetails",methods = ["POST","GET"])
2. def saveDetails():
3. msg = "msg"
4. if request.method == "POST":
5. try:
6. name = request.form["name"]
7. email = request.form["email"]
8. address = request.form["address"]
9. with sqlite3.connect("employee.db") as con:
10. cur = con.cursor()
11. cur.execute("INSERT into Employees (name, email, address) values (?
,?,?)",(name,email,address))
12. con.commit()
13. msg = "Employee successfully Added"
14. except:
15. con.rollback()
16. msg = "We can not add the employee to the list"
17. finally:
18. return render_template("success.html",msg = msg)
19. con.close()

It renders a template success.html to display the message to the admin. It also


contains a link to view the records entered by the user.

success.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>save details</title>
5. </head>
6. <body>
7. <h3>Hi Admin, {{msg}}</h3>
8. <a href="/view">View Employees</a>
9. </body>
10. </html>

The function delete() is associated to the URL /delete. It renders an HTML


template delete.html which provides the form to the admin that prompts to enter the
Employee_Id of which the records are to be deleted. It also contains a link to the
/view URL that shows all the records to the admin.

The HTML template delete.html is given below.

delete.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>delete record</title>
5. </head>
6. <body>
7.
8. <h3>Remove Employee from the list</h3>
9.
10. <form action="/deleterecord" method="post">
11. Employee Id <input type="text" name="id">
12. <input type="submit" value="Submit">
13. </form>
14. </body>
15. </html>

The Employee_Id entered by the admin is posted to the URL /deleterecord which
contains the python code to establish the connection to the database and then delete
all the records for the specified Employee ID. The URL /deleterecord is associated with
the function deleterecord() which is given below.

1. @app.route("/deleterecord",methods = ["POST"])
2. def deleterecord():
3. id = request.form["id"]
4. with sqlite3.connect("employee.db") as con:
5. try:
6. cur = con.cursor()
7. cur.execute("delete from Employees where id = ?",id)
8. msg = "record successfully deleted"
9. except:
10. msg = "can't be deleted"
11. finally:
12. return render_template("delete_record.html",msg = msg)

The function deleterecord() generates a message depending upon the scenario


whether the data is successfully deleted or some error occurred. It renders an HTML
template delete_record.html to show the message to the admin.

delete_record.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>delete record</title>
5. </head>
6. <body>
7. <h3>{{msg}}</h3>
8. <a href="/view">View List</a>
9. </body>
10. </html>

The template delete_record.html contains a link to the URL /view which shows the
Employee records to the admin.

It is associated with the function view() which establishes the connection to the
database, fetch all the information and pass that information to the HTML
template view.html to display on the client side browser.

1. app.route("/view")
2. def view():
3. con = sqlite3.connect("employee.db")
4. con.row_factory = sqlite3.Row
5. cur = con.cursor()
6. cur.execute("select * from Employees")
7. rows = cur.fetchall()
8. return render_template("view.html",rows = rows)

The HTML template view.html which shows all the information on the browser is
given below.

view.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>List</title>
5. </head>
6. <body>
7.
8. <h3>Employee Information</h3>
9. <table border=5>
10. <thead>
11. <td>ID</td>
12. <td>Name</td>
13. <td>Email</td>
14. <td>Address</td>
15. </thead>
16.
17. {% for row in rows %}
18.
19. <tr>
20. <td>{{row["id"]}}</td>
21. <td>{{row["name"]}}</td>
22. <td>{{row["email"]}}</td>
23. <td>{{row["address"]}}</td>
24. </tr>
25.
26. {% endfor %}
27. </table>
28. <br><br>
29.
30. <a href="/">Go back to home page</a>
31.
32. </body>
33. </html>

The full python script is given below.

crud.py

1. from flask import *


2. import sqlite3
3.
4. app = Flask(__name__)
5.
6. @app.route("/")
7. def index():
8. return render_template("index.html");
9.
10. @app.route("/add")
11. def add():
12. return render_template("add.html")
13.
14. @app.route("/savedetails",methods = ["POST","GET"])
15. def saveDetails():
16. msg = "msg"
17. if request.method == "POST":
18. try:
19. name = request.form["name"]
20. email = request.form["email"]
21. address = request.form["address"]
22. with sqlite3.connect("employee.db") as con:
23. cur = con.cursor()
24. cur.execute("INSERT into Employees (name, email, address) values (?,?,?)",(n
ame,email,address))
25. con.commit()
26. msg = "Employee successfully Added"
27. except:
28. con.rollback()
29. msg = "We can not add the employee to the list"
30. finally:
31. return render_template("success.html",msg = msg)
32. con.close()
33.
34. @app.route("/view")
35. def view():
36. con = sqlite3.connect("employee.db")
37. con.row_factory = sqlite3.Row
38. cur = con.cursor()
39. cur.execute("select * from Employees")
40. rows = cur.fetchall()
41. return render_template("view.html",rows = rows)
42.
43.
44. @app.route("/delete")
45. def delete():
46. return render_template("delete.html")
47.
48. @app.route("/deleterecord",methods = ["POST"])
49. def deleterecord():
50. id = request.form["id"]
51. with sqlite3.connect("employee.db") as con:
52. try:
53. cur = con.cursor()
54. cur.execute("delete from Employees where id = ?",id)
55. msg = "record successfully deleted"
56. except:
57. msg = "can't be deleted"
58. finally:
59. return render_template("delete_record.html",msg = msg)
60.
61. if __name__ == "__main__":
62. app.run(debug = True)

Run the python script EmployeeDB.py to create the database and the Employees table
using the following command on the terminal.

1. $ python EmployeeDB.py

Now, run the flask script crud.py and visit https://localhost:5000 on the browser.
Click on the link Add Employee to add a new employee to the database.

Fill this form and click submit to save the details into the database.
Now, click on the view employee to list all the employees of the database. Till now,
we have only one employee in the list as shown in the below image.

Click on the link given at the bottom of the page to go back to the home page.
Now click on the Delete Record to check how the script deletes the record for the
specific employee_id.

Enter any employee id for which the records are to be deleted. Here, we must notice
that if the entered Employee Id doesn't exist in the database, then an error message
will be displayed. Let's enter the employee id 1 to delete the employee john from the
database.
Hence, the record for the employee with id 1 is deleted. Here, we can confirm this by
viewing the list. Click View List to view the list.

Flask SQLAlchemy
Flask SQLAlchemy is an ORM tool which establishes the relationship between the
objects and the tables of the relational databases.

The mapping between the both is important because the python is capable of storing
the data in the form of objects whereas the database stores the data in the form of
relational tables, i.e. the collection of rows and columns.

The object-relational mapping is the technique of storing python objects into the
database tables without writing the raw SQL queries.

In this section of the tutorial, we will create a small web application using flask-
sqlalchemy ORM techniques.

Install flask-sqlalchemy:
To create a web application using the flask ORM techniques, we must need to install
flask-sqlalchemy using pip installer.

1. $ pip install flask-sqlalchemy

To confirm the installation, try to import the module on the python shell; if it is
successfully imported, the installation is successful.

1. $ import flask_sqlalchemy

Creating a small web application using flask-sqlalchemy


In this section of the tutorial, we will create a CRUD Application in python using ORM
SQLAlchemy.

Example
add.html

1. <!DOCTYPE html>
2. <html>
3. <body>
4. <h3>Add new Employee</h3>
5. <hr/>
6.
7. {%- for category, message in get_flashed_messages(with_categories = true
) %}
8. <div class = "alert alert-danger">
9. {{ message }}
10. </div>
11. {%- endfor %}
12.
13. <form action = "{{ request.path }}" method = "post">
14. <label for = "name">Name</label><br>
15. <input type = "text" name = "name" placeholder = "Name" /><br>
16.
17. <label for = "salary">Salary</label><br>
18. <input type = "text" name = "salary" placeholder = "salary" /><br>
19.
20. <label for = "age">Age</label><br>
21. <textarea name = "age" placeholder = "age"></textarea><br>
22.
23. <label for = "PIN">Pin</label><br>
24. <input type = "text" name = "pin" placeholder = "pin" /><br>
25.
26. <input type = "submit" value = "Submit" />
27. </form>
28. </body>
29. </html>

list_employees.html

1. <!DOCTYPE html>
2. <html lang = "en">
3. <head><title>Home</title></head>
4. <body>
5. <h3>
6. <a href = "{{ url_for('list_employees') }}">Employee Management System</a>
7. </h3>
8.
9. <hr/>
10. {%- for message in get_flashed_messages() %}
11. {{ message }}
12. {%- endfor %}
13.
14. <h3>Employees List</h3>
15. <table border="2" padding = "5">
16. <thead>
17. <tr>
18. <th>Name</th>
19. <th>Salary</th>
20. <th>Age</th>
21. <th>Pin</th>
22. </tr>
23. </thead>
24.
25. <tbody>
26. {% for employee in Employees %}
27. <tr>
28. <td>{{ employee.name }}</td>
29. <td>{{ employee.salary }}</td>
30. <td>{{ employee.age }}</td>
31. <td>{{ employee.pin }}</td>
32. </tr>
33. {% endfor %}
34. </tbody>
35. </table>
36. <br><br>
37. <a href="{{ url_for('addEmployee') }}">Add New Employee</a>
38. </body>
39. </html>

app.py

1. from flask import Flask, request, flash, url_for, redirect, render_template


2. from flask_sqlalchemy import SQLAlchemy
3.
4. app = Flask(__name__)
5. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///employees.sqlite3'
6. app.config['SECRET_KEY'] = "secret key"
7.
8. db = SQLAlchemy(app)
9.
10. class Employees(db.Model):
11. id = db.Column('employee_id', db.Integer, primary_key = True)
12. name = db.Column(db.String(100))
13. salary = db.Column(db.Float(50))
14. age = db.Column(db.String(200))
15. pin = db.Column(db.String(10))
16.
17. def __init__(self, name, salary, age,pin):
18. self.name = name
19. self.salary = salary
20. self.age = age
21. self.pin = pin
22.
23. @app.route('/')
24. def list_employees():
25. return render_template('list_employees.html', Employees = Employees.query
.all() )
26.
27. @app.route('/add', methods = ['GET', 'POST'])
28. def addEmployee():
29. if request.method == 'POST':
30. if not request.form['name'] or not request.form['salary'] or not request.form['age'
]:
31. flash('Please enter all the fields', 'error')
32. else:
33. employee = Employees(request.form['name'], request.form['salary'],
34. request.form['age'], request.form['pin'])
35.
36. db.session.add(employee)
37. db.session.commit()
38. flash('Record was successfully added')
39. return redirect(url_for('list_employees'))
40. return render_template('add.html')
41.
42. if __name__ == '__main__':
43. db.create_all()
44. app.run(debug = True)

Output:

Click on the link Add new Employee to add a new employee to the database.
Click on Submit, and we will see the newly added employee in the list on the home
page.

Flask-WTF
WTF stands for WT Forms which is intended to provide the interactive user interface
for the user. The WTF is a built-in module of the flask which provides an alternative
way of designing forms in the flask web applications.

Why WTF Useful?


WTF is useful due to the following factors.

o The form elements are sent along with the request object from the client side to the
server side. Server-Side script needs to recreate the form elements since there is no
direct mapping between the client side form elements and the variables to be used at
the server side.

o There is no way to render the HTML form data at real time.

The WT Forms is a flexible, form rendering, and validation library used to provide the
user interface.

Install Flask-WTF
To use the WT forms, we need to install the flask-wtf library which can be installed
using pip installer.

Triggers in SQL (Hindi)

1. $ pip install flask-wtf

The module contains a Form class which is considered as the parent class for all the
form related operations.

The standard form fields are listed below.

SN Form Field Description

1 TextField It is used to represent the text filed HTML form element.

2 BooleanField It is used to represent the checkbox HTML form element.

3 DecimalField It is used to represent the text field to display the numbers with decimals.

4 IntegerField It is used to represent the text field to display the integer values.
5 RadioField It is used to represent the radio button HTML form element.

6 SelectField It is used to represent the select form element.

7 TextAreaField It is used to represent text area form element.

8 PasswordField It is used to take the password as the form input from the user.

9 SubmitField It provides represents the <input type = 'submit' value = 'Submit'> html form
element.

Consider the following example.

Example
In this example, we will create a form using flask WTF module. First, we will create a
form class named as forms.py and we will import those form elements into the
module formexample.py.

forms.py

1. from flask_wtf import Form


2. from wtforms import TextField, IntegerField, TextAreaField, SubmitField, RadioField, S
electField
3. from wtforms import validators, ValidationError
4.
5. class ContactForm(Form):
6. name = TextField("Candidate Name ",[validators.Required("Please enter your name.
")])
7. Gender = RadioField('Gender', choices = [('M','Male'),('F','Female')])
8. Address = TextAreaField("Address")
9.
10. email = TextField("Email",[validators.Required("Please enter your email address."),
11. validators.Email("Please enter your email address.")])
12.
13. Age = IntegerField("Age")
14. language = SelectField('Programming Languages', choices = [('java', 'Java'),('py', 'Pyt
hon')])
15.
16. submit = SubmitField("Submit")

formexample.py

1. from flask import Flask, render_template, request, flash


2. from forms import ContactForm
3. app = Flask(__name__)
4. app.secret_key = 'development key'
5.
6. @app.route('/contact', methods = ['GET', 'POST'])
7. def contact():
8. form = ContactForm()
9. if form.validate() == False:
10. flash('All fields are required.')
11. return render_template('contact.html', formform = form)
12.
13.
14.
15. @app.route('/success',methods = ['GET','POST'])
16. def success():
17. return render_template("success.html")
18.
19. if __name__ == '__main__':
20. app.run(debug = True)

contact.html

1. <!doctype html>
2. <html>
3. <body>
4. <h2 style = "text-align: center;">Registration Form</h2>
5.
6. {% for message in form.name.errors %}
7. <div>{{ message }}</div>
8. {% endfor %}
9.
10. {% for message in form.email.errors %}
11. <div>{{ message }}</div>
12. {% endfor %}
13.
14. <form action = "http://localhost:5000/success" method = "POST">
15.
16. {{ form.hidden_tag() }}
17.
18. <div style = "font-size:18px;" font-weight:bold; margin-left:150px;>
19. {{ form.name.label }}<br>
20. {{ form.name }}
21. <br>
22. {{ form.Gender.label }} {{ form.Gender }}
23. {{ form.Address.label }}<br>
24. {{ form.Address }}
25. <br>
26. {{ form.email.label }}<br>
27. {{ form.email }}
28. <br>
29. {{ form.Age.label }}<br>
30. {{ form.Age }}
31. <br>
32. {{ form.language.label}}<br><br>
33. {{ form.language }}
34. <br><br>
35. {{ form.submit }}
36. </div>
37.
38. </fieldset>
39. </form>
40. </body>
41. </html>

Success.html

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title></title>
5. </head>
6. <body>
7. <h1>Form posted successfully</h1>
8. </body>
9. </html>

Output:

Flask vs. Django


Django and Flask are the web frameworks of Python. As we know, Python is the most
versatile programming language which provides a wide range of web framework. A
web developer has the option to choose from these frameworks. A programmer has
the flexibility to take advantage of the full-stack Python web frameworks. It enhances
the development of complex web applications. Python also provides an option to
choose for micro and lightweight Python web frameworks to build simple web
applications without putting extra time and effort.
Both Django and Flask are the popular frameworks of Python. Each framework has a
unique quality, and we can use it according to the project requirement. Django is a
full-stack web framework, which is used for large and complex web application,
whereas Flask is a lightweight and extensible web framework. Django comes with
the batteries included approach and provides the most amazing functionality.

It is developed based on two POCO projects. The first one is the WSGI (Web Server
Gateway Interface) toolkit and the Jinja2 template engine. Let's have a look at the
brief introduction of Django and flask.

What is Django?
The official definition of Django is, "Django makes it easier to build better Web apps
more quickly and with less code". It is used as a full-stack web framework, and it
performs many tasks on its own. The SQLite database is already inbuilt in this
framework.

Prime Ministers of India | List of Prime Minister of India (1947-2020)

Companies that use Django

The followings are the giant companies that are using Django as a framework:

o Instagram
o Pinterest
o Udemy
o Coursera
o Zapier

What is Flask?
A Flask is a micro web framework written in Python programming language. It provides
flexibility, simplicity, and fine-grained control. The word "micro" means Flask focuses
on keeping the core extensible but straightforward. Working with flask is totally up to
you; it won't decide for you, such as which databases to use. It determines such as
what templating engine to use.

Companies that use Flask:

o Netflix
o Lyft
o Reddit
o Zillow
o MailGui

Comparison between Flask and Django


The web developers need to know the difference between these frameworks because
both frameworks have their own functionalities. Let's have a look at the difference
between these frameworks based on the following points:

Basic Information
Flask was released in 2010, created by Adrian Holovaty and Simon Willison. It was
made by using around 10000 lines of source code. It is used to develop simple web
applications, microservices, and "serverless" platforms. It provides URL routing,
request & error handling, and a development server.

Django was released in 2005 and made by using 240000 lines of source code. It takes
less time & effort to develop a more sophisticated web application. It has a well-
established, huge community that is working towards the enhancement of framework
functionality.

Functionality
Django is a full-stack Python web framework that follows a batteries-included
approach. This approach makes Django easier for a web developer to create basic web
development tasks such as user authentication, URL routing, and database schema
migration. Django also provides a built-in template engine, ORM system, and
bootstrapping tool, which is very helpful in custom web development.
Flask is a simple, lightweight, and easy to use a framework. It includes less built-in
functionality than Django. But it provides facility to a web developer to keep the core
of a web application extensible and straightforward.

Database
Flask doesn't have a database layer, no ORM, supports NoSQL, perform database
operations through SQLAlchemy.

Django provides an ORM system to perform standard database operations without


writing lengthy SQL queries.

Security
Flask has built-in security against the number of common threats such as CSRF, XSS,
and SQL injection.

Django is more secure in comparison with other web frameworks. It consists of a much
smaller codebase, so there is less possibility of getting attacked by an unauthorized
person. To make it more secure, it is needed to evaluate and monitor third-party
libraries and extensions.

Flexibility
Django follows the batteries included approach, which helps developers to build a
variety of web applications without using third-party tools and libraries. But developer
can't make changes to the modules which are provided by Django. We have to build
a web application using these available libraries.

On the other hand, Flask is a micro and extensible web framework. It provides flexibility
to develop the web app according to their requirement by using web development
tools and libraries. Flask is a preferable framework for beginners due to its simple and
customizable architecture.

Built-in Bootstrapping Tool


Django comes with the built-in Bootstrapping tool named –django-admin. Without
using any external input, the developer can build an application easily. We can divide
a single project into several applications. The developers can use django-admin to
create a new application within the project, whereas Flask doesn't consist built-in
bootstrap tool.

Speed
Both Django and Flask work with the same speed. A programming language or web
framework is never responsible for the slow speed. Instead, any website can be slow
because of the database queries, lack of caching, or not using a CDN for front-end
assert.

Features
Django

o It has robust documentation.


o Wide Community across the world.
o It consists of a built-in admin.
o Asynchronous capabilities.
o It is more secure than the other framework.

Flask

o It is a lightweight and extensible WSGI web framework.


o It provides a non-relational database.
o It has a lightweight codebase.

Hello World Program Comparison


Let's display the Hello World on the webpage using both frameworks.

Flask

First, install the flask by using pip install flask command, and it will download the
whole configuration of the flask in your system, create a new file hello_flask.py. The
program is given below:

1. from flask import Flask, escape, request


2.
3. app = Flask(__name__)
4.
5. @app.route('/')
6. def hello():
7. name = request.args.get("name", "World")
8. return f'Hello, {escape(name)}!'
Then start the Flask server from the command line:

1. $ env FLASK_APP=hello_flask.py flask run


2. * Serving Flask app "hello_flask.py"
3. * Environment: production
4. WARNING: This is a development server. Do not use it in a production deployment.
5. Use a production WSGI server instead.
6. * Debug mode: off
7. * Running on http://127.0.0. 1:5000/ (Press CTRL+C to quit)

Click on the above link, and it will print Hello World on a webpage.

Django

First, install the django by using pip install django command; create
a hello_django.py with the following code:

1. from django.conf import settings


2. from django.core.handlers.wsgi import WSGIHandler
3. from django.core.management import execute_from_command_line
4. from django.http import HttpResponse
5. from django.urls import path
6.
7. settings.configure(
8. ROOT_URLCONF=__name__,
9. DEBUG=True,
10. )
11.
12. def hello_world(request):
13. return HttpResponse("Hello, Django!")
14.
15. urlpatterns = [
16. path('', hello_world)
17. ]
18.
19. application = WSGIHandler()
20.
21. if __name__ == "__main__":
22. execute_from_command_line()
Type following command in your terminal:

1. python hello_django.py runserver

1. Watching for file changes with StatReloader


2. Performing system checks...
3.
4. System check identified no issues (0 silenced).
5. December 17, 2019 - 13:48:54
6. Django version 3.0, using settings None
7. Starting development server at http://127.0.0.1:8000/
8. Quit the server with CONTROL-C.

When you click on the above link, it will display the Hello World on a webpage.

You might also like