Python Notes
Python Notes
Python tutorial provides basic and advanced concepts of Python. Our Python
tutorialis designed for beginners and professionals.
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
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 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.
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.
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
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.
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.
Unlike the other programming languages, Python print() function is most unique and
versatile function.
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.
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.
Example -
Output:
By default, the input() function takes the string input but what if we want to take other
data types as an input.
For example -
Example -
Output:
o Python Operators
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 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.
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'>
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.
Example -
<class 'tuple'>
('Apple', 'Mango', 'Orange', 'Banana')
Example -
Output:
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 -
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 -
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:
To get the more information about sets, visit the following resources.
o Python Sets
o Python Set Methods
o Python Function
o Python map() Function
o Python filter() Function
o Python reduce() Function
o Python functool Module
o Python Lambda Function
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.
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 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.
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.
Database Connection
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.
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.
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.
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.
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.
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.
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.
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.
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.
We don't need to declare explicitly variable in Python. When we assign any value to
the variable, that variable is declared automatically.
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'>
1. a = 50
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
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.
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.
Eg:
1. x=y=z=50
2. print(x)
3. print(y)
4. print(z)
Output:
50
50
50
Eg:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c
Output:
5
10
15
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:
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:
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 -
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
Example -
Output:
<class 'int'>
10000000000000000000000000000000000000000001
Python doesn't have any special data type to store larger numbers.
Output:
5
5
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:
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.
o Keywords.
o Identifiers.
o Literals.
o Operators.
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'>
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:
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".
Example - 1
Output:
Example - 2
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.
Output:
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.
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
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 {}.
Output:
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.
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.
Output:
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.
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
A B A and B
6. not - It is a logical operator and inverts the truth value. Consider the following truth
table.
8.8M
157
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
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:
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:
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.
Output:
1
2
3
4
5
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:
22. as - It is used to create a name alias. It provides the user-define name while
importing a module.
Example:
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:
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
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.
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:
a) Single-line String- Strings that are terminated within a single-line are known as
Single line Strings.
Example:
1. text1='hello'
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
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
Output:
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
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.
1. list=['John',678,20.4,'Peter']
2. list1=[456,'Andrew']
3. print(list)
4. print(list + list1)
Output:
Dictionary:
Example
Output:
Tuple:
Example
1. tup = (10,20,"Dev",[2,3,4])
2. print(tup)
Output:
Set:
1. set = {'apple','grapes','guava','papaya'}
2. print(set)
Output:
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.
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
// (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.
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 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.
<= < > >= Comparison operators (less than, less than equal to, greater than, greater
then equal to).
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.
Here we have written comment over the print statement using the hash(#). It will not
affect our print statement.
Example:
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.
Example:
1. def intro():
2. """
3. This function prints Hello Joseph
4. """
5. print("Hi Joseph")
6. intro()
Output:
Hello Joseph
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.
Output:
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.
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.
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.
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.
1. if expression:
2. statement
Example 1
Output:
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
If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
1. if condition:
2. #block of statements
3. else:
4. #another block of statements (else-block)
Output:
Enter your age? 90
You are eligible to vote !!
Output:
The elif statement works like an if-else-if ladder statement in C. It must be succeeded
by an if statement.
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
Output:
Example 2
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.
Advantages of loops
There are the following advantages of loops in Python.OOPs Concepts in Java
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).
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
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
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 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)
1. for i in range(10):
2. print(i,end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9
Output:
Output:
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
Syntax
Output:
Output:
1
22
333
4444
55555
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.
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.
1. while expression:
2. statements
Example:
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:
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
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
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:
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
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:
Example 2
1. var = 1
2. while(var != 2):
3. i = int(input("Enter the number:"))
4. print("Entered value is %d"%(i))
Output:
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.
Output:
The break is commonly used in the cases where we need to break the loop for a given
condition.
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:
1. str = "python"
2. for i in str:
3. if i == 'o':
4. break
5. print(i);
Output:
p
y
t
h
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:
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
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
Syntax
1. #loop statements
2. continue
3. #the code to be skipped
Flow Diagram
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
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:
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.
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.
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
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.
Syntax:
Here, if we check the type of the variable str using a Python script
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.
Output:
Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring
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.
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:
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
1. str = "JAVATPOINT"
2. del str[1]
Output:
1. str1 = "JAVATPOINT"
2. del str1
3. print(str1)
Output:
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 range slice operator. It is used to access the characters from the
specified range.
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
Example
Consider the following example to understand the real use of Python operators.
Output:
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:
2. \\ Backslash print("\\")
Output:
\
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
Output:
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:
center(width ,fillchar) It returns a space padded string with the original string
centred with equal number of left and right spaces.
decode(encoding = 'UTF8', errors = Decodes the string using codec registered for encoding.
'strict')
find(substring ,beginIndex, It returns the index value of the string where substring is
endIndex) found between begin index and end index.
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.
ljust(width[,fillchar]) It returns the space padded strings with the original string
left justified to the given width.
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.
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.
startswith(str,beg=0,end=len(str)) It returns a Boolean value if the string starts with given str
between begin and end.
title() It is used to convert the string into the title-case i.e., The
string meEruT will be converted to Meerut.
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 [].
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:
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.
Output:
True
Output:
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.
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)
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.
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.
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]
Repetition The repetition operator enables the list elements to L1*2 = [1, 2, 3, 4, 1,
2, 3, 4]
be repeated multiple times.
Iteration The for loop is used to iterate over the list elements. for i in l1:
print(i)
Output
1
2
3
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.
Output:
John
David
James
Jonathan
Consider the following example in which, we are taking the elements of the list from
the user and printing the list on the console.
Output:
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:
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.
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:
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:
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:
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.
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
Note: The tuple which is created without using parentheses is also known as tuple
packing.
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.
Example - 1
Output:
Example - 2
Output:
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.
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.
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.
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.
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
SN Function Description
1 cmp(tuple1, It compares two tuples and returns true if tuple1 is greater than tuple2
tuple2) otherwise false.
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.
SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
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.
Output:
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.
Output:
<class 'set'>
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.
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:
In the above code, we can see that set5 consisted of multiple duplicate elements when
we printed it remove the duplicity from the set.
Output:
To add more than one item in the set, Python provides the update() method. It
accepts iterable as an argument.
Output:
Output:
Python provides also the remove() method to remove the item from the set. Consider
the following example to remove the items using remove() method.
Output:
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.
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.
Output:
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-
Output:
Output:
Python also provides the union() method which can also be used to calculate the
union of two sets. Consider the following example.
1. Days1 = {"Monday","Tuesday","Wednesday","Thursday"}
2. Days2 = {"Friday","Saturday","Sunday"}
3. print(Days1.union(Days2)) #printing the union of the sets
Output:
Output:
{'Monday', 'Tuesday'}
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}
Output:
{'castle'}
Output:
{'Thursday', 'Wednesday'}
Output:
{'Thursday', 'Wednesday'}
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}
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.
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'>
Output:
<class 'dict'>
<class 'frozenset'>
Name
Country
ID
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:
1. set1 = set([1,2,4,"John","CS"])
2. set1.update(["Apple","Mango","Grapes"])
3. print(set1)
Output:
Output:
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}
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:
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
SN Method Description
4 difference_update(....) It modifies this set by removing all the items that are
also present in the specified sets.
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).
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.
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.
Syntax:
In the above dictionary Dict, The keys Name and Age are the string that is an immutable
object.
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.
Output:
Empty Dictionary:
{}
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.
Note: If the key-value already present in the dictionary, the value gets updated. Otherwise,
the new keys added in the dictionary.
Example - 1:
Output:
Empty Dictionary:
{}
Example - 2:
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:
{}
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.
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:
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
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
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.
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.
2. for x in Employee.items():
3. print(x)
Output:
('Name', 'John')
('Age', 29)
('salary', 25000)
('Company', 'GOOGLE')
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.
Output:
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.
SN Method Description
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.
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
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
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
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
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.
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:
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.
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
Output:
Hi Devansh
Example 2
Output:
Enter a: 10
Enter b: 20
Sum = 30
Output:
Output:
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.
Example 1
1. def func(name):
2. message = "Hi "+name
3. return message
4. name = input("Enter the name:")
5. print(func(name))
Output:
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:
Example 3
Output:
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:
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:
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:
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.
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:
Output:
If we provide the different name of arguments at the time of function call, an error will
be thrown.
Example 3
Output:
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.
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:
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.
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.
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:
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
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
Output:
True
False
False
False
True
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.
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.
Output:
1. x = 8
2. print(callable(x))
Output:
False
Output:
<class 'code'>
sum = 15
1. x = 8
2. exec('print(x==8)')
3. exec('print(x+4)')
Output:
True
12
1. s = sum([1, 2,4 ])
2. print(s)
3.
4. s = sum([1, 2, 4], 10)
5. print(s)
Output:
7
17
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
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.
Output:
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.
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'
Output:
123
123.456790
1100
Python frozenset()
The python frozenset() function returns an immutable frozenset object initialized with
elements from the given iterable.
Output:
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:
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.
Output:
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
Output:
1
2
3
4
5
1. strA = 'Python'
2. print(len(strA))
Output:
6
Python list()
The python list() creates a list in python.
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]
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.
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}
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:
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.
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.
Output:
Since the mode is omitted, the file is opened in 'r' mode; opens for reading.
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:
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.
Output:
(1.5+0j)
(1.5+2.2j)
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
1. # Calling function
2. att = dir()
3. # Displaying result
4. print(att)
Output:
Output:
(5, 0)
1. # Calling function
2. result = enumerate([1,2,3])
3. # Displaying result
4. print(result)
5. print(list(result))
Output:
Python dict()
Python dict() function is a constructor which creates a dictionary. Python dictionary
provides three different constructors to create a dictionary:
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.
Output:
[6]
Hashable types: * bool * int * long * float * string * Unicode * tuple * code object.
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
1. # Calling function
2. info = help() # No argument
3. # Displaying result
4. print(info)
Output:
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
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'}
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
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
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
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.
Output:
['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v']
This method calls on iterator and throws an error if no item is present. To avoid the
error, we can set a default value.
256
32
82
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
If the number is not a number or if a base is given, the number must be a string.
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
The isinstance() function takes two arguments, i.e., object and classinfo, and then it
returns either True or False.
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
Output:
Output:
56
82
38
Output:
16
16
0.0625
0.0625
Output:
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]
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:
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
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.
1. str('4')
Output:
'4'
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.
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'>
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}
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')}
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.
Syntax
It can accept any number of arguments and has only one expression. It is useful when
the function objects are required.
Example 1
Output:
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:
The lambda function is commonly used with Python built-in functions filter() function
and map() function.
Consider the following example where we filter out the only odd number from the
given list.
Output:
Output:
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.
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.
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:
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
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
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()
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.
1. try:
2. fileptr = open("file.txt")
3. # perform file operations
4. finally:
5. fileptr.close()
The syntax to open a file using with the statement is given below.
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.
Example
1. with open("file.txt",'r') as f:
2. content = f.read();
3. print(content)
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.
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.
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
Output:
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.
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.
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:
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:
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.
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:
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.
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:
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.
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:
Output:
For this purpose, the Python provides us the seek() method which enables us to modify
the file pointer position externally.
Syntax:
1. <file-ptr>.seek(offset[, from)
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.
Example
Output:
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:
1. remove(file-name)
Example 1
1. import os;
2. #deleting the file named file3.txt
3. os.remove("file3.txt")
Syntax:
1. mkdir(directory name)
Example 1
1. import os
2.
3. #creating a new directory with the name new
4. os.mkdir("new")
Syntax
1. os.getcwd()
Example
1. import os
2. os.getcwd()
Output:
'C:\\Users\\DEVANSH SHARMA'
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.
Syntax
1. os.rmdir(directory name)
Example 1
1. import os
2. #removing the new directory
3. os.rmdir("directory_name")
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)
SN Method Description
1 file.close() It closes the opened file. The file once closed, it can't be read or
write anymore.
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.
10 File.tell() It returns the current position of the file pointer within the 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.
Here, we need to include this module into our main module to call the method
displayMsg() defined in the module named 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.
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:
Consider the following module named as calculation which contains three functions
as summation, multiplication, and divide.
calculation.py:
Main.py:
Output:
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.
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
Example
1. import json
2.
3. List = dir(json)
4.
5. print(List)
Output:
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.
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.
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
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:
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.
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
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'>
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:
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
1. try:
2. a=10/0;
3. except(ArithmeticError, IOError):
4. print("Arithmetic Exception")
5. else:
6. print("Successfully Done")
Output:
Arithmetic Exception
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.
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.
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:
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:
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.
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:
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.
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.
1. import time;
2. #prints the number of ticks spent since 12 AM, 1st January 1970
3. print(time.time())
Output:
1585928913.6519969
Example
1. import time;
2.
3. #returns a time tuple
4.
5. print(time.localtime(time.time()))
Output:
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
Example
1. import time
2. #returns the formatted time
3.
4. print(time.asctime(time.localtime(time.time())))
Output:
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
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.
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.
Example
Output:
fun hours
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
Example
1. import calendar
2. #printing the calendar of the year 2019
3. s = calendar.prcal(2020)
Output:
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.
Meta-Characters
Metacharacter is a character with the specified meaning.
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.
\S It returns a match if the string doesn't contain any white space character.
\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).
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']
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'>
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.
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.
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.
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.
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.
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
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.
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.
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.
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
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.
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
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
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")
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:
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
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:
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.
SN Function Description
2 setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.
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'
SN Attribute Description
1 __dict__ It provides the dictionary containing the information about the class namespace.
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
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...
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
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
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.
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
Output:
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)
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.
Syntax
Example -
Output:
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.
Example -
Output:
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:
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
https://files.pythonhosted.org/packages/8f/6d/fb8ebcbbaee68b172ce3dfd08c7b866
0d09f91d8d5411298bcacbd309f96/mysql-connector-python-8.0.13.tar.gz to
download the source code.
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.
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.
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.
Pass the database details like HostName, username, and the database password in the
method call. The method returns the connection object.
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:
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:
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:
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',)
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',)
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.
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.
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:
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).
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.
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:
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:
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:
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:
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.
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
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
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.
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
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.
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:
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.
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:
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:
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.
All the operations that modify the records of the database do not take place until the
commit() is called.
1. Conn.rollback()
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 !
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. # !/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:
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.
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.
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.
10 Message The Message widget is used to display the message-box 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.
18 MessageBox This module is used to display the message-box in the desktop based
applications.
However, the controls are less and widgets are generally added in the less organized
manner.
syntax
1. widget.pack(options)
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:
Syntax
1. widget.place(options)
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:
Syntax
1. W = Button(parent, options)
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.
5 Command It is set to the function call which is scheduled when the function is
called.
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.
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.
17 State This option is set to DISABLED to make the button unresponsive. The
ACTIVE represents the active state of the button.
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
Output:
Example
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)
SN Option Description
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.
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.
11 xscrollcommand If the canvas is scrollable, this attribute should be the .set() method of
the horizontal scrollbar.
13 yscrollcommand If the canvas is scrollable, this attribute should be the .set() method of
the vertical scrollbar.
Example
Output:
Output:
HTML Tutorial
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.
Syntax
1. w = checkbutton(master, options)
History of Java
SN Option Description
1 activebackground It represents the background color when the checkbutton is under the
cursor.
7 cursor The mouse pointer will be changed to the cursor name when it is over
the checkbutton.
12 highlightcolor The color of the focus highlight when the checkbutton is under focus.
14 justify This specifies the justification of the text if the text contains multiple
lines.
19 relief The type of the border of the checkbutton. By default, it is set to FLAT.
25 variable It represents the associated variable that tracks the state of the
checkbutton.
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
2 flash() The checkbutton is flashed between the active and normal colors.
3 invoke() This will invoke the method associated with the checkbutton.
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.
Syntax
SN Option Description
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.
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.
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.
18 selectborderwidth The width of the border to display around 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.
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.
4 index(index) It is used to place the cursor to the left of the character written
at the specified index.
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.
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:
Syntax
1. w = Frame(parent, options)
SN Option Description
3 cursor The mouse pointer is changed to the cursor type set to different
values like an arrow, dot, etc.
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.
Example
Output:
There are the various options which can be specified to configure the text or the part
of the text shown in the Label.
Syntax
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.
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.
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.
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.
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:
The user can choose one or more items from the list depending upon the
configuration.
1. w = Listbox(parent, options)
A list of possible options is given below.
HTML Tutorial
SN Option Description
3 cursor The mouse pointer will look like the cursor type like dot, arrow, etc.
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.
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.
Methods
There are the following methods associated with the Listbox.
SN Method Description
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.
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.
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:
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.
Syntax
1. w = Menubutton(Top, options)
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.
9 disabledforeground The text color of the widget when the widget is disabled.
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.
18 relief This option specifies the type of the border. The default value is
RAISED.
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:
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.
Syntax
1. w = Menu(top, options)
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.
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.
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
6 add(type, options) It is used to add the specific menu item 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
The message text contains more than one line. However, the message can only be
shown in the single font.
Syntax
1. w = Message(parent, options)
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.
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.
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).
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.
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
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.
Syntax
1. w = Message(parent, options)
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.
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.
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).
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.
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
Output:
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.
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.
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.
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.
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.
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.
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
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.
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.
Syntax
1. w = Scale(top, options)
SN Option Description
1 activebackground The background color of the widget when it has the focus.
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.
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.
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.
Methods
SN Method Description
Example
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.
Syntax
1. w = Scrollbar(top, options)
SN Option Description
1 activebackground The background color of the widget when it has the focus.
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.
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.
Methods
The widget provides the following methods.
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
Output:
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)
SN Option Description
1 activebackground The background color of the widget when it has the focus.
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.
10 format This option is used for the format string. It has no default value.
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.
16 state It represents the state of the widget. The default is NORMAL. The
possible values are NORMAL, DISABLED, or "readonly".
21 values It represents the tuple containing the values for this 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
2 get(startindex, endindex) It is used to get the characters present in the specified range.
5 insert(index, string) This method is used to insert the string at the specified index.
Example
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.
Syntax
1. w= PanedWindow(master, options)
SN Option Description
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.
11 sashrelief It represents the type of the border around each of the sash.
The default is FLAT.
Methods
There are the following methods that are associated with the PanedWindow.
SN Method Description
2 get(startindex, This method is used to get the text present at the specified
endindex) range.
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.
Syntax
1. w = LabelFrame(top, options)
History of Java
SN Option Description
2 bd It represents the size of the border shown around the indicator. The
default is 2 pixels.
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.
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.
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.
Syntax
Parameters
The two options that can be used are default and parent.
1. default
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.
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.
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.
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.
Redhat
On Redhat, the following commands are executed to install the Python SciPy package
stack.
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.
1. >>> a = numpy.array
We can also pass a collection object into the array routine to create the equivalent n-
dimensional array. The syntax is given below.
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.
Example
Output:
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)
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.
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:
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:
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
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:
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:
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.
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:
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.
5 int8 It is the 8-bit integer identical to a byte. The range of the value
is -128 to 127.
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.
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.
Align: It can be set to any boolean value. If true, then it adds extra padding to make it
equivalent to a C struct.
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
Example 1
1. import numpy as np
2. d = np.dtype([('salary',np.float)])
3. print(d)
Output:
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:
Numpy.empty
As the name specifies, The empty routine is used to create an uninitialized array of
specified shape and data type.
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.
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.
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.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.
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]
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]
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)
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.
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.arrange
It creates an array by using the evenly spaced values over the given interval. The syntax
to use the function is given below.
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:
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.
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.
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:
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.
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:
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.
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:
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:
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:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
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]]
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:
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
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:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
3 6 9 12 6 12 15 18 30 60 117 9
SN Operator Description
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:
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.
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:
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.
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:
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:
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:
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.
10 strip() Returns a copy of the string with the leading and trailing white
spaces removed.
1. import numpy as np
2. print("Concatenating two string arrays:")
3. print(np.char.add(['welcome','Hi'], [' to Javatpoint', ' read python'] ))
Output:
1. import numpy as np
2. print("Printing a string multiple times:")
3. print(np.char.multiply("hello ",3))
Output:
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*****
1. import numpy as np
2. print("Capitalizing the string using capitalize()...")
3. print(np.char.capitalize("welcome to javatpoint"))
Output:
1. import numpy as np
2. print("Converting string into title cased version...")
3. print(np.char.title("welcome to javatpoint"))
Output:
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:
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:
1. import numpy as np
2. print("Splitting the String word by word..")
3. print(np.char.split("Welcome To Javatpoint"),sep = " ")
Output:
1. import numpy as np
2. print("Splitting the String line by line..")
3. print(np.char.splitlines("Welcome\nTo\nJavatpoint"))
Output:
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:
1. import numpy as np
2. print(np.char.join(':','HM'))
Output:
H:M
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:
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
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]
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:
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.
1. numpy.around(num, decimals)
SN Parameter Description
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:
Example
1. import numpy as np
2. arr = np.array([12.202, 90.23120, 123.020, 23.202])
3. print(np.floor(arr))
Output:
Example
1. import numpy as np
2. arr = np.array([12.202, 90.23120, 123.020, 23.202])
3. print(np.ceil(arr))
Output:
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:
[[ 2 10 20]
[80 43 31]
[22 43 10]]
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]]
numpy.percentile() function
The syntax to use the function is given below.
1. numpy.percentile(input, q, axis)
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]]
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))
The kind of the sorting algorithm to be used in the sort operation must be mentioned
in the function call.
1 Quick Sort O (n ^ 2)
SN Parameter Description
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:
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.
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.
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:
numpy.nonzero() function
This function is used to find the location of the non-zero elements from the array.
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:
numpy.where() function
This function is used to return the indices of all the elements which satisfies a particular
condition.
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]))
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.
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
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.
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
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.
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
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.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.
Example
1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.empty((3,3)))
Output:
numpy.matlib.zeros() function
This function is used to create the matrix where the entries are initialized to zero.
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.
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)
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.
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.
Example
1. import numpy as np
2.
3. import numpy.matlib
4.
5. print(numpy.matlib.rand(3,3))
Output:
SN Function Definition
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]]
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.
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
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.
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
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.
1. 3X + 2 Y + Z = 10
2. X + Y + Z = 5
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]]
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.
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 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:
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:
In the output, a three-dimensional matrix has been shown whose elements are the
product of both array1 and array2 elements.
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
Output:
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
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.
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.
'A' Unchanged When the input is F and not C then F order otherwise C 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])
Example 2:
1. import numpy as np
2. arr=np.array([1,2.,3.])
3. arr
Output:
In the output, an array has been displayed containing elements in such type which
require minimum memory to hold the object in the sequence.
1. import numpy as np
2. arr=np.array([[1,2.,3.],[4.,5.,7]])
3. arr
Output:
1. import numpy as np
2. arr=np.array([1,2.,3.],ndmin=2)
3. arr
Output:
1. import numpy as np
2. arr=np.array([12,45.,3.],dtype=complex)
3. arr
Output:
In the output, the values of the 'arr' elements have been shown in the form of complex
numbers.
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]])
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
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 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]])
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]])
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.
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:
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 output, values of both the arrays 'z1' and 'z2' have preserved the masking of
MaskedArray input.
Output:
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
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 output, values of both arrays, i.e., 'a' and 'b', have been shown in the flattened
form, and the original array remained same.
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 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]])
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:
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
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.
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.
1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (4,3))
4. x
5. y
Output:
In the output, the array has been represented as three rows and four columns.
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:
1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (4, 3), order='F')
4. x
5. y
Output:
In the output, the array has been represented as four rows and three columns.
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:
1. import numpy as np
2. x=np.arange(12)
3. y=np.reshape(x, (2, -1))
4. x
5. y
In the output, the array has been represented as two rows and five columns.
Output:
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:
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.
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
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 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 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
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 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 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.
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:
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])
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])
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)
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:
2) np.random.permutation()
Example:
1. import numpy as np
2. a=np.random.permutation(12)
3. a
Output:
Distributions
There are the following functions of permutations:
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()
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])
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])
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])
Example:
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:
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])
Example:
1. import numpy as np
2. a = np.random.geometric(p=0.35, size=10000)
3. (a == 1).sum() / 1000
Output:
3.
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:
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>)
Example:
1. import numpy as np
2. location, scale = 0., 2.
3. s = np.random.laplace(location, scale, 10)
4. s
Output:
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:
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:
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:
Example:
1. import numpy as np
2. np.random.multinomial(20, [1/6.]*6, size=1)
Output:
array([[4, 2, 5, 5, 3, 1]])
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])
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:
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])
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:
Example:
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])
Example:
1. import numpy as np
2. n = np.random.standard_exponential((2, 7000))
Output:
25) standard_gamma([size])
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:
[[-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]]])
This function is used to draw sample from a standard Student's distribution with df
degree of freedom.
Example:
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
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])
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])
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:
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])
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:
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
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.
1. import numpy as np
2. a=np.zeros(6)
3. a
Output:
1. import numpy as np
2. a=np.zeros((6,), dtype=int)
3. a
Output:
array([0, 0, 0, 0, 0, 0])
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.]])
1. Import numpy as np
2. s1=(3,2)
3. a=np.zeros(s1)
4. a
Output:
array([[0., 0.],
[0., 0.],
[0., 0.]])
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 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
Parameters
x: array_like
This parameter defines the input value for the numpy.log() function.
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.
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)
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 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
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:
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:
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 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:
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]])
1. import numpy as np
2. x, y = np.ogrid[:3, :4]
3. a=np.where(x > y, x, 10 + y)
4. a
Output:
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.
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
Parameters
These are the following parameters in numpy.argsort() function:
a: array_like
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.
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.
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 output, a ndarray has been shown that contains the indices(indicate the position
of the element for the sorted array) and dtype.
Output:
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)
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 output, a 2-D array with sorted elements has been shown.
Output:
array([[0, 2],
[3, 5]])
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)
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]])
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:
In the output, an N-D array with sorted elements has been shown.
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:
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
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 output, the transposed array of the original array has been shown.
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 output, the transposed array of the original array has been shown.
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:
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
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.
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
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
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
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'.
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 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
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.
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:
In the output, a ndarray has been shown that contains the indices of the original array
that give unique values.
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.
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:
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 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:
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':
Syntax
1. numpy.dot(a, b, out=None)
Parameters
a: array_like
b: array_like
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]])
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
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
Parameters
These are the following parameter in numpy.loadtxt() function:
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.
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:
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:
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.])
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:
Parameters:
x: array_like
This parameter defines the source array whose elements we want to clip.
This parameter defines the minimum value for clipping values. On the lower interval
edge, clipping is not required.
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:
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
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])
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.
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:
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:
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
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:
Syntax:
Parameters
a: array_like
This parameter defines the source array whose elements standard deviation is
calculated.
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
Example 2:
1. a=np.array([[1,4,7,10],[2,5,8,11]])
2. b=np.std(a, axis=0)
3. b
Output:
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 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:
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:
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:
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 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
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.
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:
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:
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:
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]')
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
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.
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]])
Example 2:
1. import numpy as np
2. x = np.empty([3, 3], dtype=float)
3. x
Output:
Example 3:
1. import numpy as np
2. x = np.empty([3, 3], dtype=float, order='C')
3. x
Output:
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:
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:
Parameters:
x: array_like
This parameter defines a flattened array over which the histogram is computed.
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.
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
Example 1:
1. import numpy as np
2. a=np.histogram([1, 5, 2], bins=[0, 1, 2, 3])
3. a
Output:
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:
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:
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:
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:
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:
Parameters:
x: array_like
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'.
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]])
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 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:
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.
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
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 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:
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.
'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 parameter is used in 'constant'. It defines the values for setting the padded values
to each axis.
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.
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 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:
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 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:
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:
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.
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:
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:
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 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:
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:
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]])
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]
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]
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]
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.]
Syntax
1. numpy.tan(array[,out] )
Parameters
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:
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]
1. h = sqrt(b**2 + p**2)
where b and p are the base and perpendicular of the right angled triangle.
Syntax
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:
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:
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.]
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:
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:
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
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:
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
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:
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
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:
Numpy rint()
This function is used to round the array elements to the nearest integer.
Syntax
1. numpy.rint(array)
Parameters
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:
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:
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.
Syntax
1. numpy.trunc(array)
Parameters
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:
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.
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:
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]]
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.
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]]
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.]]
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.
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.]]
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]]
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.
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.]]
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]]
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
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:
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
Parameters
It accepts the following parameters.
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]
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]
Output:
numpy.frombuffer()
This function is used to create an array by using the specified buffer.
Syntax
Parameters
It accepts the following parameters.
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
Parameters
It accepts the following parameters.
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
Parameters
It accepts the following parameters.
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
Parameters
It accepts the following parameters.
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:
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
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.
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.
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.
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.
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.
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.
Creating a Series:
We can create a Series in two ways:
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
o Array
o Dict
o Scalar value
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
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.
Output
x 0.0
y 1.0
z 2.0
dtype: float64
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.
Output
0 4
1 4
2 4
3 4
dtype: int64
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
Attributes Description
Series.hasnans It returns True if there are any NaN values, otherwise returns false.
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
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,)
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
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
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.
Syntax
1. Series.map(arg, na_action=None)
Parameters
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:
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
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
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:
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:
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.
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
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.
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
Create a DataFrame
We can create a DataFrame using following ways:
o dict
o Lists
o Numpy ndarrrays
o Series
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.
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.
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().
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.
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:
Output
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:
Output
The DataFrame:
one two
a 1.0 1
b 2.0 2
c NaN 3
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.
Selection by Label:
We can select any row by passing the row label to a loc function.
Output
one 2.0
two 2.0
Name: b, dtype: float64
For selecting a row, we have passed the row label to a loc function.
Output
one 4.0
two 4.0
Name: d, dtype: float64
For selecting a row, we have passed the integer location to an iloc function.
Slice Rows
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.
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.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 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.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.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.sum() Return the sum of the values for the requested axis by the
user.
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:
Parameters:
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:
Parameters:
Returns:
It returns the result of applying func along the given axis of the DataFrame.
Example:
Output
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:
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.
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.
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
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
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.
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:
Parameters:
Returns:
It returns the count of Series or DataFrame if the level is specified.
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:
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. 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]]
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
Parameters
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
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.
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:
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.
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
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
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
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
Output
Parameters of Groupby:
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.
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.
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
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.
By using the head() in the below example, we showed only top 2 rows from the dataset.
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.
Syntax
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().
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
Output
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.
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:
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.
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.
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.
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
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
Parameters
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
Parameters
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:
Parameters:
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
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
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.
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
Parameters
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
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:
Parameters:
Returns:
It returns the DataFrame with renamed axis labels.
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:
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:
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
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
Example2
In this example, we take a csv file and extract random rows from the DataFrame by
using a sample.
Let's write a code that extract the random rows from the above dataset:
Output
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:
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.
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
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
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
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:
Parameters
It counts along a particular level and collapsing into a series, if the axis is a multiindex.
It includes only int, float, and boolean columns. If it is None, it will attempt to use
everything, so numeric data should be used.
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
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
Parameters
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
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.
Syntax:
Parameters :
func : It is a function that is used for transforming the data.
Returns:
It returns the DataFrame that must have same length as self.
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
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
Parameters
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
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
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:
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.
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
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
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.
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
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]]
Syntax:
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.
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.
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.
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.
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.
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
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
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
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.
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:
However, the pandas are also using the zero-based integer indices in the DataFrame;
we didn't tell it what our index should be.
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.
1. import pandas as pd
2. data = pd.read_json('hrdata.json')
3. print(data)
Output:
Firstly, we have to install pysqlite3 and run this command into the terminal:
sqlite3 is used to establish a connection to the database, and then we can use it to
generate a DataFrame through SELECT query.
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.
Output:
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.
Syntax:
1. pd.concat(objs,axis=0,join='outer',join_axes=None,
2. ignore_index=False)
Parameters:
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
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
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.
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.
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.
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.
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:
Partial indexing
Partial indexing can be defined as a way to choose the particular index from a
hierarchical indexing.
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
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:
'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:
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:
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.
Syntax
Parameters
method:
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:
Parameters:
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
Code:
Output
Pandas DataFrame.fillna()
We can use the fillna() function to fill the null values in the dataset.
Syntax:
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.
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:
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.
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
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
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
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.
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:
Multiple Conditions
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.
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.
Output:
P S
A 28.0 41.0
B 17.0 NaN
C14.0 34.0
D 42.0 25.0
ENaN 18.0
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
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 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:
Importing Data
Exporting data
Selection
Data cleaning
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.
Creating index
First, we have to take a csv file that consist some data used for indexing.
Example1
Output:
Example2:
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.
Output:
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:
Example:
Output:
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:
Output:
Difference between JDK, JRE, and JVM
[('it', 'one'),
('it', 'two'),
('of', 'one'),
('of', 'two'),
('for', 'one'),
('for', 'two'),
('then', 'one'),
('then', 'two')]
Example2:
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:
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:
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.
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.
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.
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.
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
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.
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.
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:
Parameters:
It is used to remove the given levels from the index and also removes all levels by
default.
It is used to modify the DataFrame in place and does not require to create a new object.
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:
Output:
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:
Parameters:
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 is used to modify the DataFrame in place. We don't need to create a new object.
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:
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
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:
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.
Pandas are built over numpy array; therefore, numpy helps us to use pandas more
effectively.
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
Output:
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:
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
Parameters
Returns:
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:
reshaped array:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]]
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
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
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.
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:
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.
Performance Pandas has a better performance for 500K NumPy has a better performance
rows or more. for 50K rows or less.
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.
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()
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:
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:
Example2:
You can pass errors='ignore' if the date does not meet the timestamp. It will return the
original input without raising any exception.
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:
Example5:
1. import pandas as pd
2. dmy = pd.date_range('2017-06-04', periods=5, freq='S')
3. dmy
Output:
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:
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.
**kwds
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')
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')
1. import pandas as pd
2. p = pd.period_range('2012', '2017', freq='A')
3. p
Output:
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:
1. import pandas as pd
2. prd
3. prd.to_timestamp()
Output:
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.
Output:
HTML Tutorial
2017-07-14 00:00:00
2017-07-14 00:00:00
2018-07-14 00:00:00
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:
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:
Parameters:
data: DataFrame
HTML Tutorial
kind: str
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.
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.
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.
yticks: Refers to a sequence that consists of values to use for the yticks.
It generally Rotates for ticks (xticks for vertical, yticks for horizontal plots)
Its main task is to specify the font size for xticks and yticks.
It provides colormap to select colors. If a value is a string, it loads colormap with that
name from matplotlib.
If the value is True, it plots the colorbar (only relevant for 'scatter' and 'hexbin' plots)
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.
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.
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
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.
456.1K
o Memory Efficient
o Data Alignment
o Reshaping
o Merge and join
o Time Series
o Lists
o Dict of ndarrays
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
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
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.
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
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.
Output:
Empty DataFrame
Columns: []
Index: []
Output:
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.
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.
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.
Remove duplicate index values by resetting the index and drop the duplicate values
from the index column.
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.
You can use the drop() method to specify the index of the rows that we want to
remove from the DataFrame.
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:
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
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
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.
Output:
vals
0 a
1 b
2 c
1. DataFrame.to_numpy(dtype=None, copy=False)
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.
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.
Time series forecasting is the machine learning modeling that deals with the Time
Series data for predicting future values through Time Series modeling.
Output:
2017-07-14 00:00:00
2017-07-14 00:00:00
2018-07-14 00:00:00
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.
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.
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.
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.
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.
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.
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).
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:
o Visit the official site of Anaconda and click on the Download Button
Matplotlib can be installed using with the Anaconda Prompt by typing command. To
install matplotlib, open Anaconda Prompt and type the following command:
The python package manager pip is also used to install matplotlib. Open the command
prompt window, and type the following command:
1. import matplotlib
2. matplotlib.__version__
3. '3.1.1'
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:
Output:
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:
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:
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.
Output:
Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Output:
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.
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).
The subplot() function also accepts the keyword arguments for the returned axes base
class.
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:
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.
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:
Output:
Let's have a look on the other example using the style() function:
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.
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:
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.
Output:
Let's consider the another example of plotting histogram:
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.
Example-1:
Output:
Example-2
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:
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:
Example-1:
Output:
Example-2:
Output:
Note: We can use the plot3D () to plot simple 3D line graph.
Example-3
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.
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.
xtricks(index, categorical variables) It is used to set or get the current tick locations labels of the x-axis.
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.
set_xlabel("string") It is an axes level method which is used to set the x-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.
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.
0.90 16 Nov
2005
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.
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.
Here, we are installing Django using pip3, the installation command is given below.
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.
10 Sec
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.
Initially, this project is a default draft which contains all the required files and folders.
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.
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.
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.
1. Install Package
2. Create a Directory
1. $ mkdir djangoenv
After it, change directory to the newly created directory by using the cd djangoenv.
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.
Django has installed successfully. Now we can create a new project and build new
applications in the separate environment.
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.
It prompts for login credentials if no password is created yet, use the following
command to create a user.
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.
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
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.
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 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.
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.
This model will create a table into the database that looks like below.
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.
For example,
1. INSTALLED_APPS = [
2. #...
3. 'appname',
4. #...
5. ]
Field Options
Each field requires some arguments that are used to set column attributes. For
example, CharField requires mac_length to specify varchar database.
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.
//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()
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.
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
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.
Output:
Syntax
require_http_methods(request_method_list)
Django Http Decorator Example
//views.py
This method will execute only if the request is an HTTP GET request.
//urls.py
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.
Django template engine is used to separate the design from the python code and
allows us to build dynamic web pages.
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. ]
// 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
//urls.py
1. path('index/', views.index),
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.
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
//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.
1. {% csrf_token %}
2.
3. {% if user.is_authenticated %}
4. Hello, {{ user.username }}.
5. {% endif %}
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
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.
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
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.
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.
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. ]
1. STATIC_URL = '/static/'
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.
// 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
//views.py
1. def index(request):
2. return render(request,'index.html')
After that access the template by localhost:8000/index URL, and it will produce the
following output to the browser.
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
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. }
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>
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 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.
// model.py
This file contains a class that inherits ModelForm and mention the model name for
which HTML form is created.
// form.py
//views.py
//urls.py
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:
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.
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.
Commonly used fields and their details are given in the below table.
Let's see a complete example to create an HTML form with the help of Django Form
class.
// views.py
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>
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:
Note: that we'll have to provide the surrounding <table> or <ul> elements yourself.
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.
// models.py
// forms.py
//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.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>
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)
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)
View (views.py)
Here, one extra parameter request.FILES is required in the constructor. This argument
contains the uploaded file instance.
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
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.
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.
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
The created migration file is located into migrations folder and contains the following
code.
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
To create a migration for this model, use the following command. It will create a
migration file inside the migration folder.
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
After creating a migration, migrate it so that it reflects the database permanently. The
migrate command is given below.
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.
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. ]
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.
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)
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.
When a client requests for a resource, a HttpRequest object is created and correspond
view function is called that returns HttpResponse object.
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.
Attribute Description
HttpRequest.scheme A string representing the scheme of the request (HTTP or HTTPs usually).
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.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.
Attribute Description
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.
Attribute Description
HttpResponse.charset It is a string denoting the charset in which the response will be encoded.
Method Description
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.
Exception Description
AppRegistryNotReady It is raised when attempting to use models before the app loading process.
EmptyResultSet If a query does not return any result, this exception is raised.
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.
ValidationError It is raised when data validation fails form or model field validation.
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.
Exception Description
DataError It raises when data related issues come into the database.
Exception Description
Exception Description
TransactionManagementError It is raised for any and all problems related to database transactions.
// views.py
1. def getdata(request):
2. data = Employee.objects.get(id=12)
3. return HttpResponse(data)
// urls.py
1. path('get',views.getdata)
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.
To set and get the session in views, we can use request.session and can set multiple
times too.
Method Description
__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.
//views.py
// urls.py
Run Server
1. $ python3 manage.py runserver
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.
History of Java
// views.py
// urls.py
Start Server
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.
Let's see an example, here we have a Django project to which we are implementing
this feature. A view function getfile() is created.
// 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
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.
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.
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
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)
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.
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.
models.py
8.5M
169
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.
Now add the following settings to the settings.py file of your project.
settings.py
In the next step, we should add the following configuration in urls.py file.
Urls.py
All required configurations are done; we are ready to run the following commands.
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
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.
View.py
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.
sampleapp/urls.py
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 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.
model.py
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.
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"'
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.
Example -
Example - 2
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.
In the below example, we will fetch the data which first name starts with the R.
Output:
, , , , ]>
o queryset_1 |queryset_2
o filter(Q(<condition_1>) | Q(<condition_2>
We get the student details those first name starts with 'A' and last name starts with 'S'.
Or
1. >>> Student.objects.all().count()
2. 7
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>]>
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>]
1. >>> Student.objects.all()[0]
2. <Student: Ritesh Tiwari>
For descending order, we will use the Not '-' before the query field.
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.
Example -
1. Student.objects.filter(first_name__startswith = 'Ritesh')
2. <QuerySet [<Student: Ritesh Tiwari>]>
o exact
Lookup should be used after the __ double underscore. We can use the case-
insensitive version called iexact.
o contains
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")
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.
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)
If we set null=True blank=True, means that the field is optional in all circumstances.
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.
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
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
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 %}
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.
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.
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 -
Output:
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 -
Output:
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 -
Output:
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
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:
Example -
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 -
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.
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'}))
Example -
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
Now we will run the following commands to make these tables in the database.
We will open the Django shell and create objects in order to store the date in tables.
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.
Above output displays the SQL queries of movie object. Same as we created the three
movies objects.
The Movie table has the foreign key relationship with the Director table. So we can use
the following Query to fetch data
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 -
Let's create a query that fetches all movies with the name of the director in 1 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.
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
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.
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.
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 -
Parameters -
Optional Parameters -
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 -
Parameters -
Example -
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().
Example -
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().
Example -
It is equivalent to:
We have discussed a few important shortcuts that provide control over the objects.
These shortcuts also allow handling the potential errors effectively.
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.
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.
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.
The terminal will display the link http://127.0.0.1:8000, visit this link
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.
Use the create database my_database query. It will create the new database.
We can check the databases through the show databases query.
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. }
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.
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.
Database changed
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.
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. 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.
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)
1. Student.objects.filter(fees__gte=F(2000))
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.
Let's see the below example to sort the students who haven't been paid fees after the
new semester.
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 -
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 -
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().
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.
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
1. $ venv\scripts\activate
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.
Write the following lines of code and save to a file named as script.py.
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)
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.
SN Option Description
2 port The port number to which the server is listening to. The default port number is 5000.
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.
Example
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
Example
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.
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.
Example
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.
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.
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
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, open the HTML file, login.html on the web browser and give the required input.
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)
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.
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
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.
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
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
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.
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
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.
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
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
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
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>
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
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>
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
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. <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.
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> )
SN Parameter Description
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
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
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
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.
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.
1. get_flashed_messages(with_categories, category_filter)
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
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
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.
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.
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.
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.
There are the following methods that can be called on the Message class object.
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)
Step 1: import the required module like flask-mail, flask using from-import statement.
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
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
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.
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. <!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
The following syntax is used to send the attachments with the mail.
The flask script which sends the attachment with the mail is given below.
mailer_attach.py
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.
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()
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>
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)
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>
crud.py
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.
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
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
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.
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.
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.
The module contains a Form class which is considered as the parent class for all the
form related operations.
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.
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.
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
formexample.py
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:
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.
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.
o Netflix
o Lyft
o Reddit
o Zillow
o MailGui
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.
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.
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
Flask
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:
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:
When you click on the above link, it will display the Hello World on a webpage.