Sei sulla pagina 1di 1484

Getting Started with Python Programming

Check out these Python tutorials and step up for becoming the next data scientist. Choose where to begin, learn at your
own pace:

Unlock the latest Python tutorials – Start it from Scratch


 Introduction to Python
 Future of Python
 Python Environment Setup
 Features of Python
 Why Python is in Demand
 Learn Python through Master Guide
 Top Python Projects with Source Code
 Python for Machine Learning
 Python Case Studies
 Python Syntax
 Python Statement, Indentation, and Comments
 Python Interpreter
 Python Advantages and Disadvantages
 Why Should I Learn Python?
 How a Fresher Get a Job in Python?
 Python Applications
 Python in Healthcare
 Python in Stock Market
 Python Career Opportunities
 Python Variables and Data Types
 Python Variable Scope
 Python Identifiers
 Python Namespace and Scope
 Python Operators
 Python Bitwise Operator
 Python Comparison Operator
 Python Operator Precedence
 Python Numbers
 Python Strings
 Python Data Structures
 Python Lists
 Python Tuples
 Python Dictionaries
 Python Sets and Booleans
 Python Tuples vs Lists
 Sequences and Collections in Python
 Python Decision-Making
 Python Switch
 Python Loops
 Python Functions
 Python Built-in Functions
 Range() Function in python
 Python zip() Function
 Python eval() Function
 Python exec() Function
 Python Function arguments
 Python Recursion
 Python Terminologies Part I
 Python Terminologies Part II
 What’s new in Python 3.6
 Python vs Scala
 Python vs Java
 Python vs R
 Python at Netflix
 How to create a Perfect Python Resume
 Best Python Books
 Python For Beginners – Infographic
 Python Features – Infographic
 Python Career – Infographic

Level up to more exciting and challenging Python tutorials


 Python Classes
 Python Methods
 Python Methods vs Functions
 Constructors in Python
 Python Objects
 Python Inheritance
 Multiple Inheritance in Python
 Python Operator Overloading
 Python property Class
 Python Compilers
 Python Modules
 Python OS Module
 Python pprint Module
 Python sys Module
 Python repr Function
 Python Directory
 Copying Files with Python
 Renaming Files with Python
 Zipping Files with Python
 Python File I/O
 Reading and Writing Files in Python
 Python Slicing and slice() Construtor
 List Comprehension in Python
 Python Iterables
 Python Iterators
 Python Decorators
 Python Generators
 Python Generators vs Iterators
 Closure in Python
 Python array Module
 Generating Random Numbers in Python
 Python Modules-Counter, Defaultdict, Ordereddict, Namedtuple
 Python Counter Module
 Python defaultdict Module
 Python OrderedDict Module
 Python namedtuple() Factory Function
 Python Packages
 Python Modules vs Packages
 Python itertools Module
 Python Date and Time
 Python Datetime Object
 Python Calendar Module
 Lambda expressions in python
 Assert Statements in Python
 Ternary Operators
 Shallow Copy and Deep Copy- Python

Master essential Python skills and evolve as an expert


 Python Errors and Exceptions
 Python Exception Handling
 Python 3 Extensions
 Python Tools
 XML Processing in Python3
 Networking in python 3
 Sending mail with Python 3
 GUI Programming in Python 3
 CGI Programming in Python
 Python Multi-threading
 Multiprocessing with Python
 Python Subprocess module
 Python Regular Expressions
 SciPy with Python
 NumPy with Python
 Accessing Database with Python
 Python Image Processing
 Unit Testing with Python
 Logging in Python
 Serialization in Python
 Python Debugger
 Python Forensics
 Python Virtual Environments and Packages
 Important Python Libraries
 Best Python Web frameworks
 Python Django Framework
 Python Pandas
 Python Flask
 Python PyQT
 Python Interview Questions- Part 1
 Python Interview Questions- Part 2
 Python Project- Drowsiness Detection System
 Python Project- Detecting Fake News
 Python Project- Speech Emotion Recognition
 Python Project- Detecting Parkinson’s Disease
 Python Project- Gender & Age Detection
 Python Project- Breast Cancer Classification

Implement your Python learning and step into the world of Data Science
 Python for Data Scientists
 Data Science with Python
 Python Data Science Environment Setup
 Data Scientist Salary in India
 Data Science Skills
 Data Operations and Data Cleansing
 Processing CSV, JSON, and XLS data
 Python Relational databases
 Python NoSQL databases
 Stemming and Lemmatization
 Data Wrangling and Aggregation
 Python Matplotlib
 Box Plots and Scatter Plots
 Bubble Charts and 3D Charts
 Python Heatmaps
 Histograms and Bar Plots
 Geographical Data and Graph Data
 Time Series Analysis
 Central Tendency and Variance
 Normal, Binomial, Poisson, Bernoulli Distributions
 p-Value and Correlation
 chi-Square Test and Linear Regression

Learn how Python is important for Machine Learning


 Machine Learning with Python
 Python Machine Learning Environment Setup
 Data Preprocessing, Analysis & Visualization- ML
 Training Data and Test Data- ML
 Python Machine Learning Techniques
 Python Machine Learning Algorithms
 Python Machine Learning Applications
 Deep Learning with Python
 Python Deep Learning Environment Setup
 Python Deep Learning Applications
 Python Deep Learning Libraries and Frameworks
 Deep Neural Networks- Deep Learning
 Computational Graphs- Deep Learning

Execute your Python skills to develop Artificial Intelligence


 Artificial Intelligence with Python
 Python Logic Programming- AI
 Python Reinforcement Learning- AI
 Python Natural Language Processing- AI
 Python NLTK Package- AI
 Python Speech Recognition- AI
 Python Heuristic Search- AI
 Python genetic Algorithm – AI
 Python Computer Vision- AI

Python Tutorials

 Python – Introduction
 Python – Features
 Python – Pros and Cons
 Python – Master Guide
 Python – Case Studies
 Python – Demand
 Python – Future
 Python – Create Resume
 Python – Career Opportunities
 Python – How Fresher Gets Job
 Python – Tools
 Python – Top Projects
 Python – Data Science Tutorial
 Python – For Data Science
 Python – Data Science Installation
 Python 3.6 Features
 Python – Install on Windows
 Python – Applications
 Python – Healthcare
 Python – Stock Market
 Python – Syntax
 Python – Comments, Indentations and Statements
 Python – Assert Statements
 Python – Number Types
 Python – Random Number
 Python – Variables and Data Types
 Python – Variable Scope
 Python – Identifiers
 Python – Strings
 Python – Interpreter
 Python – Operators
 Python – Bitwise Operators
 Python – Comparison Operators
 Python – Operator Overloading
 Python – Ternary Operator
 Python – Operator Precedence
 Python – Namespaces
 Python – Decision Making
 Python – Loops in Python
 Python – Implement Switch Case
 Python – Functions
 Python – Function Arguments
 Python – Built-in Functions
 Python – Methods
 Python – Methods vs Functions
 Python – Constructors
 Python – Range() Function
 Python – Zip Function
 Python – Eval Function
 Python – exec Function
 Python – repr Function
 Python – Data Structures
 Python – Lists
 Python – Tuples
 Python – Tuples vs Lists
 Python – Collections Module
 Python – Counters
 Python – Namedtuples
 Python – DefaultDict
 Python – OrderedDict
 Python – Dictionaries
 Python – DateTime
 Python – Modules
 Python – Serialization
 Python – Packages
 Python – Python OS Module
 Python – Python pprint Module
 Python – Virtual Environment
 Python – Modules vs Packages
 Python – Sets & Booleans
 Python – List Comprehension
 Python – Date and Time
 Python – Calendar Module
 Python – Recursion
 Python – Lambda Expression
 Python – Decorators
 Python – Generators
 Python – Iterators
 Python – Generators vs Iterators
 Python – Closures
 Python – Classes
 Python – Object
 Python – Inheritance
 Python – Multiple Inheritance
 Python – Compilers & Interpreters
 Python – ZipFile
 Python – File I/O
 Python – File Handling
 Python – Copy A File
 Python – Shallow & Deep Copy
 Python – Rename A File
 Python – Data File Formats
 Python – Errors and Exceptions
 Python – Exception Handling
 Python – Directories
 Python – Iterables
 Python – Itertool
 Python – Property
 Python – Sequences and Collections
 Python – Stemming & Lemmatization
 Python – Aggregation & Data Wrangling
 Python – Data Cleansing & Operations
 Python – Multithreading
 Python – Regular Expressions
 Python – Debugger
 Python – Multi Processing
 Python – XML Processing
 Python – CGI Programming
 Python – Library
 Python – Matplotlib
 Python – SciPy
 Python – NumPy
 Python – Pandas
 Python – PyQT
 Python – Array Module
 Python – Database Access
 Python – Programming with C
 Python – NoSQL Database
 Python – Relational Database
 Python – Frameworks
 Python – Flask
 Python – Django
 Python – Forensics
 Python – Network Programming
 Python – Image Processing
 Python – Sending Email
 Python – GUI Programming
 Python – Statistics
 Python – Descriptive Statistics
 Python – Probability Distributions
 Python – Linear Regression
 Python – Histogram & Bar Plot
 Python – Bubble & 3D Charts
 Python – Scatter & Box Plots
 Python – Heatmap & Word Cloud
 Python – Geographical Map & Graph
 Python – Time Series
 Python – Unittest
 Python – Logging
 Python – Slice
 Python – Subprocess Module
 Python – sys Module
 Python – Importance for ML
 Python ML – Tutorial
 Python ML – Environment Setup
 Python ML – Data Pre-Processing
 Python ML – Train and Test Set
 Python ML – Techniques
 Python ML – Application
 Python ML – Algorithms
 Python Deep Learning Tutorial
 Python DL – Environment Setup
 Python DL – Application
 Python DL – Python Libraries
 Python DL – Deep Neural Networks
 Python DL – Computational Graphs
 Python AI Tutorial
 Python AI – NLTK
 Python AI – Speech Recognition
 Python AI – NLP Tutorial
 Python AI – Heuristic Search
 Python AI – Genetic Algorithms
 Python AI – Computer Vision
 Python AI – Logic Programming
 Python – Best Python Books
 Python – Reasons Why Learn Python
 Python – Terminologies Part 1
 Python – Terminologies Part 2
 Python vs Scala
 Python vs Java
 Python vs R
 Case Study – Python at Netflix
 Python For Beginners – Infographic
 Python Features – Infographic
 Python Career – Infographic
 Python Project- Detecting Fake News
 Python Project- Speech Emotion Recognition
 Python Project- Detecting Parkinson’s Disease
 Python Project- Gender & Age Detection
 Python Project- Breast Cancer Classification

Python Interview Questions

 Python – Interview Questions Part 1


 Python – Interview Questions Part 2

Python certainly is here for the long run. Are you?

Furthermore, if you have any query, feel free to ask in the comment section.

1. Demystifying Python
2. Python Basic Constructs
3. String Handling in Python
4. Python Data Structures
5. Functional Programming in Python
6. Python Modules
7. Python Packages
8. Deep Dive into Python
9. Python Object-Oriented Programming Concepts
10. File Handling in Python
11. Python Exception Handling
12. Python Data Science Libraries
13. Python Advanced Concepts
14. Popular Python Frameworks
15. Python vs Other Programming Languages
16. Real-time Python Projects

1. Demystifying Python

1. Introduction to Python
2. Python Environment Setup
3. Features of Python
4. Basic Python Syntax
o Statements, Indentation, and Comments
5. 7 Reasons to Learn Python
6. Benefits and Limitations of Python
7. Career in Python
8. Applications of Python
9. Python Compilers and Interpreters Available
10. Getting to Know the Python Interpreter

2. Python Basic Constructs

1. Python Variables
o Python Variable Scope
2. Data Types in Python
3. Python Operators
o Bitwise Operators
o Comparison Operators
o Operator Precedence
o Ternary Operators
4. Python Decision Making
o Switch-case in Python
5. Loops in Python
6. Numbers with Python

3. String Handling in Python

1. String Formatters and Escape Sequences in Python


2. String Functions and Operations in Python
3. Python repr() Function

4. Python Data Structures

1. Python Lists
o List Comprehension
o The array Module
2. Python Tuples
o The zip() Function
o Tuples vs Lists
3. Slicing in Python
4. Python Binary Sequence Types- bytes, bytearray, memoryview
5. Dictionaries in Python
6. Sets in Python
7. Booleans in Python

5. Functional Programming in Python

1. Functions in Python
2. Python Lambda Expressions
3. Function Arguments
4. Python Recursion Function
5. Python Built-in Functions
o range()
o eval()
o exec()
6. Decorators in Python
7. Closure in Python
8. Working with Itertools in Python

6. Python Modules

1. Counter
2. Defaultdict
3. OrderedDict
4. namedtuple
5. Python Numeric Modules- math, decimal, random
6. sys
7. Generating Random Numbers
8. Python datetime Module
9. Python calendar Module

7. Python Packages

1. Python pip and PyPI


2. Python Modules vs Packages

8. Deep Dive into Python

1. Multithreading in Python
2. Working with Date and Time in Python
3. Namespace and Scope in Python
4. Terminologies in Python
5. Deep Copy vs Shallow Copy in Python
6. Assert Statements in Python
7. Python Pretty-Printing with pprint

9. Python Object-Oriented Programming Concepts

1. Methods in Python
o Methods vs Functions
o Magic (Dunder) Methods in Python
2. Classes in Python
3. Python Objects
4. Inheritance in Python
5. Python Multiple Inheritance
6. Python Operator Overloading
7. Generators in Python
8. Iterators in Python
o Generators vs Iterators in Python
9. Python Serialization with pickle
10. Python property Decorator

10. File Handling in Python

1. Reading and Writing Files in Python


2. Managing Directories and Files in Python
3. Python OS Module
4. Python shutil Module
5. Copying Files with Python
6. Renaming Files with Python
7. Zipping Files with Python

11. Python Exception Handling

1. Errors and Exceptions in Python


2. Python Exception Handling
3. Testing with unittest in Python

12. Python Data Science Libraries

1. Python Libraries
2. NumPy
3. SciPy
4. pandas
5. Visualizing with matplotlib
6. PyQT
7. Altair
8. Web Scraping with Scrapy
13. Python Advanced Concepts

1. Regular Expressions with Python


2. Virtual Environments and Packages
3. The Python Debugger (pdb)
4. CGI Programming with Python
5. Understanding urllib
6. XML Processing
7. Sending Mail with Python
8. Networking
9. Processing Images
10. GUI Programming
11. Tools
12. Accessing the Database
13. Logging with Python
14. Multiprocessing

14. Popular Python Frameworks

1. Web Frameworks
2. Django
3. Flask

15. Python vs Other Programming Languages

1. Python vs Java
2. Python vs Scala
3. Python vs R

16. Real-time Python Projects


5 real-time python projects based on industry use cases to master the python language

Python Tutorial for Beginners | How to Quickly Learn Python?


by DataFlair Team · December 17, 2018

In this Python tutorial, we will introduce you to Python Programming. This includes features of Python,
Python architecture and its applications in the industry. Also, we will learn about available python frameworks
like Django, Flask, Pyramid, etc.

There’s a reason they choose Python as an introductory language for programming. Simple, concise, easy-
Python has it all. It also equips you to build so much. All aboard the Python train!

So, let’s start this Python Tutorial.


Python Tutorial for Beginners | How to Quickly Learn Python?

1. What is Python?

The Python programming language is an object-oriented language, which means that it can model real-world
entities. It is also dynamically-typed because it carries out type-checking at runtime. It does so to make sure that
the type of a construct matches what we expect it to be. The distinctive feature about Python is that it is an
interpreted language. The Python IDLE (Integrated Development Environment) executes instructions one line
at a time. This also lets us use it as a calculator.

i. Why is it called Python?


Going into etymology, Guido van Rossum named it after the comedy group Monty Python. That is why the
metasyntactic variables (those we will often use to explain code syntax) used here are ‘spam’ and ‘eggs’ instead
of ‘foo’ and ‘bar’. A lot of implementations today run version 2.x, but the future belongs to Python 3.x. It is also
called ‘Python 3000’ or ‘Py3K’. CPython, written in C, is the most common implementation of Python.

ii. What makes Python so powerful?


Apart from the constructs that Python provides, you can use the PyPI (Python Package Index). It is a repository
of third-party Python modules and you can install it using a program called pip. Run the following command
in Command Prompt:

1. pip install library_name

Python or R: To learn the difference between Python and R, please follow Python vs R. For now let us move
ahead with the current Python tutorial.

2. How was Python Born?


 The Python programming language was conceived in the late 1980s and was named after the BBC TV
show Monty Python’s Flying Circus. Guido van Rossum started implementing Python at CWI in the
Netherlands in December of 1989. This was a successor to the ABC programming language which was
capable of exception handling and interfacing with the Amoeba operating system.
 On October 16 of 2000, Python 2.0 released and it had many major new features including cycle-
detecting garbage collector for memory management and support for Unicode.
 The next version of Python 3.0 released on December 3, 2008.

Now we know how Python came into the picture. So, moving ahead in this Python tutorial, let us jump to
Python Architecture.

3. Python Architecture

Let’s now talk about Python architecture and its usual flow –

i. Parser

It uses the source code to generate an abstract syntax tree.

ii. Compiler

It turns the abstract syntax tree into Python bytecode.

iii. Interpreter

It executes the code line by line in a REPL (Read-Evaluate-Print-Loop) fashion. On Windows, when you want
to run the Python interpreter in the shell, you can type the following:

1. $python

Next in Python tutorial, we discuss some useful Python Constructs to give you a better idea of the structure of
Python code.

4. Python Constructs
Python Tutorial – Python Constructs

i. Functions
A function in Python is a collection of statements grouped under a name. You can use it whenever you want to
execute all those statements at a time. You can call it wherever you want and as many times as you want in a
program. A function may return a value.

ii. Classes
As we discussed earlier, Python is an object-oriented language. It supports classes and objects. A class is an
abstract data type. In other words, it is a blueprint for an object of a certain kind. It holds no values. An object is
a real-world entity and an instance of a class.

iii. Modules
A Python module is a collection of related classes and functions. We have modules for mathematical
calculations, string manipulations, web programming, and many more. We will discuss Python Module in detail
in a later lesson.

iv. Packages
Python package is a collection of related modules. You can either import a package or create your own.

v. List
You can think of a list as a collection of values. Declared in the CSV (Comma-Separated Values) format and
delimit using square brackets:
1. life = [‘love’, ‘wisdom’,
‘anxiety’]; 2. arity = [1,2,3];

Notice that we do not declare the type for the list either. A list may also contain elements of different types, and
the indexing begins at 0:

1. person = [‘firstname’, 21];


2. print(person[1])

Output: 21
You can also slice lists; slicing is a way of retrieving some values from it. We will learn more about it in further
lessons.

vi. Tuple
A tuple is like a list, but it is immutable (you cannot change its values).

1. pizza = (‘base’, ‘sauce’, ‘cheese’, ‘mushroom’);


2. pizza[3] = ‘jalapeno’

This raises a TypeError.

vii. Dictionary
A dictionary is a collection of key-value pairs. Declare it using curly braces, and commas to separate key-value
pairs. Also, separate values from keys using a colon (:).

1. student = {‘Name’: ‘Abc’, ‘Age’: 21}


2. print(student[‘Age’])

Output: 21

viii. Comments and Docstrings


Declare comments using an octothorpe (#). However, Python does not support multiline comments. Also,
docstrings are documentation strings that help explain the code.
#This is a comment
“““
This is a docstring
”””
Python has a lot of other constructs. These include control structures, functions, exceptions, etc. We will discuss
these in further tutorials.

Now let us learn about the features of Python. Based on these features, you will be able to choose a
programming language for your next project.

5. Features of Python

The Python programming language is one of the richest languages. In this Python tutorial, we will discuss
several features of Python:
Python Tutorial – Features of Python Programming Language

i.Easy

Python is very easy to learn and understand; using this Python tutorial, any beginner can understand the basics
of Python.

ii. Interpreted

It is interpreted(executed) line by line. This makes it easy to test and debug.

iii. Object-Oriented

The Python programming language supports classes and objects. We discussed these above.

iv. Free and Open Source

The language and its source code are available to the public for free; there is no need to buy a costly license.

v. Portable

Since it is open-source, you can run Python on Windows, Mac, Linux or any other platform. Your programs
will work without needing to the changed for every machine.

vi. GUI Programming

You can use it to develop a GUI (Graphical User Interface). One way to do this is through Tkinter.

vii. Large Library


Python provides you with a large standard library. You can use it to implement a variety of functions without
needing to reinvent the wheel every time. Just pick the code you need and continue. This lets you focus on other
important tasks.

Now, let us see the frameworks available in Python.

6. Python Frameworks

Python tutorial – Frameworks in Python Programming Langauge

i. Django
Python Django is a free and open-source framework written in Python and is the most common framework for
Python. It allows you to create database-driven websites. It follows the DRY Principle (Don’t Repeat
Yourself). This is a design philosophy that keeps code simple and eloquent.

Popular websites like Instagram, Mozilla, and Disqus make use of it.

ii. Flask
Like Django, Flask is a web framework written in Python itself. It is a micro framework because it does not
need certain libraries and tools. It also does not have form validation or a database abstraction layer. However,
you can make use of extensions to add extra features.
iii. Pyramid
Pyramid is another web framework. It is neither a mega-framework that would make decisions for you nor a
micro-framework that wouldn’t force decisions. It gives you optimal liberty of your project.

iv. Tornado
Another open-source web framework, Tornado is written in Python Language. It is noted for its excellent
performance and scalability.

v. Bottle
Like Flask, it is a micro-framework for Python. It is used for web development. Bottle is known for its speed,
simplicity, and lightweight. A single file can run both Python 2.5+ and 3.x.

vi. web2py
Written in Python, web2py is another open source web framework. It emphasizes on rapid development and
follows an MVC architecture. MVC stands for Model View Controller.

vii. NumPy
NumPy is an open-source framework for Python. We use it for scientific computing. It supports large
multidimensional arrays and matrices, and functions to operate on them.

viii. SciPy
SciPy is a Python library that you can use for scientific computing. It has modules for linear algebra,
interpolation, fast Fourier transform(FFT), image processing, and many more. It uses multidimensional arrays
from the NumPy module.

ix. Pylons
This is a deprecated framework, which means it is no longer recommended. It is a web framework and is open
source as well. It makes extensive use of third-party tools.

7. Flavors of Python

Now, let’s take a look at major Python implementations –


Python Tutorial – Flavors of Python Programming Langauge

i. CPython
This is the most widely accepted implementation of Python. It is written in the language C, and is an interpreter.

ii. Jython
Jython is a Python implementation written in Java. A Jython program can import any Java class. It compiles to
Java bytecode.

iii. IronPython
IronPython is implemented in C#. It can function as an extensibility layer to application frameworks written in a
.NET language.

iv. Brython
Brython stands for Browser Python. It is an implementation of Python that runs in the browser.
v. RubyPython
It acts as a bridge between the Python and Ruby interpreters. It marshals data between Python and Ruby virtual
machines.

vi. PyPy
Interesting to know how PyPy is Python implemented in Python. This makes it faster and easier to experiment
with. However, the standard implementation is CPython.

vii. MicroPython
This is an implementation of Python meant to run on a microcontroller. It uses a MicroPython board that runs
MicroPython on bare metal.

Let’s move ahead in this Python tutorial and learn file extensions of Python.

8. File Extensions in Python

 .py –The normal extension for a Python source file


 .pyc- The compiled bytecode
 .pyd- A Windows DLL file
 .pyo- A file created with optimizations
 .pyw- A Python script for Windows
 .pyz- A Python script archive

9. Python Applications

Python is easy to pick-up even if you come from a non-programming background. You can look at the code and
tell what’s going on. Talking of Python applications, some of the cool things that you can do with Python are –

 Build a website
 Develop a game
 Perform Computer Vision (Facilities like face-detection and color-detection)
 Implement Machine Learning (Give a computer the ability to learn)
 Enable Robotics
 Perform Web Scraping (Harvest data from websites)
 Perform Data Analysis
 Automate a web browser
 Perform Scripting
 Perform Scientific Computing
 Build Artificial Intelligence

Python isn’t limited to these applications. If you’ve ever used services from brands like YouTube, Dropbox,
and Netflix, then you’ve been a consumer of Python. The search-engine Google also made great use of the
language in its initial stages.

When writing code in Python, you need fewer lines of code compared to languages like Java. This high-level
language is also open-source and free. Going by the TIOBE Index, it is among the major programming
languages with the fastest growth. This makes a career in Python a great choice.
To make it clearer about Python, we have covered how it is different from other programming languages like
Java or C++.

10. Python vs Java vs C++

Python uses whitespace indentation to delimit code, you don’t need to use curly braces for that. Also,
semicolons are optional. It has two correct syntaxes :

1. a = 7
2. print(a)

1. a = 7;
2. print(a)

While Java and C++ are statically-typed, Python is dynamically-typed. You also don’t need to declare the type
of a variable; you assign it:

1. life=42

Java is faster by a few seconds, but the difference does not invalidate Python’s advantages over it. Since you
can interpret Python, the code is easier to test and debug.

So, this was all about Python. Hope you liked our explanation.

11. Summary: Python Tutorial

Let’s conclude this Python Tutorial with a quick revision. Today, we discussed Python and how it came to be.
We also took a brief look at its architecture and various constructs. Moreover, we learned about Python
frameworks, and its flavors and file extensions. We then discussed a few Python applications and enquired
about how it is different from Java. Well, Python is a beautiful language and we wish to empower you to create.

Python certainly is here for the long run. Are you?

13 Unique Features of Python Programming Language


by DataFlair Team · August 26, 2019

1. Features of Python Programming Language

At some point in time, we had about as many programming languages as we could count on our fingers. Today,
there are so many, and all with their own specialities. But what makes a language unique is its features. And
ultimately, it is its features that get it chosen or passed for a project. So before beginning with deeper concepts
of Python, let’s take a look at the major features of python programming language that give you reasons why
you should learn Python as compared to R or other tool. So lets start with the Features of Python
Programming Language.
Features of Python Programming language

2. Easy

When we say the word ‘easy’, we mean it in different contexts.

a. Easy to code
As we have seen in earlier lessons, Python is very easy to code. Compared to other popular languages like Java
and C++, it is easier to code in Python. Anyone can learn python syntax in just a few hours. Though sure,
mastering Python requires learning about all its advanced concepts and packages and modules. That takes time.
Thus, it is programmer-friendly.

b. Easy to read
Being a high-level language, Python code is quite like English. Looking at it, you can tell what the code is
supposed to do. Also, since it is dynamically-typed, it mandates indentation. This aids readability.
Read: Applications of Python Programming Language

3. Expressive

First, let’s learn about expressiveness. Suppose we have two languages A and B, and all programs that can be
made in A can be made in B using local transformations. However, there are some programs that can be made
in B, but not in A, using local transformations. Then, B is said to be more expressive than A. Python provides us
with a myriad of constructs that help us focus on the solution rather than on the syntax. This is one of the
outstanding python features that tells you why you should learn Python.

4. Free and Open-Source


Firstly, Python is freely available. You can download it from the following link
https://www.python.org/downloads/
For insight on how to download and install Python, refer to our tutorial on Python Installation.
Secondly, it is open-source. This means that its source code is available to the public. You can download it,
change it, use it, and distribute it. This is called FLOSS(Free/Libre and Open Source Software). As the Python
community, we’re all headed toward one goal- an ever-bettering Python.

5. High- Level
As we discussed in point 2b, it is a high-level language. This means that as programmers, we don’t need to
remember the system architecture. Nor do we need to manage the memory. This makes it more programmer-
friendly and is 1 of the key python features.Any doubt yet in the features of Python. Please ask.

6. Portable

Let’s assume you’ve written a Python code for your Windows machine. Now, if you want to run it on a Mac,
you don’t need to make changes to it for the same. In other words, you can take one code and run it on any
machine, there is no need to write different code for different machines. This makes Python a portable language.
However, you must avoid any system-dependent features in this case.

7. Interpreted

If you’re any familiar with languages like C++ or Java, you must first compile it, and then run it. But in Python,
there is no need to compile it. Internally, its source code is converted into an immediate form called bytecode.
So, all you need to do is to run your Python code without worrying about linking to libraries, and a few other
things.
By interpreted, we mean the source code is executed line by line, and not all at once. Because of this, it is easier
to debug your code. Also, interpreting makes it just slightly slower than Java, but that does not matter compared
to the benefits it has to offer.
Any doubt or query yet about the features of python programming language? If yes, Drop a comment and we
will get back to you.

8. Object-Oriented

A programming language that can model the real world is said to be object-oriented. It focuses on objects, and
combines data and functions. Contrarily, a procedure-oriented language revolves around functions, which are
code that can be reused. Python supports both procedure-oriented and object-oriented programming which is
one of the key python features. It also supports multiple inheritance, unlike Java. A class is a blueprint for such
an object. It is an abstract data type, and holds no values.
9. Extensible

If needed, you can write some of your Python code in other languages like C++. This makes Python an
extensible language, meaning that it can be extended to other languages.
Let us look at some more Python features.

10. Embeddable
We just saw that we can put code in other languages in our Python source code. However, it is also possible to
put our Python code in a source code in a different language like C++. This allows us to integrate scripting
capabilities into our program of the other language.

11. Large Standard Library

Python downloads with a large library that you can use so you don’t have to write your own code for every
single thing. There are libraries for regular expressions, documentation-generation, unit-testing, web browsers,
threading, databases, CGI, email, image manipulation, and a lot of other functionality.

12. GUI Programming

You can use Tk to create basic GUIs.

13. Dynamically Typed

Python is dynamically-typed. This means that the type for a value is decided at runtime, not in advance. This is
why we don’t need to specify the type of data while declaring it.
This is all about the features of python programming language tutorial. lets get to the conclusion.

14. Features of Python: Conclusion

Now that you know what are the features of python, you know what makes it special. In this tutorial, we learned
about various features of Python. We saw that it is interpreted, dynamically-typed, and object-oriented, among
other python features. It is also portable, free, and easy. Now that’s some motivation to dive into the world of
Python.

Hope you like the Features of Python Programming Tutorial.

If you have any query regarding the features of python programming language, drop a comment below and we
will get back to you.

Refer Top Python Books to deep dive into the Python Programming.

Advantages and Disadvantages of Python Programming Language


by DataFlair Team · December 3, 2018

1. Advantages and Disadvantages of Python Programming Language

When we want to choose a language for a project, we want to be thorough with what we can do with it. We
want to be aware of how it can help us be efficient at what we want to do, but we also want to be careful of the
problems that can arise. So, we believe it is worthwhile to take out some time and find out more. In this
advantages and disadvantages of the Python programming language tutorial, we will learn the advantages and
disadvantages of a python programming language that will help you in knowing the benefits of learning Python
programming.
Advantages and Disadvantages of Python Programming Language

2. Advantages of Python Programming Language

Let’s see why Python is cool.

a. Extensive Libraries
Like we mentioned in our article on Python features, it downloads with an extensive library. These contain
code for various purposes like regular expressions, documentation-generation, unit-testing, web browsers,
threading, databases, CGI, email, image manipulation, and more. So, we don’t have to write the complete code
for that manually.

b. Extensible
As we have seen earlier, Python can be extended to other languages. You can write some of your code in
languages like C++ or C. This comes in handy, especially in projects.

c. Embeddable
Complimentary to extensibility, Python is embeddable as well. You can put your Python code in your source
code of a different language, like C++. This lets us add scripting capabilities to our code in the other language.

d. Improved Productivity
The language’s simplicity and extensive libraries render programmers more productive than languages like Java
and C++ do. Also, the fact that you need to write less lets more get done.

e. IOT Opportunities
Since Python forms the basis of new platforms like Raspberry Pi, it finds the future bright for Internet Of
Things. This is a way to connect the language with the real world.

f. Simple and Easy


When working with Java, you may have to create a class to print ‘Hello World’. But in Python, just a print
statement will do. It is also quite easy to learn, understand, and code. This is why when people pick up Python,
they have a hard time adjusting to other more verbose languages like Java.

g. Readable
Because it is not such a verbose language, reading Python is much like reading English. This is also why it is so
easy to learn, understand, and code. It also does not need curly braces to define blocks, and indentation is
mandatory. This further aids the readability of the code.

h. Object-Oriented
This language supports both the procedural and object-oriented programming paradigms. While functions help
us with code reusability, classes and objects let us model the real world. A class allows the encapsulation of data
and functions into one.

i. Free and Open-Source


Like we said earlier, Python is freely available. But not only can you download python for free, but you can also
download its source code, make changes to it, and even distribute it. It downloads with an extensive collection
of libraries to help you with your tasks.

j. Portable
When you code your project in a language like C++, you may need to make some changes to it if you want to
run it on another platform. But it isn’t the same with Python. Here, you need to code only once, and you can run
it anywhere. This is called Write Once Run Anywhere (WORA). However, you need to be careful enough not to
include any system-dependent features.

k. Interpreted
Lastly, we will say that it is an interpreted language. Since statements are executed one by one, debugging is
easier than in compiled languages.
Python Pros and Cons

3. Disadvantages of Python Programming Language

So far, we’ve seen why Python is a great choice for your project. But if you must choose it, you should be aware
of its consequences as well. Let’s now see the downsides of choosing Python over another language.

a. Speed Limitations
We have seen that Python code is executed line by line. But since Python is interpreted, it often results in slow
execution. This, however, isn’t a problem unless speed is a focal point for the project. In other words, unless
high speed is a requirement, the benefits offered by Python are enough to distract us from its speed limitations.

b. Weak in Mobile Computing and Browsers


While it serves as an excellent server-side language, Python is much rarely seen on the client-side. Besides that,
it is rarely ever used to implement smartphone-based applications. One such application is called Carbonnelle.
The reason it is not so famous despite the existence of Brython is that it isn’t that secure.

c. Design Restrictions
As you know, Python is dynamically-typed. This means that you don’t need to declare the type of variable
while writing the code. It uses duck-typing. But wait, what’s that? Well, it just means that if it looks like a duck,
it must be a duck. While this is easy on the programmers during coding, it can raise run-time
errors. In Any query regarding the advantages and disadvantages of Python programming language, tutorial feel
free to drop a comment.

d. Underdeveloped Database Access Layers


Compared to more widely used technologies like JDBC (Java DataBase Connectivity) and ODBC (Open
DataBase Connectivity), Python’s database access layers are a bit underdeveloped. Consequently, it is less often
applied in huge enterprises.

e. Simple
No, we’re not kidding. Python’s simplicity can indeed be a problem. Take my example. I don’t do Java, I’m
more of a Python person. To me, its syntax is so simple that the verbosity of Java code seems
unnecessary. This was all about the Advantages and Disadvantages of Python Programming Language.

4. Conclusion – Advantages and Disadvantages of Python Programming Languages

Concluding the tutorial on advantages and disadvantages of Python Programming Language I would say while
there are some speed, security, and runtime issues, Python is a great language to pick up. Its popularity speaks
for itself. And this popularity is attributed to its being free, easy, interpreted, object-oriented, extensible,
embeddable, portable, and readable. Now that you know the advantages and disadvantages of python
programming language, tell us in the comments if you would choose it for your next project. We would love to
hear from you.

Hope you like the Advantages and Disadvantages of Python Programming Language Tutorial

Learn Python through the Master Guide – Python Notes for


Beginner to Advanced Learners
by DataFlair Team · September 10, 2019

The best way to initiate your steps towards any new language after choosing the language you want to learn is
mind mapping the process you will go about throughout the learning process. It basically starts with how you
plan to learn the language, followed by the sequence of topics you will cover to become completely skilled in
that language. Once you have the idea of what are the major sections to cover with possible background
information,
you will be able to cope up well with language and the knowledge gaining procedure too! The same thing will
happen when you learn Python programming language with DataFlair.

Focusing on how to plan so that one can keep up with the changes that usually and gradually take place in a
growing language like Python. We recommend you to code every day and side by side following the sessional
topics. Start with 25-30 mins on a daily basis, it will help you to develop that muscle memory.

While you move from one Python topic to another, do take time to make notes and jot down information that
you will require for the long-term. This has proven beneficial for users who are aiming to become a full-time
developer. This habit of making Python notes can help you plan your code before you move to the computer
when you go for any small/big projects.

With these strategies, you are ready to learn Python. I’m providing the complete series of Python and its
resources so that you can have exposure to other topics not listed here and explore the best in Python.

Learn Python Programming from Scratch

Python notes for beginners


 Introduction
 Syntax
 Statement, Indentation, and Comments
 Variables and Datatypes
 Operators
 Numbers
 Strings
 Data structure
 List
 Tuples

Python notes for intermediates


 Module
 Classes
 Methods
 Iterators
 Decorators
 Generators

Python notes for advanced learners


 Web framework
 Machine Learning
 Deep learning
 Artificial Intelligence
 Relational Database

These blogs are not supposed to be accessed and learned at once. As suggested above, one can plan out the
whole process to learn Python and then navigate through them according to their grasping capabilities and
practice.
Python Notes

Now, in these python notes, the first part is learning Python beginner-level topics

Learn Python Beginner Level Topics


Below is the list of Python topics for beginners that will help you to learn Python from scratch

1. Python basic tutorial


It covers a general introduction of the Python language with its key features, architecture, and applications. It
will provide you with answers to questions like- why choose python and what its future holds.

For further reference


-Features
-Applications
-Future opportunities

2. Python syntax
Door to learn any language, not only technical but any language in general, one has to have strong command on
its grammar. Grammar in a technical scenario means syntax. It will demonstrate to you how Python’s syntax is
different than Java and C++. You will unfold the use of different identifiers and variables and also how they are
different from each other.

3. Python statements, indentation and comments


As you are getting used to Python and its syntax, you can move forward with the next level of understanding,
i.e. statements and indentation. Python overall is not considered a strict language to follow, the only thing that is
needed to be taken care of is the indentation. It increases its reliability and effectiveness.

Throughout the journey of learning Python, conditional statements like ‘if’, ‘for’, ‘while’ etc will be observed
constructing the logical code for the given problem. Whereas “Python comments” will be used for
understanding the code written by the coder and make changes accordingly. Hence good hold of “comments”
might not help you in coding but will improve your presentation skills of the code when you present it in front
of its target audience.

4. Python variable and datatypes


To give a general idea, we don’t specify the type of a variable when declaring one also there are some set of
rules of naming a variable. Although we don’t declare the type of data, but a value certainly has a type that is
previously known to the interpreter. It includes- integer, float, long, complex, etc.

5. Python Operators
It is responsible for performing various mathematical and logical operations in the code. The operand is a value
on which the operation is performed. 7 main categories are present in which operands are divided for proper
programming from- Arithmetic, Relational, Assignment, Logical, Bitwise, Comparison, Membership, Identity,
etc. So, in the journey of learning Python, Python Operators plays a major role.

6. Python Numbers
An entity that lets you measure something is termed as numbers. It will allow you to dig a little deeper into the
number types of Python, i.e. int, float, complex numbers and so on. Programmers often deal with numbers in
binary, octal and hexadecimal, and their interconversion.

7. Python Strings
It will cover the general introduction of a string ( a sequence of characters ) and its examples. Also, look at
Python string functions and its operations and how to access it.

8. Python Data Structures


Different data structures of python will be covered that includes list, tuple, set and dictionaries. It is nothing
but a way of organizing, storing, and accessing data. According to their advantages and disadvantages, one can
decide which data structure will be suitable.

9. Python List
How to create, access, slice and re-assign list in Python will be discussed. Also, how to apply functions to them
by covering various operations and concatenation. A general idea of the list will be provided in the Data
structures, here you will come across a detailed study. Learn how to delete or reassign elements or list and some
built-in functions and methods.

The next topic in beginner level Python notes is Python Tuples.


10. Python Tuples
A deeper look than that of Data Structure material and insights of how to create, access, slice, delete tuples.
Also, look into inbuilt functions, methods, and operations that can be called on a tuple.

Before moving forward to the next level of learn Python mission do take a while to understand the building
blocks of Python language. You do not need to learn “all” the Python syntax before you start to do something
interesting with it but you should have a good hold on the basics mentioned above. But don’t spend months on
it, with practice and good revision plan one can easily grasp the concepts.

Learn Python Intermediate Level Topics


The next level in learns Python mission is intermediate level. So, let’s start exploring Python notes –

1. Python Module
Learn about modules- how to create them, import modules in python, how can we execute module as a script,
standard python modules, python dir functions and lot more! Have a glance at packages vs modules section to
get an idea of Packages too and their advantages and disadvantages over each other!

No idea about Packages? Work it out here! – Python Packages Guide

2. Python Classes
It is nothing but a blueprint for objects. Explore more about how they work and access. Different attributes
belonging to a python class with its objects is discussed. As python is an object-oriented language, it focuses on
objects more than procedures. Though it is too much to take in, still have good hands-on these concepts as their
understanding will help a lot later.

3. Python Methods
Much like functions, they are labels that you can call on an object; a piece of code to execute on that object. At
the end of it, you will know how to define and make use of _init_ method, and also a self parameter. Here and
onwards, have a good hold on data structure for better combinational implementation.

Can’t differentiate between Functions & Methods? Refer here! – Python Methods vs Functions

4. Python Iterators
Create your own methods with Python Iterators. Also, create ‘for loop’ in iterators, infinite python iterators,
and benefits of iterators with examples. After their complete understanding, they become handy and very
interesting to use.

5. Python Decorators
What are they and why we use them, all here! Also, along with this, cover decorators with Parameters and Pie
syntax. Decorators, nothing but help add extra functionality to a function without modifying it.

6. Python Generators
Generators are kind of iterators that generate for us a sequence of values that we can iterate on. Example: Use
it to iterate on a for-loop in python, but you can’t index it.

Before you start diving into the supreme and advance level of learn python mission, it’s good that you ask
yourself ‘why you want to learn it?’ This is because Python has a ‘sea’ of applications and each one of them is
in the growing stage. So is going to be a long journey and sometimes painful too! Figuring out what actually
you want to pursue will really help to find the end goal and set paths towards it.

Learn Python Advanced Level Topics


Ready to explore Python notes for advanced level topics? Let’s start with Python and web framework –

1. Python & Web-Framework


If web-development is the goal then python will provide help with a range of frameworks. It includes a full-
stack framework and a non-full stack framework. It allows developers to write web applications and services
without worrying about protocols or thread management. Example: Django, Flask, etc.

For Reference-

Django

Flask

2. Python & Machine Learning


It aims to grant machines the ability to learn by using various techniques. Data is used to learn from and make
further predictions in various applications. Python has a number of characteristics that make it the language to
be chosen for Machine learning and its uses in the industry.

Python has a major contribution in Machine learning application. Know how!

3. Python & Deep Learning


Discuss what exactly is deep learning and why Python adds glory to it? Also, see its uses in artificial neural
networks and deep neural networks. Look into some deep learning applications and their analysis to the reason
why Python?

4. Python & Artificial Intelligence


Starting with a general introduction then AI programming that will include problems, tools and approaches.
With the journey of AI, Natural language processing will be introduced with its components, libraries and its
benefits. We will also come across speech recognition and the NLTK toolkit with their components.

Don’t forget to check the best tutorial on Natural Language Processing

5. Python & Relational Database


It’s working with Python. Learn to read CSV files, insert data into them, and delete data from them using simple
query language-SQL. Come across some important prerequisites of the Relational Database. Pandas is one of
the main Python libraries for data manipulation and analysis.

For reference – Python Pandas Library

These resources are presented in a way that assists your journey of mastering Python. There may be other
possible ways too but this one was found to be very effective and promising as it serves well for mostly all kinds
of users.

Summary

So, here completes our learn Python mission. We all know Python is evolving with time and one cannot
confidently say that he/she has the “complete” knowledge and understanding of Python and its area of
application. A constant need for learning is a requirement to master it to a larger extent. Working on the things
that interest you will take you one notch higher than before. Python is a very rewarding language and with its
help, anyone can do wonders! All you need to do is keep a constant check on what you already know and what
new you need to add on to your skillset.

Hope you like these Python Notes.

Google, Facebook, Netflix, Quora – the


Secret Ingredient – Python!
by DataFlair Team · September 9, 2019

From the time Python has come into existence, its popularity has started rising since then and reached to the tag
of “most-popular programming language” now. Python was and still is the choice for most of the startups
because of its low cost, simplicity and various different frameworks. On top of every other thing, it’s free, open-
source, and has a mighty army of developers that have been formed over the years. Giant companies like
Instagram, Spotify use python and appreciate its use in bulk by others too! Let’s look at some Python Case
Studies or I can say entrepreneurial cases written and solved by Python.
Python Case Studies
Starting with the very first Python case study that is Spotify, then we will discuss other companies like Quora,
Netflix, Google, and Facebook.

Spotify
One of the music streaming giant which is using Python as its sword primarily for data analysis and backend
services. It is one of the major market players and amongst top Python users. Overall backend communication
takes place with the use of ZeroMQ, an open networking framework written in Python and in C++. The reason
behind the choice of Python’s services is the fast development pipeline during coding. The suggestions and
recommendations on Spotify largely depend upon volumes of data analytics. Hence as an interpreter for this
stream of data, Spotify uses Luigi, a Python module that syncs with Hadoop. This is how Spotify also manages
functions such as “Radio and Discovery”. Python handles how libraries work together and cross-check errors
quickly to allow troubleshooting and redeployment. Around 6000 individual processes work together now in
Spotify over the nodes of the Hadoop cluster.

Quora
So, after knowing how Spotify works using Python, now let’s move on to another Python case study that is
Quora. It is a social network platform of question and answer. There was a lot of consideration before choosing
Python amongst C#, Java, and Python. Lack of type checking and relative slow behavior was drawing back the
interest from python. They also had reasons for not choosing other languages. They didn’t go with C# because
of it’s proprietary Microsoft Language and didn’t want to put efforts to cope up with future updates to it. For
Java, its strict syntax was enough to get rejected. Also, Java was a little less older in the market so there were
doubts about its future growths. So the founders of Quora got inspired by Google and choose Python for its ease
of writing and readability. They got rid of their initial problem of type checking by writing unit tests that
accomplish the same
thing. Another reason for the choice was again the good framework support that python provided, such as
Django and Pylons. Having Python and JS together for handling user interactions was also a plus point.

Netflix
The use of Python by Netflix is very similar to that by Spotify. That is they rely on python for data analysis of
the server-side. A significant increase in the number of users of Python in Netflix itself is because they allow
their engineers to choose the language of code. They prefer the standard and third-party libraries and enjoy the
extremely active community. Also, Python is so easy to develop that the developers are left with no choice other
than python! The primary use of Python is in the Central Alert Gateway. This app process alerts, suppressing
the duplicate ones and then route them to people that need to see them. This app has proven to a good addition
to Netflix as it can give automated solutions to shaky problems such as terminating the process or rebooting.
Secondly, the monkey app used to track down security and history issues has also proven to be a hit. It is also
used to ensure to track dozens of SSL certificates related to Netflix’s domain.

DataFlair has published this Netflix Python Case Study in detail. Have a look – how Netflix uses Python

Facebook
This platform is an exceptional user of python because of its developer’s extensive use of the language. This is
so because of the Python libraries and framework reduce the “code-load” and they can majorly focus on
“actual” improvements. It is also noticed that the infrastructure of Facebook has become more scale efficient.

According to a 2016 post by Facebook, “Python is currently responsible for multiple services in infrastructure
management.”

There are various Py3 written projects of Python that were published by Facebook including Facebook Ads API
and a Python async IRC bot framework.
Facebook, upgrading their infrastructure and handlers to 3.4 from 2, and guess who is helping their engineers in
the process – AsyncIO!

Google
Above any company, Google has been a super supporter of Python, that too from the very beginning. There
decision – “Python where we can, C++ where we must” stated that python will be implemented for enabling
ease of maintenance and relatively fast delivery than C++.

Some scripts which were originally written in Perl or Bash were re-coded into Python. It is now the official
Google server-side language. To top it all off, Peter Norvig said:

“Python has been an important part of Google since the beginning and remains so as the system grows and
evolves. Today dozens of Google engineers use Python, and we’re looking for more people with skills in this
language.”

Summary
Python initially was used as a language for drafts as it was simple and cheap. But as we know that “simple is
reliable” and so is python. Many companies learned it later and a lot harder way that more parts a mechanism
has more mess it creates. That is why they choose python and now it covers most of the world’s popular apps
built-
in mechanisms. It is proved in black and white by Python that an amazing product can be backed up by simple
and time-honored tools. As long as the users are satisfied with the performance, there is no loss!

Hope you liked these top Python case studies. I am sure you also want to share some more amazing Python case
studies, do share them with comments.

Learn everything about Python through DataFlair’s best ever Python Tutorial Series. Save this now for FREE
and you can thank me later

What are the Reasons behind Increasing Demand for Python?


by DataFlair Team · July 17, 2019

With every inch of time, we move forward, we can observe that Artificial intelligence and Machine Learning
are becoming the shine on the eyes of every developer. Which is why, beginner or expert, all are driving the
“popularity traffic” towards Python. This is the reason for the increase in Python demand.

Claimed by some old school coders/working professionals, there are some negative conceptions concerning
with the “non-serious” nature of Python. Yet still, increasing demand of python programmers seems to be at
odds with this idea. For proving the same DataFlair have set down some key points.

Why Python Demand is high?


Below are the reasons for the increase in demand for Python –
1. Data Science
Python holds a special place in the hearts of Data Scientists compared to any available language, such as R or
C++. Data Science is all about dealing with data at huge amounts (Big Data). Hence with simple usage and a
large set of libraries and frameworks, Python has become the most promising option to handle it! e.g. PyBrain,
PyMySQL, and NumPy are one of the big reasons. Another step forward is because of Python’s easy integration
with other programming languages, making it more scalable and future-oriented.

2. Machine Learning
Python being an interpreted language makes it comprehensive enough for the language to be interpreted by
virtual machine against any other machine language which is what the hardware understands. It can even be
used in complicated scenarios by making use of variables, objects, complex arithmetic or boolean expressions
and other concepts to make its demand and usability increase exponentially. Even the growth of machine
learning has been phenomenal in the last couple of years and is rapidly changing everything around us.
Algorithms are becoming sophisticated every day. Examples are: Google’s search algorithms and Uber are
completely driven by algorithms. The machine learning libraries compared to Java, found more content around
Python as the current preference of the developer community is Python over anything else for machine learning.

3. Web Development
While concerning backend development, Python is chosen by 2 out of 3 developers who initially worked with
PHP. Python’s trend has been witnessing a steep upward in the past two years as it is serving as a better
alternative. It offers so many good libraries and frameworks, e.g. Flask and Django, which make web
development easy. After adopting Python some of the product based platforms have become the biggest names
– YouTube, Instagram, Facebook, Google, Netflix, and Spotify. Considering the general perception towards
python, in web development Python creates more robust code that can be used to form versatile use cases.

4. Simplicity
Single yet the biggest reason for beginners to learn Python. Whenever you decide to start learning
programming and coding, you don’t want to start with a programming language that is complex in syntax and
tough with rules. Python is readable as well as simple. It’s even easy to set up Python; There’s nothing like
classpath problems like that in Java and compiler issues present in C++. Just install it and run it! Whenever you
write something in Python, you will want to show off to others as it is so elegant to look, not generally in the
case with typically written languages.

5. Huge Community
Mostly decision are community influenced and when to learn something new you need a community and friends
which serve as your biggest asset. Thanks to Google, many solutions to single Python-related problem can be
found out in minutes. With the progression of time, learning python has become the new “cool” in the
programming community. As Python has such vast use cases like development, scientific application, scripting
and so on, hence it brings many Python experts together. The more the merrier!
6. Libraries and Frameworks
Not just a framework but it has got “superior framework”. Django is the clear winner when python’s
frameworks are considered. Frameworks are easy to use, secure and fast. Mostly developers prefer these
frameworks, as their use results in shorter development time and ease of setup. The richer the framework, more
deeper one should learn as it will translate into a lot of long-term valuable results.

7. Automation
You are required to write test scripts to automate tasks, that’s where Python comes into existence in automation.
You’ll be impressed with the time and the number of lines required to write codes for tools. As python supports
with lots of tools and modules, it makes things easier and even highlight the power of Python. With basic
python codes, one can reach the advanced level of automation easily. Software testing is one of the tedious tasks
in automation and python becomes its performance booster!

8. Multipurpose
Swiss Army knife-like nature describes well the overall work of python. It’s not tied to just one discipline, you
can do many things. You don’t really need to fetch data from a SQL server or a MongoDB database; Python
supports all these sources of data with very clean syntax use. Python API called PySpark can be used to
distribute computing. It also provides support for Natural language processing through NLTK.

9. Jobs and Growth


Python is growing as fast as it seems that everyone is adopting python. So, it makes a lot of sense to learn a
growing programming language – Python. As the number of python users are increasing, the language is
becoming more and more popular. It will not only help you to get a job quickly but will also accelerate your
career growth. For beginners, one of the most important reasons to learn Python after simplicity is job
opportunity.

10. Salary
Last but most important – money talks! Salary keeps every individual going. Python developers are one of
the highest-paid jobs available. Particularly in the fields of data science, machine learning, and web
development, these jobs shine. Though much of the factors contributing to it are also dependent upon
experience, location, and area of specialty.

Conclusion
With the increasing demand of programming and coding in today’s world, you are really missing something if
you don’t know Python. Nevertheless, it is even a great way to start learning experience of coding.

Programmers who already know C/C++ and Java, learning Python will be a great opportunity to achieve more
in less time in the exciting fields of data science, machine learning and a lot more which is in demand! To learn
Python you can check the complete Python Tutorial Series for free.
Python’s Future is even above the ‘C’ level!
by DataFlair Team · July 9, 2019

Imagine everything that you are surrounded by is raw, unstructured, incomplete, vast and just Data!
Python will definitely be there for your rescue!

Python is the Future and it will serve as a huge, reliable, effective and ready-to-use technology. What it can do
for us? Well, it can play around with data, visualize the data, transform inputs into a numerical matrix, or actual
machine learning and assessment.

All that needs to be done is to write the code — that would glue everything together. As simple as that!

What will be the Future of Python?

Laying some grounds on which we will define the future of Python.

How are we defining the popularity of programming language?


According to recent studies, Python showed 456-percent growth. Quite a number, isn’t it? In business terms —
Netflix, IBM, Dropbox and hundreds more utilize Python as their success key!

If you want to explore Python more, you need to check how Netflix is using Python

How are we defining Python’s popularity in a specific field?


Python is and remains a stable programming language currently used not only by developers but it is considered
great for deployment automation and web development. Non-developers when first introduced to the Python
language and its ecosystem, also prefer it for doing data work.
Who is driving the growth till now and will drive it in the future?
The sudden rise of demand for python and every platform becoming a “python-oriented-platform”, isn’t
everything just getting “pythonized”?

First of all, man’s life chasing point – money!

For future and present programmers, Python is becoming the first-choice language for learning it to get
“actual jobs”. It is also being used for many purposes, from web development to mobile app development to
data science. However, Data science is attracting the mob towards Python.

Secondly, Python’s serious versatility. Python is a multipurpose language used for various tasks, as seen
above. “Pandas” is by far the fastest-growing Python package. Therefore it seems clear that the rise of data
science is responsible for the growth of Python as “it’s” programming language.

Finally, Machine Learning. There is no special programming language dedicated to Machine Learning, but
looking at the characteristics of each language that can do ML, choose the best that will fulfill their needs,
Python. It is one of the most popular and the best languages for Machine Learning.

Python’s growth is for now!? or this growth has some Future!!??


Trends somehow define the need and needs definitely define the actual trends. There’s no doubt that Python is
now in “the trend”. All thanks to its popularity and the helpful community, Python is really friendly. Since it is
so easy to learn Python, you can start your python journey at this moment itself. That is not all, it is the way it
lets developers express their thoughts in a simpler way, adds on to the “choose python!” thing.

Python in Robotics and Machine Learning


The use is not little but extensive. It is a gift to the systems originally written in C and C++ as it makes it easy to
interface with Python. Anywhere that data analysis is required, Python and its diverse libraries shine.

At the end of the day, everyone is aware of how important machine learning is in the modern world. From
moderating social networks to driving cars, trained neural networks are doing everything. The field has moved
forward incredibly in recent years due to quickly increase in processing power. The potential for machine
learning is vast, and not yet even close to fulfillment.

What has this got to do with Python?


A lot. While there are libraries designed to work with other popular languages, Python is in actuality language
of machine learning. Notably, Google’s TensorFlow works primarily with Python. Almost every course on
neural networks uses Python.

Machine learning as a skill is in greater demand every day. A good grasp of the Python programming language
puts you in a leading position amongst others learning it from scratch.

Python being open source


Saying Python is free may not take your interest in it at first glance. After all, the language you decide to pursue,
simply using it, doesn’t actually cost anything. Python is free as in ‘Open Source’. It is entirely Open Source
and GPL compatible. Open-source software has already changed the world. Python’s being a perfect tool even
takes the “no-strings-attached status” to complement its use.

Python is blooming
Over the years, Python has swept into the coding world and has managed to reach the top. The survey and job
research in major countries calculated that over 40,000 jobs are entirely for python experienced developers. To
add on, internet search tags are also higher for python only in those countries which helped it to make it to the
top! It is no wrong in saying that Data science combined with python has the best career and future
complementing each other and to new heights.

You must check the latest article on Python Career Opportunities

Conclusion

We’re going to see continuous growth in the scientific programming part of Python. So things that support the
performance of Python as a language and its stability are going to continue to evolve. Beyond that, Python is a
pretty powerful and solid language. Even if you stopped development today, Python is a good sewed up
language.

Not trying to declare a war between language as to make a belief that Python’s the king. Whereas, it is crucial to
understand that trends might be changing, and there’s room for discussion left for everyone. We live in a data-
driven world and we’re really setting Python up for another 20 years of success and growth.

Hope you liked our Python Future article. Drop your feedback in the comment section, it means a lot to us. We
recommend you to try this DataFlair’s Python Self-paced Training to become a master of Python.

How to create a perfect Python Resume – Let your resume


speak!
by DataFlair Team · September 10, 2019

Python Resume – Not just a piece of paper but a way to outshine from other candidates.

When I created my resume for a Python job, I remember that I was not able to figure out what skills to add and
what all certifications should I mention. And, after checking 4-5 websites on google, I somehow found an
appropriate article which helped me in creating my first Python resume.

You don’t need to scroll through these much websites, DataFlair is here for your rescue. I have collected all the
essential points and strategies that you need to follow while creating your Python resume. Make sure to read
till the last as without knowing the last point, you won’t be able to get interview calls.

So, why wait? Let’s unlock your door to success in Python programming.
How to put Python in your Resume?
Follow the below steps to build Python resume for fresher as well as experienced:

1. Keep It Brief
The very first important thing you should strive for Python resume is – keeping it short and sweet! It should not
be any longer than a page unless you are an experience holder of 12-15+ years. On average, the recruiters have
about 30 seconds to go through the resume and make a decision, so it should be very clear. Jot your experience
down to the most important points and make it easily scannable.

2. Customize the Resume according to the Python Job


description
Add or subtract small details in your resume for each application you submit instead of giving out a single
resume to every job. You may also want to have a look at the company’s website to try to get an idea of what
style, tone and writing adjustments they will prefer and change your resume for Python job accordingly.

“The most important tool you have on a resume is language.”

– Jay Samit

3. Choose Proper
Templates
While all the resume involves information like work experience, skills, contact information, etc but it should be
unique to you. It can also be accomplished by changing the looks or design of the resume. For example- if you
are applying for companies with a traditional feel, aim for a classic or subdued style for resume whereas more
colors can be added for startup vibe companies. Even though you start with a template, take time to personalize
it.

4. Contact Information
This piece of information should be kept at a very approachable position. In case the recruiter decides to contact
you based on your resume of Python, you don’t want them to search through the whole resume. Double-check
all the information to assure that they are right. Make sure your headline reflects what you are aiming through
the job and not the one you currently have. If you are trying to become a Python Developer, your heading
should be “Python-developer” even if you are working as a marketing intern.

5. Work Experience
The most recent job should be listed on top, with a job below that, and so on in chronological order. Each entry
written in this section should include the job title, the company, the period of time you held the position, and
your accomplishments. Employers want to see what you actually did, not just what you were assigned. Frame
your Python accomplishments in the context of business metrics to show that you are aware of the bigger
picture and can translate your work results into real-world outcomes.

6. Education
The things you should list here are post-secondary degrees (i.e. community college, college, and graduate
degrees). If your degree is not relevant to the job you are applying for, still list it. Many Python developers are
from a variety of fields that do not have a computer science background. Don’t list your micro- degrees here,
like certifications or professional training. We will list them later.

7. Skills and Certifications


For technical positions, the skills section is not optional but a necessity. You want to make sure that the terms
like Python, Data Science or Machine Learning are highlighted. Also, list your soft skills like communication
or leadership here. Don’t rank your skills, it can lead to overpromise or sell yourself short. You can list skills
you are strongest at first and weakest later but do not overload this section with unnecessary details. Here you
can specify your professional training or Python online certifications. Coding based hackathons are a huge plus
point in resumes. Shows your healthy competitive spirit and confidence in your skills.

8. Add Python Projects and Applications


The main thing that you have to highlight is what you have created. For Python-developers, this could be Data
Science projects, machine learning projects, Web-development projects, published scientific articles or coding
tutorials. The companies are actually looking for what you actually do with your skills instead of what skills
you have. They don’t aim to see whether the solution was “cool” or not or how technically difficult the
challenge you took. It is all about whether you were able to solve the problem or not with your skillset. Hence
definitely include projects in your resume but their description should be very specific, including all main
tools and technologies you used and how you created it i.e. individual contribution.

WORRIED? Not done any Python Projects yet? Don’t worry, DataFlair is here to resolve this
problem. Check out 5 Python Projects from our Certified Python Training Course

Summary
Of course, a resume doesn’t mean you have to prove that you’ve got the skills that you have listed. You just
have to let the recruiters know that you are capable to handle that position you are applying for and also will be
very productive towards it. Hope this tutorial of Python resume creation helped you.

Now, it is the right time to prepare yourself for Python interviews. Here are the Python Interview
Questions and Answers that were asked to me.

Practice these questions. Don’t forget to share your valuable feedback with us.
Happy learning

Python Career Opportunities – Python Job Profiles


by DataFlair Team · February 20, 2019

1. Python Career Opportunities

We’ve been seeing a lot of Python every day here, but what is it all worth? Sure, Python is easy, and cool to
learn, and powerful, but what do you get in return? To answer questions like these, we base today’s article on
the Python Career Opportunities and Python Scope it gifts you. In this tutorial on Python Careers, we talk about
India. Read on.

2. Number of Python Jobs

While there’s a high demand and career opportunities for Python developers in India, the supply is really, really
low. To testify this, we’ll take account of an HR professional statement. The professional was expected to
recruit 10 programmers each for both Java and Python for a few projects. About a hundred good resumes
flooded in for Java, but they received only 8 good ones for Python. So, while they had to go through a long
process to filter out good candidates, with Python, they had no choice but to take those 8 candidates.

What does this tell us about the situation? Even though Python has really easy syntax, we really need more
people in India to consider it. But then, this is what makes it a great opportunity for an Indian with the skills.
When we talk about the number of jobs, there may not be too many for Python in India. But we have an
excellent number of jobs per Python programmer. This a good news about Python Careers

Not very long ago, one of India’s unicorn software companies faced a dilemma. It had won a $200 million (Rs.
1200 crore) contract with a large US bank to develop an app store for them. But the company lacked enough
dexterous Python programmers. Since Python was the best language for the project, it ended up paying thrice
the billing amount to a group of freelance Python programmers in the US instead.

Job boards like Indeed and Naukri offer around 20,000 to 50,000 job listings for Python and this shows that
Python career opportunities in India are High. Python Careers are good to go with. The below screenshot from
indeed job trends shows job trends in Python compared to other languages.
Python Career Opportunities – Python job Trends

Source: Indeed Job Trends

3. Types of Python Jobs

So what types of jobs can you land with Python?

Well, for one, Python Scope is intensive use in data science and analysis. Clients often want hidden patterns
extracted from their data pools. It is also preferred in Machine Learning and Artificial Intelligence. Data
scientists love Python. Also, in our article on Applications of Python, we read about NumPy, SciPy, scikit-
learn, pandas, IPython notebook. These are some useful libraries available for Python, and they let us explore
the advanced areas of Python and different Python career opportunities.
Python Career Opportunities – Python Careers

a. Job Profiles:
With Python on your resume, you may end up with one of the following positions in a reputed company:

i. Software Engineer

 Analyze user requirements


 Write and test code
 Write operational documentation
 Consult clients and work closely with other staff
 Develop existing programs

ii. Senior Software Engineer

 Develop high-quality software architecture


 Automate tasks via scripting and other tools
 Review and debug code
 Perform validation and verification testing
 Implement version control and design patterns

iii. DevOps Engineer

 Deploy updates and fixes


 Analyze and resolve technical issues
 Design procedures for maintenance and troubleshooting
 Develop scripts to automate visualization
 Deliver Level 2 technical support
iv. Data Scientist

 Identify data sources and automate collection


 Preprocess data & analyze it to discover trends
 Design predictive models and ML algorithms
 Perform data visualization
 Propose solutions to business challenges

v. Senior Data Scientist

 Supervise junior data analysts


 Build analytical tools to generate insight, discover patterns, and predict behavior
 Implement ML and statistics-based algorithms
 Propose ideas for leveraging possessed data
 Communicate findings to business partners

4. Future of Python

In our write-up on Applications of Python, we saw where Python finds its use. But what about the future?
While many top companies are stuck with Java, Python is one of the new technologies. The future is bright for
Python with:

Python Career Opportunities – Python Future

a. Artificial Intelligence
Artificial Intelligence is the intelligence displayed by machines. This is in contrast to the natural intelligence
displayed by humans and other animals. It is one of the new technologies taking over the world. When it’s
about AI, Python is one of the first choices; in fact, it is one of the most-suited languages for it.
For this purpose, we have different frameworks, libraries, and tools dedicated to let AI replace human efforts.
Not only does it help with that, but it also raises efficiency and accuracy. AI gives us speech recognition
systems, autonomous cars, and so. The following tools and libraries ship for these branches of AI:

 Machine Learning- PyML, PyBrain, scikit-learn, MDP Toolkit, GraphLab Create, MIPy
 General AI- pyDatalog, AIMA, EasyAI, SimpleAI
 Neural Networks- PyAnn, pyrenn, ffnet, neurolab
 Natural Language and Text Processing- Quepy, NLTK, genism

b. Big Data
Big Data is the term for data sets so voluminous and complex that traditional data-processing application
software are inadequate in dealing with them.

Python has helped Big Data grow, its libraries allow us to analyze large amount of data across clusters:

 Pandas
 scikit-learn
 NumPy
 SciPy
 GraphLab Create
 IPython
 Bokeh
 Agate
 PySpark
 Dask

c. Networking
Python also lets us configure routers and switches, and lets us perform other network-automation tasks cost-
effectively. For this, we have the following libraries:

 Ansible
 Netmiko
 NAPALM(Network Automation and Programmability Abstraction Layer with Multivendor Support)
 Pyeapi
 Junos PyEZ
 PySNM
 Paramiko SSH

All these technologies rely on Python today and tomorrow.

5. Top Organizations Using Python

With its extreme popularity and powerfulness, Python is preferred by unicorns too:
Python Career Opportunities – Top Companies Using Python

a. NASA
The National Aeronautics and Space Administration uses Workflow Automation System (WAS), an application
written and developed in Python. It was developed by NASA’s shuttle-support contractor USA (United Space
Alliance).
NASA also uses Python for APOD(Astronomy Picture Of the Day), API, PyTransit, PyMDP Toolbox,
EVEREST.

b. Google
Who, on this Earth, lives and doesn’t know Google? We use it for everything- sometimes, even to find answers
to life’s deepest questions. Google uses Python for its internal systems, and its APIs for report-generation, log
analysis, A/Q and testing, and writing core search-algorithms.

c. Nokia
This one reminds me of Nokia 3310, that pocket phone that could break a tile. Nokia makes use of PyS60
(Python for S60). It also uses PyMaemo(Python for Maemo) for its S60(Symbian), and Maemo(Linux) software
platforms.

d. IBM
An American multinational technology company headquartered in New York, IBM uses Python for its factory
tool control applications.

e. Yahoo! Maps
Maps is an online mapping portal by Yahoo! It uses Python in many of its mapping lookup services and
addresses.

f. Walt Disney Feature Animation


WDFA uses Python as a scripting language for animation. All the magic that happens in Disneyland has a bit of
Python behind it.

6. Payscale in Python

In section 4, we saw a rough approximate of how much a Python professional makes. In section 3, we saw some
job profiles. So, how does each profile fair in this department?

Python Career Opportunities – Python Salary

 Software Engineer – $103,035/yr


 Sr. Software Engineer – $129,328/yr
 DevOps Engineer – $115,666/yr
 Data Scientist – $117,345/yr
 Sr. Data Scientist – $136,633/yr

These statistics have been sourced from payscale.com and indeed.com.

7. Why Must You Learn Python

So, after all this Python career opportunities talk, why must you learn Python? What has it to offer to you?
What is the cope of Python? Let’s see.
Python Career Opportunities – Why Python

1. Its simplicity and conciseness make it perfect for beginners.


2. It has a large community that continuously contributes to its development.
3. Because of the highly demand-supply ratio, it provides excellent career opportunities, especially in
India.
4. We have a number of frameworks to make web development easy as pie.
5. Python is the chosen language for Artificial Intelligence and Machine Learning.
6. Raspberry Pi, a microcomputer, lets us make our own DIYs with Python, at prices that do not blast holes
in your pockets.
7. Both startups and corporates, make extensive use of Python, thanks to its powerfulness and simplicity.
8. Python replaced Java as the second-most popular language on GitHub, with 40 percent more pull
requests opened this year than last.
Python Career Opportunities

Source: GitHub –The State of the Octoverse 2017

So, this was all about our blog post on Python Career Opportunities

8. Conclusion: Python Scope


Now that you know what doors Python can open for you and what are the different Python Career
opportunities, which one will you take? Let us know in the comments.

Want to crack your upcoming Python Interviews? – Practice Most Asked Python Interview Questions

If you have any question on Python Career Opportunities, please drop a comment.

How to get a Job in Python as a Fresher? The essential steps you must
follow!
by DataFlair Team · September 24, 2019

Python has changed the industry with its ease-of-use and powerful libraries, and its vast applications and
productivity. Python jobs pay well and developers are in demand. And since Python is easy, many choose to go
for a lucrative job in Python. Are you dreaming of a job in Python too? And wanted to know how to get a job in
Python as a fresher? Let’s find out.

Can a Fresher get a Job in Python?


Yes! If you’re a fresher, you can definitely secure a job in Python. Get ready to perfect your foundation and
make your dreams come true. Read on to learn how to get a job in python as a fresher.

How to get a Job in Python as a Fresher?

There are two ways to learn Python

 One is through a quick, inclusive online course, and the other will take almost a year. If you learn
Python through an online course, you’ll be sure you’re not missing anything, will get more insight based
on what others think, and also finish practicals and build projects.
 If you rather don’t want to spend on this, you can go for free online tutorials at your own pace. But it
will not guarantee success and will be slow.

Who knows what the competition will be in a year? This is the right time to learn Python but with correct
guidance. The time is not far when every other person is learning Python technology. That’s why I recommend
you to master it as soon as possible and become top of everyone.

Sharing both the ways with you. All up to you what to choose.

1. The Quick Way – Online Course


DataFlair has this Certified Python Online Course with over 20 hours of material and 5 exciting real-time
projects with complete code. This will ensure you learn everything and with a practical approach. Check it out
now and start learning Python.

This Certified Python Course by DataFlair is what you need. It has countless practicals and will give you a
hands-on experience. Remember that you’ll need to practice along with the course; it will make you better at
Python.

2. The Long Way – Online Tutorials


Now, below is the way to learn Python through online tutorials –

a. What to study?
As a fresher in Python, you should study the following things to build a strong foundation in Python.

 Python installation and syntax


 Basic concepts like statements, variables, and operators
 Differences between Python 2 and Python 3
 Data structures like lists, sets, and dictionaries
 Decision making and loops
 Virtual environments
 Functions and recursion
 OOP concepts- classes, methods, inheritance, overloading
 Modules and packages
 File handling
 Decorators, generators, iterators
 Random numbers and regular expressions
 Exception handling
 Networking, XML processing, multiprocessing
 GUI
 SciPy, NumPy, Pandas
 Debugging, unit testing, logging, serializing, accessing the database
 It is also beneficial to know more than two high-level languages.

DataFlair has published a Python Master Guide for you by which you can learn all these topics.

b. Ways to Learn Python


Whether you’re beginning from scratch or not, there are a few sources where you can choose to study Python
for free:

 Online Python Tutorials


 Good Books on Python
 Python Official Documentation

c. Python Projects
After you’ve finished learning about the constructs in Python, you should build some personal projects to solve
the problems you face. Or, if you feel confident, you can contribute to some open-source Python projects like
Django.

DataFlair has published a complete package of 5 Python Projects which you must try.

d. Do an Internship
If you’re in your last semester of college or are done with college, you can go for a data science internship. This
will show the employer that you’re serious about Python, and will also give you some experience and exposure
to the professional world.
e. Build a good resume
Build a strong resume if you want to be preferred over other candidates. List your skills and projects carefully
on it. Mention any hackathons you’ve participated in- they show your confidence in your skills. Read this
article by DataFlair to understand how to create a perfect resume for your next interview.

Check Python Job Profiles and Salary


What profiles to apply for? Or what profiles can you get as a Python techie? If you’re wondering this, we’ve
listed out 10 profiles you can end up with if you go for Python.

 Software engineer
 Front-end software/web developer
 Python developer/programmer
 DevOps engineer
 Product manager
 Data Analyst/Journalist
 Educator
 Financial Advisor
 Research Analyst
 Data Scientist

In your first job with Python, you will take home around 3-5 lakhs per year. With experience, that can go up to
7-8 lakhs quick.

Who said a fresher can’t get a job in Python? If you try hard enough, you can get a Python job at a company
you want. Also note that some knowledge about data science, machine learning, big data, and artificial
intelligence can be beneficial for an interview and for a job itself. For the knowledge of all these technologies,
you can check DataFlair.

What you have selected – The fast way or the long way? Tell us through comments.

Python Tools – 4 Major Utilities of Python


by DataFlair Team · September 27, 2018

1. Python Tools

After reading Python Flask we move towards Python Tools. Python provides us with many utilities that we can
also use as command-line utilities. In this Python Tools tutorial, we will focus on – Python Dis modules, Python
PDB module, Python Profile module, and Python Tabnanny module with an example.

So, let’s start with Python Tools.


Python Tools – 4 Major Utilities of Python

2. Which Python Tools are Commonly Used?

Here, we will discuss 4 types of Python Utilities.

a. Python Dis Module


To convert bytecode into a more human-readable format, Python has the ‘dis’ module. You can say that it
compiles a script, disassembles the bytecode, and prints the output to the STDOUT. We can use it as a module
or through the command line. As an argument, we can pass a function, a method, a class, or a code object to
the dis() function.

Let’s take an example.

1. >>> def add():


2. a=3
3. b=4
4. c=a+b
5. print(f"{a}+{b}={c}")
6. >>> add()
7. 3+4=7

Now, we import the Python dis module and call the dis() function on it.

1. >>> dis.dis(add)

2 0 LOAD_CONST 1 (3)

2 STORE_FAST 0 (a)
3 4 LOAD_CONST 2 (4)

6 STORE_FAST 1 (b)

4 8 LOAD_FAST 0 (a)

10 LOAD_FAST 1 (b)

12 BINARY_ADD

14 STORE_FAST 2 (c)

5 16 LOAD_GLOBAL 0 (print)

18 LOAD_FAST 0 (a)

20 FORMAT_VALUE 0

22 LOAD_CONST 3 (‘+’)

24 LOAD_FAST 1 (b)

26 FORMAT_VALUE 0

28 LOAD_CONST 4 (‘=’)

30 LOAD_FAST 2 (c)

32 FORMAT_VALUE 0

34 BUILD_STRING 5

36 CALL_FUNCTION 1

38 POP_TOP

40 LOAD_CONST 0 (None)

42 RETURN_VALUE

Do you know the difference between Python Modules vs Packages

b. Python Tabnanny Module


Python tabnanny checks code for ambiguous indentation. It tells us about any weird combinations of tabs and
spaces in the code. In Python, whitespace shouldn’t be ambiguous.

Like dis, we can run tabnanny from the command line or using the function check().

1. >>> import os
2. >>> import tabnanny
3. >>> os.chdir('C:\\Users\\lifei\\Desktop')
4. >>> tabnanny.check('nannydemo.py')
5. >>>

Python Tabnanny module has the following methods:

Python Tools Tutorial – Python Tabnanny Module

i. tabnanny.check(file_or_dir)
This checks the file or directory we pass to it for examining whitespace-related issues. Then, it prints the
diagnostic messages to the standard output.
Let’s define Python Datetime Module with Quick Examples

ii.tabnanny.verbose
This is a flag that depicts whether Python will print verbose messages.

iii. tabnanny.filename_only
This is a flag that depicts whether Python should print only the filenames for those files that contain issues
involving whitespace.
It also has the following function:

I. tabnanny.process_tokens(tokens)
The tokenize module generates some tokens. check() uses process_tokens() to process these tokens.
Finally, this module may raise the following exception:

II. exception tabnanny.NannyNag


When process_tokens() detects an ambiguous indent, it raises this exception. check() captures and handles this.

c. Python Profile Module


We may sometimes like to know which parts of our code take the longest. So in a way, through profiling, we try
to find bottlenecks in our code. Other profilers in Python are cProfile and hotshot.

Let’s explore List of 63 Python os Modules in 2018

Let’s try this module with the cProfile module.

1. >>> import hashlib


2. >>> import cProfile
3. >>> cProfile.run("hashlib.md5('abcdefghijkl').digest()")

4 function calls in 0.000 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.000 0.000 <string>:1(<module>)

1 0.000 0.000 0.000 0.000 {built-in method _hashlib.openssl_md5}

1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}

1 0.000 0.000 0.000 0.000 {method ‘disable’ of ‘_lsprof.Profiler’ objects}

We profile the creation of an MD5 hash here. The first output we get tells us that four function calls were made.
These are ordered by standard name. Here’s what all the other columns mean:

 ncalls- The number of calls made.


 tottime- Total time spent in a function.
 percall- The quotient of tottime divided by ncalls.
 cumtime- Cumulative time spent in this function and all subfunctions.
 percall- The quotient of cumtime divided by primitive calls.
 filename: lineno(function)- Data of each function.

Read about Python Collections Module in detail

d. Python PDB Module


Pdb is the standard Python debugger. It helps us debug our code line by line and we have talked about this at
length in our article on Python Debugger.

1. >>> import pdb


2. >>> n=4
3. >>> def raised(a):
4. return a**a
5. pdb.set_trace()
6. eight=raised(8)
7. >>> print(eight)
8. >>> two=raised(2)
9. >>> print(two)

In the command prompt:

C:\Users\lifei\Desktop>python nannydemo.py

> c:\users\lifei\desktop\pdbdemo.py(6)<module>()

-> seven=raised(7)

(Pdb)

Now, we can use the commands of the pdb module to debug our code.

For a detailed demonstration, please refer to Debugging in Python.

So, this was all about Python Tools Tutorial. Hope you like our explanation.

3. Conclusion

Hence, these are few basic tools and utilities of Python: Dis modules, PDB module, the Profile module, and
Tabnanny module with an example. Furthermore, if have a doubt regarding Python Tools, feel free to ask in the
comment box.
See Also – Recursion in Python with Examples
For reference – Python Dis Module

Python Projects with Source Code – Practice Top Projects in Python


by DataFlair Team · October 1, 2019

Python Projects with Source Code

Looking to build a career in Python? Want to improve your resume with multiple personal projects on it? Then
this blog of Python projects with source code is for you. You earlier read about the top 5 data science projects;
now, we bring you 5 projects implementing data science with Python. In this blog, you’ll find the entire code to
all the projects. Read on to give your data science/ Python career a head-start.

List of amazing Python Projects with source code –

 Detecting Fake News Python Project with source code


 Speech Emotion Recognition Python Project with source code
 Detecting Parkinson’s Disease Python Project with source code
 Age and Gender Detection Python Project with source code
 Breast Cancer Classification Python Project with source code

Why do Projects in Python?


In an interview, a resume with projects shows interest and sincerity. Spending time on personal projects
ultimately proves helpful for your career. In this blog of python projects, we try our best to include different
data science and machine learning libraries of Python to give you a better experience.

Top Python Projects with Source Code

Let’s start discussing python projects with source code –

1. Detecting Fake News with Python

Fake news can be dangerous. This is a type of yellow journalism and spreads fake information as ‘news’ using
social media and other online media. This is a common way to achieve a certain political agenda. Fake news
may contain false and/or exaggerated claims. Social media algorithms often viralize these and create a filter
bubble. In this, we will train on a news.csv dataset of shape 7796×4. We’ll mainly use two things- a
TfidfVectorizer and a PassiveAggressiveClassifier. A TfidfVectorizer turns a collection of raw documents into
a matrix of TF-IDF features. And a PassiveAggressiveClassifier is an online learning algorithm that stays
passive for a correct classification and becomes aggressive when there’s a miscalculation.

Please refer – Detecting Fake News with Python for the complete implementation of the python project
with source code.

2. Speech Emotion Recognition with librosa


Speech Emotion Recognition (SER) is an attractive application of data science today as we constantly attempt
to give the consumer a better experience. This includes recognizing human emotion and affective states from
speech. Since voice often exposes underlying emotions with tone and pitch, it can be used to understand the
users’ needs and use it to improve the service. We will use the RAVDESS dataset and the libraries librosa,
soundfile, and sklearn to build a model using an MLPClassifier. In most projects, we use Jupyter Lab to run our
code.

Ready to build your own model? Speech Emotion Recognition Python Project with Source Code

3. Detecting Parkinson’s Disease with XGBoost


Parkinson’s disease is a progressive disorder of the central nervous system that affects over 1 million people in
India every year. It affects movement and can be a cause of tremors and stiffness. This is a neurodegenerative
disorder with 5 stages to it, and affects dopamine-producing neurons in the brain. In this python project, we will
use the UCI ML Parkinsons dataset and use XGBClassifier from xgboost to build a model that can accurately
detect the presence of Parkinson’s disease in a person. We will also use the libraries scikit-learn, numpy, and
pandas.

Check the complete python project with source code – Detecting Parkinson’s Disease with Python

4. Gender and Age Detection with OpenCV

Computer Vision is a field of study enabling computers to see and recognize digital images and videos- this is
something only humans (and animals) are generally capable of. This involves processes like object recognition,
video tracking, motion estimation, and image restoration. It is exciting to be able to predict a person’s gender
and age from just a photograph. CNNs (Convolutional Neural Networks) are often the choice when we work
with images. In this project, we’ll use OpenCV (Open Source Computer Vision) and implement deep learning,
using trained models on the Adience dataset.

This is just a brief, explore detailed Gender and Age Detection Python Project with source code

5. Breast Cancer Classification with Keras


IDC (Invasive Ductal Carcinoma) is the most common form of breast cancer, forming about 80% of all breast
cancer diagnoses. This is a cancer that develops in milk ducts, and then invades the fibrous/fatty breast tissue
outside them. In this project, we use the IDC_regular dataset (with breast cancer histology images). Histology is
the study of the microscopic structure of tissues. With 2,77,524 patches of size 50×50 from 162 whole mount
slide images scanned at 40x, we’ll learn to build a classifier to train on 80% of the dataset. We’ll use 10% of it
for validation. We’ll be using Keras to define a CNN (CancerNet).

Excited? Check the entire python project of breast cancer classification with source code

Summary

We have learned to build 5 exciting python projects with source code. Try them at your own end and pay
attention to every step as you do it. Which one was your favorite? Tell us in the comment.

Data Science Tutorial – Introduction to Data Science for Python


by DataFlair Team · May 27, 2019

1. Data Science Tutorial – Objective

This Data Science tutorial aims to guide you to the world of data science and get you started with the basics like
what is Data Science, History of Data Science, and Data Science Methodologies. Here, we will cover the Data
Science Applications, a difference between Business Intelligence and Data Science. Along with this, we will
discuss Life-Cycle of Data Science and Python Libraries.

So, let’s begin Data Science Tutorial.


Data Science Tutorial – Introduction to Data Science with Python

2. What is Data Science?

Before we start the Data Science Tutorial, we should find out what data science really is.

Data science is a way to try and discover hidden patterns in raw data. To achieve this goal, it makes use of
several algorithms, machine learning(ML) principles, and scientific methods. The insights it retrieves from
data lie in forms structured and unstructured. So in a way, this is like data mining. Data science encompasses
all- data analysis, statistics, and machine learning. With more practices being labelled into data science, the term
itself becomes diluted beyond usefulness. This leads to variation in curricula for introductory data science
courses worldwide.

Do you know the Best Data Scientist Certifications to Choose from

3. Data Science Tutorial – History

Through the recent hype that data science has picked up, we observe that it has been around for over thirty
years. What one we could use as a synonym for practices like business analytics, business intelligence, or
predictive modeling, now refers to a broad sense of dealing with data to find a relationship within it. To quote a
timeline, it would go something like this:

a. In 90’s
 1960- Peter Naur uses the term as a substitute for computer science.
 1974- Peter Naur publishes Concise Survey of Computer Methods, uses a term in a survey of
contemporary data processing methods.
 1996- Biennial conference in Kobe; members of the IFCS (International Federation of Classification
Societies include the term in the conference title.
 1997- November- Professor C.F. Jeff Wu delivers inaugural lecture on the topic “Statistics=Data
Science?”.

b. In 2000’s
 2001- William S. Cleveland introduces data science as an independent discipline in article Data Science:
An Action Plan for Expanding the Technical Areas of the Field of Statistics.
 2002- April- The ICSU (International Council for Science): Committee on Data for Science and
Technology (CODATA) starts Data Science Journal- this publication is to focus on issues pertaining to
data systems- description, publication, application, and also legal issues.
 2003- January- Columbia University publishes journal The Journal of Data Science- a platform that
allows data workers to exchange ideas.
 2005- National Science Board publishes Long-lived Digital Data Collections: Enabling Research and
Education in the 21st Century- this provides a new definition to the term “data scientists”.
 2007- Jim Gray, Turing awardee, envisions data-driven science as the fourth paradigm of science.
 2012- Harvard Business Review article attributes coinage of the term to DJ Patil and Jeff
Hammerbacher in 2008.
 2013- IEEE launches a task force on Data Science and Advanced Analytics; first European Conference
on Data Analysis (ECDA)organized in Luxembourg, European Association for Data Science (EuADS)
comes into existence.
 2014- IEEE launches first international conference International Conference on Data Science and
Advanced Analytics; General Assembly launches student-paid Bootcamp, The Data Incubator launches
data science fellowship for free.
 2015- Springer launches International Journal on Data Science and Analytics.

4. Data Science Tutorial – Methodologies


In this Data Science Tutorial, we will cover the following Methodologies in data Science:

Data Science Tutorial – Methodologies of Data Science

a. Machine Learning for Pattern Discovery


With this, clustering comes into play. This is an algorithm to use to discover patterns; an unsupervised model.
When you don’t have parameters on which to make predictions, clustering will let you find hidden patterns
within a dataset.

One such use-case is to use clustering in a telephone company to determine tower locations for optimum signal
strength.

b. Machine Learning for Making Predictions


When we have the data we need to train our machine, we can use supervised learning to deal with transactional
data. Making use of machine learning algorithms, we can build a model and determine what trends the future
will observe.

c. Predictive Causal Analytics


Causal analytics lets us make predictions based on a cause. This will tell us how probable an event is to hold
occurrence in future. One use-case will be to perform such analytics on payment histories of customers in a
bank. This tells us how likely customers are to reimburse loans.

d. Prescriptive Analytics
Predictive analysis will prescribe your actions and the outcomes associated with those. This intelligence lets it
take decisions and modify those using dynamic parameters. For a use-case, let us suggest the self-driving car by
Google. With the algorithms in place, it can decide when to speed up or slow down, when to turn, and which
road to take.

5. Data Science Applications

Let’s see some applications in this Data Science Tutorial:

Data Science Tutorial – Data Science Applications


a. Image Recognition
Using the face recognition algorithm of data science, we can get a lot done. Did Facebook ever suggest people
tag in your pictures? Have you tried the search-by-image feature from Google? Do you remember scanning a
barcode to log in to WhatsApp Web using your smartphone?

b. Speech Recognition
Siri, Alexa, Cortana, Google Voice all make use of speech recognition to understand your commands.
Attributing to issues like different accents and ambient noise, this isn’t always completely accurate, though
intelligible most of the time. This facilitates luxury like speaking the content of a text to send, using your virtual
assistant to set an alarm, or even use it to play music, inquire about the weather, or make a call.

c. Internet Search
Search engines like Google, Duckduckgo, Yahoo, and Bing make good use of data science to make fast, real-
time searching possible.

d. Digital Advertisements
Data science algorithms let us understand customer behaviour. Using this information, we can put up relevant
advertisements curated for each user. This also applies to advertisements as banners on websites and digital
billboards at airports.

e. Recommender Systems
Names like Amazon and Youtube will throw in suggestions about similar products aside or below as you
browse through a product or a video. This enriches the UX(user experience) and helps retain customers and
users. This will also take into account the user’s search history and wishlist.

f. Price Comparison Websites


Websites like Junglee and PriceDekho let us compare prices for the same products across different platforms.
This facility lets you make sure you grab the best deal. These websites work in the domains of technology,
apparel, and policy among many others, and use APIs and RSS feeds to fetch data.

g. Gaming
As a player levels up, a machine learning algorithm can improve or upgrade itself. It is also possible for the
opponent to analyze the player’s moves and add an element of difficulty to the game. Companies like Sony and
Nintendo make use of this.

h. Delivery Logistics
Freight giants like UPS, FedEx, and DHL use practices of data science to discover optimal routes, delivery
times, and transport modes among many others. A plus with logistics is the data obtained from the GPS devices
installed.
i. Fraud and Risk Detection
Practices like customer profiling and past expenditures let us analyze whether there will be a failure. This lets
banks avoid debts and losses.

6. Business Intelligence vs Data Science

Here, in this part of Data Science Tutorial, we discuss Data Science Vs BI. Business intelligence and data
science aren’t exactly the same thing.

 BI works on structured data; data science works on both- structured and unstructured data.
 Where BI focuses on the past and the present, data science considers the present and the future.
 The approach to BI is statistics and visualization; that to data science is statistics, machine learning,
graph analysis, and NLP.
 Some tools for BI are Pentaho, Microsoft BI, and R; those for data science are RapidMiner, BigML,
and R.

7. Data Science Tutorial – Life-Cycle

The journey with data science goes through six phases-

Data Science Tutorial – Introduction to Data Science with Python

a. Discovery
Before anything else, you should understand what the project requires. Also consider the specifications, the
budget needed, and priorities. This is the phase where you frame the business problem and form initial
hypotheses.
b. Data Preparation
In the preparation phase, you will need to perform analytics in an analytical sandbox. This is for an entire
project. You will also extract, transform, load, and transform data into the sandbox.

c. Model Planning
In the third phase, you choose the methods you want to work with to find out how the variables relate to each
other. This includes carrying out Exploratory Data Analytics (EDA) making use of statistical formulae and
visualization tools.

d. Model Building
This phase includes developing datasets for training and testing. It also means you will have to analyze
techniques like classification and clustering and determine whether the current infrastructure will do.

e. Communicate results
This is the second last phase in the cycle. You must determine whether your goals have been met. Document
your findings, communicate to stakeholders, label the project a success or failure.

f. Operationalize
In the last phase, you must craft final reports, technical documents, and briefings

This Data Science Tutorial is dedicated to Python. So, let’s start Data Science for Python.

8. Data Science Tutorial – Why Python?

So, now you know what data science is all about. But why is Python the best choice for it? Here are a few
reasons-

 Open-source and free.


 Easy to learn; intuitive.
 Fewer lines of code.
 Portability.
 Better productivity.
 Demand and popularity.
 Excellent online presence/ community.
 Support for many packages usable with analytics projects; can also use packages that can use code from
other languages.
 It is faster than similar tools like R and MATLAB.
 Amazing memory management abilities.

9. Python 2.x or 3.x- Which should you go for?


Among a lot of other factors, the support for Python 2 ends officially on January 1st, 2020, so the future belongs
to Python 3. Also, 95% of the libraries for data science are done being migrated from Python 2 to Python 3.
Apart from that, Python 3 is cleaner and faster.

Well, then what about Python 2? It has its own perks- it is rich with a large online community and plenty of
third-party libraries, and some features are backwards-compatible and work with both versions.

With the perks of each version listed, make your choices.

10. Data Science Tutorial – Python Libraries

For carrying out data analysis and other scientific computation, you will need any of the following libraries:

Data Science Tutorial – Data Science Libraries

a. Pandas

Pandas help us with munging and preparing data; it is great for operating on and maintaining structured data.

b. SciPy

SciPy (Scientific Python) stands on top of NumPy. With this library, we can carry out functionality like Linear
Algebra, Fourier Transform, Optimization, and many others.

c. NumPy

NumPy (Numerical Python) is another library that lets us deal with features like linear algebra, Fourier
transforms and advanced random number capabilities. One very import feature of NumPy is the n-dimensional
array.
d. Matplotlib

Matplotlib will let you plot different kinds of graphs. These include pie charts, bar graphs, histograms, and
even heat plots.

e. Scikit-learn

Scikit-learn is great for machine learning. It will let you statistically model and implement machine learning.
The tools for these include clustering, regression, classification, and dimensionality reduction.

f. Seaborn

Seaborn is good with statistical data visualization. Making use of it, we can create useful and attractive
graphics.

g. Scrapy

Scrapy will let you crawl the web. It begins on a home page and gets deeper within a website for information.

Follow this link to know more about Python Libraries in detail

11. Learning in Data Science Tutorial

Before you begin with data science Tutorials, we suggest you should brush up on the following:

 Variables in Python
 Operators in Python
 Dictionaries in Python
 Strings in Python
 Python Lists
 Python Tuples

So, this was all about Data Science Tutorial. Hope you like our explanation.

12. Conclusion
Hence, we complete this Data Science Tutorial, in which we learned: what is Data Science, History of Data
Science, and Data Science Methodologies. In addition, we covered the Data Science Applications, BI Vs Data
Science. At last, we discussed Life-Cycle of Data Science and Python Libraries. This will get you started with
Python.

Got something else to add in this Data Science Tutorial? Drop it in the comments below.

Python for Data Science – Data speaks, Python listens!


by DataFlair Team · July 13, 2019

As you must know by now, it is a great choice to do data analysis using Python. This is why data scientists
prefer Python.
Out there, there’s a battle taking place in minds of future Data scientists for choosing the best tools. Though
there are quite a number of tools with many options, the close combat narrows down between two popular
languages – Python and R.

Why Python over R for Data Science?

One of the push of choosing Python over R is from the variety of data science/data analytics libraries made
available. Some of the libraries well known in the data science community – Pandas, StatsModels, NumPy,
SciPy, and Scikit-Learn. It doesn’t stop there, some 72,000 of them in the Python Package Index (PyPI) and still
growing constantly. After all these, I recommend you to check the difference between Python and R for better
understanding

So, before we start our topic I recommend you to take a brief of what data science is?

What is Data Science?

Data science, aka data-driven science, is an interdisciplinary field of scientific methods, processes, and systems.
It is used to extract knowledge or insights from data in various forms, either structured or unstructured. In this
way, it is similar to data mining. With data at its heart, it employs a wide range of techniques on the data to
extract essential insights from it.

Data science attracting beginners towards Python. How?

For this reason and others, Python is the most demanding for programmers . Data scientists coming from
engineering or scientific backgrounds might feel a bit out of place the first time they try to use it for data
analysis but when they do use it, they make most out of it. Python didn’t make its inroads to data science
initially when it was conceived in the late 1980s. Tools for covering every aspect of scientific computing are
now readily available in Python.

Python’s readability and simplicity make it comparatively easy to pick up. The number of dedicated and
analytical libraries freely available for download today mean that data scientists present in every sector will find
packages already tailored to their needs. As a jack of all trades, Python is not specialized to do statistical
analysis, but in many cases, organizations already have heavily invested in extending it to that purpose as they
saw advantages of standardizing on it. In short, we can say, Python has become the go-to language for data
scientists. And you
must start learning Python now! Our DataFlair team has designed a perfect self-paced course of Python for
Data Science and the passionate learners like you. Get the course now and move a step ahead in the data science
field.

Essential Python Libraries for Data Scientists

Data science has the early benefits of these extensions and libraries!

1. Python Pandas

Now the big daddy to all of them is Python Pandas. From importing data from spreadsheets to processing sets
for time-series analysis, Pandas is used for everything. Pandas pretty much convert one data form to another on
your fingertips. Hence, Pandas powerful data frames can perform both, basic cleanup and advance data
manipulations.

“One of the reasons we like to use Pandas is because we like to stay in the Python ecosystem,” Burc Arpat,
a quantitative engineering manager at Facebook.

Behind Python’s data science success story, one of the earliest libraries is Numpy (Numerical Python), on
which Pandas is built. NumPy’s functions exposure is used in Pandas for advanced analysis. For more
specialization, one can use Scipy which is scientifically equivalent to Numpy, offering tools and techniques for
scientific data analysis.

a. NumPy
NumPy facilitates easy and efficient numeric computation. It has many other libraries built on top of it. Make
sure to learn NumPy arrays.

b. SciPy
SciPy will give you all the tools you need for scientific and technical computing. It has modules for
optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing,
ODE solvers, and other tasks.

2. Matplotlib
Python also provides powerful visualization libraries – Matplotlib. It can be used in all kinds of GUI toolkits
such as python scripts, web applications as well as shell, etc. With this, you have the opportunity to use
different types of plots and work with multiple plots.

3. Scikit – Learn & Pybrain

Scikit – Learn & Pybrain, one of the attractions of python where you implement machine learning. With the
support of simple and efficient tools in this library which can be used for data analysis and data mining. Various
algorithms have their back, like — logistic regression, time series, etc.

4. TensorFlow

TensorFlow is the most popular tool for Machine Learning in Python. It was developed specifically for carrying
out deep learning operations. The basic data structure of TensorFlow ecosystem are the tensors. As a matter of
fact, the name of TensorFlow is derived from these tensors. TensorFlow is continuously evolving owing to an
open-source community who have made it a pioneering toolkit for machine learning operations. It provides
support for CPUs, GPUs as well as TPUs. Due to this, it provides lightning speed execution speed for various
machine learning algorithms.

TensorFlow has numerous applications. This is mainly because of its high processing capability. It is used for
the development of speech recognition product, recommendation systems, Generative Adversarial Networks,
etc. TensorFlow is basically the standardized tool for performing Deep Learning operations.

5. Seaborn
Welcoming the next important library of Python for Data Science – Seaborn! So whatever and whenever you
will be using Python for data science, the first thing that will click to and should click will be using matplotlib
(for 2D visualization) & seaborn. They have many high-level interfaces and styles in default for drawing
statistical graphics.

Python is an obvious language choice for Data Science. These above-stated libraries and other specialized one’s
aid everything in python, from machine learning to neural networks to data processing. Hence this flexibility
has become the main benefit of choosing python at every step of the way towards data science.

Python’s large community is taking Data Science to the top!

Another plus point to above all the extensions/ libraries and properties of python that contribute to “python
being the choice” is the large community of data scientist, machine learning experts, and programmers who are
not only working their heart out to make it easy to learn python but also provide datasets to test one’s mastery
and skillset in python. So, whether you are a social scientist needing python for advanced data analysis or a
growing developer who needs inspiration, one of the parts of this python community will be ready to help you!

Summary
Along with Data Science and analytics, Python has also built a major force to conquer artificial intelligence and
machine learning. So if you learn python, a lot of career opportunities have open doors for you. Even if you
don’t work on AI, ML or data analysis, Python itself is capable to set to up! Being one of the contributors to the
web development world and graphic user interfaces. When you have Data Science hand in hand with Python, an
average salary of $92,000 to $132,000 a year (according to Glassdoor analysis) is not away!

Python Data Science Environment Setup


by DataFlair Team · August 7, 2019

1. Data Science Environment Setup With Python


Today, in this Python Data Science tutorial, we will see Data Science Environment Setup for Python.
Moreover, we will tell you about all that you need to install for Data Science Environment Setup, such as
Python, Anaconda, Miniconda. Along with this, we will see how to set a virtual environment for Data Science
Environment Setup and also importing Data Science Packages. Today, we will guide you to set up your
machine so you can begin your journey with data science. Before you begin, we suggest you read up on Python
Data Science Introduction to make things flow easier when you
come back. So, let’s start the Python data Science Environment Setup.
Python Data Science Environment Setup

2. Install Python
Before anything else, you should get Python on your machine. You can refer to the Step-by-Step Guide to
Install Python on Windows for this.
While 2.7 is widely adopted, 3.x will take over the future and has already started to leave its mark. Apart from
that, some software and features aren’t backward-compatible. So take your pick.

3. Getting Anaconda for Data Science Environment Setup

Data Science Environment Setup – Install Anaconda

Anaconda is a Python distribution for data science and machine learning. It is free and open-source and makes
managing and deploying packages simple.
It has more than 1000 data science packages and the Conda package. Other tools it comes with are core Python,
IPython, among others.

a. Anaconda Navigator
Anaconda ships with a virtual environment manager- the Anaconda Navigator. This is a desktop GUI that lets
you launch applications and manage packages, environments, and channels for conda. This lets you bypass the
command-line commands. The Navigator searches for a package on the Anaconda Cloud, or in a local
repository for Anaconda, and installs, runs, and updates them. It has the following applications-

 Glueviz
 Jupyter Notebook
 JupyterLab
 Orange 3 App
 VSCode
 RStudio
 Rodeo
 Spyder
 QTConsole

Anaconda will give you two package managers- pip and conda. When some packages aren’t available with
conda, you can use pip to install them. Note that using pip to install packages also available to conda may cause
an installation error.

b. Installing Anaconda
To download an Anaconda distribution, you can use the official download page:
https://www.anaconda.com/download/
Here, you can select your platform and then choose the installer. For this, you can choose which version you
want and whether 32-bit or 64-bit.
To install a package with conda, you can use the following command–

1. conda install scipy

4. Install Miniconda
Miniconda is a minimal installer for conda; a small, bootstrap version of Anaconda. It is free and ships with
conda, Python, and packages like pip and zlib. This lets you install more than 720 packages from conda. Since
Miniconda is a lighter version of Anaconda, it lets you download faster.
To install Miniconda, you can get to the following page-
https://conda.io/miniconda.html
Here, choose your platform and then pick a 32-bit or a 64-bit installer according to the needs of your machine.

5. Setting up a Virtual Environment


Since, here, we talk about setting up a data science environment with Python, let’s find out what a virtual
environment is. Well, a virtual environment lets us create different Python versions with the packages we want,
or as the project needs. Such an environment helps us ensure that there are no clashes between the versions of
packages and that of Python and its package managers. You should check out this blog on How to Create a
Python Virtual Environment and Install Packages.
For now, let’s see how we can create one with Anaconda. Use the following command in your Anaconda
prompt-
Data Science Environment Setup – setting up Virtual Environment

This should give you an idea of what the Anaconda prompt looks like. Now, to activate this environment, you
can type-

1. conda activate demo

This lets you start using it. Now to deactivate it, try-

1. conda deactivate

The following command tells you all the environments that exist; the asterisk (*) marks the current-

1. conda info -e

6. Important Python Data Science Packages


Important Python Data Science Packages

Working with data science, out of more than 1000 packages available, you will need a few that will let you
implement the basic functionalities. Let’s take a quick look at some of those packages.

a. NumPy

Python data Science Packages – NumPy

As discussed ample times earlier, NumPy lets you deal with large, multi-dimensional arrays and matrices. To
act on these, it also gives us various high-level mathematical functions.

b. SciPy

Python data Science Packages – SciPy


Scipy is a Python library for scientific and technical computin, and is free and open-source. Modules from
SciPy include those for-

 Optimization
 Linear algebra
 Integration
 Interpolation
 Special functions
 FFT
 Signal and Image processing
 ODE solvers

c. Matplotlib

Python Data Science packages – Matplotlib

We’ve used Matplotlib so far to plot many of the figures we needed to get started with visualization. Some of
these were bubble charts and scatter plots. This is a plotting library with Python and extends NumPy. With an
object-oriented API, it lets you embed plots into applications. For this, it uses GUI toolkits like Tkinter, Qt,
GTK+, and wxPython.

d. Pandas

Python Data Science Packages – Pandas

We have taken an extensive Pandas Tutorial. Now, it’s time for a quick recap. pandas is a software library for
Python that is supposed to serve for data manipulation and analysis. It is free and lets you manipulate numerical
tables and time series using data structures and operations.

e. scikit-learn

Python Data Science Packages – Scikit-learn


scikit-learn is a software machine learning library for Python. It is free and offers different algorithms for
classification, regression, and clustering-

 Support Vector Machines


 Random forests
 Gradient boosting
 K-means
 DBSCAN

We usually use it alongside NumPy and SciPy.

f. seaborn
Finally, seaborn is a visualization library for Python and is based on matplotlib. It lets us perform data
visualization in a statistical manner with a high-level interface that results in attractive graphics.
Let’s revise Python regular expression

7. How to Get Jupyter Notebook?

Data Science Environment Setup – getting Jupyter Notebook

As we saw earlier, the Jupyter Notebook ships with Anaconda. To run it, you can get in your virtual
environment and type the following-

1. jupyter notebook

You can also install it with pip-

1. python3 -m pip install --upgrade pip


2. python3 -m pip install jupyter

The notebook looks something like this-


Data Science Environment Setup – Jupyter Notebook

You can find this at http://localhost:8888/


Now to run Python here, you can create a new file. It looks like this-

Data Science Environment Setup – Jupyter Notebook

You can quit using the logout button at the top-right corner.
Let’s revise the Python Array Module
So, this was all in the Data Science Environment Setup with Python. Hope you like our explanation.

8. Conclusion: Data Science Environment Setup


Hence, in this Python Data Science Environment Setup tutorial, we discussed all that to need to install for data
Science Environment Setup. Moreover, we look at Python packages as Numpy, Scipy, matplotlib. With this, we
conclude our data Science environment setup tutorial, on how to set your machine up for data science. Still, if
any query regarding Python Data Science Environment setup, feel free to drop your questions in comments
below.

What’s New in Python 3.6 ? | New Features in Python 3.6


by DataFlair Team · September 27, 2018

1. Python 3.6 Tutorial

In this article on Python 3.6, we will discuss the following new features in Python 3.6. Moreover, we will
discuss

 New Syntax Features of Python 3.6


 New Modules in Python 3.6
 Improved Python 3.6 Modules

So, let’s start the Python 3.6 Tutorial.

What’s New in Python 3.6? | New Features in Python 3.6

2. What’s new in Python 3.6?

We know that the future belongs to Python 3.x. But we have a number of versions in Python 3000. For example,
Python 3.3.3 and Python 3.4.3. The latest version of Python is 3.6. So, let’s see what’s new in Python 3.6 and
how it is different from older versions, Python 3.6 performance and features.

3. New Syntax Features of Python 3.6


With Python 3.6, we see some new syntax features. Let’s take a look.

a. PEP 498 (Formatted String Literals)

You guessed it right. PEP 498 introduces f-strings.

An f-string is a string that you can format to put values inside it. For this, we precede the string with an ‘f’ or an
‘F’. We mention the variables inside curly braces.

1. >>> hometown,city='Ahmedabad','Indore'
2. >>> print(f"I love {hometown}, but I live in {city}")

I love Ahmedabad, but I live in Indore

For more on f-strings, read Python Strings.

b. PEP 526 (Syntax for Variable Annotations)

PEP 484 standardizes type annotations for function parameters. We also call these type hints.

1. >>> class A:
2. name:str
3. >>> A. annotations
4. {'name': <class 'str'>}

Annotations do not pose meaning to the interpreter, and are of use to third-party tools and libraries.

c. PEP 515 (Underscores in Numeric Literals)

With PEP 515, we can use underscores in numeric literals- between digits and after base specifiers.

1. >>>

0x_FF_FE_FF_FE

4294901758

d. PEP 525 (Asynchronous Generators)

PEP 492 introduced native coroutines and async/await syntax. Unlike Python 3.5, Python 3.6 can have await
and yield in the same function body. So, we can define asynchronous generators:

1. >>> async def ticker(delay, to):


2. """Yield numbers from 0 to *to* every *delay* seconds."""
3. for i in range(to):
4. yield i
5. await asyncio.sleep(delay)

This makes code faster and more concise.

e. PEP 530 (Asynchronous Comprehensions)

With PEP 530, you can use async for in lists, sets, dict comprehensions, and generator expressions.
result = [i async for i in aiter() if i % 2]

f. PEP 487 (Simpler Customization of Class Creation)

Now, we don’t need a metaclass to customize subclass creation. Whenever we create a new subclass, it calls the
init_subclass classmethod.

1. class PluginBase:
2. subclasses = []
3. def init_subclass (cls, **kwargs):
4. super(). init_subclass__(**kwargs)
5. cls.subclasses.append(cls)
6. class Plugin1(PluginBase): pass
7. class Plugin2(PluginBase): pass

g. PEP 495 (Local Time Disambiguation)

PEP 495 introduces the ‘fold’ attribute to instances of the datetime.datetime and datetime.time classes. This
way, it can differentiate between two moments in time with the same local times.

h. PEP 529 (Change Windows filesystem encoding to UTF-8)

With Python 3.6, no data loss occurs when we use bytes paths on Windows.

i. PEP 528 (Change Windows console encoding to UTF-8)

Now, the default console on Windows accepts all Unicode characters. It also provides correctly-read str objects
to Python code. Now, sys.stdin, sys.stdout, and sys.stderr default to utf-8 encoding.

j. PEP 520 (Preserving Class Attribute Definition Order)

With PEP 520, the natural ordering of attributes in a class is preserved in the class’ dict attribute. Now, the
default effective class ‘execution’ namespace is an insertion-order-preserving mapping.

k. PEP 468 (Preserving Keyword Argument Order)

Python now guarantees that **kwargs in a function signature is an insertion-order-preserving mapping.

l. PEP 523 (Adding a frame evaluation API to CPython)

PEP 523 introduces an API to make frame evaluation pluggable at the C level. This way, tools like debuggers
and JITs can intercept frame evaluation before the Python code even begins to execute.

These are the Python 3.6 new feature syntax.

Read:13 Unique Features of Python Programming Language

4. Other Additions in Python 3.6

 The PYTHONMALLOC environment variable allows us to set the Python memory allocators and install
debug hooks.
 New dict implementation- Now, dict() uses between 20% and 25% less memory compared to
Python 3.5.
 Earlier, it would give you a SyntaxWarning if you did not use a ‘global’ or ‘nonlocal’ statement before
the first use of the affected name in that scope.
 Now, Import raises the new exception ModuleNotFoundError, which is a subclass of ImportError. Code
that checks for ImportError still works.
 The interpreter now abbreviates long sequences of repeated traceback lines as
“[Previous line repeated {count} more times]”.
 Class methods that rely on zero-argument super() now work perfectly when we call them from metaclass
methods at class creation.
 Now, we can set a special method to None when we want to indicate that the operation is unavailable.
For instance, a class that sets iter () to None isn’t iterable.

The is some this what’s extremely new in Python 3.6.

5. New Modules in Python 3.6

a. secrets

Python 3.6 introduces a new module, ‘secrets’. This module lends us a way to reliably generate
cryptographically strong pseudo-random values. Using these, we can manage secrets like account
authentication, tokens, and so.

What’s new in python 3.6 – Secrets

Any doubt yet in What’s new in Python 3.6 tutorial because now there is a long list of Improved modules. Also
refer this article on Python Modules vs Packages.

6. Improved Python 3.6 Modules

Why stop at what we have, when we can tweak it into something even more awesome? Python 3.6 makes the
following improvements:
 array – Now, exhausted iterators of array.array stay exhausted even when the iterated array extends.
This is in consistence with other mutable sequences’ behavior.
 ast – Python adds the new ast.Constant AST node. External AST optimizers use them for constant
folding.
 asyncio – With Python 3.6, the asyncio module is no longer provisional; its API is now stable.
 binascii – Now, the function b2a_base64() accepts an optional newline keyword. This way, it can
control whether the newline character appends to the return value.
 cmath – Python 3.6 added a new constant cmath.tau(τ).

1. >>> from cmath import tau


2. >>> tau

6.283185307179586

 collections – Here’s all that is new in the ‘collections’ module:

Collection ABC- to represent sized iterable container classes.

Reversible ABC- to represent iterable classes. These also provide the method reversed ().

AsyncGenerator ABC- to represent asynchronous generators.

Other than these, the namedtuple() function will now accept an optional keyword argument ‘module’. Now, the
arguments ‘verbose’ and ‘rename’ for namedtuple() are keyword-only. Finally, we can now pickle recursive
collections.deque instances.

 concurrent.futures – Now, the class constructor ThreadPoolExecutor accepts an optional


thread_name_prefix argument. This lets us customize thread names for the thread created by the
pool.
 contextlib – The new contextlib.AbstractContextManager class provides an ABC for context managers.
This provides a sensible default implementation for enter ().
 datetime – Python 3.6 has a fold attribute for the datetime and time classes. This disambiguates local
time.

The function datetime.isoformat() takes an optional argument ‘timespec’.


The function datetime.combine() takes an optional argument tzinfo.

 decimal – The decimal module has a new method Decimal.as_integer_ratio(). It returns (n,d)- a pair of
integers representing a given Decimal instance as a fraction.

1. >>> Decimal('-3.14').as_integer_ratio()

(-157, 50)

 k. distutils – Python 3.6 removes the default_format attribute from distutils.command.sdist.sdist. Also,
now, the formats attribute defaults to [‘gztar’].
 l. encodings – On Windows, we now have the ‘oem’ encoding for ‘CPOEMCP’. We also have the ‘ansi’
alias for ‘mbcs’ encoding.
 m. enum – The enum module has two new enumeration base classes- Flag and IntFlags. These define
constants that we can combine using bitwise operators.

1. >>> from enum import Enum, auto


2. >>> class Color(Enum):
3. red=auto()
4. blue=auto()
5. green=auto()
6. >>> list(Color)
7. [<Color.red: 1>, <Color.blue: 2>, <Color.green: 3>]

 faulthandler – This module installs a handler for Windows exceptions.


 fileinput – With Python 3.6, hook_encoded() supports the ‘errors’ argument.
 http.client – Now, chunked encoding request bodies work with both HTTPConnection.request() and
endheaders().
 idlelib and IDLE – The idlelib package is refactored to give the IDLE a better look and
better performance. This also makes the code easier to understand, test, and improve.
 importlib – Now, importlib raises the exception ModuleNotFoundError when it is unable to find a
module. This is a subclass of ImportError.
 inspect – Now, the function inspect.signature() reports the implicit .0 parameters that the compiler
generates for comprehension and generator expression scopes.
 json – Now, json.load() and json.loads() support binary input.
 logging – To check if a log file must be reopened, we have WatchedFileHandler.reopenIfNeeded().

Read: Python Collections Module

 math – We have the new constant tau(τ) in both math and cmath modules.
 multiprocessing – We can now nest proxy objects returned by multiprocessing.Manager().
 x. os – Now, scandir() supports bytes paths on Windows. The method close() lets us explicitly close a
scandir() iterator.
 pathlib – Now, pathlib supports path-like objects.
 pdb – Python 3.6 adds a new optional readrc argument to the class constructor. This controls whether
.pdbrc files should be read. This is what’s new in Python 3.6, but this is not it.
 pickle – Pickle is the module that helps with serialization. We can now use pickle protocols older than
protocol version 4 to pickle objects needing new called with keyword arguments.
 pickletools – Now, pickletools.dis() outputs the implicit memo index for the MEMOIZE opcode.
 ac. pydoc – With Python 3.6, pydoc has learned to respect the MANPAGER environment variable.
 random – With the new random module, choices() returns a list of elements of a certain size. It picks
these elements from a given population of optional weights.
 re – The module re now has support for modifier spans in regular expressions. For instance,
‘(?i:p)ython’ will match ‘python’ and ‘Python’, but not ‘PYTHON’.
 readline – The function set_auto_history() can enable/disable automatic addition of input to the history
list.
 rlcompleter – We no longer have private and special attribute names unless prefixed with an
underscore. Sometimes, you can see a space or colon after some completed keywords.
 shlex – To control what characters must be treated as punctuation, shlex now has much improved shell
compatibility. This is through the punctuation_chars argument.
 site – We can now specify file paths on top of directories to add paths to sys.path in a .pth file.
 aj. sqlite3 – Now, sqlite3.Cursor.lastrowid supports the REPLACE statement.
 ak. socket – getsockopt() now supports constants SO_DOMAIN, SO_PROTOCOL, SO_PEERSEC,
and SO_PASSSEC.
setsockopt() now supports the setsockopt(level, optname, None, optlen: int) form.
 socketserver – The servers based on the socketserver module support the context manager protocol.
 am. ssl – Now, ssl supports OpenSSL 1.1.0. Also, SSLContext now has better default configuration for
options and ciphers.
 statistics – The statistics module has the new harmonic_mean() function.
 struct – Now, struct supports IEEE 754 half-precision floats. It does this via the ‘e’ format specifier.
 subprocess – With Python 3.6, if the child process is still running, the subprocess.Popen destructor
emits a ResourceWarning warning.
 sys – The function getfilesystemencodeerrors() returns the name of the error mode used to
convert between Unicode filenames and bytes filenames.
 telnetlib – Now, Telnet is a context manager.
 time – struct_time attributes tm_gmtoff and tm_zone now work on all platforms.
 timeit – When there is substantial (4x) variance between best and worst times, timeit warns.
 tkinter – New methods in the tkinter.Variable class include trace_add(), trace_remove() and
trace_info().
 traceback – Along with the interpreter’s built-in exception display, the traceback module abbreviate
long sequences of repeated lines in tracebacks. For instance:

1. >>> def f(): f()


2. ...
3. >>> f()

Traceback (most recent call last):

File “<stdin>”, line 1, in <module>

File “<stdin>”, line 1, in f

File “<stdin>”, line 1, in f

File “<stdin>”, line 1, in f

[Previous line repeated 995 more times]

RecursionError: maximum recursion depth exceeded

 aw. tracemalloc – tracemalloc now supports tracing memory allocations in multiple different address
spaces.
 typing – This module now has an improved support for generic type aliases. Also, new classes include
typing.ContextManager and typing.Collection.
 unicodedata – This module now uses data from Unicode 9.0.0.
 unittest.mock – New methods include Mock.assert_called() and Mock.assert_called_once().
 urllib.request – If an HTTP request has a file or iterable body, other than a bytes object, but no
Content-Length header, it does not throw an error. Now, AbstractHTTPHandler uses chunked transfer
encoding.
 urllib.robotparser – The RobotFileParser now supports the Crawl-delay and Request-rate extensions.
 venv – venv now accepts a new parameter- –prompt. This is an alternative prefix for the virtual
environment.
 warnings – The warnings.warn_explicit() function now has an optional ‘source’ parameter.
 winreg – What’s new? The 64-bit integer type REG_QWORD.
 winsound – winsound now allows us to pass keyword arguments to Beep, MessageBeep, and
PlaySound.
 bg. xmlrpc.client – This module now supports unmarshalling additional data types that are used by
the Apache XML-RPC implementation for numerics and None.
 zipfile – The class method ZipInfo.from_file() allows us to make a ZipInfo instance from a filesystem
file.
 zlib – We can now pass keyword arguments to functions compress() and decompress().
So, this is all on what’s new in Python 3.6 Tutorial. Hope you like our explanation.

7. Conclusion

In this article on what’s new in Python 3.6, we discussed what changes have been made to Python 3.5 to make it
Python 3.6. Tell us how you like them the what’s new in Python 3.6 article.

How to Install Python on Windows – Possible Issues after Installation


by DataFlair Team · November 15, 2018

1. Install Python – Objective

Previously, we discussed Introduction to python. In this Install Python Windows tutorial, we will see how to
install python windows. Along with this, we will discuss issues we faced after the installation of Python.

Learn: Python Syntax and Semantics for Beginners

So, let’s start how to install Python Windows Tutorial.

How to Install Python on Windows – Possible Issues after Installation

2. How to Install Python on Windows?

Installing and running Python on your personal computer is no difficult task. It involves just a few simple steps:

 Download binaries from python.org


 Install the binaries
 Add Python to system environment variables
 Install pip
 [Optional] Install virtualenv using pip
a. Download binaries from python.org

Install Python 3 on Windows

Firstly, to install Python Windows you need to download required binaries from the following link:

https://www.python.org/downloads/

We recommend you to download Python latest (Python 3.6.3, currently) installer for Windows.

If you have a different OS, download binaries accordingly. You may choose an x86-64 installer if you have a
64-bit system. Choose an x86 installer if you have a 32-bit system. But you can also click on the download
button in the page header.

b. Install Python binaries


Next, you need to install the binaries you downloaded.

Run the installer. It will show you two options.

Install PIP Windows

This is the default option. It also includes the IDLE (Integrated Development Environment),

pip, and the official documentation. It also creates shortcuts.

Learn:Python Comment, Indentation and Statement


Customize Python Installation

This option allows you to choose the features that you want.

Install Python 3 on Windows: Customize Python Installation

You will see the following checkboxes:

 Documentation
 pip
 tcl/tk and IDLE- installs tkinter and the IDLE
 Python test suite- This installs the standard library test suite of Python

Advanced Options while Installing Python


Install Python on Windows: Advanced Options while Installing Python

 Next, it gives you a set of advanced options:


 Install for all users
 Associate files with Python (requires the py launcher)
 Create shortcuts for installed applications
 Add Python to environment variables
 Precompile standard library
 Download debugging symbols
 Download debug binaries (requires VS 2015 or later)
 You may also specify the location to install the directory at.

Learn: Variables and Data Types in Python

Other than these two options, the installer also asks you if you want to add Python to PATH.

We will look at this in step 3. Click ‘Install’ and wait till the progress meter hits the end.
Locate the Python directory on your computer and append the location to the end of the PATH variable. It may
look like this: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32

An environment variable holds values about the current environment. You need it so you can access Python
through the command line.

d. Install pip
If you tick the checkbox for pip in Step 2, pip will install for you.

e. [Optional] Install virtualenv using pip

Python Installation – Install virtualenv using pip


This step is optional. If you install virtualenv, you can create a virtual Python environment. Now you don’t need
to worry about version conflicts among projects. Use pip to install it:
pip install virtual env

Pip will install this for you.

Learn:Python Strings with String Functions and String Operations

3. After You Install Python on Windows

You have now installed Python. Now, you can find a list of programs in the Start Menu.
Install Python 3 on Windows

So now, you can reach Python in the following ways:

a. Command Prompt
You can run Python on the command prompt in two ways:

1) The Conventional way

Search for Command Prompt, and type the following:

python
Now, you can use it as an interpreter. As an example, we have calculated 2+3.

Install Python 3 on Windows

2) Using the Start Menu

Open the Start menu. Click on Python 3.6 (32-bit). This will take you to the command prompt for Python. You
can now begin coding.

Learn: Python Operators with Syntax and Examples

b. The IDLE

Python 3 install on Windows

You can use the Integrated Development Environment to code in Python. If you use the Python Shell, you can
use it as an interpreter. Otherwise, you can create a new file to write a Python program. Later, you can save it
and click ‘Run’ to run the program.

1. for a in range(7):
2. print(a)

c. Python 3.6 Manuals (32-bit)


This is the official Python documentation. You can refer to it when you run into an issue with a program.

d. Python 3.6 Module Docs (32-bit)


This takes you to the index of modules available in Python through a localhost.
Learn:Bitwise Operator in Python with Syntax and Example
4. Possible Issues After Python Installation

 The Python setup may fail if you don’t have Windows Service Pack 1 (SP1) installed on your computer.
It is a requirement for installing Python. You may download it from the following link:
https://support.microsoft.com/en-in/help/15090/windows-7-install-service-pack-1-sp1
 If it states an unspecified error, try downloading KB2999226 here:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=49093
It is an update for Windows 7.
 For other issues, check the log file.

So, this was all about the Install Python Windows Tutorial. Hope you like the tutorial on Install Python on
Windows.

5. Conclusion – Install Python Windows

We saw how easy it is to set up an environment for you to install Python on your computer. It does not take
more than 5 minutes in the best case. See you in the next lesson where we explain the syntax of Python.

If you have any query or feedback on the tutorial on Install Python on Windows, drop a comment and we will
get back to you.

Python Applications – 9 Real World Applications of Python


Programming
by DataFlair Team · July 17, 2019

Let’s discuss python applications to that python can accomplish in the world. In this applications of Python
programming tutorial, you will know about 9 applications of Python Lets go through these Python applications
one by one.

We’ve been learning Python programming over the last two months and we’ve learned quite some useful stuff.
But when you can see what you can do with something, it feels powerful. It lends you some actual motivation to
keep going.

So, let’s start Python Applications.


Python Applications – 9 Real World Applications of Python Programming

Python Applications

1. Web and Internet Development


Python lets you develop a web application without too much trouble. It has libraries for internet protocols like
HTML and XML, JSON, e-mail processing, FTP, IMAP, and easy-to-use socket interface. Yet, the package
index has more libraries:

 Requests – An HTTP client library


 BeautifulSoup – An HTML parser
 Feedparser – For parsing RSS/Atom feeds
 Paramiko – For implementing the SSH2 protocol
 Twisted Python – For asynchronous network programming

We also have a gamut of frameworks available. Some of these are- Django, Pyramid. We also get
microframeworks like flask and bottle. We’ve discussed these in our write-up on an Introduction to
Python Programming.
We can also write CGI scripts, and we get advanced content management systems like Plone and Django CMS.

2. Applications of Python Programming in Desktop GUI


Most binary distributions of Python ship with Tk, a standard GUI library. It lets you draft a user interface for an
application. Apart from that, some toolkits are available:

 wxWidgets
 Kivy – for writing multitouch applications
 Qt via pyqt or pyside
And then we have some platform-specific toolkits:

 GTK+
 Microsoft Foundation Classes through the win32 extensions
 Delphi

3. Science and Numeric Applications


This is one of the very common applications of python programming. With its power, it comes as no surprise
that python finds its place in the scientific community. For this, we have:

 SciPy – A collection of packages for mathematics, science, and engineering.


 Pandas- A data-analysis and -modeling library
 IPython – A powerful shell for easy editing and recording of work sessions. It also supports
visualizations and parallel computing.
 Software Carpentry Course – It teaches basic skills for scientific computing and running bootcamps. It
also provides open-access teaching materials.
 Also, NumPy lets us deal with complex numerical calculations.

4. Software Development Application


Software developers make use of python as a support language. They use it for build-control and management,
testing, and for a lot of other things:

 SCons – for build-control


 Buildbot, Apache Gump – for automated and continuous compilation and testing
 Roundup, Trac – for project management and bug-tracking.
 Roster of Integrated Development Environments

5. Python Applications in Education


Thanks to its simplicity, brevity, and large community, Python makes for a great introductory programming
language. Applications of python programming in education has huge scope as it is a great language to teach in
schools or even learn on your own.
If you still haven’t begun, we suggest you read up on what we have to say about the white and dark sides of
Python. Also, check out Python Features.

6. Python Applications in Business


Python is also a great choice to develop ERP and e-commerce systems:

 Tryton – A three-tier, high-level general-purpose application platform.


 Odoo – A management software with a range of business applications. With that, it’s an all-rounder and
forms a complete suite of enterprise-management applications in-effect.

7. Database Access
With Python, you have:
 Custom and ODBC interfaces to MySQL, Oracle, PostgreSQL, MS SQL Server, and others. These are
freely available for download.
 Object databases like Durus and ZODB
 Standard Database API

8. Network Programming
With all those possibilities, how would Python slack in network programming? It does provide support for
lower-level network programming:

 Twisted Python – A framework for asynchronous network programming. We mentioned it in section 2.


 An easy-to-use socket interface

9. Games and 3D Graphics


Safe to say, this one is the most interesting. When people hear someone say they’re learning Python, the first
thing they get asked is – ‘So, did you make a game yet?’

PyGame, PyKyra are two frameworks for game-development with Python. Apart from these, we also get a
variety of 3D-rendering libraries.

If you’re one of those game-developers, you can check out PyWeek, a semi-annual game programming contest.

Other Python Applications


These are some of the major Python Applications. Apart from what we just discussed, it still finds use in more
places:

 Console-based Applications
 Audio – or Video- based Applications
 Applications for Images
 Enterprise Applications
 3D CAD Applications
 Computer Vision (Facilities like face-detection and color-detection)
 Machine Learning
 Robotics
 Web Scraping (Harvesting data from websites)
 Scripting
 Artificial Intelligence
 Data Analysis (The Hottest of Python Applications)

This was all about the Python Applications Tutorial. If you like this tutorial on applications of Python
programming comment below.

Why should you Learn python? Refer this link to get your answer.
Ready to install Python? Refer this link Python Installation
Summary

Python is everywhere and now that we know python Applications. We can do with it, we feel more powerful
than ever. If there’s a unique project you’ve made in the Python language.

Share your experience with us in the comments. You can also share your queries regarding Python Application
tutorial.

For Reference

Python in Healthcare – Good Health now depends on Code too!!


by DataFlair Team · September 11, 2019

We have always known python as an object-oriented, high-level programming language with dynamic
semantics. It has attracted users from different platforms for its advantages in rapid application development
and its dynamic building options. Little did we know that it is considered to be one of the “safest” programming
languages too apart from all the popularity because of its functionality. Also, Python plays an important role in
the healthcare sector.

Although it is tough for a normal person to believe that programming languages like Python are important in
healthcare. There’s not only one reason that makes python in the limelight of healthcare. There are a number of
reasons which make Python an important asset for the ones researching in the healthcare department.

Let’s explore, what is the importance of Python in healthcare and why it is considered to be one of the safest
languages.

Before moving ahead in Python in Healthcare article, read a little about Python Frameworks that are Django and
Flask.
Django and Flask – The Python Frameworks

The performance of Python is appreciated against abilities like meeting deadlines, quality and amount of code.
To achieve the same, Python is present with a framework Django.

Its trustworthy modules are so effective that you don’t need to develop them by yourself. Also, the built-in
maintenance against the web-app attack adds to its utility. Django framework allows developers to meet their
requirements of any business idea related to eHealth or telemedicine projects.

There is also Flask, again a Python-based framework often used for building prototypes because of its great
efficiency and can be extended to the full-stack framework with existing extensions. Its uses are still less than
that of Django because it requires more time for configuration.

Why Python in Healthcare is Important?


Below are some important reasons for using Python in Healthcare sector, have a look –

1. Python and its frameworks work on principles that are grounded equally and agreed upon the HIPAA
checklist.
2. A full look at the big data healthcare allows the organization to exchange information for seeking
patient outcomes.
3. The performance of the platforms made with Python is focused on their availability in phones and the
web.

How Medical Startups are using Python?

 Roam Analysis
Uses machine learning (artificial intelligence) and comprehensive contextual data to take over
biopharmaceutical and medical device companies, that need to make decisions, suggest treatments and get the
best possible patient outcomes.

Roam’s platform is powered by a proprietary data asset called Health Knowledge Graph which is continuously
enriched using natural language processing to gather information and make connections with new data.

According to the official description, “Roam’s machine learning and data platform powers rich analysis
of patient journeys to reveal the factors affecting treatment decisions and outcomes.”

 AiCure
It is an NIH and VC-funded healthcare startup in New York. AiCure automates the process of ensuring that
patients are taking their medicines, that too at their assigned time. It has combined artificial intelligence and
mobile technology together.

For example, it uses computer vision to identify the patient (face recognition) and verify the right pill for the
specific patient (using pill recognition) and they are actually consuming them (action recognition). They
develop their backend applications using Django frameworks and use python base coding and research
engineers to develop it.
Amazed by the magic of Python in Healthcare? Here are some more startups –

 Drchrono
It is a software as a service patient care platform that consists of a web and cloud-based app. This American
company is for doctors and patients that make electronic health records available digitally and provides
management and medical billing services. This is also a phone and web concentrated platform.

 Fathom Health
Again a Healthcare startup with deep learning NLP system for reading and understanding electronic health
records. This startup with headquarters in San Francisco, California is backed up by Google. Their employees
are familiar with Flask for API programming and data engineers prefer Python’s NLTK.

Summary
As we saw that Python is not only suitable for programming and web-based applications but it is also helpful in
the healthcare sector. The Python is in the continuous development face. That is one of the major reasons why
Python got such a big hand in the healthcare department. This application of Python will lead towards a better
future and betterment of healthcare. Healthcare is a challenging field and Python is performing very well here.

Python role in Healthcare is just not limited here there are much more applications of Python that will lead
towards a better and more high tech future. Many of us are unknown from the fact that Iron Man’s Jarvis is
made up of Python.

So you must start thinking of your future in Python. As Python is the most trending language, you must
start learning Python on your own to secure your future.

Happy learning

Python will make you rich in the stock market!


by DataFlair Team · September 13, 2019

There are so many factors involved in the prediction of stock market performance hence it becomes one of the
most difficult things to do especially when high accuracy is required. Here data science & its techniques have
been used to search patterns and insights that were not approachable before. Learning Python- object-oriented
programming, data manipulation, data modeling, and visualization is a ton of help for the same. So, what are
you waiting for? Read the complete article and know how helpful Python for stock market.

Stocker is a Python class-based tool used for stock prediction and analysis. (for complete code refer GitHub)
Stocker is designed to be very easy to handle. Even the beginners in python find it that way. It is one of the
examples of how we are using python for stock market and how it can be used to handle stock market-related
adventures.

So, before we move further, I recommend you to check the Python master guide, to learn everything
about Python.
Python for Stock market
Let’s look at the analytical capabilities of Stocker in parts.

Starting with Stocker


The first thing that should be done is importing the Stocker class into the current python session after installing
the required libraries. You can use it to create an object. The constructed object will contain all the properties of
the Stocker class. As the stocker is built on quandl WIKI database hence it allows access to 3000 and more US
stocks.

Python classes are comprised of – attributes and methods. Amongst all the attributes of the class, one of it is
stock data for a specific company.

The benefits of using the Python class include – the functions and the data it acts on are associated with the
same object. The entire history of the stock can be plotted by using the method of the Stocker object. The
‘plot_stock’ function has a number of arguments that are optional and by default, it plots the adjusted closing
price for the entire date range that can also be customized according to our needs (range, stats to be plotted, type
of plot). Using ‘plot)stock’ we can investigate any number of quantities in the data present in any data range and
also suggest real-world correlations.

Additive tools
These are very powerful for analyzing and predicting time series. We know that the long term trend of any
established multinational company seems to be increasing in nature but there is a possibility of identifying
yearly or daily basis patterns. Such help of time series with daily observations can be provided by Prophet,
developed by Facebook. Stocker can do all the work that be done by Prophet behind the scenes using simple
method call to create and inspect the model.
These types of models remove disturbance present in data and smoothen it. Prophet models also look into
fluctuations of data in real-life processes and make predictions for the future. Though there is concern related to
past data but future data analysis is what companies strive for. This method call returns two objects (data and
model) which are then assigned to variables that are later on used to plot time series components.

Explore top Python Applications to know more about the use of Python

Changepoints
It occurs when the time-series go from increasing to decreasing or vice-versa. These patterns are also very
important as one needs to know when the stock rate is at its peak or there are significant economic benefits.
Identifying these points and their cause of change helps in predicting the future. The stocker object can
automatically predict the 10 largest changepoints which tend to line up near the peaks and valleys of the stock
price graph (generally). On the other hand, the prophet can only find changepoints in the first 80% data only.
Google search tools allow us to see the popularity of any search word over time in Google searches. Stocker can
automatically retrieve this data for any specific term.

These are only the first half capabilities of the stocker where Python for stock market is used. The second half –

Predictions
They are designed for forecasting, or predicting future prices. This is a tiresome exercise and hence needs plenty
of learning to get into the actual process. The capabilities are publically available, even creating the tool itself.

Summary
We live in an age where anyone can learn programming or arts like data science or machine learning without
that much of formal instructions. The idea can be anything, even stock prediction, python can be used in any
sort of application base. All you need is hands-on knowledge of it!

Python Syntax | The Best Tutorial to Learn Python Syntax


by DataFlair Team · September 17, 2019

1. Python Syntax: Objective

Previously, we saw what is Python and how to set up a Python environment on your computer. The code in
any language must follow a set of rules. Today, we will learn about the Python syntax. In doing so, you will see
what is Python Syntax and how it is different to Java and C++. After this lesson, you will be able to identify and
debug beginner Python syntax.

So, let’s start the Python Syntax Tutorial.


Python Syntax | The Best Tutorial to Learn Python Syntax

Read: Python Built-In Functions with Syntax and Examples

2. Introduction to Python Syntax

Let us see various basic python syntax that is used while doing programming in Python.

3. Python Line Structure

A Python program comprises logical lines. A NEWLINE token follows each of those. The interpreter ignores
blank lines.

The following line causes an error.

1. >>> print("Hi
2. How are you?")

Output:

SyntaxError: EOL while scanning string literal

If you face any doubt anywhere in the Python Syntax Tutorial, Please Comment.

Read: Python Function Arguments with Types, Syntax, and Examples

4. Python Multiline Statements

This one is an important Python Syntax


We saw that Python does not mandate semicolons. A new line means a new statement. But sometimes, you may
want to split a statement over two or more lines. It may be to aid readability. You can do so in the following
ways.

a. Use a backward slash


1. >>> print("Hi\
2. how are you?")

Output:

Hihow are you?

You can also use it to distribute a statement without a string across lines.

1. >>> a\
2. =\
3. 10
4. >>> print(a)

Output:

10

b. Put the string in triple quotes


1. >>> print("""Hi
2. how are you?""")

Output:

Hi
how are you?

However, you can’t use backslashes inside a docstring for statements that aren’t a string.

1. >>> """b\
2. =\
3. 10"""

Output:
‘b=10’

1. >>> print(b)

Output:

Traceback (most recent call last):


File “<pyshell#6>”, line 1, in <module>
print(b)
NameError: name ‘b’ is not defined
Read: Python Functions with Syntax and Examples

5. Python Comments

Python Syntax ‘Comments’ let you store tags at the right places in the code. You can use them to explain
complex sections of code. The interpreter ignores comments. Declare a comment using an octothorpe (#).

1. >>> #This is a comment

Python does not support general multiline comments like Java or C++.

6. Python Docstrings

A docstring is a documentation string. Like a comment, this Python Syntax is used to explain code. But unlike
comments, they are more specific. Also, they are retained at runtime. This way, the programmer can inspect
them at runtime. Delimit a docstring using three double quotes. You may put it as a function’s first line to
describe it.

1. >>> def func():


2. """
3. This function prints out a greeting
4. """
5. print("Hi")
6. >>> func()

Output:

Hi

Any query yet in Python Syntax Tutorial? Please Comment.

7. Python Indentation

Since Python doesn’t use curly braces to delimit blocks of code, this Python Syntax is mandatory. You can
indent code under a function, loop, or class.

1. >>> if 2>1:
2. print("2 is the bigger person");
3. print("But 1 is worthy

too"); Output:

2 is the bigger
person But 1 is
worthy too

You can indent using a number of tabs or spaces, or a combination of those. But remember, indent statements
under one block of code with the same amount of tabs and spaces.

1. >>> if 2>1:
2. print("2 is the bigger person");
3. print("But 1 is worthy too");
Output:

SyntaxError: unindent does not match any outer indentation level

8. Python Multiple Statements in One Line

You can also fit in more than one statement on one line. Do this by separating them with a semicolon. But
you’d only want to do so if it supplements readability.

1. >>> a=7;print(a);

Output:

9. Python Quotations

Python supports the single quote and the double quote for string literals. But if you begin a string with a single
quote, you must end it with a single quote. The same goes for double quotes.

The following string is delimited by single quotes.

1. >>> print('We need a

chaperone'); Output:

We need a chaperone

This string is delimited by double quotes.

1. >>> print("We need a 'chaperone'");

Output:

We need a ‘chaperone’

Notice how we used single quotes around the word chaperone in the string? If we used double quotes
everywhere, the string would terminate prematurely.

1. >>> print("We need a "chaperone"");

Output:

SyntaxError: invalid syntax

10. Python Blank Lines

If you leave a line with just whitespace, the interpreter will ignore it.

Let us see next python syntax of Identifiers.


11. Python Identifiers

An identifier is a name of a program element, and it is user-defined. This Python Syntax uniquely identifies the
element. There are some rules to follow while choosing an identifier:

1. An identifier may only begin with A-Z, a-z, or an underscore(_).


2. This may be followed by letters, digits, and underscores- zero or more.
3. Python is case-sensitive. Name and name are two different identifiers.
4. A reserved keyword may not be used as an identifier. The following is a list of keywords.

and def False import not True


as del finally in or try
assert elif for is pass while
break else from lambda print with
class except global None raise yield
continue exec if nonlocal return

Apart from these rules, there are a few naming conventions that you should follow while using this Python
syntax:

1. Use uppercase initials for class names, lowercase for all others.
2. Name a private identifier with a leading underscore ( _username)
3. Name a strongly private identifier with two leading underscores ( password)
4. Special identifiers by Python end with two leading underscores.

12. Python Variables

In Python, you don’t define the type of the variable. It is assumed on the basis of the value it holds.

1. >>> x=10
2. >>> print(x)

Output:

10

1. >>> x='Hello'
2. >>> print(x)

Output:

Hello

Here, we declared a variable x, and assigned it a value of 10. Then we printed its value. Next, we assigned it the
value ‘Hello’, and printed it out. So, we see, a variable can hold any type of value at a later instant. Hence,
Python is a dynamically-typed language.

13. Python String Formatters

Let us see next python syntax String formatters.


a. % Operator
You can use the % operator to format a string to contain text as well as values of identifiers. Use %s where you
want a value to appear. After the string, put a % operator and mention the identifiers in parameters.

1. >>> x=10; printer="HP"


2. >>> print("I just printed %s pages to the printer %s" % (x,

printer)) Output:

I just printed 10 pages to the printer HP

b. format method
The format method allows you to format a string in a similar way. At the places, you want to put values, put
0,1,2,.. in curly braces. Call the format method on the string and mention the identifiers in the parameters.

1. >>> print("I just printed {0} pages to the printer {1}".format(x, printer))

Output:

I just printed 10 pages to the printer HP

You can also use the method to print out identifiers that match certain values.

1. >>> print("I just printed {x} pages to the printer {printer}".format(x=7,

printer='HP')) Output:

I just printed 7 pages to the printer HP

c. f-strings
If you use an f-string, you just need to mention the identifiers in curly braces. Also, write ‘f’ right before the
string, but outside the quotes used.

1. >>> print(f"I just printed {x} pages to the printer {printer}")

Output:

I just printed 10 pages to the printer HP

So, this was all about the Python Syntax tutorial. Hope you like our explanation

14. Python Syntax – Conclusion

In this Python Syntax tutorial, we learned about the basic Python syntax. We learned about its line structure,
multiline statements, comments and docstrings, indentation, and quotations. We also learned about blank lines,
identifiers, variables, multiple statements in one line, and string formatters. In the next lesson, we will look at
different variable types in Python.
If you have any query regarding the Python Syntax Tutorial, please drop a comment.

Python Comment | Python Indentation | Python Statement


by DataFlair Team · May 17, 2019

1. Python Comment, Python Indentation and Python Statement

Now that we’re getting used to Python and its ways, we think it is time to sharpen python syntax in our brains.
In this tutorial, we will revise basic syntax- Python Comment, Python indentation, and a Python statement with
their sub types, syntax and examples.

So, let’s start learning Python Comment, Python Indentation, and Python Statement.

Python Comment, indentation, and Statement

2. Python Statement

The Python interpreter deals with statements. Through our journey, we have seen the conditional statements in
python like ‘if’, ‘for’, ‘while’, and so on. The following is a Python statement.

1. >>> a*=3
Python Comment – Python Statement

a. Multiline Python Statement


In Python, every statement ends with a newline character. But like we have seen, it is possible to span a
statement over multiple lines. We do this using the ‘\’ character.

1. >>> a=\
2. 10\
3. +20
4. >>> a

30

1. >>> "Hello\
2. hi"

‘Hellohi’
But you can also use a set of parentheses for this.

1. >>> a=(
2. 10+
3. 20)
4. >>> a

30

1. >>> type(a)
2. <class 'int'>

Read: Syntax in Python with Examples

b. Multiple Python Statement in One Line


You can easily put multiple statements in python on one line.
1. >>> a=7; print(a)

7
You can also do this to an if-statement.

1. >>> if 2>1: print("2")

c. Strings Python Statements


To declare strings in python, you may use single or double quotes.

1. >>> "Hello 'user'"

“Hello ‘user'”
If you use double quotes outside, use single quotes wherever you need to use a quote inside.

d. Blank Lines Python Statements


The interpreter simply ignores blank lines.

3. Python Indentation

Unlike C++ or Java, Python does not use curly braces for indentation. Instead, Python mandates indentation. At
this point, our inner monsters are laughing at the lazy programmers around us.

1. >>> if 2>1:
2. print("2")

2
There are no strict rules on what kind of Python indentation you use. But it must be consistent throughout the
block. Although, four whitespaces are usually preferred, and tabs are discouraged. Let’s take an example with
an inconsistent indentation in python.

1. >>> if 2>1:
2. print("1")
3. print("2")

SyntaxError: unexpected indent

Read: Python Built-In Functions with Syntax and Examples

4. Python Comment

Python Comment is a programmer’s tool. We use them to explain the code, and the interpreter ignores them.
You never know when a programmer may have to understand code written by another and make changes to it.
Other times, give your brain a month’s time, and it might forget what it once had conjured up in your code. For
these purposes, good code will hold comments in the right places.
In C++, we have // for single-lined comments, and /* … */ for multiple-lined comments. Here, we only have
single-lined python comment.
To declare a Python comment, use the octothorpe (hash) (#).

1. >>> #This is a comment


2. >>>

Types of Python comment

a. Multiline Python Comment


To have multiline python comment in your code, you must use a hash at the beginning of every line of your
comment in python.

1. >>> #Line 1 of comment


2. >>> #Line 2 of comment
3. >>> #Line 3 of comment

You can also use triple quotes (‘’’ ‘’’ or “”” “””) for this purpose.

1. >>> """This comment


2. is spanned across
3. multiple lines"""

‘This comment\nis spanned across\nmultiple lines’


This gives us an output because we type it in the shell. When you create a file and write this in that, there is no
output. While triple quotes are generally used for multiline python comment, we can conveniently use them for
python comment as well.
Triple quotes will also preserve formatting.

1. >>> print("""Hello
2. Hi""")

Hello
Hi
b. Docstrings Python Comment
A docstring is a documentation string in Python. It is the first statement in a module, function, class, or
method in Python. In this, we explain what a python function/class does.

1. >>> def sayhi():


2. """
3. This function prints Hi
4. """
5. print("Hi")
6. >>> sayhi()

Hi
To check a function’s docstring, use its doc attribute.

1. >>> def sayhi():


2. """
3. This function prints Hi
4. """
5. print("Hi")
6. >>> sayhi. doc

‘\n\tThis function prints Hi\n\t’


The interpreter is unable to get the docstring to a function if it isn’t the first thing in the python function.

1. >>> def sayhi():


2. print("Hi")
3. """
4. This function prints Hi
5. """
6. >>> sayhi. doc
7. >>>

Read: Applications of Python Programming Language

So, this was all about Python indentation, comment and statement. Hope you like our explanation.

5. Python Comment – Conclusion

Hope we’ve reconnected you to some basics python Syntax- Python indentation, comments and statement.
Don’t forget to try out your own combinations. If you don’t get something in Python indentation, Comment and
statement ask us in the comments.

Python Assert Statements | Assertionerror in Python


by DataFlair Team · May 17, 2019

1. Objective – Python Assert


In what follows, we will discuss Python Assert Statements. We will see what they are, how to implement and
where to use Python Assert Statements. Moreover, we will discuss Python Assert syntax and example. Also, we
will learn using error message and handling Assertionerror Python.

So, let’s start Python Assert Tutorial.

Python Assert Statements | Assertion Error in Python

2. What are Python Assert Statements?

Let’s discuss the meaning of Assertion in Python Programming Langauge in detail:

a. What is Assertion in Python?


An Assertion in Python or a Python Assert Statement is one which asserts (or tests the trueness of) a condition
in your code. This is a Boolean expression that confirms the Boolean output of a condition.
Do you know about Python variables and Data Types

b. Why Assertion in Python is Used?


This is a debugging tool. If it finds the condition to be true, there is nothing it needs to do. So, it moves over to
the next line of code. If not, it stops all operations and throws an error. It also shows the point of error in the
code.
The point of assertions in Python is to help deal with unrecoverable errors.
The assertion in Python programming Language

c. Where Assertion in Python used?


 In checking types/ in checking valid input.
 In checking values of arguments.
 Checking outputs of functions.
 As a debugger to halt where an error occurs.
 In testing code.
 In detecting abuse of an interface by another programmer.

3. Python Assert Example and Syntax

Python Assert Statement is one we always want to see true. Let’s take an example and see what happens when a
condition turns false-
You must read about Python Syntax

1. >>> assert 2==3

Traceback (most recent call last):


File “<pyshell#0>”, line 1, in <module>
assert 2==3
AssertionError
Hence, this is an AssertionError in Python. We’d use it to catch false assumptions we made while writing the
code.

a. Python Assert Syntax


1. assert <condition>
1. assert <condition>, <error message>

b. Python Assert Example


Now, let’s devise a function to divide two numbers. What happens when we try to divide a number by 0?

1. >>> def div(p,q):


2. assert q!=0
3. return p/q
4. >>> div(2,3)
5. 0.6666666666666666
6. >>> div(2,0)

Traceback (most recent call last):


File “<pyshell#7>”, line 1, in <module>
div(2,0)
File “<pyshell#5>”, line 2, in div
assert q!=0
AssertionError
As you can see, this stopped the program and threw a Python AssertionError.
Have a look at Python Classes and Object-Oriented Programming

4. Using an Error Message in Python Assert

Instead of throwing six red lines at the developer, Python may want to display something more sophisticated
and less hostile. For this, we give an error message to the assert statement. Let’s see how.

1. >>> def div(p,q):


2. assert q!=0, "You cannot divide a number by zero\nPlease try again"
3. return p/q
4. >>> div(2,0)

Traceback (most recent call last):


File “<pyshell#10>”, line 1, in <module>
div(2,0)
File “<pyshell#9>”, line 2, in div
assert q!=0, “You cannot divide a number by zero\nPlease try again”
AssertionError: You cannot divide a number by zero
Please try again
Okay, this seems longer than what we got before this, but pull a magnifying glass to the last line. It tells you
what really happened, but we can do better than this.

5. Handling the AssertionError in Python

Let’s put this function in a try block and hope to catch it.

1. >>> try:
2. def div(p,q):
3. assert q!=0, "You cannot divide a number by zero\nPlease try again"
4. return p/q
5. except:
6. print("So you tried to divide by 0. Please try again")
7. >>> div(2,0)

Traceback (most recent call last):


File “<pyshell#18>”, line 1, in <module>
div(2,0)
File “<pyshell#17>”, line 3, in div
assert q!=0, “You cannot divide a number by zero\nPlease try again”
AssertionError: You cannot divide a number by zero
Please try again
Wow, this did not work at all. Where’s the “So you tried to divide by 0. Please try again”? Let’s give this
another try.
Let’s discuss Exception Handling in Python

1. >>> def div(p,q):


2. try:
3. assert q!=0, "You cannot divide a number by zero\nPlease try again"
4. return p/q
5. except:
6. print("So you tried to divide by 0. Please try again")
7. >>> div(2,0)

So, you tried to divide by 0. Please try again


Ah, works like a charm this time!

So, this was all in Python Assert. Hope you like our explanation

6. Conclusion: Python Assert

Call it sanity check or paranoia, Python assert statements help us make sure everything’s going fine with our
code. Or maybe we can say that assertions in Python are a secret gift to you from yourself. Twenty days from
today, you will open the box and feel grateful.

Hence, we discussed Assert Statements in Python. Moreover, we saw Python Assert example and syntax. Also,
we discussed using error message and handling Assertionerror Python. Have something to add to this tutorial?
Feel free to drop it in the comments below.

Python Number Types – Python Int, Float, Complete Numbers


by DataFlair Team · May 17, 2019

1. Python Number Types

In our article on Python Variables and Data Types, we learnt about different data types supported by Python.
Now, we will dig a little deeper into those Python number types. As we have seen, a python number can be-
Python int, Python float, or even Python complex number. long is no longer supported by Python 3.x.

So, let’s begin with the python number types tutorial.


Python Number Types – Python Int, Float, Complete Numbers

2. Python Number Types

Before starting with Python number types in, let us revise Python basics and various syntax used in Python
for better understanding.

A number is an arithmetic entity that lets us measure something. Python allows us to store the integer, floating,
and complex numbers and also lets us convert between them. Since Python is dynamically-typed, there is no
need to specify the type of data for a variable. So now let’s start with python number types.

 None- The None keyword indicates the absence of value.

3. Python int

Python can hold signed integers.

1. >>> a=7
2. >>> a

7
It can hold a value of any length, the only limitation being the amount of memory available.

1. >>> a=9999999999999999999999999999999999999
2. >>> a

9999999999999999999999999999999999999

There are three types of this Python number types:


Python Number Types – Python Int

i. type() function
It takes one argument, and returns which class it belongs to.

1. >>> a=9999999999999999999999999999999999999
2. >>> type(a)

<class ‘int’>

ii. isinstance() function


It takes two arguments. The first is the construct(ex- a variable or a list), and the
second is a class. It returns True or False based on whether the construct belongs to
that class. Suppose we want to check if ‘a’ belongs to class bool. We write the following
code for the same.

1. >>>

isinstance(a,bool) False

Since it belongs to the class ‘int’ instead, it returns False.

iii. Exponential numbers

You can write an exponential number using the letter ‘e’ between the mantissa and the
exponent.
1. >>> print(2e5)

200000.0

Remember that this is power of 10. To raise a number to another’s power, we use the ** operator.
If you face difficulty in Python number types, please comment.

4. Python float

Python also supports floating-point real values. An int cannot store the value of the mathematical constant pi,
but a float can.

1. >>> from math import pi


2. >>> pi

3.141592653589793

1. >>> type(pi)

<class ‘float’>

A float value is only accurate upto 15 decimal places. After that, it rounds the number off.

1. >>> a=1.1111111111111111119
2. >>> a

1.1111111111111112

Note that division results in floats.

1. >>> 2/2

1.0

5. Python Complex Numbers

A complex number is a Python number type made of real and imaginary parts. It is represented as a+bj.

1. >>> a=2+3j
2. >>> a

(2+3j)

i. Coefficient to the imaginary part


Here, 2 is the real part, and 3j is the imaginary part. To denote the irrational part, however, you can’t use the
letter ‘i’, like you would do on paper.

1. >>> a=2+3i
SyntaxError: invalid syntax

Also, it is mandatory to provide a coefficient to the imaginary part.

1. >>> a=2+j

Traceback (most recent call last):

File “<pyshell#33>”, line 1, in <module>

a=2+j

NameError: name ‘j’ is not defined

In this case, a coefficient of 1 will do.

1. >>> a=2+1j
2. >>> a

(2+1j)

ii. Operations on complex numbers


Finally, you can perform the basic operations on complex numbers too.

1. >>> a=2+3j
2. >>> b=2+5j
3. >>> a+b

(4+8j)

1. >>> a*=2
2. >>> a

(4+6j)

Here, *= is an in-place assignment operator.

Any Doubt yet in Python number Type? Please Comment.

6. Writing numbers in binary, octal, and hexadecimal

More often than not, programmers need to deal with numbers other than decimal. To do this, you can use
appropriate prefixes.

Number System Prefix


Binary 0b or 0B
Octal 0o or 0O
Hexadecimal 0x or 0X
i. Binary
When you want to write a binary number, use the prefix 0b or 0B. For example, we know that the binary for 7 is
111.

1. >>> print(0b111)

You can also apply conversion functions on these numbers.

1. >>> int(0b10)

ii. Octal
The prefix for octal is 0o or 0O.

1. >>> print(0O10)

The following code causes an error. This is because the octal number system does not have the number 8. It has
the numbers 0-7.

1. >>> print(0O8)

SyntaxError: invalid token

1. >>> float(0B10)

2.0

iii. Hexadecimal
The hexadecimal number system has numbers 0-9 and then A-F. For that, use the prefix 0x or 0X.

1. >>> print(0xFF)

255

1. >>> print(0xFE)

254

7. Python Conversion Functions


Although most times Python does the conversion as needed, you can do it explicitly if you want. These
functions allow us to convert one numeric type into another python number types.
Python Number Types – Python Conversion Functions

i. int()
The int() function can convert another numeric type into an int. It can also convert other types into an int, but in
this tutorial, we focus on numeric types.

1. >>> int(7)

1. >>> int(7.7)

As you can see, it does not round the number 7.7 up to 8; it truncates the 0.7.

However, you cannot convert a complex number into an int.

1. >>> int(2+3j)

Traceback (most recent call last):

File “<pyshell#22>”, line 1, in <module>

int(2+3j)
TypeError: can’t convert complex to int

1. >>> int(3j)

Traceback (most recent call last):

File “<pyshell#23>”, line 1, in <module>

int(3j)

TypeError: can’t convert complex to int

We can also apply this function on representations other than decimal, i.e., binary, octal, and hexadecimal.

1. >>> int(0b10)

1. >>> int(0xF)

15

ii. float()
This function converts another numeric type into a float.

1. >>> float(110)

110.0

1. >>> float(110.0)

110.0

Like int(), float() can’t convert a complex either.

1. >>> float(3j)

Traceback (most recent call last):

File “<pyshell#26>”, line 1, in <module>

float(3j)

TypeError: can’t convert complex to float

1. >>> float(0o10)

8.0

Here, we applied it to an octal number.


iii. complex()
The complex() function converts another numeric type into a complex number.

1. >>> complex(2)

(2+0j)

1. >>> complex(2.3)

(2.3+0j)

1. >>> complex(2+3.0j)

(2+3j)

iv. bin()
The bin() function returns the binary value of a number.

1. >>> bin(2)

‘0b10’

However, you can’t apply it to a float value or a complex value. The same is true for oct() and hex() functions
too.

1. >>> bin(2.3)

Traceback (most recent call last):

File “<pyshell#49>”, line 1, in <module>

bin(2.3)

TypeError: ‘float’ object cannot be interpreted as an integer

1. >>> bin(2+3j)

Traceback (most recent call last):

File “<pyshell#50>”, line 1, in <module>

bin(2+3j)

TypeError: ‘complex’ object cannot be interpreted as an integer

v. oct()
This function returns the octal value of a number.
1. >>> oct(8)

‘0o10’

We know that 8.0 is the same as 8, but the function doesn’t think the same. It is a float, so it cannot convert it
into an oct.

1. >>> oct(8.0)

Traceback (most recent call last):

File “<pyshell#59>”, line 1, in <module>

oct(8.0)

TypeError: ‘float’ object cannot be interpreted as an integer

vi. hex()
The hex() function returns the hexadecimal value of a number.

1. >>> hex(255)

‘0xff’

1. >>> hex(0)

‘0x0’

1. >>> hex(0)

‘0x0’

8. Python Decimal Module

Let’s try out adding 1.1 and 2.2 in the shell, and let’s compare it with 3.3.

1. >>> (1.1+2.2)==3.3

False

Why did it return False? Let’s try printing the sum.

1. >>> 1.1+2.2

3.3000000000000003

Woah, how did this happen? Well, this is duly attributed to hardware limitations, and is not a flaw of Python.
Because the hardware stores decimals as binary fractions, it isn’t possible to store it very accurately. Let’s take
an example.
1. >>> 1/3

0.3333333333333333

When we divide 1 by 3, it doesn’t return the full value, which is 0.3333333333333333… Python does provide a
solution to this problem. It has the ‘decimal’ module, which lets us choose precision. We will learn about
modules in a later lesson.

1. >>> import decimal


2. >>> print(decimal.Decimal(0.1))

0.1000000000000000055511151231257827021181583404541015625

Did you see what happened here? The Decimal() function preserved the significance.This was the Decimal
Function Python number type.

9. The fractions Module

Another module that Python provides, the fractions module lets you deal with fractions. The Fraction() function
returns the value in the form of numerator and denominator.

1. >>> from fractions import Fraction


2. >>> print(Fraction(1.5))

3/2

It can also take two arguments.

1. >>> print(Fraction(1,3))

1/3

10. The math Module


Another essential module is the math module. It has all important mathematical functions like exp,
trigonometric functions, logarithmic functions, factorial, and more.

1. >>> import math


2. >>> math.factorial(5)

120

1. >>> math.exp(3)

20.085536923187668

1. >>> math.tan(90)

-1.995200412208242

This was all about the Python number types tutorial.


11. Conclusion – Python Number Types

In this lesson, we learnt about Python number types. We looked at int, float, and complex numbers. We also
looked at how to write numbers in binary, octal, and hexadecimal representations. Then we looked at how to
convert one numeric type into another. We also looked at some important modules- decimal, fractions, math.
Hope you like the Python Number Type Tutorial. Try your own combinations and leave your feedback on
Python number types tutorial in the comments.
Refer Best Books for Python for in depth Python learning.

Python Random Number – Generate Random Numbers With NumPy


by DataFlair Team · September 29, 2018

1. Objective – Python Random Number

Today, in this Python tutorial, we will talk about Python Random Number. Moreover, we will see ways to
generate Random Number in Python. Also, we will discuss generating Python Random Number with NumPy.
At last, we will see Import Random Python with the example.
So, let’s begin with Python Random Number.

Python Random Number – Generate Random Numbers With NumPy

2. Need of Python Random Number

A Random Number in Python is any number in a range we decide. From initializing weights in an ANN to
splitting data into random train and test sets, the need for generating random numbers is apparent. Another use-
case could be the random shuffling of a training dataset in stochastic gradient descent. So today, we will discuss
pseudorandom generators in Python. We will also try doing that with the standard Python library and with
NumPy.

3. How to Generate Python Random Number?

What we really generate is pseudorandom numbers. There are numbers that appear nearly random but are
actually something we generate with a deterministic process.
A process of Generating Python Random Number

You must read about Python Libraries


Python uses the Mersenne Twister pseudorandom number generator. The process of generating random
numbers involves deterministically generating sequences and seeding with an initial number. The default for the
seed is the current system time in seconds/ milliseconds. A different seed will produce a different sequence of
random numbers.

a. Import Python Random


Before we can begin, let’s first import the module random from the Python Standard Library. You can directly
import it-
import random
To import a piece of functionality from it- say, random, you can:
from random import random
Or for seed, you can:
from random import seed

b. Random Floating Point Numbers


Let’s take an example of generation Python Random Number.

1. >>> from random import seed


2. >>> from random import random
3. >>> seed(7)
4. >>> random(),random(),random(),random()

(0.32383276483316237, 0.15084917392450192, 0.6509344730398537, 0.07243628666754276)


Have a look at Python Packages
Works for us. Now, what if we reseed to the same value and call the random() functions/methods again?

1. >>> seed(7)
2. >>> random(),random(),random(),random()
(0.32383276483316237, 0.15084917392450192, 0.6509344730398537, 0.07243628666754276)
You’ll find it gives us the same thing as it did earlier.

 Working with minimum values and multiplying the floats-

Some days, you may not want to generate Random Number in Python values between 0 and 1. In the following
piece of code, 2 is the minimum value, and we multiple the random number generated by 10.

1. >>> seed(7)
2. >>> 2+10*random()

5.238327648331624

c. Python Random Integers


We use the randint() function to get integers instead, randomly. It takes two arguments- the start and the top,
and then draws a random value from a uniform distribution. Each value has an equal chance of being picked.

1. >>> from random import randint


2. >>> seed(7)
3. >>> randint(0,9),randint(0,9),randint(0,9)

(5, 2, 6)
We asked for three random values, this gave us 5, 2, and 6.
Let’s learn Python Debugger

d. Getting Integers Randomly from a Range


randrange() randomly selects an element from range(start,stop,step).

1. >>> from random import randrange


2. >>> randrange(-2,4)

-1

1. >>> randrange(-2,4)

1. >>> randrange(-2,4)

1. >>> randrange(-2,4)

-1

1. >>> randrange(-2,4)

2
1. >>> randrange(-2,4)

-2

e. Random Gaussian Values


The gauss() function takes in two arguments- the mean and the standard deviation. This gives us a real-valued
distribution.

1. >>> from random import


gauss 2. >>> seed(7)
3. >>> gauss(0,1),gauss(0,1),gauss(0,1)

(-0.2558802884476004, 0.511431512516514, -0.2260961647831047)

f. Choosing Randomly From Lists


It is possible to randomly pick values from our own custom lists. We have the choice() function/method for this.

1. >>> list=[2,4,3,9,6,2,1,0,7,4,3,5,3,6,8]
2. >>> from random import choice
3. >>> seed(7)
4. >>> choice(list),choice(list),choice(list),choice(list),choice(list),choice(list)

(2, 3, 1, 3, 2, 4)
For a choice of multiple values, you can use choices() instead.
Let’s take a tour to Python Network Programming

1. >>> from random import choices


2. >>> choices(list,k=4)

[7, 2, 9, 7]

g. Randomly Choosing a Subset from a List


Once sample() puts an item from a list into the sublist, it does not add it back to the original list for picking
from. This is selection without replacement. Note that this does not modify the original list. This function/
method also takes the size of the subset to create.

1. >>> list

[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]

1. >>> from random import sample


2. >>> sample(list,6)

[6, 7, 4, 2, 4, 2]

h. Shuffling a List Randomly


We can shuffle a list like a deck of cards with the shuffle() function/ method. This shuffles the list in-place. In
other words, it does not need to create a new list to put shuffled items into one by one.

1. >>> list

[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]

1. >>> from random import shuffle


2. >>> shuffle(list)
3. >>> list

[6, 3, 2, 3, 6, 5, 0, 3, 4, 1, 4, 2, 9, 7, 8]
Note that this modifies the list.
Let’s revise Image Processing with NumPy and SciPy

4. How to Generate Python Random Number with NumPy?

With the seed() and rand() functions/ methods from NumPy, we can generate random numbers. The
functionality is the same as above.

1. >>> from numpy.random import seed


2. >>> from numpy.random import rand
3. >>> seed(7)
4. >>> rand(3)

array([0.07630829, 0.77991879, 0.43840923])

1. >>> seed(7)
2. >>> rand(3)

array([0.07630829, 0.77991879, 0.43840923])


So, this was all about Generating Python Random Number. Hope you like our explanation.

5. Conclusion – Python Random Number

Now you know how to generate random numbers in Python. We used two modules for this- random and numpy.
Moreover, we discussed the process of generating Python Random Number with examples. Tell us what you
think about the article Python Random Number. Got a topic you want us to write us on? Let us know in the
comments.

The Best Tutorial on Python Variables and Python Data Types


by DataFlair Team · June 19, 2019

1. Python Variables and Python Data Types

In this Python tutorial on Python Variables and Python Data Types, we will learn about at Python variables and
data types being used in Python. Since we know that Python is a dynamically-typed language, we don’t specify
the type of a variable when declaring one. We will also learn about converting one data type to another in
Python and local and global variables in Python. So, let’s begin with Python variables and Python Data Type
Tutorial.
The Best Tutorial on Python Variables and Python Data Types

2. What is Python Variables?

Let us now proceed towards Python variables and Python data types. Before starting, let us revise syntax in
python.

A variable is a container for a value. It can be assigned a name, you can use it to refer to it later in the program.
Based on the value assigned, the interpreter decides its data type. You can always store a different type in a
variable.

For example, if you store 7 in a variable, later, you can store ‘Dinosaur’.

a. Python Variables Naming Rules


There are certain rules to what you can name a variable(called an identifier).

 Python variables can only begin with a letter(A-Z/a-z) or an underscore(_).

1. >>> 9lives=9

SyntaxError: invalid syntax

1. >>> flag=0
2. >>> flag

1. >>> _9lives='cat'
2. >>> _9lives

‘cat’
 The rest of the identifier may contain letters(A-Z/a-z), underscores(_), and numbers(0-9).

1. >>> year2='Sophomore'
2. >>> year2

‘Sophomore’

1. >>> _$$=7

SyntaxError: invalid syntax

 Python is case-sensitive, and so are Python identifiers. Name and name are two different identifiers.

1. >>> name='Ayushi'
2. >>> name

‘Ayushi’

1. >>> Name

Traceback (most recent call last):


File “<pyshell#21>”, line 1, in <module>
Name
NameError: name ‘Name’ is not defined

 Reserved words (keywords) cannot be used as identifier names.

and def False import not True


as del finally in or try
assert elif for is pass while
break else from lambda print with
class except global None raise yield
continue exec if nonlocal return

b. Assigning and Reassigning Python Variables


To assign a value to Python variables, you don’t need to declare its type. You name it according to the rules
stated in section 2a, and type the value after the equal sign(=).

1. >>> age=7
2. >>> print(age)

1. >>> age='Dinosaur'
2. >>> print(age)

Dinosaur

However, age=Dinosaur doesn’t make sense. Also, you cannot use python variables before assigning it a value.
1. >>> name

Traceback (most recent call last):


File “<pyshell#8>”, line 1, in <module>
name
NameError: name ‘name’ is not defined

You can’t put the identifier on the right-hand side of the equal sign, though. The following code causes an error.

1. >>> 7=age

SyntaxError: can’t assign to literal

Neither can you assign python variables to a keyword.

1. >>> False=choice

SyntaxError: can’t assign to keyword

c. Multiple Assignment
You can assign values to multiple python variables in one statement.

1. >>> age,city=21,'Indore'
2. >>> print(age,city)

21 Indore

Or you can assign the same value to multiple python variables.

1. >>> age=fav=7
2. >>> print(age,fav)

77

This is how you assign values to Python Variables

d. Swapping variables
Swapping means interchanging values. To swap Python variables, you don’t need to do much.

1. >>> a,b='red','blue'
2. >>> a,b=b,a
3. >>> print(a,b)

blue red

e. Deleting variables
You can also delete python variables using the keyword ‘del’.
1. >>> a='red'
2. >>> del a
3. >>> a

Traceback (most recent call last):


File “<pyshell#39>”, line 1, in <module>
a
NameError: name ‘a’ is not defined

This is How you can delete Python Variables

Python Variables and Python Data Types

3. Python Data Types

Although we don’t have to declare type for python variables, a value does have a type. This information is vital
to the interpreter. Python supports the following Python data types.

a. Python Numbers
There are four numeric Python data types.

1. int– int stands for integer. This Python Data Type holds signed integers. We can use
the type() function to find which class it belongs to.

1. >>> a=-7
2. >>> type(a)

<class ‘int’>
An integer can be of any length, with the only limitation being the available memory.

1. >>> a=9999999999999999999999999999999
2. >>> type(a)

<class ‘int’>

2. float– This Python Data Type holds floating point real values. An int can only store
the number 3, but float can store 3.25 if you want.

1. >>> a=3.0
2. >>> type(a)

<class ‘float’>

3. long – This Python Data Types holds a long integer of unlimited length. But this construct does not exist in
Python 3.x.

4. complex- This Python Data Types holds a complex number. A complex number looks like this: a+bj Here, a
and b are the real parts of the number, and j is imaginary.

1. >>> a=2+3j
2. >>> type(a)

<class ‘complex’>

Use the isinstance() function to tell if python variables belong to a particular class. It takes two parameters- the
variable/value, and the class.

1. >>> print(isinstance(a,complex))

True

b. Strings
A string is a sequence of characters. Python does not have a char data type, unlike C++ or Java. You can delimit
a string using single quotes or double quotes.

1. >>> city='Ahmedabad'
2. >>> city

‘Ahmedabad’

1. >>> city="Ahmedabad"
2. >>> city

‘Ahmedabad’

1. Spanning a string across lines – To span a string across multiple lines, you
can use triple quotes.
1. >>> var="""If
2. only"""
3. >>> var

‘If\n\tonly’

1. >>> print(var)

If
Only

1. >>> """If
2. only"""

‘If\n\tonly’

As you can see, the quotes preserved the formatting (\n is the escape sequence for newline, \t is for tab).

2. Displaying part of a string– You can display a character from a string using its
index in the string. Remember, indexing starts with 0.

1. >>> lesson='disappointment'
2. >>> lesson[0]

‘d’

You can also display a burst of characters in a string using the slicing operator [].

1. >>> lesson[5:10]

‘point’

This prints the characters from 5 to 9.

3. String Formatters– String formatters allow us to print characters and values at once.
You can use the % operator.

1. >>> x=10;
2. >>> printer="Dell"
3. >>> print("I just printed %s pages to the printer %s" % (x,

printer)) Or you can use the format method.

1. >>> print("I just printed {0} pages to the printer {1}".format(x, printer))
2. >>> print("I just printed {x} pages to the printer {printer}".format(x=7,

printer="Dell")) A third option is to use f-strings.

1. >>> print(f"I just printed {x} pages to the printer {printer}")

4. String Concatenation– You can concatenate(join) strings.


1. >>> a='10'
2. >>> print(a+a)

1010

However, you cannot concatenate values of different types.

1. >>> print('10'+10)

Traceback (most recent call last):

File “<pyshell#89>”, line 1, in <module>;

print(’10’+10)

TypeError: must be str, not int

c. Python Lists
A list is a collection of values. Remember, it may contain different types of values. To define a list, you must
put values separated with commas in square brackets. You don’t need to declare a type for a list either.

1. >>> days=['Monday','Tuesday',3,4,5,6,7]
2. >>> days

[‘Monday’, ‘Tuesday’, 3, 4, 5, 6, 7]

1. Slicing a list – You can slice a list the way you’d slice a string- with the slicing operator.

1. >>> days[1:3]

[‘Tuesday’, 3]

Indexing for a list begins with 0, like for a string. A Python doesn’t have arrays.

2. Length of a list– Python supports an inbuilt function to calculate the length of a list.

1. >>> len(days)

3. Reassigning elements of a list– A list is mutable. This means that you can
reassign elements later on.

1. >>> days[2]='Wednesday'
2. >>> days

[‘Monday’, ‘Tuesday’, ‘Wednesday’, 4, 5, 6, 7]

4. Multidimensional lists– A list may have more than one dimension. We will look
further into this in the tutorial on Python Lists.
1. >>> a=[[1,2,3],[4,5,6]]
2. >>> a

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

d. Python Tuples
A tuple is like a list. You declare it using parentheses instead.

1. >>> subjects=('Physics','Chemistry','Maths')
2. >>> subjects

(‘Physics’, ‘Chemistry’, ‘Maths’)

1. Accessing and Slicing a Tuple– You access a tuple the same way as you’d access a list. The same goes for
slicing it.

1. >>> subjects[1]

‘Chemistry’

1. >>> subjects[0:2]

(‘Physics’, ‘Chemistry’)

2. A tuple is immutable– However, it is immutable. Once declared, you can’t change


its size or elements.

1. >>> subjects[2]='Biology'

Traceback (most recent call last):


File “<pyshell#107>”, line 1, in <module>
subjects[2]=’Biology’
TypeError: ‘tuple’ object does not support item assignment

1. >>> subjects[3]='Computer Science'

Traceback (most recent call last):


File “<pyshell#108>”, line 1, in <module>
subjects[3]=’Computer Science’
TypeError: ‘tuple’ object does not support item assignment

e. Dictionaries
A dictionary holds key-value pairs. Declare it in curly braces, with pairs separated by commas. Separate keys
and values by a colon(:).

1. >>> person={'city':'Ahmedabad','age':7}
2. >>> person

{‘city’: ‘Ahmedabad’, ‘age’: 7}


The type() function works with dictionaries too.

1. >>> type(person)

<class ‘dict’>

1. Accessing a value– To access a value, you mention the key in square brackets.

1. >>> person['city']

‘Ahmedabad’

2. Reassigning elements– You can reassign a value to a key.

1. >>> person['age']=21
2. >>> person['age']

21

3. List of keys– Use the keys() function to get a list of keys in the dictionary.

1. >>> person.keys()

dict_keys([‘city’, ‘age’])

f. bool
A Boolean value can be True or False.

1. >>> a=2>1
2. >>> type(a)

<class ‘bool’>

g. Sets
A set can have a list of values. Define it using curly braces.

1. >>> a={1,2,3}
2. >>> a

{1, 2, 3}

It returns only one instance of any value present more than once.

1. >>> a={1,2,2,3}
2. >>> a

{1, 2, 3}

However, a set is unordered, so it doesn’t support indexing.


1. >>> a[2]

Traceback (most recent call last):


File “<pyshell#127>”, line 1, in <module>
a[2]
TypeError: ‘set’ object does not support indexing

Also, it is mutable. You can change its elements or add more. Use the add() and remove() methods to do so.

1. >>> a={1,2,3,4}
2. >>> a

{1, 2, 3, 4}

1. >>> a.remove(4)
2. >>> a

{1, 2, 3}

1. >>> a.add(4)
2. >>> a

{1, 2, 3, 4}

4. Type Conversion

Since Python is dynamically-typed, you may want to convert a value into another type. Python supports a list of
functions for the same.

a. int()
It converts the value into an int.

1. >>> int(3.7)

3
Notice how it truncated 0.7 instead of rounding the number off to 4. You can also turn a Boolean into an int.

1. >>> int(True)

1. >>> int(False)

However, you cannot turn a string into an int. It throws an error.

1. >>> int("a")

Traceback (most recent call last):


File “<pyshell#135>”, line 1, in <module>;
int(“a”)
ValueError: invalid literal for int() with base 10: ‘a’
However, if the string has only numbers, then you can.

1. >>> int("77")

77

b. float()
It converts the value into a float.

1. >>> float(7)

7.0

1. >>> float(7.7)

7.7

1. >>> float(True)

1.0

1. >>> float("11")

You can also use ‘e’ to denote an exponential number.


11.0

1. >>> float("2.1e-2")

0.021

1. >>> float(2.1e-2)

0.021
However, this number works even without the float() function.

1. >>> 2.1e-2

0.021

c. str()
It converts the value into a string.

1. >>> str(2.1)

‘2.1’

1. >>> str(7)
‘7’

1. >>> str(True)

‘True’

You can also convert a list, a tuple, a set, or a dictionary into a string.

1. >>> str([1,2,3])

‘[1, 2, 3]’

d. bool()
It converts the value into a boolean.

1. >>> bool(3)

True

1. >>> bool(0)

False

1. >>> bool(True)

True

1. >>> bool(0.1)

True

You can convert a list into a Boolean.

1. >>> bool([1,2])

True

The function returns False for empty constructs.

1. >>> bool()

False

1. >>> bool([])

False

1. >>> bool({})

False
None is a keyword in Python that represents an absence of value.

1. >>> bool(None)

False

e. set()
It converts the value into a set.

1. >>> set([1,2,2,3])

{1, 2, 3}

1. >>> set({1,2,2,3})

{1, 2, 3}

f. list()
It converts the value into a list.

1. >>> del list


2. >>> list("123")

[‘1’, ‘2’, ‘3’]

1. >>> list({1,2,2,3})

[1, 2, 3]

1. >>> list({"a":1,"b":2})

[‘a’, ‘b’]

However, the following raises an error.

1. >>> list({a:1,b:2})

Traceback (most recent call last):


File “<pyshell#173>”, line 1, in <module>;
list({a:1,b:2})
TypeError: unhashable type: ‘set’

g. tuple()
It converts the value into a tuple.

1. >>> tuple({1,2,2,3})

(1, 2, 3)
You can try your own combinations. Also try composite functions.

1. >>> tuple(list(set([1,2])))

(1, 2)

5. Python Local and Python Global Variables

Another classification of python variables is based on scope.

a. Python Local variables


When you declare a variable in a function, class, or so, it is only visible in that scope. If you call it outside of
that scope, you get an ‘undefined’ error.

1. >>> def func1():


2. uvw=2
3. print(uvw)
4. >>> func1()

1. >>> uvw

Traceback (most recent call last):


File “<pyshell#76>”, line 1, in <module>
uvw
NameError: name ‘uvw’ is not defined[/php]

Here, the variable uvw is local to function func1().

b. Global variables
When you declare a variable outside any context/scope, it is visible in the whole program.

1. >>> xyz=3
2. >>> def func2():
3. xyz=0
4. xyz+=1
5. print(xyz)
6. >>> func2()

1. >>> xyz

3
You can use the ‘global’ keyword when you want to treat a variable as global in a local scope.
1. >>> foo=1
2. >>> def func2():
3. global foo
4. foo=3
5. print(foo)
6. >>> func2()

1. >>> foo

This was all about the Python Variables and Python data type tutorial

6. Python Variables and Python Data Types – Conclusion

In this tutorial on Python Variables and Python Data Types, we learnt about different python variables and data
types with examples. We looked at the naming rules, and defining and deleting them. Then we saw different
types of data- numbers, strings, lists, dictionaries, tuples, sets, and many more. We also learnt how to convert
one variable type into another and local and global variables in python. Don’t forget to try out your own
combinations.

If you have any queries the Python Variables and Python Data Type Tutorial, Please drop a comment.

Python Variable Scope – Local, Global, Built-in, Enclosed


by DataFlair Team · November 15, 2018

1. Objective

In our previous article on Python Namespace and Scope. Today, we will discuss Python Variable Scope. Here,
we will learn different types of variable scope in Python, Python Global Keyword and Python Non-local
keywords.

So, let’s start the Python Variable Scope.


Python Variable Scope – Introduction

2. What is Python Variable Scope?

The scope of a variable in python is that part of the code where it is visible. Actually, to refer to it, you don’t
need to use any prefixes then. Let’s take an example, but before let’s revise python Syntax.

1. >>> b=8
2. >>> def func():
3. a=7
4. print(a)
5. print(b)
6. >>> func()

7
8

1. >>> a

Traceback (most recent call last):

File “<pyshell#6>”, line 1, in <module>

NameError: name ‘a’ is not defined


Also, the duration for which a variable is alive is called its ‘lifetime’.

3. Types of Python Variable Scope

There are 4 types of Variable Scope in Python, let’s discuss them one by one:
Python Variable Scope – Types

a. Local Scope
In the above code, we define a variable ‘a’ in a function ‘func’. So, ‘a’ is local to ‘func’. Hence, we can
read/write it in func, but not outside it. When we try to do so, it raises a NameError.
Look at this code.

1. >>> a=0
2. >>> def func():
3. print(a)
4. a=1
5. print(a)
6. >>> func()

Traceback (most recent call last):

File “<pyshell#79>”, line 1, in <module>

func()

File “<pyshell#78>”, line 2, in func

print(a)

UnboundLocalError: local variable ‘a’ referenced before assignment


Here, we could’ve accessed the global Scope ‘a’ inside ‘func’, but since we also define a local ‘a’ in it, it no
longer accesses the global ‘a’. Then, the first print statement inside ‘func’ throws an error in Python, because
we’re trying to access the local scope ‘a’ before assigning it. However, it is bad practice to try to manipulate
global values from inside local scopes. Try to pass it as a parameter to the function.

1. >>> def func(a=0):


2. a+=1
3. print(a)
4. >>> func()

1
Let’s discuss Error and Exception Handling in Python.

b. Global Scope
We also declare a variable ‘b’ outside any other python Variable scope, this makes it global scope.
Consequently, we can read it anywhere in the program. Later in this article, we will see how to write it inside
func.

c. Enclosing Scope
Let’s take another example.

1. >>> def red():


2. a=1
3. def blue():
4. b=2
5. print(a)
6. print(b)
7. blue()
8. print(a)
9. >>> red()

1
2
1
In this code, ‘b’ has local scope in Python function ‘blue’, and ‘a’ has nonlocal scope in ‘blue’. Of course, a
python variable scope that isn’t global or local is nonlocal. This is also called enclosing scope.

d. Built-in Scope
Finally, we talk about the widest scope. The built-in scope has all the names that are loaded into python variable
scope when we start the interpreter. For example, we never need to import any module to access functions like
print() and id().
Now that we’ve discussed different types of python variable scopes, let’s see how to deal with them.

3. Global Keyword in Python

So far, we haven’t had any kind of a problem with global scope. So let’s take an example.

1. >>> a=1
2. >>> def counter():
3. a=2
4. print(a)
5. >>> counter()
2
Now, when we make a reference to ‘a’ outside this function, we get 1 instead of 2.

1. >>> a

1
Why does this happen? Well, this is because when we set ‘a’ to 2, it created a local variable ‘a’ in the local
scope of ‘counter’. This didn’t change anything for the global ‘a’. Now, what if you wanted to change the global
version of ‘a’? We use the ‘global’ keyword in python for this.

1. >>> a=1
2. >>> def counter():
3. global
a 4. a=2
5. print(a)
6. >>> counter()

1. >>> a

2
What we do here is, we declare that the ‘a’ we’re going to use in this function is from the global scope. After
this, whenever we make a reference to ‘a’ inside ‘counter’, the interpreter knows we’re talking about the global
‘a’. In this example, it changed the value of the global ‘a’ to 2.

4. Nonlocal Keyword

Like the ‘global’ keyword, you want to make a change to a nonlocal variable, you must use the ‘nonlocal’
keyword. Let’s first try this without the keyword.

1. >>> def red():


2. a=1
3. def blue():
4. a=2
5. b=2
6. print(a)
7. print(b)
8. blue()
9. print(a)
10. >>> red()

2
2
1
As you can see, this did not change the value of ‘a’ outside function ‘blue’. To be able to do that, we use
‘nonlocal’.

1. >>> def red():


2. a=1
3. def blue():
4. nonlocal a
5. a=2
6. b=2
7. print(a)
8. print(b)
9. blue()
10. print(a)
11. >>> red()

2
2
2
See? This works perfectly fine.

So, this was all about Python Variable Scope. Hope you like our explanation.

5. Conclusion

Hence, we hope that the Python variable scope is clearer to you. We saw four types of scope- local scope,
enclosed scope, global scope, and built-in scope. We also saw two keywords- ‘global’ and ‘nonlocal’. Hope you
had fun, see you again. Tell us in a comment box, is this information is worth or not.

Identifiers in Python – Naming Rules & Best Practices


by DataFlair Team · November 19, 2018

1. Objective
In our last Python tutorial, we discussed Python Subprocess Module. Today, in this Python tutorial, we will
learn about identifiers in Python and how to name them. Moreover, we will see the rules, best practices,
reserved classes in Python Identifiers. Also, we will test the validity of identifiers in Python.
So, let’s start Identifiers in Python.
Identifiers in Python – Naming Rules & Best Practices

2. Ways to Define Identifiers in Python


We can define identifiers in Python in a few ways:
“An identifier is a user-defined name to represent a variable, a function, a class, a module, or any other object.”
“It is a programmable entity in Python- one with a name.”
“It is a name given to the fundamental building blocks in a program.”
You must read about Python Tuples

3. Python Identifier Naming Rules

a. Rules in Identifiers in Python


So we know what a Python Identifier is. But can we name it anything? Or do certain rules apply? Well, we do
have five rules to follow when naming identifiers in Python:
a. A Python identifier can be a combination of lowercase/ uppercase letters, digits, or an underscore. The
following characters are valid:

 Lowercase letters (a to z)
 Uppercase letters (A to Z)
 Digits (0 to 9)
 Underscore (_)

Have a look at Python Number Types


Some valid names are:

 myVar
 var_3
 this_works_too

b. An identifier cannot begin with a digit.


Some valid names:

 _9lives
 lives9

An invalid name:

 9lives

Identifiers in Python – Naming Rules

c. We cannot use special symbols in the identifier name. Some of these are:
!
@
#
$
%
.

Identifiers in Python – Naming Rules in Python

d. We cannot use a keyword as an identifier. Keywords are reserved names in Python and using one of those as
a name for an identifier will result in a SyntaxError.

Identifiers in Python – Identifiers Naming Rules

Naming Rules in Python Identifiers

e. An identifier can be as long as you want. According to the docs, you can have an identifier of infinite length.
However, the PEP-8 standard sets a rule that you should limit all lines to a maximum of 79 characters.
Do you know about Python Variables
b. Lexical Definitions in Python Identifiers
To sum those rules up lexically, we can say:
identifier ::= (letter | “_”) (letter | digit | “_”)* #It has to begin with a letter or an underscore; letters, digits,
or/and underscores may follow
letter ::= lowercase | uppercase #Anything from a-z and from A-Z
lowercase ::= “a” … “z” #Lowercase letters a to z
uppercase ::= “A” … “Z” #Uppercase letters A to Z
digit ::= “0” … “9” #Integers 0 to 9

4. Best Practices in Identifiers in Python


While it’s mandatory to follow the rules, it is also good to follow some recommended practices:

 Begin class names with an uppercase letter, begin all other identifiers with a lowercase letter
 Begin private identifiers with an underscore (_); Note that this doesn’t make a variable private, but
discourages the user from attempting to access it
 Put around names of magic methods (use leading and trailing double underscores), avoid doing this
to anything else. Also, built-in types already use this notation.
 Use leading double underscores only when dealing with mangling.

Let’s discuss Python Iterator

 Prefer using names longer than one character- index=1 is better than i=1
 Use underscores to combine words in an identifier, like in this_is_an_identifier
 Since Python is case-sensitive, name and Name are two different identifiers.
 Use camel case for naming. Let’s just clear the air here by saying camel case is myVarOne and
Pascal case is MyVarOne.

5. Testing the Validity of Identifiers in Python


While it is great to follow the rules and guidelines, we can test an identifier’s validity just to be sure. For this,
we make use of the keyword.iskeyword() function.
Have a look at Python Network Programming
The keyword module lets us determine whether a string is a keyword. It has two functions:

 keyword.iskeyword(s)- If s is a Python keyword, return true


 Keyword.kwlist- Return a sequence holding all keywords the interpreter understands. This includes
even those that are active only when certain future statements are in effect.

Coming back to iskeyword(s), it returns True if the string s is a reserved keyword. Else, it returns False. Let’s
import this module.

1. >>> import keyword


2. >>> keyword.iskeyword('_$$_')

False

1. >>> keyword.iskeyword('return')
True
Also, the str.isidentifier() function will tell us if a string is a valid identifier. This is available since Python 3.0.

1. >>> ' $$ '.isidentifier()

False

1. >>> ' 99 '.isidentifier()

True

1. >>> '9lives'.isidentifier()

False

1. >>> '9.5okay'.isidentifier()

False
Let’s discuss Python Flask

6. Reserved Classes of Python Identifiers


Finally, let us talk about classes of identifiers. Some classes have special meanings and to identify them, we use
patterns of leading and trailing underscores:

a. Single Leading Underscore (_*)


We use this identifier to store the result of the last evaluation in the interactive interpreter. This result is stored
in the builtin module. Importing a module as from module import * does not import such private variables.

b. Leading and Trailing Double Underscores ( * )


These are system-defined names (by the interpreter). A class can implement operations to be invoked by
special syntax using methods with special names. Consider this an attempt at operator overloading in a Pythonic
fashion. One such special/ magic method is getitem (). Then, x[i] is equivalent to x. getitem (i). In the
near future, the set of names of this class by Python may be extended.
Have a look at Python SciPy Tutorial

c. Leading Double Underscores ( *)


These are class-private names. Within a class definition, the interpreter rewrites (mangles) such a name to avoid
name clashes between the private attributes of base and derived classes.
So, this was all in Identifiers in Python tutorial. Hope you like our explanation.

7. Conclusion – Identifiers in Python


Hence, in this Python Identifiers, we discussed the meaning of Identifiers in Python. Moreover, we learned
naming rules and best practices in Python Identifiers. Also, we discussed reserved classes in Python Identifier.
Still, if you have any doubt, ask in the comment tab.

Python String Tutorial – Python String Functions & Operations


by DataFlair Team · November 15, 2018

1. Python String Tutorial

In this Python String tutorial, we will discuss what is Python string and its examples. Moreover, we will learn how to
declare and slice a string in python and also look at the Python String functions and Python String operations. At last, we
cover escape sequences in Python. As we saw earlier, you don’t need to mention the data type when declaring a string.

So, let’s start the Python String Tutorial.

Python Strings Tutorial – Functions and Operations

2. What is Python String?

A Python string is a sequence of characters. There is a built-in class ‘str’ for handling Python string. You can
prove this with the type() function.

1. >>> type('Dogs are love')

<class ‘str’>

Python doesn’t have the char data-type like C++ or Java does.
3. How to Declare Python String?

You can declare a Python string using either single quotes or double quotes.

1. >>> a='Dogs are love'


2. >>> print(a)

Dogs are love

1. >>> a="Dogs are love"


2. >>> print(a)

Dogs are love

However, you cannot use a single quote to begin a string and a double quote to end it, and vice-versa.

1. >>> a='Dogs are love"

SyntaxError: EOL while scanning string literal

4. How to Use Quotes inside Python String?

Since we delimit strings using quotes, there are some things you need to take care of when using them inside a
string.

1. >>> a="Dogs are "love""

SyntaxError: invalid syntax

If you need to use double quotes inside a Python string, delimit the string with single quotes.

1. >>> a='Dogs are "love"'


2. >>> print(a)

Dogs are “love”

And if you need to use single quotes inside a string, delimit it with double quotes.

1. >>> a="Dogs are 'love'"


2. >>> print(a)

Dogs are ‘love’

You can use as many quotes as you want, then.

1. >>> a="'Dogs' 'are' 'love'"


2. >> print(a)

‘Dogs’ ‘are’ ‘love’

Also refer to section 9 for how to escape quotes.


Learn: Python Namespace and Variable Scope – Local and Global Variables

5. Spanning a String Across Lines

When you want to span a Python string across multiple lines, you can use triple quotes.

1. <pre data-enlighter-language="php" class="EnlighterJSRAW">>>> a="""Hello


2. Welcome"""
3. >>> print(a)</pre>

Hello
Welcome
It preserves newlines too, unlike using a backward slash for the same.

1. >>> a="Hello\
2. Welcome"
3. >>> print(a)

Hello Welcome

6. How to Access Python String?

A string is immutable; it can’t be changed.

1. >>> a="Dogs"
2. >>> a[0]="H"

Traceback (most recent call last):

File “<pyshell#22>”, line 1, in <module>

a[0]=”H”

TypeError: ‘str’ object does not support item assignment

But you can access a string.

1. >>> a="Dogs are love"


2. >>> a

‘Dogs are love’

1. >>> print(a)

Dogs are love

i. Displaying a single character


To display a single character from a string, put its index in square brackets. Indexing
begins at 0.
1. >>> a[1]

‘o’

b. Slicing a string
Sometimes, you may want to display only a part of a string. For this, use the slicing
operator [].

1. >>> a[3:8]

‘s are’

Here, it printed characters 3 to 7, with the indexing beginning at 0.

1. >>> a[:8]

‘Dogs are’

This prints characters from the beginning to character 7.

1. >>> a[8:]

‘ love’

This prints characters from character 8 to the end of the string.

1. >>> a[:]

‘Dogs are love’

This prints the whole string.

1. >>> a[:-2]

‘Dogs are lo’

This prints characters from the beginning to two characters less than the end of the string.

1. >>> a[-2:]

‘ve’

This prints characters from two characters from the end to the end of the string.

1. >>> a[-3:-2]

‘o’

This prints characters from three characters from the string’s end to two characters from it.
The following codes return empty strings.

1. >>> a[-2:-2]

1. >>> a[2:2]

7. Python String Concatenation

Concatenation is the operation of joining stuff together. Python Strings can join using the concatenation
operator +.

1. >>> a='Do you see this, '


2. >>> b='$$?'
3. >>> a+b

‘Do you see this, $$?’

Let’s take another example.

1. >>> a='10'
2. >>> print(2*a)

1010

Multiplying ‘a’ by 2 returned 1010, and not 20, because ‘10’ is a string, not a number. You cannot concatenate a
string to a number.

1. >>> '10'+10

Traceback (most recent call last):

File “<pyshell#49>”, line 1, in <module>

’10’+10

TypeError: must be str, not int

8. Python String Formatters

Sometimes, you may want to print variables along with a string. You can either use commas, or use string
formatters for the same.

1. >>> city='Ahmedabad'
2. >>> print("Age",21,"City",city)

Age 21 City Ahmedabad


i. f-strings
The letter ‘f’ precedes the string, and the variables are mentioned in curly braces in their
places.

1. >>> name='Ayushi'
2. >>> print(f"It isn't {name}'s birthday")

It isn’t Ayushi’s birthday

Notice that because we wanted to use two single quotes in the string, we delimited the entire string with double
quotes instead.

ii. format() method


You can use the format() method to do the same. It succeeds the string, and has the
variables as arguments separated by commas. In the string, use curly braces to posit
the variables. Inside the curly braces, you can either put 0,1,.. or the variables. When
doing the latter, you must assign values to them in the format method.

1. >>> print("I love {0}".format(a))

I love dogs

1. >>> print("I love {a}".format(a='cats'))

I love cats

The variables don’t have to defined before the print statement.

1. >>> print("I love {b}".format(b='ferrets'))

I love ferrets

c. % operator
The % operator goes where the variables go in a string. %s is for string. What follows the
string is the operator and variables in parentheses/in a tuple.

1. >>> b='ferrets'
2. >>> print("I love %s and %s" %(a,b))

I love dogs and cats

Other options include:

%d – for integers

%f – for floating-point numbers


9. Escape Sequences in Python

In a Python string, you may want to put a tab, a linefeed, or other such things. Escape sequences allow us to do
this. An escape sequence is a backslash followed by a character, depending on what you want to do. Python
supports the following sequences.

 \n – linefeed
 \t – tab

1. >>> print("hell\to")

hell o

 \\ – backslash

Since a backslash may be a part of an escape sequence, so, a backslash must be escaped by a backslash too.

 \’ – A single quote can be escaped by a backslash. This lets you use single quotes freely in a string.
 \” – Like the single quote, the double quote can be escaped too.

Any Doubt yet in Python String and Python String Operations and Functions? Please Comment.

10. Python String Functions

Python provides us with a number of functions that we can apply on strings or to create strings.

a. len()
The len() function returns the length of a string.

1. >>> a='book'
2. >>> len(a)

You can also use it to find how long a slice of the string is.

1. >>> len(a[2:])

b. str()
This function converts any data type into a string.

1. >>> str(2+3j)

‘(2+3j)’

1. >>> str(['red','green','blue'])
“[‘red’, ‘green’, ‘blue’]”

c. lower() and upper()


These methods return the string in lowercase and uppercase, respectively.

1. >>> a='Book'
2. >>> a.lower()

‘book’

1. >>> a.upper()

‘BOOK’

d. strip()
It removes whitespaces from the beginning and end of the string.

1. >>> a=' Book '


2. >>> a.strip()

‘Book’

e. isdigit()
Returns True if all characters in a string are digits.

1. >>> a='777'
2. >> a.isdigit()

True

1. >>> a='77a'
2. >>> a.isdigit()

False

f. isalpha()
Returns True if all characters in a string are characters from an alphabet.

1. >>> a='abc'
2. >>> a.isalpha()

True

1. >>> a='ab7'
2. >>> a.isalpha()
False

g. isspace()
Returns True if all characters in a string are spaces.

1. >>> a=' '


2. >>> a.isspace()

True

1. >>> a=' \' '


2. >>> a.isspace()

False

h. startswith()
It takes a string as an argument, and returns True is the string it is applied on begins
with the string in the argument.

1. >>> a.startswith('un')

True

i. endswith()
It takes a string as an argument, and returns True if the string it is applied on ends with
the string in the argument.

1. >>> a='therefore'
2. >>> a.endswith('fore')

True

j. find()
It takes an argument and searches for it in the string on which it is applied. It then
returns the index of the substring.

1. >>> 'homeowner'.find('meow')

If the string doesn’t exist in the main string, then the index it returns is 1.

1. >>> 'homeowner'.find('wow')
-1
k. replace()
It takes two arguments. The first is the substring to be replaced. The second is the
substring to replace with.

1. >>> 'banana'.replace('na','ha')

‘bahaha’

l. split()
It takes one argument. The string is then split around every occurrence of the argument
in the string.

1. >>> 'No. Okay. Why?'.split('.')

[‘No’, ‘ Okay’, ‘ Why?’]

m. join()
It takes a list as an argument and joins the elements in the list using the string it is
applied on.

1. >>> "*".join(['red','green','blue'])

‘red*green*blue’

11. Python String Operations

Python String Operations

a. Comparison
Python Strings can compare using the relational operators.
1. >>> 'hey'<'hi'

True

‘hey’ is lesser than ‘hi lexicographically (because i comes after e in the dictionary)

1. >>> a='check'
2. >>> a=='check'

True

1. >>> 'yes'!='no'

True

b. Arithmetic
Some arithmetic operations can be applied on strings.

1. >>> 'ba'+'na'*2

‘banana’

c. Membership
The membership operators of Python can be used to check if string is a substring to another.

1. >>> 'na' in 'banana'

True

1. >>> 'less' not in 'helpless'

False

d. Identity
Python’s identity operators ‘is’ and ‘is not’ can be used on strings.

1. >>> 'Hey' is 'Hi'

False

1. >>> 'Yo' is not 'yo'

True

e. Logical
Python’s and, or, and not operators can be applied too. An empty string has a Boolean value of False.
1. and- If the value on the left is True it returns the value on the right. Otherwise, the
value on the left is False, it returns False.

1. >>> '' and '1'

1. >>> '1' and ''


2. or- If the value on the left is True, it returns True. Otherwise, the value on the right
is returned.

3. not- As we said earlier, an empty string has a Boolean value of False.

1. >>> not('1')

False

1. >>> not('')

True

This was all about the tutorial on Python strings. Hope you like the Python strings tutorial.

12. Python String – Conclusion

In this Python String tutorial, we learned about python string with string functions and Operators , and how to
declare and access them. Then we learned about python string concatenation and formatters in python. We also
learned about Python string functions. Lastly, we looked at operations that we can perform on strings. Hope you
enjoyed today’s lesson. Leave your feedback in the comments if any query on Python strings.

What is Python Interpreter – Environment, Invoking & Working


by DataFlair Team · May 17, 2019

1. Python Interpreter Tutorial – Objective


In our previous tutorial, we studied Python Matplotlib Tutorial. In this Python Interpreter tutorial, we will see
how to work with the Python interpreter. Moreover, we will discuss the environment and features of Interpreter
of Python Programming Langauge.
So, let’s start the Python Interpreter Tutorial.
What is Python Interpreter – Environment, Invoking & Working

2. Python Interpreter & its Environment (Source Code Encoding)

The default encoding for a Python source file is UTF-8. This is a Unicode Standard variable-width character
encoding; it can encode 1,112,064 valid code points in Unicode using up to four 8-bit bytes. Using this
encoding, we can use characters of most languages – we can use these in string literals, comments, and
identifiers. Since the standard library makes use of ASCII characters only, you must declare the use of this
encoding to your editor. This is to ensure that all such characters display without a problem. The font should be
such that supports all characters in the file.
We add this comment as the first line of the file we want to use it in-

1. # -*- coding: encoding -*-

In this, encoding is a valid codec that Python supports. Similarly, when you want to use the Windows-1252
encoding, you can use this as the first line of code:

1. # -*- coding: cp1252 -*-

However, when you want to begin code with a UNIX shebang line, you can put the comment for encoding
second-

1. #!/usr/bin/env python3
2. # -*- coding: cp1252 -*-

Do you know about Python Iterables and Python Itertools

3. How to Invoke the Python Interpreter?


On your machine, you can find your interpreter at an address like:
C:\Python36
Or it may reside on the location you selected at the time of installation. Add path using this command:
1. set path=%path%;C:\python36

How to Invoke the Python Interpreter?

a. Start the Python Interpreter


On Windows, when you want to run the Python interpreter in the shell, you can type the following:

1. $python

To get out of the interpreter in disassembling the Bytecode shell, you can type:

1. >>> quit()

Bye
Let’s Discuss Python Multiple Inheritance – Python MRO (Method Resolution Order)
Alternately, you can use an end-of-file character at the prompt. Python interpreter exits with a zero exit status.
You can use it in a REPL (Read-Evaluate-Print-Loop) fashion. But if you want, you can save your Python code
as a script and execute it using the interpreter:

1. $python demo.py

To enter interactive mode after running a script, you can pass –i before the script.
The command python -c command [arg] … executes statements in command, and python -m module [arg] …
executes the source file for module.

b. Features of Python Interpreter


Python interpreter offers some pretty cool features:

 Interactive editing
 History substitution
 Code completion on systems with support for readline

In the first Python prompt, try pressing the following keys:


Ctrl+P
This tells you if your interpreter supports command-line editing. A beep indicates that it does support
command-line editing. Otherwise, it will either perform a no-operation or echo ^p to indicate it isn’t available.

c. Passing Arguments
When you pass a script name and additional arguments to the shell when invoking the Python interpreter, it
turns these into a list of strings. Then, it assigns these to the variable argv in the sys module. The following
command will give us a list of this-

1. import sys

Without a script or arguments, sys.argv[0] denotes an empty string. A script name of ‘-‘ means that it sets
sys.argv[0] to ‘-‘, and with ‘-c’, it is set to ‘-c’. A value of ’-m’ sets sys.argv[0] to the module’s full name. The
command/ module handles the options after ‘-c’ or ‘-m’.
Do you know about Python Collections Module?

d. Interactive Mode
Python interpreter is in an interactive mode when it reads commands from a tty. The primary prompt is the
following:

1. >>>

When it shows this prompt, it means it prompts the developer for the next command. This is the REPL. Before
it prints the first prompt, Python interpreter prints a welcome message that also states its version number and a
copyright notice.
This is the secondary prompt:

1. …

This prompt denotes continuation lines.

1. $ python3.7
2. Python 3.7 (default, Jul 16 2018, 04:38:07)
3. [GCC 4.8.2] on Windows
4. Type "help", "copyright", "credits" or "license" for more
information. 5. >>>

You will find continuation lines when working with a multi-line construct:

1. >>> it_rains =True


2. >>> if it_rains:
3. print("The produce will be good")

The produce will be good


You can also use the Python interpreter as a calculator:
1. >>> 2*7
2. 14
3. >>> 4/2
4. 2.0

4. How Does Python Interpreter Works?

Well, internally, four things happen in a REPL:


i. Lexing- The lexer breaks the line of code into tokens.
ii. Parsing- The parser uses these tokens to generate a structure, here, an Abstract Syntax Tree, to depict the
relationship between these tokens.
iii. Compiling- The compiler turns this AST into code object(s).
iv. Interpreting- The interpreter executes each code object.
Have a look at Python Operators with Syntax and Examples

How Does Python Interpreter Works?

a. Function Objects & Code Objects


When we talk of function objects, we mean to say that in Python, functions are first-class objects (functions
indeed are objects). You can pass them around and talk about them without making a call to them.

1. >>> def bar(a):


2. x=3
3. return x+a
4. >>> bar

<function bar at 0x107ef7aa2>

Now bar. code returns a code object:


1. >>> bar. code
2. <code object bar at 0x107eeccb2, file "<stdin>", line 1>

So, we conclude that a code object is an attribute of a function object. The dir() function will tell us more about
the function:

1. >>> dir(bar. code )

[‘ class ’, ‘ cmp ’, ‘ delattr ’, ‘ doc__’, ‘ eq ’, ‘ format ’, ‘ ge ’,


‘ getattribute ’, ‘ gt ’, ‘ hash ’, ‘ init ’, ‘ le ’, ‘ lt ’, ‘ ne ’, ‘ new ’,
‘ reduce ’, ‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ sizeof ’, ‘ str ’,
‘ subclasshook ’, ‘co_argcount’, ‘co_cellvars’, ‘co_code’, ‘co_consts’, ‘co_filename’,
‘co_firstlineno’, ‘co_flags’, ‘co_freevars’, ‘co_lnotab’, ‘co_name’, ‘co_names’, ‘co_nlocals’,
‘co_stacksize’, ‘co_varnames’]
Let’s revise Recursion in Python with Examples
This gives us the attributes of the code object. Values of some more attributes:

1. >>> bar. code .co_varnames

(‘a’, ’x’)

1. >>> bar. code .co_consts

(None, 3)

1. >>> bar. code .co_argcount

b. The Bytecode
The following command gives us the bytecode:

1. >>> bar. code .co_code

‘d\x01\x00}\x01\x00|\x01\x00|\x00\x00\x17S’

This is a series of bytes, each of which the interpreter loops through and then makes an execution.

c. Disassembling the Bytecode


We will use the dis() method from the dis module to understand what’s going on- this isn’t part of what the
interpreter does.

1. >>>import dis
2. >>> dis.dis(bar. code__)

2 0 LOAD_CONST 1 (3)
3 STORE_FAST 1 (x)
3 6 LOAD_FAST 1 (x)
9 LOAD_FAST 0 (a)
12BINARY_ADD
13RETURN_VALUE
In this, the first set of numbers is the line numbers in the actual code. The second one depicts offsets of the
bytecode. Then comes the set of names for the bytes- for human readability. The next column depicts the
arguments and the last column lists the constants and names in the fourth column.

1. >>> bar. code .co_consts[1]

1. >>> bar. code .co_varnames[1]

‘x’
So, this was all about Python Interpreter Tutorial. Hope you like our explanation.

5. Conclusion

Hence, we can say the compiler for Python generates bytecode for the interpreter. The Python interpreter uses
this with the virtual machine. The same bytecode doesn’t always end up doing the same things. This is another
thing that makes Python dynamic. Also, the default prompt for the interpreter is >>>. If we miss anything, feel
free to share with us in a comment section.

Python Operator – Types of Operators in Python


by DataFlair Team · September 24, 2019

Python Operator – Objective

In this Python Operator tutorial, we will discuss what is an operator in Python Programming Language. On the
other hand, we will learn different types of Python Operators: Arithmetic, Relational, Assignment, Logical,
Membership, Identity, and Bitwise Operators with their syntax and examples.

Python operator is a symbol that performs an operation on one or more operands. An operand is a variable or a
value on which we perform the operation.

Before starting with operators in python, let us revise the basics of Python.

So, let’s start the Python Operator Tutorial.


Python Operator – Types of Operators in Python

Introduction to Python Operator


Python Operator falls into 7 categories:

 Python Arithmetic Operator


 Python Relational Operator
 Python Assignment Operator
 Python Logical Operator
 Python Membership Operator
 Python Identity Operator
 Python Bitwise Operator

It is recommended to check the Python master guide before we start with the operator in Python.

Python Arithmetic Operator

These Python arithmetic operators include Python operators for basic mathematical operations.
Arithmetic Operators in Python

a. Addition(+)

Adds the values on either side of the operator.

1. >>> 3+4

Output: 7

b. Subtraction(-)

Subtracts the value on the right from the one on the left.

1. >>> 3-4

Output: -1

c. Multiplication(*)

Multiplies the values on either side of the operator.

1. >>> 3*4

Output: 12

d. Division(/)

Divides the value on the left by the one on the right. Notice that division results in a floating-point value.

1. >>> 3/4
Output: 0.75

e. Exponentiation(**)

Raises the first number to the power of the second.

1. >>> 3**4

Output: 81

f. Floor Division(//)

Divides and returns the integer value of the quotient. It dumps the digits after the decimal.

1. >>> 3//4
2. >>> 4//3

Output: 1

1. >>> 10//3

Output: 3

g. Modulus(%)

Divides and returns the value of the remainder.

1. >>> 3%4

Output: 3

1. >>> 4%3

Output: 1

1. >>> 10%3

Output: 1

1. >>> 10.5%3

Output: 1.5

If you face any query in Python Operator with examples, ask us in the comment.

Python Relational Operator

Let’s see Python Relational Operator.


Relational Operators in Python

Relational Python Operator carries out the comparison between operands. They tell us whether an operand is
greater than the other, lesser, equal, or a combination of those.

a. Less than(<)

This operator checks if the value on the left of the operator is lesser than the one on the right.

1. >>> 3<4

Output: True

b. Greater than(>)

It checks if the value on the left of the operator is greater than the one on the right.

1. >>> 3>4

Output: False

c. Less than or equal to(<=)

It checks if the value on the left of the operator is lesser than or equal to the one on the right.

1. >>> 7<=7

Output: True

d. Greater than or equal to(>=)

It checks if the value on the left of the operator is greater than or equal to the one on the right.
1. >>> 0>=0

Output: True

e. Equal to(= =)

This operator checks if the value on the left of the operator is equal to the one on the right. 1 is equal to the
Boolean value True, but 2 isn’t. Also, 0 is equal to False.

1. >>> 3==3.0

Output: True

1. >>> 1==True

Output: True

1. >>> 7==True

Output: False

1. >>> 0==False

Output: True

1. >>> 0.5==True

Output: False

f. Not equal to(!=)

It checks if the value on the left of the operator is not equal to the one on the right. The Python operator <> does
the same job, but has been abandoned in Python 3.

When the condition for a relative operator is fulfilled, it returns True. Otherwise, it returns False. You can use
this return value in a further statement or expression.

1. >>> 1!=-1.0

Output: False

1. >>> -1<>-1.0

#This causes a syntax error

Python Assignment Operator


Assignment Operators in Python

Assignment Python Operator explained –

An assignment operator assigns a value to a variable. It may manipulate the value by a factor before assigning
it. We have 8 assignment operators- one plain, and seven for the 7 arithmetic python operators.

a. Assign(=)

Assigns a value to the expression on the left. Notice that = = is used for comparing, but = is used for assigning.

1. >>> a=7
2. >>> print(a)

Output: 7

b. Add and Assign(+=)

Adds the values on either side and assigns it to the expression on the left. a+=10 is the same as a=a+10.

The same goes for all the next assignment operators.

1. >>> a+=2
2. >>> print(a)

Output: 9

c. Subtract and Assign(-=)

Subtracts the value on the right from the value on the left. Then it assigns it to the expression on the left.

1. >>> a-=2
2. >>> print(a)

Output: 7

d. Divide and Assign(/=)

Divides the value on the left by the one on the right. Then it assigns it to the expression on the left.

1. >>> a/=7
2. >>> print(a)

Output: 1.0

e. Multiply and Assign(*=)

Multiplies the values on either sides. Then it assigns it to the expression on the left.

1. >>> a*=8
2. >>> print(a)

Output: 8.0

f. Modulus and Assign(%=)

Performs modulus on the values on either side. Then it assigns it to the expression on the left.

1. >>> a%=3
2. >>> print(a)

Output: 2.0

g. Exponent and Assign(**=)

Performs exponentiation on the values on either side. Then assigns it to the expression on the left.

1. >>> a**=5
2. >>> print(a)

Output: 32.0

h. Floor-Divide and Assign(//=)

Performs floor-division on the values on either side. Then assigns it to the expression on the left.

1. >>> a//=3
2. >>> print(a)

Output: 10.0

This is one of the important Python Operator.


Python Logical Operator

These are conjunctions that you can use to combine more than one condition. We have three Python logical
operator – and, or, and not that come under python operators.

Logical Operators in Python

a. and

If the conditions on both the sides of the operator are true, then the expression as a whole is true.

1. >>> a=7>7 and 2>-1


2. >>> print(a)

Output: False

b. or

The expression is false only if both the statements around the operator are false. Otherwise, it is true.

1. >>> a=7>7 or 2>-1


2. >>> print(a)

Output: True

‘and’ returns the first False value or the last value; ‘or’ returns the first True value or the last value

1. >>> 7 and 0 or 5

Output: 5

c. not
This inverts the Boolean value of an expression. It converts True to False, and False to True. As you can see
below, the Boolean value for 0 is False. So, not inverts it to True.

1. >>> a=not(0)
2. >>> print(a)

Output: True

Membership Python Operator

These operators test whether a value is a member of a sequence. The sequence may be a list, a string, or a
tuple. We have two membership python operators- ‘in’ and ‘not in’.

a. in

This checks if a value is a member of a sequence. In our example, we see that the string ‘fox’ does not belong to
the list pets. But the string ‘cat’ belongs to it, so it returns True. Also, the string ‘me’ is a substring to the string
‘disappointment’. Therefore, it returns true.

1. >>> pets=[‘dog’,’cat’,’ferret’]
2. >>> ‘fox’ in pets

Output: False

1. >>> ‘cat’ in pets

Output: True

1. >>> ‘me’ in ‘disappointment’

Output: True

b. not in

Unlike ‘in’, ‘not in’ checks if a value is not a member of a sequence.

1. >>> ‘pot’ not in ‘disappointment’

Output: True

In doubt yet in any Python operator with examples? Please comment.

Don’t you know about the trending Python Project at DataFlair? Here it is – Gender and Age Detection
with Python

Python Identity Operator


Let us proceed towards identity Python Operator.

These operators test if the two operands share an identity. We have two identity operators- ‘is’ and ‘is not’.
a. is

If two operands have the same identity, it returns True. Otherwise, it returns False. Here, 2 is not the same as
20, so it returns False. Also, ‘2’ and “2” are the same. The difference in quotes does not make them different.
So, it returns True.

1. >>> 2 is 20

Output: False

1. >>> ‘2’ is “2”

Output: True

b. is not

2 is a number, and ‘2’ is a string. So, it returns a True to that.

1. >>> 2 is not ‘2’

Output: True

Python Bitwise Operator


Let us now look at Bitwise Python Operator.

Bitwise Operators in Python

On the operands, these operate bit by bit.

a. Binary AND(&)

It performs bit by bit AND operation on the two values. Here, binary for 2 is 10, and that for 3 is 11. &-ing
them results in 10, which is binary for 2. Similarly, &-ing 011(3) and 100(4) results in 000(0).
1. >>> 2&3

Output: 2

1. >>> 3&4

Output: 0

b. Binary OR(|)

It performs bit by bit OR on the two values. Here, OR-ing 10(2) and 11(3) results in 11(3).

1. >>> 2|3

Output: 3

c. Binary XOR(^)

It performs bit by bit XOR(exclusive-OR) on the two values. Here, XOR-ing 10(2) and 11(3) results in 01(1).

1. >>> 2^3

Output: 1

d. Binary One’s Complement(~)

It returns the one’s complement of a number’s binary. It flips the bits. Binary for 2 is 00000010. Its one’s
complement is 11111101. This is binary for -3. So, this results in -3. Similarly, ~1 results in -2.

1. >>>~-3

Output: 2

Again, one’s complement of -3 is 2.

e. Binary Left-Shift(<<)

It shifts the value of the left operand the number of places to the left that the right operand specifies. Here,
binary of 2 is 10. 2<<2 shifts it two places to the left. This results in 1000, which is binary for 8.

1. >>> 2<<2

Output: 8

f. Binary Right-Shift(>>)

It shifts the value of the left operand the number of places to the right that the right operand specifies. Here,
binary of 3 is 11. 3>>2 shifts it two places to the right. This results in 00, which is binary for 0. Similarly, 3>>1
shifts it one place to the right. This results in 01, which is binary for 1.

1. >>> 3>>2
2. >>> 3>>1

Output: 1

This was all about the Python Operator Tutorial.

Conclusion – Python Operator

Finally, in this lesson, we looked at seven different classes of Python operator. We executed them in the Python
Shell(IDLE) to find out how they work. We can further use this operator in conditions, and to combine them.
Go ahead and practice some combinations.
Hope you like the Python Operator tutorial by DataFlair. If you have any queries on Python operator, please
leave a comment.

Python Bitwise Operators with Syntax and Example


by DataFlair Team · February 27, 2019

1. Python Bitwise Operators

Previously, in our tutorial on Python Operators., Today, in this Python Bitwise Operators Tutorial, we will
discuss Python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operators in
Python Programming. Along with this, we will discuss syntax and example of Python Bitwise Operators.

So, let’s start the Python Bitwise Operators Tutorial.

Python Bitwise Operators with Syntax and Example

Learn: Python Functions with Syntax and Examples

2. Introduction to Python Bitwise Operators


Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole. To take
an example, let’s see the ‘and’ and ‘&’ operators for the same thing.

Let’s take two numbers- 5 and 7. We’ll show you their binary equivalents using the function bin().

1. >>> bin(5)

‘0b101’

1. >>> bin(7)

‘0b111’

Now let’s try applying ‘and’ and ‘&’ to 5 and 7.

1. >>> 5 and 7

1. >>> 5&7

You would have expected them to return the same thing, but they’re not the same. One acts on the whole value,
and one acts on each bit at once.

Actually, ‘and’ sees the value on the left. If it has a True Boolean value, it returns whatever value is on the right.
Otherwise, it returns False. So, here, 5 and 7 is the same as True and 7. Hence, it returns 7. However, 5&7 is the
same as 101&111. This results in 101, which is binary for 5. Let’s look at each of these operators bit by bit (pun
intended).

Learn: Loops in Python with Syntax and Examples

Let’s move ahead with next Python Bitwise Operator

3. Python Bitwise AND (&) Operator

1 has a Boolean value of True, and 0 has that of False. Take a look at the following code.

1. >>> True/2

0.5

1. >>> False*2

This proves something. Now, the binary and (&) takes two values and performs an AND-ing on each pair of
bits. Let’s take an example.

1. >>> 4 & 8
Binary for 4 is 0100, and that for 8 is 1000. So when we AND the corresponding bits, it gives us 0000, which is
binary for 0. Hence, the output.

The following are the values when &-ing 0 and 1.

Table.1 Python Bitwise Operators – AND Operators

0&0 0
0&1 0
1&0 0
1&1 1

As you can see, an &-ing returns 1 only if both bits are 1.

You cannot, however, & strings.

1. >>> '$'&'%'

Traceback (most recent call last):

File “<pyshell#30>”, line 1, in <module>

‘$’&’%’

TypeError: unsupported operand type(s) for &: ‘str’ and ‘str’

Since Boolean values True and False have equivalent integer values of 1 and 0, we can & them.

1. >>> False&True

False

1. >>> True&True

True

Let’s try a few more combinations.

1. >>> 1&True

1. >>> 1.0&1.0

Traceback (most recent call last):

File “<pyshell#36>”, line 1, in <module>

1.0&1.0

TypeError: unsupported operand type(s) for &: ‘float’ and ‘float’


You can also type your numbers directly in binary, as we discussed in section 6a in our Python Numbers
tutorial.

1. >>> 0b110 & 0b101

Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.

4. Python Bitwise OR (|) Operators

Now lets discuss Python Bitwise OR (|) Operator

Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.

Table.2 Python Bitwise Operators – OR Operators

0|0 0
0|1 1
1|0 1
1|1 1

1. >>> 6|1

This is the same as the following.

1. >>> 0b110|0b001

Let’s see some more examples.

1. >>> True|False

True

Lets move to another Python Bitwise Operator

5. Python Bitwise XOR (^) Operator

XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.

Table.1 Python Bitwise Operators – XOR Operators

0^0 0
0^1 1
1^0 1
1^1 0
Let’s take a few examples.

1. >>> 6^6

Here, this is the same as 0b110^0b110. This results in 0b000, which is binary for 0.

1. >>> 6^0

This is equivalent to 0b110^0b000, which gives us 0b110. This is binary for 6.

1. >>> 6^3

Here, 0b110^0b011 gives us 0b101, which is binary for 5.

Now lets discuss Bitwise 1’s Complement (~)

6. Bitwise 1’s Complement (~)


This one is a bit different from what we’ve studied so far. This operator takes a number’s binary, and returns its
one’s complement. For this, it flips the bits until it reaches the first 0 from right. ~x is the same as -x-1.

1. >>> ~2

-3

1. >>> bin(2)

‘0b10’

1. >>> bin(-3)

‘-0b11’

To make it clear, we mention the binary values of both. Another example follows.

1. >>> ~45

-46

1. >>> bin(45)

‘0b101101’

1. >>> bin(-46)

‘-0b101110’
7. Python Bitwise Left-Shift Operator (<<)

Finally, we arrive at left-shift and right-shift operators. The left-shift operator shifts the bits of the number by
the specified number of places. This means it adds 0s to the empty least-significant places now. Let’s begin with
an unusual example.

1. >>> True<<2

Here, True has an equivalent integer value of 1. If we shift it by two places to the left, we get 100. This is binary
for 4.

Now let’s do it on integers.

1. >>> 2<<1

10 shifted by one place to the left gives us 100, which is, again, 4.

1. >>> 3<<2

12

Now, 11 shifted to the left by two places gives us 1100, which is binary for 12.

Now lets move to Next Python Bitwise Operator

8. Python Bitwise Right-Shift Operator (>>)


Now we’ll see the same thing for right-shift. It shifts the bits to the right by the specified number of places. This
means that those many bits are lost now.

1. >>> 3>>1

3 has a binary value of 11, which shifted one place to the right returns 1. But before closing on this tutorial,
we’ll take one last example.

Let’s check what’s the decimal value for 11111.

1. >>> int(0b11111)

31

Now, let’s shift it three places to the right.

1. >>> 31>>3
3

As you can see, it gives us 3, whose binary is 11. Makes sense, doesn’t it?

This was all about the Python Bitwise Operators.

9. Conclusion

While they’re not so common in real-world programming, Python Bitwise Operators do find their use in places
like encryption, compression, and byte manipulation. If you have any doubts regarding Python Bitwise
Operators, leave them in the comments.
Hope you like the Python Bitwise Operators.

Python Comparison Operators with Syntax and Examples


by DataFlair Team · November 21, 2018

1. Python Comparison Operators

In our previous article, we talked about Python bitwise operators. Today, we focus our words on Python
Comparison Operators. These are also called relational operators in Python. Along with this, we will learn
different types of Comparison Operators in Python: less than, greater than, less than, greater than, equal to, and
not equal to with their syntax and examples.

So, let’s start the Python Comparison Operators Tutorial.

Python Comparison Operators with Syntax and Examples

2. Python Comparison Operators


A comparison operator in python, also called python relational operator, compares the values of two operands
and returns True or False based on whether the condition is met. We have six of these, including and limited to-
less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. So, let’s begin
with the Python Comparison operators.

3. Python Less Than (<) Operator

The first comparison operator in python we’ll see here is the less than operator. Denoted by <, it checks if the
left value is lesser than that on the right.

1. >>> 3<6

Output: True

Since 3 is lesser than 6, it returns True.

1. >>> 3<3

Output: False

Because 3 is equal to 3, and not less than it, this returns False.

But let’s see if we can apply it to values other than ints.

1. >>> 3<3.0

Output: False

Here, 3 is an int, and 3.0 is a float, but 3 isn’t lesser than 3.0, or vice versa.

1. >>> 3.0<3

Output: False

Now, let’s try it on strings.

1. >>> 'Ayushi'<'ayushi'

Output: True

This one results in True because when comparing strings, their ASCII values are compared. The ASCII value
for ‘A’ is 65, but that for ‘a’ is 97. Hence, ‘A’ is lesser than ‘a’. Likewise, ‘Ayushi’ is lesser than ‘ayushi’.

But does it work with Python Booleans?

1. >>> 0.9999999<True

Output: True

Yes, it does. But what’s fascinating is that it works on containers like tuples as well. Let’s see some of these.
1. >>> (1,2,3)<(1,2,3,4)

Output: True

1. >>> (1,3,2)<(1,2,3)

Output: False

1. >>> (1,2,3)<(1,3,2)

Output: True

1. >>> ()<(0,)

Output: True

But you can’t compare tuples with different kinds of values.

1. >>> (1,2)<('One','Two')

Traceback (most recent call last):

File “<pyshell#84>”, line 1, in <module>

(1,2)<(‘One’,’Two’)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’

However, if you get comparable elements at the same indices, it is possible to compare two tuples.

Must Check – Python Tuples vs Lists

1. >>> (1,'one')<(2,'two')

Output: True

And when we say same indices, we mean it.

1. >>> (1,'one')<('two',2)

Traceback (most recent call last):

File “<pyshell#86>”, line 1, in <module>

(1,’one’)<(‘two’,2)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’

Finally, these work on lists, and set, but not dictionaries.

1. >>> [0]<[False]
Output: False

1. >>> {1,2,3}<{1,3,2}

Output: False

Here, because the other set rearranges itself to {1,2,3}, the two sets are equal. Consequently, it returns False.

1. >>> {1:'one',2:'two'}<{1:'three',2:'four'}

Traceback (most recent call last):

File “<pyshell#91>”, line 1, in <module>

{1:’one’,2:‘two‘}<{1:‘three‘,2:‘four‘}

TypeError: ‘<‘ not supported between instances of ‘dict‘ and ‘dict‘

If you face any doubt in Python Comparison Operators? Please Comment.

4. Python Greater Than (>) Operator

Let’s see the Greater than Python Comparison Operator

Now that we’ve seen which constructs we can apply these operators to, we will focus on the operators now on.
The greater than an operator, denoted by >, checks whether the left value is greater than the one on the right.

We recommend you to read Python Decision Making

1. >>> 0.5>False

Output: True

1. >>> 3,4,5>3,4,5.0

Output: (3, 4, True, 4, 5.0)

Hey, this created a tuple, when all we wanted to do was compare. This is because it took 5>3 as a value (True).
It put this as a value in the tuple. So let’s try to find our way around this.

1. >>> 3,4,5 > 3,4,5.0

Output: (3, 4, True, 4, 5.0)

So we see that spaces didn’t do it. Let’s try something else.

1. >>> 3,4,5>(3,4,5.0)

Traceback (most recent call last):

File “<pyshell#96>”, line 1, in <module>


3,4,5>(3,4,5.0)

TypeError: ‘>’ not supported between instances of ‘int’ and ‘tuple’

Hmm, we think we need to put parentheses around both tuples.

1. >>> (3,4,5)>(3,4,5.0)

Output: False

Yes, it works now. We told you earlier that it’s okay to skip parentheses while declaring a tuple. But in this
situation, it took 3, 4, and 5 to be ints, and believed that we were declaring a tuple, and not comparing two. You
should take care of such situations by coding carefully.

5. Less Than or Equal To (<=) Operator

We guess the next two operators won’t be much of a problem with you. We will quickly learn how to write less
than or equal to in Python.

The less than or equal to operator, denoted by <=, returns True only if the value on the left is either less than or
equal to that on the right of the operator.

1. >>> a=2
2. >>> a<=a*2

Output: True

6. Equal To or Greater Than – Python (>=) Operator

Likewise, this operator returns True only if the value on the left is greater than or equal to that on the right.

1. >>> from math import pi


2. >>> 3.14>=pi

Output: False
Any doubt in Python Comparison Operators? Please Ask us in the comment.

7. Python Equal To (==) Operator

The final two operators we’ll be looking at are equal to (==) and not equal to (!=).

The equal to operator returns True if the values on either side of the operator are equal.

1. >>> 3=='3'

Output: False

As we know, 3 is an integer, and ‘3’ is a string. Hence, they’re unequal. Let’s take about a couple more
examples.

1. >>> {1,3,2}=={1,2,3}
Output: True

Like you know, a set rearranges itself. This is why this returns True.

1. >>> 0==False

Output: True

Of course, False has an integer value of 0. Therefore, it returns True.

8. Python Not Equal Operator (!=) Operator

Finally, we’ll discuss the not equal to operator. Denoted by !=, this does the exact opposite of the equal to
operator. It returns True if the values on either side of the operator are unequal.

1. >>> 3!=3.0

Output: False

1. >>> 3==3.0

Output: True

Note that the operator <> for the same purpose is no longer functional.

This is all about the Python Comparison Operators.

9. Conclusion: Python Comparison Operators

Concluding for today, we learned six comparison operator in python. These are- python less than, python
greater than, Less Than or Equal To, Equal to or greater than, Python Equal To and Python Not Equal Operator.
Their functioning is actually very easy to understand, but feel free to ask a doubt or add to the article in the
comments. Hope you like the Python Comparison Operators Tutorial. See you again.

Python Operator Overloading and Python Magic Methods


by DataFlair Team · September 26, 2018

1. Python Operator Overloading

In this Python tutorial, we are going to discuss Python Operator Overloading, examples of operator overloading
in python, and python magic methods with some operators: python binary operator, python comparison
operator, python unary operators, and python extended assignments. In the programming world, operator
overloading is also called Operator Ad-hoc Polymorphism. Indeed, it is a case of polymorphism where
different operators have different implementations based on their arguments. This is either defined by the
programmer, by the programming language, or both.

So, let’s start the Python Magic Methods Tutorial.


Python Operator Overloading and Python Magic Methods

2. What is Python Operators Overloading?

In Python, an operator lets us perform an operation/procedure on one or more operands(values). Read up on our
article on Python Operators. But for now, let’s take an example.

1. >>> 42+1

43
Here, we performed the addition of two numbers using the addition operator. You know that we can apply this
operator to Python string too. In that case, we call it the concatenation operator. Let’s discuss Python
Syntax before proceeding.

1. >>> '42'+'1'

‘421’

1. >>> 'hello'+' '+'world'

‘hello world’
And then when we do this to Python list, we concatenate two lists.

1. >>> [1,2,3]+[4,5,6]

[1, 2, 3, 4, 5, 6] Python does this implicitly, but what for when you want to apply this operator to your own
class? Can we? Let’s give it a try.
In this python operator overloading tutorial, we take a class ‘myfloat’ to represent floating-point numbers.

1. >>> class myfloat:


2. def init (self,whole,fraction):
3. self.whole=whole
4. self.fraction=fraction
5. def shownumber(self):
6. print(f"I am {self.whole}.{self.fraction}")
7. >>> obj1=myfloat(3,7)
8. >>> obj1.shownumber()

I am 3.7

1. >>> obj2=myfloat(3,3)
2. >>> obj2.shownumber()

I am 3.3
Now, let’s try adding two objects.

1. >>> obj1+obj2

Traceback (most recent call last):

File “<pyshell#24>”, line 1, in <module> obj1+obj2

TypeError: unsupported operand type(s) for +: ‘myfloat’ and ‘myfloat’


As you can see, this raised a TypeError. But don’t fret; we can do this, we’ll discuss in a later section.

3. Python Magic Methods

We observe that python methods have double underscores before and after their names. These are special
methods and are also called ‘dunders’. These help us implement functionality that a normal method can’t
represent.
By now, we have come across only one magic method- init (). But we can, in fact, define our own magic
methods to implement operator overloading in Python. With this, we can define these operators to work on our
custom classes. Some of these are-

a. Python Binary Operators


add for +

sub for –

mul for *

truediv for /

floordiv for //

mod for %

pow for **

and for &


xor for ^

or for |

lshift for <<

rshift for >>

In our article on Python Methods, we discussed these.

b. Python Extended Assignments


iadd for +=

isub for -=

imul for *=

idiv for /=

ifloordiv for //=

imod for %=

ipow for **=

ilshift for <<=

irshift for >>=

iand for &=

ixor for ^=

ior for |=

c. Python Unary Operators


neg for –

pos for +

abs for abs()

invert for ~

complex for complex()

int for int()

long for long()


float for float()

oct for oct()

hex for hex()

d. Python Comparison Operators


lt for <

le for <=

eq for ==

ne for !=

ge for >=

gt for >

Read up on what we have to say about Python Variables and Python Numbers.
Others include radd for reverse add.

1. >>> class myclass:


2. def init (self,age):
3. self.age=age
4. def add (self,other):
5. return self.age+other
6. def radd (self,other):
7. return self.age+other
8. >>> a=myclass(1)
9. >>> a+2

1. >>> 2+a

3
If the interpreter cannot add left to right, it will call radd () instead. Here, radd is in reverse/reflected add.

4. Python Operator Overloading Example


To be able to add our Python objects obj1 and obj2 for class ‘myfloat’, we can do the following.

1. >>> class myfloat:


2. def init (self,whole,fraction):
3. self.whole=whole
4. self.fraction=fraction
5. def shownumber(self):
6. print(f"I am {self.whole}.{self.fraction}")
7. def add (self,other):
8. if (self.fraction+other.fraction)>9:
9. return myfloat(self.whole+other.whole+1,self.fraction+other.fraction-10) return
myfloat(self.whole+other.whole,self.fraction+other.fraction)

Here, we added another method add , that takes two parameters (‘self’ and ‘other’) for the two objects.
Then, it checks if the sum of the fraction parts of both objects is greater than 9. In that case, it transfers a 10 to
the ‘whole’ part as a 1. This is for the carry. It then returns an object with the sums of the whole and fraction
parts of both objects. However, if the condition isn’t met, it simply returns an object with the sums.
Let’s create objects obj1 and obj2 again, and try adding them.

1. >>> obj1=myfloat(3,7)
2. >>> obj1.shownumber()

I am 3.7

1. >>> obj2=myfloat(3,3)
2. >>> obj2.shownumber()

I am 3.3

1. >>> result=obj1+obj2
2. >>> print(f"I am {result.whole}.{result.fraction}")

I am 7.0
As you can see, it works absolutely fine now and lets us add two objects of class ‘myfloat’.

1. >>> result

< main .myfloat object at 0x0572FD10>

This is the resulting object of adding obj1 and obj2.

Here, the interpreter translates obj1+obj2 to obj1. add (obj2).

5. More Examples of Python Operator Overloading

To really understand something, once is never enough. So, let’s take another example of Operator overloading
in Python.

1. >>> class itspower:


2. def init (self,x):
3. self.x=x
4. def pow (self,other):
5. return self.x**other.x
6. >>> a=itspower(2)
7. >>> b=itspower(10)
8. >>> a**b

1024
In this, we take a class ‘itspower’ and two methods init and pow .
pow takes two objects and returns the ‘x’ of first raised to the power of the ‘x’ of the second. When we
type a**b, the interpreter converts it implicitly to a. pow (b).
Now, let’s take another example to demonstrate few more such magic methods.

1. >>> class Person:


2. def init (self,name,age):
3. self.name=name
4. self.age=age
5. def gt (self,other):
6. if self.age>other.age:
7. return True
8. return False
9. def abs (self):
10. return abs(self.age)
11. def iadd (self,other):
12. return self.age+other.age
13. >>> Nick=Person('Nick',7)
14. >>> Angela=Person('Angela',5)
15. >>> Nick>Angela

True

1. >>> Kim=Person('Kim',-8)
2. >>> abs(Kim)

1. >>> Tom=Person('Tom',7)
2. >>> Mikayla=Person('Mikayla',3)
3. >>> Tom+=Mikayla
4. >>> Tom

10
To leave this lesson on an engaging note, we would just like to leave this code here:

1. >>> '1'. add ('1')

’11’

1. >>> 1. add (1)

SyntaxError: invalid syntax

1. >>> [1,2,3]. add ([4,5,6])

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

So, this was all about Python Operator overloading and Python Magic Method Tutorial. Hope you like it.

6. Conclusion
Hence, we studied Python Operator overloading, in-effect, is pure syntactic sugar. Through it, we override a
magic method to be able to use an operator on a custom class. Furthermore, if you have any doubt/query, feel
free to ask in the comment section.

Python Ternary Operator – 5 Ways To Implement Ternary Operators


by DataFlair Team · May 17, 2019

1. Objective – Ternary Operator in Python

Today, we will see Python Ternary Operator. Moreover, we will discuss the example and syntax of Ternary
Operator in Python. Also, we will learn before and nested Python Ternary Operators. At last, we will discuss
ways for implementing Ternary operators in Python.
So, let’s start Python Ternary Operator.

Python Ternary Operator – Implementation With Example

2. Python Ternary Operator Example

Ternary operators in Python are terse conditional expressions. These are operators that test a condition and
based on that, evaluate a value. This was made available since PEP 308 was approved and is available ever
since version 2.4. This operator, if used properly, can reduce code size and enhance readability.
Do you know about Python Comparison Operator

a. Python if-else code


Let’s write code to compare two integers.

1. >>> a,b=2,3
2. >>> if a>b:
3. print("a")
4. else:
5. print("b")

b. Equivalent code with Ternary operator


So let’s try doing the same with ternary operators:

1. >>> a,b=2,3
2. >>> print("a" if a>b else "b")
b
Voila! Done in one line. Python first evaluates the condition. If true, it evaluates the first expression; otherwise,
it evaluates the second. There is a lazy evaluation. It also evaluates the conditions left to right.
You must learn about Python Data Structures

3. The syntax for Python Ternary Operator


Now, let’s learn a little the syntax for Python Ternary Operator.

1. [on_true] if [expression] else [on_false]

In C++, it looks like this:

1. max=(a>b)?a:b

But this isn’t quite Pythonic, so Guido, Python’s BDFL (a status from which he has resigned permanently),
rejected it. Another reason for the veto is that we already have many uses for the colon(:).
One more example of Python ternary Operators:

1. >>> from random import random


2. >>> a,b=random(),random()
3. >>> res="a" if a>b else "b"
4. >>> res

‘b’

1. >>> a,b

(0.009415785735741311, 0.9675879478005226)

4. Ways to Implement Ternary Operator

Below, we are discussing different ways of implementing Python Ternary Operator:


Ways to Implement Ternary Operator

a. Using Python Tuples


We can use tuples to specify what to do if the condition is True/False.
Before moving on, you must learn a little about Python Tuples

1. >>> a,b=random(),random()
2. >>> (b,a)[a>b]

0.8182650892806171
This is equivalent to:

1. >>> (b,a)[True]

But we’re confused which this is- a or b. Let’s try tweaking this.

1. >>> (f"b:{b}",f"a:{a}")[a>b]

‘b:0.8182650892806171’
That’s more like it. Looking at the code, you’ll reckon the first argument in the tuple corresponds to a Boolean
value of False; the second- True. This is because of False=0 and True=1. The condition resides within the [ ].
Note that this method evaluates both elements of the tuple, and hence is less efficient. This happens because it
must first build the tuple before it can look for an index.

1. >>> condition=True
2. >>> 2 if condition else 1/0 #Follows the normal if-else logic tree

2
1. >>> (1/0,2)[condition]

Traceback (most recent call last):


File “<pyshell#48>”, line 1, in <module>
(1/0,2)[condition]
ZeroDivisionError: division by zero

b. Using Python Dictionaries


Likewise, we can make this happen using dictionaries with the same logic.
You must read about Python Dictionaries

1. >>> a,b=random(),random()
2. >>> {False:f"b:{b}",True:f"a:{a}"}[a>b]

‘a:0.37237928632774675’
Since we specify what to do when here, we can interchange the positions of key-value pairs.

1. >>> {True:f"a:{a}",False:f"b:{b}"}[a>b]

‘a:0.37237928632774675’

c. Using Lambdas
We can also make use of Python Lambda Functions to act as a ternary operator.

1. >>> (lambda :f"b:{b}",lambda :f"a:{a}")[a>b]()

‘b:0.5955717855531699’

5. Nested Python Ternary Operator

Let’s try chaining these operators, shall we?

1. >>> a=random()
2. >>> "Less than zero" if a<0 else "Between 0 and 1" if a>=0 and a<=1 else "Greater than one"

‘Between 0 and 1’
Have a look at Python Closure

1. >>> a

0.8537574133103878
Here, we check for the value of a. If it falls shorter than 0, we print “Less than zero”; if between 0 and 1, we
print “Between 0 and 1”. Else, we print “Greater than one”. Notice how we nested them.

6. Before Ternary Operators in Python

Before this was a thing with Python, this is what we did (we used a common idiom):

1. >>> a,b=2,3
2. >>> a<b and a or b

2
So how does this work? Let’s see.

 a is 2 and b is 3
 It checks if a<b
 If true, it gives us True and a or b
 This gives us a or b
 It checks a
 If false, it gives us False or b
 This gives us b

This method, however, doesn’t work for a=0. This is because that would be True and 0 or b, which is True and
False or b, which is False or b, which is b. Oops!
Now why don’t you try formulating an expression for a>b and try explaining it to yourself?
Let’s learn about Python Data Science
It could also be beneficial to use the and/or logic when one of our expressions is the same as the condition:

1. >>> def sayhello(): print('Hello')


2. >>> sayhello() if sayhello() else 'Bye'

Hello
Hello
True

1. >>> sayhello() or 'Bye'

Hello
True
So, this was all in Python Ternary Operator. Hope you like our explanation.

7. Conclusion – Python Ternary Operator


Phew! That’s all. Through this Python Ternary Operator blog, we tried our best to bring to you all there is to
know about the ternary operator in Python. Moreover, we discussed the implementation of Ternary Operators in
Python. Also, we saw Python Ternary Operators example and syntax. We curated our content from the best
sources. Like this tutorial? If you’d like us to write about something that has to do with Python (and we haven’t
already), please let us know in the comments below. We are excited to hear from you!

Python Operator Precedence – PEMDAS & Short Circuiting


by DataFlair Team · November 19, 2018

1. Objective
In our last Python tutorial, we discussed Python sys Module. Today, we will see Python Operator Precedence.
Given an expression of multiple operators, how do you go about it? What is 2+3*4%5-1? 13 or 3? To answer
such questions, you will need to know what comes first. In this Python Operator Precedence tutorial, we address
this issue. Moreover, we will learn PEMDAS and short-circuiting in python. At last, we will discuss the
associativity of Python Operators.
So, let’s start the Python Operator Precedence tutorial.

Python Operator Precedence – PEMDAS & Short Circuiting

First, let’s revise a little about Python Operators.

2. Python Operator Precedence Table


Take a look at the following table of Python Operator Precedence:

Operator Description
() (Highest precedence) Parentheses (grouping)
f(args…) Function call
(expressions…), [expressions…], {key: value…}, Binding or tuple display, list display, dictionary display, set
{expressions…} display
x[index], x[index:index], x(arguments), x.attribute Subscription, slicing, call, attribute reference
await x Await expression
** Exponentiation
+x, –x, ~x Positive, negative, bitwise NOT
*, @, /, //, % Multiplication, division, remainder
+, – Addition, subtraction
<<, >> Bitwise shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in, not in, is, is not, <, <=, >, >=,
<>, !=, == Comparisons, membership, identity
not x Boolean NOT
and Boolean AND
or Boolean OR
if- else Conditional expression
lambda (Lowest precedence) Lambda expression

In here, the operators in one cell evaluate left to right and exponentiation groups right to left. The lowest
Precedence in Python Operator is the least binding and the highest Precedence in Python Operator is the most. It
is also true that we observe the same precedence for comparisons, membership tests, and identity tests. These
also have a left-to-right chaining feature.\
Have a look at Python Syntax and Semantics

3. What is Python Expression?


Before we can tell you about which operator comes first, you’d want to be introduced to expressions. An
expression is a combination of values, variables, operators, and function calls. Notably, the Python interpreter
can evaluate a valid expression. Why don’t we take an example?

1. >>> 4+3

7
4+3 is an expression with one operator. We can also put in more than one. The precedence rules show us the
way to follow an order. The divisionn has a higher precedence than addition.
You must read about Python Statements

1. >>> 3+3/3

4.0
When we use parentheses, however, we can alter the order of execution here.

1. >>> (3+3)/3

2.0
What we conclude here is that using parentheses, we can force the operators of lower precedence to run first. Or
we can say that when two operators share an operand, the one with the higher precedence gets to go first.

4. Python Operator Precedence – PEMDAS


If you’re on this page reading about Python, you sure have heard about BODMAS somewhere in your journey
so far (mathematics, school). In Python, however, we come across PEMDAS:
Parentheses
Exponentiation
Multiplication
Division
Addition
Subtraction
Let’s revise Python Iterables
A mnemonic to remember that will be “Please Excuse My Dear Aunt Susie”.
Let’s take an example.

1. >>> ((((13+5)*2)-4)/2)-13

3.0
How did that happen? Let’s work it out.
13+5 gives us 18
18*2 gives us 36
36-4 gives us 32
32/2 gives us 16.0 #Note that division gives us floats!
16-13 gives us 3.0

5. Python Operator Precedence – Short Circuiting


Python always evaluates the left operand before the right- even in function arguments. For expressions with
and or operations, it uses short-circuiting. This means it evaluates the second operand only until it is needed.
Because of this, such statements can work reliably:

Python Operator Precedence – Short Circuiting

Do you know about Python Closure

1. >>> if(s!=None) and (len(s)<10): pass

To short-circuit is to stop executing the Boolean operation if we have already arrived at the truth value of the
expression. Let’s take a look at this:

 X or Y- Evaluates Y only if X is false; otherwise, returns X


 X and Y- Evaluates Y only if X is true; otherwise, returns X
a. Short Circuiting with and/or
See what this gives us:

1. >>> 0 or "Hello" and 1

1
This doesn’t give us “Hello”, but 1, because:
0 or “Hello” gives us “Hello”
“Hello” and 1 gives us 1
Let’s discuss the Python Multiple Inheritance

b. Short-Circuiting with all()/any()


This also works with the all() and any() functions.

1. >>> def check(i):


2. return i
3. >>> all(check(i) for i in [1,1,0,0,3])

False
This stops at the first False it gets (the 0 at the third position) and returns False.

1. >>> any(check(i) for i in [0,0,0,1,3])

True
This stops at the first True it gets (the 1 at the fourth position) and returns True.

c. Short Circuiting with Conditional Operators


Watch how this unfurls with conditional operators like > and <.
Have a look at Python Bitwise Operator

1. >>> 7>8>check(4)

False
This stops at 7>8 and returns False.

d. Short-Circuiting with Ternary Operators


Now, consider the following expression which is a ternary operator:

1. >>> print("One") if print("Two") else print("Three")

Two
Three
What happens here? Let’s find out.
Python first checks the condition print(“Two”). In evaluating this, it prints “Two”. Also, the Boolean value for
this is False:

1. >>> bool(print("Two"))

Two
False
Since it is False, it does not evaluate print(“One”) and simply evaluates print(“Three”).
Hence, the final output we get is:
Two
Three
Let’s take a tour to Python Sets and Booleans

6. Associativity of Operators in Python


In that table above, many cells had more than one operator. These share precedence. So then, which to evaluate
first? Associativity comes to the rescue here. Many operators have left-to-right associativity.

a. Associative Operators
 Multiplication (*) and Floor Division (//)

For an example, let’s consider the operators multiplication(*) and floor division(//). Watch how the left operand
evaluates first:

1. >>> 3*5//4

1. >>> 3*(5//4)

3
While both give us the same result, they do that in different ways. Watch how:
For the first example:
3*5 gives us 15
15//4 gives us 3
For the second example:
5//4 gives us 1
3*1 gives us 3

 Exponentiation (**)

Now, let’s try this on exponentiation:

1. >>> (2**3)**2

64
You must read Python about Python Functions
And now without parentheses:
1. >>> 2**3**2

512
This is because this is equivalent to:
2**(3**2)
This gives us 2**9
This gives us 512

b. Non-Associative Operators
Assignment and comparison operators are not associative. What this means is that x<y<z is none of the
following:
(x<y)<z
x<(y<z)
This expression is actually equivalent to (and this evaluates left-to-right):
x<y and y<z
Let’s revise Python Strings
So, this was all in Python Operator Precedence. Hope you like our explanation.

7. Conclusion – Python Operator Precedence


Hence, in this Python Operator Precedence tutorial, we learned about operator precedence, PEMDAS, and
short-circuiting. Also, we discussed expressions and associativity in Python Operator Precedence. What would
you like to read from DataFlair? Do let us know in the comments below.

Python Namespace and Variable Scope – Local and Global Variables


by DataFlair Team · May 17, 2019

1. Python Namespace

In this Python Tutorial, we discuss Python Namespace, Types of Namespace in python and Python Variable
Scope, with their examples and python Syntax.
Python Namespace and Variable Scope – Local and Global Variables

Try this: type ‘import this’ in the interpreter.

1. >>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.


Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

2. What is Python Name?

Before we move on to namespaces in python, let’s talk about names in python. A Python name is an identifier-
something we use to access a Python object and in Python, everything’s an object.

We’ll take an example.


1. >>> rank=1

Here, ‘rank’ is the name associated with the Python object 1. To get this object’s address in RAM, we use the
id() function.

1. >>> id(rank)

492979856

1. >>> id(1)

492979856

To take a slightly more complex example, we store 2 in a name ‘a’. Then, we increment it by 1 and associate
the name ‘b’ to the object 2. We keep checking the id as we go.

1. >>> a=2
2. >>> id(a)

492979872

1. >>> a+=1
2. >>> id(a)

492979888

1. >>> b=2
2. >>> id(b)

492979872

1. >>>
2. >>> id(2)

492979872

1. >>> id(3)

492979888

So what’s actually happening? We’ll illustrate.


Java Namespace – Python Name

As you can see, when we set ‘a’ to 3 and set ‘b’ to 2, ‘b’ starts pointing to the object ‘a’ once pointed to. Isn’t
that quite efficient? It does not have to create another object to hold 2 for b. This dynamic name binding is
powerful.

Also, a name can hold any kind of value.

1. >>> a=1
2. >>> a='one'

Finally, since everything is an object, so are Python functions. Consequently, you can associate them with
names.

1. >>> identity=id
2. >>> identity(2)

492979872

Here, we associate the name ‘identity’ with the built-in function id().

Bonus Question- Check the following code and figure out what’s happening.

1. >>> def sayhello(): print('Hello')


2. >>> hi=sayhello()

Hello

1. >>> hi
2. >>> type(hi)

<class ‘NoneType’>

Well, since the function does not return anything, we get an object of class ‘NoneType’. Of course, None is an
object that indicates no value. Did function sayhello() return a value, things would be different. Let’s take
another example.
1. >>> def func1():
2. print("Hi")
3. return 1
4. >>> func2=func1()

Hi

1. >>> func2

1. >>> type(func2)

<class ‘int’>

3. What is Python Namespaces?


A namespace in python is a collection of names. So, a namespace is essentially a mapping of names to
corresponding objects. At any instant, different python namespaces can coexist completely isolated- the
isolation ensures that there are no name collisions. Simply speaking, two namespaces in python can have the
same name without facing any problem. A namespace is implemented as a Python dictionary.

When we start the interpreter, a python namespace is created for as long as we don’t exist. This holds all built-in
names. It is due to this that python functions like print() and id() are always available. Also, each module
creates its own global namespace in python.

When you call a function, a local python namespace is created for all the names in it. A module has a global
namespace. The built-in namespace encloses this. Take a look at the following figure to get a clearer
understanding.

Types of Python Namespace


4. What is Python Variable Scope?

Through various python namespaces, not each can be accessed from every part of the program. A namespace is
in variable scope in a part of a program, if it lets you access the python namespace without having to use a
prefix.

At any instant, we have at least three nested python scopes:

1. Current function’s variable scope- has local names


2. Module’s variable scope- has global names
3. The outermost variable scope- has built-in names

This in accordance with the three kinds of namespaces in python, we just discussed. This also decides the order
of searching for when a reference is made. The order is- the local Python namespace, the global namespace, the
built-in namespace. Also, a nested function creates a nested variable scope inside the outer function’s scope.

5. Few Python Namespace Example

To further what we said, let’s take an example.

1. >>> a=1
2. >>> def func1():
3. b=2
4. def func2():
5. c=3

In this code, ‘a’ is in the global namespace in python. ‘b’ is in the local namespace of func1, and ‘c’ is in the
nested local python namespace of func2.

To func2, ‘c’ is local, ‘b’ is nonlocal, and ‘a’ is global. By nonlocal, we mean it isn’t global, but isn’t local
either. Of course, here, you can write ‘c’, and read both ‘b’ and ‘c’. But you can’t access ‘a’, that would create a
new local variable ‘a’. See this example,

1. >>> a=1
2. >>> def func1():
3. b=2
4. def func3():
5. a=2
6. b=3
7. c=3
8. print(f"a={a}, b={b}, c={c}")
9. func3()
10. print(f"b={b}")
11. >>> func1()
12. a=2, b=3, c=3
13. b=2
14. >>> a

1
To deal with this situation, we can use the ‘global’ and ‘nonlocal’ keywords.
1. >>> a=1
2. >>> def func1():
3. b=2
4. def func3():
5. global
a 6. a=2
7. nonlocal b
8. b=3
9. c=3
10. print(f"a={a}, b={b}, c={c}")
11. func3()
12. print(f"b={b}")
13. >>> func1()
14. a=2, b=3, c=3
15. b=3
16. >>> a

6. Conclusion

There are three types of Python namespaces- global, local, and built-in. It’s the same with a variable scope in
python. Also, the ‘global’ keyword lets us refer to a name in a global scope. Likewise, the ‘nonlocal’ keyword
lets us refer to a name in a nonlocal scope. If you don’t get something, ask us in the comments.

Python Decision Making Statements – Python If, If-else, Nested


Statements
by DataFlair Team · May 17, 2019

1. Python Decision Making Statements

Today, we talk about Python decision making constructs. This includes Python if statements, if else
statements, elif statements, nested if conditions and single statement conditions. We will understand these with
syntax and example to get a clear understanding.

So, let’s start the Python Decision Making Tutorial.


Python Decision Making Statements – Python If, If-else, Nested Statements

2. Python Decision Making Statements

Before we begin with python decision making expressions, let us revise Python concepts.

Sometimes, in a program, we may want to make a decision based on a condition. We know that an expression’s
value can be True or False. We may want to do something only when a certain condition is true. For example,
assume variables a and b. If a is greater, then we want to print “a is greater”. Otherwise, we want to print “b is
greater”. For this, we use an if-statement. Also, operators come in handy when you want to join conditions to
make a composite one.

Let us see at various python decision making expressions in details with syntax and example. So let’s install
python on Windows first and revise Python syntax for programming in Python.

Learn: Operators in Python

3. Python if Statements

An if statement in python takes an expression with it. If the expression amounts to True, then the block of
statements under it is executed. If it amounts to False, then the block is skipped and control transfers to the
statements after the block. But remember to indent the statements in a block equally. This is because we don’t
use curly braces to delimit blocks. Also, use a colon(:) after the condition.
Python Decision Making – Python if Statement

Before starting with the example, let us see various types of variables and data types in Python as it will help in
better programming.

1. a=7
2. if a>6:
3. print(f"{a} is good")

7 is good

Here, since 7>6, the condition is true. So, it prints the given string.

1. if 1:
2. print("yay")

yay

We know, 1 has a Boolean value of True. So, the condition is true, and it prints ‘yay’.

1. if(1==1):
2. print("1")

1. >>>
You can also write the condition in parentheses. It does not cause a syntax error.

4. Python if-else Statements

What happens when the condition is untrue? We can mention that it in the block after the else statement. An
else statement comes right after the block after ‘if’.

Python Decision Making – Python if else Statement

1. if 2<1:
2. print("2")
3. else:
4. SyntaxError: Invalid syntax
5. if 2<1:
6. print("2")
7. else:
8. print("1")

Pay attention to the indent. The else keyword does not appear in the if-block. Press Backspace to undo the
automatic indent. Here, 2 is not less than 1. So, the statements in the else-block are executed. It prints 1.

1. if 2<1:
2. print("2")
3. else:
4. print("1")
5. else:
6. SyntaxError: invalid syntax
7. >>>
As appears in the above example, you cannot posit two else statements under an if. It causes a syntax error.

5. Chained Conditionals (elif ladder)

Python allows the elif keyword as a replacement to the else-if statements in Java or C++. When we have more
than one condition to check, we can use it. If condition 1 isn’t True, condition 2 is checked. If it isn’t true,
condition 3 is checked.

Python Decision Making Statements – Python Chained operators

1. if 2<1:
2. print("2")
3. else if 3<1:
4. SyntaxError: invalid syntax
5. if 2<1:
6. print("2")
7. elif 1<3:
8. print("1")

1. if 2<1:
2. print("2")
3. elif 3<1:
4. print("3")
5. else:
6. print("1")

1. >>>

As we saw, else if causes a syntax error. We must use elif. Here, 2 is not less than 1, so, the condition with elif
is checked. Since it is true (1<3), it prints 1. Also, you can put an else statement after your elif statements if you
want. Since in the last example, the first two conditions are false, the else is executed. So, it prints 1.

6. Nested if Statements in Python


You can put an if statement in the block under another if statement. This is to implement further checks.

Python Decision Making – Nested if Statements in Python

1. >>>a=1
2. >>>b=2
3. >>>if a==1:
4. if b==2:
5. print("a is 1 and b is 2")

a is 1 and b is 2

1. >>>

Here, a is 1. So, b is checked. Since it is 2, it prints the given string. Not every if block has to have statements
though.

7. Single Statement Condition in Python


If you only need to write a single statement under if, you can write it in the same line using single statement
python decision making constructs.

1. a=7
2. if a>4: print("Greater")

Greater

1. >>>

Here, we wrote it in one line, but it works without a problem.

1. if a>4: print("Hi"); print("Works")

Hi
Works

1. >>>

You can also use semicolons to write more than one statement in the same line as the condition. However, this
may affect the readability of your code.

So, this was all about Python Decision Making Statements. Hope you like our explanation.

8. Conclusion

In this lesson, we learnt about the Python decision-making constructs. These include Python if, else, elif, and
nested-if statements. We have discussed all these statements with syntax and examples for better understanding.
They allow us to make decisions in python so that we can choose a set of statements to execute. Don’t forget to
try your own combinations in the shell. Furthermore, if you have any query, feel free to share with us!

Python Loop Tutorial – Python For Loop, Nested For Loop


by DataFlair Team · May 17, 2019

1. Python Loop – Objective


In this Python Loop Tutorial, we will learn about different types of Python Loop. Here, we will study Python
For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python with their
subtypes, syntax, and examples.

So, let’s start Python Loop Tutorial.

2. Introduction to Python Loop

When you want some statements to execute a hundred times, you don’t repeat them 100 times. Think of when
you want to print numbers 1 to 99. Or that you want to say Hello to 99 friends. In such a case, you can use loops
in python.

Here, we will discuss 4 types of Python Loop:

 Python For Loop


 Python While Loop
 Python Loop Control Statements
 Nested For Loop in Python

3. Python While Loop

A while loop in python iterates till its condition becomes False. In other words, it executes the statements under
itself while the condition it takes is True.
When the program control reaches the while loop, the condition is checked. If the condition is true, the block of
code under it is executed. Remember to indent all statements under the loop equally. After that, the condition is
checked again. This continues until the condition becomes false. Then, the first statement, if any, after the loop
is executed.

1. >>> a=3
2. >>> while(a>0):
3. print(a)
4. a-=1

3
2
1
This loop prints numbers from 3 to 1. In Python, a—wouldn’t work. We use a-=1 for the same.

a. An Infinite Loop
Be careful while using a while loop. Because if you forget to increment the counter variable in python, or write
flawed logic, the condition may never become false. In such a case, the loop will run infinitely, and the
conditions after the loop will starve. To stop execution, press Ctrl+C. However, an infinite loop may actually be
useful. This in cases when a semaphore is needed, or for client/server programming. A semaphore is a variable
used solely for synchronization in accessing shared resources.

b. The else statement for while loop


A while loop may have an else statement after it. When the condition becomes false, the block under the else
statement is executed. However, it doesn’t execute if you break out of the loop or if an exception is raised.

1. >>> a=3
2. >>> while(a>0):
3. print(a)
4. a-=1
5. else:
6. print("Reached 0")

3
2
1

Reached 0

In the following code, we put a break statement in the body of the while loop for a==1. So, when that happens,
the statement in the else block is not executed.

1. >>> a=3
2. >>> while(a>0):
3. print(a)
4. a-=1
5. if a==1: break;
6. else:
7. print("Reached 0")

3
2

c. Single Statement while


Like an if statement, if we have only one statement in while’s body, we can write it all in one line.

1. >>> a=3
2. >>> while a>0: print(a); a-=1;

3
2
1
You can see that there were two statements in while’s body, but we used semicolons to separate them. Without
the second statement, it would form an infinite loop.

3. Python For Loop


Python for loop can iterate over a sequence of items. The structure of a for loop in Python is different than that
in C++ or Java. That is, for(int i=0;i<n;i++) won’t work here. In Python, we use the ‘in’ keyword. Lets see a
Python for loop Example

Python Loop Tutorial – Python for Loop

1. >>> for a in range(3):


2. print(a)

0
1
2
If we wanted to print 1 to 3, we could write the following code.

1. >>> for a in range(3):


2. print(a+1)

1
2
3
a. The range() function
This function yields a sequence of numbers. When called with one argument, say n, it creates a sequence of
numbers from 0 to n-1.

1. >>> list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We use the list function to convert the range object into a list object.

Calling it with two arguments creates a sequence of numbers from the first to the second.

1. >>> list(range(2,7))

[2, 3, 4, 5, 6]

You can also pass three arguments. The third argument is the interval.

1. >>> list(range(2,12,2))

[2, 4, 6, 8, 10]

Remember, the interval can also be negative.

1. >>> list(range(12,2,-2))

[12, 10, 8, 6, 4]

However, the following codes will return an empty list.


1. >>> list(range(12,2))

[]

1. >>> list(range(2,12,-2))

[]

1. >>> list(range(12,2,2))

[]

b. Iterating on lists or similar constructs


You aren’t bound to use the range() function, though. You can use the loop to iterate on a list or a similar
construct.

1. >>> for a in [1,2,3]:


2. print(a)

1
2
3

1. >>> for i in {2,3,3,4}:


2. print(i)

2
3
4
You can also iterate on a string.

1. >>> for i in 'wisdom':


2. print(i)

w
i
s
d
o
m

c. Iterating on indices of a list or a similar construct


The len() function returns the length of the list. When you apply the range() function on that, it returns the
indices of the list on a range object. You can iterate on that.

1. >>> list=['Romanian','Spanish','Gujarati']
2. >>> for i in range(len(list)):
3. print(list[i])
Romanian
Spanish
Gujarati

d. The else statement for for-loop


Like a while loop, a for-loop may also have an else statement after it. When the loop is exhausted, the block
under the else statement executes.

1. >>> for i in range(10):


2. print(i)
3. else:
4. print("Reached else")

0
1
2
3
4
5
6
7
8
9
Reached else
Like in the while loop, it doesn’t execute if you break out of the loop or if an exception is raised.

1. >>> for i in range(10):


2. print(i)
3. if(i==7): break
4. else: print("Reached else")

0
1
2
3
4
5
6
7

4. Nested for Loops Python

You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for, or a for
inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop. You can go as far as
you want.

1. >>> for i in range(1,6):


2. for j in range(i):
3. print("*",end=' ')
4. print()
*
**
***
****
*****
Let’s look at some nested while loops to print the same pattern.

1. >>> i=6
2. >>> while(i>0):
3. j=6
4. while(j>i):
5. print("*",end=' ')
6. j-=1
7. i-=1
8. print()

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

5. Loop Control Statements in Python

Sometimes, you may want to break out of normal execution in a loop. For this, we have three keywords in
Python- break, continue, and pass.

Python Loop Tutorial –


Loop Control Statements in Python

a. break statement
When you put a break statement in the body of a loop, the loop stops executing, and control shifts to the first
statement outside it. You can put it in a for or while loop.

1. >>> for i in 'break':


2. print(i)
3. if i=='a': break;

b
r
e
a

b. continue statement
When the program control reaches the continue statement, it skips the statements after ‘continue’. It then shifts
to the next item in the sequence and executes the block of code for it. You can use it with both for and while
loops.

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

1
2
3
4
5
7
8
If here, the iteration i+=1 succeeds the if condition, it prints to 5 and gets stuck in an infinite loop. You can
break out of an infinite loop by pressing Ctrl+C.

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

5
Traceback (most recent call last):
File “<pyshell#14>”, line 1, in <module>
while(i<8):
KeyboardInterrupt

c. pass statement
In Python, we use the pass statement to implement stubs. When we need a particular loop, class, or function in
our program, but don’t know what goes in it, we place the pass statement in it. It is a null statement. The
interpreter does not ignore it, but it performs a no-operation (NOP).

1. >>> for i in 'selfhelp':


2. pass
3. >>> print(i)

p
To run this code, save it in a .py file, and press F5. It causes a syntax error in the shell.

6. Conclusion

In this tutorial on Python Loops, we learnt about while and for loops in Python. We also learnt how to nest
loops, and use the range() function to iterate through a sequence of items. Lastly, we learnt about break,
continue, and pass statements to control loops. So, try out your own combinations in the shell, and don’t forget
to leave your feedback in the comments.

Python Function Tutorial – Type of Functions in Python(With Example)


by DataFlair Team · May 27, 2019

1. Python Function – Objective

In our tutorial, we discussed dictionaries in python. Now, we forward to deeper parts of the language, let’s
read about Python Function. Moreover, we will study the different types of functions in Python: Python built-in
functions, Python recursion function, Python lambda function, and Python user-defined functions with their
syntax and examples.

So, let’s start the Python Function Tutorial.


Python Function Tutorial – Type of Functions in Python(With Example)

Learn: Range Function in Python – Range() in Python

2. An Introduction to Function in Python

Python function in any programming language is a sequence of statements in a certain order, given a name.
When called, those statements are executed. So we don’t have to write the code again and again for each [type
of] data that we want to apply it to. This is called code re-usability.

3. User-Defined Functions in Python

For simplicity purposes, we will divide this lesson into two parts. First, we will talk about user-defined
functions in Python. Python lets us group a sequence of statements into a single entity, called a function. A
Python function may or may not have a name. We’ll look at functions without a name later in this tutorial.

a. Advantages of User-defined Functions in Python


1. This Python Function help divide a program into modules. This makes the code easier to manage,
debug, and scale.
2. It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is
to call the function.
3. This Python Function allow us to change functionality easily, and different programmers can work on
different functions.

b. Defining a Function in Python


To define your own Python function, you use the ‘def’ keyword before its name. And its name is to be followed
by parentheses, before a colon(:).

1. >>> def hello():


2. print("Hello")

The contents inside the body of the function must be equally indented.

As we had discussed in our article on Python syntax, you may use a docstring right under the first line of a
function declaration. This is a documentation string, and it explains what the function does.

1. >>> def hello():


2. """
3. This Python function simply prints hello to the screen
4. """
5. print("Hello")

You can access this docstring using the doc attribute of the function.

1. >>> def func1():


2. """
3. This is the docstring
4. """
5. print("Hello")
1. >>> func1. doc
2. '\n\tThis is the docstring\n\t'

However, if you apply the attribute to a function without a docstring, this happens.

1. >>> sum. doc


2. >>> type(sum. doc )
3. <class 'NoneType'>
4. >>> bool(sum. doc )

False

If you don’t yet know what to put in the function, then you should put the pass statement in its body. If you
leave its body empty, you get an error “Expected an indented block”.

1. >>> def hello1():


2. pass
3. >>> hello1()

You can even reassign a function by defining it again.

c. Rules for naming python function (identifier)


We follow the same rules when naming a function as we do when naming a variable.

1. It can begin with either of the following: A-Z, a-z, and underscore(_).
2. The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(_).
3. A reserved keyword may not be chosen as an identifier.

It is good practice to name a Python function according to what it does.

d. Python Function Parameters


Sometimes, you may want a function to operate on some variables, and produce a result. Such a function may
take any number of parameters. Let’s take a function to add two numbers.

1. >>> def sum(a,b):


2. print(f"{a}+{b}={a+b}")

1. >>> sum(2,3)

2+3=5

Here, the function sum() takes two parameters- a and b. When we call the function, we pass numbers 2 and 3.
These are the arguments that fit a and b respectively. We will describe calling a function in point f. A function
in Python may contain any number of parameters, or none.

In the next example, we try adding an int and a float.

1. >>> def sum2(a,b):


2. print(f"{a}+{b}={a+b}")
3. >>> sum2(3.0,2)

3.0+2=5.0

However, you can’t add incompatible types.

1. >>> sum2('Hello',2)

Traceback (most recent call last):

File “<pyshell#39>”, line 1, in <module>

sum2(‘Hello’,2)

File “<pyshell#38>”, line 2, in sum2

print(f”{a}+{b}={a+b}”)

TypeError: must be str, not int

e. Python return statement


A Python function may optionally return a value. This value can be a result that it produced on its execution. Or
it can be something you specify- an expression or a value.

1. >>> def func1(a):


2. if a%2==0:
3. return 0
4. else:
5. return 1
6. >>> func1(7)

As soon as a return statement is reached in a function, the function stops executing. Then, the next statement
after the function call is executed. Let’s try returning an expression.

1. >>> def sum(a,b):


2. return a+b
3. >>> sum(2,3)

1. >>> c=sum(2,3)

This was the Python Return Function

f. Calling a Python function


To call a Python function at a place in your code, you simply need to name it, and pass arguments, if any. Let’s
call the function hello() that we defined in section b.
1. >>> hello()

Hello

We already saw how to call python function with arguments in section e.

g. Scope and Lifetime of Variables in Python


A variable isn’t visible everywhere and alive every time. We study this in functions because the scope and
lifetime for a variable depend on whether it is inside a function.

1. Scope

A variable’s scope tells us where in the program it is visible. A variable may have local or global scope.

 Local Scope- A variable that’s declared inside a function has a local scope. In other words, it is local to
that function.

1. >>> def func3():


2. x=7
3. print(x)
4. >>> func3()

If you then try to access the variable x outside the function, you cannot.

1. >>> x

Traceback (most recent call last):

File “<pyshell#96>”, line 1, in <module>

NameError: name ‘x’ is not defined

 Global Scope- When you declare a variable outside python function, or anything else, it has global
scope. It means that it is visible everywhere within the program.

1. >>> y=7
2. >>> def func4():
3. print(y)
4. >>> func4()

However, you can’t change its value from inside a local scope(here, inside a function). To do so, you must
declare it global inside the function, using the ‘global’ keyword.

1. >>> def func4():


2. global y
3. y+=1
4. print(y)
5. >>> func4()

1. >>> y

As you can see, y has been changed to 8.

2. Lifetime

A variable’s lifetime is the period of time for which it resides in the memory.

A variable that’s declared inside python function is destroyed after the function stops executing. So the next
time the function is called, it does not remember the previous value of that variable.

1. >>> def func1():


2. counter=0
3. counter+=1
4. print(counter)
5. >>> func1()

1. >>> func1()

As you can see here, the function func1() doesn’t print 2 the second time.

h. Deleting Python function


Till now, we have seen how to delete a variable. Similarly, you can delete a function with the ‘del’ keyword.

1. >>> def func7():


2. print("7")
3. >>> func7()

1. >>> del func7


2. >>> func7()

Traceback (most recent call last):

File “<pyshell#137>”, line 1, in <module>


func7()

NameError: name ‘func7’ is not defined

When deleting a function, you don’t need to put parentheses after its name.

Different Python Function Explained Below.

4. Python Built-in Functions


In various previous lessons, we have seen a range of built-in functions by Python. This Python function apply
on constructs like int, float, bin, hex, string, list, tuple, set, dictionary, and so. Refer to those lessons to revise
them all.

5. Python Lambda Expressions

As we said earlier, a function doesn’t need to have a name. A lambda expression in Python allows us to create
anonymous python function, and we use the ‘lambda’ keyword for it. The following is the syntax for a lambda
expression.

lambda arguments:expression

It’s worth noting that it can have any number of arguments, but only one expression. It evaluates the value of
that expression, and returns the result. Let’s take an example.

1. >>> myvar=lambda a,b:(a*b)+2


2. >>> myvar(3,5)

17

This code takes the numbers 3 and 5 as arguments a and b respectively, and puts them in the expression
(a*b)+2. This makes it (3*5)+2, which is 17. Finally, it returns 17.

Actually, the function object is assigned to the identifier myvar.

Any Doubt yet in Python Function? Please Comment.

6. Python Recursion Function

A very interesting concept in any field, recursion is using something to define itself. In other words, it is
something calling itself. In Python function, recursion is when a function calls itself. To see how this could be
useful, let’s try calculating the factorial of a number. Mathematically, a number’s factorial is:

n!=n*n-1*n-2*…*2*1

To code this, we type the following.

1. >>> def facto(n):


2. if n==1:
3. return 1
4. return n*facto(n-1)
5. >>> facto(5)

120

1. >>> facto(1)

1. >>> facto(2)

1. >>> facto(3)

This was all about recursion function in Python

Isn’t this handy? Tell us where else would you use recursion. However, remember that if you don’t mention the
base case (the case which converges the condition), it can result in an infinite execution.

This was all about the Python Function

7. Conclusion: Python Function

It is important to revise in order to retain information. In this lesson, we learned about the Python function.
First, we saw the advantages of a user-defined function in Python. Now we can create, update, and delete a
function. And we know that a function may take arguments and may return a value. We also looked at the scope
and lifetime of a variable. Hope you like the Python Function Tutorial.

Don’t forget to revise the various built-in functions supported by Python. Refer to our tutorials for the same.

Python Function Arguments with Types, Syntax and Examples


by DataFlair Team · November 15, 2018

1. Python Function Argument

Previously, we have covered Functions in Python. In this Python Function Arguments tutorial, we will learn
about what function arguments used in Python and its type: Python Keyword Arguments, Default Arguments in
Python, and Python Arbitrary Arguments.

So, let’s start Python Function Arguments.


Python Function Arguments with Types, Syntax and Examples

2. What is Python Function?

Python function is a sequence of statements that execute in a certain order, we associate a name with it.
This lets us reuse code.

We define a function using the ‘def’ keyword. Let’s take an example.

1. >>> def sayhello():


2. """
3. This prints Hello
4. """
5. print("Hello")

Then to call this, we simply use the function’s name with parentheses. Also, notice the docstring.

1. >>> sayhello()

Hello
This one takes no arguments. Now, let’s see one with python function arguments.

1. >>> def sum(a,b):


2. return a+b
3. >>> sum(2,3)

5
To this, if we pass only one argument, the interpreter complains.

1. >>> sum(3)
2. Traceback (most recent call last):
3. File "<pyshell#44>", line 1, in <module>
4. sum(3)

TypeError: sum() missing 1 required positional argument: ‘b’


To deal with such situations, we see different types of arguments in python functions.
Learn: Recursion in Python with Examples

3. Types of Python Function Arguments

There are various types of Python arguments functions. Let’s learn them one by one:

a. Default Argument in Python


Python Program arguments can have default values. We assign a default value to an argument using the
assignment operator in python(=). When we call a function without a value for an argument, its default value
(as mentioned) is used.

1. >>> def greeting(name='User'):


2. print(f"Hello, {name}")
3. >>> greeting('Ayushi')

Hello, Ayushi

1. >>> greeting()

Hello, User

Here, when we call greeting() without an argument, the name takes on its default value- ‘User’.

Any number of arguments can have a default value. But you must make sure to not have a non-default argument
after a default argument. In other words, if you provide a default argument, all others succeeding it must have
default values as well. The reason is simple. Imagine you have a function with two parameters. The first
argument has a default value, but the second doesn’t. Now when you call it(if it was allowed), you provide only
one argument. The interpreter takes it to be the first argument. What happens to the second argument, then? It
has no clue.

1. >>> def sum(a=1,b):


2. return a+b

SyntaxError: non-default argument follows default argument


This was all about the default arguments in Python

b. Python Keyword Arguments


With keyword arguments in python, we can change the order of passing the arguments without any
consequences. Let’s take a function to divide two numbers, and return the quotient.

1. >>> def divide(a,b):


2. return a/b
3. >>> divide(3,2)

1.5
We can call this function with arguments in any order, as long as we specify which value goes into what.

1. >>> divide(a=1,b=2)

0.5

1. >>> divide(b=2,a=1)

0.5
As you can see, both give us the same thing. These are keyword python function arguments.
But if you try to put a positional argument after a keyword argument, it will throw Python exception of
SyntaxError.

1. >>> divide(b=2,1)

SyntaxError: positional argument follows keyword argument in python.


Learn how to do Exception Handling in Python.
Any Doubt yet in Python Function Arguments

c. Python Arbitrary Arguments


You may not always know how many arguments you’ll get. In that case, you use an asterisk(*) before an
argument name.

1. >>> def sayhello(*names):


2. for name in names:
3. print(f"Hello, {name}")

And then when you call the function with a number of arguments, they get wrapped into a Python tuple. We
iterate over them using the for loop in python.

1. >>> sayhello('Ayushi','Leo','Megha')

Hello, Ayushi
Hello, Leo
Hello, Megha
Learn: Python Applications – Python Use Cases in Real World
This was all about the Python Function Arguments.

4. Conclusion: Python Function Arguments

Hence, we conclude that Python Function Arguments and its three types of arguments to functions. These are-
default, keyword, and arbitrary arguments. Where default arguments help deal with the absence of values,
keyword arguments let us use any order. Finally, arbitrary arguments in python save us in situations where
we’re not sure how many arguments we’ll get. Furthermore, if you have a query, feel free to ask in the comment
box
Top python Books to learn Python programming language.
Python Built-In Functions with Syntax and Examples
by DataFlair Team · May 27, 2019

We have talked about Functions in Python. In that tutorial of Python Functions, we discussed user-defined
functions in Python. But that isn’t all, a list of Python built-in functions that we can toy around with. In this
tutorial on Built-in functions in Python, we will see each of those; we have 67 of those in Python 3.6 with their
Python Syntax and examples.

So, let’s start Python Built-In Functions.

Python Built-In Functions with Syntax and Examples

Python Built-In Functions

1. abs()
The abs() is one of the most popular Python built-in functions, which returns the absolute value of a number. A
negative value’s absolute is that value is positive.

1. >>> abs(-7)

1. >>> abs(7)

1. >>> abs(0)
2. all()
The all() function takes a container as an argument. This Built in Functions returns True if all values in a python
iterable have a Boolean value of True. An empty value has a Boolean value of False.

1. >>> all({'*','',''})

False

1. >>> all([' ',' ',' '])

True

3. any()
Like all(), it takes one argument and returns True if, even one value in the iterable has a Boolean value of True.

1. >>> any((1,0,0))

True

1. >>> any((0,0,0))

False

4. ascii()
It is important Python built-in functions, returns a printable representation of a python object (like a string or a
Python list). Let’s take a Romanian character.

1. >>> ascii('ș')

“‘\\u0219′”

Since this was a non-ASCII character in python, the interpreter added a backslash (\) and escaped it using
another backslash.

1. >>>

ascii('ușor')

“‘u\\u0219or'”

Let’s apply it to a list.

1. >>> ascii(['s','ș'])

“[‘s’, ‘\\u0219’]”

5. bin()
bin() converts an integer to a binary string. We have seen this and other functions in our article on Python
Numbers.

1. >>> bin(7)

‘0b111’

We can’t apply it on floats, though.

1. >>> bin(7.0)

Traceback (most recent call last):

File “<pyshell#20>”, line 1, in <module>

bin(7.0)

TypeError: ‘float’ object cannot be interpreted as an integer

6. bool()
bool() converts a value to Boolean.

1. >>> bool(0.5)

True

1. >>> bool('')

False

1. >>> bool(True)

True

7.bytearray()
bytearray() returns a python array of a given byte size.

1. >>> a=bytearray(4)
2. >>> a

bytearray(b’\x00\x00\x00\x00′)

1. >>> a.append(1)
2. >>> a

bytearray(b’\x00\x00\x00\x00\x01′)

1. >>> a[0]=1
2. >>> a
bytearray(b’\x01\x00\x00\x00\x01′)

1. >>> a[0]

Let’s do this on a list.

1. >>> bytearray([1,2,3,4])

bytearray(b’\x01\x02\x03\x04′)

8. bytes()
bytes() returns an immutable bytes object.

1. >>> bytes(5)

b’\x00\x00\x00\x00\x00′

1. >>> bytes([1,2,3,4,5])

b’\x01\x02\x03\x04\x05′

1. >>> bytes('hello','utf-8')

b’hello’

Here, utf-8 is the encoding.

Both bytes() and bytearray() deal with raw data, but bytearray() is mutable, while bytes() is immutable.

1. >>> a=bytes([1,2,3,4,5])
2. >>> a

b’\x01\x02\x03\x04\x05′

1. >>> a[4]=

Traceback (most recent call last):

File “<pyshell#46>”, line 1, in <module>

a[4]=3

TypeError: ‘bytes’ object does not support item assignment

Let’s try this on bytearray().


1. >>> a=bytearray([1,2,3,4,5])
2. >>> a

bytearray(b’\x01\x02\x03\x04\x05′)

1. >>> a[4]=3
2. >>> a

bytearray(b’\x01\x02\x03\x04\x03′)

9. callable()
callable() tells us if an object can be called.

1. >>> callable([1,2,3])

False

1. >>> callable(callable)

True

1. >>> callable(False)

False

1. >>> callable(list)

True

A function is callable, a list is not. Even the callable() python Built In function is callable.

10. chr()
chr() Built In function returns the character in python for an ASCII value.

1. >>> chr(65)

‘A’

1. >>> chr(97)

‘a’

1. >>> chr(9)

‘\t’

1. >>> chr(48)

‘0’
11. classmethod()
classmethod() returns a class method for a given method.

1. >>> class fruit:


2. def sayhi(self):
3. print("Hi, I'm a fruit")
4.
5. >>> fruit.sayhi=classmethod(fruit.sayhi)
6. >>> fruit.sayhi()

Hi, I’m a fruit

When we pass the method sayhi() as an argument to classmethod(), it converts it into a python class method one
that belongs to the class. Then, we call it like we would call any static method in python without an object.

12. compile()
compile() returns a Python code object. We use Python in built function to convert a string code into object
code.

1. >>> exec(compile('a=5\nb=7\nprint(a+b)','','exec'))

12

Here, ‘exec’ is the mode. The parameter before that is the filename for the file form which the code is read.
Finally, we execute it using exec().

13. complex()
complex() function creates a complex number. We have seen this is our article on Python Numbers.

1. >>> complex(3)

(3+0j)

1. >>> complex(3.5)

(3.5+0j)

1. >>> complex(3+5j)

(3+5j)

14. delattr()
delattr() takes two arguments- a class, and an attribute in it. It deletes the attribute.

1. >>> class fruit:


2. size=7
3.
4. >>> orange=fruit()
5. >>> orange.size

1. >>> delattr(fruit,'size')
2. >>> orange.size

Traceback (most recent call last):

File “<pyshell#95>”, line 1, in <module>

orange.size

AttributeError: ‘fruit’ object has no attribute ‘size’

15. dict()
dict(), as we have seen it, creates a python dictionary.

1. >>> dict()

{}

1. >>> dict([(1,2),(3,4)])

{1: 2, 3: 4}

This was about dict() Python Built In function

16. dir()
dir() returns an object’s attributes.

1. >>> class fruit:


2. size=7
3. shape='round'
4. >>> orange=fruit()
5. >>> dir(orange)

[‘ class ’, ‘ delattr__’, ‘ dict ’, ‘ dir ’, ‘ doc ’, ‘ eq ’, ‘__format ’, ‘ ge ’,


‘ getattribute ’, ‘ gt ’, ‘ hash ’, ‘ init ’, ‘ init_subclass ’, ‘ le ’, ‘ lt ’, ‘ module ’,
‘ ne ’, ‘ new ’, ‘__reduce ’, ‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ sizeof ’, ‘ str ’,
‘ subclasshook ’, ‘ weakref ’, ‘shape’, ‘size’]

17. divmod()
divmod() in Python built-in functions, takes two parameters, and returns a tuple of their quotient and remainder.
In other words, it returns the floor division and the modulus of the two numbers.
1. >>> divmod(3,7)

(0, 3)

1. >>> divmod(7,3)

(2, 1)
If you encounter any doubt in Python Built-in Function, Please Comment.

18. enumerate()
This Python Built In function returns an enumerate object. In other words, it adds a counter to the iterable.

1. >>> for i in enumerate(['a','b','c']):


2. print(i)

(0, ‘a’)
(1, ‘b’)
(2, ‘c’)

19. eval()
This Function takes a string as an argument, which is parsed as an expression.

1. >>> x=7
2. >>> eval('x+7')

14

1. >>> eval('x+(x%2)')

20. exec()
exec() runs Python code dynamically.

1. >>> exec('a=2;b=3;print(a+b)')

1. >>> exec(input("Enter your program"))

Enter your programprint(2+3)

21. filter()
Like we’ve seen in python Lambda Expressios, filter() filters out the items for which the condition is True.
1. >>> list(filter(lambda x:x%2==0,[1,2,0,False]))

[2, 0, False]

22. float()
This Python Built In function converts an int or a compatible value into a float.

1. >>> float(2)

2.0

1. >>> float('3')

3.0

1. >>> float('3s')

Traceback (most recent call last):

File “<pyshell#136>”, line 1, in <module>

float(‘3s’)

ValueError: could not convert string to float: ‘3s’

1. >>> float(False)

0.0

1. >>> float(4.7)

4.7

23. format()
We have seen this Python built-in function, one in our lesson on Python Strings.

1. >>> a,b=2,3
2. >>> print("a={0} and b={1}".format(a,b))

a=2 and b=3

1. >>> print("a={a} and b={b}".format(a=3,b=4))

a=3 and b=4

24. frozenset()
frozenset() returns an immutable frozenset object.
1. >>> frozenset((3,2,4))

frozenset({2, 3, 4})

Read Python Sets and Booleans for more on frozenset.

25. getattr()
getattr() returns the value of an object’s attribute.

1. >>> getattr(orange,'size')

26. globals()
This Python built-in functions, returns a dictionary of the current global symbol table.

1. >>> globals()

{‘ name ’: ‘ main ’, ‘ doc ’: None, ‘ package ’: None, ‘ loader ’: <class


‘_frozen_importlib.BuiltinImporter’>, ‘ spec ’: None, ‘ annotations ’: {}, ‘ builtins ’: <module
‘builtins’ (built-in)>, ‘fruit’: <class ‘ main .fruit’>, ‘orange’: < main .fruit object at 0x05F937D0>, ‘a’:
2, ‘numbers’: [1, 2, 3], ‘i’: (2, 3), ‘x’: 7, ‘b’: 3}

27. hasattr()
Like delattr() and getattr(), hasattr() Python built-in functions, returns True if the object has that attribute.

1. >>> hasattr(orange,'size')

True

1. >>> hasattr(orange,'shape')

True

1. >>> hasattr(orange,'color')

False

28. hash()
hash() function returns the hash value of an object. And in Python, everything is an object.

1. >>> hash(orange)

6263677

1. >>> hash(orange)
6263677

1. >>> hash(True)

1. >>> hash(0)

1. >>> hash(3.7)

644245917

1. >>> hash(hash)

25553952

This was all about hash() Python In Built function

29. help()
To get details about any module, keyword, symbol, or topic, we use the help() function.

1. >>> help()
2.
3. Welcome to Python 3.6's help utility!
4.
5. If this is your first time using Python, you should definitely check out the tutorial on the Internet at
http://docs.python.org/3.6/tutorial/.
6.
7. Enter the name of any module, keyword, or topic to get help on writing Python programs and using
Python modules. To quit this help utility and return to the interpreter, just type "quit".
8.
9. To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords",
"symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the
modules whose name or summary contain a given string such as "spam", type "modules spam".
10.
11. help> map
12. Help on class map in module builtins:
13. class map(object)
14. | map(func, *iterables) --> map object
15. |
16. | Make an iterator that computes the function using arguments from
17. | each of the iterables. Stops when the shortest iterable is exhausted.
18. |
19. | Methods defined here:
20. |
21. | getattribute (self, name, /)
22. | Return getattr(self, name).
23. |
24. | iter (self, /)
25. | Implement iter(self).
26. |
27. | new (*args, **kwargs) from builtins.type
28. | Create and return a new object. See help(type) for accurate signature.
29. |
30. | next (self, /)
31. | Implement next(self).
32. |
33. | reduce (...)
34. | Return state information for pickling.
35. help> You are now leaving help and returning to the Python interpreter.
36. If you want to ask for help on a particular object directly from the
37. interpreter, you can type "help(object)". Executing "help('string')"
38. has the same effect as typing a particular string at the help> prompt.
39. >>>

30. hex()
Hex() Python built-in functions, converts an integer to hexadecimal.

1. >>> hex(16)

‘0x10’

1. >>> hex(False)

‘0x0’

31. id() Function

id() returns an object’s identity.


1. >>> id(orange)

100218832

1. >>> id({1,2,3})==id({1,3,2})

True

32. input()
Input() Python built-in functions, reads and returns a line of string.

1. >>> input("Enter a number")

Enter a number7
‘7’
Note that this returns the input as a string. If we want to take 7 as an integer, we need to apply the int() function
to it.

1. >>> int(input("Enter a number"))

Enter a number7

33. int()
int() converts a value to an integer.

1. >>> int('7')

34. isinstance()
We have seen this one in previous lessons. isinstance() takes a variable and a class as arguments. Then, it
returns True if the variable belongs to the class. Otherwise, it returns False.

1. >>>

isinstance(0,str) False

1. >>> isinstance(orange,fruit)

True

35. issubclass()
This Python Built In function takes two arguments- two python classes. If the first class is a subclass of the
second, it returns True. Otherwise, it returns False.

1. >>> issubclass(fruit,fruit)

True

1. >>> class fruit:


2. pass
3. >>> class citrus(fruit):
4. pass

1. >>> issubclass(fruit,citrus)

False

36. iter()
Iter() Python built-in functions, returns a python iterator for an object.

1. >>> for i in iter([1,2,3]):


2. print(i)

1
2
3

37. len()
We’ve seen len() so many times by now. It returns the length of an object.

1. >>> len({1,2,2,3})

Here, we get 3 instead of 4, because the set takes the value ‘2’ only once.

38. list()
list() creates a list from a sequence of values.

1. >>> list({1,3,2,2})

[1, 2, 3]

39. locals()
This function returns a dictionary of the current local symbol table.

1. >>> locals()

{‘ name ’: ‘ main ’, ‘ doc ’: None, ‘ package ’: None, ‘ loader ’: <class


‘_frozen_importlib.BuiltinImporter’>, ‘ spec ’: None, ‘ annotations ’: {}, ‘ builtins ’: <module
‘builtins’ (built-in)>, ‘fruit’: <class ‘ main .fruit’>, ‘orange’: < main .fruit object at 0x05F937D0>, ‘a’:
2, ‘numbers’: [1, 2, 3], ‘i’: 3, ‘x’: 7, ‘b’: 3, ‘citrus’: <class ‘ main .citrus’>}

40. map()
Like filter(), map() Python built-in functions, takes a function and applies it on an iterable. It maps True or False
values on each item in the iterable.

1. >>> list(map(lambda x:x%2==0,[1,2,3,4,5]))

[False, True, False, True, False]

41. max()
A no-brainer, max() returns the item, in a sequence, with the highest value of all.
1. >>> max(2,3,4)

1. >>> max([3,5,4])

1. >>> max('hello','Hello')

‘hello’

42. memoryview()
memoryview() shows us the memory view of an argument.

1. >>> a=bytes(4)
2. >>> memoryview(a)

<memory at 0x05F9A988>

1. >>> for i in memoryview(a):


2. print(i)

43. min()
min() returns the lowest value in a sequence.

1. >>> min(3,5,1)

1. >>> min(True,False)

False

44. next()
This Python Built In function returns the next element from the iterator.

1. >>> myIterator=iter([1,2,3,4,5])
2. >>> next(myIterator)

1. >>> next(myIterator)

1. >>> next(myIterator)
3

1. >>> next(myIterator)

1. >>> next(myIterator)

Now that we’ve traversed all items, when we call next(), it raises StopIteration.

1. >>> next(myIterator)

Traceback (most recent call last):

File “<pyshell#392>”, line 1, in <module>

next(myIterator)

StopIteration

45. object()
Object() Python built-in functions, creates a featureless object.

1. >>> o=object()
2. >>> type(o)

<class ‘object’>

1. >>> dir(o)

[‘ class ’, ‘ delattr__’, ‘ dir ’, ‘ doc ’, ‘ eq ’, ‘ format ’, ‘ ge ’, ‘ getattribute ’,


‘ gt ’, ‘ hash ’, ‘__init ’, ‘ init_subclass ’, ‘ le ’, ‘ lt ’, ‘ ne ’, ‘ new ’, ‘ reduce ’,
‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ sizeof ’, ‘ str ’, ‘ subclasshook ’] Here, the function
type() tells us that it’s an object. dir() tells us the object’s attributes. But since this does not have the dict
attribute, we can’t assign to arbitrary attributes.

46. oct()
oct() converts an integer to its octal representation.

1. >>> oct(7)

‘0o7’

1. >>> oct(8)

‘0o10’
1. >>> oct(True)

‘0o1’

47. open()
open() lets us open a file. Let’s change the current working directory to Desktop.

1. >>> import os
2. >>>

os.chdir('C:\\Users\\lifei\\Desktop') Now, we

open the file ‘topics.txt’.

1. >>> f=open('topics.txt')
2. >>> f

<_io.TextIOWrapper name=’topics.txt’ mode=’r’ encoding=’cp1252′>

1. >>> type(f)

<class ‘_io.TextIOWrapper’>
To read from the file, we use the read() method.

1. >>> print(f.read())
2. DBMS mappings
3. projection
4. union
5. rdbms vs dbms
6. doget dopost
7. how to add maps
8. OOT
9. SQL queries
10. Join
11. Pattern programs

Output

Default constructor in inheritance

48. ord()
The function ord() returns an integer that represents the Unicode point for a given Unicode character.

1. >>> ord('A')

65

1. >>> ord('9')

57
This is complementary to chr().
1. >>> chr(65)

‘A’

49. pow()
pow() takes two arguments- say, x and y. It then returns the value of x to the power of y.

1. >>> pow(3,4)

81

1. >>> pow(7,0)

1. >>> pow(7,-1)

0.14285714285714285

1. >>> pow(7,-2)

0.02040816326530612

50. print()
We don’t think we need to explain this anymore. We’ve been seeing this function since the beginning of this
article.

1. >>> print("Okay, next function,

please!") Okay, next function, please!

51. property()
The function property() returns a property attribute. Alternatively, we can use the syntactic sugar @property.
We will learn this in detail in our tutorial on Python Property.

52. range()
We’ve taken a whole tutorial on this. Read up range() in Python.

1. >>> for i in range(7,2,-2):


2. print(i)

7
5
3

53. repr()
repr() returns a representable string of an object.

1. >>> repr("Hello")

“‘Hello'”

1. >>> repr(7)

‘7’

1. >>> repr(False)

‘False’

54. reversed()
This functions reverses the contents of an iterable and returns an iterator object.

1. >>> a=reversed([3,2,1])
2. >>> a

<list_reverseiterator object at 0x02E1A230>

1. >>> for i in a:
2. print(i)

1
2
3

1. >>> type(a)

<class ‘list_reverseiterator’>

55. round()
round() rounds off a float to the given number of digits (given by the second argument).

1. >>> round(3.777,2)

3.78

1. >>> round(3.7,3)

3.7

1. >>> round(3.7,-1)

0.0

1. >>> round(377.77,-1)
380.0
The rounding factor can be negative.

56. set()
Of course, set() returns a set of the items passed to it.

1. >>> set([2,2,3,1])

{1, 2, 3}
Remember, a set cannot have duplicate values, and isn’t indexed, but is ordered. Read on Sets and Booleans for
the same.

57. setattr()
Like getattr(), setattr() sets an attribute’s value for an object.

1. >>> orange.size

1. >>> orange.size=8
2. >>> orange.size

58. slice()
slice() returns a slice object that represents the set of indices specified by range(start, stop, step).

1. >>> slice(2,7,2)

slice(2, 7, 2)
We can use this to iterate on an iterable like a string in python.

1. >>> 'Python'[slice(1,5,2)]

‘yh’

59. sorted()
Like we’ve seen before, sorted() prints out a sorted version of an iterable. It does not, however, alter the
iterable.

1. >>> sorted('Python')

[‘P’, ‘h’, ‘n’, ‘o’, ‘t’, ‘y’]

1. >>> sorted([1,3,2])
[1, 2, 3]

60. staticmethod()

staticmethod() creates a static method from a function. A static method is bound to a class rather than to an
object. But it can be called on the class or on an object.

1. >>> class fruit:


2. def sayhi():
3. print("Hi")
4. >>> fruit.sayhi=staticmethod(fruit.sayhi)
5. >>> fruit.sayhi()

Hi
You can also use the syntactic sugar @staticmethod for this.

1. >>> class fruit:


2. @staticmethod
3. def sayhi():
4. print("Hi")
5. >>> fruit.sayhi()

Hi

61. str()
str() takes an argument and returns the string equivalent of it.

1. >>> str('Hello')

‘Hello’

1. >>> str(7)

‘7’

1. >>> str(8.7)

‘8.7’

1. >>> str(False)

‘False’

1. >>> str([1,2,3])

‘[1, 2, 3]’

62. sum()
The function sum() takes an iterable as an argument, and returns the sum of all values.
1. >>> sum([3,4,5],3)

15

63. super()
super() returns a proxy object to let you refer to the parent class.

1. >>> class person:


2. def init (self):
3. print("A person")
4. >>> class student(person):
5. def init (self):
6. super(). init ()
7. print("A student")
8. >>> Avery=student()

A person

A student

64. tuple()
As we’ve seen in our tutorial on Python Tuples, the function tuple() lets us create a tuple.

1. >>> tuple([1,3,2])

(1, 3, 2)

1. >>> tuple({1:'a',2:'b'})

(1, 2)

65. type()
We have been seeing the type() function to check the type of object we’re dealing with.

1. >>> type({})

<class ‘dict’>

1. >>> type(set())

<class ‘set’>

1. >>> type(())

<class ‘tuple’>

1. >>> type((1))
<class ‘int’>

1. >>> type((1,))

<class ‘tuple’>

66. vars()
vars() function returns the dict attribute of a class.

1. >>> vars(fruit)

mappingproxy({‘ module ’: ‘ main ’, ‘size’: 7, ‘shape’: ’round’, ‘ dict ’: <attribute ‘ dict ’ of


‘fruit’ objects>, ‘ weakref ’: <attribute ‘ weakref ’ of ‘fruit’ objects>, ‘ doc ’: None})

67. zip()
zip() returns us an iterator of tuples.

1. >>> set(zip([1,2,3],['a','b','c']))

{(1, ‘a’), (3, ‘c’), (2, ‘b’)}

1. >>> set(zip([1,2],[3,4,5]))

{(1, 3), (2, 4)}

1. >>> a=zip([1,2,3],['a','b','c'])

To unzip this, we write the following code.

1. >>> x,y,z=a
2. >>> x

(1, ‘a’)

1. >>> y

(2, ‘b’)

1. >>> z

(3, ‘c’)
Isn’t this just like tuple unpacking?

So, this was all about Python Built-in Functions. Hope you like our explanation.

3. Conclusion
Phew, was that too much for once? It may be overwhelming at once, but as you will get using these python
Built-in functions, you will get used to them. If you have any query regarding Python built-in functions, Please
Comment.

Python Method – Classes, Objects and Functions in Python


by DataFlair Team · May 27, 2019

1. Python Method – Objective

In our last tutorial, we discussed functions in Python. Today, in this Python Method Tutorial, we will discuss
what is a method in Python Programming Language. Moreover, we will learn Python Class Method and Python
Object. Along with this, we will study the python functions.

So, let’s start Python Class and Object.

Python Method

2. Introduction to Python Method

You are aware of the fact that Python is an object-oriented language, right? This means that it can deal with
classes and objects to model the real world. A Python method is a label that you can call on an object; it is a
piece of code to execute on that object. But before we begin getting any deeper, let’s take a quick look at classes
and objects and wherever you encounter any doubt in the Python Method, please ask in the comment.

3. Python Class Method

A Python Class is an Abstract Data Type (ADT). Think of it like a blueprint. A rocket made from referring to
its blueprint is according to plan. It has all the properties mentioned in the plan, and behaves accordingly.
Likewise, a class is a blueprint for an object. To take an example, we would suggest thinking of a car. The class
‘Car’ contains properties like brand, model, color, fuel, and so. It also holds behavior like start(), halt(), drift(),
speedup(), and turn(). An object Hyundai Verna has the following properties then.

brand: ‘Hyundai’

model: ‘Verna’

color: ‘Black’

fuel: ‘Diesel’

Here, this is an object of the class Car, and we may choose to call it ‘car1’ or ‘blackverna’.

1. >>> class Car:


2. def init (self,brand,model,color,fuel):
3. self.brand=brand
4. self.model=model
5. self.color=color
6. self.fuel=fuel
7. def start(self):
8. pass
9. def halt(self):
10. pass
11. def drift(self):
12. pass
13. def speedup(self):
14. pass
15. def turn(self):
16. pass

4. Python Objects

A Python object is an instance of a class. It can have properties and behavior. We just created the class Car.
Now, let’s create an object blackverna from this class. Remember that you can use a class to create as many
objects as you want.

1. >>> blackverna=Car('Hyundai','Verna','Black','Diesel')

This creates a Car object, called blackverna, with the aforementioned attributes. We did this by calling the class
like a function (the syntax). Now, let’s access its fuel attribute. To do this, we use the dot operator in Python(.).

1. >>> blackverna.fuel

‘Diesel’

5. Python Method

A Python method is like a Python function, but it must be called on an object. And to create it, you must put it
inside a class. Now in this Car class, we have five methods, namely, start(), halt(), drift(), speedup(), and turn().
In this example, we put the pass statement in each of these, because we haven’t decided what to do yet. Let’s
call the drift() Python method on blackverna.

1. >>> blackverna.drift()
2. >>>

Like a function, a method has a name, and may take parameters and have a return statement. Let’s take an
example for this.

1. >>> class Try:


2. def init (self):
3. pass
4. def printhello(self,name):
5. print(f"Hello, {name}")
6. return name
7. >>> obj=Try()
8. >>> obj.printhello('Ayushi')

Hello, Ayushi

‘Ayushi’

Here, the method printhello() has a name, takes a parameter, and returns a value.

 An interesting discovery– When we first defined the class Car, we did not pass the ‘self’ parameter to
the five methods of the class. This worked fine with the attributes, but when we called the drit() method
on blackverna, it gave us this error:

Traceback (most recent call last):

File “<pyshell#19>”, line 1, in <module>

blackverna.drift()

TypeError: drift() takes 0 positional arguments but 1 was given

From this error, we figured that we were missing the ‘self’ parameter to all those methods. Then we added it to
all of them, and called drift() on blackverna again. It still didn’t work.

Finally, we declared the blackverna object again, and then called drift() on it. This time, it worked without an
issue. Make out of this information what you will.

6. init ()

If you’re familiar with any other object-oriented language, you know about constructors. In C++, a constructor
is a special function, with the same name as the class, used to initialize the class’ attributes. Here in Python,
init () is the method we use for this purpose. Let’s see the init part of another class.

1. >>> class Animal:


2. def init (self,species,gender):
3. self.species=species
4. self.gender=gender
5. >>> fluffy=Animal('Dog','Female')
6. >>> fluffy.gender

‘Female’

Here, we used init to initialize the attributes ‘species’ and ‘gender’.

However, you don’t need to define this function if you don’t need it in your code.

1. >>> class Try2:


2. def hello(self):
3. print("Hello")
4. >>> obj2=Try2()
5. >>> obj2.hello()

Init is a magic method, which is why it has double underscores before and after it. We will learn about magic
methods in a later section in this article.

7. Python Self Parameter

You would have noticed until now that we’ve been using the ‘self’ parameter with every method, even the
init (). This tells the interpreter to deal with the current object. It is like the ‘this’ keyword in Java. Let’s
take another code to see how this works.

1. >>> class Fruit:


2. def printstate(self,state):
3. print(f"The orange is {state}")
4. >>> orange=Fruit()
5. >>> orange.printstate("ripe")

The orange is ripe

As you can see, the ‘self’ parameter told the method to operate on the current object, that is, orange. Let’s take
another example.

1. >>> class Result:


2. def init (self,phy,chem,math):
3. self.phy=phy
4. self.chem=chem
5. self.math=math
6. def printavg(self):
7. print(f"Average={(self.phy+self.chem+self.math)/3}")
8. >>> rollone=Result(86,95,85)
9. >>> rollone.chem

95

1. >>> rollone.printavg()

Average=88.66666666666667
You can also assign values directly to the attributes, instead of relying on arguments.

1. >>> class LED:


2. def init (self):
3. self.lit=False
4. >>> obj=LED()
5. >>> obj.lit

False

Finally, we’d like to say that ‘self’ isn’t a keyword. You can use any name instead of it, provided that it isn’t a
reserved keyword, and follows the rules for naming an identifier.

1. >>> class Try3:


2. def init (thisobj,name):
3. thisobj.name=name
4. >>> obj1=Try3('Leo')
5. >>> obj1.name

‘Leo’

This was all about the Python Self Parameter

Any doubt yet in Python Method? Please Comment.

8. Python Functions vs Method

We think we’ve learned enough about methods by now to be able to compare them to functions. A function
differs from a method in the following ways.

1. While a method is called on an object, a function is generic.


2. Since we call a method on an object, it is associated with it. Consequently, it is able to access and
operate on the data within the class.
3. A method may alter the state of the object; a function does not, when an object is passed as an argument
to it. We have seen this in our tutorial on tuples.

9. Python Magic Methods

Another construct that Python provides us with is Python magic methods. Such a method is identified by double
underscores before and after its name. Another name for a magic method is a dunder.

A magic method is used to implement functionality that can’t be represented as a normal method. init ()
isn’t the only magic method in Python; we will read more about it in a future lesson. But for now, we’ll just
name some of the magic methods:

add for +

sub for –

mul for *
and for &

The list, however, does not end here.

This was all about the Python Method.

10. Conclusion: Python Method

A Python method, as we know it, is much like a function, except for the fact that it is associated with an object.
Now you know how to define a method, and make use of the init method and the self-parameter, or
whatever you choose to call it. Don’t forget to revise the various methods we discussed in our tutorials on
Python lists, tuples, strings, sets, and dictionaries in python. If you have any doubt regarding Python
Method, Please Ask.

Difference Between Method and Function in Python | Python Method


Vs Function
by DataFlair Team · September 26, 2018

1. Python Method Vs Function – Objective

In our journey with Python, we’ve seen the Python method and function. But don’t they feel the same? Well
then, time to make a comparison between Python function and Method with examples. But before that, let’s
revise both – function and method in Python.

So, let’s start Difference Between Method and Function in Python.

Difference Between Method and Function in Python | Python Method Vs Function

Difference Between methods and function in python


2. Python Functions – A Revision

Python function is a sequence of statements that execute in a certain order, given a name. They let us implement
code reusability. When we talked about Python Functions, we talked about built-in and user-defined functions.

a. User-Defined Functions
Like any other programming language, Python lets us define our own functions. Let’s take an example.

1. >>> def add(a,b):


2. return a+b
3. >>> add(3,-3)

We call this function ‘add’. As expected, it adds two values, which it takes as arguments. When we call it with
values -3 and 3, it returns 0. And as you can see, the ‘return’ statement returns a value from Python function.

These let us create a modular program, which in turn lets us make a change easily whenever we want to.

To learn more about functions, docstrings, and scope and lifetime, read up on Python Functions.

b. Built-in Functions
In our write-up on Python In-Built Functions, we discussed about 66 functions in brief. These are the
functions that Python provides us with, and some of these, we see and use commonly. Let’s take a look at a
program that uses some of these.

1. >>> def demofunc(a,b):


2. """
3. //This function is to demonstrate a few built-in functions in Python
4. """
5. print("Begin")
6. print(max(a,b))
7. print(abs(a),abs(b))
8. print(float(a),b)
9. print(callable(a))
10. print(hash(a),hash(b))
11. print(len('ab'))
12. print(type(a))
13. for i in range(2,4): print(i)
14. >>> demofunc(2,3)

Begin

23

2.0 3
False

23

<class ‘int’>

1. >>> c=lambda :print("Hi")


2. >>> type(c)

<class ‘function’>
Also see, ython Lambda Expressions.

3. Python Methods – A Revision

Python method is like a function, except it is attached to an object. We call a method on an object, and it
possibly makes changes to that object. A method, then, belongs to a class. Let’s take an example.

1. >>> class vehicle:


2. def init (self,color):
3. self.color=color
4. def start(self):
5. print("Starting engine")
6. def showcolor(self):
7. print(f"I am {self.color}")
8. >>> car=vehicle('black')
9. >>> car.start()

Starting engine

1. >>> car.showcolor()

I am black
First, we defined class ‘vehicle’. Then, we created the object ‘car’ from this blueprint. Here, init () is a
magic method that serves as a constructor for the class. Method start() and showcolor() let us do what we
want to. Remember that Python method must have a parameter ‘self’ to allow them to refer to the current
object.

4. Comparison Between Method and Function in Python

Now that we’ve revised the Python method and function, we can compare them. Let’s list down the major
differences.

1. Python method is called on an object, unlike a function. In our example above, we call start() on the
object ‘car’. Conversely, we call Python function quite generically- we don’t call it on any object. Since
we call a method on an object, it can access the data within it.
2. A method may alter an object’s state, but Python function usually only operates on it, and then prints
something or returns a value.

So, this was all about Difference Between Method and Function in Python. Hope you like our explanation

5. Conclusion

After this discussion, we conclude that there’s a thin line between method and function in python with
examples. The only major difference is that we call Python method on an object, but it’s not the same with
functions. Also, methods may modify an object; Python functions don’t.

Python Constructor- Parameterized and Non-Parameterized


by DataFlair Team · May 27, 2019

1. Objective
In our last Python tutorial, we discussed Python Identifiers. Today, we will learn about init (self), the
Python Constructor. Moreover, we will take a look at what we can do for it, and will learn about parameterized
and non-parameterized Python Constructors. Also, we will learn about object creation and initialization and will
investigate if it is possible to overload constructors.
So, let’s start the Python Constructor tutorial.

Python Constructor- Parameterized and Non-Parameterized

2. What is a Constructor in Python?


Python Constructor in object-oriented programming with Python is a special kind of method/function we use
to initialize instance members of that class. We can also use it to ensure we have enough resources. Whenever
we create an object of that class, the constructor definition is the first to execute. The way to initialize the value
of a class attribute without a constructor:

1. >>> class three:


2. val=7
3. >>> three.val

Output – 7
Let’s take a look at Python Slice Constructor
We can also do this inside class functions:

1. >>> class three:


2. def func(self,val):
3. self.val=val
4. >>> t=three()
5. >>> t.func(8)
6. >>> t.val

Output – 8

1. >>> t.func(6) #Also lets us re-initialize attributes


2. >>> t.val

Output – 6
Or we can ask the user for input.

1. >>> class three:


2. def init (self):
3. self.val=input("What value?")
4. >>> t=three()

What value?8

1. >>> t.val

Output – ‘8’

3. Declaring a Python Constructor

a. Object Creation
new is a static class method that lets us control object creation. Whenever we make a call to the class
constructor, it makes a call to new . While this is a default function for every class, we can definitely play
around with it.

1. >>> class demo:


2. def new (self):
3. return 'dataflair'
4. >>> d=demo()
5. >>> type(d)
Output – <class ‘str’>
Let’s learn about Python Syntax

b. Object Initialization (and self-parameter)


A constructor is essentially a class function with its name surrounded by double underscores ( ). We always
call it init (). In C++ or Java, a constructor has the same name as its class, but things aren’t the same with
Python. Constructors let us initialize an object. Let’s take an example.

1. >>> class citrus:


2. def init (self):
3. self.detoxifying=True
4. def show(self):
5. print("I detoxify") if self.detoxifying==True else print("I do not detoxify")
6. >>> kumquat=citrus()
7. >>> kumquat.show()

Output – I detoxify
Now statement by statement, let’s see what’s happening.

 init () is the constructor here.


 It takes the self-keyword to tell the interpreter to work with attributes of this object. It should be the first
parameter.
 It takes no parameters.
 When setting detoxifying to true, we use the self-keyword.
 We have another method show(), we pass self to this too.
 Then, we create an object as kumquat=citrus().
 Finally, we make a call to kumquat.show().

Together, both new and init form a constructor.

c. New Attributes
We can also create a new attribute exclusively for this object and read it when defining values. There is not
much you can do once you have already defined the object.

1. >>> kumquat.color='orange'
2. >>> print("I am ",kumquat.color)

Output – I am orange

4. Not Declaring a Python Constructor


So what happens when we do not explicitly provide a constructor to a class? Can Python handle it? Why don’t
we try that out?

1. >>> class color:


2. def show(self):
3. print("You can see me")
4. >>> orange=color()
5. >>> orange.show()

Output – You can see me


Here, we did not define a constructor, but Python instantiated that object anyway! This must mean it provides a
default constructor that shows up when we do not provide any.
Let’s take a tour to Python Copy

5. Types of Python Constructors


We observe three types of Python Constructors, two of which are in our hands. Let’s begin with the one that
isn’t.

a. Default Constructor in Python


A constructor that Python lends us when we forget to include one. This one does absolutely nothing but
instantiates the object; it is an empty constructor- without a body.

1. >>> class demo:


2. def show(self):
3. print("Thank you for instantiating me :)")
4. >>> d=demo()
5. >>> d.show()

Output – Thank you for instantiating me

b. Non- Parameterized Constructor in Python


When we want a constructor to do something but none of that is to manipulate values, we can use a non-
parameterized constructor.
Let’s try the previous example with this!

1. >>> class demo:


2. def init (self):
3. print("Thank you for instantiating me :)")
4. >>> d=demo()

Output – Thank you for instantiating me


We can also use this constructor to set values we’ve decided. Watch how.

1. >>> class demo:


2. def init (self):
3. self.color='red'
4. self.drink='tea'
5. def hello(self):
6. print(f"Thank you for instantiating me, I'm all {self.color}. Would you like some {self.drink}? :)")
7. >>> d=demo()
8. >>> d.hello()
Output – Thank you for instantiating me, I’m all red. Would you like some tea?

c. Parameterized Constructor in Python


This lets us set custom values for instance variables. We can have any number of these.

1. >>> class demo:


2. def init (self,age,country):
3. self.age=age
4. self.place=country
5. def hello(self):
6. print(f"Hello, I am a {self.age}yo from {self.place}")
7. >>> d=demo(22,'Romania')
8. >>> d.hello()

Output – Hello, I am a 22yo from Romania

6. No Constructor Overloading in Python

a. More than One Python Constructor


If you give it more than one constructor, that does not lead to constructor overloading in Python.

1. >>> class one:


2. def init (self):
3. print("First constructor")
4. def init (self):
5. print("Second
constructor") 6. >>> o=one()

Output – Second constructor

b. Two Different Kinds of Constructors in Python


Not even if you try two different kinds of constructors:

1. >>> class one:


2. def init (self):
3. print("First constructor")
4. def init (self,val):
5. self.val=val
6. print("Second constructor",val)
7. >>> o=one()

Traceback (most recent call last):


File “<pyshell#58>”, line 1, in <module>
o=one()
TypeError: init () missing 1 required positional argument: ‘val’
What this means is Python rebinds the name init to the new method. This means the first declaration of
this method is inaccessible now. Internally, new is the constructor that returns a valid and unpopulated
object on which to call __init .
Have a look at Python Assert Statements

c. Using Default Arguments


Even the following piece of code is simply the use of default arguments, not constructor overloading:

1. >>> class one:


2. def init (self,a=1,b=2):
3. print(a+b)
4. >>> o=one(2)

Output – 4

1. >>> o1=one(2,3)

Output – 5

1. >>> o2=one()

Output – 3
So, this was all in Python Constructor tutorial. Hope you like our explanation.

7. Conclusion – Python Constructor


Who knew there was so much to know about constructors in Python? We learned about parameterized and non-
parameterized Python Constructors, the default Python Constructor, the self-keyword, object creation, and
object initialization. Also, we saw that there is no such thing as constructor overloading in Python. Still, if you
have any query, feel free to ask in the comment tab.

Python Range – Range() Function in Python


by DataFlair Team · August 19, 2019

1. Python Range Function Tutorial

Something you’ve always seen with a for loop, python range() function is a handy feature in Python. Like we
saw in our tutorial on Python Loops, range function in python provides us with a list of numbers to iterate on.
Actually, it returns a range object, which we then convert to a list to iterate on.

So, let’s begin the Python Range Function Tutorial.


Python Range Function

2. Introduction to Range in Python

The range() function is an inbuilt function in Python, and it returns a range object. First, let’s see what it looks
like.

Learn: Python Functions with Syntax and Examples

3. Syntax of Python range() function

The Range function in python takes from one to three arguments. Let’s see how.

a. One Parameter
For an argument n, the function returns integer values from 0 to n-1.

range(stop)

Let’s take an example to be clearer.

1. >>> list(range(3))

[0, 1, 2]

What happens if you provide a negative value?

1. >>> list(range(-3))

[]
The fact that it returned an empty list tells us that range() is internally coded to traverse to the right. And there
are no integers from 0 to the right -3, because -3 falls to the left of 0.

1. >>> list(range(0))

[]

Learn: Python Dictionaries with Methods, Functions and Dictionary Operations

b. Two Parameters
Now, we’ll try different combinations for two parameters.

range(start,stop)

1. >>> list(range(1,7))

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

Here, both arguments are positive. Also, 7 falls to the right of 1. So, it prints integers from 1 to 6 (7-1). Hence,
we conclude that it prints integers from the first number to one from the second.

Now we try passing arguments where the first is greater than the second.

1. >>> list(range(7,1))

[] To back what we just said, it returns an empty list because 7 falls to the right of 1, and it traverses to the right
from 7. Therefore, it never reaches 1.

In section a, we saw that we can’t pass a negative argument to range(). But it is indeed possible to pass a
negative value or two when you pass two or more arguments. Let’s see how.

1. >>> list(range(-7,3))

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

1. >>> list(range(-7,-3))

[-7, -6, -5, -4]

1. >>> list(range(-3,-7))

[]

This, again, returns an empty list, for -3 lies to the right of -7.

1. >>> list(range(3,3))

[]

There are no integers between 3 and less than 3.


c. Three Parameters
Finally, the range() function can also take a third parameter. This is for the interval.
range(start,stop,interval)

We’ll see this one by example.

1. >>> list(range(7,1,-1))

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

After bragging about how you can’t have the second argument smaller than the first one, now we tell you that
you can. But on one condition- you must specify a negative interval. Here, we used -1 as an interval. We
could’ve used -2 as well.

1. >>> list(range(7,1,-2))

[7, 5, 3]

1. >>> list(range(7,1,-8))

[7]

Here, note that the first integer, 7, is always returned, even though the interval -8 sends it beyond 1. Let’s now
take a look at more examples for three parameters.

1. >>> list(range(1,7,1.5))

Traceback (most recent call last):

File “<pyshell#17>”, line 1, in <module>

list(range(1,7,1.5))

TypeError: ‘float’ object cannot be interpreted as an integer

Note that all three arguments must be integers only.

1. >>> list(range(1,7,2))

[1, 3, 5]

Let’s take another example.

1. >>> list(range(1,7,0))

Traceback (most recent call last):

File “<pyshell#26>”, line 1, in <module>

list(range(1,7,0))
ValueError: range() arg 3 must not be zero

It raised a value error, because the interval cannot be zero if you need to go from one number to another.

Actually, when we provide one or two arguments, the interval is assumed to be +1. This is why we were unable
to have it traverse to the left. Now that you know what’s going on, it is easier to toy with range().

Let’s take one last example to make sure we’re on the right train of thoughts.

1. >>> list(range(12,2,2))

[]

Now this returns an empty list in python because in a positive interval of 2 means traversing to the right, but 2
falls to the left of 12.

4. Python Iterate Function

Now that we know how to use Python range() function, don’t you want to know where to use it? Well, one
application is the for loop.

1. >>> for i in range(6):


2. print(i*2)

0
2
4
6
8
10
So, this was all about Python Range Function. Hope you like our explanation.

5. Conclusion

To sum this tutorial up on Python Range, range function in python is an in-built function, in Python, that lends
us a sequence of integers. We can use this to iterate on using a for loop. Now you know that it’s possible to call
range() in python with one, two, or three arguments. We would like you to come up with a creative use of the
range() function; tell us in the comments.

Python Zip Function With Examples | Python Unzipping values


by DataFlair Team · September 29, 2018

1. Objective – Python Zip Function

In this Python tutorial, we will discuss Python Zip Function. Moreover, we will discuss the built-in zip()
function in Python with the example. Also, we will understand Zip in Python and Python Unzipping Values.

So, let’s start the Python Zip Function tutorial.


Python Zip Function With Examples | Python Unzipping values

2. What is Python Zip Function?

Like we’ve said manifold before, the interpreter for Python has some types and functions built into it; these are
the ones always available to it. zip() is one such function, and we saw a brief on it when we talked Built-in
Functions. Let’s take a quick recap before we can proceed to explain this to you from scratch.

zip() is a built-in Python function that gives us an iterator of tuples. Let’s take a quick example of Python Zip
Function

1. >>> for i in zip([1,2,3],['a','b','c']):


2. print(i)

(1, ‘a’)
(2, ‘b’)
(3, ‘c’)

3. Understanding Python zip()


Like Ziploc in the real world and .zip files in the virtual one, a zip is a kind of a container. Like a .zip file holds
real files within itself, a zip holds real data within. It takes iterable elements as input and returns an iterator on
them (an iterator of tuples). It evaluates the iterables left to right.

Have a look at Python NumPy Tutorial.

a. The syntax for Python Zip Function


Python zip() function has the following syntax-

zip(*iterables)

As arguments, it can take iterables, we see. These can be built-in like the list, string, dict, and user-defined
(objects with the iter method).
b. Python Zip Function Example
 No arguments

What happens when we provide no arguments to zip()?

1. >>> set(zip())

set()
You can see that this returns an empty iterator.

 Single argument

1. >>> for i in zip([1,2,3]):


2. print(i)

(1,)
(2,)
(3,)
This returns tuples holding single values

 Multiple arguments of the same lengths

So, let’s pass this two lists of equal lengths.

>>> for i in zip([1,2,3],['a','b','c']):

1. print(i)

(1, ‘a’)
(2, ‘b’)
(3, ‘c’)
This zips elements together from each list. How about more than two?

1. >>> for i in zip([1,2,3],['a','b','c'],['#','*','$']):


2. print(i)

(1, ‘a’, ‘#’)


(2, ‘b’, ‘*’)
(3, ‘c’, ‘$’)

 Multiple arguments of different lengths

When we provide multiple lists of different lengths, it stops at the shortest one.

1. >>> set(zip([1,2],[3,4,5]))

{(1, 3), (2, 4)}


If you want to keep those, you can borrow zip_longest() from itertools.
1. >>> from itertools import zip_longest as zl
2. >>> set(zl([1,2],[3,4,5]))

{(1, 3), (2, 4), (None, 5)}

4. Unzipping Values in Python

Now we know how to zip values together. But how to unzip them? Well, we use the * character with the zip()
function.

1. >>> z=zip([1,2,3],['a','b','c'],['#','*','$'])
2. >>> a,b,c=zip(*z)
3. >>> a,b,c

((1, 2, 3), (‘a’, ‘b’, ‘c’), (‘#’, ‘*’, ‘$’))


So, this unzips the zip object z into the variables a, b, and c.

1. >>> z=zip([1,2],[3,4,5])
2. >>> a,b=zip(*z)
3. >>> a,b

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


Now, notice that this dropped the element 5 because it didn’t zip into anything anyway.
So, this was all in Python Zip Function. Hope you like our explanation.

5. Conclusion – Python Zip Function

Hence, in this Python Zip tutorial, we discussed Python Zip Functions in detail. Moreover, we saw Zip in
Python with Python Zip function example and unzipping values in Python. Furthermore, while learning Python
Zip Function, if you feel any query, ask in comments.

Python eval Function – Examples & Uses


by DataFlair Team · November 15, 2018

1. Objective – Python eval Function

Today, in this Python tutorial, we will see Python eval function. Moreover, we will understand the eval
function in Python with examples. Also, we will look at uses and vulnerabilities in eval().

So, let’s start the Python eval tutorial.


Python eval Function – Examples & Uses

2. What is Python eval Function?

eval() in Python is a built-in function or method, to which we pass an expression. It parses this expression and
runs it as we execute the program. Let’s take a look at the syntax first.

a. The syntax of Python eval() Function


Observe the following syntax for eval function in Python:

1. eval(expression, globals=None, locals=None)

What does this tell us about Python eval()? What are the parameters of Python eval Function?

Before going on you must read the facts about Python Functions

b. Python eval Function with Parameters


 Expression in Python eval()- This is the string to parse and evaluate
 Globals in eval()- This is a dictionary that holds available global methods and variables, and is an
optional parameter
 Python eval Locals- This is a mapping object that holds available local methods and variables, and is an
optional parameter. We know that the standard mapping type in Python is a dictionary.

3. Python eval Example

Let’s take a simple example of Python eval() Function.

1. >>> x=7
2. >>> eval('x**2')
49

In this eval function in Python example, we initialize x to 7. We then pass a string to eval that, we expect, will
square the value of x and stuff it into x. We see that it works fine. eval in Python evaluates the expression x**2
to get to the value 49 and then prints it.
Now, let’s try another Python eval() example.

Have a look at Python Variables

1. >>> eval('[2,3,4][1]')

3
This converts the string to the list [2,3,4] and returns the value at position 1, that is, 3.

However, eval() in Python does not compile, only evaluates:

1. >>> eval('if 3>1: print("Okay")')

Traceback (most recent call last):


File “<pyshell#59>”, line 1, in <module>
eval(‘if 3>1: print(“Okay”)’)
File “<string>”, line 1
if 3>1: print(“Okay”)
^
SyntaxError: invalid syntax

4. Getting the Expression From the User

In the examples so far, we hardcoded the expression. What if we wanted to let the user provide one instead?

1. >>> expr=input('Enter an expression as x')

Enter an expression as x3*x**3+2*x**2+x+6

1. >>> x=int(input('Enter the value of x'))

Enter the value of x2

Do you know about Python Syntax

1. >>> eval(expr)

40

Here, we take the expression from the user, then we take the value of x and convert it to an integer. Finally, we
call eval() on the expression and evaluate it to reach a value of 40.

5. Vulnerabilities With Eval in Python

So this is useful, but it can also be used against us since it executes anything we pass to it(somewhat like SQL
injection). Let’s see how. The user can:
 Call a dangerous function
 Expose hidden values

Vulnerabilities With Python eval Function

a. Exploiting Eval in Python


Imagine having a function in your code that returns some kind of password or other confidential data. A user
can make a call to this function through Python eval() and make their way to the piece of sensitive data.

Clear your concepts for errors and exceptions in Python

1. >>> def let_me_in():


2. password='@dc#431'
3. print("The password is",password)
4. >>> expr=input('Enter an expression as x')

Enter an expression as xlet_me_in()

1. >>> eval(expr)

The password is @dc#431

Did you see how easy it was to extract the password from the code? All it took was one call to a function.

b. Protecting Python Eval from exploitation


Now consider you have imported the os module for some reason. How would you like a user to be able to read
and write your files, or worse, delete them? This is possible using the command os.system(‘rm -rf *’). This
could pose higher risks when working with applications like web apps and kiosk computers.
Have a look at Python Lists with the example

So what do we do? Well, for one, it is possible to pass a list of functions and variables to eval. This means it can
access only these functions and variables. We pass this as a Python eval dictionary. Confused? Take a look:

1. >>> def let_me_in():


2. password='@dc#431'
3. print("The password is",password)
4. >>> expr=input('Enter an expression as x')

Enter an expression as x3*x**3+2*x**2+x+6

1. >>> x=int(input('Enter the value of x'))

Enter the value of x2

1. >>> safe_dict={}
2. >>> safe_dict['x']=x
3. >>> eval(expr,safe_dict)

40

1. >>> expr=input('Enter an expression as x')

Enter an expression as xlet_me_in()

1. >>> eval(expr,safe_dict)

Traceback (most recent call last):


File “<pyshell#56>”, line 1, in <module>
eval(expr,safe_dict)
File “<string>”, line 1, in <module>
NameError: name ‘let_me_in’ is not defined
Do you know about Python Operators
Works fine. Now, let’s talk about the uses of eval.

6. Uses of eval in Python

While used sparingly because of its vulnerabilities, Python eval() manages to find use in some situations-

 To allow users to enter own scriptlets to allow customization of a complex system’s behavior.
 To evaluate mathematical expressions in applications instead of writing an expression parser.

7. A Final Python eval Example

So before we leave, let’s take a rather practical example of Python eval() Function.

Let’s take a tour of Python Iterator

1. >>> def double(n):


2. return n*2
3. >>> def triple(n):
4. return n*3
5. >>> choice=input('What would you like to do?')

What would you like to do?triple

1. >>> num=input('What number?')

What number?7

1. >>> choice+='('+num+')'
2. >>> eval(choice)

21
Here, we provide the user with a choice- to double or triple a number of her/his choice. We use eval to make
this happen.

So, this was all in Python eval Function Tutorial. Hope you like our explanation.

8. Conclusion – Python eval Function

Hence, we discussed the Python eval() function and how and where to use it. Moreover, we saw vulnerability
and uses of Python eval. Also, we understand Python eval() Function with the examples. Next, we will talk
about exec(). Still, if you have any confusion, comment below. We will definitely get back to you.

Python exec Function – Example and Risk


by DataFlair Team · May 27, 2019

1. Objective – Python exec

Over the days, we have begun discussing a few Python built-in functions we see commonly in use. Today, we
will see Python exec tutorial. Moreover, we will see the exact meaning of Python exec. Also, we will discuss
Python exec() example with syntax. At last, we will look at risk with exec in Python.
So, let’s start the Python exec tutorial.
Python exec Function – Example and Risk

2. What is Python exec()?

To say it again, exec() is a built-in function/ method with Python.

1. >>> type(exec)

<class ‘builtin_function_or_method’>
Let’s check the help for this.

1. >>> help(exec)

Help on built-in function exec in module builtins:


exec(source, globals=None, locals=None, /)
Execute the given source in the context of globals and locals.
The source may be a string representing one or more Python statements
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.

a. Python exec Syntax


Let’s see what this means:
The source can be a string that denotes one or more Python statements
The source can be a code object that the compile() function returns
The globals is a dictionary- the current is the default
The locals can be any kind of mapping- the current is the default; dictionary is the common mapping type with
Python
This function executes the source in the context of globals and locals
If we only mention globals, the locals are that by default

b. Using exec() in Python


We use exec() to dynamically execute Python code- this can be a string or some object code. When it is a
string, Python parses it as a set of statements and executes it if there is no syntax error. When it is object code,
Python executes it. But exec() doesn’t return a value; it returns None. Hence, we cannot use return and yield
statements outside function definitions.

3. Python exec Example

Let’s begin with a simple example of Python exec. For now, let’s skip the globals and locals parameters.

1. >>> code='a=7\nprint("a*17=",a*17)'
2. >>> exec(code)

a*17= 119

a. Dynamic Execution With User Input


If we can get the user to provide input at the time of execution, we can dynamically execute Python code. How
cool is that?

1. >>> code=input('What would you like to do today?')

What would you like to do today?[print(x**2) for x in range(7)]

1. >>> exec(code)

0
1
4
9
16
25
36

b. Python exec vs eval:


If we try doing this to create a list with Python exec:

1. >>> exec('[(x**2) for x in range(7)]')


2. >>>

Nothing happens! We must use Python eval instead:

1. >>> eval('[(x**2) for x in range(7)]')


[0, 1, 4, 9, 16, 25, 36]
Let’s discuss Python Forensics

4. Risks With Exec in Python

a. Problem in Python exec


When you give your users the liberty to execute any piece of code with the Python exec() function, you give
them a way to bend the rules. What if you have access to the os module in your session and they borrow a
command from that to run? Say you have imported os in your code.

1. >>> import os
2. >>> code=input('What would you like to do today?')

What would you like to do today?print(os.listdir())

1. >>> exec(code)

[‘DLLs’, ‘Doc’, ‘etc’, ‘hacked.dll’, ‘include’, ‘Lib’, ‘libs’, ‘LICENSE.txt’, ‘man’, ‘NEWS.txt’,
‘opencv_ffmpeg343.dll’, ‘out.log’, ‘python.exe’, ‘python3.dll’, ‘python37.dll’, ‘pythonw.exe’, ‘Scripts’,
‘share’, ‘tcl’, ‘Tools’, ‘vcruntime140.dll’]
Worse, they can corrupt or even delete all your files and directories. Consider we have a directory demo with
three text files:
In the directory four, we have another text file:

1. >>> os.chdir('\\demo') #The path of your demo folder


2. >>> os.listdir()

[‘four’, ‘one.txt’, ‘three.txt’, ‘two.txt’]


Now, what if the user runs this command:

1. >>> code=input('What would you like to do today?')

What would you like to do today?os.system(‘rm -rf *’)

1. >>> exec(code)

This will delete all your files in the current location! Since exec compiles and evaluates any expression you give
it, it is often more risqué than eval and pickle.

b. Solution of Python exec Problem


You can check which variables and methods are available to the user. Use the dir() method for this.

1. >>> dir()

[‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’]

1. >>> a=7
2. >>> def hello():
print("Hello") 3. >>> dir()

[‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’, ‘a’,


‘hello’]

1. >>> from os import *


2. >>> dir()

[‘DirEntry’, ‘F_OK’, ‘O_APPEND’, ‘O_BINARY’, ‘O_CREAT’, ‘O_EXCL’, ‘O_NOINHERIT’,


‘O_RANDOM’, ‘O_RDONLY’, ‘O_RDWR’, ‘O_SEQUENTIAL’, ‘O_SHORT_LIVED’,
‘O_TEMPORARY’, ‘O_TEXT’, ‘O_TRUNC’, ‘O_WRONLY’, ‘P_DETACH’, ‘P_NOWAIT’,
‘P_NOWAITO’, ‘P_OVERLAY’, ‘P_WAIT’, ‘R_OK’, ‘SEEK_CUR’, ‘SEEK_END’, ‘SEEK_SET’,
‘TMP_MAX’, ‘W_OK’, ‘X_OK’, ‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’,
‘ package ’, ‘ spec ’, ‘_exit’, ‘a’, ‘abort’, ‘access’, ‘altsep’, ‘chdir’, ‘chmod’, ‘close’, ‘closerange’,
‘cpu_count’, ‘curdir’, ‘defpath’, ‘device_encoding’, ‘devnull’, ‘dup’, ‘dup2’, ‘environ’, ‘error’, ‘execl’,
‘execle’, ‘execlp’, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’, ‘extsep’, ‘fdopen’, ‘fsdecode’,
‘fsencode’, ‘fspath’, ‘fstat’, ‘fsync’, ‘ftruncate’, ‘get_exec_path’, ‘get_handle_inheritable’,
‘get_inheritable’, ‘get_terminal_size’, ‘getcwd’, ‘getcwdb’, ‘getenv’, ‘getlogin’, ‘getpid’, ‘getppid’, ‘hello’,
‘isatty’, ‘kill’, ‘linesep’, ‘link’, ‘listdir’, ‘lseek’, ‘lstat’, ‘makedirs’, ‘mkdir’, ‘name’, ‘open’, ‘os’, ‘pardir’,
‘path’, ‘pathsep’, ‘pipe’, ‘popen’, ‘putenv’, ‘read’, ‘readlink’, ‘remove’, ‘removedirs’, ‘rename’,
‘renames’, ‘replace’, ‘rmdir’, ‘scandir’, ‘sep’, ‘set_handle_inheritable’, ‘set_inheritable’, ‘spawnl’,
‘spawnle’, ‘spawnv’, ‘spawnve’, ‘startfile’, ‘stat’, ‘stat_result’, ‘statvfs_result’, ‘strerror’,
‘supports_bytes_environ’, ‘symlink’, ‘system’, ‘terminal_size’, ‘times’, ‘times_result’, ‘truncate’,
‘umask’, ‘uname_result’, ‘unlink’, ‘urandom’, ‘utime’, ‘waitpid’, ‘walk’, ‘write’]
Let’s talk about Python Closure

c. Globals and Locals Parameters


Using the globals and locals parameters, we can restrict what variables and methods users can access. We can
either provide both, or just the globals, in which case that value suffices for both- globals and locals. At module
level, globals and locals are the same dictionary.
The globals parameter-
Let’s take an example of global and local parameter in Python exec.

1. >>> from math import *


2. >>> exec('print(dir())')

[‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’,


‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’,
‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’,
‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘pi’,
‘pow’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]
And now with an empty dictionary for globals:

1. >>> exec('print(dir())',{})

[‘ builtins ’]
This only lets the builtins be available to the object. Want to know more about builtins ? Check the
help for it:
1. >>> help( builtins )

Help on built-in module builtins:


NAME
builtins – Built-in functions, exceptions, and other objects.
You must read Python Function Arguments
We can confirm the unavailability of the math functions with another example:

1. >>> exec('print(tan(90))')

-1.995200412208242

1. >>> exec('print(tan(90))',{})

Traceback (most recent call last):


File “<pyshell#4>”, line 1, in
exec(‘print(tan(90))’,{})
File “”, line 1, in
NameError: name ‘tan’ is not defined
If, here, we want only the tan function to be available, we can do that too:

1. >>> exec('print(tan(90))',{'tan':tan})

-1.995200412208242
Didn’t we say it was a dictionary? Anyway, we can also call it anything else.

1. >>> exec('print(tanx(90))',{'tanx':tan})

-1.995200412208242
The globals and locals parameters-
Let’s try giving it both.

1. >>> exec('print(dir())',{'built': builtins },{'sum':sum,'iter':iter})

[‘iter’, ‘sum’]
This lets the user execute the sum() and iter() methods along with the builtins . We can also keep the user
from availing any builtins:
Let’s revise Decorators in Python

1. >>> exec('print(dir())',{'__builtins ':None},{'sum':sum,'print':print,'dir':dir})

[‘dir’, ‘print’, ‘sum’]


You can also use the built-in globals() and locals() functions for this.

1. >>> exec('print(dir())',globals(),locals())

[‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’,


‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’,
‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’,
‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘pi’,
‘pow’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]
So, this was all in Python exec Function. hope you like our explanation.

5. Conclusion – Exec in Python


Hence, n this Python exec tutorial, we will say that exec supports dynamic execution of Python code.
Moreover, we discussed the meaning of exec in Python. Also, we saw Python exec example. At last, we
discussed risk with exec in Python. Still, if you have any confusion, ask in the comment tab.

Python repr Function With Example | repr vs str in Python


by DataFlair Team · May 27, 2019

1. Objective – Python repr Function


Talking of built-in functions in Python, let’s take one more. Today, we will talk about Python repr,
concluding with some examples to drive the point home. Also, we will discuss Python str vs repr. Moreover, we
will see working with class objects.

So, let’s start the Python repr Function. Hope you like our explanation.

Python repr Function With Example

2. What is repr() in Python?


Let’s ask the IDLE.
What is Python repr Function

This tells us five things:

 This is a built-in function


 This takes an object
 This returns the canonical string representation of this object
 It has the following syntax:

repr(obj, /)

 It is true for many object types and most builtins that eval(repr(obj))=obj

Now, what does this last statement mean?

1. >>> s='Hello'
2. >>> eval(repr(s))

‘Hello’

1. >>> s

‘Hello’

What about an integer?

1. >>> x=7
2. >>> eval(repr(x))==x
True

Now, according to the official documentation by Python, repr() returns a string that holds a printable
representation of an object. For most types in Python, this will give you a string that yields an object with the
same value as when we pass it to eval(). For a few other types, it returns a string delimited in angle brackets.
This holds the type of object and other information like the name and address of that object.

We recommend you to read Python Method

To us, this is the repr() function. To Python, this is a class that controls what it returns for its instances via the
repr () magic method.

3. Python repr Example


Let’s get to an example of Python repr function.

1. >>> msg='Hello, world!'


2. >>> repr(msg)

“‘Hello, world!’”
A closer look at this tells us this returns ‘Hello, world!’ within double quotes. If we pass this back to eval, it
gives us:

1. >>> eval("'Hello, world!'")

‘Hello, world!’

This wouldn’t work:

1. >>> eval('Hello, world!')

Traceback (most recent call last):

File “<pyshell#16>”, line 1, in <module>

eval(‘Hello, world!’)

File “<string>”, line 1

Hello, world!

SyntaxError: unexpected EOF while parsing

This is because we don’t have a variable with that name (Hello, world!)

4. Working With Class Objects


We said repr() internally makes a call to the repr () method. Let’s try this with one of our own classes.

1. >>> class Color:


2. color='orange'
3. def repr (self):
4. return repr(self.color)
5. >>> o=Color()
6. >>> repr(o)

“‘orange'”

Here, we override the repr () method to get it to do what we want. When we say the word override, note
that Color already has an repr () method. This is because it inherits from the object class, and object has an
repr () method.

1. >>> issubclass(Color,object)

True

1. >>> dir(object)
[‘ class ’, ‘ delattr ’, ‘ dir ’, ‘ doc ’, ‘ eq ’, ‘ format ’, ‘ ge ’, ‘ getattribute ’,
‘ gt ’, ‘ hash ’, ‘__init ’, ‘ init_subclass ’, ‘ le ’, ‘ lt ’, ‘ ne ’, ‘ new ’, ‘ reduce ’,
‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ sizeof ’, ‘ str ’, ‘ subclasshook ’]

Remember when in the beginning, we said repr returns a string in angle brackets for some types? Let’s see how
and where.

1. >>> class Orange:


2. def init (self,color,size):
3. self.color=color
4. self.size=size
5. >>> o=Orange('orange',7)
6. >>> o

< main .Orange object at 0x02C6C1D0>

1. >>> print(o)

< main .Orange object at 0x02C6C1D0>

These are strings with the class name and the id of the object instance(its memory address in CPython). To print
this better, we make use of the information that the print() function makes a call to the str dunder/ method:

1. >>> class Orange:


2. def init (self,color,size):
3. self.color=color
4. self.size=size
5. def str (self):
6. return f'I am {self.size} and {self.color}'
7. >>> o=Orange('orange',7)
8. >>> o

< main .Orange object at 0x02D40E70>

1. >>> print(o)

I am 7 and orange

That was pretty cool! In Java, we would use the toString() method for this.

5. str() vs repr() in Python


The str () and repr () methods both give us strings in return. So what sets them apart?

 The goal of repr is to be unambiguous and that of str is to be readable.


 repr is kind of official and str is somewhat informal.

Take an example to ensure you understand the difference:


1. >>> s='Hello'
2. >>> print(str(s))

Hello

1. >>> print(repr(s))

‘Hello’

 The print statement and str() function make a call to str , but repr() makes on to repr .
 Any string at the interpreter prompt makes a call to str (), but an object at the prompt makes a call
to repr ().

Want to do prepration for Python Interview – Click Here >>>

Take a look:

1. >>> str(3)

‘3’

1. >>> repr(3)

‘3’

They appear the same. Okay, now take a look at this one:

1. >>> import datetime


2. >>> t=datetime.datetime.now()
3. >>> str(t) #Readable

‘2018-09-07 17:33:24.261778’

1. >>> repr(t)

‘datetime.datetime(2018, 9, 7, 17, 33, 24, 261778)’

1. >>> t

datetime.datetime(2018, 9, 7, 17, 33, 24, 261778)

Using this ‘official’ representation, we can reconstruct the object, but not with what str() gives us:

1. >>> eval('datetime.datetime(2018, 9, 7, 17, 33, 24, 261778)')

datetime.datetime(2018, 9, 7, 17, 33, 24, 261778)

1. >>> eval('2018-09-07 17:33:24.261778')

Traceback (most recent call last):


File “<pyshell#56>”, line 1, in <module>

eval(‘2018-09-07 17:33:24.261778’)

File “<string>”, line 1

2018-09-07 17:33:24.261778

SyntaxError: invalid token

Let’s take another example.

1. >>> class demo:


2. def init (self,a,b):
3. self.a=a
4. self.b=b
5. def repr (self):
6. return ' repr for demo'
7. def str (self):
8. return ' str for demo'
9. >>> d=demo(3,4)
10. >>> d

repr for demo

1. >>> print(d)

str for demo

1. >>> str(d)

‘ str for demo’

1. >>> repr(d)

‘ repr for demo’

1. >>> f'{d}'

‘ str for demo’

So, this was all in Python repr tutorial. Hope you like our explanation.

6. Conclusion – Python repr


The interactive interpreter uses repr() too in giving you the output of your expression:

result=expr; if result is not None: print repr(result)


Hence, in this Python repr tutorial, we discussed the meaning of repr in Python. Also, we looked Python repr
example and working with class objects. Moreover, we saw str vs repr in Python. Still, if you have any query
regarding Python repr tutorial, ask in the comment tab.

Python Data Structures – Lists, Tuples, Sets, Dictionaries


by DataFlair Team · May 27, 2019

1. Python Data Structures Tutorial

Earlier we have discussed Python Operators. Today, in this Python Data Structures Tutorial, we will talk
about different data structures in Python provides us with. These include Python list, Python tuple, Python set,
and Python dictionaries with their syntax and examples.

So, let’s start Python Data Structure.

Python Data Structures – Lists, Tuples, Sets, Dictionaries

2. Introduction to Python Data Structures

You can think of a data structure as a way of organizing and storing data such that we can access and modify it
efficiently. Earlier, we have seen primitive data types like integers, floats, Booleans, and strings. Now, we’ll
take a deeper look at the non-primitive Python data structures.

Let’s begin with our first Python Data Structures and lists.

3. Python List

A list in Python is a heterogeneous container for items. This would remind you of an array in C++, but since
Python does not support arrays, we have Python Lists.
a. How to Declare Python List?
To use a list, you must declare it first. Do this using square brackets and separate values with commas.

1. >>> languages=['C++','Python','Scratch']

You can put any kind of value in a list. This can be a string, a Tuple, a Boolean, or even a list itself.

1. >>> list1=[1,[2,3],(4,5),False,'No']

Note that here, we put different kinds of values in the list. Hence, a list is (or can be) heterogeneous.

b. How to Access Python List?


1. Accessing an entire list

To access an entire list, all you need to do is to type its name in the shell.

1. >>> list1

[1, [2, 3], (4, 5), False, ‘No’]

2. Accessing a single item from the list


To get just one item from the list, you must use its index. However, remember that indexing begins at 0. Let’s
first take a look at the two kinds of indexing.

 Positive Indexing– As you can guess, positive indexing begins at 0 for the leftmost/first item, and then
traverses right.

1. >>> list1[3]

False

 Negative Indexing– Contrary to positive indexing, negative indexing begins at -1 for the rightmost/last
item, and then traverses left. To get the same item form list1 by negative indexing, we use the index -2.

1. >>> type(list1[-2])

<class ‘bool’>

It is also worth noting that the index can’t be a float, it has to be an integer.

1. >>> list1[1.0]

Traceback (most recent call last):

File “<pyshell#219>”, line 1, in <module>

list1[1.0]
TypeError: list indices must be integers or slices, not float

If you face any doubt in a Python list or Python Data Structure, please comment.

3. Slicing a Python List


Sometimes, you may not want an entire list or a single item, but a number of items from it. Here, the slicing
operator [:] comes into play.

Suppose we want items second through fourth from list ‘list1’. We write the following code for this.

1. >>> list1[1:4]

[[2, 3], (4, 5), False]

Here, we wanted the items from [2,3] to False. The indices for these boundary items are 1 and 3 respectively.
But if the ending index is n, then it prints items till index n-1. Hence, we gave it an ending index of 4 here.

We can use negative indexing in the slicing operator too. Let’s see how.

1. >>> list1[:-2]

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

Here, -2 is the index for the tuple (4,5).

c. A list is mutable
Mutability is the ability to be mutated, to be changed. A list is mutable, so it is possible to reassign and delete
individual items as well.

1. >>> languages [‘C+

+’, ‘Python’, ‘Scratch’]

1. >>> languages[2]='Java'
2. >>> languages

[‘C++’, ‘Python’, ‘Java’]

Of how to delete an item, we will see in section d.

d. How to Delete a Python List?


Like anything else in Python, it is possible to delete a list.

To delete an entire list, use the del keyword with the name of the list.

1. >>> list1

Traceback (most recent call last):


File “<pyshell#225>”, line 1, in <module>

list1

NameError: name ‘list1’ is not defined

But to delete a single item or a slice, you need its index/indices.

1. >>> del languages[2]


2. >>> languages

[‘C++’, ‘Python’]

Let’s delete a slice now.

1. >>> del languages[1:]


2. >>> languages

[‘C++’]

e. Reassigning a List in Python


You can either reassign a single item, a slice, or an entire list. Let’s take a new list and then reassign on it.

1. >>> list1=[1,2,3,4,5,6,7,8]

1. Reassigning a single item

1. >>> list1[0]=0
2. >>> list1

[0, 2, 3, 4, 5, 6, 7, 8]

2. Reassigning a slice

Now let’s attempt reassigning a slice.

1. >>> list1[1:3]=[9,10,11]
2. >>> list1

[0, 9, 10, 11, 4, 5, 6, 7, 8]

3. Reassigning the entire list


Finally, let’s reassign the entire list.

1. >>> list1=[0,0,0]
2. >>> list1

[0, 0, 0]
To get an even deeper look into lists, read our article on Python Lists.

Any query yet on Python Data structures, Please Comment

4. Python Tuple

This Python Data Structure is like a, like a list in Python, is a heterogeneous container for items. But the major
difference between the two (tuple and list) is that a list is mutable, but a tuple is immutable. This means that
while you can reassign or delete an entire tuple, you cannot do the same to a single item or a slice.

To declare a tuple, we use parentheses.

1. >>> colors=('Red','Green','Blue')

a. Python Tuple Packing


Python Tuple packing is the term for packing a sequence of values into a tuple without using parentheses.

1. >>> mytuple=1,2,3, #Or it could have been mytuple=1,2,3


2. >>> mytuple

(1, 2, 3)

b. Python Tuple Unpacking


The opposite of tuple packing, unpacking allots the values from a tuple into a sequence of variables.

1. >>> a,b,c=mytuple
2. >>>

print(a,b,c) 1 2 3

c. Creating a tuple with a single item


Let’s do this once again. Create a tuple and assign a 1 to it.

1. >>> a=(1)

Now, let’s call the type() function on it.

1. >>> type(a)

<class ‘int’>

As you can see, this declared an integer, not a tuple.

To get around this, you need to append a comma to the end of the first item 1. This tells the interpreter that it’s
a tuple.

1. >>> a=(1,)
2. >>> type(a)

<class ‘tuple’>

d. Accessing, Reassigning, and Deleting Items


We can perform these operations on a tuple just like we can on a list. The only differences that exist are because
a tuple is immutable, so you can’t mess with a single item or a slice.

1. >>> del a[0]

Traceback (most recent call last):

File “<pyshell#251>”, line 1, in <module>

del a[0]

TypeError: ‘tuple’ object doesn’t support item deletion

Even though this tuple has only one item, we couldn’t delete it because we used its index to delete.

For information about functions and methods on a tuple, refer to our article on Python Tuples.

5. Python Set

This is one of the important Python Data Structures. A Python set is a slightly different concept from a list or a
tuple. A set, in Python, is just like the mathematical set. It does not hold duplicate values and is unordered.
However, it is not immutable, unlike a tuple.

Let’s first declare a set. Use curly braces for the same.

1. >>> myset={3,1,2}
2. >>> myset

{1, 2, 3}

As you can see, it rearranged the elements in an ascending order.

Since a set is unordered, there is no way we can use indexing to access or delete its elements. Then, to perform
operations on it, Python provides us with a list of functions and methods like discard(), pop(), clear(), remove(),
add(), and more. Functions like len() and max() also apply on sets.

Any Doubt yet in Python Data Structures? Please Comment.

6. Python Dictionaries in

Finally, we will take a look at Python dictionaries. Think of a real-life dictionary. What is it used for? It holds
word-meaning pairs. Likewise, a Python dictionary holds key-value pairs. However, you may not use an
unhashable item as a key.
To declare a Python dictionary, we use curly braces. But since it has key-value pairs instead of single values,
this differentiates a dictionary from a set.

1. >>> mydict={1:2,2:4,3:6}
2. >>> mydict

{1: 2, 2: 4, 3: 6}

To access pairs from a Python dictionary, we use their keys as indices. For example, let’s try accessing the
value 4.

1. >>> mydict[2]

This was all about the Python Data Structures Tutorial.

7. Conclusion – Python Data Structures

Summing up for today’s Python Data Structures tutorial, we learned about various user-defined data structures
using python like lists, tuples, sets, and dictionaries. All of these have their own advantages and disadvantages.
It is highly recommended to refer to our tutorials on each of those to sharpen your axes. See you later.

 9

Python List with Examples – A Complete Python List Tutorial


by DataFlair Team · May 27, 2019

1. Python List Tutorial

In today’s tutorial, we will learn about Python list. We will discuss how to create, access, slice, and reassign list
in Python. Then we will see how to apply functions to them. Along with this, we will discuss Python List
Operations and Concatenation.

So, let’s start the Python List Tutorial.


Python List with Examples – A Complete Python List Tutorial

2. What is Python List?

Unlike C++ or Java, Python Programming Language doesn’t have arrays. To hold a sequence of values, then, it
provides the ‘list’ class. A Python list can be seen as a collection of values.

3. How to Create Python List?

To create python list of items, you need to mention the items, separated by commas, in square brackets. This is
the python syntax you need to follow. Then assign it to a variable. Remember once again, you don’t need to
declare the data type, because Python is dynamically-typed.

1. >>> colors=['red','green','blue']

A Python list may hold different types of values.

1. >>> days=['Monday','Tuesday','Wednesday',4,5,6,7.0]

A list may have python list.

1. >>> languages=[['English'],['Gujarati'],['Hindi'],'Romanian','Spanish']
2. >>> languages

[[‘English’], [‘Gujarati’], [‘Hindi’], ‘Romanian’, ‘Spanish’]

1. >>> type(languages[0])

<class ‘list’>
A list may also contain tuples or so.

1. >>> languages=[('English','Albanian'),'Gujarati','Hindi','Romanian','Spanish']
2. >>> languages[0]

(‘English’, ‘Romanian’)

1. >>> type(languages[0])

<class ‘tuple’>

1. >>> languages[0][0]='Albanian'

Traceback (most recent call last):

File “<pyshell#24>”, line 1, in <module>

languages[0][0]=’Albanian’

TypeError: ‘tuple’ object does not support item assignment

4. How to Access Python List?

To access a Python list as a whole, all you need is its name.

1. >>> days

[‘Monday’, ‘Tuesday’, ‘Wednesday’, 4, 5, 6, 7.0]

Or, you can put it in a print statement.

1. >>> languages=[['English'],['Gujarati'],['Hindi'],'Romanian','Spanish']
2. >>> print(languages)

[[‘English’], [‘Gujarati’], [‘Hindi’], ‘Romanian’, ‘Spanish’]

To access a single element, use its index in square brackets after the list’s name. Indexing begins at 0.

1. >>> languages[0]

[‘English’]

An index cannot be a float value.

1. >>> languages[1.0]

Traceback (most recent call last):

File “<pyshell#70>”, line 1, in <module>

languages[1.0]
TypeError: list indices must be integers or slices, not float

5. Slicing a Python List

When you want only a part of a Python list, you can use the slicing operator [].

1. >>> indices=['zero','one','two','three','four','five']
2. >>> indices[2:4]

[‘two’, ‘three’]

This returns items from index 2 to index 4-1 (i.e., 3)

1. >>> indices[:4]

[‘zero’, ‘one’, ‘two’, ‘three’]

This returns items from the beginning of the list to index 3.

1. >>> indices[4:]

[‘four’, ‘five’]

It returns items from index 4 to the end of the list in Python.

1. >>> indices[:]

[‘zero’, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’]

This returns the whole list.

 Negative indices- The indices we mention can be negative as well. A negative index means traversal
from the end of the list.

1. >>> indices[:-2]

[‘zero’, ‘one’, ‘two’, ‘three’]

This returns item from the list’s beginning to two items from the end.

1. >>> indices[1:-2]

[‘one’, ‘two’, ‘three’]

It returns items from the item at index 1 to two items from the end.

1. >>> indices[-2:-1]

[‘four’]

This returns items from two from the end to one from the end.
1. >>> indices[-1:-2]

[] This returns an empty Python list, because the start is ahead of the stop for the traversal.

6. Reassigning a Python List (Mutable)


Python Lists are mutable. This means that you can reassign its items, or you can reassign it as a whole. Let’s
take a new list.

1. >>> colors=['red','green','blue']

a. Reassigning the whole Python list


You can reassign a Python list by assigning it like a new list.

1. >>> colors=['caramel','gold','silver','occur']
2. >>> colors

[‘caramel’, ‘gold’, ‘silver’, ‘occur’]

b. Reassigning a few elements


You can also reassign a slice of a list in Python.

1. >>> colors[2:]=['bronze','silver']
2. >>> colors

[‘caramel’, ‘gold’, ‘bronze’, ‘silver’]

If we had instead put two values to a single one in the left, see what would’ve happened.

1. >>> colors=['caramel','gold','silver','occur']
2. >>> colors[2:3]=['bronze','silver']
3. >>> colors

[‘caramel’, ‘gold’, ‘bronze’, ‘silver’, ‘occur’]

colors[2:3] reassigns the element at index 2, which is the third element.

2:2 works too.

1. >>> colors[2:2]=['occur']
2. >>> colors

[‘caramel’, ‘gold’, ‘occur’, ‘bronze’, ‘silver’]

c. Reassigning a single element


You can reassign individual elements too.
1. >>> colors=['caramel','gold','silver','occur']
2. >>> colors[3]='bronze'
3. >>> colors

[‘caramel’, ‘gold’, ‘silver’, ‘bronze’]

Now if you want to add another item ‘holographic’ to the list, we cannot do it the conventional way.

1. >>> colors[4]='holographic'

Traceback (most recent call last):

File “<pyshell#2>”, line 1, in <module>

colors[4]=’holographic’

IndexError: list assignment index out of range

So, you need to reassign the whole list for the same.

1. >>> colors=['caramel','gold','silver','bronze','holographic']
2. >>> colors

[‘caramel’, ‘gold’, ‘silver’, ‘bronze’, ‘holographic’]

7. How can we Delete a Python List?

You can delete a Python list, some of its elements, or a single element.

a. Deleting the entire Python list


Use the del keyword for the same.

1. >>> del colors


2. >>> colors

Traceback (most recent call last):

File “<pyshell#51>”, line 1, in <module>

colors

NameError: name ‘colors’ is not defined

b. Deleting a few elements


Use the slicing operator in python to delete a slice.

1. >>> colors=['caramel','gold','silver','bronze','holographic']
2. >>> del colors[2:4]
3. >>> colors
[‘caramel’, ‘gold’, ‘holographic’]

1. >>> colors[2]

‘holographic’

Now, ‘holographic’ is at position 2.

c. Deleting a single element


To delete a single element from a Python list, use its index.

1. >>> del colors[0]


2. >>> colors

[‘gold’,

‘holographic’]

8. Multidimensional Lists in Python

You can also put a list in a list. Let’s look at a multidimensional list.

1. >>> grocery_list=[['caramel','P&B','Jelly'],['onions','potatoes'],['flour','oil']]
2. >>> grocery_list

[[‘caramel’, ‘P&B’, ‘Jelly’], [‘onions’, ‘potatoes’], [‘flour’, ‘oil’]]

This is a grocery Python list with lists in it, where the lists are according to a category.

Or, you can choose to go deeper.

1. >>> a=[[[1,2],[3,4],5],[6,7]]
2. >>> a

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

To access the element 4 here, we type the following code into the shell.

1. >>> a[0][1][1]

9. Concatenation of Python List

The concatenation operator works for lists as well. It lets us join two lists, with their orders preserved.

1. >>> a,b=[3,1,2],[5,4,6]
2. >>> a+b

[3, 1, 2, 5, 4, 6]
10. Python List Operations
a. Multiplication
This is an arithmetic operation. Multiplying a Python list by an integer makes copies of its items that a number
of times while preserving the order.

1. >>> a*=3
2. >>> a

[3, 1, 2, 3, 1, 2, 3, 1, 2]

However, you can’t multiply it by a float.

1. >>> a*3.0

Traceback (most recent call last):

File “<pyshell#89>”, line 1, in <module>

a*3.0

TypeError: can’t multiply sequence by non-int of type ‘float’

b. Membership
You can apply the ‘in’ and ‘not in’ operators on a Python list.

1. >>> 1 in a

True

1. >>> 2 not in a

False

11. Iterating on a list

Python list can be traversed with a for loop in python.

1. >>> for i in [1,2,3]:


2. if i%2==0:
3. print(f"{i} is composite\n")

2 is composite

12. Python List Comprehension

You can create a new list just like you would do in mathematics. To do so, type an expression followed by a for
statement, all inside square brackets. You may assign it to a variable. Let’s make a list for all even numbers
from 1 to 20.

1. >>> even=[2*i for i in range(1,11)]


2. >>> even

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Optionally, you can add an if-statement to filter out items. If we want to change this list to hold only those items
from 1 to 20 that are even and are divisible by 3, we write the following code.

1. >>> even=[2*i for i in range(1,11) if i%3==0]


2. >>> even

[6, 12, 18]

13. Built-in List Functions

There are some built-in functions in Python that you can use on python lists.

Python List Tutorial – Built-in List Functions

a. len()
It calculates the length of the list.

1. >>> len(even)

b. max()
It returns the item from the list with the highest value.
1. >>> max(even)

18

If all the items in your list are strings, it will compare.

1. >>> max(['1','2','3'])

‘3’

But it fails when some are numeric, and some are strings in python.

1. >>> max([2,'1','2'])

Traceback (most recent call last):

File “<pyshell#116>”, line 1, in <module>

max([2,’1′,’2′])

TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

c. min()
It returns the item from the Python list with the lowest value.

1. >>> min(even)

d. sum()
It returns the sum of all the elements in the list.

1. >>> sum(even)

36

However, for this, the Python list must hold all numeric values.

1. >>> a=['1','2','3']
2. >>> sum(a)

Traceback (most recent call last):

File “<pyshell#112>”, line 1, in <module>

sum(a)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’


It works on floats.

1. >>> sum([1.1,2.2,3.3])

6.6

e. sorted()
It returns a sorted version of the list, but does not change the original one.

1. >>> a=[3,1,2]
2. >>> sorted(a)

[1, 2, 3]

1. >>> a

[3, 1, 2]

If the Python list members are strings, it sorts them according to their ASCII values.

1. >>> sorted(['hello','hell','Hello'])

[‘Hello’, ‘hell’, ‘hello’]

Here, since H has an ASCII value of 72, it appears first.

f. list()
It converts a different data type into a list.

1. >>> list("abc")

[‘a’, ‘b’, ‘c’]

It can’t convert a single int into a list, though, it only converts iterables.

1. >>> list(2)

Traceback (most recent call last):

File “<pyshell#122>”, line 1, in <module>

list(2)

TypeError: ‘int’ object is not iterable

g. any()
It returns True if even one item in the Python list has a True value.
1. >>> any(['','','1'])

True

It returns False for an empty iterable.

1. >>> any([])

False

h. all()
It returns True if all items in the list have a True value.

1. >>> all(['','','1'])

False

It returns True for an empty iterable.

1. >>> all([])

True

14. Built-in Methods

While a function is what you can apply on a construct and get a result, a method is what you can do to it and
change it. To call a method on a construct, you use the dot-operator(.). Python supports some built-in methods
to alter a Python list.

Python List – Built-in Methods


a. append()
It adds an item to the end of the list.

1. >>> a

[2, 1, 3]

1. >>> a.append(4)
2. >>> a

[2, 1, 3, 4]

b. insert()
It inserts an item at a specified position.

1. >>> a.insert(3,5)
2. >>> a

[2, 1, 3, 5, 4]

This inserted the element 5 at index 3.

c. remove()
It removes the first instance of an item from the Python list.

1. >>> a=[2,1,3,5,2,4]
2. >>> a.remove(2)
3. >>> a

[1, 3, 5, 2, 4]

Notice how there were two 2s, but it removed only the first one.

d. pop()
It removes the element at the specified index, and prints it to the screen.

1. >>> a.pop(3)

1. >>> a

[1, 3, 5, 4]

e. clear()
It empties the Python list.

1. >>> a.clear()
2. >>> a

[]

It now has a False value.

1. >>> bool(a)

False

f. index()
It returns the first matching index of the item specified.

1. >>> a=[1,3,5,3,4]
2. >>> a.index(3)

g. count()
It returns the count of the item specified.

1. >>> a.count(3)

h. sort()
It sorts the list in an ascending order.

1. >>> a.sort()
2. >>> a

[1, 3, 3, 4, 5]

i. reverse()
It reverses the order of elements in the Python lists.

1. >>> a.reverse()
2. >>> a

[5, 4, 3, 3, 1]

This was all about the Python lists


15. Conclusion

Woah, that was a lot, wasn’t it? Let’s make a quick revision so you don’t forget it. In this lesson on Python
Lists, we first looked at how to declare and access a list. That included slicing lists in python. Then we looked at
how to delete and reassign elements or an entire list. Next, we learned about multidimensional lists and
comprehension. We saw how to iterate on python lists, concatenate them, and also the operations that you can
perform on them. Lastly, we looked at some built-in functions and methods that you can call on lists. Hope you
enjoyed, see you again. Furthermore, if you have any query, feel free to ask in the comment section

What is Python Tuple – Creating, Functions, Methods, Operations


by DataFlair Team · February 22, 2019

1. Python Tuple Tutorial

In this Python Tuple tutorial, we will rather take a deeper look at Python tuple. First, let’s look at what a
Python tuple is and then we will discuss how to create, access, slice, delete tuple in Python. Moreover, we will
learn the functions, methods, and operations of Python tuples.

Python provides a range of constructs to deal with items. These include python lists, dictionaries, sets, tuples,
and many more. It also supports in-built functions and methods that we can apply on these constructs.

So, let’s start the Python tuple Tutorial.

What is Python Tuple – Creating, Functions, Methods, Operations

2. What is Python Tuple?

Python Tuples are like a list. It can hold a sequence of items. The difference is that it is immutable. Let’s learn
the syntax to create a tuple in Python.
3. How to Create a Python Tuple?

To declare a Python tuple, you must type a list of items separated by commas, inside parentheses. Then assign it
to a variable.

1. >>> percentages=(90,95,89)

You should use a tuple when you don’t want to change just an item in future.

a. Python Tuples Packing


You can also create a Python tuple without parentheses. This is called tuple packing.

1. >>> b= 1, 2.0, 'three'

b. Python Tuples Unpacking


Python tuple unpacking is when you assign values from a tuple to a sequence of variables in python.

1. >>> percentages=(99,95,90,89,93,96)
2. >>> a,b,c,d,e,f=percentages
3. >>> c

90

You can do the same to a list.

c. Creating a tuple with a single item


Until now, we have seen how easy it is to declare a Python tuple. But when you do so with just one element, it
may create some problems. Let’s take a look at it.

1. >>> a=(1)
2. >>> type(a)

<class ‘int’>

Wasn’t the type() method supposed to return class ‘tuple’?

To get around this, we add a comma after the item.

1. >>> a=(1,)
2. >>> type(a)

<class ‘tuple’>

Problem solved. And as we saw in tuple packing, we can skip the parentheses here.

1. >>> a=1,
2. >>> type(a)
<class ‘tuple’>

Also, like a list, a Python tuple may contain items of different types.

1. >>> a=(1,2.0,'three')

4. How to Access Python Tuple?

a. Accessing the entire tuple


To access a tuple in python, just type its name.

1. >>>

percentages (90, 95, 89)

Or, pass it to the print statement.

1. >>> print(percentages)

(90, 95, 89)

b. Accessing a single item


To get a single item from a Python tuple, use its index in square brackets. Indexing begins at 0.

1. >>> percentages[1]

95

5. Slicing a Tuple in Python


If you want a part(slice) of a tuple in Python, use the slicing operator [].

1. >>> percentages=(99,95,90,89,93,96)

a. Positive Indices
When using positive indices, we traverse the list from the left.

1. >>> percentages[2:4]

(90, 89)

This prints out items from index 2 to index 3 (4-1) (items third to fourth).

1. >>> percentages[:4]

(99, 95, 90, 89)


This prints out items from the beginning to the item at index 3.

1. >>> percentages[4:]

(93, 96)

This prints out items from index 4 to the end of the list.

1. >>> percentages[2:2]

()

However, this returns an empty Python tuple.

b. Negative indexing
Now, let’s look at negative indexing. Unlike positive indexing, it begins traversing from the right.

1. >>> percentages[:-2]

(99, 95, 90, 89)

This prints out the items from the tuple’s beginning to two items from the end.

1. >>> percentages[-2:]

(93, 96)

This prints out items from two items from the end to the end.

1. >>> percentages[2:-2]

(90, 89)

This prints out items from index 2 to two items from the end.

1. >>> percentages[-2:2]

()

This last piece of code, however, returns an empty tuple. This is because the

start(-2) is behind the end(2) in this case.

Lastly, when you provide no indices, it prints the whole Python tuple.

1. >>> percentages[:]

(99, 95, 90, 89, 93, 96)

6. Deleting a Python Tuple


As we discussed above, a Python tuple is immutable. This also means that you can’t delete just a part of it. You
must delete an entire tuple, if you may.

1. >>> del percentages[4]

Traceback (most recent call last):

File “<pyshell#19>”, line 1, in <module>

del percentages[4]

TypeError: ‘tuple’ object doesn’t support item deletion

So, deleting a single element didn’t work. Let’s try deleting a slice.

1. >>> del percentages[2:4]

Traceback (most recent call last):

File “<pyshell#20>”, line 1, in <module>

del percentages[2:4]

TypeError: ‘tuple’ object does not support item deletion

As you can see, that didn’t work either. Now, let’s try deleting the entire tuple.

1. >>> del percentages


2. >>> percentages

Traceback (most recent call last):

File “<pyshell#40>”, line 1, in <module>

percentages

NameError: name ‘percentages’ is not defined

We see that the Python tuple has successfully been deleted.

7. Reassigning Tuples in Python

As we discussed, a Python tuple is immutable. So let’s try changing a value. But before that, let’s take a new
tuple with a list as an item in it.

1. >>> my_tuple=(1,2,3,[4,5])

Now, let’s try changing the list [4,5]. Its index is 3.

1. >>> my_tuple[3]=6
Traceback (most recent call last):

File “<pyshell#43>”, line 1, in <module>

my_tuple[3]=6

TypeError: ‘tuple’ object does not support item assignment

See, that failed. Now how about changing an element from the same list]?

1. >>> my_tuple[3][0]=6
2. >>> my_tuple

(1, 2, 3, [6, 5])

This worked without a flaw. So we can see that while tuples are immutable, a mutable item that it holds may be
reassigned.

8. Python Tuple Functions

A lot of functions that work on lists work on tuples too. A function applies on a construct and returns a result. It
does not modify the construct. Let’s see what we can do.

Python Tuple Tutorial – Python Tuple Functions

a. len()
Like a list, a Python tuple is of a certain length. The len() function returns its length.

1. >>> my_tuple
(1, 2, 3, [6, 5])

1. >>> len(my_tuple)

It returned 4, not 5, because the list counts as 1.

b. max()
It returns the item from the tuple with the highest value.

We can’t apply this function on the tuple my_tuple, because ints cannot be compared to a list. So let’s take yet
another tuple in Python.

1. >>> a=(3,1,2,5,4,6)
2. >>> max(a)

Let’s try that on strings.

1. >>> max(('Hi','hi','Hello'))

‘hi’

‘hi’ is the greatest out of these, because h has the highest ASCII value among h and H.

But you can’t compare an int and a string.

1. >>> max(('Hi',9))

Traceback (most recent call last):

File “<pyshell#59>”, line 1, in <module>

max((‘Hi’,9))

TypeError: ‘>’ not supported between instances of ‘int’ and ‘str’

c. min()
Like the max() function, the min() returns the item with the lowest values.

1. >>> min(a)

As you can see, 1 is the smallest item in this Python tuple.

d. sum()
This function returns the arithmetic sum of all the items in the tuple.

1. >>> sum(a)

21

However, you can’t apply this function on a tuple with strings.

1. >>> sum(('1','2','3'))

Traceback (most recent call last):

File “<pyshell#57>”, line 1, in <module>

sum((‘1′,’2′,’3’))

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

e. any()
If even one item in the tuple has a Boolean value of True, then this function returns True. Otherwise, it returns
False.

1. >>> any(('','0',''))

True

The string ‘0’ does have a Boolean value of True. If it was rather the integer 0, it would’ve returned False.

1. >>> any(('',0,''))

False

f. all()
Unlike any(), all() returns True only if all items have a Boolean value of True. Otherwise, it returns False.

1. >>> all(('1',1,True,''))

False

g. sorted()
This function returns a sorted version of the tuple. The sorting is in ascending order, and it doesn’t modify the
original tuple in Python.

1. >>> sorted(a)

[1, 2, 3, 4, 5, 6]
h. tuple()
This function converts another construct into a Python tuple. Let’s look at some of those.

1. >>> list1=[1,2,3]
2. >>> tuple(list1)

(1, 2, 3)

1. >>> string1="string"
2. >>> tuple(string1)

(‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’)

How well would it work with sets?

1. >>> set1={2,1,3}
2. >>> tuple(set1)

(1, 2, 3)

1. >>> set1

{1, 2, 3}

As we can see, when we declared a set as 2,1,3, it automatically reordered itself to 1,2,3. Furthermore, creating
a Python tuple from it returned the new tuple in the new order, that is, ascending order.

9. Python Tuple Methods


A method is a sequence of instructions to perform on something. Unlike a function, it does modify the construct
on which it is called. You call a method using the dot operator in python. Let’s learn about the two in-built
methods of Python.

a. index()
This method takes one argument and returns the index of the first appearance of an item in a tuple. Let’s take a
new tuple.

1. >>> a=(1,2,3,2,4,5,2)
2. >>> a.index(2)

As you can see, we have 2s at indices 1, 3, and 6. But it returns only the first index.

b. count()
This method takes one argument and returns the number of times an item appears in the tuple.
1. >>> a.count(2)

10. Python Tuple Operations

Now, we will look at the operations that we can perform on tuples in Python.

a. Membership
We can apply the ‘in’ and ‘not in’ operators on items. This tells us whether they belong to the tuple.

1. >>> 'a' in

tuple("string") False

1. >>> 'x' not in tuple("string")

True

b. Concatenation
Like we’ve previously discussed on several occasions, concatenation is the act of joining. We can join two
tuples using the concatenation operator ‘+’.

1. >>> (1,2,3)+(4,5,6)

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

Other arithmetic operations do not apply on a tuple.

c. Logical
All the logical operators (like >,>=,..) can be applied on a tuple.

1. >>> (1,2,3)>(4,5,6)

False

1. >>> (1,2)==('1','2')

False

As is obvious, the ints 1 and aren’t equal to the strings ‘1’ and ‘2’. Likewise, it returns False.

d. Identity
Remember the ‘is’ and ‘is not’ operators we discussed about in our tutorial on Python Operators? Let’s try that
on tuples.
1. >>> a=(1,2)
2. >>> (1,2) is a

False

That did not make sense, did it? So what really happened? Well, in Python, two tuples or lists do not have the
same identity. In other words, they are two different tuples or lists. As a result, it returns False.

11. Iterating on a Python Tuple

You can iterate on a Python tuple using a for loop like you would iterate on a list.

1. >>> for i in (1,3,2):


2. print(i)

1
3
2

12. Nested Tuples in Python

Finally, we will learn about nesting tuples. You may remember how we can nest lists. Due to the similarities of
a tuple to a list, we do the same with tuples.

1. >>> a=((1,2,3),(4,(5,6)))

Suppose we want to access the item 6. For that, since we use indices, we write the following code.

1. >>> a[1][1][1]

Python tuple may also contain other constructs, especially, lists. After all, it is a collection of items, and items
can be anything.

1. >>> (1,2,[3,4])

(1, 2, [3, 4])

This was all on Python Tuple Tutorial. Hope you like our explanation.

13. Conclusion

Hence, today, we learned about creating tuples in Python. In that, we looked at tuple packing and unpacking,
and how to create a tuple with just one item. Then we talked about how to access, slice, delete, and reassign a
tuple. After that, we looked at the inbuilt functions and methods that we can call on a tuple. Lastly, we learned
about the operations we can perform on a Python tuple, how to iterate on it, and nested tuples. Try them in the
shell and leave your honest comments.

Python Tuples vs Lists – Comparison Between Lists and Tuples


by DataFlair Team · May 27, 2019

1. Python Tuples vs Lists – Objective

In our previous python tutorials, we’ve seen tuples in python and lists in python. Both are heterogeneous
collections of python objects. But which one do you choose when you need to store a collection? To answer this
question, we first get a little deeper into the two constructs and then we will study comparison between python
tuples vs lists.

So, let’s start Python Tuples vs Lists Tutorial.

Python Tuples vs Lists – Comparison between Lists and Tuples

2. A Revision of Tuples in Python

Before comparing tuples and lists, we should revise the two. First, we look at a tuple.
A tuple is a collection of values, and we declare it using parentheses. However, we can also use tuple packing to
do the same, and unpacking to assign its values to a sequence of variables.

1. >>> numbers=(1,2,'three')
2. >>> numbers=4,5,6
3. >>> a,b,c=numbers
4. >>> print(numbers,a,b,c,type(numbers))

(4, 5, 6) 4 5 6 <class ‘tuple’>

A tuple is returned when we call the method localtime().

1. >>> import time


2. >>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=1, tm_hour=23, tm_min=1, tm_sec=59, tm_wday=0,
tm_yday=1, tm_isdst=0)

To access a tuple, we use indexing, which begins at 0.

1. >>> numbers[1]

We can also slice it to retrieve a part of it.

1. >>> numbers[:-1]

(4, 5)

Finally, we can delete an entire tuple.

1. >>> del numbers


2. >>> numbers

Traceback (most recent call last):

File “<pyshell#40>”, line 1, in <module>

numbers

NameError: name ‘numbers’ is not defined

We also learned some functions and methods on tuples and lists. You must read our tutorials on them for more
insight.

3. A Revision of Lists in Python

Unlike in C++, we don’t have arrays to work with in Python. Here, we have a list instead.

We create lists using square brackets.

1. >>>

colors=['red','blue','green'] We can

slice lists too.

1. >>> colors[-2:]

[‘blue’, ‘green’]

Then, we learned how to reassign and delete them.

1. >>> colors[0]='pink'
2. >>> colors

[‘pink’, ‘blue’, ‘green’]


1. >>> del colors[0]
2. >>> colors[0]

‘blue’

1. >>> del colors


2. >>> colors

Traceback (most recent call last):

File “<pyshell#52>”, line 1, in <module>

colors

NameError: name ‘colors’ is not defined

Now that we’ve refreshed our memories, we can proceed to differentiate between python tuples vs lists.

4. Python tuples vs lists – Mutability

The major difference between tuples and lists is that a list is mutable, whereas a tuple is immutable. This means
that a list can be changed, but a tuple cannot.

a. A List is Mutable
Let’s first see lists. Let’s take a new list for exemplar purposes.

1. >>> list1=[0,1,2,3,4,5,6,7]

Now first, we’ll try reassigning an element of a list. Let’s reassign the second element to hold the value 3.

1. >>> list1[1]=3
2. >>> list1

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

Again, let’s see how we can reassign the entire list.

1. >>> list1=[7,6,5,4,3,2,1,0]
2. >>> list1

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

It worked, great.

Now, we will delete just one element from the list.

1. >>> del list1[1]


2. >>> list1

[7, 5, 4, 3, 2, 1, 0]
This was easy, but could we delete a slice of the list? Let’s try it.

1. >>> del list1[3:]


2. >>> list1

[7, 5, 4]

We can access a slice the same way. Can we reassign a slice?

1. >>> nums=[1,2,3,4,5]
2. >>> nums[1:3]=[6,7,8]
3. >>> nums

[1, 6, 7, 8, 4, 5]

Indeed, we can. Finally, let’s try deleting the entire list.

1. >>> del list1


2. >>> list1

Traceback (most recent call last):

File “<pyshell#67>”, line 1, in <module>

list1

NameError: name ‘list1’ is not defined

The list doesn’t exist anymore.

b. A Tuple is Immutable
Now, let’s try doing the same things to a tuple. We know that a tuple is immutable, so some of these operations
shouldn’t work. We’ll take a new tuple for this purpose.

1. >>> mytuple=0,1,2,3,4,5,6,7

First, let’s try reassigning the second element.

1. >>> mytuple[1]=3

Traceback (most recent call last):

File “<pyshell#70>”, line 1, in <module>

mytuple[1]=3

TypeError: ‘tuple’ object does not support item assignment

As you can see, a tuple doesn’t support item assignment.


However, we can reassign an entire tuple.

1. >>> mytuple=2,3,4,5,6
2. >>> mytuple

(2, 3, 4, 5, 6)

Next, let’s try slicing a tuple to access or delete it.

1. >>> mytuple[3:]

(5, 6)

1. >>> del mytuple[3:]

Traceback (most recent call last):

File “<pyshell#74>”, line 1, in <module>

del mytuple[3:]

TypeError: ‘tuple’ object does not support item deletion

As is visible, we can slice it to access it, but we can’t delete a slice. This is because it is immutable.

Can we delete a single element?

1. >>> del mytuple[3]

Traceback (most recent call last):

File “<pyshell#75>”, line 1, in <module>

del mytuple[3]

TypeError: ‘tuple’ object doesn’t support item deletion

Apparently, the answer is no.

Finally, let’s try deleting the entire tuple.

1. >>> del mytuple


2. >>> mytuple

Traceback (most recent call last):

File “<pyshell#77>”, line 1, in <module>

mytuple

NameError: name ‘mytuple’ is not defined


So, here, we conclude that you can slice a tuple, reassign it whole, or delete it whole.

But you cannot delete or reassign just a few elements or a slice.

Let us proceed with more differences between python tuples vs lists.

5. Functions

Some python functions apply on both, these are- len(), max(), min(), sum(), any(), all(), sorted(). We’ll take just
one example here for both containers.

1. >>> max((1,3,-1))

1. >>> max([1,3,-1])

6. Methods

Lists and tuples share the index() and count() methods. But other than those, there are a few methods that apply
to lists. These are- append(), insert(), remove(), pop(), clear(), sort(), and reverse(). Let’s take an example of one
of these.

1. >>> [1,3,2].index(3)

1. >>> (1,3,2).index(3)

1
To get an insight into all of these methods and functions we mentioned, you should refer to our articles on lists
and tuples.

7. Tuples in a List

We can store tuples in a list when we want to.

1. >>> mylist=[(1,2,3),(4,5,6)]
2. >>> type(mylist)

<class ‘list’>

1. >>> type(mylist[1])

<class ‘tuple’>

But when would we need to do this? Take an example.

[(1, ‘ABC’), (2, ‘DEF’), (3, ‘GHI’)]


8. Lists in a Tuple

Likewise, we can also use a tuple to store lists. Let’s see how.

1. >>> mytuple=([1,2],[3,4],[5,6])

9. Nested Tuples

A tuple may hold more tuples, and this can go on in more than two dimensions.

1. >>> mytuple=((1,2),(3,(4,5),(6,(7,(8,9)))))

To access the element with the value 8, we write the following code.

1. >>> mytuple[1][2][1][1][0]

10. Nested Lists

Similarly, a list may hold more lists, in as many dimensions as you want.

1. >>> mylist=[[1,2],[3,4]]
2. >>> myotherlist=[[1,2],[3,[4,5]]]

To access the element with the value 5, we write the following code.

1. >>> myotherlist[1][1][1]

11. When to Use Which

Use a tuple when you know what information goes in the container that it is. For example, when you want to
store a person’s credentials for your website.

1. >>> person=('ABC','admin','12345')

But when you want to store similar elements, like in an array in C++, you should use a list.

1. >>> groceries=['bread','butter','cheese']

Note that this does not say that a list can only contain homogeneous values. Also, you can’t use a list as a key
for a dictionary. This is because only immutable values can be hashed. Hence, we can only set immutable
values like tuples as keys. But if you still want to use a list as a key, you must turn it into a tuple first.

So, this was all


Python Collections Module – Python Counter, DefaultDict, OrderedDict,
NamedTuple
by DataFlair Team · November 15, 2018

1. Collections in Python

Over the last few days, we opened ourselves up to three subclasses of the class ‘dict’, and function
namedtuple(). In this Python Collections Module tutorial, we will study Python Counter, Python DefaultDict,
Python OrderedDict, and Python NamedTuple with their subtypes, syntax and examples.

So, let’s start Python Collection Modules.

Python Collections Module – Introduction

2. Introduction to Python Collections Module

The ‘collections’ module in Python that implements special container datatypes. These provide alternatives to
Python’s general-purpose built-in containers.

Like we said, all three of these are subclasses of Python class ‘dict’.

1. >>> from collections import Counter,defaultdict,OrderedDict,namedtuple


2. >>> issubclass(Counter,dict) and issubclass(defaultdict,dict) and issubclass(OrderedDict,dict)

True

1. >>> type(namedtuple)

<class ‘function’>
To use any of its functionality, we must first import it.
1. >>> import collections

Python Collections Module

Lets’ see all the collections of Python

3. Python Collections Counter

The first thing we discuss in this Python Collection Modeule tutorial, is counter in python collections
module. Python counter is a container that keeps count of the number of occurrences of any value in the
container. It counts hashable objects. Let’s take an example.

a. Python Counter – Syntax


To define a Python counter, we use the Counter() factory function. To it, we can pass a container like Python
list or a tuple, or even a string or a dictionary. We may also use keyword arguments.

1. >>> from collections import Counter


2. >>> c=Counter({'a':3,'b':2,'c':1})
3. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

1. >>> c=Counter('Hello')
2. >>> c

Counter({‘l’: 2, ‘H’: 1, ‘e’: 1, ‘o’: 1})

1. >>> c=Counter(a=3,b=2,c=1)
2. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})


b. Updating a Python Counter
To declare an empty counter in python, and then populate it, we use the update() method.

1. >>> c=Counter()
2. >>> c.update('bfg')
3. >>> c

Counter({‘b’: 1, ‘f’: 1, ‘g’: 1})

c. Accessing Counts in python


To get a value’s count, we pass it as an index to the counter we defined.

1. >>> c['f']

1. >>> c['h']

As you can see, it does not raise a KeyError.

What are Python Errors and How to Handle Python Errors?

The elements() method returns a python iterator for the values in the container.

1. >>> for i in c.elements():


2. print(f"{i}: {c[i]}")

b: 1

f: 1

g: 1

We can also call the most_common() method to get the n most-common values. These are the ones with the
highest frequencies.

1. >>> c=Counter('hello')
2. >>> c

Counter({‘l’: 2, ‘h’: 1, ‘e’: 1, ‘o’: 1})

1. >>> c.most_common(2)

[(‘l’, 2), (‘h’, 1)]

d. Python Counter Arithmetic


We can also perform arithmetic on python counters.
1. >>> c1=Counter('hello')
2. >>> c2=Counter('help')
3. >>> c1+c2

Counter({‘l’: 3, ‘h’: 2, ‘e’: 2, ‘o’: 1, ‘p’: 1})

1. >>> c1&c2

Counter({‘h’: 1, ‘e’: 1, ‘l’: 1})

4. Python DefaultDict

Python DefaultDict collection, lets us provide a default value for keys. We define it using the defaultdict()
factory function, which takes another function as an argument. This function returns a default value for it.

a. Python DefaultDict – Syntax


To define Python defaultdict, we use the factory function defaultdict().

1. >>> from collections import defaultdict


2. >>> d=defaultdict(lambda :35)
3. >>> d['Ayushi']=95
4. >>> d['Bree']=89
5. >>> d['Leo']=90.5
6. >>> d['Adam']

35
Here, we did not initialize ‘Adam’. So, it took 35, because that’s what our function returns to defaultdict(). We
can also check the default value with the missing () method.

1. >>> d. missing ('Adam')

35

b. Using a Type as a Default Factory


We can tell the interpreter what type of values we’re going to work with. We do this by passing it as an
argument to defaultdict().

1. >>> d=defaultdict(list)
2. >>> for i,j in [('a',(1,2)),('b',(3,4)),('c',(5,6))]:
3. d[i].append(j)
4. >>> d

defaultdict(<class ‘list’>, {‘a’: [(1, 2)], ‘b’: [(3, 4)], ‘c’: [(5, 6)]})

5. Python OrderedDict

Python OderDict, remembers the order in which the key-value pairs were added. Let’s take python ordereddict
example.
1. >>> from collections import OrderedDict
2. >>> o=OrderedDict()
3. >>> o['a']=3
4. >>> o['c']=1
5. >>> o['b']=4
6. >>> o

OrderedDict([(‘a’, 3), (‘c’, 1), (‘b’, 4)])

a. Move_to_end()
We’ll take a look at two methods in python orderedDict. The first we discuss is move_to_end(). It lets us move
a key-value pair either to the end, or to the front.

1. >>> o.move_to_end('c')
2. >>> o

OrderedDict([(‘a’, 3), (‘b’, 4), (‘c’, 1)])

1. >>> o.move_to_end('c',last=False)
2. >>> o

OrderedDict([(‘c’, 1), (‘a’, 3), (‘b’, 4)])

b. Popitem()
This method lets us pop a key-value pair out of the container, and then displays it.

1. >>> o.popitem()

(‘b’, 4)

1. >>> o.popitem(last=False)

(‘c’, 1)

1. >>> o

OrderedDict([(‘a’, 3)])

6. Python NamedTuple

Finally, in python collections module we discuss Python NamedTuple. This is a container that

lets us access elements using names/labels.

1. >>> from collections import namedtuple


2. >>> colors=namedtuple('colors','r g b')
3. >>> red=colors(r=255,g=0,b=0)
a. Accessing Elements
To access these elements, we use the dot Python operator.

1. >>> red.r

255

1. >>> red.g

1. >>> red.b

Or, we could just use indices.

1. >>> red[0]

255
We can also use the getattr() function.

1. >>> getattr(red,'r')

255
Python namedtuple is immutable. So, you can’t reassign a value directly.

1. >>> red.r=3
2. Traceback (most recent call last):
3. File "<pyshell#95>", line 1, in <module>
4. red.r=3

AttributeError: can’t set attribute

b. Converting into Python Dictionary


To convert a namedtuple into Python dictionary, we use the _asdict() method.

1. >>> red._asdict()

OrderedDict([(‘r’, 255), (‘g’, 0), (‘b’, 0)])

c. Converting an Iterable into a namedtuple


The _make() method lets us create a namedtuple from Python list and the format we specified.

1. >>> colors._make(['1','2','3'])

colors(r=’1′, g=’2′, b=’3′)


Follow this link to know more about Python Iterables.

d. Creating a namedtuple from the dictionary


To use a dictionary to make python namedtuple, we use this code:

1. >>> colors(**{'r':255,'g':0,'b':0})

colors(r=255, g=0, b=0)

e. Checking What Fields Belong to the Tuple


For this, we have the _fields attribute.

1. >>> red._fields

(‘r’, ‘g’, ‘b’)

f. Changing a Value
Like we said, Python namedtuples are immutable. But to change a value, we can use the _replace() method.

1. >>> red._replace(g=3)

colors(r=255, g=3, b=0)

So, this was all about Pythons Collections Module Tutorial. Hope you like our explanation.

7. Conclusion
Now that we’ve discussed all four Python collections module- Counter, defaultdict, OrderedDict, and namedtuple with
their syntax, methods, and examples of python collections module. We can move on to other things. Hope you had fun, If
you had any query, write us in a comment box we surely get back to you.

Python Counter with Example & Python Collections Type


by DataFlair Team · May 27, 2019

1. Python Counter Tutorial


Earlier, we discussed three classes from Python module collections. Today, we’ll talk about another such
class- Python Counter. Here, we will study initializing, updating, accessing, and reassigning counters in Python.
Moreover, we will learn Python counter list, loops and arithmetic.

So, let’s start the Python Counter Tutorial.


Python Counter with Example & Python Collections Type

2. Introduction to Python Counter

Python Counter, like the other three containers we mentioned above, is a subclass of ‘dict’. It keeps a count of
the number of occurrences of any value in the container. Simply speaking, if you add the value ‘hello’ thrice in
the container, it will remember that you added it thrice. So, Counter counts hashable objects in Python. Lets
see this Python Counter example.

1. >>> from collections import Counter


2. >>> c=Counter(['a','b','c','a','b','a'])
3. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

1. >>> c['a']

1. >>> issubclass(Counter,dict)

True
Now, let’s take a look at python counter syntax.

3. Initializing a Python Counter

To initialize or define a counter in python, we use the counter factory function. Yet, in that, we can do it in three
ways:

a. Using a List or Similar Containers


We can pass Python list of values to Counter(). Every time it encounters a value again, it raises its count by 1.

1. >>> c=Counter(['a','b','c','a','b','a'])
2. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})


As you can see, it could recognize that there are three ‘a’s in the list in python, two ‘b’s, and one ‘c’.
We can also use Python tuple.

1. >>> c=Counter(('a','b','c','a','b','a'))
2. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})


Or, you can pass it Python string.

1. >>> c=Counter("Hello")
2. >>> c

Counter({‘l’: 2, ‘H’: 1, ‘e’: 1, ‘o’: 1})


Python string is a container too, remember? Also, it displays the counts in a descending order. But when we use
a Set, it only holds every value once. So, it does not make sense to use Counter() with Python Set.

1. >>> c=Counter({'a','b','c','a','b','a'})
2. >>> c

Counter({‘a’: 1, ‘c’: 1, ‘b’: 1})

b. Using a Python Dictionary


We can also manually tell the Python Counter the count of values, using a dictionary in Python.

1. >>> c=Counter({'a':3,'b':2,'c':1})
2. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

c. Using Keyword Arguments


Finally, we can use keyword arguments to manually tell Counter() the count.

1. >>> c=Counter(a=3,b=2,c=1)
2. >>> c

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

4. Updating a Python Counter

Like it is with every other container, we can first declare an empty Python Counter and then populate it. We
perform the update calling the update() method on the Counter.

1. >>> d=Counter()
2. >>>
d.update("Hello") 3. >>>
d

Counter({‘l’: 2, ‘H’: 1, ‘e’: 1, ‘o’: 1})


We updated it once; we can update it further, in a different way if we want.

1. >>> d.update({'e':2,'o':4})
2. >>> d

Counter({‘o’: 5, ‘e’: 3, ‘l’: 2, ‘H’: 1})


Here, we told Python Counter that we’re adding two more ‘e’s and four more ‘o’s. Then, when we accessed it, it
printed the counts in a descending order.

5. Accessing Counts in Python

We can access the count for a particular value simply by using it as an index to the Python Counter we defined.

1. >>> d['e']

3
Let’s try displaying the counts of values in a string, with Python counter that takes a different string as an
argument.

1. >>> e=Counter("Hello")
2. >>> for i in "Help":
3. print(f"{i}: {e[i]}")
4. H: 1
5. e: 1
6. l: 2
7. p: 0

From this, we interpret that in Python Counter e, ‘H’ has a count of 1, ‘e’ has a count of 1, ‘l’ has 2, and ‘p’ has
none(hence, 0).
From this, we conclude that if a key doesn’t exist, it will consider its count to be 0, instead of raising a
KeyError.

1. >>> e['q']

To compare, let’s also see how this would fair in a regular dictionary.

1. >>> dict1={'H':1,'e':1,'l':2,'o':1}
2. >>> dict1['p']
3. Traceback (most recent call last):
4. File "<pyshell#36>", line 1, in <module>
5. dict1['p']

KeyError: ‘p’

a. The elements() Method


Or, we could use the elements() method, which returns an iterator object for the values in the Counter. We can
use this with a Python for-loop.

1. >>> e=Counter({'a':3,'b':2,'c':1,'d':0})
2. >>> for i in e.elements():
3. print(f"{i}: {e[i]}")
4. a: 3
5. a: 3
6. a: 3
7. b: 2
8. b: 2
9. c: 1
10. >>> e.elements()

<itertools.chain object at 0x06336390>


We’ll discuss python itertools in a later lesson.

b. Accessing the Most Common Values


To get n most-common values (the ones with the highest frequencies), we call the method most_common().

1. >>> e.most_common(2)

[(‘a’, 3), (‘b’, 2)]

1. >>> e.most_common(4)

[(‘a’, 3), (‘b’, 2), (‘c’, 1), (‘d’, 0)] When we call it without any argument, however, we get all the items. So, it is
the same as calling it with an argument with a value equal to the number of elements in Python counter.

1. >>> e.most_common()

[(‘a’, 3), (‘b’, 2), (‘c’, 1), (‘d’, 0)]

6. Reassigning Counts in Python

Of course, Python Counters aren’t immutable. You can reassign a count the following way:

1. >>> e['e']=5
2. >>> e

Counter({‘e’: 5, ‘l’: 2, ‘H’: 1, ‘o’: 1})


To clear a Python counter, we use clear().

1. >>> b.clear()
2. >>> b

Counter()
7. Python Counter Arithmetic

Finally, we’ll discuss some Counter arithmetic in Python.


To aggregate results, we can perform arithmetic and set operations on Python Counters. Let’s take two sample
Counters for exemplary purposes.

1. >>> a=Counter({'a':3,'b':2,'c':1})
2. >>> b=Counter({'c':3,'d':2,'e':1})

Now, sit back and watch it unfold.

1. >>> a+b

Counter({‘c’: 4, ‘a’: 3, ‘b’: 2, ‘d’: 2, ‘e’: 1})

1. >>> a-b

Counter({‘a’: 3, ‘b’: 2})

1. >>> b-a

Counter({‘c’: 2, ‘d’: 2, ‘e’: 1})


Here, a-b does not have ‘c’, because 1-3=-2. A negative count does not mean anything. Likewise, b-a has ‘c’
with a count of 2, because 3-1=2. We can also do this calling the subtract() method.

1. >>> a.subtract(b)
2. >>> a

Counter({‘a’: 3, ‘b’: 2, ‘e’: -1, ‘c’: -2, ‘d’: -2})


This way, it gives us negative counts if it has to.
Now, look at the following code:

1. >>> a. add (b)

Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

1. >>> a&b

Counter({‘c’: 1})

1. >>> a|b

Counter({‘a’: 3, ‘c’: 3, ‘b’: 2, ‘d’: 2, ‘e’: 1})


Here, a|b has a count of 3 for ‘c’, because that is what OR does.

1. >>> a=Counter({'e':-1})
2. >>> -a

Counter({‘e’: 1})
If you’ve forgotten about these operators, read up on Python Operators.
Check out the result for dir(Counter), and try to apply all of those methods to your Counters in Python.
8. Conclusion

Python Counter is a container that keeps track of the number of occurrences of a value. Today, we looked at
methods like update(), most_common(), clear(), elements(), and subtract(). In our next lesson, we’ll revise all
these four classes in brief.

Python Namedtuple – Working and Benefits of Namedtuple in Python


by DataFlair Team · September 26, 2018

1. Python Namedtuple Tutorial

In our journey so far, we have seen Python Tuple, which is an important container type. Today, we will talk
about python namedtuple with its example & syntax. Moreover, we will start our learning with a quick revision
of Python tuple and Accessing Elements of python namedtuple. At last, we will cover the working, and benefits
of python namedtuple.

So, let’s start the Python Namedtuple Tutorial.

Python Namedtuple – Working and Benefits of Namedtuple in Python

2. Python Tuple – A Revision

Before we proceed, we think we must review tuples in python.


A python tuple is a container that can hold different values. Let’s take an example.

1. >>> colors=(255,0,0)
As you can see, we define it using parentheses. To access an element, we use indices. (And remember, indexing
starts at 0)

1. >>> colors[0]

255
What makes a >>> colors[0] python tuple unique is that its values cannot be changed, i.e., it is immutable. Let’s
try changing a value.

1. >>> colors[1]=10

Traceback (most recent call last):

File “<pyshell#316>”, line 1, in <module> c

olors[1]=10

TypeError: ‘tuple’ object does not support item assignment


Now that we’ve taken a quick revision at tuples, let’s proceed to python syntax.

3. Python Namedtuple – Syntax

A Python namedtuple lets us access elements in a tuple using names/labels. To define it, we import namedtuple
from Python collections module and use the namedtuple() factory function.

1. >>> from collections import namedtuple


2. >>> Colors=namedtuple('Colors','red green blue')

Here, we use the function namedtuple().

1. >>> type(namedtuple)

<class ‘function’>

This takes two arguments-

1. Tuple name- Here, Colors is the tuple


2. Python string of fields, separated by spaces- Here, we have three fields- red, green, and blue.

Now, let’s define some python tuples.

1. >>> red=Colors(red=255,green=0,blue=0)
2. >>> green=Colors(red=0,green=255,blue=0)
3. >>> blue=Colors(red=0,green=0,blue=255)
4. >>> red

Colors(red=255, green=0, blue=0)


We’ll just leave here another way to define a python namedtuple- using a list instead of a string of fields.

1. >>> pets=namedtuple('pets',['name','age'])
2. >>> Fluffy=pets('Fluffy',8)
3. >>> Fluffy

pets(name=’Fluffy’, age=8)

4. Accessing Elements in a Python Namedtuple

To access an item, we can now use the dot-operator to access a field by its name. We do it this way:

1. >>> red.red

255

1. >>> red.green

1. >>> red.blue

See how simple it makes this? This way, a namedtuple is something like a python dictionary. But really,
python namedtuple is backward-compatible to a regular python tuple. This means that we can access a value
using indexing as well.

1. >>> red[0]

255

1. >>> red[1]

Finally, you can also access a value using the getattr() function.

1. >>> getattr(red,'green')

To see what kind of python objects these are, we use the type() function, like we always do.

1. >>> type(Colors)

<class ‘type’>

1. >>> type(red)

<class ‘ main .Colors’>

1. >>> type(red.red)

<class ‘int’>

5. How Namedtuple in Python Works?

Let’s see some more things we can do to a namedtuple in python.

a. A Python namedtuple is Immutable


Like its regular counterpart, a python namedtuple is immutable. We can’t change its attributes. To prove this,
we’ll try changing one of the attributes of a tuple of type ‘Colors’.

1. >>> red.blue=1

Traceback (most recent call last):

File “<pyshell#319>”, line 1, in <module>

red.blue=1

AttributeError: can’t set attribute


As is visible, it raises an AttributeError. Hence, we conclude that a python namedtuple is immutable.

b. Converting a Python namedtuple into a Python Dictionary


A namedtuple in Python is much like a dictionary, but if we want to convert it into one, we can:

1. >>> red._asdict()

OrderedDict([(‘red’, 255), (‘green’, 0), (‘blue’, 0)])

As you can see, we use the ._asdict() function for this. Also, this gives us a Python OrderedDict. We’ll see
that in detail in its own lesson.

c. Creating a namedtuple from a python Iterable


If you have a python list, you can make a namedtuple from it. But for this, you must have defined the format.

1. >>> Colors._make(['Purple','Violet','Gold'])

Colors(red=’Purple’, green=’Violet’, blue=’Gold’)


Follow this link to know more about Python Iterable

d. Converting a Python Dictionary into a namedtuple


CoA conversef what we learned in point b, we can turn a dictionary into a namedtuple in Python. We do this
using two asterisks before the dictionary to be passed as an argument to Colors here.

1. >>> Colors(**{'red':255,'green':0,'blue':0})

Colors(red=255, green=0, blue=0)

e. Checking what fields belong to a Python tuple


For this, we use the _fields attribute.

1. >>> red._fields

(‘red’, ‘green’, ‘blue’)


f. Changing a Value
Yes, a namedtuple’s attributes are immutable. But we can use the _replace() function to change them.

1. >>> red._replace(green=1)

Colors(red=255, green=1, blue=0)

6. Benefits of Python Namedtuple

Of course, no one would be using these if they observed no benefits at all. So, here we go:
1. Unlike a regular tuple, python namedtuple can also access values using names of the fields.

1. >>> red.red

255
2. Python namedtuple is just as memory-efficient as a regular tuple, because it does not have per-instance
dictionaries. This is also why it is faster than a dictionary.

7. Conclusion

Today, we learned about Python namedtuple. It is just like a regular tuple, apart from the fact that it can also
access fields through their names. This is easy on the programmer. It is also more memory-efficient than a
dictionary. Next, we will discuss some more functions from the collections module. Till give us your valuable
feedbacks.

Python Defaultdict – Int and List as a Default Factory


by DataFlair Team · September 26, 2018

1. Python Defaultdict

In today’s lesson, we will look at Python defaultdict, a subclass of the built-in dict class. So let’s start with
defaultdict Python 3 tutorial. Here, we will discuss what is Python Defaultdict with its syntax and exmaple.
Moreover, we will study Python Defaultdict using Int and List as a defaultdict in Python.

Dictionaries are an important part of Python. They are containers to hold key-value pairs.

1. >>> issubclass(defaultdict,dict) #You’ll need to import defaultdict from collections for this

True

So, let’s start Python Defualtdict Tutorial.


Python Defaultdict – Int and List as a Default Factory

2. A Revision – Python Dictionaries

Before we proceed to defaultdict in Python, we suggest you read up on Python Dictionaries.

A python dictionary is a container that holds key-value pairs. Keys must be unique, immutable objects. This
means that a Python tuple can be a key, but a python list can’t since it is mutable.

1. >>> {[1,2]:1,2:2}
2. Traceback (most recent call last):
3. File "<pyshell#355>", line 1, in <module>
4. {[1,2]:1,2:2}

TypeError: unhashable type: ‘list’

As you can see, a list, then, is unhashable. Let’s take a quick example to see how to define a python dictionary.

1. >>> mydict={'a':1,'b':2,'c':3}
2. >>> mydict['b']

2
We can also use the dict() function.

1. >>> otherdict=dict((['a',1],['b',2],['c',3]))
2. >>> otherdict

{‘a’: 1, ‘b’: 2, ‘c’: 3}

To get a list of keys, we use the keys() method.

1. >>> otherdict.keys()

dict_keys([‘a’, ‘b’, ‘c’])


But we have one problem. If the key does not exist, and we try to access it, it raises a KeyError.

1. >>> otherdict['d']
2. Traceback (most recent call last):
3. File "<pyshell#364>", line 1, in <module>
4. otherdict['d']

KeyError: ‘d’

3. Introduction to Python Defaultdict

To deal with this situation, we have defaultdict in Python. First, we must import it from Python collections
module.

1. >>> from collections import defaultdict

Here, we use the Python defaultdict() factory function. It takes, as an argument, a function object that will return
a value. Actually, if we don’t provide a value for a particular key, it will take that value for it. Let’s take a look
at the syntax.

4. Python Defaultdict – Syntax

Before Python defaultdict syntax, we revise python syntax.

As we discussed, we first import defaultdict from the ‘collections’ module. Let us see this Python defaultdict
example.

1. >>> from collections import defaultdict

Next, we define a python function to return a default value for the keys we don’t initialize.

1. >>> def defaultvalue():


2. return 0

Now, we create a Python defaultdict using the defaultdict() function. To this, we pass the function defaultvalue
as a function argument. Remember, you must pass the function object, you don’t have to call it with
parentheses.

1. >>> otherdict=defaultdict(defaultvalue)

Let’s now define values for keys ‘a’, ‘b’, and ‘c’.

1. >>> otherdict['a']=1
2. >>> otherdict['b']=2
3. >>> otherdict['c']=3

What if we access the value for key ‘d’? We haven’t initialized it yet.

1. >>> otherdict['d']
As you can see, it assigned it the default value we specified through function defaultvalue().
Let’s see the whole code at once now.

1. >>> def defaultvalue():


2. return 0
3. >>> otherdict=defaultdict(defaultvalue)
4. >>> otherdict['a']=1
5. >>> otherdict['b']=2
6. >>> otherdict['c']=3
7. >>> otherdict['d']

5. Another Example of Python Defaultdict

Let’s take another python defaultdict example. We take a defaultdict in python to hold the marks of students.
Also, we let the default value for marks be 35.

1. >>> marks=defaultdict(lambda :35)


2. >>> marks['Ayushi']=95
3. >>> marks['Gigi']=86
4. >>> marks['Ayushi']

95
Here, we used a Lambda Expression since all we need to do in it is to return a value. So why waste space,
right? Now, if we try to access the value to a key that doesn’t exist, it saves 35 to it.

1. >>> marks['Oshin']

35
Finally, these are the keys to this python defaultdict now:

1. >>> marks.keys()

dict_keys([‘Ayushi’, ‘Gigi’, ‘Oshin’])

6. More on Defaultdict in Python


Let’s see what happens if we return a None from the function.

1. >>> a=defaultdict(lambda :None)


2. >>> a[1]
3. >>> type(a[1])

<class ‘NoneType’>

What if a python exception was raised in the factory function?

1. >>> def defaultvalue():


2. print(1/0)
3. return 0
4. >>> a=defaultdict(defaultvalue)
5. >>> a[1]
Traceback (most recent call last):

File “<pyshell#386>”, line 1, in <module>

a[1]

File “<pyshell#384>”, line 2, in defaultvalue

print(1/0)

ZeroDivisionError: division by zero

Python defaultdict supports all operations that a regular dictionary does. These include keys() and len().

1. >>> dir(a)

[‘ class ’, ‘ contains ’, ‘ copy ’, ‘ delattr ’, ‘ delitem ’, ‘ dir ’, ‘ doc ’, ‘ eq ’,


‘ format ’, ‘ ge ’, ‘ getattribute ’, ‘ getitem ’, ‘ gt ’, ‘ hash ’, ‘ init ’,
‘ init_subclass ’, ‘ iter ’, ‘ le ’, ‘ len ’, ‘ lt ’, ‘ missing ’, ‘ ne ’, ‘ new ’,
‘ reduce ’, ‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ setitem ’, ‘ sizeof ’, ‘ str ’,
‘ subclasshook ’, ‘clear’, ‘copy’, ‘default_factory’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’,
‘setdefault’, ‘update’, ‘values’]

Let’s know about Exception handling in python.

a. missing (key)

This Python method is called by the getitem () method of the dict class when the requested key isn’t
found.

1. >>> from collections import defaultdict


2. >>> a=defaultdict(lambda :0)
3. >>> a['a']=1
4. >>> a['b']=2
5. >>> a['c']=3
6. >>> a. missing ('d')

It returned 0 here because that’s what we specified as a default value through a lambda expression to the
defaultdict() factory function.

1. >>> a. getitem ('a')

1. >>> a. getitem ('e')

7. Using ‘list’ as a Default Factory

If we pass ‘list’ (without the quotes) to the defaultdict() Pytohn function, we can group a sequence of key-value
pairs into a dictionary of lists. We can do this to more types as well. We’ll see another in the next section.
1. >>> a=[('a',1),('b',2),('c',3)]
2. >>> b=defaultdict(list)
3. >>> for i,j in a:
4. b[i].append(j)
5. >>> b

defaultdict(<class ‘list’>, {‘a’: [1], ‘b’: [2], ‘c’: [3]})

Let’s explain what this code does. First, we define ‘a’ as a list of tuples to hold the key-value pairs. Next, we
pass ‘list’ to defaultdict(), and store this in ‘b’. This tells the interpreter that b will hold a dictionary with values
that are list.

Then, we traverse on the tuples using names ‘I’ and ‘j’ with a for-loop. In this, we append j to b[i]. What this
means is that we take the second value in each tuple in ‘a’, and append it to the python defaultdict for each key
(first value in each tuple). For ‘c’ in the list ‘a’, as a key in the defaultdict ‘b’, we append the value 3 to the
python defaultdict to that key. This is its value now.

8. Using ‘int’ as a Default Factory

We can tell the interpreter to create a Python dictionary of integers. Here’s how.

1. >>> name='Bubbles'
2. >>> mydict=defaultdict(int)
3. >>> for i in name:
4. mydict[i]+=1
5. >>> mydict

defaultdict(<class ‘int’>, {‘B’: 1, ‘u’: 1, ‘b’: 2, ‘l’: 1, ‘e’: 1, ‘s’: 1})

This way, we can use it as a counter. Of course, we could have done this with a regular dictionary as well. But
here, we tell the interpreter that we’re going to work with only integer values.

So, this was all about Python Defaultdict Tutorial. Hope you like our explanation.

9. Conclusion
With a subtle difference to a regular dictionary, we see that a python defaultdict is actually quite useful. It has a
simple syntax as well and lets us define a default value for uninitialized keys. It also lets us tell the interpreter
that we want to work with a particular type of values.

Python OrderedDict – Reordering, Delete and Reinsert New


OrderedDict
by DataFlair Team · September 26, 2018
1. Python OrderedDict
We’ve been talking about classes from Python collections module– python namedtuple, and python
defaultdict. Today, we base our article on Python OrderedDict, another class in ‘collections’. Here we study
what is Python OrderedDict with its syntax and examples. Moreover, we will study comparing to a regular
dictionary, and reordering a OrderedDict in Python. At last, we will cover how to delete and reinsert a key for
new ordereddict, and popitem method.

So, let’s start the Python OderedDict Tutorial.

Python OrderedDict – Reordering, Delete and Reinsert New OrderedDict

2. Introduction to Python OrderedDict


Python OrderedDict is just like a regular dictionary in python, but it also saves the order in which pairs were
added. Actually, it is a subclass of ‘dict’.

1. >>> issubclass(OrderedDict,dict)

True

3. Python OrderedDict Syntax


To define an OrderedDict in python, we import it from the module ‘collections’. Let us see this Python
ordereddict example.
1. >>> from collections import OrderedDict

Then, we call this factory function and assign it to a variable in python.

1. >>> d=OrderedDict()

We now add some key-value pairs to it.

1. >>> d['a']=1
2. >>> d['e']=5
3. >>> d['d']=4
4. >>> d['b']=2
5. >>> d['c']=3

Now, if we access ‘d’, we see that it preserved the order in which we defined the key-value pairs.

1. >>> d

OrderedDict([(‘a’, 1), (‘e’, 5), (‘d’, 4), (‘b’, 2), (‘c’, 3)])

4. Comparing to a Regular Dictionary


Let’s try this with a regular dictionary. If we declared this as a regular dictionary, it would produce the pairs
according to how the keys are stored in a hash table. This depends on a random value to reduce collisions.

a. Equality

To a regular dictionary, the order does not matter. Let u take another Python ordereddict example.

1. >>> {'a': 1, 'e': 5, 'd': 4, 'b': 2, 'c': 3}=={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

True
To Python, these two are equal. Let’s create another OrderedDict, and check if it is the same as the OrderedDict
‘d’ we defined above.

1. >>> e=OrderedDict()
2. >>> e['a']=1
3. >>> e['b']=2
4. >>> e['c']=3
5. >>> e['d']=4
6. >>> e['e']=5
7. >>> e

OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4), (‘e’, 5)])
Now, let’s check if they’re equal.

1. >>> d==e
False
As you can see, they’re not.

5. How to Reorder Python OrderedDict?


To reorder a key-value pair to the front or the end of an OrderedDict in python, we have the move_to_end()
method. Look, this is ‘d’:

1. >>> d

OrderedDict([(‘a’, 1), (‘e’, 5), (‘d’, 4), (‘b’, 2), (‘c’, 3)])
Now, let’s move (‘e’,5) to the end.

1. >>> d.move_to_end('e')
2. >>> d

OrderedDict([(‘a’, 1), (‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5)])

This method takes a key as an argument. But it may also take another argument- ‘last’. This may take one of
two values: True or False. When true, the item is moved to the end. And when false, it is moved to the front.

1. >>> d.move_to_end('a',last=True)
2. >>> d

OrderedDict([(‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5), (‘a’, 1)])
Here, we moved (‘a’,1) to the end. Now, let’s move it back to the front.

1. >>> d.move_to_end('a',last=False)
2. >>> d

OrderedDict([(‘a’, 1), (‘d’, 4), (‘b’, 2), (‘c’, 3), (‘e’, 5)])
Now, to sort this, we do the following:

1. >>> d.move_to_end('b')
2. >>> d

OrderedDict([(‘a’, 1), (‘d’, 4), (‘c’, 3), (‘e’, 5), (‘b’, 2)])

1. >>> d.move_to_end('c')
2. >>> d

OrderedDict([(‘a’, 1), (‘d’, 4), (‘e’, 5), (‘b’, 2), (‘c’, 3)])

1. >>> d.move_to_end('d')
2. >>> d

OrderedDict([(‘a’, 1), (‘e’, 5), (‘b’, 2), (‘c’, 3), (‘d’, 4)])

1. >>> d.move_to_end('e')
2. >>> d

OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4), (‘e’, 5)])

6. Assigning a Key Again in Python


OrderedDict
Let’s define a new OrderedDict in Python for this example.

1. >>> f=OrderedDict()
2. >>> f['c']=3
3. >>> f['e']=5
4. >>> f['a']=1
5. >>> f['b']=2
6. >>> f['d']=4
7. >>> f

OrderedDict([(‘c’, 3), (‘e’, 5), (‘a’, 1), (‘b’, 2), (‘d’, 4)])
Now, let’s try changing the value of key ‘e’.

1. >>> f['e']=7
2. >>> f

OrderedDict([(‘c’, 3), (‘e’, 7), (‘a’, 1), (‘b’, 2), (‘d’, 4)])
Okay. Since the key ‘e’ already exists, the old order is preserved; it isn’t ordered to the end.

7. Deleting and Reinserting a Key


When we delete a key and then reinsert it, it is ordered to the end of the new order. Let’s try deleting ‘e’.

1. >>> f.pop('e')

1. >>> f

OrderedDict([(‘c’, 3), (‘a’, 1), (‘b’, 2), (‘d’, 4)])


Now, let’s insert it again.

1. >>> f

OrderedDict([(‘c’, 3), (‘a’, 1), (‘b’, 2), (‘d’, 4), (‘e’, 7)])
As you can see, it is at the back of the list now.
8. Python Popitem ()
The popitem() method in Python may take 0 or 1 argument- ‘last’. When ‘last’ is true, LIFO is followed to
delete, otherwise, FIFO is followed. When we don’t pass an argument, last is assumed to be true.
Let’s take a new Python OrderedDict for this.

1. >>> g=OrderedDict()
2. >>> g['d']=4
3. >>> g['b']=2
4. >>> g['a']=1
5. >>> g['c']=3
6. >>> g['e']=5
7. >>> g

OrderedDict([(‘d’, 4), (‘b’, 2), (‘a’, 1), (‘c’, 3), (‘e’, 5)])
Now, let’s start popping.

1. >>> g.popitem(last=False)

(‘d’, 4)

1. >>> g.popitem()

(‘e’, 5)

1. >>> g.popitem(last=True)

(‘c’, 3)

So, this was all about Python OrderedDict Tutorial. Hope you like our explanation.

9. Conclusion
Hopefully, we’ve covered every important detail about python orderedDict in this article. We saw how to define
them, Syntax, deleting and reinserting, assigning A key to python ordereddict, and we saw methods
move_to_end(), popitem(), and pop(). Next, we will discuss Counter python module, yet another class from
collections.
Python Dictionary with Methods, Functions
and
Dictionary Operations
by DataFlair Team · May 27, 2019

1. Python Dictionary Tutorial


In the last few lessons, we have learned about some Python constructs like lists and tuples. Today, we will have
a word about Python dictionary which is another type of data structure in Python. Moreover, we will study how
to create, access, delete, reassign dictionary in Python. Along with this, we will learn Python Dictionary method
and operations.

So, let’s start the Python Dictionary Tutorial.

Python Dictionary

2. An Introduction to Python Dictionaries


A real-life dictionary holds words and their meanings. As you can imagine, likewise, a Python dictionary holds
key-value pairs. Let’s look at how to create one.
3. How to Create a Dictionary in Python?
Creating a Python Dictionary is easy as pie. Separate keys from values with a colon(:), and a pair from another
by a comma(,). Finally, put it all in curly braces.

1. >>> {'PB&J':'Peanut Butter and Jelly','PJ':'Pajamas'}

{‘PB&J’: ‘Peanut Butter and Jelly’, ‘PJ’: ‘Pajamas’}

Optionally, you can put the dictionary in a variable. If you want to use it later in the program, you must do this.

1. >>> lingo={'PB&J':'Peanut Butter and Jelly','PJ':'Pajamas'}

To create an empty dictionary, simply use curly braces and then assign it to a variable

1. >>> dict2={}
2. >>> dict2

{}

1. >>> type(dict2)

<class ‘dict’>

a. Python Dictionary Comprehension


You can also create a Python dict using comprehension. This is the same thing that you’ve learned in your math
class. To do this, follow an expression pair with a for-statement loop in python. Finally, put it all in curly
braces.

1. >>> mydict={x*x:x for x in range(8)}


2. >>> mydict

{0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6, 49: 7}

In the above code, we created a Python dictionary to hold squares of numbers from 0 to 7 as keys, and numbers
0-1 as values.

b. Dictionaries with mixed keys


It isn’t necessary to use the same kind of keys (or values) for a dictionary in Python.

1. >>> dict3={1:'carrots','two':[1,2,3]}
2. >>> dict3

{1: ‘carrots’, ‘two’: [1, 2, 3]}


As you can see here, a key or a value can be anything from an integer to a list.

c. dict()
Using the dict() function, you can convert a compatible combination of constructs into a Python dictionary.

1. >>> dict(([1,2],[2,4],[3,6]))

{1: 2, 2: 4, 3: 6}

However, this function takes only one argument. So if you pass it three lists, you must pass them inside a list or
a tuple. Otherwise, it throws an error.

1. >>> dict([1,2],[2,4],[3,6])

Traceback (most recent call last):

File “<pyshell#121>”, line 1, in <module>

dict([1,2],[2,4],[3,6])

TypeError: dict expected at most 1 arguments, got 3

d. Declaring one key more than once


Now, let’s try declaring one key more than once and see what happens.

1. >>> mydict2={1:2,1:3,1:4,2:4}
2. >>> mydict2

{1: 4, 2: 4}

As you can see, 1:2 was replaced by 1:3, which was then replaced by 1:4. This shows us that a dictionary cannot
contain the same key twice.

e. Declaring an empty dictionary and adding elements later


When you don’t know what key-value pairs go in your Python dictionary, you can just create an empty Python
dict, and add pairs later.

1. >>> animals={}
2. >>> type(animals)

<class ‘dict’>

1. >>> animals[1]='dog'
2. >>> animals[2]='cat'
3. >>> animals[3]='ferret'
4. >>> animals

{1: ‘dog’, 2: ‘cat’, 3: ‘ferret’}

Any query on Python Dictionay yet? Leave a comment.

4. How to Access a Python Dictionary?


a. Accessing the entire Python dictionary
To get the entire dictionary at once, type its name in the shell.

1. >>> dict3

{1: ‘carrots’, ‘two’: [1, 2, 3]}

b. Accessing a value
To access an item from a list or a tuple, we use its index in square brackets. This is the python syntax to be
followed. However, a Python dictionary is unordered. So to get a value from it, you need to put its key in square
brackets.

To get the square root of 36 from the above dictionary, we write the following code.

1. >>> mydict[36]

c. get()
The Python dictionary get() function takes a key as an argument and returns the corresponding value.

1. >>> mydict.get(49)

d. When the Python dictionary keys doesn’t exist


If the key you’re searching for doesn’t exist, let’s see what happens.

1. >>> mydict[48]

Traceback (most recent call last):

File “<pyshell#125>”, line 1, in <module>

mydict[48]
KeyError: 48

Using the key in square brackets gives us a KeyError. Now let’s see what the get() method returns in such a
situation.

1. >>> mydict.get(48)
2. >>>

As you can see, this didn’t print anything. Let’s put it in the print statement to find out what’s going on.

1. >>> print(mydict.get(48))

None

So we see, when the key doesn’t exist, the get() function returns the value None. We discussed it earlier, and
know that it indicates an absence of value.

5. Reassigning a Python Dictionary


The python dictionary is mutable. This means that we can change it or add new items without having to reassign
all of it.

a. Updating the Value of an Existing Key


If the key already exists in the Python dictionary, you can reassign its value using square brackets.

Let’s take a new Python dictionary for this.

1. >>> dict4={1:1,2:2,3:3}

Now, let’s try updating the value for the key 2.

1. >>> dict4[2]=4
2. >>> dict4

{1: 1, 2: 4, 3: 3}

b. Adding a new key


However, if the key doesn’t already exist in the dictionary, then it adds a new one.

1. >>> dict4[4]=6
2. >>> dict4

{1: 1, 2: 4, 3: 3, 4: 6}

Python dictionary cannot be sliced.


6. How to Delete Python Dictionary?
You can delete an entire dictionary. Also, unlike a tuple, a Python dictionary is mutable. So you can also delete
a part of it.

a. Deleting an entire Python dictionary


To delete the whole Python dict, simply use its name after the keyword ‘del’.

1. >>> del dict4


2. >>> dict4

Traceback (most recent call last):

File “<pyshell#138>”, line 1, in <module>

dict4

NameError: name ‘dict4’ is not defined

b. Deleting a single key-value pair


To delete just one key-value pair, use the keyword ‘del’ with the key of the pair to delete.

Now let’s first reassign dict4 for this example.

1. >>> dict4={1:1,2:2,3:3,4:4}

Now, let’s delete the pair 2:2

1. >>> del dict4[2]


2. >>> dict4

{1: 1, 3: 3, 4: 4}

A few other methods allow us to delete an item from a dictionary in Python. We will see those in section 8.

7. In-Built Functions on a Python Dictionary


A function is a procedure that can be applied on a construct to get a value. Furthermore, it doesn’t modify the
construct. Python gives us a few functions that we can apply on a Python dictionary. Take a look.

a. len()
The len() function returns the length of the dictionary in Python. Every key-value pair adds 1 to the length.
1. >>> len(dict4)

An empty Python dictionary has a length of 0.

1. >>> len({})

b. any()
Like it is with lists an tuples, the any() function returns True if even one key in a dictionary has a Boolean value
of True.

1. >>> any({False:False,'':''})

False

1. >>> any({True:False,"":""})

True

1. >>> any({'1':'','':''})

True

c. all()
Unlike the any() function, all() returns True only if all the keys in the dictionary have a Boolean value of True.

1. >>> all({1:2,2:'',"":3})

False

d. sorted()
Like it is with lists and tuples, the sorted() function returns a sorted sequence of the keys in the dictionary. The
sorting is in ascending order, and doesn’t modify the original Python dictionary.

But to see its effect, let’s first modify dict4.

1. >>> dict4={3:3,1:1,4:4}

Now, let’s apply the sorted() function on it.

1. >>> sorted(dict4)

[1, 3, 4]
Now, let’s try printing the dictionary dict4 again.

1. >>> dict4

{3: 3, 1: 1, 4: 4}

As you can see, the original Python dictionary wasn’t modified.

This function returns the keys in a sorted list. To prove this, let’s see what the type() function returns.

1. >>> type(sorted(dict4))

<class ‘list’>

This proves that sorted() returns a list.

8. In-Built Methods on a Python Dictionary


A method is a set of instructions to execute on a construct, and it may modify the construct. To do this, a
method must be called on the construct. Now, let’s look at the available methods for dictionaries.

Let’s use dict4 for this example.

1. >>> dict4

{3: 3, 1: 1, 4: 4}

a. keys()
The keys() method returns a list of keys in a Python dictionary.

1. >>> dict4.keys()

dict_keys([3, 1, 4])

b. values()
Likewise, the values() method returns a list of values in the dictionary.

1. >>> dict4.values()

dict_values([3, 1, 4])

c. items()
This method returns a list of key-value pairs.
1. >>> dict4.items()

dict_items([(3, 3), (1, 1), (4, 4)])

d. get()
We first saw this function in section 4c. Now, let’s dig a bit deeper.

It takes one to two arguments. While the first is the key to search for, the second is the value to return if the key
isn’t found. The default value for this second argument is None.

1. >>> dict4.get(3,0)

1. >>> dict4.get(5,0)

Since the key 5 wasn’t in the dictionary, it returned 0, like we specified.

Any doubt yet in Python Dictionary? Leave a comment.

e. clear()
The clear function’s purpose is obvious. It empties the Python dictionary.

1. >>> dict4.clear()
2. >>> dict4

{}

Let’s reassign it though for further examples.

1. >>> dict4={3:3,1:1,4:4}

f. copy()
First, let’s see what shallow and deep copies are. A shallow copy only copies contents, and the new construct
points to the same memory location. But a deep copy copies contents, and the new construct points to a
different location. The copy() method creates a shallow copy of the Python dictionary.

1. >>> newdict=dict4.copy()
2. >>> newdict

{3: 3, 1: 1, 4: 4}
g. pop()
This method is used to remove and display an item from the dictionary. It takes one to two arguments. The first
is the key to be deleted, while the second is the value that’s returned if the key isn’t found.

1. >>> newdict.pop(4)

Now, let’s try deleting the pair 4:4.

1. >>> newdict.pop(5,-1)

-1

However, unlike the get() method, this has no default None value for the second parameter.

1. >>> newdict.pop(5)

Traceback (most recent call last):

File “<pyshell#191>”, line 1, in <module>

newdict.pop(5)

KeyError: 5

As you can see, it raised a KeyError when it couldn’t find the key.

h. popitem()
Let’s first reassign the Python dictionary newdict.

1. >>> newdict={3:3,1:1,4:4,7:7,9:9}

Now, we’ll try calling popitem() on this.

1. >>> newdict.popitem()

(9, 9)

It popped the pair 9:9.

Let’s restart the shell and reassign the dictionary again and see which pair is popped.

1. =============================== RESTART: Shell ===============================


2. >>> newdict={3:3,1:1,4:4,7:7,9:9}
3. >>> newdict.popitem()
(9, 9)

As you can see, the same pair was popped. We can interpret from this that the internal logic of the popitem()
method is such that for a particular dictionary, it always pops pairs in the same order.

i. fromkeys()
This method creates a new Python dictionary from an existing one. To take an example, we try to create a new
dictionary from newdict. While the first argument takes a set of keys from the dictionary, the second takes a
value to assign to all those keys. But the second argument is optional.

1. >>> newdict.fromkeys({1,2,3,4,7},0)

{1: 0, 2: 0, 3: 0, 4: 0, 7: 0}

However, the keys don’t have to be in a set.

1. >>> newdict.fromkeys((1,2,3,4,7),0)

{1: 0, 2: 0, 3: 0, 4: 0, 7: 0}

Like we’ve seen so far, the default value for the second argument is None.

1. >>> newdict.fromkeys({'1','2','3','4','7'})

{‘4’: None, ‘7’: None, ‘3’: None, ‘1’: None, ‘2’: None}

j. update()
The update() method takes another dictionary as an argument. Then it updates the dictionary to hold values
from the other dictionary that it doesn’t already.

1. >>> dict1={1:1,2:2}
2. >>> dict2={2:2,3:3}
3. >>> dict1.update(dict2)
4. >>> dict1

{1: 1, 2: 2, 3: 3}

9. Python Dictionary Operations


We learned about different classes of operators in Python earlier. Let’s now see which of those can we apply on
dictionaries.

a. Membership
We can apply the ‘in’ and ‘not in’ operators on a Python dictionary to check whether it contains a certain key.
For this example, we’ll work on dict1.

1. >>> dict1

{1: 1, 2: 2, 3: 3}

1. >>> 2 in dict1

True

1. >>> 4 in dict1

False

2 is a key in dict1, but 4 isn’t. Hence, it returns True and False for them, correspondingly.

10.Python Iterate Dictionary


When in a for loop, you can also iterate on a Python dictionary like on a list, tuple, or set.

1. >>> dict4

{1: 1, 3: 3, 4: 4}

1. >>> for i in dict4:


2. print(dict4[i]*2)

2
6
8

The above code, for every key in the Python dictionary, multiplies its value by 2, and then prints it.

11.Nested Dictionary
Finally, let’s look at nested dictionaries. You can also place a Python dictionary as a value within a dictionary.

1. >>> dict1={4:{1:2,2:4},8:16}
2. >>> dict1

{4: {1: 2, 2: 4}, 8: 16}

To get the value for the key 4, write the following code.

1. >>> dict1[4]

{1: 2, 2: 4}
However, you can’t place it as a key, because that throws an error.

1. >>> dict1={{1:2,2:4}:8,8:16}

Traceback (most recent call last):

File “<pyshell#227>”, line 1, in <module>

dict1={{1:2,2:4}:8,8:16}

TypeError: unhashable type: ‘dict’

So, this was all about Python Dictionary tutorial. Hope you like our explanation

12.Conclusion
In today’s lesson, we took a deep look at Python Dictionary. We first saw how to create, access, reassign, and
delete a dictionary or its elements. Then we looked at built-in functions and methods for dictionaries. After that,
we learned about the operations that we can perform on them. Lastly, we learned about nested dictionaries and
how to iterate on a Python dictionary. Still, you have a confusion? feel free to ask in the comment box.

Python Datetime Module with Quick Examples


by DataFlair Team · May 27, 2019

1. Python Datetime Module


With the ever-complex lifestyle we have adopted, date and time are extremely important. It is like we’re all
slaves to time. In this Python Datetime Module tutorial, we will see how to work with Python Datetime Module
and Python date Objects, Python time Objects, Python datetime Objects, and Python timedelta Objects. You
should refer to Python Date and Time.

So, let’s start the Python Datetime Module.


Introduction to Python Datetime Module

2. Python Datetime Module


The Python datetime module offers functions and classes for working with date and time parsing, formatting,
and arithmetic. Let’s start with Python Date time with Examples.

Two kinds of date and time objects exist- naïve and aware.

A naïve object is easier to deal with and doesn’t have enough information to unambiguously locate itself
relative to other date/time objects. An aware object is more realistic, it knows applicable algorithmic and
political time adjustments.

To use this module, we must first import it.

1. >>> import datetime

It has the following constants:

a. MAXYEAR
MAXYEAR tells us the maximum value a year can take, which is 9999.

1. >>> datetime.MAXYEAR

9999
b. MINYEAR
The minimum value of year is 1. This is what MINYEAR returns.

1. >>> datetime.MINYEAR

1
Both MAXYEAR and MINYEAR are of the type integer.

1. >>> type(datetime.MAXYEAR),type(datetime.MINYEAR)

(<class ‘int’>, <class ‘int’>)


Bonus- type() is of the type type.

1. >>> type(type)

<class ‘type’>
Other than these constants, datetime has these datetime class types:

1. class datetime.date

Python date is an idealized naïve date considering the current Gregorian calendar.
Attributes: year, month, and day.

1. class datetime.time

Python time is an idealized time, independent of any particular day. Here, we assume that each day is made of
exactly 24*60*60 seconds (no leap seconds).
Attributes: hour, minute, second, microsecond, and tzinfo.

1. class datetime.datetime

When you combine a date and a time, you get a datetime.

1. >>> issubclass(datetime.datetime,datetime.date)

True

1. >>> issubclass(datetime.datetime,datetime.time)

False
Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.

1. class datetime.timedelta

A timedelta is a duration expressing the difference between two date, time, or datetime instances to
microsecond resolution.

1. class datetime.tzinfo
tzinfo is an abstract base class we use for time zone information objects. The date and time classes use it to
provide a customizable notion of time adjustment (for example, to account for time zone and/or DST(daylight
saving time)).

1. class datetime.timezone

timezone implements the tzinfo abstract base class as a fixed offset from the UTC.
Such objects are immutable. Also, objects of the type ‘date’ is naïve, but those of the types ‘time’ or ‘datetime’
may be aware or naïve. The next in Python Datetime Module Tutorial is Date Objects

3. Python date Objects


A date object represents a date with year, month, and day, according to the ideal Gregorian calendar.
With a date object, we have the following methods:

a. date(year, month, day)


This method will create an object of type ‘date’.

1. >>> d=datetime.date(2018,2,28)

Here, year can be from MAXYEAR to MINYEAR, and month can be from 1 to 12. The day can be from 1 to
the number of days in the given month for the given year.

b. today()
today() will return the current local date.

1. >>> datetime.date.today()

datetime.date(2018, 2, 28)

c. fromtimestamp(timestamp)
fromtimestamp will return the date for the Python timestamp provided.

1. >>> import time


2. >>> time.time()

1519818814.358453

1. >>> datetime.date.fromtimestamp(time.time())

datetime.date(2018, 2, 28)

1. >>> datetime.date.fromtimestamp(time.time()+999999)
datetime.date(2018, 3, 12)
The date class also has these attributes:

a. date.min
It returns the earliest representable date.

1. >>> datetime.date.min

datetime.date(1, 1, 1)

b. date.max
Like min, max returns the latest representable date.

1. >>> datetime.date.max

datetime.date(9999, 12, 31)

c. date.resolution
resolution returns the smallest possible difference between non-equal date objects.

1. >>> datetime.date.resolution

datetime.timedelta(1)

We also have the following instance attributes:

a. year
This returns the year from a date object.

1. >>> d=datetime.date(2018,2,28)
2. >>> d.year

2018

b. month
month returns the month from a date object.

1. >>> d.month

2
c. day
This returns the day from a date object.

1. >>> d.day

28
And then, we have the following instance methods:

a. replace(year, month, day)


This will let us update any or all of these three values.

1. >>> d=d.replace(month=3,day=1)
2. >>> d

datetime.date(2018, 3, 1)

b. timetuple()
timetuple() returns a tuple of attributes for the current local time.

1. >>> d.timetuple()

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2,


tm_yday=59, tm_isdst=-1)

c. weekday()
weekday() will return the day of the week, where 0 denotes Monday.

1. >>> d.weekday()

d. isoweekday()
Here, Monday is 1.

1. >>> d.isoweekday()

3
e. isocalendar()
This method returns a tuple of three things- ISO year, ISO week number, and ISO weekday.

1. >>> d.isocalendar()

(2018, 9, 3)

f. isoformat()
This returns the date in the ISO format.

1. >>> d.isoformat()

‘2018-02-28’

g. str ()
This will return the current local date as a string.

1. >>> d. str ()

‘2018-02-28’

1. >>> str(d)

‘2018-02-28’

h. ctime()
ctime returns the current local date in a string format.

1. >>> d.ctime()

‘Wed Feb 28 00:00:00 2018’


We can also perform some operations on a date object:

1. >>> d=datetime.date(2018,12,30)
2. >>> td=datetime.timedelta(0,99999)
3. >>> d1=d+td
4. >>> d1

datetime.date(2018, 12, 31)

1. >>> d1<d

False
Any doubt yet in Date time module in Python or Examples? Please comment.

The Next in Python Datetime Module Tutorial is Time Objects

4. Python time Objects


A time object represents a local time of day. Its arguments are- hour, minute, second, microsecond, and tzinfo.

1. >>> t=datetime.time(11,59,59,99999)

The class time has the following attributes:

a. min
This returns the earliest representable time.

1. >>> t.min

datetime.time(0, 0)

b. max
This returns the latest representable time.

1. >>> t.max

datetime.time(23, 59, 59, 999999)

c. resolution
resolution returns the smallest possible difference between non-equal time objects.

1. >>> t.resolution

datetime.timedelta(0, 0, 1)
Now, let’s look at some non-readable instance attributes:

a. hour
This tells us the hour from the object.

1. >>> t.hour

11
b. minute
This returns the minute.

1. >>> t.minute

59

c. second
This returns the second.

1. >>> t.second

59

d. microsecond
This returns the microsecond.

1. >>> t.microsecond

99999

e. tzinfo
This returns whatever we pass as tzinfo to the object while creating it. If we didn’t, it returns None.

1. >>> print(t.tzinfo)

None

f. fold
This disambiguates wall times during a repeated interval.

1. >>> t.fold

We have the following instance methods:

i. replace()

This does the same as for ‘date’ objects.

1. >>> t=t.replace(hour=22,second=7)
ii.isoformat()

This method returns the time in the object in the ISO format.

1. >>> t.isoformat()

’22:59:07.099999′

iii. str ()

This does the same as it does for ‘date’ objects.

1. >>> t. str ()

’22:59:07.099999′
Now in the Python Datetime Module Tutorial Lets move ahead with datetime Objects

5. Python datetime Objects


A datetime objects knows about the date and the time. Arguments include year, month, day, hour, minute,
second, microsecond, tzinfo.

1. >>> d=datetime.datetime(1995,12,31,10,0)

We have the following methods:

a. today()
This method returns the current local datetime.

1. >>> datetime.datetime.today()

datetime.datetime(2018, 2, 28, 19, 9, 40, 751445)

b. now()
now() returns the current local date and time, but is more precise than today().

1. >>> datetime.datetime.now()

datetime.datetime(2018, 2, 28, 19, 12, 45, 710542)

c. utcnow()
utcnow() returns the current UTC time.

1. >>> datetime.datetime.utcnow()
datetime.datetime(2018, 2, 28, 13, 49, 14, 486367)

d. fromtimestamp()
This returns the date and time for the provided timestamp.

1. >>> datetime.datetime.fromtimestamp(time.time())

datetime.datetime(2018, 2, 28, 19, 20, 24, 55451)

e. utctimestamp()
This is like the previous one, but it returns the UTC time.

1. >>> datetime.datetime.utcfromtimestamp(time.time())

datetime.datetime(2018, 2, 28, 13, 51, 56, 615793)


Now, let’s look at the class attributes:

a. min
Like for everything else, this returns the earliest representable datetime.

1. >>> datetime.datetime.min

datetime.datetime(1, 1, 1, 0, 0)

b. max
max returns the latest representable datetime.

1. >>> datetime.datetime.max

datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)

c. resolution
Resolution is about the smallest possible difference between non-equal datetime objects.

1. >>> datetime.datetime.resolution

datetime.timedelta(0, 0, 1)
Now, let’s see some read-only instance attributes:
a. year
This returns the year.

1. >>> d.year

1995

b. month
This returns the month.

1. >>> d.month

12

c. day
This returns the day.

1. >>> d.day

31

d. hour
This returns the hour.

1. >>> d.hour

10

e. minute
This returns the minute.

1. >>> d.minute

f. second
This returns the second.

1. >>> d.second
g. microsecond
This returns the microsecond.

1. >>> d.microsecond

h. tzinfo
This returns the tzinfo; None, if we passed none while creating the object.

1. >>> print(d.tzinfo)

None

i. fold
This is the same as we discussed before.

1. >>> d.fold

Finally, let’s try some instance methods.

a. replace()
Again, this is like we have been doing so far.

1. >>> d.replace(microsecond=7)

datetime.datetime(1995, 12, 31, 10, 0, 0, 7)

b. date()
date() returns a date object from the datetime object.

1. >>> d.date()

datetime.date(1995, 12, 31)

c. time()
time() returns a time object from the datetime object.

1. >>> d.time()

datetime.time(10, 0)
d. timetz()
Other than what time() does, timetz() also returns the tzinfo value.

1. >>> d.timetz()

datetime.time(10, 0)

e. astimezone()
This returns a datetime object with new tzinfo attribute tz, adjusting the date and time data so the result is the
same UTC time as self, but in tz’s local time.

1. >>> d.astimezone()

datetime.datetime(1995, 12, 31, 10, 0, tzinfo=datetime.timezone(datetime.timedelta(0, 19800), ‘India Standard


Time’))

f. utcoffset()
If there is a UTC offset, it returns that, else, None.

1. >>> print(d.utcoffset())

None

g. dst()
If Daylight Savings Time applies, it returns its magnitude.

1. >>> print(d.dst())

None

h. tzname
If you would have set tzinfo, this would return its name.

1. >>> print(d.tzname())

None

i. timetuple()
Like we saw earlier, this returns a time tuple of the object.
1. >>> d.timetuple()

time.struct_time(tm_year=1995, tm_mon=12, tm_mday=31, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6,


tm_yday=365, tm_isdst=-1)

j. utctimetuple()
This returns a time tuple of the UTC time.

1. >>> d.utctimetuple()

time.struct_time(tm_year=1995, tm_mon=12, tm_mday=31, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6,


tm_yday=365, tm_isdst=0)

k. timestamp()
This returns the timestamp of the object (in seconds).

1. >>> d.timestamp()

820384200.0

l. weekday()
This returns the number of weekday for the object, where 0 is for Monday.

1. >>> d.weekday()

6
#A Sunday

m. isoweekday()
This returns the day of the week, but with Monday as 1.

1. >>> d.isoweekday()

n. isocalendar()
This returns a tuple of the following- ISO year, ISO week number, ISO weekday.

1. >>> d.isocalendar()

(1995, 52, 7)
o. isoformat()
This returns the date and time in the ISO format.

1. >>> d.isoformat()

‘1995-12-31T10:00:00’

p. str ()
This returns the date and time in a string representation.

1. >>> d. str ()

‘1995-12-31 10:00:00’

1. >>> str(d)

‘1995-12-31 10:00:00’

q. ctime()
This, like str, returns the datetime as a string.

1. >>> d.ctime()

‘Sun Dec 31 10:00:00 1995’


Some operations that we can perform on a datetime object are:

1. >>> d=datetime.datetime(2018,12,30)
2. >>> td=datetime.timedelta(0,99999)
3. >>> d1=d+td
4. >>> d1

datetime.datetime(2018, 12, 31, 3, 46, 39)

1. >>> d<d1

True
6. Python timedelta Objects
A timedelta object represents the duration between two dates or times.
Attributes: days, seconds, microseconds, milliseconds, minutes, hours, weeks
Here, seconds can be from 0 to 86399.

1. >>> td=datetime.timedelta(9,3,9999,999,58,22,3)

These attributes belong to the timedelta class:

a. min
This returns the most negative timedelta object.

1. >>> datetime.timedelta.min

datetime.timedelta(-999999999)

b. max
This one returns the most positive timedelta object.

1. >>> datetime.timedelta.max

datetime.timedelta(999999999, 86399, 999999)

c. resolution
resolution returns the smallest possible difference between non-equal timedelta objects.

1. >>> datetime.timedelta.resolution

datetime.timedelta(0, 0, 1)
timedelta also supports one instance method:

a. total_seconds()
This returns the total number of seconds in the duration.

1. >>> td.total_seconds()

2674684.008999
You can perform arithmetic operations on timedelta objects.

1. >>> td=datetime.timedelta(0,333)
2. >>> td1=td*2
3. >>> td1

datetime.timedelta(0, 666)

So, this was all about the Python Datetime Module Tutorial. Hope you like our explanation.

7. Conclusion: Python Datetime Module Tutorial


We hope that after this article on Python datetime Module Tutorial, and after Date and Time, you will be able to
handle all date and time easily. If you think this is a bit too much for once, don’t panic. Work on it twice or
thrice. Practice will do it for you. And comment if you need any help on Python Datetime examples.

Advance Python Modules – How to Create &


Import with dir Function
by DataFlair Team · September 27, 2018

1. Python Modules Tutorial


In this Python Modules tutorial, we will discuss what is a module in Python programming language?. Moreover,
we will talk about how to create python modules and import modules in python. Along with this, we will learn
how can we execute python module as a script, standard Python modules, and Python dir functions.

So, let’s start Python Modules Tutorial.


Python Modules

2. Define Python Modules?


Python module is but a piece of code.

Exiting the interpreter destroys all functions and variables we created. But when we want a longer program, we
create a script. With Python, we can put such definitions in a file, and use them in a script, or in an interactive
instance of the interpreter. Such a file is a module. If you face any difficulty in article on Python modules, ask
us in comments.

In essence, a module is a file that contains Python statements and definitions. A Python modules looks like this:

calc.py

3. How to Create Python Modules?


Let’s create a Python modules ‘calc’.

Microsoft Windows [Version 10.0.16299.309]

(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\lifei>cd Desktop

C:\Users\lifei\Desktop>mkdir calc

C:\Users\lifei\Desktop>cd calc

C:\Users\lifei\Desktop\calc>echo > init .py

C:\Users\lifei\Desktop\calc>echo >calc.py

C:\Users\lifei\Desktop\calc>

And this is what we put inside the module calc.py:

1. def add(a,b):
2. return a+b
3. def sub(a,b):
4. return a-b
5. def mul(a,b):
6. return a*b
7. def div(a,b):
8. return a/b
9. def exp(a,b):
10. return a**b
11. def floordiv(a,b):
12. return a//b

Also, calc is a package we create, and we place init .py inside it (Refer to Python Packages).

4. How can we Import Modules in Python?


Now, to import Python modules, we first get to the Desktop in Python.

1. >>> import os
2. >>> os.chdir('C:\\Users\\lifei\\Desktop\\calc')
3. >>> import calc
4. >>>

To find the name of this module, we can use the __name attribute.

1. >>> calc. name

‘calc’

We can now use functions from this module:

We can also assign one of these functions a name:

1. >>> fd=calc.floordiv
2. >>> fd(5.5,4)

1.0

1. >>> fd(9,4)

1. >>> type(fd(9,4))

<class ‘int’>

1. >>> type(fd(5.5,4))

<class ‘float’>

Read: Python Module vs Package

5. More on Python Modules and Importing


Python modules may contain anything from executable statements to function definitions. Such statements
initialize the module. Hence, they execute only once, when we import the module. However, they also run if we
execute the file as a script.
Each module uses its own private symbol table globally for all of its functions. So, its author can use global
variables in the module without worrying that they will accidentally clash with a user’s global variables.

A module can always import other modules in Python. In fact, we can place import statements at the beginning
of a module/script, but we don’t ‘need’ to. This places the imported module’s name in the importing module’s
symbol table.

We can also selectively import functions from a Python modules:

1. >>> from calc import div as d,floordiv as fd


2. >>> d(9,4)

2.25

1. >>> fd(9,4)

2
We can also import all from a module:

1. >>> from calc import *


2. >>> floordiv(9,4)

This will import all names other than those beginning with an underscore(_). However, we disparage this use, as
it makes for poorly readable code.

We can also import a module under an alias.

1. >>> import calc as cal


2. >>> cal.sub

<function sub at 0x0655CC90>

6. How to Execute Python Modules as Scripts?


Look at the following code:

1. def add(a,b):
2. print(a+b)
3. def sub(a,b):
4. print(a-b)
5. def mul(a,b):
6. print(a*b)
7. def div(a,b):
8. print(a/b)
9. def exp(a,b):
10. print(a**b)
11. def floordiv(a,b):
12. print(a//b)
13. if name == " main ":
14. import sys
15. if int(sys.argv[1])==1:
16. add(int(sys.argv[2]),int(sys.argv[3]))
17. elif int(sys.argv[1])==2:
18. sub(int(sys.argv[2]),int(sys.argv[3]))

These last few lines let us use the sys module to deal with command line arguments. To execute subtraction,
this is what we type in the command prompt:

C:\Users\lifei\Desktop\calc>python calc.py 2 3 4

-1

This way, you can complete the code for other operations as well. Hence, we’ve created a script. But we can
also import this normally like a module:

1. >>> import calc


2. >>>

We may want to run a module as a script for testing purposes.

Any Doubt yet in Python Modules? Please Comment.

7. Python Module Search Path


Whenever we import Python modules, say eggs, the interpreter searches a built-in version. If not found, it
searches for a file named eggs.py under a list of directories given by variable sys.path. This variable is
initialized from the following locations:

 The directory holding the input script (or the current directory, in case no file is specified).
 PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
 The installation-dependent default.

Once initialized, a Python program may modify sys.path.

8. Compiled Python Files


In an attempt to speed up loading a module, Python will cache each module’s compiled version in the
pycache directory. It does so under the name module.version.pyc. Here, the version encodes the compiled
file’s format. For instance, under CPython 3.3, we’ll have eggs.py as pycache /eggs.cpython-33.pyc. This
allows compiled modules from different Python versions and releases to coexist. These compiled modules are
platform-independent.
If the compiled version is out of date, Python recompiles it automatically.
9. Python Standard Modules
And like we’ve always said, Python ships with a library of standard Python modules. While some of them are
built into the interpreter, we can create our own. The standard ones lend us extra functionality, in turn reducing
the need to code too much. Other times, they provide efficiency to a programmer, in cases like providing access
to operating system primitives, the likes of system calls.

The module sys is built into every Python interpreter. However, some modules are only available to certain
operating platforms. For instance, the winreg module is only available to Windows programmers.

The sys module will also tell you which version of Python you are using.

1. >>> import sys


2. >>> sys.version

‘3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]’

10. Python dir() Function


The dir() is a built-in function that returns a sorted list of all the names that a Python modules defines.

1. >>> dir(sys)

[‘ displayhook ’, ‘ doc ’, ‘ excepthook ’, ‘ interactivehook ’, ‘ loader ’, ‘ name ’,


‘ package ’, ‘ spec ’, ‘ stderr ’, ‘ stdin ’, ‘ stdout ’, ‘_clear_type_cache’, ‘_current_frames’,
‘_debugmallocstats’, ‘_enablelegacywindowsfsencoding’, ‘_getframe’, ‘_home’, ‘_mercurial’, ‘_xoptions’,
‘api_version’, ‘argv’, ‘base_exec_prefix’, ‘base_prefix’, ‘builtin_module_names’, ‘byteorder’, ‘call_tracing’,
‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhandle’, ‘dont_write_bytecode’, ‘exc_info’, ‘excepthook’,
‘exec_prefix’, ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, ‘get_asyncgen_hooks’,
‘get_coroutine_wrapper’, ‘getallocatedblocks’, ‘getcheckinterval’, ‘getdefaultencoding’,
‘getfilesystemencodeerrors’, ‘getfilesystemencoding’, ‘getprofile’, ‘getrecursionlimit’, ‘getrefcount’,
‘getsizeof’, ‘getswitchinterval’, ‘gettrace’, ‘getwindowsversion’, ‘hash_info’, ‘hexversion’, ‘implementation’,
‘int_info’, ‘intern’, ‘is_finalizing’, ‘last_traceback’, ‘last_type’, ‘last_value’, ‘maxsize’, ‘maxunicode’,
‘meta_path’, ‘modules’, ‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’,
‘set_asyncgen_hooks’, ‘set_coroutine_wrapper’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’,
‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘version’, ‘version_info’, ‘warnoptions’,
‘winver’]

1. >>> for i in dir(calc): print(i)

builtins
cached
doc
file
loader
name
package
spec
add
div
exp
floordiv
mul
sub
And without any arguments, dir() returns a lilst of the names that we have defined currently.

1. >>> dir()

[‘ annotations ’, ‘ builtins ’, ‘ doc ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’, ‘add’, ‘cal’,


‘calc’, ‘div’, ‘exp’, ‘floordiv’, ‘i’, ‘mul’, ‘os’, ‘sub’, ‘sys’]

To get a list of built-in functions and variables, we do the following, instead:

1. >>> import builtins


2. >>> dir(builtins)

[‘ArithmeticError’, ‘AssertionError’, ‘AttributeError’, ‘BaseException’, ‘BlockingIOError’, ‘BrokenPipeError’,


‘BufferError’, ‘BytesWarning’, ‘ChildProcessError’, ‘ConnectionAbortedError’, ‘ConnectionError’,
‘ConnectionRefusedError’, ‘ConnectionResetError’, ‘DeprecationWarning’, ‘EOFError’, ‘Ellipsis’,
‘EnvironmentError’, ‘Exception’, ‘False’, ‘FileExistsError’, ‘FileNotFoundError’, ‘FloatingPointError’,
‘FutureWarning’, ‘GeneratorExit’, ‘IOError’, ‘ImportError’, ‘ImportWarning’, ‘IndentationError’, ‘IndexError’,
‘InterruptedError’, ‘IsADirectoryError’, ‘KeyError’, ‘KeyboardInterrupt’, ‘LookupError’, ‘MemoryError’,
‘ModuleNotFoundError’, ‘NameError’, ‘None’, ‘NotADirectoryError’, ‘NotImplemented’,
‘NotImplementedError’, ‘OSError’, ‘OverflowError’, ‘PendingDeprecationWarning’, ‘PermissionError’,
‘ProcessLookupError’, ‘RecursionError’, ‘ReferenceError’, ‘ResourceWarning’, ‘RuntimeError’,
‘RuntimeWarning’, ‘StopAsyncIteration’, ‘StopIteration’, ‘SyntaxError’, ‘SyntaxWarning’, ‘SystemError’,
‘SystemExit’, ‘TabError’, ‘TimeoutError’, ‘True’, ‘TypeError’, ‘UnboundLocalError’, ‘UnicodeDecodeError’,
‘UnicodeEncodeError’, ‘UnicodeError’, ‘UnicodeTranslateError’, ‘UnicodeWarning’, ‘UserWarning’,
‘ValueError’, ‘Warning’, ‘WindowsError’, ‘ZeroDivisionError’, ‘_’, ‘ build_class ’, ‘ debug ’, ‘ doc ’,
‘ import ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’, ‘abs’, ‘all’, ‘any’, ‘ascii’, ‘bin’, ‘bool’,
‘bytearray’, ‘bytes’, ‘callable’, ‘chr’, ‘classmethod’, ‘compile’, ‘complex’, ‘copyright’, ‘credits’, ‘delattr’, ‘dict’,
‘dir’, ‘divmod’, ‘enumerate’, ‘eval’, ‘exec’, ‘exit’, ‘filter’, ‘float’, ‘format’, ‘frozenset’, ‘getattr’, ‘globals’,
‘hasattr’, ‘hash’, ‘help’, ‘hex’, ‘id’, ‘input’, ‘int’, ‘isinstance’, ‘issubclass’, ‘iter’, ‘len’, ‘license’, ‘list’, ‘locals’,
‘map’, ‘max’, ‘memoryview’, ‘min’, ‘next’, ‘object’, ‘oct’, ‘open’, ‘ord’, ‘pow’, ‘print’, ‘property’, ‘quit’, ‘range’,
‘repr’, ‘reversed’, ’round’, ‘set’, ‘setattr’, ‘slice’, ‘sorted’, ‘staticmethod’, ‘str’, ‘sum’, ‘super’, ‘tuple’, ‘type’,
‘vars’, ‘zip’]

This was all on Python modules. Hope the Python Modules article was informative.
11. Conclusion
While this is all about Python modules, we suggest you should also read about Packages in Python. Then,
maybe you should switch to Packages vs Modules. For any question regarding, Please Comment. Until then,
have a good day.

Python Pickle | What is Serialization in Python with


Example
by DataFlair Team · September 27, 2018

1. Python Pickle Tutorial


In the Last tutorial, we talked about Python Virtual Environment. In this Python Pickle tutorial, we will study
what is a Pickle in Python and how Python Serialization deals with the ‘pickle’ module of Python for the
purpose of serialization. At last, we will discuss some Python Pickle Examples.

So, let’s start the Python Pickle Tutorial.

Python Pickle and Python Serialization

2. What is Serialization in Python?


In Python, when we want to serialize and de-serialize a Python object, we use functions and methods from the
module Python Pickle. Pickling, then, is the act of converting a Python object into a byte stream. We also call
this ‘serialization’, ‘marshalling’, or ‘flattening’. Unpickling is its inverse, ie., converting a byte stream from a
binary file or bytes-like object into an object. Lets start with comparing Python serialize with other modules of
Python.
3. Comparing Python Pickle to Other Python Modules
a. Comparing Python pickle to marshal
‘marshal’ is a more primitive module for serialization in Python, and its purpose is to support .pyc files.
However, we prefer Python pickle. The two differ in the following ways:

1. Python pickle tracks the objects it has serialized. Because of this, it doesn’t have to serialize the same
objects again when it references them again. This is unlike marshal.
2. marshal cannot serialize user-defined classes and their instances. If the class definition is importable
and in the same module as when we stored the object, pickle can save and restore class instances.
3. The serialization format for pickle in Python is backwards-compatible. This isn’t the same with marshal.

b. Comparing Python pickle to json


json is a standard library module for serialization and deserialization with Python.

1. Where Python pickle has a binary serialization format, json has a text serialization format.
2. Python pickle isn’t human-readable, but marshal isn’t.
3. pickle is Python-specific, but JSON is interoperable.
4. pickle can represent a very large number of Python types. However, json can only represent a subset of
Python’s in-built types.

4. Python Pickle Supports Data Stream Format


Python pickle uses a Python-specific data format. So, external standards like JSON or XDR impose no
restrictions. But this makes for inability of non-Python programs to reconstruct pickled Python objects.

Like we said above, Python pickle uses a data format with a relatively compact binary representation. We can
efficiently compress it.

Complementary to Python pickle is the module ‘pickletools’ for analyzing data streams that it generates.

We have five different protocols for pickling:

1. Protocol version 0: Original, human-readable protocol; backwards-compatible with earlier versions of


Python.
2. Protocol version 1: Old, binary format; compatible with earlier versions of Python.
3. Protocol version 2: Added in Python 2.3; provides more efficient pickling of new-style classes.
4. Protocol version 3: Introduced in Python 3.0; default; supports bytes objects; cannot be unpickled by
Python 2.x. It is recommended when we need compatibility with other Python 3 versions.
5. Protocol version 4: Introduced in Python 3.4; supports very large objects, more kinds of objects, and
certain data format optimizations.
5. Python Pickle Module Interface
To serialize and deserialize, we use functions dumps() and loads(), respectively. Alternatively, we can create
our own Pickler and Unpickler objects for more control over this.
Python pickle has two constants:

a. HIGHEST_PROTOCOL
This is an integer, and it holds the highest protocol version that is available. We can pass this as a protocol
value to dump() and dumps(), and to the Pickler constructor.

b. DEFAULT_PROTOCOL
Also an integer, this holds the default protocol version for pickling. The default is currently Protocol 3.

It also has the following functions:

i. dump(obj, file, protocol=None, *, fix_imports=True)


This writes a pickled representation of object obj to file, an open file object. Consider this equivalent to
Pickler(file, protocol).dump(obj)

1. >>> x=7
2. >>> import os
3. >>> os.chdir('C:\\Users\\lifei\\Desktop')
4. >>> import pickle
5. >>> f=open('abcde.txt','r+b') //opened it in binary mode to pickle
6. >>> pickle.dump(x,f)

When we checked in the file abcde.txt, we found this:

€K. €K.€K.

file, here, must have a write() method accepting a single bytes argument. So, it can be a file you opened in
binary mode, an io.BytesIO instance, or a custom object meeting this interface. Protocol lets us choose which
protocol to use.

When fix_imports is true and we use a protocol less than 3, pickle maps new Python 3 names to old module
names in Python 2. This lets Python 2 read the pickle data stream.

ii. dumps(obj, protocol=None, *, fix_imports=True)


This returns the pickled representation of obj as a bytes object. This does not write it to a file.

1. >>> pickle.dumps(x)
b’\x80\x03K\x07.’

iii. load(file, *, fix_imports=True, encoding=”ASCII”, errors=”strict”)


load() takes in file, an open file object, reads a pickled representation from it, and returns the reconstructed
object hierarchy. Consider this equivalent to Unpickler(file).load()
file can be a file object opened in the binary reading mode, an io.BytesIO object, or an object that meets its
interface. This is because it must have two methods- read(), that takes one integer argument, and readline(), that
takes no arguments. Both of these methods must return bytes.

fix_imports , encoding, and errors help control compatibility support for pickle streams by Python 2. When
fix_imports is true, pickle maps old Python 2 names to new Python 3 names. The other two guide pickle with
decoding 8-but string instances pickled by Python 2. The default encoding is ‘ASCII’, and the default value for
errors is ‘strict’. To read such 8-bit string instances as bytes objects, we can set the encoding to ‘bytes’.

Let’s try doing this.

1. >>> pickle.load(f)

Traceback (most recent call last):

File “<pyshell#63>”, line 1, in <module>

pickle.load(f)

EOFError: Ran out of input

Uh-oh. Let’s get to the beginning of the file.

1. >>> f.seek(0)

Now, we can successfully load it.

1. >>> pickle.load(f)

iv. loads(bytes_object, *, fix_imports=True, encoding=”ASCII”,


errors=”strict”)
This function takes in a bytes object, reads a pickled object hierarchy, and returns the reconstructed object
hierarchy.

fix_imports, encoding, and errors help control compatibility support for pickle streams that Python 2 generates.
When it is true, pickle maps old Python 2 names to new Python 3 names. encoding guides pickle with decoding
8-bit string instances pickled by Python 2. The default for encoding is ‘ASCII’, and that for errors is ‘strict’. To
read such 8-byte instances as bytes objects, we can set the encoding to ‘bytes’.

1. >>> f.seek(0)

1. >>> pickle.loads(f.read())

6. Python Pickle Exceptions


The Python pickle module also defines three kinds of exceptions:

a. PickleError

This is the common parent class for all other pickling exceptions. It, in turn, inherits from Exception.

b. PicklingError

When the Pickler encounters an unpicklable object, it raises a PicklingError. This class inherits from
PickleError.

c. UnpicklingError

When Python pickle cannot unpickle an object due to data corruption or a security violation, it raises an
UnpicklingError. This inherits from PickleError.

Some other exceptions we observe when pickling, as we did above, include:


EOFError
TypeError
ValueError
AttributeError
ImportError
IndexError

Learn: Python Recursion with Examples

7. Imported Classes in Python Pickle


Python pickle imports two classes- Pickler and Unpickler:

a. Pickler(file, protocol=None, *, fix_imports=True)


Pickler takes in a binary file and writes a pickle data stream.
file must have a write() method accepting a single bytes argument. This can be a file object for a file opened for
writing in binary mode, an io.BytesIO instance, or a custom object meeting this interface.
protocol is an integer, and informs the pickler about which protocol to use (0 to HIGHEST_PROTOCOL).
Otherwise, it uses DEFAULT_PROTOCOL. On providing a negative number, it selects
HOGHEST_PROTOCOL.

When fix_imports is true and the protocol version is less than 3, pickle maps new Python 3 names to old Python
2 names. This makes the Python data stream readable by Python 2.

Python Pickler has the following members:

i. dump(obj)
This takes in obj and writes a pickled representation of it to the open file object specified in the constructor of
Pickler.

ii. persistent_id(obj)
By default, it does nothing. It only exists to let subclasses override it. If it returns none, pickle pickles obj as
usual. Otherwise, Pickler emits the returned value as a persistent ID for obj. Unpickler.persistent_load() defines
this context.

iii. dispatch_table
For an object of Pickler, a dispatch table is a registry holding reduction functions that we can declare with
copyreg.pickle(). This mapping has classes as its keys, and reduction functions as its values.

A reduction function takes one argument of the class, and conforms to this interface as a reduce () method.

But pickler objects don’t have dispatch_tables by default. Instead, it makes use of the global dispatch table that
the copyreg module manages. To customize pickling for an object of a specific object of Pickler, we can set
dispatch_table to a dict-like object. Or, if one of the subclasses of Pickler has dispatch_table, then this serves as
the default dispatch table for instances of that class.

iv. fast
Although this is deprecated(no longer advised), it enables fast mode when set to true. This mode disables
memo, thereby speeding pickling as it doesn’t generate extra PUT opcodes. However, do not use it with self-
referential objects, as it can set Pickler off into infinite recursion.

For more compact pickles, we can use pickletools.optimize().

b. Unpickler(file, *, fix_imports=True, encoding=”ASCII”,


errors=”strict”)
The Unpickler takes in a binary file and reads a pickle data stream.
file must have the methods read()- that takes an integer argument, and readline()- that needs no arguments. Both
of these methods must return bytes. This can be a file object opened for reading in binary mode, an io.BytesIO
object, or a custom object meeting this interface.

pickle automatically detects the version of protocol used; we don’t need an argument for that.

fix_imports, encoding, and errors help control compatibility support for pickle streams generated by Python 2.
When fix_imports is true, pickle maps old Python 2 names to new Python 3 names. encoding and errors guide
pickle with decoding 8-bit string instances pickled by Python 2. The default for encoding is ‘ASCII’, and that
for errors is ‘strict’. When we want to read such 8-bit string instances as bytes objects, we can set the
encoding to ‘bytes’.

Unpickler has the following members:

1. load()

This takes in an open file object, reads a pickled object representation, and returns the reconstructed object
hierarchy.

2. persistent_load(pid)

By default, this raises an UnpicklingError. When we define it, however, it must return the object pertaining to
the persistent ID pid. If we pass an invalid persistent ID, it raises an UnpicklingError.

3. find_class(module,name)

If necessary, it imports module, and returns the object name from it. Here, module and name are str objects. We
can also use find_class() to find functions.

A subclass can override this to control what kind of objects it can take, and how we can load them. This
alleviates security risks.

Any doubt yet in Python Pickle? Please Comment.

7. What Can We Pickle and Unpickle?


We can pickle the following types:

 None, True, and False


 integers, floating point numbers, complex numbers
 strings, bytes, bytearrays
 tuples, lists, sets, and dictionaries holding only picklable objects
 functions defined at a module’s top level (using def, not lambda)
 built-in functions defined at a module’s top level
 classes defined at a module’s top level
 instances of such classes whose dict or the result of calling getstate () is picklable

When we try to pickle an unpicklable object, pickle raises the PicklingError exception. In this process, it is
possible that an unspecified number of bytes have already been written to the file.
In trying to pickle a highly-recursive data structure, we may exceed the maximum recursion depth. Such a case
raises a RecursionError. However, we can raise that limit with sys.setrecursionlimit().
Let’s take a quick look.

We pickle functions by their ‘fully-qualified’ name references, not by their values. This way, we only pickle the
function name and the module it resides in. We do not pickle the function’s code or attributes. So, we should be
able to import the defining module in the unpickling environment. This module must hold the named object.
Otherwise, it raises an exception.

We pickle classes by named reference. This way, the same restrictions apply in the unpickling environment. We
do not pickle the class’ code or data. We only pickle instance data.
Such restrictions mandate that we define picklable classes and functions in a module’s top level.

This was all on Python Pickle and Python Serialization. Hope you now understand Python Serialization.

8. Conclusion: Python Pickle and Serialization


Simply speaking, Python serialization is the act of converting a Python object into a byte stream. In Python, we
use the module ‘pickle’, which has a binary serializable format. We can also serialize classes and functions. We
have also studied in detail about Python Pickle and its comparison with other modules. If you have any queries
in Python pickle or Python Serialization, Please Comment.

Python Packages Tutorial – How to Create Your Own


Package
by DataFlair Team · September 26, 2018

1. Python Packages Tutorial


In today’s article, we will discuss Python Packages. Moreover, we will learn the structure of Python Packages
and how to import modules from packages in Python.

So, let’s start the Python Packages Tutorial.


Python Packages Tutorial – How to Create Your Own Package in Python

2. What are Python Packages?


In our computer systems, we store our files in organized hierarchies. We don’t store them all in one location.
Likewise, when our programs grow, we divide it into packages. In real-life projects, programs are much larger
than what we deal with in our journey of learning Python. A package lets us hold similar modules in one place.

Like a directory may contain subdirectories and files, a package may contain sub-packages and modules. We
have been using modules a lot in the previous lessons. Remember math, os, and collections? Those were all
modules that ship with Python officially. We will discuss the difference between a module and a package in our
next lesson. But for now, let’s dive into the world of Python packages.

3. Structure of Python Packages


As we discussed, a package may hold other Python packages and modules. But what distinguishes a package
from a regular directory? Well, a Python package must have an init .py file in the directory. You may leave
it empty, or you may store initialization code in it. But if your directory does not have an init .py file, it
isn’t a package; it is just a directory with a bunch of Python scripts. Leaving init .py empty is indeed good
practice.
Take a look at the following structure for a game:

Python Packages Module Structure


Here, the root package is Game. It has sub packages Sound, Image, and Level, and file init .py. Sound
further has modules load, play, and pause, apart from file init .py. Image has modules open, change, and
close, apart from init .py. Finally, Level has modules start, load, and over, apart from init .py.

4. How to Import Modules from Packages


in Python?
A Python package may contain several modules. To import one of these into your program, you must use the
dot operator(.)

In the above example, if you want to import the load module from subpackage sound, we type the following at
the top of our Python file:

import Game.Sound.load

Note that we don’t type the extension, because that isn’t what we refer to the module as. The subpackage Level
has a module named load too, but there is no clash here. This is because we refer to the module by its fully
qualified name.

To escape having to type so much every time we needed to use the module, we could also import it under an
alias:

import Game.Sound.load as loadgame

(If you’re working the interpreter, you may also do the following:
loadgame=Game.Sound.load

This works equally fine.)

Alternatively, you could do:

from Game.Sound import load

Now, if the Sound subpackage has a function volume_up(), we call it this way:
loadgame.volume_up(7)

If we imported this way:

from Game.Sound.load import volume_up() as volup

We could call the function simply, without needing to use a full qualifier:
volup(7)

But this isn’t recommended, as this may cause names in a namespace to clash.
5. Further Notes
When you import a package, only the modules directly under it are imported. An import does not import the sub
packages.

1. >>> import one


2. >>> one.two

Traceback (most recent call last):

File “<pyshell#488>”, line 1, in <module>

one.two.evenodd

AttributeError: module ‘one’ has no attribute ‘two’

Also note that if you want to check where your Python packages are being created, your path will look
something like this:

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\Lib\site-packages

6. How to Create Your Own Python Package?


Now, on to the most interesting part. Like we said, Python packages are nothing but a dictionary with sub-
packages and modules, and an init .py file.
In our example, this is the hierarchy we create:

How to Create Your Own Python Package

This is what we have in evenodd.py:

1. def check():
2. a=int(input('Enter a number'))
3. if a%2==0: print("Even")
4. else: print("Odd")

Also, we keep each init .py empty.

Now, we import and use it this way:

1. >>> from one.two.evenodd import check as check


2. >>> check()

Enter a number7
Odd

1. >>> check()

Enter a number0
Even

So, this was all about Python Packages. Hope you like our explanation.

7. Conclusion
In this Python Packages tutorial, we discussed packages, and how to create them. Apart from that, the Python
Package Index(PyPI) provides us with a lot of Python packages to help us with our projects. We talked about
this in our Introduction to Python. Still, have a confusion? Feel free to ask in the comment box.

List of 63 Python os Module with Syntax & Examples


(Latest)
by DataFlair Team · May 27, 2019

1. Python os Module
In this tutorial on Python os Module, we will get closer to the os module and its methods. Moreover, we will
study syntax and examples of os Module in Python Programming Language.

So, let’s start Python Module Tutorial


List of 63 Python os Module with Syntax & Examples (Latest)

2. Python os Module
The Python OS module lets us work with files and directories. We have been using it a lot to get to the Desktop
in our examples. But it is much more. Let’s discuss the important functions/methods it offers. In case of any
doubt, please as us in comments. Let’s check the dir() on this module?

1. >>> dir(os)

[‘DirEntry’, ‘F_OK’, ‘MutableMapping’, ‘O_APPEND’, ‘O_BINARY’, ‘O_CREAT’, ‘O_EXCL’, ‘O_NOINHERIT’,


‘O_RANDOM’, ‘O_RDONLY’, ‘O_RDWR’, ‘O_SEQUENTIAL’, ‘O_SHORT_LIVED’, ‘O_TEMPORARY’, ‘O_TEXT’,
‘O_TRUNC’, ‘O_WRONLY’, ‘P_DETACH’, ‘P_NOWAIT’, ‘P_NOWAITO’, ‘P_OVERLAY’, ‘P_WAIT’, ‘PathLike’,
‘R_OK’, ‘SEEK_CUR’, ‘SEEK_END’, ‘SEEK_SET’, ‘TMP_MAX’, ‘W_OK’, ‘X_OK’, ‘_Environ’, ‘ all ’, ‘ builtins ’,
‘ cached ’, ‘ doc ’, ‘ file ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’, ‘_execvpe’, ‘_exists’,
‘_exit’, ‘_fspath’, ‘_get_exports_list’, ‘_putenv’, ‘_unsetenv’, ‘_wrap_close’, ‘abc’, ‘abort’, ‘access’, ‘altsep’,
‘chdir’, ‘chmod’, ‘close’, ‘closerange’, ‘cpu_count’, ‘curdir’, ‘defpath’, ‘device_encoding’, ‘devnull’, ‘dup’,
‘dup2’, ‘environ’, ‘errno’, ‘error’, ‘execl’, ‘execle’, ‘execlp’, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’,
‘extsep’, ‘fdopen’, ‘fsdecode’, ‘fsencode’, ‘fspath’, ‘fstat’, ‘fsync’, ‘ftruncate’, ‘get_exec_path’,
‘get_handle_inheritable’, ‘get_inheritable’, ‘get_terminal_size’, ‘getcwd’, ‘getcwdb’, ‘getenv’, ‘getlogin’,
‘getpid’, ‘getppid’, ‘isatty’, ‘kill’, ‘linesep’, ‘link’, ‘listdir’, ‘lseek’, ‘lstat’, ‘makedirs’, ‘mkdir’, ‘name’, ‘open’,
‘pardir’, ‘path’, ‘pathsep’, ‘pipe’, ‘popen’, ‘putenv’, ‘read’, ‘readlink’, ‘remove’, ‘removedirs’, ‘rename’,
‘renames’, ‘replace’, ‘rmdir’, ‘scandir’, ‘sep’, ‘set_handle_inheritable’, ‘set_inheritable’, ‘spawnl’, ‘spawnle’,
‘spawnv’, ‘spawnve’, ‘st’, ‘startfile’, ‘stat’, ‘stat_float_times’, ‘stat_result’, ‘statvfs_result’, ‘strerror’,
‘supports_bytes_environ’, ‘supports_dir_fd’, ‘supports_effective_ids’, ‘supports_fd’,
‘supports_follow_symlinks’, ‘symlink’, ‘sys’, ‘system’, ‘terminal_size’, ‘times’, ‘times_result’, ‘truncate’,
‘umask’, ‘uname_result’, ‘unlink’, ‘urandom’, ‘utime’, ‘waitpid’, ‘walk’, ‘write’]

3. access(path,mode)

This method uses the real uid/gid to test for access to a path. If access is allowed, it returns True. Else, it returns
False. The first argument is the path; the second is the mode. The mode can take one of four values:
1. os.F_OK — Found
2. os.R_OK — Readable
3. os.W_OK — Writable
4. os.X_OK — Executable

Now, let’s take an example.

1. >>> os.chdir('C:\\Users\\lifei\\Desktop')
2. >>> os.access('Today.txt',os.R_OK)

True

1. >>> os.access('Today.txt',os.F_OK)

True

1. >>> os.access('Today.txt',os.W_OK)

True

1. >>> os.access('Today.txt',os.X_OK)

True

4. chdir(path)

This Python os module changes the current working directory to the path we specify.
Does this need another example?

1. >>> os.chdir('C:\\Users\\lifei\\Desktop')

It returns None.

5. chflags(path,flags)

chflags() sets path flags to the numeric flags. These flags may take a combination(bitwise OR) of the following
values:

 os.UF_NODUMP – Don’t dump the file


 os.UF_IMMUTABLE − You may not change the file
 os.UF_APPEND − You may only append to the file
 os.UF_NOUNLINK – You may not rename or delete the file
 os.UF_OPAQUE − The directory is opaque when we view it through a union stack
 os.SF_ARCHIVED – You may archive the file
 os.SF_IMMUTABLE – You may not change the file
 os.SF_APPEND – You may only append to the file
 os.SF_NOUNLINK – You may not rename or delete the file
 os.SF_SNAPSHOT − It is a snapshot file

Most flags are such that only the super-user can change them. Also, some flags don’t work on all systems.
Sample usage:

1. >>> os.chflags('Today.txt',os.SF_NOUNLINK)

6. chmod(path,mode)

This Python os Module alters the mode of the path to the passed numeric mode. The mode may be on of the
following values(or a bitwise OR combination of them):

 stat.S_ISUID − Set user ID on execution


 stat.S_ISGID − Set group ID on execution
 stat.S_ENFMT – Enforced record locking
 stat.S_ISVTX – After execution, save text image
 stat.S_IREAD − Read by owner
 stat.S_IWRITE − Write by owner
 stat.S_IEXEC − Execute by owner
 stat.S_IRWXU − Read, write, and execute by owner
 stat.S_IRUSR − Read by owner
 stat.S_IWUSR − Write by owner
 stat.S_IXUSR − Execute by owner
 stat.S_IRWXG − Read, write, and execute by group
 stat.S_IRGRP − Read by group
 stat.S_IWGRP − Write by group
 stat.S_IXGRP − Execute by group
 stat.S_IRWXO − Read, write, and execute by others
 stat.S_IROTH − Read by others
 stat.S_IWOTH − Write by others
 stat.S_IXOTH − Execute by others

Sample usage:

1. >>> import stat


2. >>> os.chmod('Today.txt',stat.S_ISVTX)

This method does not return any value.

7. chroot(path)

chroot Python os Module alters the current process’ root directory to the given path. To use this, we need super-
user privileges.

Sample usage:

1. >>> os.chroot("/Photos")

This method returns no value.

8. close(fd)

This Python os module closes the associated file with descriptor fd.
1. >>> fd=os.open('Today.txt',os.O_RDWR)
2. >>> os.close(fd)

It does not return any value.

9. closerange(fd_low,fd_high)

closerange() closes all file descriptors from fd_low to fd_high. Here, fd_low is inclusive, and fd_high is
exclusive. Here, fd_low is the lowest file descriptor to be closed, while fd_high is the highest. This method
ignores errors.

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> os.closerange( fd, fd)

This method does not return any value.

10. dup(fd)

Python os Module dup(fd) returns a duplicate of the file descriptor fd.


Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> d_fd = os.dup( fd )
3. >>> os.write(d_fd, "Testing")
4. >>> os.closerange( fd, d_fd)

This method returns a duplicate of the file descriptor.

11. dup2(fd,fd2)

dup2() duplicates the descriptor fd to fd2. And if necessary, it closes fd2 first.

The interpreter assigns the new file description only when it is available.

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> fd2 = 1000
4. >>> os.dup2(fd, fd2)
5. >>> os.lseek(fd2, 0, 0)
6. >>> str = os.read(fd2, 100)
7. >>> print(f"Read String is {str}")
8. >>> os.close( fd )

This method returns a duplicate of the file descriptor.


12. fchdir(fd)

fchdir() alters the current working directory to the directory that the file descriptor fd represents. For this, it is
mandatory that the descriptor must refer to an opened directory, and not to an open file.
Sample usage:

1. >>> os.chdir("/var/www/html" )
2. >>> print "Current working dir : %s" % os.getcwd()
3. >>> fd = os.open( "/tmp", os.O_RDONLY )
4. >>> os.fchdir(fd)
5. >>> print "Current working dir : %s" % os.getcwd()
6. >>> os.close( fd )

This method doesn’t return any value.

13. fchmod(fd,mode)

This Python os Module alters the file mode of the file, specified by fd, to the numeric mode. The mode may be
one of the following (or an ORed combination of):

 stat.S_ISUID − Set user ID on execution


 stat.S_ISGID − Set group ID on execution
 stat.S_ENFMT − Record locking enforced
 stat.S_ISVTX − Save text image aGer execution
 stat.S_IREAD − Read by owner
 stat.S_IWRITE − Write by owner
 stat.S_IEXEC − Execute by owner
 stat.S_IRWXU − Read, write, and execute by owner
 stat.S_IRUSR − Read by owner
 stat.S_IWUSR − Write by owner
 stat.S_IXUSR − Execute by owner
 stat.S_IRWXG − Read, write, and execute by group
 stat.S_IRGRP − Read by group
 stat.S_IWGRP − Write by group
 stat.S_IXGRP − Execute by group
 stat.S_IRWXO − Read, write, and execute by others
 stat.S_IROTH − Read by others
 stat.S_IWOTH − Write by others
 stat.S_IXOTH − Execute by others

Sample usage:

1. >>> fd = os.open( "/tmp", os.O_RDONLY )


2. >>> os.fchmod( fd, stat.S_IXGRP)
3. >>> os.fchmod(fd, stat.S_IWOTH)
4. >>> print "Changed mode successfully!!"
5. >>> os.close( fd )

This method doesn’t return any value.


14. fchown(fd,uid,gid)

fchown() alters the owner and the group id of the file specified by fd to the numeric uid and gid. Setting an id to
-1 leaves it unchanged.
Sample usage:

1. >>> fd = os.open( "/tmp", os.O_RDONLY )


2. >>> os.fchown( fd, 100, -1)
3. >>> os.fchown( fd, -1, 50)
4. >>> print "Changed ownership successfully!!"
5. >>> os.close( fd )

This method doesn’t return any value.

15. fdatasync(fd)

fdatasync() forces writing the file with filedescriptor fd to disk. This, however, doesn’t force update on
metadata. You can do this to flush your buffer.

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> os.fdatasync(fd)
4. >>> os.lseek(fd, 0, 0)
5. >>> str = os.read(fd, 100)
6. >>> print(f"Read String is {str}")
7. >>> os.close( fd )

This method doesn’t return any value.

16. fdopen(fd[, mode[, bufsize]])

fdopen(), Python os Module returns an open file object. This object is connected to the descriptor fd. Once you
do this, you can perform all defined functions on the file object.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> fo = os.fdopen(fd, "w+")
3. >>> print (f"Current I/O pointer position {fo.tell()}")
4. >>> fo.write( "Python is a great language.\nYeah its great!!\n");
5. >>> os.lseek(fd, 0, 0)
6. >>> str = os.read(fd, 100)
7. >>> print (f"Read String is {str}")
8. >>> print (f"Current I/O pointer position {fo.tell()}")
9. >>> fo.close()

fdopen() returns an open file object that is connected to the file descriptor.

17. fpathconf(fd, name)


fpathconf() returns system configuration information that is relevant to an open file. This is quite similar to the
unix system call fpathconf(). It also accepts similar arguments.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> print (f"{os.pathconf_names}")
3. >>> no = os.fpathconf(fd, 'PC_LINK_MAX')
4. >>> print (f"Maximum number of links to the file: {no}")
5. >>> no = os.fpathconf(fd, 'PC_NAME_MAX')
6. >>> print (f"Maximum length of a filename :{no}")
7. >>> os.close( fd)

fpathconf() returns system configuration that is relevant to an open file.

18. fstat(fd)

Python os Module fstat() returns information about the file pertaining to the fd. Let’s take a look at the structure
fstat() returns:

 st_dev − ID of device containing file


 st_ino − inode number
 st_mode – protection
 st_nlink − number of hard links
 st_uid − user ID of owner
 st_gid − group ID of owner
 st_rdev − device ID (if special file)
 st_size − total size, in bytes
 st_blksize − blocksize for filesystem I/O
 st_blocks − number of blocks allocated
 st_atime − time of last access
 st_mtime − time of last modification
 st_ctime − time of last status change

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> info = os.fstat(fd)
3. >>> print (f"File Info: {info}")
4. >>> print (f"UID of the file: {info.st_uid}")
5. >>> print (f"GID of the file: {info.st_gid}")
6. >>> os.close( fd)

fstat() returns information about the file linked with the fd.

19. fstatvfs(fd)

This Python os module returns information pertaining to the file system containing the file linked with file
descriptor fd. This is the structure it returns:

 f_bsize − file system block size


 f_frsize − fragment size
 f_blocks − size of fs in f_frsize units
 f_bfree − free blocks
 f_bavail − free blocks for non-root
 f_files – inodes
 f_ffree − free inodes
 f_favail − free inodes for non-root
 f_fsid − file system ID
 f_flag − mount flags
 f_namemax − maximum filename length

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> info = os.fstatvfs(fd)
3. >>> print(f"File Info: {info}")
4. >>> print(f"Maximum filename length: {info.f_namemax}")
5. >>> print (f"Free blocks: {info.f_bfree}")
6. >>> os.close( fd)

fstatvfs() returns information about the file system containing the file linked.

20. fsync(fd)

This Python os Module forces write on the file liknked to the descriptor fd to disk. Beginning with a Python file
object f, first execute f.flush(), then perform os.fsync(f.fileno()). Do this to ensure all internal buffers linked to f
are written to the disk.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> os.fsync(fd)
4. >>> os.lseek(fd, 0, 0)
5. >>> str = os.read(fd, 100)
6. >>> print("Read String is: {str} ")
7. >>> os.close( fd )

fsync() doesn’t return any value.

21. ftruncate(fd,length)

ftruncate() truncates the file linked to the descriptor fd, so it holds at most length bytes in size.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> os.ftruncate(fd, 10)
4. >>> os.lseek(fd, 0, 0)
5. >>> str = os.read(fd, 100)
6. >>> print("Read String is: {str}”)
7. >>> os.close( fd )

ftruncate() doesn’t return any value.


22. getcwd()

getcwd() Python os Module returns the current working directory of a process.


Sample usage:

1. >>> os.getcwd()

‘C:\\Users\\lifei\\Desktop’

23. getcwdu()
getcwdu() returns a unicode object that represents the current working directory.
Sample usage:

1. >>> os.chdir("/var/www/html" )
2. >>> print(f"Current working dir: {os.getcwdu()}")
3. >>> fd = os.open( "/tmp", os.O_RDONLY )
4. >>> os.fchdir(fd)
5. >>> print(f"Current working dir: {os.getcwdu()}”)
6. >>> os.close( fd )

24. isatty(fd)
isatty()returns True if the descriptor fd is open, and is connected to a tty(-like) device. Otherwise, it returns
False.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "Testing")
3. >>> ret = os.isatty(fd)
4. >>> print(f"Returned value is: {ret}")
5. >>> os.close( fd )

25. lchflags(path,flags)
This Python os Module sets path flags to the numeric flags. Unlike chflags(), ut doesn’t follow symbolic links.
The flags may be one of the following values, or a bitwise OR combination of:

 UF_NODUMP − Do not dump the file


 UF_IMMUTABLE − The file may not be changed
 UF_APPEND − The file may only be appended to
 UF_NOUNLINK − The file may not be renamed or deleted
 UF_OPAQUE − The directory is opaque when viewed through a union stack
 SF_ARCHIVED − The file may be archived
 SF_IMMUTABLE − The file may not be changed
 SF_APPEND − The file may only be appended to
 SF_NOUNLINK − The file may not be renamed or deleted
 SF_SNAPSHOT − The file is a snapshot file.

Sample usage:
1. >>> path = "/var/www/html/Today.txt"
2. >>> fd = os.open( path, os.O_RDWR)
3. >>> os.close( fd )
4. >>> ret = os.lchflags(path, os.UF_IMMUTABLE )

lchflags() doesn’t return a value.

26. lchmod(path,mode)
lchmod() Python os Module ters the path mode to the numeric mode. If the path is a symlink, it affects the
symlink, not the target.
The mode may be one of the following values, or a bitwise OR combination of:

 stat.S_ISUID − Set user ID on execution


 stat.S_ISGID − Set group ID on execution
 stat.S_ENFMT − Record locking enforced
 stat.S_ISVTX − Save text image aGer execution
 stat.S_IREAD − Read by owner
 stat.S_IWRITE − Write by owner
 stat.S_IEXEC − Execute by owner
 stat.S_IRWXU − Read, write, and execute by owner
 stat.S_IRUSR − Read by owner
 stat.S_IWUSR − Write by owner
 stat.S_IXUSR − Execute by owner
 stat.S_IRWXG − Read, write, and execute by group
 stat.S_IRGRP − Read by group
 stat.S_IWGRP − Write by group
 stat.S_IXGRP − Execute by group
 stat.S_IRWXO − Read, write, and execute by others
 stat.S_IROTH − Read by others
 stat.S_IWOTH − Write by others
 stat.S_IXOTH − Execute by others

Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> fd = os.open( path, os.O_RDWR )
3. >>> os.close( fd )
4. >>> os.lchmod( path, stat.S_IXGRP)
5. >>> os.lchmod("/tmp/Today.txt", stat.S_IWOTH)

lchmod() doesn’t return any value.

27. lchown(path,uid,gid)
Python os Module lchown() alters the owner and group id of path to the numeric uid and gid. It doens’t follow
symbolic links. Setting an id to -1 leaves it unchanged.
Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> fd = os.open( path, os.O_RDWR)
3. >>> os.close( fd )
4. >>> os.lchown( path, 500, -1)
5. >>> os.lchown( path, -1, 500)

lchown() doesn’t return any value.

28. link(src,dst)
link() will create a hard link that points to an src named dst. You can do this when you want to create a copy of
an existing file.
Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> fd = os.open( path, os.O_RDWR )
3. >>> os.close( fd )
4. >>> dst = "/tmp/Today.txt"
5. >>> os.link( path, dst)

lilnk() doesn’t return any value.


29. listdir(path)
listdir() will return a list holding the names of the entries in the directory at the path. This list is in an arbitrary
order, and it exclude special entries ‘.’ and ‘..’, even if they exist in the directory.
Sample usage:

1. >>> path = "/var/www/html/"


2. >>> dirs = os.listdir( path )
3. >>> for file in dirs:
4. print(file)

30. lseek(fd,pos,how)
lseek() will set the current position of the descriptor fd to the specified position pos. ‘how’ modifies it.
Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "This is test")
3. >>> os.fsync(fd)
4. >>> os.lseek(fd, 0, 0)
5. >>> str = os.read(fd, 100)
6. >>> print(f"Read String is: {str}")
7. >>> os.close( fd )

lseek() doesn’t return any value.


31. lstat(path)
Like fstat(), lstat() returns information about a file, but does not follow symbolic links. lstat is an alias for fstat()
on those platforms that do not support symbolic links, for instance, Windows.
It returns the following structure:

 st_dev − ID of device containing file


 st_ino − inode number
 st_mode – protection
 st_nlink − number of hard links
 st_uid − user ID of owner
 st_gid − group ID of owner
 st_rdev − device ID (if special file)
 st_size − total size, in bytes
 st_blksize − blocksize for filesystem I/O
 st_blocks − number of blocks allocated
 st_atime − time of last access
 st_mtime − time of last modification
 st_ctime − time of last status change

Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> fd = os.open( path, os.O_RDWR)
3. >>> os.close( fd )
4. >>> info = os.lstat(path)
5. >>> print(f"File Info: {info}")
6. >>> print(f"UID of the file: {info.st_uid}")
7. >>> print(f"GID of the file: {info.st_gid}")

32. major(device)
major() takes a raw device number, and extracts the device major number (usually the st_dev or st_rdev field
from stat).
Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> info = os.lstat(path)
3. >>> major_dnum = os.major(info.st_dev)
4. >>> minor_dnum = os.minor(info.st_dev)
5. >>> print(f"Major Device Number: {major_dnum}")
6. >>> print(f"Minor Device Number: {minor_dnum}")

major() returns the device major number.


33. makedev(major,minor)
This Python os Module takes the minor and major device numbers, and creates a raw device number.
Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> info = os.lstat(path)
3. >>> major_dnum = os.major(info.st_dev)
4. >>> minor_dnum = os.minor(info.st_dev)
5. >>> print(f"Major Device Number: {major_dnum}")
6. >>> print(f"Minor Device Number: {minor_dnum}")
7. >>> dev_num = os.makedev(major_dnum, minor_dnum)
8. >>> print(f"Device Number: {dev_num}")

makedev() returns the device number.


34. makedirs(path[, mode])
makedirs() creates a directory recursively. This way, it is like mkdir(). However, it mandates that all
intermediate-level directories contain the leaf directory.
Sample usage:

1. >>> path = "/tmp/home/monthly/daily"


2. >>> os.makedirs( path, 0755 )

35. minor(device)
Python os Module minor() will take a raw device number, and extract the device’s minor (usually the st_dev or
st_rdev field from stat).
Sample usage:

1. >>> path = "/var/www/html/Today.txt"


2. >>> info = os.lstat(path)
3. >>> major_dnum = os.major(info.st_dev)
4. >>> minor_dnum = os.minor(info.st_dev)
5. >>> print(f"Major Device Number: {major_dnum}")
6. >>> print(f"Minor Device Number: {minor_dnum}")

minor() returns the device’s minor number.


36. mkdir(path[, mode])
mkdir() Python os Module creates a directory ‘path’ with the numeric mode ‘mode’. Some systems ignore
mode. But where used, it masks out the current umask value first.
Default mode=0777 (octal).
Sample usage:

1. >>> path = "/tmp/home/monthly/daily/hourly"


2. >>> os.mkdir( path, 0755 )

mkdir() doesn’t return any value.


37. mkfifo(path[, mode])
mkfifo() creates a FIFO named ‘path’ with the specified numeric mode. It masks out the current umask value
first.
Default mode=0666 (octal).
Sample usage:

1. >>> path = "/tmp/hourly"


2. >>> os.mkfifo( path, 0644 )

mkfifo() doesn’t return any value.

38. mknod(filename[, mode=0600, device])


This Python os Module will create a filesystem node named ‘filename’. This can be a file, a device-special file,
or a named pipe.
Sample usage:

1. >>> filename = '/tmp/tmpfile'


2. >>> mode = 0600|stat.S_IRUSR
3. >>> os.mknod(filename, mode)

mknod() doesn’t return any value.


39. open(file, flags[, mode])
open() will open the file ‘file’, and will set flags based on the specified flags. It possibly sets its mode according
to the specified mode. It also masks out the current umask value first.
Default mode=0777 (octal).
The flags may take one of these values, or a bitwise-OR combination of these:

 os.O_RDONLY − open for reading only


 os.O_WRONLY − open for writing only
 os.O_RDWR − open for reading and writing
 os.O_NONBLOCK − do not block on open
 os.O_APPEND − append on each write
 os.O_CREAT − create file if it does not exist
 os.O_TRUNC − truncate size to 0
 os.O_EXCL − error if create and file exists
 os.O_SHLOCK − atomically obtain a shared lock
 os.O_EXLOCK − atomically obtain an exclusive lock
 os.O_DIRECT − eliminate or reduce cache effects
 os.O_FSYNC − synchronous writes
 os.O_NOFOLLOW − do not follow symlinks

Sample usage:

1. >>> fd = os.open( "Today.txt", os.O_RDWR)


2. >>> os.write(fd, "This is test")
3. >>> os.close( fd )

open() returns the descriptor for the file we opened.

40. openpty()
Python os Module openpty() opens a pseudo-terminal pair. Then, it returns a pair of descriptors- master &
slave- for the pty & the tty, respectively.
Sample usage:

1. >>> m,s = os.openpty()


2. >>> print(m)
3. >>> print(s)
4. >>> s = os.ttyname(s)
5. >>> print(m)
6. >>> print(s)

41. pathconf(path,name)
Python os Module pathconf() returns system configuration information pertaining to a named file.
Sample usage:

1. >>> print(f"{os.pathconf_names}" )
2. >>> no = os.pathconf('a2.py', 'PC_NAME_MAX')
3. >>> print(f"Maximum length of a filename: {no}")
4. >>> no = os.pathconf('a2.py', 'PC_FILESIZEBITS')
5. >>> print(f"file size in bits: {no}")
42. pipe()
pipe() creates a pipe. Then, it returns a pair of descriptors- r & w- for reading and writing.
Sample usage:

1. >>> os.pipe()

(3, 4)
43. popen(command[, mode[, bufsize]])
This Python os Module popen() will open a pipe to, or from, the command specified .It returns an open file
object that is connected to the pipe. We can read or write to this object depending on whether the mode is ‘r’
(default) or ‘w’. The bufsize argument means the same as in the open() function.
Sample usage:

1. >>> a = 'mkdir nwdir'


2. >>> b = os.popen(a,'r',1)

44. read(fd,n)
read() Python os Module will let us read at most n bytes from the desciptor fd. It returns a string holding the
bytes we just read. And if it reaches the end of file, it returns an empty string.
Sample usage:

1. >>> fd = os.open("f1.txt",os.O_RDWR)
2. >>> ret = os.read(fd,12)
3. >>> print(ret)
4. >>> os.close(fd)

45. readlink(path)
Python os Module readlink() will return a string denoting the path to which the symbolic link points. It may
return a relative or an absolute pathname.
Sample usage:

1. >>> src = '/usr/bin/python'


2. >>> dst = '/tmp/python'
3. >>> os.symlink(src, dst)
4. >>> path = os.readlink( dst )
5. >>> print(path)

46. remove(path)
remove() removes the specified file path. If that path is a directory, it raises an OSError.
Sample usage:

1. >>> print(f"The dir is: {os.listdir(os.getcwd())}")


2. >>> os.remove("aa.txt")
3. >>> print(f"The dir after removal of path: {os.listdir(os.getcwd())}")

remove() doesn’t return any value.

47. removedirs(path)
This Python os Module will remove directories recursively. And if we successfully remove the leaf directory, it
attempts to successively remove every parent directory displayed in that path.
Sample usage:
1. >>> print(f"The dir is: {os.listdir(os.getcwd())}")
2. >>> os.removedirs("/tutorialsdir")
3. >>> print(f"The dir after removal is: {os.listdir(os.getcwd())}")

removedirs() doesn’t return any value.


48. rename(src,dst)
rename() renames a file or directory. If the destination is a file or a directory that already exists, it raises an
OSError.
Sample usage:

1. >>> print(f"The dir is: {os.listdir(os.getcwd())}”)


2. >>> os.rename("tutorialsdir","tutorialsdirectory")
3. >>> print(“Successfully renamed”)
4. >>> print(f"The dir is: {os.listdir(os.getcwd())}")

rename() doesn’t return any value.


49. renames(old,new)
renames() Python os Module renames directories and files recursively. It is like os.rename(), but it also moves a
file to a directory, or a whole tree of directories, that do not already exist.
Sample usage:

1. >>> print("Current directory is: { os.getcwd()}")


2. >>> print("The dir is: { os.listdir(os.getcwd())}")
3. >>> os.renames("aa1.txt","newdir/aanew.txt")
4. >>> print("Successfully renamed”)
5. >>> print(f"The dir is: {os.listdir(os.getcwd())}")

renames() does not return any value.

50. rmdir(path)
Python os Module rmdir() removes the directory path specified. If the directory isn’t empty, however, it raises
an OSError.
Sample usage:

1. >>> print(f"the dir is: { os.listdir(os.getcwd())}")


2. >>> os.rmdir("mydir")
3. >>> print(f"the dir is: { os.listdir(os.getcwd())}"

rmdir() doesn’t return any value.


51. stat(path)
This Python os Module performs a stat system call on the specified path.
These are the members of the stat structure:

 st_mode − protection bits


 st_ino − inode number
 st_dev − device
 st_nlink − number of hard links
 st_uid − user id of owner
 st_gid − group id of owner
 st_size − size of file, in bytes
 st_atime − time of most recent access
 st_mtime − time of most recent content modification
 st_ctime − time of most recent metadata change.

Sample usage:

1. >>> statinfo = os.stat('a2.py')


2. >>> print(statinfo)

52. stat_float_times([newvalue])
stat_float_times() Python os Module decides whether stat_result denotes time stamps as float objects.
Sample usage:

1. >>> import os, sys


2. >>> statinfo = os.stat('a2.py')
3. >>> print(statinfo)
4. >>> statinfo = os.stat_float_times()
5. >>> print(statinfo)

53. statvfs(path)
Python os Module statvfs() executes a statvfs system call on the specified path.
The structure has the following members:

 f_bsize − preferred file system block size


 f_frsize − fundamental file system block size
 f_blocks − total number of blocks in the filesystem
 f_bfree − total number of free blocks
 f_bavail − free blocks available to non-super user
 f_files − total number of file nodes
 f_ffree − total number of free file nodes
 f_favail − free nodes available to non-super user
 f_flag − system dependent
 f_namemax − maximum file name length

Sample usage:

1. >>> stinfo = os.statvfs('a1.py')


2. >>> print(stinfo)

54. symlink(src,dst)
symlink() composes a symbolic link dst that points to the source.
Sample usage:

1. >>>src = '/usr/bin/python'
2. >>> dst = '/tmp/python'
3. >>> os.symlink(src, dst)

symlink() returns no value.


55. tcgetpgrp(fd)
This Python os Module returns the process group linked to the terminal specified by fd, which is an open file
descriptor, and is returned by os.open().
Sample usage:

1. >>> print(f"Current working dir : { os.getcwd()}")


2. >>> fd = os.open("/dev/tty",os.O_RDONLY)
3. >>> f = os.tcgetpgrp(fd)
4. >>> print(f"the process group associated is: {f}")
5. >>> os.close(fd)

tcgetpgrp() returns the process group.

56. tcsetpgrp(fd, pg)


Python os Module tcsetpgrp() sets the process group linked to the terminal specified by fd, which is an open file
descriptor, and is returned by os.open(), to pg.
Sample usage:

1. >>> print(f"Current working dir : { os.getcwd()}")


2. >>> fd = os.open("/dev/tty",os.O_RDONLY)
3. >>> f = os.tcgetpgrp(fd)
4. >>> print(f"the process group associated is: {f}")
5. >>> os.tcsetpgrp(fd,2672)
6. >>> print("done")
7. >>> os.close(fd)

tcsetpgrp() returns no value.


57. tempnam([dir[, prefix]])
tempnam() Python os Module returns a unique path name reasonable enough to create a temporary file.
Sample usage:

1. >>> tmpfn = os.tempnam('/tmp/tutorialsdir,'tuts1')

tempnam() returns a unique path.

58. tmpfile()
tmpfile() will return a new temporary file object, opening it in update mode (w+b). This file has zero directory
entries linked to it, and will automatically delete when no descriptors are available.
Sample usage:

1. >>> tmpfile = os.tmpfile()


2. >>> tmpfile.write('Temporary newfile is here.....')
3. >>> tmpfile.seek(0)
4. >>> print(tmpfile.read())
5. >>> tmpfile.close()

59. tmpnam()
tmpnam() will return a unique path name reasonable enough to create a temporary file.
Sample usage:

1. >>> tmpfn = os.tmpnam()


2. >>> print(f"This is the unique path: {tmpfn}")
60. ttyname(fd)
ttyname() Python os Module will return a string that denotes the terminal device linked to the descriptor fd. If it
isn’t linked to a terminal device, it raises an exception.
Sample usage:

1. >>> print(f"Current working dir : { os.getcwd()}")


2. >>> fd = os.open("/dev/tty",os.O_RDONLY)
3. >>> p = os.ttyname(fd)
4. >>> print(f"the terminal device associated is: {p}")
5. >>> os.close(fd)

ttyname() returns a string that denotes the terminal device.

61. unlink(path)
This Python os Module will remove specified file path. If it is a directory, it raises an OSError.
Sample usage:

1. >>> print(f"The dir is: { os.listdir(os.getcwd())}")


2. >>> os.unlink("aa.txt")
3. >>> print(f"The dir after removal of path : { os.listdir(os.getcwd())}")

unlink() doesn’t return any value.


62. utime(path,times)
Python os Module utime() sets the access and modified times of the file at the specified path.
Sample usage:

1. >>> stinfo = os.stat('a2.py')


2. >>> print(stinfo)
3. >>> print(f"access time of a2.py: { stinfo.st_atime }")
4. >>> print(f"modified time of a2.py: { stinfo.st_mtime }")
5. >>> os.utime("a2.py",(1330712280, 1330712292))

utime() returns no value.


63. walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
walk() creates file names in a directory tree. It does so by walking the tree either bottom-up or top-down.
It has the following parameters:

1. top − Each directory rooted at directory


2. topdown − If topdown is True, or not specified, it scans directories top-down.
3. onerror − This may show an error to continue with the walk, or may raise an exception to abort the
walk.
4. followlinks − This will visit directories that symlinks points to, that is, if set to true.

Sample usage:

1. >>> for root, dirs, files in os.walk(".", topdown=False):

for name in files:


print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))

64. write(fd,str)
This Python os Module will write the specified string to descriptor fd. It returns the number of bytes that it
actually wrote.
Sample usage:

1. >>> fd = os.open("f1.txt",os.O_RDWR|os.CREAT)
2. >>> ret = os.write(fd,"This is test")
3. >>> print(f"the number of bytes written: {ret}")
4. >>> print("written successfully")
5. >>> os.close(fd)

So, this was all about Python os Module. Hope you like our explanation.

66. Conclusion
Hence, we cover all the Python os module. This will allow you to work your way around the directories without
any problem. If you have any query regarding Python os module, feel free to share with us. Surely we will get
back to you!

Python pprint Module – Python Data Pretty Printer


by DataFlair Team · March 1, 2019

Earlier we have discussed Python OS Module. Today, we will see Python pprint i.e. Python Pretty Print. Also,
we will discuss Python pprint format and its example. This article includes recursive data structures and own
classes in Pretty print in Python. At last, we will see Python pprintpp Module.

So, let’s start the Python pprint tutorial.


Python pprint Module – Python Data Pretty Printer

1. What is Python Pretty Print?


The pprint module lets us pretty-print arbitrary data structures in Python to make them prettier, well-
formatted, and more readable. What it gives us is in a form we can use as input to the interpreter. This is just
the kind of thing for aesthetes and it keeps the output on a single line wherever possible. And when on multiple
lines, it indents it. Let’s import it:

1. >>> from pprint import pprint

Basically, this module has the following class:

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)

This constructs a PrettyPrinter instance for us. Let’s talk about its parameters:

 indent gives us the amount of indentation for each recursive level


 width gives us the desired output width
 depth gives us the number of levels to print
 stream lets us set an output stream
 compact lets us fit as many items within the width as we can on each line of output

Now, let’s try an example without pprint. Let’s take a list to work with:

1. >>> data=[(1,{'a':'A','b':'B','c':'C','d':'D'}),(2,{'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L'}),(3,['m','n']),(4,
['o',' p','q','r','s','t','u','v','w']),(5,['x','y','z']),]
2. >>> print(data)
[(1, {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}), (2, {‘e’: ‘E’, ‘f’: ‘F’, ‘g’: ‘G’, ‘h’: ‘H’, ‘i’: ‘I’, ‘j’: ‘J’, ‘k’: ‘K’, ‘l’:
‘L’}), (3, [‘m’, ‘n’]), (4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]), (5, [‘x’, ‘y’, ‘z’])]

2. Python pprint Example


So, let’s try printing this with pprint instead.

1. >>> pprint(data)

[(1, {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}),

(2,

{‘e’: ‘E’,

‘f’: ‘F’,

‘g’: ‘G’,

‘h’: ‘H’,

‘i’: ‘I’,

‘j’: ‘J’,

‘k’: ‘K’,

‘l’: ‘L’}),

(3, [‘m’, ‘n’]),

(4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),

(5, [‘x’, ‘y’, ‘z’])]

So, what does Python pprint do? It formats an object and writes it to the data stream we pass to it as an
argument. By default, this is sys.stdout.

Learn about Python sys Module.

3. Python pprint Formatting


We can format a data structure without having to write it to a stream. To do this, we use pformat() to build a
string representation, and possible use cases include logging.

1. >>> import logging


2. >>> from pprint import pformat
3. >>> logging.basicConfig(
4. level=logging.DEBUG,
5. format='%(levelname)-8s %(message)s',)
6. >>> logging.debug('Logging pformatted data')

DEBUG Logging pformatted data

1. >>> formatted=pformat(data)

So, we can print or log this string independently:

1. >>> for line in formatted.splitlines():


2. logging.debug(line.rstrip())

DEBUG [(1, {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}),

DEBUG (2,

DEBUG {‘e’: ‘E’,

DEBUG ‘f’: ‘F’,

DEBUG ‘g’: ‘G’,

DEBUG ‘h’: ‘H’,

DEBUG ‘i’: ‘I’,

DEBUG ‘j’: ‘J’,

DEBUG ‘k’: ‘K’,

DEBUG ‘l’: ‘L’}),

DEBUG (3, [‘m’, ‘n’]),

DEBUG (4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),

DEBUG (5, [‘x’, ‘y’, ‘z’])]

4. Python Pretty-Printing Our Own Classes


Last, we talked about the repr() built-in function in Python. In that, we observed the repr () method.
Today, let’s see how we can make pprint work with our own classes by making use of the repr () method.

1. >>> class Color:


2. def init (self,name,hex_value):
3. self.name=name
4. self.hex_value=hex_value
5. def repr (self):
6. return(
7. 'I am '+self.name+' and you can find me at '+self.hex_value)
8. >>> colors=[Color('salmon','#FA8072'),Color('olive','#808000'),Color('purple','#800080')]
9. >>> print(colors)

[I am salmon and you can find me at #FA8072, I am olive and you can find me at #808000, I am purple
and you can find me at #800080]

Now see what Python pprint does to this:

1. >>> pprint(colors)

[I am salmon and you can find me at #FA8072,

I am olive and you can find me at #808000,

I am purple and you can find me at #800080]

5. Python Pprint with Recursive Data Structures


Let’s create a list and append it to its end. This makes for a recursive list.

1. >>> from pprint import pprint


2. >>> mylist=[1,2,'c','d']
3. >>> mylist.append(mylist)
4. >>> id(mylist)

45553504

1. >>> pprint(mylist)

[1, 2, ‘c’, ‘d’, <Recursion on list with id=45553504>] #The recursive reference

1. >>> mylist #Pay attention

[1, 2, ‘c’, ‘d’, […]]

 Controlling Depth-

To control how far the Python pretty printer recurses down a nested structure, we can use the depth argument:

1. >>> pprint(data,depth=1)

[(…), (…), (…), (…), (…)]

1. >>> pprint(data,depth=2)

[(1, {…}), (2, {…}), (3, […]), (4, […]), (5, […])]
1. >>> pprint(data,depth=3)

[(1, {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}),

(2,

{‘e’: ‘E’,

‘f’: ‘F’,

‘g’: ‘G’,

‘h’: ‘H’,

‘i’: ‘I’,

‘j’: ‘J’,

‘k’: ‘K’,

‘l’: ‘L’}),

(3, [‘m’, ‘n’]),

(4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),

(5, [‘x’, ‘y’, ‘z’])]

Here, the ellipses denote the levels it excludes from the output.

6. Deciding Output Width


We can decide the width for the output- this is the number of columns. The default is 80, but we can change that
with width.

1. >>> mylist=[1,2,'c','d']
2. >>> pprint(mylist)

[1, 2, ‘c’, ‘d’]

1. >>> pprint(mylist,width=-1)

[1,

2,

‘c’,

‘d’]
So, you can also pretty-print the output of the listdir() method or your system’s environment variables with
pprint(dict(os.environ),width=1).

1. >>> pprint(os.listdir())

[‘DLLs’,

‘Doc’,

‘etc’,

‘include’,

‘Lib’,

‘libs’,

‘LICENSE.txt’,

‘man’,

‘NEWS.txt’,

‘opencv_ffmpeg343.dll’,

‘out.log’,

‘python.exe’,

‘python3.dll’,

‘python37.dll’,

‘pythonw.exe’,

‘Scripts’,

‘share’,

‘tcl’,

‘Tools’,

‘vcruntime140.dll’]

You can use the compact flag to make this more compact-

1. >>> pprint(os.listdir(),compact=True)

[‘DLLs’, ‘Doc’, ‘etc’, ‘include’, ‘Lib’, ‘libs’, ‘LICENSE.txt’, ‘man’,


‘NEWS.txt’, ‘opencv_ffmpeg343.dll’, ‘out.log’, ‘python.exe’, ‘python3.dll’,

‘python37.dll’, ‘pythonw.exe’, ‘Scripts’, ‘share’, ‘tcl’, ‘Tools’,

‘vcruntime140.dll’]

7. The pprintpp Module


Before we say goodbye, why don’t we talk about pprintpp? This is another module for pretty printing and we
like to call it pprint++. It is available in the PyPI and you can install it as:

1. Python pip install pprintpp

Let’s import this.

1. >>> import pprintpp

Now, let’s try printing something with this.

1. >>> pprintpp.pprint(data)

Python pprintpp module

 pp-ez

You can also install the pp-ez package to do this:

1. >>> pp.pprint(data)
This gives us the same output. So how is pprint++ different from pprint? One difference is what you can see in
the outputs. Well, the goal of pprint++ is to emit a readable representation of the input that is also largely PEP-8
compliant.

So, this was all in Python pprint. Hope you liked our explanation of Python Pretty Print.

8. Summary
Hence, we discussed the Python pprint module and its method with its attributes. Some among these were
width, depth, and compact. Moreover, we looked at the example of Python pretty print. Also, we saw formatting
in Pretty Print. Happy pretty-printing! This is the time to explore the most asked Python interview questions.

Create Python Virtual Environment – Install Python Packages


by DataFlair Team · September 27, 2018

1. Python Virtual Environment


In this blog, we will discuss what a Python virtual environment is? Moreover, we will study how to create
Virtual Environment in Python Programming Language and how to install Python packages using pip. Along
with this, we talked about installing Python. If you still haven’t installed Python, do it right away.

So, let’s start creating Python Virtual Environment.

Python Virtual Environment and Install Python Packages


2. What is Python Virtual Environment?
Developing a Python application, you may want to use modules that don’t ship with the standard library. Or
sometimes, you need a specific version of a library for a bug to be fixed, or for some other reason.

Does this mean that installing Python once isn’t enough for every application you craft? Maybe application X
needs version 1.0, but application Y needs version 2.0. This leaves one of them unable to run.

To work with this, we create a Python virtual environment. In essence, it is a self-contained directory tree
containing a Python installation for a particular version of Python. Apart from that, it also has a list of additional
packages. This way, application X can have its virtual environment with version 1.0, and B can have its own
with version 2.0.

Lets start with how to create Python Virtual Environment.

3. How to Create a Virtual Environment


in Python?
To create a Python virtual environment and manage it, we use the module venv. Normally, it will install the
latest version of Python for you, but you can choose that. But before we begin, let’s find out which version of
Python we are using.

To do this, type the following in your Command Prompt (for Windows; type ‘cmd’ in search):
python –version

This is what it will look like:

Microsoft Windows [Version 10.0.16299.248]

(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\lifei>python –version
Python 3.6.0

C:\Users\lifei>
Now, let’s check if we have pip installed.

C:\Users\lifei>pip –version
pip 9.0.1 from c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages (python 3.6)

We do. This is because we installed Python from python.org. In case you do not have pip installed, you may
need to install it manually.

Now, let’s begin. In your command prompt, move to the directory you want to work in:

C:\Users\lifei>cd Desktop
Now, type in the following:

C:\Users\lifei\Desktop>python -m venv workwithenv

You will see the following directory on your Desktop (or whichever directory you chose to work with):

Python Virtual Environment and Install Python Packages

Inside this, you can see the following contents:

Python Virtual Environment and Install Python Packages

Python Virtual Environment and Install Python Packages

Virtual Environment and Install Python Packages – sitepackages


Python Virtual Environment and Install Python Packages – scripts

While the directory Include is empty, the other two aren’t:

As you can see, the command created the directory ‘workwithenv’ because it didn’t already exist. It also created
subdirectories containing a copy of the Python interpreter, the standard library, and various supporting files.

Now, to activate the Python Virtual Environment, we will run the batch file activate.bat in the directory Scripts:

C:\Users\lifei\Desktop>workwithenv\Scripts\activate.bat

We get the following output:

(workwithenv) C:\Users\lifei\Desktop>

This script is for the bash shell. If you have any doubt in Python Virtual environment, Please comment. Before
starting with Next topic you can read Python Packages Comprehensive Guide here.

4. Managing Python Packages with pip


Like we said earlier, pip is a program that will let you install, upgrade, and remove Python packages. Using pip,
you can install Python packages from the Package Index (PyPI). Or, you can use pip’s search feature this way:
C:\Users\lifei\Desktop\workwithenv>pip search astronomy
acalib (0.1.3) – Advanced Computing for Astronomy Library
vaex-astro (0.1.5) – Astronomy related transformations and FITS file support
astro-scripts (0.4.0) – Small scripts for astronomy
astrobase (0.3.8) – Python modules and scripts useful for variable star work in astronomy.
astrocats (0.3.32) – Package for downloading, analyzing, and constructing open astronomy catalogs.
astrodbkit (0.6.6) – Astronomy database management using SQL and Python
astroML (0.3) – Tools for machine learning and data mining in Astronomy
astromodels (0.4.1) – Astromodels contains models to be used in likelihood or Bayesian analysis in
astronomy
astroobs (1.4.5) – Provides astronomy ephemeris to plan telescope observations
astropy (3.0) – Community-developed python astronomy tools
astropyp (0.0.dev133) – Astronomy Pypeline Framework and FITS Viewer
astrotoyz (0.1.4) – Astronomy tools built on the Toyz framework
Barak (0.3.2) – A set of astronomy-related routines for generating Voigt profiles from atomic data,
reading and writing data, working with SEDs, passbands and dust extinction laws.
cadcdata (1.2.1) – Client for accessing data at the Canadian Astronomy Data Centre
python-casacore (2.2.1) – A wrapper around CASACORE, the radio astronomy library
cygrid (0.9.8) – Cygrid is a cython-powered convolution-based gridding module for astronomy
DDFacet (0.3.2) – Facet-based radio astronomy continuum imager
f311 (18.1.31.1) – Astronomy-related API, command-line tools, and windowed applications
gammapy (0.7) – A Python package for gamma-ray astronomy
gary (0.1a) – Galactic astronomy and gravitational dynamics.
gastropy (0.0dev) – (g)astronomy
hips (0.2) – Python astronomy package for HiPS
mclearn (0.1.6) – Active learning algorithms with application in astronomy.
novas (3.1.1.4) – The United States Naval Observatory NOVAS astronomy library
palpy (1.8.1) – PAL — A Positional Astronomy Library
PyAstronomy (0.12.0) – A collection of astronomy related tools for Python.
pyzpace (0.2.dev1) – Zach Pace’s astronomy-related python tools
sinistra (0.3.3) – A collection of astronomy related tools.
skyfield (1.1) – Elegant astronomy for Python
SWHT (0.1.2) – Spherical Wave Harmonic Transform for radio astronomy visibility data

C:\Users\lifei\Desktop\workwithenv>

This listed out a list of packages that work with astronomy. Now, we want to search for packages working with
numbers. (We won’t include the output since it’s too long to include here) But I have a feeling we want to work
with the package ‘numpy’ here. Let’s install python packages. I also recommend you to read Python Modules
vs Packages for better understanding.

a. Installing Python Packages


C:\Users\lifei\Desktop\workwithenv>pip install numpy

Collecting numpy

Using cached numpy-1.14.1-cp36-none-win32.whl

Installing collected packages: numpy


Successfully installed numpy-1.14.1

C:\Users\lifei\Desktop\workwithenv>

This is what it looks like in the command prompt:

Python Virtual Environment – Install Python Packages

Now, we look into C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\Lib\site-packages, and we find


this:

Python Virtual Environment – Install Python Packages

Actually, pip has a number of subcommands like search, install, uninstall, and freeze.

You can also install a specific version of a package by following the package name by ==, and then by the
version number:

pip install numpy==1.14.1

In fact, to check what version of numpy you are using, you can type the following in the interpreter:

1. >>> import numpy


2. >>> numpy.version.version
‘1.14.1’

Or, you can do:

1. >>> numpy. version

‘1.14.1’

Running the command to install numpy again will just be ignored by the interpreter.

C:\Users\lifei\Desktop\workwithenv>pip install numpy

Requirement already satisfied: numpy in

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages

b. Checking for Upgrades


However, you can check for upgrades:

C:\Users\lifei\Desktop\workwithenv>pip install –upgrade numpy

Requirement already up-to-date: numpy in

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages

c. Uninstalling a Python Package


To uninstall a package, we do this:

C:\Users\lifei\Desktop\workwithenv>pip uninstall certifi

Uninstalling certifi-2018.1.18:

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\description.rst

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\installer c:\users\lifei\appdata\local\programs\python\python36-

32\lib\site-packages\certifi- 2018.1.18.dist-info\license.txt

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\metadata
c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\metadata.json

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\record

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\top_level.txt

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi-

2018.1.18.dist-info\wheel

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\ init .py

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\ main__.py

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\ pycache \ init .cpython-36.pyc

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\ pycache \ main .cpython-36.pyc

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\ pycache \core.cpython-36.pyc

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-

packages\certifi\cacert.pem

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages\certifi\core.py

Proceed (y/n)? y

Successfully uninstalled certifi-2018.1.18

C:\Users\lifei\Desktop\workwithenv>
d. Learning About a Python Package
To find out about a particular package, we use the subcommand ‘show’:

C:\Users\lifei\Desktop\workwithenv>pip show numpy

Name: numpy

Version: 1.14.1

Summary: NumPy: array processing for numbers, strings, records, and objects.

Home-page: http://www.numpy.org

Author: NumPy Developers

Author-email: numpy-discussion@python.org

License: BSD

Location: c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages

Requires:

e. List the Packages Installed in Python


Now, to list all packages that are installed, we use ‘list’:

C:\Users\lifei\Desktop\workwithenv>pip list

DEPRECATION: The default format will switch to columns in the future. You can use –format=

(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section)

to disable this warning.

numpy (1.14.1)

pip (9.0.1)

setuptools (28.8.0)

virtualenv (15.1.0)

This listed the packages alongside the versions installed.


f. Freeze
The freeze subcommand with pip will return a list of packages installed, but in the format that

pip install expects:

C:\Users\lifei\Desktop\workwithenv>pip freeze

numpy==1.14.1

virtualenv==15.1.0

So, this is all about Python Virtual Environment and Install Python Packages. Hope you like our explanation.

5. Conclusion
Hence, in this lesson on Python virtual environment and Python Packages, we learned about how to set up a
virtual environment so all your applications will run without a dispute. Then, we learned how to use pip to
install a package. We also saw subcommands like show, list, freeze, and so. Furthermore, if you have any
query, feel free to share with us!

Python Modules vs Packages | Differences Between Python


Modules and Packages
by DataFlair Team · September 26, 2018

1. Python Modules vs Packages


In our article on Python Modules vs Packages, we discussed what they are, and saw how to create our own. We
also saw where they are located in our file system. But throughout the article, we saw a lot of similarities to
modules. Hence, we dedicate this article to Differences Between Python Modules and Packages.

Python Modules vs Packages


2. What is Python Modules?
A module is a Python file containing Python statements and definitions. For example, a file evenodd.py is a
module, and we call it ‘evenodd’. We put similar code together in one module. This helps us modularize our
code, and make it much easier to deal with. And not only that, a module grants us reusability. With a module,
we don’t need to write the same code again for a new project that we take up.

In our previous article, we created a module evenodd in package two. This is what evenodd.py holds:

1. def check():
2. a=int(input('Enter a number'))
3. if a%2==0: print("Even")
4. else: print("Odd")

a. How to Import a Python Module?


So, as you can see, a module simply contains Python code. Consequently, we can import it, like a package.

1. >>> import one.two.evenodd


2. >>>

To call function check(), we do the following:

1. >>> from one.two.evenodd import check


2. >>> check()

Enter a number7
Odd

1. >>>

Another example would be the constants ‘pi’ and ‘e’ from the ‘math’ module.

1. >>> import math


2. >>> from math import pi
3. >>> math.pi

3.141592653589793

1. >>> math.e

2.718281828459045
We can also import using an alias or using the from..import statement, like we do with packages. To import
everything from a module, we do the following:

1. >>> from math import *


2. >>> e
2.718281828459045

1. >>> pi

3.141592653589793

1. >>>

Let’s update evenodd.py to have two functions- check and evenodd.

1. def check():
2. a=int(input('Enter a number'))
3. if a%2==0: print("Even")
4. else: print("Odd")
5. def add(a,b):
6. return a+b

Now, if we want to import all functions from module evenodd, we can just use the wildcard *:

1. >>> from one.two.evenodd import *


2. >>> check()

Enter a number0
Even

1. >>> add(3,4)

b. Search Path
When we import a module, the interpreter first looks in the current directory. Then, it looks into
PYTHONPATH, an environment variable with a list of directories. Finally, it looks into the installation-
dependent default directory.

1. >>> import sys


2. >>> for i in sys.path:
3. print(i)

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\Lib\idlelib

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\python36.zip

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\DLLs

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\site-packages
c. Reloading a Python Module
A module is imported only once. This is for efficiency purposes. Let’s add this code to evenodd.py:

1. print("Loading evenodd")
2. num=7

Now, let’s restart the shell, and import evenodd thrice.

1. >>> import one.two.evenodd

Loading evenodd

1. >>> import one.two.evenodd


2. >>> import one.two.evenodd

See? It imported it only once. Because of this, Python gives us a function to let us reload the module when we
want to. This is in the module ‘imp’.

1. >>> import imp


2. >>> imp.reload(one.two.evenodd)

Loading evenodd
<module ‘one.two.evenodd’ from ‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-
packages\\one\\two\\evenodd.py’>

d. dir()
Finally, dir() will let us check the components of a module.

1. >>> dir(one.two.evenodd)

[‘ builtins ’, ‘ cached ’, ‘ doc ’, ‘ file ’, ‘ loader ’, ‘ name ’, ‘ package ’, ‘ spec ’, ‘add’,


‘check’ , ‘num’] We saw this function when we talked about the Built-in Functions in Python.

3. What is Python Packages?


A package, in essence, is like a directory holding subpackages and modules. While we can create our own
packages, we can also use one from the Python Package Index (PyPI) to use for our projects.

To import a package, we type the following:

import Game.Sound.load

We can also import it giving it an alias:

import Game.Sound.load as load game


You can’t import a function using the dot operator(.) For that, you must type this:

from Game.Sound.load import volume_up

A package must have the file init .py, even if you leave it empty.

But when we import a package, only its immediate modules are imported, not the sub-packages. If you try to
access those, it will raise an AttributeError.

To get a deeper insight into packages, check Python Packages.

4. Differences Between Python Modules and


Packages
So, now that we’ve revised both modules and packages, let’s see how they differ:

1. A module is a file containing Python code. A package, however, is like a directory that holds sub-
packages and modules.
2. A package must hold the file init .py. This does not apply to modules.
3. To import everything from a module, we use the wildcard *. But this does not work with packages.

This was all about the article on Python Modules vs Packages

5. Conclusion
Now that we know what subtle differences exist between a module and a package, let’s take up a challenge and
take up a project of our own. Let’s do this to test our package skills? Okay.

Furthermore, if you have any query, feel free to approach us!

Python Set and Booleans with Syntax


and Examples
by DataFlair Team · May 27, 2019

1. Python Set and Python Booleans


So far, we have learned about various data types in Python. We dealt with strings, numbers, lists, tuples, and
dictionaries. We’ve also learned that we don’t need to declare the type of data while defining it. Today, we will
talk about Python set examples and Python Booleans. After that, we will move onto python functions in further
lessons.
Python Sets and Booleans

2. Sets in Python
First, we focus on Python sets. A set in Python holds a sequence of values. It is sequenced but does not support
indexing. We will understand that as we get deeper into the article with the Python set Examples.

a. Creating a Python Set


To declare a set, you need to type a sequence of items separated by commas, inside curly braces. After that,
assign it to a Python variable.

1. >>> a={1,3,2}

As you can see, we wrote it in the order 1, 3, 2. In point b, we will access this set and see what we get back.

A set may contain values of different types.

1. >>> c={1,2.0,'three'}

1. Duplicate Elements

A set also cannot contain duplicate elements. Let’s try adding duplicate elements to another set, and then access
it in point b.

1. >>> b={3,2,1,2}
2. Mutability

A set is mutable, but may not contain mutable items like a list, set, or even a dictionary.

1. >>> d={[1,2,3],4}

Traceback (most recent call last):

File “<pyshell#9>”, line 1, in <module>

d={[1,2,3],4}

TypeError: unhashable type: ‘list’

As we discussed, there is no such thing as a nested Python set.

1. >>> d={{1,3,2},4}

Traceback (most recent call last):

File “<pyshell#10>”, line 1, in <module>

d={{1,3,2},4}

TypeError: unhashable type: ‘set’

3. The Python set() function

You can also create a set with the set() function.

1. >>> d=set()
2. >>> type(d)

<class ‘set’>

This creates an empty set object. Remember that if you declare an empty set as the following code, it is an
empty dictionary, not an empty set. We confirm this using the type() function.

1. >>> d={}
2. >>> type(d)

<class ‘dict’>

The set() function may also take one argument, however. It should be an iterable, like a list.

1. >>> d=set([1,3,2])
b. Accessing a Set in Python
Since sets in Python do not support indexing, it is only possible to access the entire set at once. Let’s try
accessing the sets from point a.

1. >>> a

{1, 2, 3}

Did you see how it reordered the elements into an ascending order? Now let’s try accessing the set c.

1. >>> c

{1, 2.0, ‘three’}

Finally, let’s access set b.

1. >>> b

{1, 2, 3}

As you can see, we had two 2s when we declared the set, but now we have only one, and it automatically
reordered the set.

Also, since sets do not support indexing, they cannot be sliced. Let’s try slicing one.

1. >>> b[1:]

Traceback (most recent call last):

File “<pyshell#26>”, line 1, in <module>

b[1:]

TypeError: ‘set’ object is not subscriptable

As you can see in the error, a set object is not subscriptable.

c. Deleting a Set
Again, because a set isn’t indexed, you can’t delete an element using its index. So for this, you must use one the
following methods. A method must be called on a set, and it may alter the set. For the following examples, let’s
take a set called numbers.

1. >>> numbers={3,2,1,4,6,5}
2. >>> numbers

{1, 2, 3, 4, 5, 6}
1. discard()

This method takes the item to delete as an argument.

1. >>> numbers.discard(3)
2. >>> numbers

{1, 2, 4, 5, 6}

As you can see in the resulting set, the item 3 has been removed.

2. remove()

Like the discard() method, remove() deletes an item from the set.

1. >>> numbers.remove(5)
2. >>> numbers

{1, 2, 4, 6}

 discard() vs remove()-

These two methods may appear the same to you, but there’s actually a difference. If you try deleting an item
that doesn’t exist in the set, discard() ignores it, but remove() raises a KeyError.

1. >>> numbers.discard(7)
2. >>> numbers

{1, 2, 4, 6}

1. >>> numbers.remove(7)

Traceback (most recent call last):

File “<pyshell#37>”, line 1, in <module>

numbers.remove(7)

KeyError: 7

3. pop()

Like on a dictionary, you can call the pop() method on a set. However, here, it does not take an argument.
Because a set doesn’t support indexing, there is absolutely no way to pass an index to the pop method. Hence, it
pops out an arbitrary item. Furthermore, it prints out the item that was popped.

1. >>> numbers.pop()

Let’s try popping anot/her element.


1. >>> numbers.pop()

Let’s try it on another set as well.

1. >>> {2,1,3}.pop()

4. clear()

Like the pop method(), the clear() method for a dictionary can be applied to a Python set as well. It empties the
set in Python.

1. >>> numbers.clear()
2. >>> numbers

set()

As you can see, it denoted an empty set as set(), not as {}.

d. Updating a Set
As we discussed, a Python set is mutable. But as we have seen earlier, we can’t use indices to reassign it.

1. >>> numbers={3,1,2,4,6,5}
2. >>> numbers[3]

Traceback (most recent call last):

File “<pyshell#56>”, line 1, in <module>

numbers[3]

TypeError: ‘set’ object does not support indexing

So, we use two methods for this purpose- add() and update(). We have seen the update() method on tuples, lists,
and strings.

1. add()

It takes as argument the item to be added to the set.

1. >>> numbers.add(3.5)
2. >>> numbers

{1, 2, 3, 4, 5, 6, 3.5}

If you add an existing item in the set, the set remains unaffected.
1. >>> numbers.add(4)
2. >>> numbers

{1, 2, 3, 4, 5, 6, 3.5}

2. update()

This method can add multiple items to the set at once, which it takes as arguments.

1. >>> numbers.update([7,8],{1,2,9})
2. >>> numbers

{1, 2, 3, 4, 5, 6, 3.5, 7, 8, 9}

As is visible, we could provide a list and a set as arguments to this. This is because this is different than creating
a set.

e. Functions on Sets
A function is something that you can apply to a Python set, and it performs operations on it and returns a value.
Let’s talk about some of the functions that a set supports.

We’ll take a new set for exemplary purposes.

1. >>> days={'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'}

1. len()

The len() function returns the length of a set. This is the number of elements in it.

1. >>> len(days)

2. max()

This function returns the item from the set with the highest value.

1. >>> max({3,1,2})

We can make such a comparison on strings as well.

1. >>> max(days)

‘Wednesday’

The Python function returned ‘Wednesday’ because W has the highest ASCII value among M, T, W, F, and S.
But we cannot compare values of different types.

1. >>> max({1,2,'three','Three'})

Traceback (most recent call last):

File “<pyshell#69>”, line 1, in <module>

max({1,2,’three’,’Three’})

TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

3. min()

Like the max() function, the min() function returns the item in the Python set with the lowest value.

1. >>> min(days)

‘Friday’

This is because F has the lowest ASCII value among M, T, W, F, and S.

4. sum()

The sum() functionin Python set returns the arithmetic sum of all the items in a set.

1. >>> sum({1,2,3})

However, you can’t apply it to a set that contains strings.

1. >>> sum(days)

Traceback (most recent call last):

File “<pyshell#72>”, line 1, in <module>

sum(days)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

5. any()

This function returns True even if one item in the set has a Boolean value of True.

1. >>> any({0})

False

1. >>> any({0,'0'})
True

It returns True because the string ‘0’ has a Boolean value of True.

6. all()

Unlike the any() function, all() returns True only if all items in the Python set have a Boolean value of True.
Otherwise, it returns False.

1. >>> all({0,'0'})

False

1. >>> all(days)

True

7. sorted()

The sorted() function returns a sorted python set to list. It is sorted in ascending order, but it doesn’t modify the
original set.

1. >>> numbers={1, 2, 3, 4, 5, 6, 3.5}


2. >>> sorted(numbers)

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

f. Methods on Sets
Unlike a function in Python set, a method may alter a set. It performs a sequence on operations on a set, and
must be called on it. So far, we have learned about the methods add(), clear(), discard(), pop(), remove(), and
update(). Now, we will see more methods from a more mathematical point of view.

1. union()

This method performs the union operation on two or more Python sets. What it does is it returns all the items
that are in any of those sets.

Union in Python Set

1. >>> set1,set2,set3={1,2,3},{3,4,5},{5,6,7}
2. >>> set1.union(set2,set3)

{1, 2, 3, 4, 5, 6, 7}

1. >>> set1

{1, 2, 3}

As you can see, it did not alter set1. A method does not always alter a set in Python.

2. intersection()

This method takes as argument sets, and returns the common items in all the sets.

Intersection Set in Python

1. >>> set2.intersection(set1)

{3}

Let’s intersect all three sets.

1. >>> set2.intersection(set1,set3)

set()

It returned an empty set because these three sets have nothing in common.

3. difference()

The difference() method returns the difference of two or more sets. It returns as a set.
Difference Python Set

1. >>> set1.difference(set2)

{1, 2}

This returns the items that are in set1, but not in set2.

1. >>> set1.difference(set2,set3)

{1, 2}

4. symmetric_difference()

This method returns all the items that are unique to each set.

Symmetric difference set in Python

1. >>> set1.symmetric_difference(set2)

{1, 2, 4, 5}

It returned 1 and 2 because they’re in set1, but not in set2. It also returned 4 and 5 because they’re in set2, but
not in set1. It did not return 3 because it exists in both the sets.

5. intersection_update()

As we discussed in intersection(), it does not update the set on which it is called. For this, we have the
intersection_update() method.

1. >>> set1.intersection_update(set2)
2. >>> set1

{3}

It stored 3 in set1, because only that was common in set1 and set2.

6. difference_update()

Like intersection-update(), this method updates the Python set with the difference.
1. >>> set1={1,2,3}
2. >>> set2={3,4,5}
3. >>> set1.difference_update(set2)
4. >>> set1

{1, 2}

7. symmetric_difference_update()

Like the two methods we discussed before this, it updates the set on which it is called with the symmetric
difference.

1. >>> set1={1,2,3}
2. >>> set2={3,4,5}
3. >>> set1.symmetric_difference_update(set2)
4. >>> set1

{1, 2, 4, 5}

8. copy()

The copy() method creates a shallow copy of the Python set.

1. >>> set4=set1.copy()
2. >>> set1,set4

({1, 2, 4, 5}, {1, 2, 4, 5})

9. isdisjoint()

This method returns True if two sets have a null intersection.

1. >>> {1,3,2}.isdisjoint({4,5,6})

True

However, it can take only one argument.

1. >>> {1,3,2}.isdisjoint({3,4,5},{6,7,8})

Traceback (most recent call last):

File “<pyshell#111>”, line 1, in <module>

{1,3,2}.isdisjoint({3,4,5},{6,7,8})

TypeError: isdisjoint() takes exactly one argument (2 given)

10. issubset()
This method returns true if the set in the argument contains this set.

1. >>> {1,2}.issubset({1,2,3})

True

1. >>> {1,2}.issubset({1,2})

True

{1,2} is a proper subset of {1,2,3} and an improper subset of {1,2}.

11. issuperset()

Like the issubset() method, this one returns True if the set contains the set in the argument.

1. >>> {1,3,4}.issuperset({1,2})

False

1. >>> {1,3,4}.issuperset({1})

True

g. Operations on Sets
Now, we will look at the operations that we can perform on sets.

1. Membership

We can apply the ‘in’ and ‘not in’ python operators on items for a set. This tells us whether they belong to the
set.

1. >>> 'p' in {'a','p','p','l','e'}

True

1. >>> 0 not in {'0','1'}

True

h. Iterating on a Set
Like we have seen with lists and tuples, we can also iterate on a set in a for-loop.

1. >>> for i in {1,3,2}:


2. print(i)

1
2

As you can see, even though we had the order as 1,3,2, it is printed in ascending order.

i. The frozenset
A frozen set is in-effect an immutable set. You cannot change its values. Also, a set can’t be used a key for a
dictionary, but a frozenset can.

1. >>> {{1,2}:3}

Traceback (most recent call last):

File “<pyshell#123>”, line 1, in <module>

{{1,2}:3}

TypeError: unhashable type: ‘set’

Now let’s try doing this with a frozenset.

1. >>> {frozenset(1,2):3}

Traceback (most recent call last):

File “<pyshell#124>”, line 1, in <module>

{frozenset(1,2):3}

TypeError: frozenset expected at most 1 arguments, got 2

As you can see, it takes only one argument. Now let’s see the correct syntax.

1. >>> {frozenset([1,2]):3}

{frozenset({1, 2}): 3}

3. Python Booleans
Finally, let’s discuss Booleans. A Boolean is another data type that Python has to offer.

a. Value of a Boolean
As we have seen earlier, a Boolean value may either be True or be False. Some methods like isalpha() or
issubset() return a Boolean value.
b. Declaring a Boolean
You can declare a Boolean just like you would declare an integer.

1. >>> days=True

As you can see here, we didn’t need to delimit the True value by quotes. If you do that, it is a string, not a
Boolean. Also note that what was once a set, we have reassigned a

Boolean to it.

1. >>> type('True')

<class ‘str’>

c. The bool() function


Like we have often seen earlier, the bool() function converts another value into the Boolean type.

1. >>> bool('Wisdom')

True

1. >>> bool([])

False

d. Boolean Values of Various Constructs


Different values have different equivalent Boolean values. In this example, we use the bool() Python set
function to find the values.

For example, 0 has a Boolean value of False.

1. >>> bool(0)

False

1 has a Boolean value of True, and so does 0.00000000001.

1. >>> bool(0.000000000001)

True

A string has a Boolean value of True, but an empty string has False.

1. >>> bool(' ')


True

1. >>> bool('')

False

In fact, any empty construct has a Boolean value of False, and a non-empty one has

True.

1. >>> bool(())

False

1. >>> bool((1,3,2))

True

e. Operations on Booleans
1. Arithmetic
You can apply some arithmetic operations to a set. It takes 0 for False, and 1 for True, and then applies the
operator to them.

 Addition

You can add two or more Booleans. Let’s see how that works.

1. >>> True+False #1+0

1. >>> True+True #1+1

1. >>> False+True #0+1

1. >>> False+False #0+0

 Subtraction and Multiplication

The same strategy is adopted for subtraction and multiplication.

1. >>> False-True

-1
 Division

Let’s try dividing Booleans.

1. >>> False/True

0.0

Remember that division results in a float.

1. >>> True/False

Traceback (most recent call last):

File “<pyshell#148>”, line 1, in <module>

True/False

ZeroDivisionError: division by zero

This was an exception that raised. We will learn more about exception in a later lesson.

 Modulus, Exponentiation, and Floor Division

The same rules apply for modulus, exponentiation, and floor division as well.

1. >>> False%True
2. >>> True**False

1. >>> False**False

1. >>> 0//1

Try your own combinations like the one below.

1. >>> (True+True)*False+True

2. Relational

The relational operators we’ve learnt so far are >, <, >=, <=, !=, and ==. All of these apply to Boolean values.
We will show you a few examples, you should try the rest of them.

1. >>> False>True

False
1. >>> False<=True

True

Again, this takes the value of False to be 0, and that of True to be 1.

3. Bitwise

Normally, the bitwise operators operate bit-by bit. For example, the following code ORs the bits of 2(010) and
5(101), and produces the result 7(111).

1. >>> 2|5

But the bitwise operators also apply to Booleans. Let’s see how.

 Bitwise &

It returns True only if both values are True.

1. >>> True&False

False

1. >>> True&True

True

Since Booleans are single-bit, it’s equivalent to applying these operations on 0 and/or

1.

 Bitwise |

It returns False only if both values are False.

1. >>> False|True

True

 Bitwise XOR (^)

This returns True only if one value is True and one is False.

1. >>> False^True

True

1. >>> False^False
False

1. >>> True^True

False

 Binary 1’s Complement

This calculates 1’s complement for True(1) and False(0).

1. >>> ~True

-2

1. >>> ~False

-1

 Left-shift(<<) and Right-shift(>>) Operators

As discussed earlier, these operators shift the value by specified number of bits left and right, respectively.

1. >>> False>>2
2. >>> True<<2

True is 1. When shifted two places two the left, it results in 100, which is binary for 4. Hence, it returns 4.

4. Identity

The identity operators ‘is’ and ‘is not’ apply to Booleans.

1. >>> False is False

True

1. >>> False is 0

False

5. Logical

Finally, even the logical operators apply on Booleans.

1. >>> False and True

False

This was all about the article on Python set and booleans.
4. Conclusion
In conclusion, we see that a Python Boolean value may be True or False. You may create it or use it when it’s
returned by a method. We learned how to create, access, update, and delete a set. We saw how it is mutable and
that is why we can’t use indexing to access, update, or delete it. So, we use certain Python set functions and
methods for the same. Lastly, we learned about various operations that we can apply on a set. And we learned
that some bitwise and logical operators mean the same thing on Booleans. See you tomorrow. Hope you liked
our article on python sets and booleans.
Refer Best python books for detailed study.

Python List Comprehension (Syntax & Examples) – Nested List


Comprehension Python
by DataFlair Team · October 21, 2018

1. Python List Comprehension


Previously, we discussed Lists in Python. In that tutorial, what is Python list comprehension and how to use
it? Along with this, we will learn syntax, list comprehension vs lambda expression in Python3. Along with this,
we will study conditionals and nested list comprehension in Python Programming Language.

So, let’s begin the Python list comprehension Tutorial.

Python List Comprehension with Syntax and Examples


2. What is Python List Comprehension?
To get Python list comprehensions back into memory, we’ll take a quick example.

Suppose you want to take the letters in the word ‘anxiety’, and want to put them in a list. Remember that a
python string is iterable.

Using a for loop, you would:

1. >>> mylist=[]
2. >>> for i in 'anxiety':
3. mylist.append(i)
4. >>> mylist

[‘a’, ‘n’, ‘x’, ‘i’, ‘e’, ‘t’, ‘y’]

But with a Python list comprehension, you can do this in one line:

1. >>> [i for i in 'anxiety']

[‘a’, ‘n’, ‘x’, ‘i’, ‘e’, ‘t’, ‘y’]

Now that we’ve got your interest, we’ll dig a little deeper.

3. The syntax of List Comprehension in Python


For a python list comprehension, we use the delimiters for a list- square brackets. Inside those, we use a for-
statement on an iterable (a list, here). We’ll take an example.

1. >>> [i*2 for i in {3,1,2}]

[2, 4, 6]

Here, we return twice of every value in the set {3,1,2} into a new list.

So we guess we can state the syntax for a Python list comprehension as follows:

[expression for item in list]

Note that not every loop has an equivalent list comprehension in Python.

4. Python List Comprehension vs Lambda Expression


Something about this syntax leaves us nostalgic. Remember when we learned about python lambda
expressions in an earlier lesson? A Python 3 lambda expression returns a certain expression’s value which it
calculates using values of the arguments it receives. Let’s create a list from a set using the list comprehension
syntax.
1. >>> myset={3,1,2}
2. >>> makelist=lambda i:list(i)
3. >>> mylist=makelist(myset)
4. >>> mylist

[1, 2, 3]

Here, we first took a set {3,1,2}. Like you’re aware by now, it rearranges itself as {1,2,3}. Then, we defined a
lambda function, and stored it in the variable ‘makelist’. This lambda function takes a value, converts it into a
list, and returns it. Finally, we called makelist on the set myset, and stored it in the variable mylist, which now
holds a list.

1. >>> type(mylist)

<class ‘list’>

To do this using the map function instead, we write the following code:

1. >>> list(map(lambda i:i,myset))

[1, 2, 3]

This code first takes a lambda expression: For each value i, it returns i and maps this on each value in the set
myset. Next, it converts this into a python list and prints it.

A list comprehension’s advantage over a lambda function is that it is more readable. Try reading both, and see
for yourself.

5. Conditionals in Python List Comprehension


So far, you know that we use a for-statement to declare our intentions. But did you know that it is possible to
add a condition to this? This will add only those items to the list that meet the condition (for which the
condition is True).

1. >>> [i for i in range(8) if i%2!=0]

[1, 3, 5, 7]

This code takes the values in range(8), i.e., 0 to 7, and adds the odd values to a list.

a. Nested Conditionals
With a Python list comprehension, it doesn’t have to be a single condition; you can nest conditions. Here’s an
example.

1. >>> [i for i in range(8) if i%2==0 if i%3==0]

[0, 6]
Let’s see how this works. For integers 0 to 7, it first filters out the elements that aren’t perfectly divisible by 2.
For the remaining elements, it keeps only those that are divisible by 3. Finally, it stores these elements in a list,
and prints it out.

Remember, this is a nested conditional, not an AND operation of two conditions.

b. if..else in List Comprehension in Python


You can also use an if-else in a list comprehension in Python. Since in a comprehension, the first thing we
specify is the value to put in a list, this is where we put our if-else.

1. >>> ["Even" if i%2==0 else "Odd" for i in range(8)]

[‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’]

This code stores in a list, for each integer from 0 to 7, whether it is even or odd.

Try using different conditions with this one, and tell us in the comments.

6. Nested List Comprehension Python


Finally, in this tutorial, we will end by discussing how to use a Python list comprehension for a nested for-loop.

Let’s take some code to print the tables of numbers 7 and 8. Using regular for-loops, we’d write the following
code:

1. >>> for i in range(7,9):


2. for j in range(1,11):
3. print(f"{i}*{j}={i*j}")

7*1=7

7*2=14

7*3=21

7*4=28

7*5=35

7*6=42

7*7=49

7*8=56

7*9=63

7*10=70
8*1=8

8*2=16

8*3=24

8*4=32

8*5=40

8*6=48

8*7=56

8*8=64

8*9=72

8*10=80

To do this using a python list comprehension, however, we use the following code:

1. >>> [[i*j for j in range(1,11)] for i in range(7,9)]

[[7, 14, 21, 28, 35, 42, 49, 56, 63, 70], [8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]

We used the for-loop for j as the inner comprehension, because it is the inner loop in the previous code.

So, this was all about Python List Comprehension Tutorial. Hope you like our explanation.

7. Conclusion
Now that you do better with python list comprehensions, we hope you’ll make good use of it for speed and
readability. However, it makes no sense to write a very long and complicated list comprehension. Also, you can
write a for-loop for every list comprehension in python, but not you can’t write list comprehensions for very
complex for-loops.

Python Date and Time – Syntax and examples


by DataFlair Team · May 27, 2019

1. Python Date and Python Time


Our last session was on data types and constructs of Python. Here, we will talk about Python date and time
module. Moreover, we will discuss what is Python Time and Python Date. Along with this, we will learn about
Python time tuple and Python time module.

A computer often has to perform the task of converting between different date and time formats. For this,
Python provides time and calendar modules.
So, let’s start Python Date and Time.

Python Date and Time – Syntax and examples

2. Introduction to Python Date and Time


To see how Python handles date and time, let’s first look at some important concepts of Python time. you can
refer to our article on Python Datetime Module with Quick Examples

a. Epoch
The Epoch is the point in time in Python from which time is measured. It is labelled 12:00AM, Jan 1, 1970. It is
the beginning of an era.

b. Ticks
In Python, a tick is a floating-point number in seconds, and it denotes a time interval in Python. Ticks are
calculated from the epoch.

c. Daylight Savings Time(DST)


In Agrarian culture, they set the clocks to be one hour faster. This way, they get up an hour before actual sunrise
and get an hour extra after work. In northern and southern regions, days are considerably longer in the summer.
However, in areas around the equator, the difference isn’t that much for DST to be efficient. Hence, not every
country implements it. Before proceeding on Python time tuple you may also want to read our article on python
tuples.

3. Python Time Tuple


We represent Python time in a way that’s easy for us to understand. However, Python stores it in tuples. These
python tuples are made of nine numbers.

Index Field Domain of Values

0 Year (4 digits) Ex.- 1995

1 Month 1 to 12

2 Day 1 to 31

3 Hour 0 to 23

4 Minute 0 to 59

5 Second 0 to 61 (60/61 are leap seconds)

6 Day of Week 0 to 6 (Monday to Sunday)

7 Day of Year 1 to 366 (Julian day)

8 DST -1,0,1

Leap seconds are added to make up to Earth’s slowing rotation. When DST is 0, it isn’t applied. When it’s 1, it
is applied. However, when it is -1, it is up to the library to determine that.

This tuple has an equivalent struct_time structure.

4. Getting the Number of Ticks


First, we’ll look at the Python ‘time’ module. To use functions from it, you must import it in your program.
See h

1. >>> import time

If you wanted to import a single method from ‘time’, you would do it this way.

1. >>> from time import time


2. >>> time()

1514472272.5830126

To get how many ticks have passed since the epoch, we call the Python time() method.
1. >>> import time
2. >>> time.time()

1514472318.761928

This is the number of ticks that have yet passed from the epoch. Clearly, this isn’t very human-readable. We can
read it, but with only this information, we can’t instantly perceive how much time has passed. Read our article
on Python Functions with Syntax and Examples

5. Getting the Local Time


To get the current time in Python in a more perceivable Python Date Time format, we use the localtime()
method instead. It returns the Python time according to the area you’re in.

1. >>> time.localtime()

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=28, tm_hour=20, tm_min=17, tm_sec=48, tm_wday=3,


tm_yday=362, tm_isdst=0)

This still isn’t completely instantly perceivable. Let’s try something else yet.

6. Getting the Formatted Time


When we use the asctime() method, we get something much more readable.

1. >>> time.asctime()

‘Thu Dec 28 20:22:55 2017’

It only takes us about a second to read this and understand.

You can also provide a tuple or a struct_time structure as an argument.

1. >>> time.asctime(time.localtime())

‘Thu Dec 28 20:24:52 2017’

But the plain time() method doesn’t work with it.

1. >>> time.asctime(time.time())

Traceback (most recent call last):

File “<pyshell#31>”, line 1, in <module>

time.asctime(time.time())

TypeError: Tuple or struct_time argument required


7. Python Calendar
‘calendar’ is yet another module available in Python. It lets you play with monthly and yearly calendars using a
variety of functions.
To get the calendar for Dec 2017, we use the month() method.

1. >>> calendar.month(2017,12)

‘ December 2017\nMo Tu We Th Fr Sa Su\n 1 2 3\n 4 5 6 7 8 9 10\n11 12 13 14 15 16 17\n18 19


20 21 22 23 24\n25 26 27 28 29 30 31\n’

I’m guessing we should use the print function here to make this readable.

1. >>> print(calendar.month(2017,12))

December 2017

Mo Tu We Th Fr Sa Su

12 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

8. Methods in the Python Time Module


Do refer Python Method – Classes, Objects and Functions in Python before proceeding.

The three methods we discussed aren’t the only ones applicable on the Python time module. One by one, we’ll
see them all.

a. time()
We already saw this in section 4. Let’s get the number of ticks once again.

1. >>> time.time()

1514474057.2921965

This method does not take any parameters. And if you set the clock back before making another call to this
method, it may return a lower value.
b. localtime()
This is the second method we discussed in the text. It returns the current time according to your geographical
location, in a more readable format.

1. >>> time.localtime()

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=28, tm_hour=21, tm_min=2, tm_sec=13, tm_wday=3,


tm_yday=362, tm_isdst=0)

This returns a time tuple.

However, localtime() may take an argument in seconds. Then, it returns a time tuple for that number of seconds
from the epoch. First, let’s try providing 0 seconds as an argument.

1. >>> time.localtime(0)

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3,


tm_yday=1, tm_isdst=0)

We just discussed that epoch is 12:00, Jan 1, 1970. Why then did it return 5:30? Well that’s because we’re here
in India. Our time zone is UTC+05:30 (UTC stands for Coordinated Universal Time).

Now, let’s try it for 777 seconds.

1. >>> time.localtime(777)

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=42, tm_sec=57, tm_wday=3,


tm_yday=1, tm_isdst=0)

c. asctime()
This method gives us the current time in a very readable format. It returns a string of 24 characters.

1. >>> time.asctime()

‘Thu Dec 28 21:17:39 2017’

It may also take a Python time tuple as an argument. Since the localtime() method returns a time tuple, let’s use
it as the argument.

1. >>> time.asctime(time.localtime())

‘Thu Dec 28 21:21:36 2017’


d. clock()
The clock method does not take any arguments. On Windows, it returns the number of seconds elapsed since
the first call made to it. It returns the value as a floating number. Let’s try it a few times.

1. >>> time.clock()

0.0

1. >>> time.clock()

5.456429169367129

1. >>> time.clock()

9.670899252239298

1. >>> time.clock()

16.96956734536135

However, it does not return 0.0 on every first call.

Now, if you restart the shell, the next time you call it, it will assume it to be the first call.

e. gmtime()
Remember the problem we faced in localtime(0), about UTC? Well, gmtime() converts the current time into
UTC before returning it.

1. >>> time.gmtime()

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=28, tm_hour=16, tm_min=11, tm_sec=3, tm_wday=3,


tm_yday=362, tm_isdst=0)

It may also take seconds as an argument. Let’s try gmtime(0).

1. >>> time.gmtime(0)

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3,


tm_yday=1, tm_isdst=0)

Like localtime(), it returns a time tuple.

f. ctime()
This method is equivalent to asctime().
g. mktime()
The mktime() method takes a time tuple as an argument, and returns the equivalent amount of ticks/seconds. It
returns in floating point.

1. >>> time.mktime(time.localtime())

1514478943.0

You can’t however call it without an argument.

1. >>> time.mktime()

Traceback (most recent call last):

File “<pyshell#72>”, line 1, in <module>

time.mktime()

TypeError: mktime() takes exactly one argument (0 given)

h. sleep()
When called, sleep() holds the execution for a given amount of seconds. This number is passed to it as an
argument.

1. >>> time.asctime(); time.sleep(2); time.asctime()

‘Thu Dec 28 22:11:55 2017’

‘Thu Dec 28 22:11:57 2017’

i. strftime()
Now what if you wanted to display the Python date and time in the format you wish to? The strftime() method
takes a format and optionally, a Python time tuple, and returns the date in the format you specify. The syntax is
as follows.
Time.strftime(format[, t])
You can apply the following directives in the format, as per your need.

 %a – abbreviated day of week


 %A – full day of week
 %b – abbreviated name of month
 %B – full name of month
 %c – preferred date and time representation
 %C – century number (the year divided by 100; range 00 to 99)
 %d – day of month (1 to 31)
 %D – the same as %m/%d/%y
 %e – day of month (1 to 31)
 %g – like %G, but without the century
 %G – 4-digit year corresponding to the ISO week number (ref. %V).
 %h – same as %b
 %H – hour; using a 24-hr clock (0 to 23)
 %I – hour; using a 12-hr clock (1 to 12)
 %j – day of year (1 to 366)
 %m – month (1 to 12)
 %M – minute
 %n – newline character
 %p – AM/PM according to given time value
 %r – time in AM/PM representation
 %R – time in 24-hr representation
 %S – second
 %t – tab
 %T – current time; equal to %H:%M:%S
 %u – day of week as a number (1 to 7); Monday=1
 %U – week of year, beginning with the first Sunday as the first day of the first week
 %V – The ISO 8601 week of year (1 to 53); week 1 is the first with at least 4 days in the current year,
and with Monday as the first day of the week
 %W – week of year; beginning with the first Monday as the first day of the first week
 %w – day of week as a decimal; Sunday=0
 %x – the preferred date representation without the time
 %X – the preferred time representation without the date
 %y – year without century (00 to 99)
 %Y – year including century
 %Z or %z – time zone/name/abbreviation
 %% – a % character

Let’s try printing the shortened name of the month in the local time in Python.

1. >>> time.strftime("%b",time.localtime())

‘Dec’

Let’s try another one.

1. >>> time.strftime("%B %uth, '%y",time.localtime())

“December 4th, ’17”

As you can see, it is possible to add letters in this python string normally.

j. Python strptime()
We just saw that the strftime() method returns a string from a format (and optionally, a tuple). The strptime()
takes such a string and parses it. Finally, it returns a time tuple. It takes two arguments; the following is the
syntax.
Time.strptime(string[,format])

1. >>> time.strptime("31 Dec 1995","%d %b %Y")

time.struct_time(tm_year=1995, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6,


tm_yday=365, tm_isdst=-1)

9. Attributes of the time Module


a. altzone
This returns the local DST timezone’s offset. While it returns a positive value for locations west of UTC, it
returns a negative value for those east of UTC.

1. >>> time.altzone

-23400

From where we write this article, we can tell that we live east of UTC. Note that this attribute’s value does not
change for a location.

b. timezone
The timezone attribute is the same as altzone, but it does not consider the DST.

1. >>> time.timezone

-19800

c. tzname
Finally, you can use this attribute to print the name of the local time zone with and without DST.

1. >>> time.tzname

(‘India Standard Time’, ‘India Daylight Time’)

10. Methods in the calendar Module


Like the time module, calendar provides us with some methods too. Let’s see what we can do with it.

a. isleap()
isleap() takes a 4-digit year as an argument, and returns True if it is a leap year. Otherwise, it returns False.
1. >>> calendar.isleap(2019)

False

1. >>> calendar.isleap(2020)

True

b. leapdays(y1,y2)
Talking of leap years, this one returns the total number of leap days from year y1 to year y2.

1. >>> calendar.leapdays(1990,2020)

c. firstweekday()
This one returns which day is currently set as the first day of the week. By default, this is 0.

1. >>> calendar.firstweekday()

d. setfirstweekday()
Complementary to what we learnt in the above point, setfirstweekday() allows us to set the first day of the week
to what we want. 0 is for Monday, 6 is for Sunday.

1. >>> calendar.setfirstweekday(4)
2. >>> calendar.firstweekday()

Now, let’s print the calendar for this January.

1. >>> print(calendar.month(2018,1))

January 2018

Fr Sa Su Mo Tu We Th

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

As you can see, the calendar now begins at Friday. However, this change of information is invalidated once the
shell is restarted. That is, it changes back to 0.

e. month()
As we have seen so far in this document, month() returns a calendar for a certain month of a certain year. These
are two of the arguments. There are two other optional arguments- one for width of characters in name of day,
and one for the number of lines for each week.

1. >>> print(calendar.month(2017,12,3,2))

December 2017

Mon Tue Wed Thu Fri Sat Sun

12 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

f. calendar()
Python Date and Time – Calendar() in Python

The calendar() method returns the calendar for an entire year. It may also take three optional parameters. These
are- width of characters for day of week, lines for week, spaces between columns.

1. >>> print(calendar.calendar(2018))

g. monthcalendar()
This method returns a list of lists of day numbers as lists. Also, it takes two arguments- year and month.
1. >>> print(calendar.monthcalendar(2017,12))

[[0, 0, 0, 0, 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]]

h. monthrange()
Now this method takes two arguments- year and month. Then, it returns the day of week for the first day of the
month, and the number of days in the month.

1. >>> print(calendar.monthrange(2017,12))

(4, 31)

Here, 4 is for Friday.

i. prcal()
If you want to skip the print statement to the calendar method, use prcal() instead. Let’s take the following code.

1. >>>calendar.prcal(2018)

Its output is the same as of the code in point 6 in this section.

j. prmonth()
It’s the same thing here as well. It prints out the calendar for the specified month.

k. weekday()
This method takes as arguments the year, the month, and the day. Then, it returns which day of the week it is.

1. >>> calendar.weekday(2018,11,30)

l. timegm()
Finally, this method takes a time tuple and returns the equivalent in ticks since the epoch, in floating point.

1. >>> calendar.timegm(time.localtime())

1514504864

1. >>> calendar.timegm(time.localtime())
1514504872

Read: The Tremendous Python Career Opportunities

So, this was all about Python Date and Time. Hope you like our explanation.

11. Conclusion
We learned a lot today, didn’t we? First, we learned about some concepts of Python date and time like the
epoch, ticks, and DST. Then, we used the Python time and calendar modules, and their methods and attributes.
Why don’t you try and use this knowledge to create a console-based game?

Still, Confuse in Python Date and Time? Share your query/ feedback with us in the comments section

Python Calendar Module | Calendar Class & HTML Calendar


by DataFlair Team · November 19, 2018

1. Objective – Python Calendar Module


We took a brief look at the Python Calendar module when we talked about Python date and time. Let’s take a
deeper look at the Calendar Module in Python now. Moreover, we will talk about three classes in the calendar
module- Calendar, TextCalendar, and HTMLCalendar. Also, we will take a look at their available instance
methods. At last, we will learn about the available functions in the Python Calendar Modules.
So, let’s start the Python Calendar Module tutorial.

Python Calendar Module


2. Introduction To Python Calendar
Python Calendar Module lets you print calendars and perform functions related to those. These calendars begin
at Monday (day=0) and end at Sunday (day=6). In this Python calendar tutorial, we will notice the Gregorian
calendar, set indefinitely in both directions, as the base calendar. The ISO 8601 standard decides how to
interpret zero and negative years. We call year 0 ‘1 BC’, year -1 ‘2 BC’, and so on.
This module holds the Calendar class that lends us various functions to manipulate date and time. Other than
that, we have the TextCalendar and HTMLCalendar classes to produce pre-formatted output.
We can import this Python Calendar Module as-

1. >>> import calendar as cd

You must read about Python Multiprocessing Module

3. Python Calendar Class


The first class we discuss here is the Calendar. The constructor for this has the following syntax:

1. class calendar.Calendar(firstweekday=0)

This creates a Calendar object with whichever day of the week we want it to begin at. By default, it starts at
Monday with a parameter value of 0. Like we said earlier, Sunday is 6. With the methods of this class, we
prepare a calendar for formatting.
An instance of Calendar will have the following methods-

a. iterweekdays() – Days of week


This returns an iterator for the weekday numbers for an entire week. The first number this iterator returns is the
number we set as the firstweekday.

1. >>> import calendar as cd


2. >>> c=cd.Calendar(firstweekday=1)
3. >>> for i in c.iterweekdays():
4. print(i)

1
2
3
4
5
6
0
b. itermonthdates(year, month) – Dates of month
This returns an iterator for all days of the month and all days before or after it so we can get a complete week.
Let’s set the first weekday to be Monday.

1. >>> c=cd.Calendar(firstweekday=0)
2. >>> for i in c.itermonthdates(2018,12):
3. print(i)

2018-11-26
2018-11-27
2018-11-28
2018-11-29
2018-11-30
2018-12-01
2018-12-02
2018-12-03
2018-12-04
2018-12-05
2018-12-06
2018-12-07
2018-12-08
2018-12-09
2018-12-10
2018-12-11
2018-12-12
2018-12-13
2018-12-14
2018-12-15
2018-12-16
2018-12-17
2018-12-18
2018-12-19
2018-12-20
2018-12-21
2018-12-22
2018-12-23
2018-12-24
2018-12-25
2018-12-26
2018-12-27
2018-12-28
2018-12-29
2018-12-30
2018-12-31
2019-01-01
2019-01-02
2019-01-03
2019-01-04
2019-01-05
2019-01-06
Let’s revise Python Ternary Operator

c.itermonthdays(year,month) – Days of month


This gives us an iterator for the month as ‘day of month’. The days outside the month we specify return 0.

1. >>> for i in c.itermonthdays(2018,12):


2. print(i)

0
0
0
0
0
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
0
0
0
0
0
0
Notice how the days from months November and January fade into a 0?
Have a look at Python Subprocess Module

d.itermonthdays2(year,month) – Day of month, day of


week
This is the same as itermonthdays(), but returns tuples of day of month and day of week.

1. >>> for i in c.itermonthdays2(2018,12):


2. print(i)

(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
(4, 1)
(5, 2)
(6, 3)
(7, 4)
(8, 5)
(9, 6)
(10, 0)
(11, 1)
(12, 2)
(13, 3)
(14, 4)
(15, 5)
(16, 6)
(17, 0)
(18, 1)
(19, 2)
(20, 3)
(21, 4)
(22, 5)
(23, 6)
(24, 0)
(25, 1)
(26, 2)
(27, 3)
(28, 4)
(29, 5)
(30, 6)
(31, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(0, 6)
Let’s discuss Python Collections Module

e. itermonthdays3(year,month) – Year, month, day of


month
This is like the previous two, but gives us a tuple of year, the month, and the day of the month. This is new in
Python 3.7.

1. >>> for i in c.itermonthdays3(2018,12):


2. print(i)

(2018, 11, 26)


(2018, 11, 27)
(2018, 11, 28)
(2018, 11, 29)
(2018, 11, 30)
(2018, 12, 1)
(2018, 12, 2)
(2018, 12, 3)
(2018, 12, 4)
(2018, 12, 5)
(2018, 12, 6)
(2018, 12, 7)
(2018, 12, 8)
(2018, 12, 9)
(2018, 12, 10)
(2018, 12, 11)
(2018, 12, 12)
(2018, 12, 13)
(2018, 12, 14)
(2018, 12, 15)
(2018, 12, 16)
(2018, 12, 17)
(2018, 12, 18)
(2018, 12, 19)
(2018, 12, 20)
(2018, 12, 21)
(2018, 12, 22)
(2018, 12, 23)
(2018, 12, 24)
(2018, 12, 25)
(2018, 12, 26)
(2018, 12, 27)
(2018, 12, 28)
(2018, 12, 29)
(2018, 12, 30)
(2018, 12, 31)
(2019, 1, 1)
(2019, 1, 2)
(2019, 1, 3)
(2019, 1, 4)
(2019, 1, 5)
(2019, 1, 6)
Let’s revise Python Modules vs Packages

f. itermonthdays4(year, month) – Year, month, day of


month, day of week
Like so far, this returns a tuple with year, month, day of month, and day of week. This is since Python 3.7.

1. >>> for i in c.itermonthdays4(2018,12):


2. print(i)

(2018, 11, 26, 0)


(2018, 11, 27, 1)
(2018, 11, 28, 2)
(2018, 11, 29, 3)
(2018, 11, 30, 4)
(2018, 12, 1, 5)
(2018, 12, 2, 6)
(2018, 12, 3, 0)
(2018, 12, 4, 1)
(2018, 12, 5, 2)
(2018, 12, 6, 3)
(2018, 12, 7, 4)
(2018, 12, 8, 5)
(2018, 12, 9, 6)
(2018, 12, 10, 0)
(2018, 12, 11, 1)
(2018, 12, 12, 2)
(2018, 12, 13, 3)
(2018, 12, 14, 4)
(2018, 12, 15, 5)
(2018, 12, 16, 6)
(2018, 12, 17, 0)
(2018, 12, 18, 1)
(2018, 12, 19, 2)
(2018, 12, 20, 3)
(2018, 12, 21, 4)
(2018, 12, 22, 5)
(2018, 12, 23, 6)
(2018, 12, 24, 0)
(2018, 12, 25, 1)
(2018, 12, 26, 2)
(2018, 12, 27, 3)
(2018, 12, 28, 4)
(2018, 12, 29, 5)
(2018, 12, 30, 6)
(2018, 12, 31, 0)
(2019, 1, 1, 1)
(2019, 1, 2, 2)
(2019, 1, 3, 3)
(2019, 1, 4, 4)
(2019, 1, 5, 5)
(2019, 1, 6, 6)
Do you know about CGI Programming in Python

g.monthdatescalendar(year,month) – List of weeks in month


This gives us a list of full weeks in the month we want. Each week is a list of seven datetime.date objects.

1. >>> for i in c.monthdatescalendar(2018,12):


2. print(i)

[datetime.date(2018, 11, 26), datetime.date(2018, 11, 27), datetime.date(2018, 11, 28), datetime.date(2018,
11, 29), datetime.date(2018, 11, 30), datetime.date(2018, 12, 1), datetime.date(2018, 12, 2)]
[datetime.date(2018, 12, 3), datetime.date(2018, 12, 4), datetime.date(2018, 12, 5), datetime.date(2018, 12,
6), datetime.date(2018, 12, 7), datetime.date(2018, 12, 8), datetime.date(2018, 12, 9)]
[datetime.date(2018, 12, 10), datetime.date(2018, 12, 11), datetime.date(2018, 12, 12), datetime.date(2018,
12, 13), datetime.date(2018, 12, 14), datetime.date(2018, 12, 15), datetime.date(2018, 12, 16)]
[datetime.date(2018, 12, 17), datetime.date(2018, 12, 18), datetime.date(2018, 12, 19), datetime.date(2018,
12, 20), datetime.date(2018, 12, 21), datetime.date(2018, 12, 22), datetime.date(2018, 12, 23)]
[datetime.date(2018, 12, 24), datetime.date(2018, 12, 25), datetime.date(2018, 12, 26), datetime.date(2018,
12, 27), datetime.date(2018, 12, 28), datetime.date(2018, 12, 29), datetime.date(2018, 12, 30)]
[datetime.date(2018, 12, 31), datetime.date(2019, 1, 1), datetime.date(2019, 1, 2), datetime.date(2019, 1, 3),
datetime.date(2019, 1, 4), datetime.date(2019, 1, 5), datetime.date(2019, 1, 6)]
To confirm this is correct, check the calendar for December 2018 with Monday as the first day:
The weeks begin at 26, 3, 19, 17, 24, and 31.
Let’s take a tour to Python ZipFile

h. monthdays2calendar(year,month) – List of day of


month and day of week in month
This gives us a list of weeks with each week as seven tuples of day of month and day of the week.

1. >>> for i in c.monthdays2calendar(2018,12):


2. print(i)

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)]
[(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)]
[(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)]
[(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)]
[(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]
[(31, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
Every week has week numbers 0 to 6 as the second value in the tuples and the first value is the day of month- 0
to 31 here.
i. monthdayscalendar(year,month) – List of day of month
This gives us a list of weeks with each week as a list of day of month.

1. >>> for i in c.monthdayscalendar(2018,12):


2. print(i)

[0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]

j. yeardatescalendar(year,width=3) – List of row months


This gives us a list of month rows. The width parameter is to specify up to how many months we want in a
month row, the default for which is 3. The days are datetime.date objects.

1. >>> for i in c.yeardatescalendar(2018,2):


2. print(i)

Have a look at Python Database Access

k.yeardays2calendar(year,width=3) – List of day of


month and day of week
This gives us a list of list of weeks as tuples of day of month and day of week.

1. >>> for i in c.yeardays2calendar(2018,2):


2. print(i)

l. yeardayscalendar(year,width=3) – List of day of month


This gives us a list of weeks as day of month.

1. >>> for i in c.yeardayscalendar(2018,2):


2. print(i)

[[[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, 0, 0, 0, 0]], [[0, 0, 0, 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, 0, 0, 0, 0]]]
[[[0, 0, 0, 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, 0]], [[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]]
[[[0, 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, 0, 0, 0]], [[0, 0, 0, 0, 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, 0]]]
[[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]], [[0, 0, 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, 0, 0]]]
[[[0, 0, 0, 0, 0, 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]], [[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, 0, 0, 0, 0]]]
[[[0, 0, 0, 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, 0, 0]], [[0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]]

4. Python TextCalendar Class


This class lets us generate plain text calendars. The constructor for this has the following syntax-

1. class calendar.TextCalendar(firstweekday=0)

Take a look at its available methods-

a. formatmonth(theyear,themonth,[w=0,l=0]) – Multiline
string of month
This gives us a month’s calendar as a multiline string. w is the width of centered date columns, l the number of
lines for each week- both are optional parameters. We need the value of the firstweekday for this to work.

1. >>> t=cd.TextCalendar(0)
2. >>> t.formatmonth(2018,12)

‘ December 2018\nMo Tu We Th Fr Sa Su\n 1 2\n 3 4 5 6 7 8 9\n10 11 12 13 14 15 16\n17 18 19


20 21 22 23\n24 25 26 27 28 29 30\n31\n’

1. >>> print(t.formatmonth(2018,12))

Let’s learn about Python Django

b. prmonth(theyear,themonth,[w=0,l=0]) – Print month


calendar
What we printed last, we can also do with prmonth() instead.

1. >>> t.prmonth(2018,12)
c.formatyear(theyear,[w=2,l=1,c=6,m=3]) – Multiline string
calendar
This returns an m-column calendar for the entire year as a multiline string. Optional parameters- w for date
column width, l for lines per week, c for number of spaces between month columns. For this to work, we need
the firstweekday. The earliest year you can generate a calendar for depends on your platform.

1. >>> print(t.formatyear(2018,5)) #sets w=5 and m is 3 by default


2. >>> print(t.formatyear(2018,m=5))

You must know Python Stemming

d.pryear(theyear,[w=2,l=1,c=6,m=3]) – Print year calendar


This prints the same thing as above.

1. >>> t.pryear(2018,m=5)

5. Python HTMLCalendar
Finally, let’s talk about the class that lets us generate HTML Calendars in Python. The constructor for this has
the following syntax:

1. class calendar.HTMLCalendar(firstweekday=0)

An HTML Python Calendar instance has the following methods:

a. formatmonth(theyear,themonth,withyear=True) – HTML
table for month calendar
This gives us the calendar for a month as an HTML table

1. >>> h=cd.HTMLCalendar()
2. >>> print(h.formatmonth(2018,12))

<table border=”0″ cellpadding=”0″ cellspacing=”0″ class=”month”>


<tr><th colspan=”7″ class=”month”>December 2018</th></tr>
<tr><th class=”mon”>Mon</th><th class=”tue”>Tue</th><th class=”wed”>Wed</th><th
class=”thu”>Thu</th><th class=”fri”>Fri</th><th class=”sat”>Sat</th><th class=”sun”>Sun</th></tr>
<tr><td class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td
class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td
class=”sat”>1</td><td class=”sun”>2</td></tr>
<tr><td class=”mon”>3</td><td class=”tue”>4</td><td class=”wed”>5</td><td class=”thu”>6</td><td
class=”fri”>7</td><td class=”sat”>8</td><td class=”sun”>9</td></tr>
<tr><td class=”mon”>10</td><td class=”tue”>11</td><td class=”wed”>12</td><td
class=”thu”>13</td><td class=”fri”>14</td><td class=”sat”>15</td><td class=”sun”>16</td></tr>
<tr><td class=”mon”>17</td><td class=”tue”>18</td><td class=”wed”>19</td><td
class=”thu”>20</td><td class=”fri”>21</td><td class=”sat”>22</td><td class=”sun”>23</td></tr>
<tr><td class=”mon”>24</td><td class=”tue”>25</td><td class=”wed”>26</td><td
class=”thu”>27</td><td class=”fri”>28</td><td class=”sat”>29</td><td class=”sun”>30</td></tr>
<tr><td class=”mon”>31</td><td class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td
class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td class=”noday”>&nbsp;</td><td
class=”noday”>&nbsp;</td></tr>
</table>
Let’s revise Python Matplotlib

b. formatyear(theyear,width=3) – HTML table for


year calendar
This gives us an HTML table for an entire year’s calendar. The width gives us the number of months in a row
and defaults to 3.

1. >>> print(h.formatyear(2018,5))

c.
formatyearpage(theyear,width=3,css=’calendar.css’,encoding
=None)
This gives us an entire year’s calendar as an HTML page with the width defaulting to 3. If we use no cascading
style sheet, we can put None there.

1. >>> print(h.formatyearpage(2018,css=None))

6. Functions in the Python Calendar Module


So far, we have seen two subclasses of the Python Calendar Class- TextCalendar and HTMLCalendar. Now,
let’s take a look at all the functions we have with the Python Calendar Module.
Have a look at Python Data Cleansing

a. calendar.setfirstweekday(weekday) – Set a day to begin


the week at
This lets us set which day of the week to begin a week at. 0 is Monday and 6 is Sunday.

1. >>> t.setfirstweekday(cd.SUNDAY)
2. >>> print(t.formatyear(2018,5))
b. calendar.firstweekday – First day of week
This gives us the first day of the week.

1. >>> t.firstweekday

c.calendar.isleap(year) – Whether a year is leap


This tells us whether a year is leap.

1. >>> cd.isleap(2018)

False

d.calendar.leapdays(y1,y2) – Number of leap years in


range
From year y1 to year y2, this gives us the number of leap years.

1. >>> cd.leapdays(1995,2018)

e. calendar.weekday(year,month,day) – Day of week


This returns the day of week where 0 is Monday.

1. >>> cd.weekday(2018,12,31)

f. calendar.weekheader(n) – Abbreviated weekday names


This gives us a header holding abbreviated weekday names. w is the width in characters for one weekday.

1. >>> cd.weekheader(1)

‘S M T W T F S’

1. >>> cd.weekheader(2)

‘Su Mo Tu We Th Fr Sa’

1. >>> cd.weekheader(3)
‘Sun Mon Tue Wed Thu Fri Sat’
Let’s revise Python Interpreter

g.calendar.monthrange(year,month) – First day of month,


number of days
This gives us the first day of the month and the number of days

1. >>> cd.monthrange(2018,12)

(5, 31)

h. calendar.monthcalendar(year,month) – Matrix of month


calendar
This gives us a matrix for a month’s calendar.

1. >>> for i in cd.monthcalendar(2018,12):


2. print(i)

[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]

i. calendar.prmonth(theyear,themonth,w=0,l=0) – Print
multiline month
This prints the month that calendar.month gives us.

1. >>> cd.prmonth(2018,12)

Learn more about Aggregation and Data Wrangling

j. calendar.month(theyear,themonth,w=0,l=0) – Multiline
month string
This gives us a month’s calendar in a multiline string. For this, it uses the formatmonth() method from
TextCalendar.

1. >>> cd.month(2018,12)
‘ December 2018\nSu Mo Tu We Th Fr Sa\n 1\n 2 3 4 5 6 7 8\n 9 10 11 12 13 14 15\n16 17 18
19 20 21 22\n23 24 25 26 27 28 29\n30 31\n’

k.calendar.prcal(year,w=0,l=0,c=6,m=3) – Print year calendar


This prints the calendar of an entire year- one that calendar.calendar returns.

1. >>> cd.prcal(2018)

l. calendar.calendar(year,w=2,l=1,c=6,m=3) – Multiline string


year calendar
This gives us a 3-column year calendar as a multiline string. For this, it uses the formatyear() method from
TextCalendar.

1. >>> cd.calendar(2018)

‘ 2018\n\n January February March\nSu Mo Tu We Th Fr Sa Su Mo Tu We Th


Fr Sa Su Mo Tu We Th Fr Sa\n 1 2 3 4 5 6 1 2 3 1 2 3\n 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9
10\n14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17\n21 22 23 24 25 26 27 18 19 20 21 22
23 24 18 19 20 21 22 23 24\n28 29 30 31 25 26 27 28 25 26 27 28 29 30 31\n\n April May June\nSu
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa\n 1 2 3 4 5 6 7 1 2 3 4 5 1 2\n 8 9
10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9\n15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14
15 16\n22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23\n29 30 27 28 29 30 31 24 25 26 27
28 29 30\n\n July August September\nSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We
Th Fr Sa\n 1 2 3 4 5 6 7 1 2 3 4 1\n 8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8\n15 16 17 18 19 20
21 12 13 14 15 16 17 18 9 10 11 12 13 14 15\n22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21
22\n29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29\n 30\n\n October November
December\nSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa\n 1 2 3 4 5 6 1 2 3
1\n 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8\n14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14
15\n21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22\n28 29 30 31 25 26 27 28 29 30 23 24 25
26 27 28 29\n 30 31\n’
You must read Python NumPy tutorial

m.calendar.timegm(tuple) – Timestamp value from time


tuple
This take a time tuple and returns a timestamp value with the epoch being 1970 and with a POSIX encoding.
This is the inverse of time.gmtime().

1. >>> import time


2. >>> cd.timegm(time.gmtime(30000))

30000
So, this was all in Python calendar Module Tutorial. Hope you like our explanation.
7. Conclusion – Python Calendar Module
Hence, in this Python Calendar tutorial, we saw Calendar Module in Python. Moreover, we discussed Python
Calendar example to understand the module easily. Also, we look at TextCalendar and HTML Calendar in
Python. At last, we learned the functions in Python calendar Module. So, how do you prefer your calendars-
beginning on a Monday or on a Sunday? Tell us in the comments below.

Learn Python Recursion Function – Example, Pros and Cons


by DataFlair Team · November 15, 2018

1. Python Recursion Function


A few lessons back, we introduced you toFunctions in Python, in which we studied Python Recursion
Function. Here, in this Python Recursion tutorial, we discuss working an example of recursion function in
Python. Along with this, we will learn pros and cons of Python Recursion Function.

So, let’s start the Python Recursion Function Tutorial.

Learn Python Recursion Function – Example, Pros and Cons

2. What is Recursion Function in Python?


According to the Oxford English Dictionary, recursion is the repeated application of a recursive procedure or
definition.
Do you see the recursion in this definition itself? They used the word ‘recursive’ to define ‘recursion’. We sense
an Easter egg here. Anyway, so as we talk about recursion, we’ll take the coolest example first. Take a look at
the logo for PyPy, an implementation of Python with a Just-In-Time Compiler.

Python Recursion – pypy

The snake biting its own tail, feeding itself, is an example of recursion we’d like to give to you.

To take a more general example, when our anxiety creates more anxiety for us, it is recursion.

In programming, recursion is when a function calls itself. We’ll see this in detail in the following sections of
recursion in Python Example.

3. Example of Python Recursive Function


We know that in Python, a function can call another. But when it calls itself, there must be a base condition,
along with a decrement statement, to avoid an infinite loop. For this, we’ll take a python recursive function
example to calculate a number’s Python recursion factorial, since it’s the Hello World for recursion. The
factorial of a number n is n*(n-1)*(n-2)*..*2*1. So, 5! = 5*4*3*2*1. Let us see how to write a recursive
function.

First, let’s do it without Python recursion function.

1. >>> def factorial(n):


2. f=1
3. while n>0:
4. f*=n
5. n-=1
6. print(f)
7. >>> factorial(4)

24

1. >>> factorial(5)

120

Now, let’s implement this with recursion. We mean to make factorial() call factorial().

1. >>> def factorial(n):


2. if n==1:
3. return 1
4. return n*factorial(n-1)
5. >>> factorial(5)
120

1. >>> factorial(4)

24

See how easy that was?

a. How Python Recursion Function Works?


To be clearer, we’ll explain how recursion function works in Python.

Our factorial() function takes n as argument. The base case that it defines to get out of the Python recursion is
when n is equal to 1. In that case, the function will return 1.

Otherwise, it will return n multiplied by factorial(n-1). This is a recursive call to itself. So this is how it goes:

factorial(5)
=5*factorial(4)
=5*4*factorial(3)
=5*4*3*factorial(2)
=5*4*3*2*factorial(1)
=5*4*3*2*1
=5*4*3*2
=5*4*6
=5*24
=120

Hence, we get 120 as the output for factorial(5).

b. RecursionError in Python
So far, the code seems to work fine. But now, let’s try passing -2 as an argument to this.

1. >>> factorial(-2)

Traceback (most recent call last):

File “<pyshell#262>”, line 1, in <module>

factorial(-2)

File “<pyshell#259>”, line 4, in factorial

return n*factorial(n-1)

File “<pyshell#259>”, line 4, in factorial

return n*factorial(n-1)
File “<pyshell#259>”, line 4, in factorial

return n*factorial(n-1)

[The Previous line repeated 989 more times]

File “<pyshell#259>”, line 2, in factorial

if n==1:

RecursionError: maximum recursion depth exceeded in comparison

Add this one to the list of exceptions in our tutorial on Python Errors and Exceptions.Also, read our article on

4. Python Recursion Function – Pros & Cons


a. Python Recursion Function Advantages
With Python recursion, there are some benefits we observe:

1. A recursive code has a cleaner-looking code.


2. Recursion makes it easier to code, as it breaks a task into smaller ones.
3. It is easier to generate a sequence using recursion than by using nested iteration.

b. Python Recursion Function Disadvantages


The flip side of the coin is easy to quote:

1. Although it makes code look cleaner, it may sometimes be hard to follow.


2. They may be simpler, but recursive calls are expensive. They take up a lot of memory and time.
3. Finally, it isn’t as easy to debug a recursive function.

6. More Examples of Python Recursion Function


Before we leave for today, we’ll take a couple more examples to understand Python Recursion better.

First, let’s define a function to calculate the sum of the first n natural numbers.

1. >>> def sumofn(n):


2. if n==1:
3. return 1
4. return n+sumofn(n-1)
5. >>> sumofn(16)

136
Here, sumofn(16)= 16+sumofn(15)

=16+15+sumofn(14)

=16+120

=136

Let’s take just one more example before we say goodbye. In this one, we’ll store the first n terms from the
Fibonacci series for argument ‘n’.

1. >>> a,b,fib=0,1,[]
2. >>> fib.append(a)
3. >>> fib.append(b)
4. >>> def fibonacci(n):
5. if n==2:
6. return
7. global a,b
8. a,b=b,a+b
9. fib.append(b)
10. fibonacci(n-1)
11. >>> fib

[0, 1]

1. >>> fibonacci(9)
2. >>> fib

[0, 1, 1, 2, 3, 5, 8, 13, 21]

Honestly, we had fun coding this one. What we do here is, first, we store the values 0 and 1 in variables a and b.
We also declare an empty list ‘fib’. Then, we append a and b to fib (for 0 and

It is now that we define the recursive function for our purpose.

In our base case, we check for n==2, because we already added two terms to the list. When n is

We ‘return’ (our work is done here). Then we use the global keyword for a and b to be able to access them.

Our main logic is in the line a,b=b,a+b. With this, we give b’s value to a, while simultaneously adding both
values and giving the sum to b. Then we append b’s value to fib, and then call the function for one less term.

Finally, we call the function on the value 9 to get 9 terms in the list fib. Wasn’t this interesting? Would you like
to add some more examples?

Read: Advantages and Disadvantages of Python Programming Language

So, this was all about Python Recursion Function Tutorial. Hope you like our explanation.
7. Conclusion
In this entire article, we’ve focused on recursion in python and its examples. Repeating it, recursion is when you
use something to define itself. Next, we saw its advantages and disadvantages. While it is expensive, it also
leads to cleaner code. Keep doing some recursion Python Practice and exercises.

Python Lambda Expression – Declaring Lambda Expression & Its Defaults


by DataFlair Team · March 1, 2019

1. Python Lambda Expression


In this Python Tutorial we will study, What is Python Lambda Expression with its syntax and examples.
Moreover, we will study how to and when to declare Lambda Expression in Python, Python Lambda with
inbuilt Python lambda functions, and some python functions are: reduce(), map(), and filter().

When creating functions in Python, we use the def keyword. We bind them to a name while doing so. But
sometimes, we may want to declare a function anonymously. Or we may want to use a function only once. In
such a case, defining a function seems a bit extra.

So, let’s start learning Python Lambda Expression Tutorial.

Python Lambda Expression


2. What is Python Lambda Expression?
Python lambda expression allows us to define a function anonymously. It is worthwhile to note that it is an
expression, not a statement. That is, it returns a value; it has an implicit return statement. The following is
the Python syntax of a lambda expression in Python.
lambda [arg1,arg2,..]:[expression]

Where you place lambda expression in Python, it returns the value of the expression.

3. How to Declare Python Lambda


Expression?
To declare a lambda expression in Python, you use the lambda keyword.

1. >>> lambda e:e-2

<function <lambda> at 0x03DBA978>

You can assign it to a variable if you want to be able to call it again later.

1. >>> downbytwo=lambda e:e-2

Here, e is the argument, and e-2 is the expression. Then you would call it like you would call any other Python
Lambda function. Here, we passed the integer 1 as an argument.

1. >>> downbytwo(1)

-1
You can also pass an argument along with the declaration. Use parentheses for this.

1. >>> (lambda e:e-2)(1)

-1

4. What Exactly is an Expression in Python?


As we’ve seen, the expression is what the Python3 lambda returns, so we must be curious about what qualifies
as an expression. An expression here is what you would put in the return statement of a Python Lambda
function. The following items qualify as expressions.

1. Arithmetic operations like a+b and a**b


2. Function calls like sum(a,b)
3. A print statement like print(“Hello”)

Other things like an assignment statement cannot be provided as an expression to Python lambda because it
does not return anything, not even None.
5. When to Use Which Lambda Expression in
Python?

Like we’ve seen so far, Python lambda expression may take arguments, and takes one expression. The value of
this expression is what it returns after evaluating. Python Lambda expression isn’t necessary, but it’s handy in
certain situations. These are:

1. When you have only a single statement to execute in your Python Lambda function

Suppose we want to print a Hello in the body of a function printhello(). In this function’s body, we have only
one line of code.

1. >>> def printhello():


2. print("Hello")
3. >>> printhello()

Hello
Now, let’s do this with a Python lambda expression.

1. >>> (lambda :print("Hello"))()

Hello
Notice that we did not provide any arguments here. We’ll discuss this in a later section in this article. Let’s take
another example yet.

1. >>> z=lambda a=2:print("Hello")


2. >>> z()

Hello

Read more about Python Function Argument

2. When you need to call that code only once

One of the main reasons why we use functions, apart from modularity, is the reusability of code. But when you
want to need some code only once, or less often, you may use a Python lambda expression in place of defining a
function for it.

6. Defaults in Python Lambda Expression


In Python, and in other languages like C++, we can specify default arguments. But what is that? Suppose a
function func1() has an arity of 2 with parameters a and b. What happens if the user provides only one argument
or none? This is why you may want to provide default values for your parameters. Let’s take an example.
1. >>> def func1(a=2,b=3):
2. print(a,' ',b)
3. >>> func1()

2 3

1. >>> func1(3)

3 3
Here, the default values for a and b are 2 and 3, respectively.
To call as y(), need default arguments:

1. >>> o=lambda x=1,y=2,z=3:x+y+z


2. >>> o(2,3)

1. >>> o(2)

1. >>> o

<function <lambda> at 0x00A46150>

1. >>> o()

7. Python Lambda Expression Syntax


We’ve seen how we declare lambda expression in Python, but where there are building blocks, there are
possibilities. So, let’s see what we can or can’t do with a lambda.

a. Python Arguments
One or more variables appearing in the expression may be declared previously. But if it’s there in the
arguments, either it should have a default value or must be passed as an argument to the call.

1. >>> a,b=1,2
2. >>> y=lambda a,b:a+b
3. >>> y()

Traceback (most recent call last):

File “<pyshell#167>”, line 1, in <module>

y()
TypeError: <lambda>() missing 2 required positional arguments: ‘a’ and ‘b’

Here, both a and b are missing values.

1. >>> y=lambda a:a+b


2. >>> y()

Traceback (most recent call last):

File “<pyshell#169>”, line 1, in <module>

y()

TypeError: <lambda>() missing 1 required positional argument: ‘a’

The variable a is still missing a value.

1. >>> y=lambda :a+b


2. >>> y()

3
Finally, since no argument here is missing a value, it works just fine.

b. Omitting Arguments
It isn’t mandatory to provide arguments to a lambda expression in Python, it works fine without them.

1. >>> y=lambda :2+3


2. >>> y()

5
Another example would be where the expression is a print statement.

1. >>> (lambda :print("Hi"))()

Hi
Hence, we conclude that omitting arguments is acceptable.

c. Omitting the Expression


Now let’s try if it works without an expression.

1. >>> y=lambda a,b:

SyntaxError: invalid syntax


Clearly, it didn’t work. And why would it? The expression’s value is what it returns, so without an expression,
it would be a waste of time.
8. Python Lambdas with In-Built Functions
There are some built-in functions, like filter() and map(), on which we can apply Python lambda expression to
filter out elements from a list. Let’s see how. First, we take a list called numbers.

1. >>> numbers=[0,1,2,3,4,5,6,7,8,9,10]

Next, we take a lambda function as follows.


lambda x:x%3==0

a. filter()
The filter() function takes two parameters- a function, and a list to operate on. Finally, we apply the list()
function on this to return a list.

1. >>> list(filter(lambda x:x%3==0,numbers))

[0, 3, 6, 9]

This is the final code for our purpose, and guess what it does. Well, this code takes the list ‘numbers’, and filters
out all elements from it except those which do not successfully divide by 3.
Note that this does not alter the original list.
Read more about Python List

b. map()
The map() function, unlike the filter() function, returns values of the expression for each element in the list.
Let’s take the same list ‘numbers’.

1. >>> list(map(lambda x:x%3==0,numbers))

[True, False, False, True, False, False, True, False, False, True, False]

c. reduce()
Lastly, the reduce() function takes two parameters- a function, and a list. It performs computation on sequential
pairs in the list and returns one output. But to use it, you must import it from the functools module.

1. >>> from functools import reduce


2. >>> reduce(lambda x,y:y-x,numbers)

5
Let’s perform a dry run.
1-0=1
2-1=1
3-1=2
4-2=2
5-2=3
6-3=3
7-3=4
8-4=4
9-4=5
10-5=5
Hence, it outputs 5. Let’s take another example.

1. >>> reduce(lambda x,y:y+x,numbers)

55
Try doing the same thing here for y+x instead; you should get 55.

So, this was all about Python lambda expression. Hope you like our expression.

9. Conclusion
That’s all for today. We learned quite a bit about Python3 lambda expression with syntax, how to create
Lambda expressions in python. Then we saw what qualifies as an expression, and also learned how to provide
default arguments in Python Lambda expression. Finally, we tried out three functions, filter(), map(), and
reduce(), to make use of lambdas. A lambda, as compared to a normal function, offers its own benefits.

Furthermore, if you have any query, feel free to ask in the comment box.

Python Decorator Tutorial – Chaining Decorators, Python Pie Syntax


by DataFlair Team · May 27, 2019

1. Python Decorator – Objective


In this Python Decorator tutorial, we will study what is a decorator in Python and why we use a Python Nested
Functions. Along with this, we will learn Python Decorators with Parameters and Python Pie Syntax. At last, we
will study Chaining Decorators in Python programming language.

In Python, a function is a first-class object. This means that you can pass it around with absolute ease. You can
return it, and even pass it as an argument to another. You can also nest a python function inside another.

So, let’s start the Python Decorator Tutorial.


Python Decorator Tutorial – Chaining Decorators, Python Pie Syntax

2. What is Python Decorator?


Python Decorator function is a function that adds functionality to another, but does not modify it. In other
words, Python Decorator wraps another function. This is like a gift wrapper in real life. Also, this is called
metaprogramming, because a part of the program tries to modify another at compile time. In the rest of the
lesson, we will see the python syntax of a Python decorator in detail.

This is useful in cases when you want to add functionality to a function, but don’t want to modify it for the
same. Let’s take a look.

3. A Simple Python Decorator


Talking about Python decorator for the first time can be confusing. So we begin with a very simple example
with no arguments. Take this code.

1. >>> def decor(func):


2. def wrap():
3. print("$$$$$$$$$$$$$$$$$$$$$$")
4. func()
5. print("$$$$$$$$$$$$$$$$$$$$$$")
6. return wrap
7. >>> def sayhello():
8. print("Hello")
9. >>> newfunc=decor(sayhello)
10. >>> newfunc()
$$$$$$$$$$$$$$$$$$$$$$

Hello

$$$$$$$$$$$$$$$$$$$$$$

Now let’s see each part of the syntax one by one. Also, Read Recursion in Python.

a. Python Decorator Function


First, we define a simple function sayhello() that prints out “Hello”. Now, we’ll define the decorator function in
Python. You can call this anything; it doesn’t have to be ‘decor’. This is a higher order function. Note that the
relative order of these functions does not matter. You could define sayhello() before defining decor(), and it
wouldn’t make a difference. Let’s discuss the decor function in detail.

def decor(func):

The first thing to notice here is that it takes a function as an argument. This is the function that we want to
decorate. We want to call it func; you may want to call it something else. Inside this function, we nest a
function, and we call it wrap(). Again, you can call it anything you want.

b. The nested wrap function


It is inside this function that we put our extra functionality, and also call the function to be decorated.

1. def wrap():
2. print("$$$$$$$$$$$$$$$$$$$$$$")
3. func()
4. print("$$$$$$$$$$$$$$$$$$$$$$")

Here, we used some print statements. It could have been anything else too, like an if-block.

Finally, we make the decor() function return the wrap function.

return wrap

Why do we do this? We’ll discuss this further in this lesson.

c. Assigning and Calling


Finally, we assign this Python decorator to a variable and pass the function to be decorated as an argument to
the decorating function.

newfunc=decor(sayhello)

Then, we call the function using parentheses after the variable to which we assign the decorators in

python.
newfunc()

However, you can also assign this to the function to be decorated itself. This will reassign it. Let’s see that as
well.

1. >>> def sayhello():


2. print("Hello")
3. >>> def decor(func):
4. def wrap():
5. print("$")
6. func()
7. print("$")
8. return wrap
9. >>> sayhello=decor(sayhello)
10. >>> sayhello()

Hello

Before Proceeding Read Python Functions with Syntax and Examples.

4. Why use a Python Nested Function?


When I was attempting to understand Python decorator, this question totally confused me. Why do we use the
wrap function, and then return it? Couldn’t we simply write the code inside the decor function? So I ended up
on the interpreter, trying it out.

1. >>> def decor(func):


2. print("$")
3. func()
4. print("$")
5. >>> def sayhello():
6. print("Hello")

Here, we wrote the extra functionality right inside our decor() function. Now, let’s try to assign it to a variable.

1. >>> newfunc=decor(sayhello)

Hello

$
Woah. Why did it print it out? This is because decor() calls a function (here, func) instead of returning a value.
When we use wrap (or whatever you’d call it), and then return it, we can store it in a variable. Then, we can use
that name to call the decorated function whenever we want. Now let’s call the newfunc() function.

1. >>> newfunc()

Traceback (most recent call last):

File “<pyshell#70>”, line 1, in <module>

newfunc()

TypeError: ‘NoneType’ object is not callable

As you can see, since decor did not return a value, the line of assignment did not assign the decorated function
to newfunc. This is why it isn’t callable. So, it is impossible to access this Python decorator again except for the
following method:

1. >>> decor(sayhello)

Hello

Finally, let’s try calling our original function sayhello().

1. >>> sayhello()

Hello

Works perfectly. See, decor() did not modify sayhello().

We use the same example everywhere so you can focus on what’s being explained, and not be invested in trying
to understand the code.

5. Python Decorator with Parameters


So far, we’ve only seen decorators in python with regular print statements. Now, let’s get into the real thing. To
see how decorators fare with parameters, we’ll take the example of a function that divides two values. All that
our function does is to return the division of two numbers. But when we decorate it, we add functionality to deal
with the situation where the denomination is 0. Watch how.

1. >>> def divide(a,b):


2. return a/b
3. >>> def decorator(func):
4. def wrapper(a,b):
5. if b==0:
6. print("Can't divide by 0!")
7. return
8. return func(a,b)
9. return wrapper

Like you can see, the Python decorator function takes one argument for the function to decorate. The wrapper
here takes the same arguments as does the function to decorate. Finally, we return the function to be decorated
instead of calling it. This is because we want to return a value here from the divide() to the wrapper() to the
decorator().

a. Python Closure
When we call func, it remembers the value of func from the argument to the function decorator(). This is called
closure in Python. Here’s another example to clear this up.

1. >>> msg="Hello"
2. >>> def func1(msg):
3. def func2():
4. print(msg)
5. func2()
6.
7. >>> func1(msg)

Hello

Also, note that if we called func(a,b) instead of returning it, we’d get this:

1. >>> divide(2,3)
2. >>> print(divide(2,3))

None

Now, let’s assign and call.

1. >>> divide=decorator(divide)
2. >>> divide(2,3)

0.6666666666666666

1. >>> divide(2,0)

Can’t divide by 0!

Problem Solved.

b. *args and **kwargs


If you don’t want to type in the whole list of arguments for the two statements, *args and **kwargs will do the
trick for you.
1. >>> def divide(a,b):
2. return a/b
3. >>> def decorate(func):
4. def wrapper(*args,**kwargs):
5. if args[1]==0:
6. print("Can't divide by 0!")
7. return
8. return func(*args,**kwargs)
9. return wrapper
10.
11. >>> divide=decorate(divide)
12. >>> divide(2,0)

Can’t divide by 0!

See, it works. Actually, *args is a tuple of arguments, and **kwargs is a dictionary of keyword arguments.

Any Doubt yet in Python 3 Decorators? Please ask in comments.

6. Pie Syntax in Python


First Go through Python Syntax | The Best Tutorial to Python Syntax

If you feel the assignment and calling statements are unnecessary, we’ve got the pie syntax for you. It’s simple;
name the decorating function after the @ symbol, and put this before the function to decorate. Here’s an
example.

1. >>> @decor
2. def sayhello():
3. print("Hello")
4. >>> sayhello()

$$

Hello

$$

7. Chaining Decorators in Python


You don’t have to settle with just one Python decorator. That’s the beauty of the pie syntax. Let’s see how.

1. >>> def decor1(func):


2. def wrap():
3. print("$$$$$$$$$$$$$$")
4. func()
5. print("$$$$$$$$$$$$$$")
6. return wrap
7.
8. >>> def decor2(func):
9. def wrap():
10. print("##############")
11. func()
12. print("##############")
13. return wrap

Now, let’s define the sayhello() function. We’ll use decor1 and decor2 on this.

1. >>> @decor1
2. @decor2
3. def sayhello():
4. print("Hello")
5.
6. >>> sayhello()

$$$$$$$$$$$$$$

########

Hello

########

$$$$$$$$$$$$$$

Note how the octothorpes (#) are sandwiched by dollars ($)? This lends us an important piece of information- the order of
the decorators in python in the pie syntax matters. Since we used decor1 first, we get the dollars first.

This was all about the Python Decorator Tutorial.

8. Conclusion
Concluding what we discussed so far in this Python Decorator with arguments simple examples Tutorial, decorators in
python help us add extra functionality to a function without modifying it. We also saw the pie syntax for the same. And
now you know- anything that confuses you, you can conquer it by facing it, for you can’t run forever. Escape isn’t real.
See you again.

Python Generator – Python Generator Expressions (Best


Lesson)
by DataFlair Team · May 27, 2019
1. Python Generator – Objective
In our last Python Tutorial, we studied Python functions. Today, in this Python Generator tutorial, we will
study what is a generator in Python Programming. Along with this, we will discuss Python Generator
Expressions, Python list vs generator, and Python Function vs Generators.

So, let’s start the Python Generator Tutorial.

Python Generator – Python Generator Expressions (Best Lesson)

2. Define Python Generator?


A Python generator is a kind of an iterable, like a Python list or a python tuple. It generates for us a sequence
of values that we can iterate on. You can use it to iterate on a for-loop in python, but you can’t index it. Let’s
take a look at how to create one with python generator example.

a. The Syntax of Generator in Python 3


To create a python generator, we use the yield statement, inside a function, instead of the return statement.
Let’s take a quick example.

1. >>> def counter():


2. i=1
3. while(i<=10):
4. yield i
5. i+=1
With this, we defined a Python generator called counter() and assigned 1 to the local variable i. As long as i is
less than or equal to 10, the loop will execute. Inside the loop, we yield the value of I, and then increment it.

Then, we iterate on this generator using the for-loop.

1. >>> for i in counter():


2. print(i)

1
2
3
4
5
6
7
8
9
10

b. Working of Python Generator


To understand how this code works, we’ll start with the for-loop. For each item in the Python generator (each
item that it yields), it prints it, here.

We begin with i=1. So, the first item that it yields is 1. The for-loop prints this because of our print statement.
Then, we increment I to 2. And the process follows until i is incremented to 11. Then, the while loop’s
condition becomes False.

However, if you forget the statement to increment I, it results in an infinite generator. This is because a Python
generator needs to hold only one value at a time. So, there are no memory restrictions.

1. >>> def even(x):


2. while x%2==0:
3. yield 'Even'
4. >>> for i in even(2):
5. print(i)

Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even

EvenTraceback (most recent call last):

File “<pyshell#24>”, line 2, in <module>

print(i)

KeyboardInterrupt

Here, since 2 is even, 2%2 is always 0. Hence, the condition for while is always true. Because of this, the
Python3 generator even() keeps yielding the value True until we hit Ctrl+C on the keyboard to interrupt the
execution.

Note that a generator may contain more than one Python yield statement. This is comparable to how a Python
generator function may contain more than one return statement.

1. >>> def my_gen(x):


2. while(x>0):
3. if x%2==0:
4. yield 'Even'
5. else:
6. yield 'Odd'
7. x-=1
8. >>> for i in my_gen(7):
9. print(i)

Odd
Even
Odd
Even
Odd
Even
Odd

3. Yielding into a Python List


This one’s a no-brainer. If you apply the list() function to the call to the Python generator, it will return a list of
the yielded values, in the order in which they are yielded. Here, we take an example that creates a list of squares
of numbers, on the condition that the squares are even.
1. >>> def even_squares(x):
2. for i in range(x):
3. if i**2%2==0:
4. yield i**2

To create a list, we just apply the list() function to the call to this Python generator. We do not iterate on it using
a for-loop.

1. >>> print(list(even_squares(10)))

[0, 4, 16, 36, 64]

As you can see, in numbers 1 to 9 (not 10, because range(10) gives us 0 to 9), the even squares are 0, 4, 16, 36,
and 64. The others, that are 1, 9, 25, 49, 81 are odd. So, they’re not yielded.

4. Python List vs Generator in Python


This is a very simple difference. A list holds a number of values at once. But a Python generator holds only one
value at a time, the value to yield. This is why it needs much less space compared to a list. With a generator, we
also don’t need to wait until all the values are rendered.

5. Python Generator vs Function


Now, to compare a generator to a function, we first talk about return and Python yield. When the interpreter
reaches the return statement in a function, it stops executing the Python Generator function and executes the
statement after the function call. However, when it reaches the Python yield statement in a generator, it yields
the value to the iterable. Finally, it gets back to the generator to continue for the next value.
Also, when a function stops executing, its local variables are destroyed. This is not the same with a Python
generator. Take a look.

1. >>> def mygen():


2. i=7
3. while i>0:
4. yield i
5. i-=1
6. >>> for i in mygen():
7. print(i)

7
6
5
4
3
2
1
6. Python Generator Expressions
Just like a list comprehension, we can use expressions to create python generators shorthand. Let’s take a list for
this.

1. >>> mylist=[1,3,6,10]
2. >>> (x**2 for x in mylist)

<generator object <genexpr> at 0x003CC330>

As is visible, this gave us a Python generator object. But to access the values, we need to store this into a
variable, and then apply the next() function to it.

1. >>> a=(x**2 for x in mylist)


2. >>> next(a)

1. >>> next(a)

1. >>> next(a)

36

1. >>> next(a)

100

1. >>> next(a)

Traceback (most recent call last):

File “<pyshell#89>”, line 1, in <module>

next(a)

StopIteration

So, this was all about Python Generator Tutorial. Hope you like our explanation.

7. Conclusion
Now that you know the benefits of python generator over a list or over a function, you will understand its
importance. Some things, we can do with a generator, with a function, or even with a list comprehension. But
using a Python generator is the most efficient. Tell us what you think.
What is Python Iterator (Syntax & Example) – Create your
own Iterator
by DataFlair Team · September 25, 2018

1. Python Iterator
In this Python Iterator Tutorial, we will learn what is Python iterator. We will also discuss how to create our
own iter () and next () methods, building a python iterator, for loop in python iterator, infinite python
iterator, and benefits of an iterator in python with an example.

In our article on python in-built functions, one function we saw was iter(). Let us generate an iterator in python,
which we traversed using the next() function.

S, let’s start Python Iterator Tutorial.

What is Python Iterator (Syntax & Example) – Create your own Iterator

2. What are Python3 Iterators?


An iterator in Python programming language is an object which you can iterate upon. That is, it returns one
object at a time. Python Iterator, implicitly implemented in constructs like for-loops, comprehensions, and
python generators. The iter() and next() functions collectively form the iterator protocol.

If we can get iterable from an object in python, it is iterable. Examples include python lists, python tuples, and
python strings.
3. How to Create Python Iterator?
To build a python3 iterator, we use the iter() and next() functions. Let’s begin with iter() to create an iterator.

First, we create a list of events to store even numbers from 2 to 10.

1. >>> evens=[2,4,6,8,10]

Then, we apply the iter() function to this Python list to create a Python iterator object. We store it in the
variable evenIterator.

1. >>> evenIterator=iter(evens)
2. >>> evenIterator

<list_iterator object at 0x05E35410>

Remember, it does not have to be a list you create an iterator on.

1. >>> iter((1,3,2))

<tuple_iterator object at 0x05E35550>

Now, to access the first element, we apply the function next() on the Python iterator object.

1. >>> next(evenIterator)

2
We do the same for the next element(s) as well.

1. >>> next(evenIterator)

1. >>> next(evenIterator)

1. >>> next(evenIterator)

1. >>> next(evenIterator)

10
We have reached the end of the list. When we call it once more, we raise a StopIteration error (exception). The
interpreter internally catches it.

1. >>> next(evenIterator)
Traceback (most recent call last):

File “<pyshell#442>”, line 1, in <module>

next(evenIterator)

StopIteration

 next ()

You can also traverse the Python iterator using the next () method.

1. >>> nums=[1,2,3]
2. >>> numIter=iter(nums)
3. >>> numIter. next ()

1. >>> next(numIter)

1. >>> numIter. next ()

1. >>> numIter. next ()

Traceback (most recent call last):

File “<pyshell#448>”, line 1, in <module>

numIter. next ()

StopIteration

We can see this with the dir() function we saw in in-built functions in Python.

1. >>> dir(numIter)

[‘ class ’, ‘ delattr ’, ‘ dir ’, ‘ doc ’, ‘ eq ’, ‘ format ’, ‘ ge ’, ‘ getattribute ’, ‘ gt ’,


‘ hash ’, ‘ init ’, ‘ init_subclass ’, ‘ iter ’, ‘ le ’, ‘ length_hint ’, ‘ lt ’, ‘ ne ’,
‘ new ’, ‘ next ’, ‘ reduce ’, ‘ reduce_ex ’, ‘ repr ’, ‘ setattr ’, ‘ setstate ’, ‘ sizeof ’,
‘ str ’, ‘ subclasshook ’]

4. For-loop in Python Iterator


You can also use a for loop in python to iterate on an iterable like a python list or a python tuple.
1. >>> for i in 'Python':
2. print(i)

P
y
t
h
o
n
But how is this actually implemented? Let’s take a look.

1. >>> iter_obj=iter('Python')
2. >>> while True:
3. try:
4. i=next(iter_obj)
5. print(i)
6. except StopIteration:
7. break

P
y
t
h
o
n
So, this is how the above for loop is actually implemented.

5. How to Create Your Own Python Iterator?


Now you know how to use an iterator with the iter() and next() functions. But we won’t stop here. We will now
start from scratch.

We implement the following class to create an iterator in Python for squares of numbers from 1 to max.

1. >>> class PowTwo:


2. def init (self,max=0):
3. self.max=max
4. def iter (self):
5. self.n=0
6. return self
7. def next (self):
8. if self.n<=self.max:
9. result=2**self.n
10. self.n+=1
11. return result
12. else:
13. raise StopIteration
Here, init () is to take the value of max. Then, we create a python object ‘a’ of the class, with the argument
4. Then, we create an iterator in Python using iter(). Next, we use the next() function to get the elements one by
one.

1. >>> a=PowTwo(4)
2. >>> i=iter(a)
3. >>> next(i)

1. >>> next(i)

1. >>> next(i)

1. >>> next(i)

1. >>> next(i)

16

1. >>> next(i)

Traceback (most recent call last):

File “<pyshell#484>”, line 1, in <module>

next(i)

File “<pyshell#476>”, line 13, in next

raise StopIteration

StopIteration

Alternatively, you can use the iter () and next () methods for this object.

1. >>> j=a. iter ()


2. >>> j. next ()

1. >>> j. next ()

2
1. >>> j. next ()

1. >>> j. next ()

1. >>> j. next ()

16

1. >>> j. next ()

Traceback (most recent call last):

File “<pyshell#491>”, line 1, in <module>

j. next ()

File “<pyshell#476>”, line 13, in next

raise StopIteration

StopIteration

Internally, the iter() function calls the iter () method.

6. Infinite Python3 Iterator


It is indeed possible to create an iterator in Python that never exhausts. The iter() function can take another
argument, called the ‘sentinel’, it keeps a watch. As long as the first value isn’t the same as the sentinel, the
Python iterator does not exhaust.

We know that the int() function, without a parameter, returns 0.

1. >>> int()

Now, we call iter() on two arguments- int and 1.

1. >>> a=iter(int,1)

This Python iterator will never exhaust; it is infinite. This is because 0 is never equal to 1.

1. >>> next(a)

1. >>> next(a)

1. >>> next(a)
1. >>> next(a)

1. >>> next(a)

And so on.

To create an infinite Python iterator using a class, take the following example.

1. >>> class Even:


2. def iter (self):
3. self.num=2
4. return self
5. def next (self):
6. num=self.num
7. self.num+=2
8. return num
9. >>> e=Even()
10. >>> i=iter(e)
11. >>> next(i)

1. >>> next(i)

1. >>> next(i)

1. >>> next(i)

8
This python iterates on even numbers beginning at 2, ending nowhere. So, you must be careful to include a
terminating condition.

7. Benefits of Python Iterator


An iterator in python saves resources. To get all the elements, only one element is stored in the memory at a
time. Unlike this, a list would have to store all the values at once.

8. Conclusion
In this article, we learned about python iterators. Aren’t they fun and super-handy? An iterator makes use of two
functions- iter() and next(). However, we can make our own iterator in python with a python class. Finally, we
looked at infinite iterators.
Furthermore, if you have any query/doubt feel free to ask in the comment box.

Python Generators vs Iterators – Comparison Between Python


Iterators and Generators
by DataFlair Team · September 25, 2018

1. Generators vs Iterators
It’s been more than a month we began our journey with Python Programming Language. Through the days,
we have also learned concepts like Python generators and iterators in Python. We know their functionalities.
But how are they different? In this lesson, we discuss the comparison of python generator vs iterator. Before we
proceed, let’s discuss Python Syntax.

Difference Between Python Generator vs Iterator

2. Introduction to Python Generators


A python generator function lends us a sequence of values to python iterate on. The following is an example of
generators in python.

1. >>> def even(x):


2. while(x!=0):
3. if x%2==0:
4. yield x
5. x-=1
6. >>> for i in even(8):
7. print(i)

8
6
4
2
To see the generator in detail, refer to our article on Python Generator.

3. Introduction to Python Iterators


A Python iterator returns us an iterator object- one value at a time. Let’s take an example of an iterator in
python.

1. >>> iter_obj=iter([3,4,5])
2. >>> next(iter_obj)

1. >>> next(iter_obj)

1. >>> next(iter_obj)

1. >>> next(iter_obj)

Traceback (most recent call last):

File “<pyshell#560>”, line 1, in <module>

next(iter_obj)

StopIteration

For more insight, check our Python Iterator tutorial.

4. Comparison Between Python Generator vs


Iterator
Let’s see the difference between Iterators and Generators in python.

1. In creating a python generator, we use a function. But in creating an iterator in python, we use the
iter() and next() functions.
2. A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
3. Python generator saves the states of the local variables every time ‘yield’ pauses the loop in python.
An iterator does not make use of local variables, all it needs is iterable to iterate on.
4. A generator may have any number of ‘yield’ statements.
5. You can implement your own iterator using a python class; a generator does not need a class in
python.
6. To write a python generator, you can either use a Python function or a comprehension. But for an
iterator, you must use the iter() and next() functions.
7. Generator in python let us write fast and compact code. This is an advantage over Python iterators.
They are also simpler to code than do custom iterator.
8. Python iterator is more memory-efficient. Lest see this with example below:

1. >>> def func():


2. i=1
3. while i>0:
4. yield i
5. i-=1
6. >>> for i in func():
7. print(i)

1. >>> func(). sizeof ()

32
Here, we got 32. But for a python iterator, we get 16.

1. >>> iter([1,2]). sizeof ()

16

9. A generator returns a generator. Below an s example to understand it.

1. >>> f=func()
2. >>> type(f)

<class ‘generator’>
However, an iterator returns an iterator object.

1. >>> i=iter({1,3,2})
2. >>> type(i)

<class ‘set_iterator’>

10. python Generator provides even more functionality as co-routines.

5. Relationship Between Python Generators and Iterators


a. A python generator is an iterator
Generator in python is a subclass of Iterator. To prove this, we use the issubclass() function.

1. >>> import collections,types


2. >>> issubclass(types.GeneratorType,collections.Iterator)
True

1. >>> issubclass(collections.Generator,collections.Iterator)

True

1. >>> issubclass(collections.Iterator,types.GeneratorType)

False
b. Python iterator is an iterable
Iterator in python is a subclass of Iterable.

1. >>> issubclass(collections.Iterator,collections.Iterable)

True

6. Conclusion
Hence, we study the difference between python generator vs iterator and we can say every generator is an
iterator in Python, not every python iterator is a generator. Both come in handy and have their own perks. Tell
us what you think in the comments.

Python Closure Tutorial – How to Define Python Closures


Function
by DataFlair Team · September 26, 2018

1. Python Closure
Previously, we talked about Python closure, when we discussed Python Decorators. Time to turn it up a notch.
In this Python Closure tutorial, we’ll learn about Python closures and how to define them. Moreover, we will
learn nested function and nonlocal variables used in Closures. At last, we cover benefits & examples of Python
closure.

So, let’s start the Python Closure Tutorial.


Python Closure Tutorial –

2. What is Python Closure?


When we define a function inside of another, the inner function is said to be nested inside the outer one. Let’s
take an example.

1. >>> def outerfunc(x):


2. def innerfunc():
3. print(x)
4. innerfunc()
5. >>> outerfunc(7)

7
If you noticed, innerfunc could read the variable ‘x’, which is nonlocal to it. And if it must modify ‘x’, we
declare that it’s nonlocal to innerfunc. We do this the way we saw in our tutorial on Python Namespace and
Variable Scope.

3. How can we Define Python Closure Function?


Now that we’ve revised a couple topics, we can move on to Python3 closure. Let’s define a closure.

1. >>> def outerfunc(x):


2. def innerfunc():
3. print(x)
4. return innerfunc #Return the object instead of calling the function
5. >>> myfunc=outerfunc(7)
6. >>> myfunc()
7
The point to note here is that instead of calling innerfunc here, we returned it (the object). Once we’ve defined
outerfunc, we call it with the argument 7 and store it in variable myfunc. Okay, we’ve finished executing
outerfunc now. So, when we call myfunc next, how does it remember that ‘x’ is 7?
This is the point here. A Python3 closure is when some data gets attached to the code. So, this value is
remembered even when the variable goes out of scope, or the function is removed from the namespace. If we
delete outerfunc, myfunc still gives us 7.

1. >>> del outerfunc


2. >>> myfunc()

7
So, we conclude that we have Python closure when a nested function references a value in its enclosing scope.
These three conditions must be met:

1. We must have a nested function.


2. This nested function must refer to a variable nonlocal to it(a variable in the scope enclosing it).
3. The enclosing scope must return this function.

4. Benefits of Python Closure


While it seems like a very simple concept, a closure in python3 helps us in the following ways:

1. With Python closure, we don’t need to use global values. This is because they let us refer to nonlocal
variables. A closure then provides some form of data hiding.
2. When we have only a few Python methods (usually, only one), we may use a Python3 closure instead
of implementing a class for that. This makes it easier on the programmer.
3. A closure, lets us implement a Python decorator.
4. A closure lets us invoke Python function outside its scope.

5. More Examples of Python Closure


Before we say goodbye for today, let’s take a couple more examples of closure in python.

1. >>> def outer(x):


2. result=0
3. def inner(n):
4. nonlocal result
5. while n>0:
6. result+=x*n
7. n-=1
8. return result
9. return inner
10. >>> myfunc=outer(7)
11. >>> myfunc(3)

42

1. >>> myfunc=outer(3)
2. >>> myfunc(3)

18
In this example, we declare result to be nonlocal, and we return it from inner. And then, we return inner from
outer. Time for another example.

1. >>> def outer(func):


2. def inner(msg):
3. func(msg)
4. return inner
5. >>> def sayhi(msg):
6. print(msg)
7. >>> myfunc=outer(sayhi)
8. >>> myfunc("Hello")

Hello
Here, we passed a function object to outer.

6. Conclusion
With this last example, we conclude this Python closure tutorial with example. Indeed, Python closures are cool.
They let us refer to nonlocal variables, in turn helping with data hiding. We can also use them instead of classes
when we only need about a couple methods. Furthermore, if you feel any query, feel free to ask in a comment
section.

Python Class Tutorial – Python Object & Attributes


Belonging to Class
by DataFlair Team · May 27, 2019

1. Python Class Tutorial


In this Python Class tutorial, we are going to explore about Python Classes. how they work and access. On the
other hand, we will discuss what is the Python Object and different attributes belong to Python Class. Atlast, we
cover How can we delete an object, attributes, or a class in Python.

Like we’ve often said, Python is an object-oriented language. This means it focuses on objects instead of
procedures. An object can model the real world.

So, let’s start Python Class and Object Tutorial.


Python Class Tutorial – Python Object & Attributes Belonging to Class

2. Introduction to Python Class


A class is a blueprint for objects- one class for any number of objects of that type. You can also call it an
abstract data type. Interestingly, it contains no values itself, but it is like a prototype for objects.

Let’s see the Python classes explained in detail.

3. Python Class Syntax


a. Defining a Python Class
To define a class in python programming, we use the ‘class’ keyword. This is like we use ‘def’ to define a
function in python. And like a function, a Python3 class may have a docstring as well.

We can do this to write a couple of lines explaining what the class does. To focus on the syntax here, we will
pass in a ‘pass’ statement in its body for now.

1. >>> class fruit:


2. """
3. This Python3 class creates instances of fruits
4. """

Pass

As soon as we define a class, a Python class object is created. But remember, you can only name a class
according to the identifier naming rules as we discussed in our tutorial on Python Variables.
1. >>> fruit

<class ‘ main .fruit’> #The class object

A Python3 class may have attributes and methods to execute on that data. We declare/define these the usual
way. Let’s take an example.

1. >>> class fruit:


2. """
3. This class creates instances of fruits
4. """
5. color=''
6. def sayhi(self):
7. print("Hi")
8.
9. >>> orange=fruit()

Here, color is an attribute, and sayhi() is a method to call on an object of class fruit.

You can also define a regular first-class function inside a method, but not outside it in a Python class.

1. >>> class try1:


2. def mymethod(self):
3. def sayhello():
4. print("Hello")
5. print("Hi")
6. sayhello()
7.
8. >>> obj1=try1()
9. >>> obj1.mymethod()

Hi

Hello

You can also create an attribute on the fly.

1. >>> orange.shape='Round'
2. >>> orange.shape

‘Round’

b. Accessing Python Class Members


To access the members of a Python class, we use the dot operator. Let’s create an orange object for our fruit
class.

1. >>> orange=fruit()
Now, let’s access the color attribute for orange.

1. >>> orange.color


This returns an empty string because that is what we specified in the class definition.

1. >>> orange.sayhi()

Hi

Here, we called the method sayhi() on orange. A method may take arguments, if defined that way.

1. >>> class fruit:


2. def size(self,x):
3. print(f"I am size {x}")
4.
5. >>> orange=fruit()
6. >>> orange.size(7)

I am size 7

A Python class may also have some special attributes, like doc for the docstring.

1. >>> fruit. doc

‘\n\tThis class creates instances of fruits\n\t’

To get more insight into methods in Python, read up on Python Methods. But for now, we’ll take an example
including the init () magic method and the self parameter.

1. >>> class fruit:


2. def init (self,color,size):
3. self.color=color
4. self.size=size
5. def salutation(self):
6. print(f"I am {self.color} and a size {self.size}")
7.
8. >>> orange=fruit('Orange',7)
9. >>> orange.salutation()

I am Orange and a size 7

As you can see, the init () method is equivalent to a constructor in C++ or Java. It gets called every time we
create an object of the class. Likewise, the self parameter is to tell the interpreter to deal with the current object.
This is like the ‘this’ keyword in Java. But you don’t have to call it ‘self’; you can call it anything. The object is
passed as the first argument, fitting in for ‘self’. Here, orange.salutation() translates to fruit.salutation(orange).

Also, salutation is a function object for the Python class, but a method object for the instance object ‘orange’.
1. >>> fruit.salutation

<function fruit.salutation at 0x0628FE40>

1. >>> orange.salutation

<bound method fruit.salutation of < main .fruit object at 0x062835F0>>

You can store a method object into a variable for later use.

1. >>> sayhi=orange.salutation
2. >>> sayhi()

I am Orange and a size 7

4. What is the Python Object?


Now, how useful is a Python3 class without an object? If a class is an idea, an object is its execution.

When we create an object, its init () method is called. And like we previously discussed, the object gets
passed to the class through the function with the ‘self’ keyword.

1. >>> orange=fruit('Orange',7)

Here, we passed ‘Orange’ and 7 as values for the attributes color and size. We can also declare attributes for an
object on the fly. See how.

1. >>> orange.shape='Round'
2. >>> orange.shape

‘Round’

You can assign the values of an object in python to another.

1. >>> apple=orange
2. >>> apple.color

‘Orange’

We will check objects in detail in our next tutorial.

5. Attributes Belonging to Python Class


For explaining this, we’ll redefine the Python class fruit.

1. >>> class fruit:


2. size='Small'
3. def init (self,color,shape):
4. self.color=color
5. self.shape=shape
6. def salutation(self):
7. print(f"I am {self.color} and a shape {self.shape}")

Here, the attribute ‘size’ belongs to the class, but we can call it on an object as well.

1. >>> fruit.size

‘Small’

1. >>> orange=fruit('Orange','Round')
2. >>> orange.size

‘Small’

Since we redefined the class, we declared the object again as well.

Likewise, a Python class can contain a function as well.

1. >>> class fruit:


2. size='Small'
3. def init (self,color,shape):
4. self.color=color
5. self.shape=shape
6. def salutation():
7. print(f"I am happy")
8.
9. >>> fruit.salutation()

I am happy

1. >>> fruit.salutation

<function fruit.salutation at 0x030C8390>

6. How to Delete Python Class, Attribute, and Object?


You can delete an attribute, an object, or a class using the del keyword.

1. >>> del orange.shape


2. >>> orange.shape

Traceback (most recent call last):

File “<pyshell#118>”, line 1, in <module>

orange.shape
AttributeError: ‘fruit’ object has no attribute ‘shape’

Let’s try deleting an object.

1. >>> del orange


2. >>> orange

Traceback (most recent call last):

File “<pyshell#120>”, line 1, in <module>

orange

NameError: name ‘orange’ is not defined

Finally, let’s try deleting the Python class itself.

1. >>> fruit
2. <class ' main .fruit'>
3. >>> del fruit
4. >>> fruit

Traceback (most recent call last):

File “<pyshell#133>”, line 1, in <module>

fruit

NameError: name ‘fruit’ is not defined

So, this was all about Python Class and Object Tutorial. Hope you like our explanation.

7. Conclusion
In this Python Class tutorial, we opened ourselves to Python classes and how to Python create a class. We saw
how to declare and access attributes and methods in python. A little too much to take on at once? Get practicing.
Next, we’ll talk about objects in python.

Python Object Tutorial – How to Create, Delete & Initialize


Object
by DataFlair Team · May 27, 2019

1. Python Object
Honestly, anything in python programming Language is an object. In this python object tutorial, we will
focus on what is Python object, instance Python object, and initialization. Along with this, we will cover how
to create python object, and how to delete an object in python with examples and syntax.

So, let’s Python Object Tutorial.


Python Object Tutorial – How to Create, Dthe elete & Initialize Object

2. What is Python Object?


To prove our point that everything in Python is an object, we’ll first take some example. Before this, let’s
revise Python Syntax.

>>> type(7)

<class ‘int’>

>>> a=[1,2,3]

>>> type(a)

<class ‘list’>

>>> def sayhi():

print("Hello")

>>> sayhi

<function sayhi at 0x064B7300>


>>> a=True

>>> type(a)

<class ‘bool’>

>>> type('True')

<class ‘str’>

>>> class fruit:

pass

>>> fruit

<class ‘ main .fruit’>

See the point? This is because every class is derived from the class ‘object’. Now, let’s talk about python
instance objects. An object is any real-world entity that has attributes and behaviors. Honestly, an object is to a
class as is a prototype to a blueprint.

3. Python Object Initialization


When we create object for a class, the init () method is called.

>>> class fruit:

def init (self,color,shape):

self.color=color

self.shape=shape

def sayhi(self):

print(f"Hi.\nI am {self.color}and{self.shape}")

>>> orange=fruit('Orange','Round')

>>> orange.sayhi()

Hi.

I am Orange and Round

We use this to fill in values to attributes when we create a object. Here, init () has two attributes apart from
‘self’- color and shape. Then, we pass corresponding arguments for these at the time of object creation.
You know that we use ‘self’ to be able to refer to the object we’re dealing with. Here, that is ‘orange’. We’ve
discussed this in our comprehension on Python Methods.

However, if you don’t supply the init () method, Python will use a default one for you.

>>> class Person:

def sayhi(self):

print("Hi")

>>> Hannah=Person()

>>> Hannah.sayhi()

Hi
In saying this, we conclude that init () is like a constructor in C++.

4. Assigning One Object to Another Python Object


Like we do with any other construct in Python, it is possible to assign one object to another using the
assignment operator in python.

>>> Miley=Hannah

>>> Miley.sayhi()

Hi

Are these the same? Let’s check with the id() function.

>>> id(Miley)==id(Hannah)

True

>>> id(Miley)

95812144

>>> id(Hannah)

95812144

Also, let’s check their hash values.

>>> hash(Miley)

5988259

>>> hash(Hannah)
5988259

We assigned Hannah to Miley. Now, what if we delete Hannah? How does it affect Miley?

>>> del Hannah

>>> Miley

< main .Person object at 0x05B5FA30>


As you can see, it does not.

5. Assigning Attributes to an Object on the Fly


So what, if only one object out of all, for a class needs an attribute called ‘size’? Simply assign it on the fly even
after you have already declared the python class.

>>> orange.size=7

>>> orange.size

7
This attribute belongs to ‘orange’, but not to ‘fruit’. But it is possible to assign an attribute to ‘fruit’ instead.

6. How to Delete Python Object?


Garbage collection is a very important aspect of any good programming language. Likewise, Python lets us
delete a lot of stuff, including objects. To delete an object in Python, we use the ‘del’ keyword. A when we try
to refer to a deleted object, it raises NameError.

>>> del orange

>>> orange

Traceback (most recent call last):


File “<pyshell#729>”, line 1, in <module>
orange
NameError: name ‘orange’ is not defined

7. Conclusion
Hence, we have covered the three basic concepts of python object-oriented programming, object initialization in
python, assigning one object to another object in python, assigning attributes to an object on the fly, deleting a
python object. These are python classes, python methods, and python objects. Classes and objects in Python
are very important part of Python Programming Language. Indeed, what is a class without its objects?

Furthermore, if you have any query, feel free to ask in the comment section.
Python Inheritance Tutorial- Method Overloading &
Method Overriding
by DataFlair Team · March 14, 2019

1. Python Inheritance Tutorial


In this Python tutorial, we talk about Python inheritance and types of inheritance in python with their syntax.
Moreover, we will study Python super function, Python method overriding and Python method overloading.

So, let’s start the Python Inheritance Tutorial.

Python Inheritance Tutorial- Method Overloading & Method Overriding

2. What is Inheritance in Python?


It is a universal fact that every student is a person. This is in hindsight of non-human students if any. To depict
this relationship, we take an illustration.
Python Inheritance – Illustration

The relationship from person to a student is termed ‘Specialization’. Conversely, every student is a person, this
is called Generalization. In this representation, we use an arrow towards the base class as a UML (Unified
Modeling Language) convention.
Here, Person can be called any of the following:

Super Class

Parent Class

Base Class

Likewise, Student here is:

Sub Class

Child Class

Derived Class

3. Python Inheritance Syntax


To make a class inherit from another, we apply the name of the base class in parentheses to the derived class’
definition.

>>> class Person:

pass

>>> class Student(Person):

pass
>>> issubclass(Student,Person)

True
Here, class Student inherits from class Person. Here, since we only want to focus on the python syntax, we use
the ‘pass’ statement in the bodies of the classes. Also, we use the function issubclass() to confirm that student is
a subclass of person.

4. Types of Inheritance in Python


There are five types of inheritance in python, we observe.

Types of Python Inheritance

a. Single Inheritance in Python


A single Python inheritance is when a single class inherits from a class.

>>> x=0

>>> class fruit:

def init (self):

global x

x+=1
print("I'm a fruit")

>>> class citrus(fruit):

def init (self):

super(). init ()

global x

x+=2

print("I'm citrus")

>>> x

>>> lime=citrus()

I’m a fruit
I’m citrus

>>> x

b. Python Multiple Inheritance


Multiple Python inheritance are when a class inherits from multiple base classes.

>>> class Color:

pass

>>> class Fruit:

pass

>>> class Orange(Color,Fruit):

pass

>>> issubclass(Orange,Color) and issubclass(Orange,Fruit)

True
c. Multilevel Inheritance in Python
When one class inherits from another, which in turn inherits from another, it is multilevel python inheritance.

>>> class A:

x=1

>>> class B(A):

pass

>>> class C(B):

pass

>>> cobj=C()

>>> cobj.x

d. Hierarchical Inheritance in Python


When more than one class inherits from a class, it is hierarchical Python inheritance.

>>> class A:

pass

>>> class B(A):

pass

>>> class C(A):

pass

>>> issubclass(B,A) and issubclass(C,A)

True

e. Hybrid Inheritance in Python


Hybrid Python inheritance is a combination of any two kinds of inheritance.

>>> class A:
x=1

>>> class B(A):

pass

>>> class C(A):

pass

>>> class D(B,C):

pass

>>> dobj=D()

>>> dobj.x

5. Python Inheritance Super Function –


Super()
With inheritance, the super() function in python actually comes in quite handy. It allows us to call a method
from the parent class. Let’s define a new class for this.

>>> class Vehicle:

def start(self):

print("Starting engine")

def stop(self):

print("Stopping engine")

>>> class TwoWheeler(Vehicle):

def say(self):

super().start()

print("I have two wheels")

super().stop()

>>> Pulsar=TwoWheeler()

>>> Pulsar.say()
Starting engine
I have two wheels
Stopping engine

6. Python Override Method


A subclass may change the functionality of a Python method in the superclass. It does so by redefining it. This
is termed python method overriding. Lets see this Python Method Overriding Example.

>>> class A:

def sayhi(self):

print("I'm in A")

>>> class B(A):

def sayhi(self):

print("I'm in B")

>>> bobj=B()

>>> bobj.sayhi()

I’m in B

7. Python Method Overloading


Before we say anything, we’d like you to take a look at the following code:

>>> def add(a,b):

return a+b

>>> def add(a,b,c):

return a+b+c

>>> add(2,3)

Traceback (most recent call last):


File “<pyshell#8>”, line 1, in <module>
add(2,3)
TypeError: add() missing 1 required positional argument: ‘c’
What looks like overloading methods, it is actually that Python keeps only the latest definition of a method you
declare to it. This code doesn’t make a call to the version of add() that takes in two arguments to add. So we
find it safe to say Python doesn’t support method overloading. However, we recently ran into a rather Pythonic
way to make this happen. Check this out:

>>> def add(instanceOf,*args):

if instanceOf=='int':

result=0

if instanceOf=='str':

result=''

for i in args:

result+=i

return result

In this code, not only do we use the *args magic variable for variable arity, we also let the code deal with both
integers and strings. Watch it happen:

>>> add('int',3,4,5)

12

>>> add('str','I ','speak ','Python')

‘I speak Python’
You say what if I do this?:

>>> def add(a,b,c=0):

return a+b+c

>>> add(2,3)

5
To that, we’ll say this isn’t method overloading, this is simply used of default arguments.

8. Conclusion
Of all things good, Python inheritance saves us time, effort, and memory. In this tutorial, we looked at Python
inheritance syntax, inheritance types, Python method overloading, method overriding in python and python
super functions. Tell us in a comment box, if something is missing.
Python Multiple Inheritance – Python MRO (Method
Resolution Order)
by DataFlair Team · September 25, 2018

1. Python Multiple Inheritance & MRO


In our last article, we discussed Python inheritance. Here, Python Multiple Inheritance tutorial, we will discuss
what is Multiple inheritances in Python with its examples and uses. On the other hand, we will learn Python
MRO (Method Resolution Order). Atlast, we will learn complications in Multiple Inheritance in Python
Programming Lanaguge.

So, let’s start Python Multiple Inheritance Tutorial.

Python Multiple Inheritance

2. What is Python Multiple Inheritance?


As its name is indicative, multiple inheritance in python is when a class inherits from multiple classes. One
example of this would be that a child inherits personality traits from both parents.
Python Multiple Inheritance – Example

Before we proceed to multiple inheritance syntaxes, let’s see the python syntax.

3. Python Multiple Inheritance Syntax


To make a class inherit from multiple python classes, we write the names of these classes inside the
parentheses to the derived class while defining it. We separate these names with commas.
The code for the previous example would be:

>>> class Mother:

pass

>>> class Father:

pass

>>> class Child(Mother,Father):

pass

>>> issubclass(Child,Mother) and issubclass(Child,Father)

True

4. Python MRO (Method Resolution Order)


When we search for an attribute in a class that is involved in python multiple inheritance, an order is followed.
First, it is searched in the current class. If not found, the search moves to parent classes. This is left-to-right,
depth-first.

So, in the above class, the search order will be – Child, Mother, Father, Object.

This order is called linearization of class Child, and the set of rules applied are called MRO (Method Resolution
Order). To get the MRO of a class, you can use either the mro attribute or the mro() method.

>>> Child. mro

(<class ‘ main .Child’>, <class ‘ main .Mother’>, <class ‘ main .Father’>, <class ‘object’>)
The mro attribute returns a tuple, but the mro() method returns a python list.

>>> Child.mro()

[<class ‘ main .Child’>, <class ‘ main .Mother’>, <class ‘ main .Father’>, <class ‘object’>] To take a
more complex example that also demonstrates depth-first search, we take 6 classes.
>>> class X:pass

>>> class Y: pass

>>> class Z:pass

>>> class A(X,Y):pass

>>> class B(Y,Z):pass

>>> class M(B,A,Z):pass

>>> M.mro()

[<class ‘ main .M’>, <class ‘ main .B’>, <class ‘ main .A’>, <class ‘ main .X’>, <class
‘ main .Y’>, <class ‘ main .Z’>, <class ‘object’>] We can represent this with the following diagram.

Python Multiple Inheritance – Method Resolution order (MRO)

First, the interpreter scans M. Then, it scans B, and then A-B first because of the order of arguments at the time
of inheritance. It scans Z later, after X and Y. The order is- X, then Y, then Z. This is because due to depth-first
search, X comes first to A. Finally, it scans the class object. Hence, the order.

5. Complications in Python Multiple


Inheritance
What happens when the classes we inherit from all have a common attribute? Whose value does the child class
take, then? Let’s take three classes A, B, and C.

>>> class A:

id=1
>>> class B:

id=2

>>> class C:

id=3

>>> class M(A,C,B):

pass

>>> M.id

>>> M.id

>>> class M(C,B,A):

pass

>>> M.id

3
Like we see here, the class named first in the inheritance passes its value to the child class for the common
attribute. Earlier, it was A, so, M had id=1. Then, when we changed it to C, M got id=3. This is the same with
Python methods/functions of the class.

>>> class A:

def sayhi():

print("A")

>>> class B:

def sayhi():

print("B")

>>> class M(A,B):

pass

>>> M.sayhi()

A
So, this was all about Python Multiple Inheritance Tutorial. Hope you like our explanation on Python MRO.

6. Conclusion
Hence, in this tutorial, we discussed python multiple inheritances and its syntax and examples. We also talked
about Method Resolution Order(MRO) and last we discuss, complications in multiple inheritances in python.
Keep going with us in Python and give us your valuable feedbacks.

7 Major Python Compilers and Interpreters – Free Online


Available
by DataFlair Team · September 28, 2018

1. Online Python Compilers


In our last tutorial, we studied Python Interpreters. Today, we will talk about Python Compilers, we discuss
different compilers and interpreters available for Python
Programming: CPython, Jython, IronPython, ActivePython, Nuitka, PyJS, and Stackless Python.

So, let’s start the list of available Python Compilers and Python Interpreters.

7 Major Python Compilers and Interpreters – Free Online Available

2. Python Compilers and Interpreters


Today, we will share with you a list of various Python interpreters and compilers available for Python. You may
have heard that Python is an interpreted language. Well, this is the way it goes.

A compiler converts the .py source file into a .pyc bytecode for the Python virtual machine.

A Python interpreter executes this bytecode on the virtual machine.


Let’s look at what compilers and interpreters are available for Python.
Do you know the Difference between Scala Vs Python

3. CPython
This is the default and most widely-used implementation of Python and is written in C. This is an interpreter
and has a foreign function interface with languages like C. Here’s a brief on it-

Developers- Python code developers, the Python community

Stable Release- 3.6.5; March, 2018 and 2.7.15; May, 2018

Written in- C

Type- Python programming language interpreter

Python Compilers and Interpreters-CPython

a. Design of CPython
Each CPython interpreter for Python, the process uses a GIL(Global Interpreter Lock). This serves as a
limitation as it disables concurrent Python threads for a process. Another problem is that to achieve
concurrency, you must manage separate CPython interpreter processes with a multitasking OS. This also makes
it harder for concurrent CPython processes to communicate.
For more on this, you can refer to www.cython.org.

4. Jython
Jython is JPython’s successor. It is an implementation of Python that runs on the Java platform. Here’s a brief-

First Release- January 2001

Stable Release- July 2017, version 2.7.1

Written in- Python programming, Java

Type- Python programming language interpreter


Python Compilers and Interpreters – Jython

Jython takes Python code and compiles it to Java bytecode. This means we can run Python on any machine that
runs a JVM (Java Virtual Machine). Jython supports static and dynamic compilation and let’s extend Java
classes.
For, more on this, refer to www.jython.org.
Follow this link to know about Python Iterator

5. IronPython
This is a Python implementation around the .NET Framework and Mono. Here’s a brief:

Author- Jim Hugunin

First Release- September 2006

Stable Release- February, 2018; version 2.7.8

Written in- C#

Type- Python programming language implementation

Python Compilers and Interpreters – IronPython

IronPython supports dynamic compilation and an interactive console. Python scripts are capable of interacting
with .NET objects.
For more on this, you can refer to ironpython.net.

6. ActivePython
ActivePython is a Python distribution from ActiveState. It makes installation easy and cross-platform
compatibility possible. Apart from the standard libraries, it has many different modules.
Python Compilers and Interpreters – ActivePython

7. Nuitka
Nuitka, source-to-source Python compilers that take Python code and compiles it to C/C++ executables or
source code. Let’s take a brief:

Developers- Kay Hayen, others

Stable Release- October, 2017; version 0.5.28

Type- Source-to-source compiler

Python Compilers and Interpreters – Nuitka

Even when you don’t run Python on your machine, you can create standalone programs with Nuitka.
Read about Python Zipfile – Benefits, Modules

8. PyJS
PyJS is an internet application framework that will let you use Python to develop client-side web and desktop
applications. You can run such an application in a web browser and also as a standalone desktop application.
Python Compilers and Interpreters – PyJS

Let’s take a brief:

Author- James Tauber

First Release- March 2007

Stable Release- May, 2012; version 0.8.1

Written in- Python, JavaScript

Earlier, it was called Pyjamas. It translates your Python code into JavaScript to let it run in a browser. PyJS
ships with an AJAX framework and a Widget Set API.
For more on this, refer to www.pyjs.org.

9. Stackless Python
Stackless Python is a Python interpreter. It is ‘stackless’ because it doesn’t depend on the C call for its stack. It
uses the C stack and clears it between calls.

Python Compilers and Interpreters – Stackless Python

Here’s a brief:

Author- Christian Tismer

First Release- 1998

Stable Release- September, 2017; 3.5.4-slp

Written in- C, Python

Type- Interpreter

Stackless Python also supports threads and microthreads. Other than that, it provides tasklets, round-robin
scheduling, serialization, and pre-compiled binaries.
So, this was all about Python Interpreter and Compilers. Hope you like our explanation.
10.Conclusion
Hence, we studied a list of Python Compiler and Interpreter are available for Python Programming. With that,
we have covered every popular Python compilers/interpreter. Do you want to add more to the list? Drop it in the
comments.

Python Zipfile – Benefits, Modules, Objects in


Zipfiles in Python
by DataFlair Team · September 27, 2018

1. Python Zipfile – Objective


In our previous tutorial, we studied Image Processing with Scipy and NumPy in Python and in this article,
we will learn about Python Zipfile. Moreover, we will see how we can extract Zipfile in Python. In addition, we
will cover how can we write Python Zipfile, and getting information about them using Python. At last, we will
see some methods of the Python Zipfiles module provides and exceptions.

So, let’s begin Python Zipfile Tutorial.

Python Zipfile – Benefits, Modules, Objects in Zipfiles in Python

2. What is Python Zipfile?


Python ZIPfile is an ideal way to group similar files and compress large files to reduce their size. The
compression is lossless. This means that using the compression algorithm, we can operate on the compressed
data to perfectly reconstruct the original data. So, in Python zipfile is an archive file format and a compression
standard; it is a single file that holds compressed files.
Do you know about Python Forensics – Hash Function, Virtualization & much more

3. Advantages of Python Zipfiles


Bunching files into zips offer the following advantages:
1. It reduces storage requirements
Since ZIP files use compression, they can hold much more for the same amount of storage
2. It improves transfer speed over standard connections
Since it is just one file holding less storage, it transfers faster
Now, let’s learn about the module zipfile.

4. Python Zipfile Module


The Python zipfile module has tools to create, read, write, append, and list ZIP files. At the time of writing. It
lets us handle ZIP files that use ZIP64 extensions and decrypt encrypted files in ZIP archives. It cannot handle
multi-disk ZIP files or create encrypted files.

Python Zipfile Module has the following members:

a. exception zipfile.BadZipFile

For a bad ZIP file, it raises this exception.

b. exception zipfile.BadZipfile

This is an alias for the previous exception in the list. It is to make it compatible with older Python versions. This
is deprecated since version 3.2.

Let’s Discuss Errors and Exceptions in Python Programming

c. exception zipfile.LargeZipFile

When a Python ZIPfile needs ZIP64 functionality, but it hasn’t been enabled, Python throws this exception.

d. class zipfile.ZipFile

This is the class for reading and writing ZIP files in Python.

e. class zipfile.PyZipFile

This class lets us create ZIP archives holding Python libraries.

f. class zipfile.ZipInfo(filename=’NoName’, date_time=(1980, 1, 1, 0, 0, 0))

With this class, we can represent information about an archive member. The getinfo() and infolist() methods of
Python ZipFile objects return instances of this class.
g. zipfile.is_zipfile(filename)

This considers the magic number of a ZIP file. If it is a valid ZIP file, it returns True; otherwise, False. This
works on files and file-like objects.

h. zipfile.ZIP_STORED

This is the numeric constant for uncompressed archive members.

i. zipfile.ZIP_DEFLATED

This is the numeric constant for the usual ZIP compression method. It needs the zlib module.

j. zipfile.ZIP_BZIP2

This is the numeric constant for the BZIP2 compression method. It needs the bz2 module.

k. zipfile.ZIP_LZMA

This is the numeric constant for the LZMA compression method. It needs the lzma module.
Read about Exception Handling in Python for Python Programming

5. Python ZipFile Objects


Python zipfile class this type:

a. class zipfile.ZipFile(file, mode=’r’, compression=ZIP_STORED, allowZip64=True)

This method opens a Python ZIPfile. Here, file may be a file-like object or a string path to a file. We have the
following modes:

‘r’- To read an existing file

‘w’- To truncate and write a new file

‘a’- To append to an existing file

Using the compression argument, we can select the compression method to use when writing the archive.
allowZip64 is True by default. This creates ZIP files that use ZIP64 extensions for zipfiles larger than GiB.

ZipFile has the following functions:

i. ZipFile.close()

This closes the archive file. If we do not call this before exiting the program, Python doesn’t write the records
intended to.

b. ZipFile.getinfo(name)

This returns a ZipInfo object holding information about the archive member name.
c. ZipFile.infolist()

This returns a list holding a ZipInfo object for each archive member.

Let’s revise Python Classes and Object Oriented Programming

d. ZipFile.namelist()

This returns a list of archive members by name.

e. ZipFile.open(name, mode=’r’, pwd=None)

This function extracts a member from the archive as a file-like object (CipExtFile). The mode can be ‘r’, ‘U’, or
‘rU’. pwd is the password for an encrypted file. name is a filename in the archive or a ZipInfo object.

Since it is also a context manager, we can use it with the ‘with’ statement:

with ZipFile(‘spam.zip’) as myzip:

with myzip.open(‘eggs.txt’) as myfile:

print(myfile.read())

f. ZipFile.extract(member, path=None, pwd=None)

This extracts a member from the archive to the current working directory. member may be a filename or a
ZipInfo object, path is a different directory to extract to, and pwd is the password for an encrypted file.

g. ZipFile.extractall(path=None, members=None, pwd=None)

This extracts all members from the archive to the current working directory. The arguments mean the same as
above.

h. ZipFile.printdir()

This prints a table of contents for the archive to sys.stdout.

i. ZipFile.setpassword(pwd)

This sets pwd as the default password to extract encrypted files.

j. ZipFile.read(name, pwd=None)

This returns the bytes of name in the archive, where name is the name of a file in the archive, or of a ZipFile
object.

Let’s Know about Python Function Arguments with Types, Syntax and Examples

k. ZipFile.testzip()
This checks the CRCs and file headers for all files in the archive, and returns the name of the first bad file. If
there is none, it returns None.

l. ZipFile.write(filename, arcname=None, compress_type=None)

This writes the file filename to the archive, calling it arcname.

m. ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])

This writes the string bytes to the archive

We also have some data attirbutes:

ZipFile.debug

This denotes the level of debug output to use. 0 means no output (default) and 3 means the most output.

ZipFile.comment

This is the comment text linked with a Python ZIPfile.

6. Extracting ZIP Files in Python


Now, let’s try it hands-on. Let’s try extracting a Python Zipfile.

>>> from zipfile import ZipFile

>>> import os

>>> os.chdir("C:\\Users\\lifei\\Desktop")

>>> file="Demo.zip"

>>> with ZipFile(file,'r') as zip: #ZipFile constructor; READ mode; ZipFile object named as zip

zip.printdir() #To print contents of the archive

print("Extracting files")

zip.extractall() #Extract contents of the ZIP to the current working directory

print("Finished extracting")

File Name Modified Size

Demo/1.txt 2018-06-15 17:40:06 0

Demo/2.txt 2018-06-15 17:40:12 0


Demo/3.txt 2018-06-15 17:40:16 0

Extracting files

Finished extracting

As you can see, this extracts all files in the ZIP Demo.zip. It creates a folder labeled ‘Demo’ on the Desktop.
We explain the code through comments. You can also extract just a single file using the method extract():

>>> with ZipFile(file,'r') as zip:

zip.extract('Demo/2.txt')

‘C:\\Users\\lifei\\Desktop\\Demo\\2.txt’

This creates a folder on the Desktop labeled ‘Demo’. But this time, it only contains one file- ‘2.txt’.
Let’s Discuss Python Read And Write File – File Handling In Python

7. How to Write Python ZIP File?


We use the write() method to write to a ZIP. Here’s the code we use:

>>> from zipfile import ZipFile

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>>

>>> def get_paths(directory):

paths=[]

for root, directories, files in os.walk(directory):

for filename in files:

filepath=os.path.join(root,filename)

paths.append(filepath)

return paths

>>>

>>> directory='./Demo'

>>> paths=get_paths(directory)
>>> print("Zipping these files:")

Zipping these files:

>>> for file in paths:

print(file)

./Demo\1.txt

./Demo\2.txt

./Demo\3.txt

>>> with ZipFile('Demo.zip','w') as zip:

for file in paths:

zip.write(file)

>>> print("Zip successful")

Zip successful
Now let’s see how this works:
We create a function with uses the method os.walk(). In every iteration, it appends the files in that directory to
the list paths. Then, we get a list of the file paths bypassing the Demo directory’s path to the function
get_paths(). Then, we create a ZipFile object in WRITE mode. Finally, we use the write() method to write all
these files to the ZIP.

8. Getting Information about ZIP Files in


Python
To find out more about a Python zipfile, we use the method infolist(). Let’s see how:

>>> from zipfile import ZipFile

>>> import datetime

>>> file="Demo.zip"

>>> with ZipFile(file,'r') as zip:

for info in zip.infolist():

print(info.filename)

print('\tModified:\t'+str(datetime.datetime(*info.date_time)

print('\tSystem:\t\t'+str(info.create_system)+'(0=Windows,3=Unix)')
print('\tZIP version:\t'+str(info.create_version))

print('\tCompressed:\t'+str(info.compress_size)+' bytes')

print('\tUncompressed:\t'+str(info.file_size)+' bytes')

Demo/1.txt
Modified: 2018-06-15 17:56:32
System: 0(0=Windows,3=Unix)
ZIP version: 20
Compressed: 0 bytes
Uncompressed: 0 bytes
Demo/2.txt
Modified: 2018 06-15 17:57:18
System: 0(0=Windows,3=Unix)
ZIP version: 20
Compressed: 29 bytes
Uncompressed: 40 bytes
Demo/3.txt
Modified: 2018 06-15 17:56:42
System: 0(0=Windows,3=Unix)
ZIP version: 20
Compressed: 0 bytes
Uncompressed: 0 bytes

Read about Python File I/O – Python Write to File and Read File

Here, we use the method infolist() to create an instance of the ZipInfo class that holds all information about the
Python zipfile. It lets us access information like file names, a system where the file was created, file
modification data, ZIP version, size of files, and so.

So, this was all about Python Zipfile Tutorial. Hope you like our explanation.

9. Conclusion
Hence, like we’ve always said, there are so many things you can do with Python. Using the Python zipfile
module, we can even handle ZIP files. Tell us what you think about Python Zipfile in the comments.

Python File i/o – Python Write to File and Python Read File
by DataFlair Team · September 26, 2018

1. Python File i/o & File Read and Write


An important component of an operating system is its file and directories. We’ve talked about Python
Directories. In that, we learned to manipulate (create, rename, delete,..) directories. Today, we will talk about
Python file I/O. After this lesson, you will be able to Python open file, Python close file, Python read file, and
Python writes to file. Let’s begin.
Python File I/O – Introduction

2. What is Python File?


A file is a location on disk that stores related information and has a name. A hard-disk is non-volatile, and we
use files to organize our data in different directories on a hard-disk. The RAM (Random Access Memory) is
volatile; it holds data only as long as it is up. So, we use files to store data permanently. To read from or write to
a file, we must first open it. And then when we’re done with it, we should close it to free up the resources it
holds (Open, Read/Write, Close).

3. Python Open File


To start Python file i/o, we deal with files and have a few in-built functions and methods in Python. To open a
file in Python, we use the read() method.

But first, let’s get to the desktop, and choose a file to work with.

>>> import os

>>> os.getcwd()

‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32’

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> os.listdir()

[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Backup iPhone7+ 20-1-18’, ‘Burn Book.txt’, ‘ch’, ‘desktop.ini’,
‘dmkidnap.png’, ‘Documents’, ‘Eclipse Cpp Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’,
‘gifts.jpg’, ‘Items for trip.txt’, ‘Major temp’, structure’, ‘office temp.jpg’, ‘Papers’, ‘Remember to
remember.txt’, ‘To do.txt’, ‘Today.txt’] If this seems new to you, be sure to check out Python Directory.
Now, let’s open Python file ‘To do.txt’.

>>> open('To do.txt')

<_io.TextIOWrapper name=’To do.txt’ mode=’r’ encoding=’cp1252′>

But to work with this, we must store it into a Python variable. Let’s do this.

>>> todo=open('To do.txt')

>>> todo

<_io.TextIOWrapper name=’To do.txt’ mode=’r’ encoding=’cp1252′>

We wouldn’t have to change directory if we just passed the full path of Python file to open(). But let’s work
with this for now.

Follow this link to know more about Python Functions

Python File I/O – Python Open File

a. Python File Modes


While opening Python file, we can declare our intentions by choosing a mode. We have the following modes:

Mode Description

r To read a file (default)

To write a file; Creates a new file if it doesn’t exist,


w
truncates if it does

x Exclusive creation; fails if a file already exists


a To append at the end of the file; create if doesn’t exist

t Text mode (default)

b Binary mode

+ To open a file for updating (reading or writing)

Let’s take a couple of examples.

>>> todo=open('To do.txt','r+b') #To read and write in binary mode

>>> todo=open('To do.txt','a')

b. Choosing Your Encoding


Also, it is good practice to specify what encoding we want, because different systems use a different encoding.
While Windows uses ‘cp1252’, Linux uses ‘utf-8’.

>>> todo=open('To do.txt',mode='r',encoding='utf-8')

c. When Python File Doesn’t Exist


Finally, if you try opening Python file that doesn’t exist, the interpreter will throw a FileNotFoundError.

>>> todo=open('abc.txt')

Traceback (most recent call last):

File “<pyshell#27>”, line 1, in <module>

todo=open(‘abc.txt’)

FileNotFoundError: [Errno 2] No such file or directory: ‘abc.txt’

Tell us how do you like the Python Open file Explanation.

4. Python Close File


When we tried to manually go rewrite the Python file, it threw this error dialog when we attempted to save it.
So, remember, always close what you open:

>>> todo.close()
Python File I/O – Python Close File

a. Try..finally in Python
But if an exception occurs in the middle of our code, the file remains open, and the resources aren’t freed. To
take care of these situations, we put the close() method in the finally-block.

>>> try:

f=open('To do.txt')

print("Before")

print(1/0)

finally:

f.close()

Before

Traceback (most recent call last):

File “<pyshell#51>”, line 4, in <module>

print(1/0)

ZeroDivisionError: division by zero

b. With
If you think having to put close() every time you’re done with a Python file is bunk, use the ‘with’ statement.
>>> with open('To do.txt') as f:

f.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam’


With this, it will close the file implicitly as soon as it finishes executing the statements under the block.

5. Python Read File


To read the contents of a Python file, we can do one of these:

Python File I/o – Python Read File

a. The read() Method


We can use the read() method to read what’s in a Python file.

>>> with open('To do.txt') as todo:

todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for examLearn to cook\nLearn to cook’


When we provide an integer argument to read(), it reads that many characters from the beginning of the Python
file.

>>> todo=open('To do.txt')


>>> todo.read(5)

‘Get g’
Now when we call read() without any arguments, it reads the rest of the Python file.

>>> todo.read()

‘roceries\nOrganize room\nPrint labels\nWrite article\nStudy for examLearn to cook\nLearn to cook’


Notice that it prints a ‘\n’ for a newline. And when we yet read it, it prints an empty string, because the cursor
has reached the end of the Python file.

>>> todo.read()

''

>>> todo.close()

b. Seek() and tell()


Okay, jokes apart, these two methods let us reposition the cursor and find its position. tell() tells us where the
cursor is.

>>> todo=open('To do.txt')

>>> todo.read(5)

‘Get g’

>>> todo.tell()

5
seek() takes an integer argument, and positions the cursor after that many characters in the Python file. Along
with that, it returns this new position of the cursor.

>>> todo.seek(0)

>>> todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for examLearn to cook\nLearn to cook’


seek() and tell() are like seekg(), seekp(), tellg(), and tellp() in C++.

c. Using Python For-loop


We can simply use a for-loop to iterate on a file. This is the beauty of Python- it simplifies everything.

>>> for line in open('To do.txt'):

print(line,end='')
Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cook
We used the ‘end’ parameter to prevent the interpreter from adding extra newlines to each output. Otherwise,
the output would have looked like this:

>>> for line in open('To do.txt'):

print(line)

Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cook

d. Readline()
Alternatively, the method readline() lets us read one line at a time. Simply put, the interpreter stops after every
‘\n’.

>>> todo=open('To do.txt')

>>> todo.readline()

‘Get groceries\n’

>>> todo.readline()

‘Organize room\n’

>>> todo.readline()

‘Print labels\n’

>>> todo.readline()

‘Write article\n’

>>> todo.readline()

‘Study for exam\n’

>>> todo.readline()

‘Learn to cook’
>>> todo.readline()

>>> todo.readline()

e. Readlines()
Lastly, the readlines() method reads the rest of the lines/file.

>>> todo.seek(0)

>>> todo.read(5)

‘Get g’

>>> todo.readlines()

[‘Groceries\n’, ‘Organize room\n’, ‘Print labels\n’, ‘Write article\n’, ‘Study for exam\n’, ‘Learn to cook’]

6. Python Write to File


To write a Python file, we use the write() method.

>>> todo=open('To do.txt')

>>> todo.write("HI")

Traceback (most recent call last):

File “<pyshell#56>”, line 1, in <module>

todo.write(“HI”)

io.UnsupportedOperation: not writable

Here, we did not open the Python file in a writable format. Let’s open it properly.

>>> todo=open('To do.txt','a')

>>> todo.write('\nLearn to cook')

14

>>> todo.close()
When we checked in the file (refreshed it), we found:
Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cook
Concluding, you can use ‘w’, ‘a’, or ‘x’. The ‘w’ erases the content and writes over it. So, be careful with it.
Also, write() returned 14 here because it appended 14 characters to Python file.
But you can’t read a Python file if you open it in ‘a’ mode.

>>> todo=open('To do.txt','a')

>>> todo.read()

Traceback (most recent call last):

File “<pyshell#13>”, line 1, in <module>

todo.read()

io.UnsupportedOperation: not readable

To get around this, you’d have to use ‘a+r’.

So, this was all about Python File I/O Tutorial. Hope you like our explanation.

7. Conclusion
In this Python file i/o tutorial, we saw a few Python functions and methods like read(), write(), readline(),
readlines(), seek(), and tell(). Now, you’re able to manipulate files on a rudimentary level. Go ahead and
practice, and come back again tomorrow for more. Furthermore, if you feel any query, feel free to ask in the
comment section.

File Handling In Python – Python Read And Write File


by DataFlair Team · September 26, 2018

1. File Handling in Python

Yesterday, we told you about File I/O in Python. Today we will see File Handling in Python in which we
study: Python Read file, Python Write File, Python Open File, and Python Close File. Along with this, we will
learn Python File Methods with their syntax and examples.

So, let’s start exploring File Handling in Python.


File Handling in Python – Python Read And Write File

2. A Revision – Python Read and Write File

In our previous article, we saw Python open, close, read, and write to file. Let’s take examples for each of
Python Read and Write File.

Let’s revise Python Syntax & Python Dictionaries

a. Python Open File


In Python, to open a file, we use the open() method.

>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','r+')

We may choose out of many opening modes for a file:

Mode Description
r To read a file (default)
w To write a file; Creates a new file if it doesn’t exist, truncates if it does
x Exclusive creation; fails if file already exists
a To append at the end of file; create if doesn’t exist
t Text mode (default)
b Binary mode
+ To open a file for updating (reading or writing)

b. Python Close File


To close a file in Python, we use the close() method.

>>> todo.close()
A safer practice is to put it inside a try..finally block.

>>> try:

todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','r+')

print(1/0)

finally:

todo.close()

Traceback (most recent call last):

File “<pyshell#127>”, line 3, in <module>

print(1/0)

ZeroDivisionError: division by zero

We can also use the with-statement so that the file automatically closes as the code under it finishes executing.

>>> with open('C:\\Users\\lifei\\Desktop\\To do.txt','r+') as todo:

todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cook’


To know more about Python Errors and Error & Exception Handling in Python, follow these link.

c. Python Read File


To read from a file in Python, we call the read() method on the file object.

>>> type(todo)

<class ‘_io.TextIOWrapper’>

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','r')

>>> todo.read(3)

‘Get’

>>> todo.read()

‘ groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cook’

>>> todo.read()


When we provide an argument to read(), it reads that many characters. After that, calling read() reads from
where the cursor is right now. To find the cursor’s position, and to reposition it, we use the seek() and tell()
methods respectively.

>>> todo.tell()

88

>>> todo.seek(8)

>>> todo.read()

‘eries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cook’


Python readline() and readlines()
The method readline() reads one line at a time.

>>> todo.seek(0)

>>> todo.readline()

‘Get groceries\n’

>>> todo.readline()

‘Organize room\n’

>>> todo.readline()

‘Print labels\n’
Now, the readlines() method prints the rest of the file.

>>> todo.readlines()

[‘Write article\n’, ‘Study for exam\n’, ‘Learn to cook’] Otherwise, we can also iterate on a file using a for-loop.

>>> todo.seek(0)

>>> for line in todo:

print(line,end='')

Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cook

d. Python Write to File


To be able to write to a file in Python, we must first open it in ‘a’, ‘w’, or ‘x’ mode. Refer to the table above to
find out more about these.
Let’s first close the file.

>>> todo.close()

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','a')

>>> todo.write('Write to the end of the file')

28

>>> todo.close()

>>> with open('C:\\Users\\lifei\\Desktop\\To do.txt','r') as todo:

todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cookWrite to the end of
the file’

Any Doubt yet in File handling in Python? Please comment.

3. Python Read and Write File – Python File Methods

Let’s now look at some methods to deal with Python Read and Write File.

Python Read and Write File – Python File Methods


a. close() in Python
With close(), we close a file to free up the resources held by it.

>>>todo.close()

You must always close a file after you’re done working with it. Check section 2b above.

b. detach() in Python
This detaches the underlying binary buffer from TextIOBase and returns it.

>>> todo.detach()

<_io.BufferedRandom name=’C:\\Users\\lifei\\Desktop\\To do.txt’>

>>> todo.read()

Traceback (most recent call last):

File “<pyshell#171>”, line 1, in <module>

todo.read()

ValueError: underlying buffer has been detached


After detaching from the buffer, when we try to read the file, it raises a ValueError.

c. fileno() in Python
fileno() returns a file descriptor of the file. This is an integer number.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt')

>>> todo.fileno()

>>> todo.fileno()

>>> myfile=open('C:\\Users\\lifei\\Desktop\\Today.txt')

>>> myfile.fileno()

>>> gifts=open('C:\\Users\\lifei\\Desktop\\gifts.jpg')

>>> gifts.fileno()
5

>>> one=open('C:\\Users\\lifei\\Desktop\\1.txt')

>>> one.fileno()

>>> one.read()

d. flush() in Python
flush() writes the specified content from the program buffer to the operating system buffer in event of a power
cut.

>>> todo.flush()

>>>

e. isatty() in Python
This method returns True if the file is connected to a tty-like device.

>>> todo.isatty()

False

f. read(n) in Python
This lets us read n characters from a file.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt')

>>> todo.read(5)

‘Get g’

g. readable() in Python
This returns True if the object is readable.

>>> todo.readable()

True

>>> todo.close()

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','a')


>>> todo.readable()

False

>>> todo.close()

h. readline(n=-1) in Python
readline() reads the next line.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt')

>>> todo.readline()

‘Get groceries\n’

>>> todo.readline(5)

‘Organ’

>>> todo.readline(2)

‘iz’

>>> todo.readline()

‘e room\n’

>>> todo.readline(5)

‘Print’

>>> todo.readline(-1)

‘ labels\n’

i. readlines() in Python
This one reads the rest of the lines.

>>> todo.seek(0)

>>> todo.readlines(0)

[‘Get groceries\n’, ‘Organize room\n’, ‘Print labels\n’, ‘Write article\n’, ‘Study for exam\n’, ‘Learn to
cookWrite to the end of the fileHi’]

>>> todo.seek(0)

>>> todo.readlines(1)
[‘Get groceries\n’]

j. seek() in Python
seek() lets us reposition the cursor to the specified position.

>>> todo.seek(3)

k. seekable() in Python
This returns True if the file stream supports random access.

>>> todo.seekable()

True

l. tell() in Python
tell() tells us the current position of the cursor.

>>> todo.tell()

118

m. truncate() in Python
truncate() resizes the file. You must open your file in a writing mode for this.
We have To do.txt sized 118 bytes currently. Let’s resize it to 100 bytes.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','a')

>>> todo.truncate(100)

100
This returned the new size. We even went to our desktop and checked, it indeed resized it. But in doing this, it
truncated some of the content of the file from the end.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt')

>>> todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cookWrite to the’


When we provide no argument, it resizes to the current location.

n. writable() in Python
This returns True if the stream can be written to.
>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt')

>>> todo.writable()

False

o. write(s) in Python
This method takes string ‘s’, and writes it to the file. Then, it returns the number of characters written.

>>> todo=open('C:\\Users\\lifei\\Desktop\\To do.txt','a')

>>> todo.write('\nWrite assignment')

17

>>> todo.close()

>>> with open('C:\\Users\\lifei\\Desktop\\To do.txt') as todo: todo.read()

‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cookWrite to the\nWrite


assignment’

p. writelines() in Python
writelines() writes a list of lines to a file.

>>> with open('C:\\Users\\lifei\\Desktop\\To do.txt','a') as todo:

todo.writelines([‘\nOne’,’\nTwo’,’\nThree’])

>>> with open('C:\\Users\\lifei\\Desktop\\To do.txt') as todo:

print(todo.read())

Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cookWrite to the
Write assignment
One
Two
Three

Let’s look at Python Operators & Python Collection Modules

So this was all about file handling in Python and Python read and write file.

4. Conclusion
After this article, we hope you’ve revised Python Read and Write File, what we saw in the previous one, and
learned something new. If you can’t understand something in File Handling in Python , ask us in the comments.
Hope you like the File Handling in Python tutorial.

How Python Copy a File – 9 Simple & Quick Ways


by DataFlair Team · September 27, 2018

1. How Python Copy a File?

In our last tutorial, we studied Python Zipfile. Today, in this Python Tutorial, we will discuss how python copy
a file. Moreover, we will look at the 9 simple ways to copy a file in Python Programming: Using Python OS
Module, Python Threading Library, Python Subprocess Module, Python Shutil Module.

So, let’s start How Python Copy a File.

How Python Copy a File – 9 Simple & Quick Ways

2. How Python Copy a File?

Here, are main 4 categories of ways through which Python Copy a file.

a. Using Python OS Module


There are two ways to copy a file in Python- the popen() method and the system() method. Let’s discuss them.
How Python Copy a File – 9 Simple & Quick Ways

i. popen()
The popen() method returns a file object that connects to a pipe. In mode ‘w’, we can write to it, and in mode
‘r’, we can read from it (‘r’ is the default). You can say that it creates a pipe to a command or from it.
Read About Python Directory and Files Management
This is the syntax we use:

os.popen(command[, mode[, bufsize]])

Here, command is the command we use, and mode is ‘r’ or ‘w’, as discussed previously. When bufsize is 0,
there is no buffering. When it is 1, there is line buffering when accessing a file. When it is an integer greater
than 1, there is buffering with the indicated buffer size. For a negative value, a default buffer size is used.
We have a file labeled ‘1.txt’ on the Desktop. We use the following code:

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> os.popen('copy 1.txt 2.txt')

As you can see, this creates a new file labeled ‘2.txt’ on the Desktop. This has the same contents as 1.txt. For
Linux, you’d have to write ‘cp’ instead of ‘copy’. This method has been deprecated in Python 2.6.

ii. system()
The system() method lets us execute a command as a string in a subshell. Any output the command generates
goes to the interpreter standard output stream.
The syntax is:

os.system(command)

Let’s try doing the same thing using system() here.

>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> os.system('copy 1.txt 2.txt')

We can see the same action with this method. It copies the contents of a file 1.txt into a new file 2.txt.
Let’s revise Python File I/O – Python Write to File and Read File
Note that if we already had a file called 2.txt, this operation replaces its contents. This applies to the previous
method as well.
While this method is similar to popen(), it executes in a subshell. It executes in a separate thread parallel to our
executing code. By calling .wait() on the object it returns, we can wait for its completion.

b. Using Python Threading Library


We can borrow the Thread module from the threading library to Python copy a file. This does the copying in an
async manner.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> import shutil

>>> from threading import Thread

>>> Thread(target=shutil.copy, args=['1.txt','2.txt']).start()

This code copies the contents of file 1.txt to a new file 2.txt. If your application uses multiple threads for
reading or writing a file, you should employ locking to avoid deadlocks.

c. Using Python Subprocess Module


The subprocess module lets us work with child processes- launch them, attach to their pipes for input, output,
and error, and retrieve return values. From this module, we can use the methods call() and check_output(), for
Python copy a file.
Let’s see how this module helps python copy a file.
Python Copy a File – Python Subprocess Module

i. call()
The syntax for this command is the following:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

This method runs the command that args describes. It waits for the command to complete and then returns the
attribute returncode. We use the following code to copy a file:

>>> import os, subprocess

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> status=subprocess.call('copy 1.txt 2.txt', shell=True)

>>> if status!=0:

if status<0:

print(f"Killed by signal {status}")

else:

print(f"Command failed with return code {status}")

else:

print("Successfully executed command")


Successfully executed command
Let’s Discuss Python Modules in Details
Since here we get the output we do, we figure that the code returns a 0.

ii. check_output()
Now, let’s try the same with check_output(). This method runs commands with arguments and returns the
output. For a non-zero return code, it raises a CalledProcessError. It also supports pipes. This is the syntax to
follow:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

This method is a lot like subprocess.run, but it pipes data from stdout as encoded bytes by default. Let’s see
how we can copy a file with it.

>>> import os, subprocess

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> status=subprocess.check_output('copy 1.txt 2.txt',shell=True)

d. Using Python Shutil Module


There are four methods we will consider in this module to help Python copy a file- copyfile(), copy(), copy2(),
and copyfileobj().
Let’s Read the List of 63 Python os Modules in 2018

Python copy a file – Python shutil Modules

i. copyfile()
copyfile() copies one file’s contents into another. For this, the target should be writable; otherwise, it raises an
IOError. When the destination is a directory, it raises Error 13. And like we discussed previously, these methods
replace a destination file if it already exists and throw an error if the source and destination names are the same.
We have the following syntax for it:

shutil.copyfile(src_file, dest_file, *, follow_symlinks=True)

Let’s try the following code:

>>> import os,shutil

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> shutil.copyfile('1.txt','2.txt')

'2.txt'

This code gives us the name of the file it created. Also, when we set the follow_symlinks argument to False, it
creates a symbolic link from the source file, which is a symbolic link, instead of copying it.

ii. copy()
Like copyfile(), copy() lets us copy content from one file to another. However, this one also copies file system
permissions.
Do you know about Errors and Exceptions in Python Programming

We have the following syntax:

shutil.copy(src_file, dest_file, *, follow_symlinks=True)

Let’s try copying with this method too:

>>> import os,shutil

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> shutil.copy('1.txt','2.txt')

'2.txt'

So then, what is different here from copyfile()? Here we go:

copyfile() copies data, copy() also copies permission bits

copy() can copy to directories, copyfile() raises an Error 13 instead

copyfile() uses method copyfileobj() in its implementation, but copy uses functions copyfile() and
copymode() in turn

The last aspect makes copy() slower than copyfile(), which doesn’t have the overhead of preserving
permissions

iii. copy2()
This one’s like copy(), but while copying the data, it also gets access and modification times linked to the
metadata. For platforms that do not allow for full metadata saving, it preserves any metadata it can.

We have the following syntax:

shutil.copy2(src_file, dest_file, *, follow_symlinks=True)

Now let’s hit the payload.

>>> import os,shutil

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> shutil.copy2('1.txt','2.txt')

'2.txt'

This method preserves the creation date. Let’s see the differences between copy() and copy2().

copy() sets permission bits, copy2() also updates file metadata with timestamps

Internally, copy() calls copyfile() and copymode(); copy2() calls copyfile() and copystat()

Let’s Explore the Difference Between Python Modules vs Packages

iv. copyfileobj()
If you have been reading from your source file object, copyfileobj() will start copying from the position you
stopped reading at.

It has the following syntax:

shutil.copyfileobj(src_file_object, dest_file_object[, length])

Here, source and destination file parameters refer to objects. length is the buffer size. This is the number of
bytes in memory during a copy. This comes in handy when copying very large files, and the default is 16KB.
Let’s copy using this last method.

>>> import os,shutil

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> one=open('1.txt','rb')

>>> two=open('2.txt','wb')

>>> shutil.copyfileobj(one,two)

Remember to open the files in binary mode. Open the source file as readable and the destination file as writable.
Also, remember to close the files once you’re done.
So, this was all about How Python Copy a File. Hope you like our explanation.
3. Conclusion

Hence, in this tutorial, we discussed nine ways for Python copy a file. Moreover, we discussed the commands in
the Python OS Module, Python Threading Library, Python Subprocess Module, and Python Shutil Module.
Which one do you prefer? Comment below.

Copy in Python – Python Deep Copy and Shallow Copy


by DataFlair Team · November 3, 2018

1. Copy in Python

Today, in this Python Tutorial, we will see the copy in Python. Moreover, we will define Deep Copy and
Shallow Copy in Python. Also, we will see a difference between Shallow Copy vs Deep Copy. Along with this,
we will discuss Copy Module and Shallow Copying Dictionaries.

So, let’s start Copy in Python.

Copy in Python – Shallow Copy and Deep Copy

2. Python Copy Module

When we perform an assignment in Python, it does not copy the object we assign. All it does is create bindings
between a target and an object. But sometimes, we may need to change one Python copy without changing the
other fora mutable collection.
In this Python Copy tutorial, we discuss the module copy in Python. It has the following members-

copy.copy(x)

This returns a shallow copy of x.

copy.deepcopy(x)
This returns a deep copy of x.

exception copy.error

Do you know about Python Decision Making Statements


This is the exception it raises for module-specific errors.

Before we can begin explaining shallow copy and deep copy, we think it is necessary to tell you these concepts
apply to compound objects only- those that hold other objects like lists or class instances.

3. Python Deep Copy

When a deep copy in Python creates a new object, it inserts into the new object copies of the objects in the
original object. In other words, it copies an object into another. This means any changes we make to the copy do
not reflect in the original.

a. Deep Copy Example


Let’s try implementing this in Python. We use the deepcopy() function.

>>> import copy

>>> list1=[1,3,[7,4],6]

>>> list2=copy.deepcopy(list1) #Making a deep copy

>>> list1

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

>>> list2

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


Have a look at Python Syntax and Semantics

>>> list2[2][0]=5 #Modifying the element at index 2,0

>>> list1

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

>>> list2

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


As you can see, this did not change anything for the original object.

b. Problems with Python Deep Copy


It is possible that recursive objects cause a recursive loop; these are compound objects that directly or
indirectly reference themselves.
It is possible that a deep copy may copy too much.

To deal with these problems, deepcopy():

Keeps a memo dictionary of objects is copied during the current copying pass.

Allows user-defined classes to override the copying operation or the copied component set.

4. Python Shallow Copy


With a Shallow Copy in Python, we create a new object of which we recursively put copies of objects into the
original. In other words, we copy a reference of an object into another. Any changes we make to the copy do
reflect in the original.
Do you know about Python Function Arguments
Let’s implement this with Python. We’ll use the copy() function.

>>> import copy

>>> list1=[1,3,[7,4],6]

>>> list2=copy.copy(list1) #Making a shallow copy

>>> list1

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

>>> list2

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

>>> list2[2][0]=5 #Modifying the element at index 2,0

>>> list1

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

>>> list2

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


It is apparent that making changes to a shallow copy does change the original object.

5. Shallow Copying Dictionaries

Let’s first create a dictionary in Shallow Copy in Python.

>>> dict1={'a':1,'b':2,'c':[1,2,3]}

Now, we’ll make a copy of it.


Let’s revise the Python Built-in Function

>>> dict2=dict1.copy()
Finally, we’ll append a new element to this.

>>> dict2['c'].append(7)

>>> dict1

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}

>>> dict2

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}


Other ways to create a shallow copy of a dictionary are dict(dict1) and copy.copy(dict1). As you can see,
altering the mutable in the copy changed the original too. To prevent this, we can do a deep copy in Python.

>>> import copy

>>> dict1={'a':1,'b':2,'c':[1,2,3]}

>>> dict2=copy.deepcopy(dict1)

>>> dict2['c'].append(7)

>>> dict1

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3]}

>>> dict2

{‘a’: 1, ‘b’: 2, ‘c’: [1, 2, 3, 7]}


So, this was all in Copy in Python. Hope you like our explanation of Shallow Copy and Deep Copy.
You must read Python Multiple Inheritance

6. Conclusion

Hence, today, in this Copy in Python Tutorial, we discussed Shallow copy and Deep copy with Python. Where
shallow copy changes reflect in the original object, deep copy changes don’t. Moreover, we understood a
difference between Shallow Copy vs Deep Copy in Python. Also, we saw Shallow Copy Dictionaries. Still, if
any doubt regarding Copy in Python, ask in the comment tab.

How Python Rename File – Single & Multiple Files With


Example
by DataFlair Team · September 27, 2018

1. Objective

In our last Python tutorial, we studied How Python Copy a File. Today, we will see how Python rename file –
Single and multiple files. For renaming files in python, we will use the method rename() from the module os.
Let’s begin How Python Rename File.
How Python Rename File – Single & Multiple Files With Example

2. os.rename()

OS.rename() is used to python rename file. Let’s first take a look at the rename() method from the os module.
Like the name suggests, it lets us rename files and directories.
Let’s Discuss a List of 63 Python os Modules in 2018

a. A Syntax of os.rename()
We have the following syntax for the rename() method:

os.rename(src, dst)

Here, src is the source file or directory. dst is the destination file or directory. It doesn’t return any value. Let’s
take an example.

b. os.rename() Example
In the following example, we rename the folder ‘NewFolder’ to ‘Photos’.

>>> import os,sys

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> print(f"We have: {os.listdir()}")

We have: [‘0.jpg’, ‘1.txt’, ‘Documents’, ‘NewFolder’, ‘Today.txt’]

>>> os.rename('NewFolder',’Photos’) #This does the renaming


>>> os.listdir()

[‘0.jpg’, ‘1.txt’, ‘Documents’, ‘Photos’, ‘Today.txt’] As you can see, this has renamed the directory
‘NewFolder’ to ‘Photos’. At the time of writing this article, we tried to rename the directory to the name
‘Documents’. This raised the following error:

>>> os.rename('NewFolder','Documents')

Traceback (most recent call last):

File “<pyshell#7>”, line 1, in <module>

os.rename(‘NewFolder’,’Documents’)

FileExistsError: [WinError 183] Cannot create a file when that file already exists: ‘NewFolder’ -> ‘Documents’
Let’s Discuss Errors and Exceptions in Python Programming

3. How Python Rename Multiple Files?

Suppose in this folder ‘Photos’, we have the following photos of dogs.


Here, we can manually rename each of these files, but what if we had over a hundred photos? Or maybe
thousands? It would be too much to do. Instead, we make Python do it. Watch how.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop\\Photos')

>>> i=1

>>> for file in os.listdir():

src=file dst="Dog"+str(i)

+".jpg" os.rename(src,dst)

i+=1

Read about Exception Handling in Python

4. Python Rename File (Single File)

Sometimes, we may want to rename just one file from tens or hundreds. We can search for it and then use
Python to rename it. In the following code, we rename ‘Dog7.jpg’ to ‘SeventhDog.jpg’.

>>> for file in os.listdir():

src=file

if src=='Dog7.jpg':
dst="Seventh Dog.jpg"

os.rename(src,dst)

5. An Example- Incrementing Each Picture Number

In this example, we increment the picture number for each picture in this folder. For this example, we have
renamed ‘Seventh Dog.jpg’ to ‘Dog7.jpg’.

>>> print("How many pictures?")

How many pictures?

>>> i=int(input())

10

>>> count=1

>>> while(count<=10):

for file in os.listdir():

src=file

if src=='Dog'+str(i)+'.jpg':

dst='Dog'+str(i+1)+'.jpg'

os.rename(src,dst)

i-=1

count+=1

This code does the following:

What we do here is that we ask the user how many pictures she has. We set a variable count to 1. We keep
processing till count becomes equal to 10. For each time we are in the while loop, we check the filenames one
by one. We compare the file names; the first question is- Is the filename ‘Dog10.jpg’? For the file it is, it
renames it by incrementing the number. Then, we decrement i by 1 and increment count by 1. Hence, once
count has reached 11, we have fulfilled our purpose by now.

So, this was all about How Python Rename File tutorial. Hope you like our explanation.

6. Conclusion

Hence, we learned how Python rename file(single and multiple files) with os.rename module. In addition, we
discuss an example to understand this process better. Got more ideas? Drop them in the comments.
Python Data File Formats – How to Read CSV, JSON, and XLS Files
by DataFlair Team · September 27, 2018

1. How to Read CSV, JSON, and XLS Files

In our last python tutorial, we studied How to Work with Relational Database with Python. In this tutorial,
we will discuss different types of Python Data File Formats: Python CSV, JSON, and XLS. Moreover, we will
discuss how to read CSV, JSON, XLS files in Python Programming Language.
So, let’s see Python Data File Formats.

Python Data File Formats – How to Read CSV, JSON, and XLS Files

2. Python Data File Formats

Let’s first learn a little about the Python Data File formats we will be processing with.

a. Python Data File Formats – Python CSV


Python CSV data is a basic with data science. A Comma-Separated-Value file uses commas to separate values.
You can look at it as a delimited text file that holds tabular data as plain text. One problem with this may arise
when the data it holds contains a comma or a line break- we can use other delimiters like a tab stop. This Python
data file format proves useful in exchanging data and in moving tabular data between programs. The extension
for a CSV file is .csv.
Follow this link to know about File Handling In Python
Here’s a Python CSV file we will use for our demo-
id,title,timing,genre,rating
1,Dog with a Blog,17:30-18:00,Comedy,4.7
2,Liv and Maddie,18:00-18:30,Comedy,6.3
3,Girl Meets World,18:30-19:00,Comedy,7.2
4,KC Undercover,19:00-19:30,Comedy,6.1
5,Austin and Ally,19:30-20:00,Comedy,6
We saved this as schedule.csv on our Desktop. Remember to save as All files (*.*). When we open this file, it
opens in Microsoft Excel by default on Windows-

Python Data File Formats – How to Read CSV, JSON, and XLS Files

b. Python Data File Formats – Python JSON


JSON stands for JavaScript Object Notation and is an open standard file format. While it holds attribute-value
pairs and array data types, it uses human-readable text for this. This Python data file format is language-
independent and we can use it in asynchronous browser-server communication. The extension for a Python
JSON file is .json.

Python Data File Formats – How to Read CSV, JSON, and XLS Files

Let’s Explore Python Zipfile – Benefits, Modules, Objects


Here’s the JSON file in Python we will use for the demo-

{ "ID":

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

"Title":["Dog with a Blog","Liv and Maddie","Girl Meets World","KC Undercover","Austin and Ally"],

"Timing":["17:30-18:00","18:00-18:30","18:30-19:00","19:00-19:30","19:30-20:00"],

"Genre":["Comedy","Comedy","Comedy","Comedy","Comedy"],

"Rating":["4.7","6.3","7.2","6.1","6"]

}
We save this as schedule.json on the Desktop.

c. Python Data File Formats – Python XLS


The extension for an Excel spreadsheet is .xlsx. This proves useful for data science; we create a workbook with
two sheets in Microsoft Excel.
Sheet 1-

Python Data File Formats – How to Read CSV, JSON, and XLS Files

Sheet 2-

Python Data File Formats – How to Read CSV, JSON, and XLS Files

We save this workbook as schedule.xlsx on our Desktop.


Do you Know the XML Processing in Python 3

3. Prerequisites

To process these Python data file formats, we need the library pandas.

Python Data File Formats – How to Read CSV, JSON, and XLS Files

Install it using pip-

>>> pip install pandas


4. How to Read CSV File in Python?

To read an entire file, rows, columns, or combinations of those, read on.

Python Data File Formats – How to Read CSV, JSON, and XLS Files

a. Reading an entire Python CSV File


To read an entire file, we can use the read_csv() function.

>>> import pandas

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> print(pandas.read_csv('schedule.csv'))

Python Data File Formats – How to Read CSV, JSON, and XLS Files
b. Reading rows for one column
Sometimes, we may only want to fetch certain rows at once for a certain column. We use the slicing operator
for this. Read up on Python Operators.

>>> data=pandas.read_csv('schedule.csv')

>>> print(data[0:3]['title'])

Python Data File Formats – How to Read CSV, JSON, and XLS Files

c. Reading certain columns


Now we can also fetch only certain columns. For this, we use the .loc() method for multi-axis indexing.

>>> data=pandas.read_csv('schedule.csv')

>>> print(data.loc[:,['title','rating']])

Python CSV File – Reading certain columns

d. Reading certain rows and certain columns


Using the .loc() method, we can also fetch certain rows and certain columns:

>>> data=pandas.read_csv('schedule.csv')

>>> print(data.loc[[1,3],['title','rating']])

Python Data File Formats – How to Read CSV, JSON, and XLS Files
e. Reading certain columns for a range of rows
Now to fetch certain columns for a range of rows, we slightly change the previous syntax and use slicing instead
of indices.

data=pandas.read_csv('schedule.csv')

>>> print(data.loc[1:3,['title','rating']])

Python CSV File – Reading certain columns for a range of rows

Let’s revise Python Generators and Generator Expressions

f. Using the Python CSV Module


You can also install the module CSV using pip, and then use the following code-

>>> import csv

>>> fields=rows=[]

>>> with open('schedule.csv','r') as file:

reader=csv.reader(file) #Reader object

fields=next(reader)

for row in reader:

rows.append(row)

print(f"You have {reader.line_num} rows") #Number of rows

You have 6 rows

>>> print(' '.join(field for field in fields)) #Field names

id title timing genre rating

>>> for row in rows[:6]:

for col in row:

print(col)
print('\n')

1
Dog with a Blog
17:30-18:00
Comedy
4.7

2
Liv and Maddie
18:00-18:30
Comedy
6.3

3
Girl Meets World
18:30-19:00
Comedy
7.2

4
KC Undercover
19:00-19:30
Comedy
6.1

5
Austin and Ally
19:30-20:00
Comedy
6

5. How to Read JSON File in Python?


Python Data File Formats – How to Read CSV, JSON, and XLS Files

Let’s Discuss Python Decision Making Statements

a. Reading an entire Python JSON file


To read an entire JSN file in Python, we can use the read_json() function.

>>> import pandas

>>> print(pandas.read_csv('schedule.csv'))

Python Data File Formats – How to Read JSON Files

b. Reading certain rows and columns


And when you only want some rows and some columns, you can-

>>> data=pandas.read_csv('schedule.csv')

>>> data.loc[[1,3],['title','rating']]
Python JSON Files – Reading certain rows and columns

c. Reading rows and columns as records


You can also fetch the rows and columns as a sequence of records-

>>> data=pandas.read_csv('schedule.csv')

>>> print(data.to_json(orient='records',lines=True))

Python JSON File – Reading rows and columns as records

6. How to Read XLS File in Python?


Finally, let’s find out how to read Python XLS files.
Python Data File Formats – How to Read JSON File in Python

a. Reading an entire file


You can read an entire file using the read_excel() function.

>>> import pandas

>>> print(pandas.read_excel('schedule.xlsx'))

Python Data File Formats – How to Read XLS Files

Note- You may need to install a module for this-


C:\Users\lifei>pip install xlrd
Have a Look Python Sets and Booleans with Syntax and Examples

b. Reading certain rows and columns


When you only want to fetch certain rows and columns, you can use the .loc() method.

>>> data=pandas.read_excel('schedule.xlsx')

>>> print(data.loc[[1,3],['title','rating']])

Python XLS File – Reading certain rows and columns

c. Reading more than one sheet


>>> with pandas.ExcelFile('schedule.xlsx') as book:

s1=pandas.read_excel(book,'Sheet1')

s2=pandas.read_excel(book,'Sheet2')

>>> s1[0:5]['title']
Python XLS File – Reading more than one sheet

>>> s2[0:5]['years']

Python XLS File – Reading more than one sheet

d. Using the Python XLRD Module


>>> import xlrd

>>> book=xlrd.open_workbook('schedule.xlsx')

>>> sheet=book.sheet_by_index(0)

>>> sheet.cell_value(0,0) #Value at cell at row 0 and column 0

'id'

>>> sheet.nrows #Number of rows

>>> sheet.ncols #Number of columns

>>> for i in range(sheet.ncols):

sheet.cell_value(0,i) #Column names

'id'

'title'

'timing'

'genre'
'rating'

>>> sheet.row_values(1) #First row

[1.0, 'Dog with a Blog', '17:30-18:00', 'Comedy', 4.7]

So, this was all about Python Data File Formats. Hope you like our explanation.

7. Conclusion

Hence, in this tutorial, we discussed different types of Python Data File Formats. In addition, we also learned
how to process/read formats like CSV, JSON, and XLS files in Python. Leave your comments below and don’t
forget to visit again for another tutorial tomorrow.

What is Python Exception – Python Error & In-built Exception


in Python
by DataFlair Team · September 25, 2018

1. Python Error – Objective

In this Python Error tutorial, we will discuss what is a syntax error in Python. Along with this, we will study
Python Exception, error message, and come in-built exception in Python Programming Language.

It will cover all possible python error and python exception to help you in running your python code smoothly
as there are many reasons why to learn Python.

So, let’s begin Python Error and Python Exception.

What is Python Error – Python Exception Message & In-built Exception


2. Syntax Errors in Python

In your code, when you mess up the rules of Python Syntax, your code doesn’t run. The following code causes
a syntax error.

>>> if 2>1 print("2")

SyntaxError: invalid syntax

This code doesn’t run because it misses a colon after the condition 2>1.

A syntax error also called a parsing error, displays ‘Syntax Error: invalid syntax’.

3. What is Python Exception?

It may be convenient to recognize the problems in your python code before you put it to real use. But that does
not always happen. Sometimes, problems show up when you run the code; sometimes, midway of that.

A Python exception is an error that’s detected during execution. It may be fatal for the program, but not
necessarily so. Let’s take the most common example.

>>> a,b=1,0

>>> print(a/b)

Traceback (most recent call last):

File “<pyshell#208>”, line 1, in <module>

print(a/b)

ZeroDivisionError: division by zero

Throughout our python tutorials so far, you’ve noticed words like TypeError, NameError, and so. It’s time to
find out what that is. Also, learn Exception Handling in Python for Python Programming.

4. Python Error and Python Exception Message

When Python error and exceptions occur, it prints a four-line message on the screen, if not handled.

The first line declares that this is a traceback. This means that the interpreter traces the Python exception back to
its source.

The second tells us the line number for the code that caused the Python exception. In our case, it is line 1. #208
means this is the 208th statement we’re running in the interpreter since we opened it.

The third line tells us which line (the statement that) caused the Python exception.

Finally, the fourth line tells us the type of Python exception that occurred. This is accompanied by a short
description of what happened.
5. In-built Python Exception

Now that we know what an exception is, we will talk of a list of python exception that is inbuilt in Python. As
you read the list, try to recall if you ever encountered any of these Python exceptions. Tell us in the comments.

a. AssertionError in python

This Python exception raises when an assert statement fails. This is also called Python raise expression.

A successful assert statement looks like this:

>>> assert(1==1)

But when we write the following code, we get an AssertionError:

>>> assert(1==2)

Traceback (most recent call last):

File “<pyshell#213>”, line 1, in <module>

assert(1==2)

AssertionError

We’ll take the assert statement in detail in a future lesson.

b. AttributeError in python

This one occurs when an attribute assignment or reference fails. As an example, let’s take class

‘fruit’.

>>> class fruit:

pass

>>> fruit.size

Traceback (most recent call last):

File “<pyshell#223>”, line 1, in <module>

fruit.size

AttributeError: type object ‘fruit’ has no attribute ‘size’

Here, the attribute size does not exist. Hence, it raises an AttributeError.

c. EOFError in Python
This Python exception raises when the input() function reaches the end-of-file condition.

d. FloatingPointError in Python

When a floating point operation fails, this python error occurs.

e. GeneratorExit in python

This raises when a generator’s close() method is called.

f. ImportError in Python

When the imported module isn’t found, an ImportError occurs.

>>> from math import ppi

Traceback (most recent call last):

File “<pyshell#234>”, line 1, in <module>

from math import ppi

ImportError: cannot import name ‘ppi’

g. IndexErrorin Python

When you access an index, on a sequence, that is out of range, you get an IndexError.

>>> list=[1,2,3]

>>> list[3]

Traceback (most recent call last):

File “<pyshell#236>”, line 1, in <module>

list[3]

IndexError: list index out of range

h. KeyError in Python

This raises when a key isn’t found in a dictionary.

>>> dict1={1:1,2:2}

>>> dict1[3]

Traceback (most recent call last):

File “<pyshell#239>”, line 1, in <module>


dict1[3]

KeyError: 3

i. KeyboardInterrupt in Python

This one occurs when the user hits the interrupt key (Ctrl + C).

>>> while True:

print("Hello") Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello
Hello

Traceback (most recent call last):

File “<pyshell#244>”, line 1, in <module>

while True: print(“Hello”)

KeyboardInterrupt

j. MemoryError in Python

This raises when an operation runs out of memory.

k. ModuleNotFoundError in Python

When you import a module that does not exist, you will get the ModuleNotFoundError.

>>> import maths

Traceback (most recent call last):

File “<pyshell#233>”, line 1, in <module>

import maths

ModuleNotFoundError: No module named ‘maths’

l. NameError in Python

A NameError occurs when a name isn’t found in a scope.

>>> eggs

Traceback (most recent call last):

File “<pyshell#245>”, line 1, in <module>

eggs

NameError: name ‘eggs’ is not defined

m. NotImplementedError in Python

An abstract method raises a NotImplementedError.

n. OSError in Python

Now this one is raised when a system operation causes a system-related error.

o. OverflowError in Python
This raises when the result of an arithmetic operation is too large to be represented.

p. ReferenceError in Python

This is raised when a weak reference proxy is used to access a garbage collected referent.

q. RuntimeError in Python

When an error does not fall under any specific category, we call it a RuntimeError.

r. StopIteration in Python

The next() function raises StopIteration to indicate that no further item is to be returned by the iterator.

>>> def countdown():

n=4

while(n>0):

yield n

n-=1

>>> c=countdown()

>>> next(c)

>>> next(c)

>>> next(c)

>>> next(c)

>>> next(c)

Traceback (most recent call last):

File “<pyshell#23>”, line 1, in <module>

next(c)
StopIteration

s. IndentationError in Python

An IndentationError raises on incorrect indentation.

t. TabError in Python

When the indentation is inconsistent in tabs and spaces, there’s a TabError.

u. SystemError in Python

When the interpreter detects an internal error, it’s a SystemError.

v. SystemExit in Python

The sys.exit() function raises this one.

w. TypeError in Python

When you apply a function or an operation to an object of incorrect type, you get a TypeError.

>>> '10'+10

Traceback (most recent call last):

File “<pyshell#38>”, line 1, in <module>

’10’+10

TypeError: must be str, not int

x. UnboundLocalError in Python

You get an UnboundLocalError when you try to access a local variable without first assigning a value to it.

>>> def sayhi():

m+=1

print(m)

>>> sayhi()

Traceback (most recent call last):

File “<pyshell#53>”, line 1, in <module>

sayhi()

File “<pyshell#52>”, line 2, in sayhi


m+=1

UnboundLocalError: local variable ‘m’ referenced before assignment

y. UnicodeError in Python

When a Unicode-related encoding/decoding error occurs, you get the UnicodeError exception.

z. UnicodeEncodeError in Python

This is a Unicode error during encoding.

Learn: Python Inheritance, Method Overloading & Method Overriding

aa. UnicodeDecodeError in Python

The Unicode error during decoding is termed UnicodeDecodeError.

ab. UnicodeTranslateError in Python

A UnicodeTranslateError occurs during translating.

ac. ValueError in Python

You get a ValueError when you send in an argument of the correct type, but an improper value.

>>> int(input())

3.5

Traceback (most recent call last):

File “<pyshell#55>”, line 1, in <module>

int(input())

ValueError: invalid literal for int() with base 10: ‘3.5’

ad. ZeroDivisionError in Python

Finally, a ZeroDivisionError is one we’ve seen in section 3. When the denominator of a division is 0, this
Python exception is raised. This doesn’t necessarily violate syntax.

>>> print(1/0)

Traceback (most recent call last):

File “<pyshell#56>”, line 1, in <module>

print(1/0)
ZeroDivisionError: division by zero

So, this is all about the Python Error and Python Exception. Hope you like our explanation.

6. Conclusion

In this module, we learned about Python Error and Python Exception and looked at some in-built exceptions in
python. These are unexpected situations at runtime. Furthermore, if you have any query, feel free to ask in the
comment box.

Python Exception Handling – Try/Except Block, Finally Block


by DataFlair Team · September 25, 2018

1. Python Exception Handling

In our previous lesson on Errors and Exceptions in Python. Now, we are going to explore Python Exception
Handling. Here, we will discuss try/except blocks, finally block, and raise block. Along with this, we will learn
how to define your own python exception.

So, let’s begin Python Exception Handling.

Python Exception Handling

2. Exception Handling in Python

As we have already seen different types of exceptions in python, let us see various ways for Python exception
handling if we get any python exception while programming in python.

3. The try/except blocks


When you think a part of your code might throw an exception, put it in a try block. Let us see a Python try
exception example.

try:

for i in range(3):

print(3/i)

except:

print("You divided by 0")

print(‘This prints because the exception was handled’)

= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =

You divided by 0

This prints because the exception was handled

What follows is an except block. When you don’t specify which exception to catch, it will catch any. In other
words, this is generic for exceptions.

When an exception is thrown in a try block, the interpreter looks for the except block following it. It will not
execute the rest of the code in the try block.

Python Exceptions are particularly useful when your code takes user input. You never know what the user will
enter, and how it will mess with your code.

a. Python Multiple Excepts


It is possible to have multiple except blocks for one try block. Let us see Python multiple exception handling
examples.

>>> a,b=1,0

>>> try:

print(a/b)

print("This won't be printed")

print('10'+10)

except TypeError:

print("You added values of incompatible types")

except ZeroDivisionError:
print("You divided by 0")

You divided by 0

When the interpreter encounters an exception, it checks the except blocks associated with that try block. These
except blocks may declare what kind of exceptions they handle. When the interpreter finds a matching
exception, it executes that except block.

In our example, the first statement under the try block gave us a ZeroDivisionError. We handled this in its
except block, but the statements in try after the first one didn’t execute. This is because once an exception is
encountered, the statements after that in the try block are skipped. And if an appropriate except block or a
generic except block isn’t found, the exception isn’t handled. In this case, the rest of the program won’t run. But
if you handle the exception, the code after the excepts and the finally block will run as expected. Let’s try some
code for this.

a,b=1,0

try:

print(a/b)

except:

print("You can't divide by 0")

print("Will this be printed?")

Let’s see the output now.


== RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try.py ==
You can’t divide by 0
Will this be printed?

>>>

Any doubt yet in Python Exceptional Handling? Please Comment.

b. Python Multiple Exception in one Except


You can also have one except block handle multiple exceptions. To do this, use parentheses. Without that, the
interpreter will return a syntax error.

>>> try:

print('10'+10)

print(1/0)

except (TypeError,ZeroDivisionError):

print("Invalid input")
Invalid input

c. A Generic except After All Excepts


Finally, you can compliment all specific except blocks with a generic except at the end. This block will serve to
handle all exceptions that go undetected by the specific except blocks.

>>> try:

print('1'+1)

print(sum)

print(1/0)

except NameError:

print("sum does not exist")

except ZeroDivisionError:

print("Cannot divide by 0")

except:

print("Something went wrong")

Something went wrong

Here, the first statement under the try block tries to concatenate a string to an int. This raises a TypeError. As
the interpreter comes across this, it checks for an appropriate except block that handles this.

Also, you can’t put a statement between try and catch blocks.

try:

print("1")

print("2")

except:

print("3")

This gives you a syntax error.

But there can only be one generic or default except block for one try block. The following code gives us
“default ‘except:’ must be last”:

try:
print(1/0)

except:

raise

except:

print("Raised exception caught")

finally:

print("Okay")

print("Bye")

4. Finally Block in Python

Optionally, you may include a finally exception block after the last except block. The code under this block
executes in all circumstances.

>>> try:

print(1/0)

except ValueError:

print("This is a value error")

finally:

print("This will print no matter what.")

This will print no matter what.

Traceback (most recent call last):


File “<pyshell#113>”, line 2, in <module>
print(1/0)

ZeroDivisionError: division by zero


\Note that the Python exception message is printed after the finally block executes. Now you may think, why
don’t we just use a print statement instead? As you can see in the previous example, ‘finally’ runs even when
we fail to catch the exception that occurs.

Now what if an exception occurred in except?

>>> try:

print(1/0)

except ZeroDivisionError:
print(2/0)

finally:

print("Sorry, not happening")

Sorry, not happening

Traceback (most recent call last):

File “<pyshell#122>”, line 2, in <module>

print(1/0)

ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File “<pyshell#122>”, line 4, in <module>

print(2/0)

ZeroDivisionError: division by zero

So as you can see, the code under the finally block executes no matter what.

5. Raise Keyword in Python

Sometimes, you may want to deal with a situation by raising a certain exception. A simple print statement won’t
work here.

>>> raise ZeroDivisionError

Traceback (most recent call last):

File “<pyshell#132>”, line 1, in <module>

raise ZeroDivisionError

ZeroDivisionError

Let’s take our case of division.

>>> a,b=int(input()),int(input())

0
>>> if b==0:

raise ZeroDivisionError

Traceback (most recent call last):

File “<pyshell#140>”, line 2, in <module>

raise ZeroDivisionError

ZeroDivisionError

Here, we convert the inputs to a and b to integers. Then, we check if b is 0. In that case, we raise a
ZeroDivisionError.

Now what if we put this in try-except blocks? Let’s make a .py file for it.

a,b=int(input()),int(input())

try:

if b==0:

raise ZeroDivisionError

except:

print("You divided by 0")

print("Will this print?")

When we run this using Fn+F5, we get the following output:

= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =

You divided by 0

Will this print?

We’ll take just one more example before moving on.

>>> raise KeyError

Traceback (most recent call last):

File “<pyshell#180>”, line 1, in <module>


raise KeyError

KeyError

a. Raise Without a Specified Exception in Python


It is possible to use the raise keyword without specifying what exception to raise. Then, it reraises the exception
that occurred. This is why you can only put it in an except block.

>>> try:

print('1'+1)

except:

raise

Traceback (most recent call last):

File “<pyshell#152>”, line 2, in <module>

print(‘1’+1)

TypeError: must be str, not int

b. Raise With an Argument in Python


Additionally, you can provide an argument to the specified exception in raise. You can do this to give out
additional details about the exception.

>>> raise ValueError("Inappropriate value")

Traceback (most recent call last):

File “<pyshell#156>”, line 1, in <module>

raise ValueError(“Inappropriate value”)

ValueError: Inappropriate value

6. Assertions in Python

An assertion is actually a sanity-check for your cynical, paranoid soul. It takes an expression as an argument
and raises a python exception if the expression has a False Boolean value. Otherwise, it performs a No-
operation (NOP).

>>> assert(True)

>>>

Now what if the expression was False?


>>> assert(1==0)

Traceback (most recent call last):

File “<pyshell#157>”, line 1, in <module>

assert(1==0)

AssertionError

Let’s take another example, and let’s create a .py file for that.

try:

print(1)

assert 2+2==4

print(2)

assert 1+2==4

print(3)

except:

print("An assert failed.")

raise

finally:

print("Okay")

print("Bye")

Now, let’s see the output.

= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =

An assert failed.

Okay

Traceback (most recent call last):

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py”, line 5, in <module>


assert 1+2==4

AssertionError

Interestingly, if you remove the raise from under the except block, this is the output:
= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =

An assert failed.

Okay

Bye

This is because when we ‘raise’ an exception, we aren’t provisioning a handle for it.
We can use assertions to check for valid input and output to functions.

a. A Second Argument to assert


You may optionally provide a second argument to give out some extra information about the problem.

>>> assert False,"That's a problem"

Traceback (most recent call last):

File “<pyshell#173>”, line 1, in <module>

assert False,”That’s a problem”

AssertionError: That’s a problem

7. Defining Your Own Exceptions in Python

Finally, we’ll talk about creating our own exceptions. For this, we derive a new class from the Exception class.
Later, we call it like any other exception.

>>> class MyError(Exception):

print("This is a problem")

>>> raise MyError("MyError happened")

Traceback (most recent call last):

File “<pyshell#179>”, line 1, in <module>

raise MyError(“MyError happened”)


MyError: MyError happened

This was all about the Python Exception Handling Cheat Sheet.

8. Conclusion: Python Exception Handling

After this article, we hope you’ll play safer with your code. This is because now you can do Python Exception
Handling, raise it, and even create your own. If you’d like to add your own code on Python Exception Handling
to the comments, we’d love to hear.

Python Directory & File Management – A Quick and Easy


Tutorial
by DataFlair Team · September 25, 2018

1. Python Directory Tutorial

So far, we’ve mostly only seen the computational capabilities of Python. Today, we’ll talk about how we can
use it to handle Python directory. After this tutorial, you’ll be able to create, rename, list files in a directory in
Python, and work with the Python Directory.

In a computer system, files are organized into directories. These may contain subdirectories and files. Indeed,
this makes a vital part of a user-friendly UI. But don’t be confused; a dictionary is simply what you call a
folder.

In this Python Directory tutorial, we will import the OS module to be able to access the methods we will apply.

>>> import os

Introduction to Python Directory


2. Getting Current Python Directory

To find out which directory in python you are currently in, use the getcwd() method.

>>> os.getcwd()

‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32’

Cwd is for current working directory in python. This returns the path of the current python directory as a string
in Python. To get it as a bytes object, we use the method getcwdb().

>>> os.getcwdb()

b’C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32′

Here, we get two backslashes instead of one. This is because the first one is to escape the second one since this
is a string object.

>>> type(os.getcwd())

<class 'str'>

To render it properly, use the Python method with the print statement.

>>> print(os.getcwd())

C:\Users\lifei\AppData\Local\Programs\Python\Python36-32

3. Changing Current Python Directory


To change our current working directories in python, we use the chdir() method. This takes one argument- the
path to the directory to which to change.

>>> os.chdir('C:\Users\lifei')

SyntaxError: (unicode error) ‘unicodeescape’ code can’t decode bytes in position 2-3: truncated
\UXXXXXXXX escape

But remember that when using backward slashes, it is recommended to escape the backward slashes to avoid a
problem.

>>> os.chdir('C:\\Users\\lifei')

>>> os.getcwd()

‘C:\\Users\\lifei’

When you restart the shell, we get back to the default working python directory.

>>> os.chdir('C:\\Users\\lifei')
>>> os.getcwd()

‘C:\\Users\\lifei’

You can also use forward slashes for the path. This way, you don’t have to use backward slashes to escape.

>>> os.chdir('C:/Users/lifei')

>>> os.getcwd()

‘C:\\Users\\lifei’

Finally, you can also use double quotes.

>>> os.chdir("C:\\Users\\lifei")

4. Python List Directories and Files

To get the contents of a directory into a python list, we use the listdir() method.

>>> os.listdir()

[‘.atom’, ‘.eclipse’, ‘.idlerc’, ‘.p2’, ‘.tooling’, ‘.vscode’, ‘3D Objects’, ‘afiedt.buf’, ‘AppData’, ‘Application
Data’, ‘Contacts’, ‘Cookies’, ‘Desktop’, ‘Documents’, ‘Downloads’, ‘Dropbox’, ‘eclipse’, ‘eclipse-workspace’,
‘eclipse-workspace-C++’, ‘eclipse-workspace-EE’, ‘Favorites’, ‘iCloudDrive’, ‘IntelGraphicsProfiles’, ‘Links’,
‘Local Settings’, ‘MicrosoftEdgeBackups’, ‘Music’, ‘My Documents’, ‘NetHood’, ‘NTUSER.DAT’,
‘ntuser.dat.LOG1’, ‘ntuser.dat.LOG2’, ‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TM.blf’,
‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TMContainer00000000000000000001.regtrans-
ms’, ‘NTUSER.DAT{03a9cc49-f0a2-11e7-904c-

d9989e93b548}.TMContainer00000000000000000002.regtrans-ms’, ‘ntuser.ini’, ‘OneDrive’, ‘Oracle’,


‘Pictures’, ‘PrintHood’, ‘Recent’, ‘Roaming’, ‘Saved Games’, ‘Searches’, ‘SendTo’, ‘Start Menu’, ‘Templates’,
‘Videos’, ‘workspace’]

Note that this includes the hidden and system files as well.

>>> os.chdir("C:\\Users\\lifei\\Desktop")

>>> os.listdir()

[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Burn Book.txt’, ‘desktop.ini’, ‘Documents’, ‘Eclipse Cpp
Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’, ‘For the book.txt’, ‘Items for trip.txt’,
‘Papers’, ‘Remember to remember.txt’, ‘Sweet anticipation.png’, ‘Today.txt’, ‘topics.txt’, ‘unnamed.jpg’]

This shows us the contents on the desktop. This was about Python List directory.

5. How to Create Python Directory?

We can also create new python directories with the mkdir() method. It takes one argument, that is, the path of
the new python directory to create.
>>> os.mkdir('Christmas Photos')

>>> os.listdir()

[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Burn Book.txt’, ‘Christmas Photos’, ‘desktop.ini’, ‘Documents’,
‘Eclipse Cpp Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’, ‘For the book.txt’, ‘Items for
trip.txt’, ‘Papers’, ‘Remember to remember.txt’, ‘Sweet anticipation.png’, ‘Today.txt’, ‘topics.txt’,
‘unnamed.jpg’]

Here, we supplied the name of the python directory to create. We can also create a directory in a directory other
than the current working directory in python. For this, you must specify the full path.

6. How to Rename Python Directory?

To rename directories in python, we use the rename() method. It takes two arguments- the python directory to
rename, and the new name for it.

>>> os.rename('Christmas Photos','Christmas 2017')

>>> os.listdir()

[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Burn Book.txt’, ‘Christmas 2017’, ‘desktop.ini’, ‘Documents’,
‘Eclipse Cpp Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’, ‘For the book.txt’, ‘Items for
trip.txt’, ‘Papers’, ‘Remember to remember.txt’, ‘Sweet anticipation.png’, ‘Today.txt’, ‘topics.txt’,
‘unnamed.jpg’]

7. How to Remove Python Directory/File?

We made a file named ‘Readme.txt’ inside our folder Christmas 2017. To delete this file, we use the method
remove().

>>> os.chdir('C:\\Users\\lifei\\Desktop\\Christmas 2017')

>>> os.listdir()

[‘Readme.txt’] To remove a python directory, we use the rmdir() method. But for this, the directory must be
empty. So we’ll add Readme.txt again to check if we’re able to delete Christmas 2017.

>>> os.rmdir('Christmas 2017')

Traceback (most recent call last):

File "<pyshell#412>", line 1, in <module>

os.rmdir('Christmas 2017')

OSError: [WinError 145] The directory is not empty: ‘Christmas 2017’

As you can see, it raised a python exception called OSError. So let’s first remove the file and then delete the
python directory.
>>> os.remove('C:\\Users\\lifei\\Desktop\\Christmas 2017\\Readme.txt')

>>> os.rmdir('Christmas 2017')

>>> os.listdir()

[‘Adobe Photoshop CS2.lnk’, ‘Atom.lnk’, ‘Burn Book.txt’, ‘desktop.ini’, ‘Documents’, ‘Eclipse Cpp
Oxygen.lnk’, ‘Eclipse Java Oxygen.lnk’, ‘Eclipse Jee Oxygen.lnk’, ‘For the book.txt’, ‘Items for trip.txt’,
‘Papers’, ‘Remember to remember.txt’, ‘Sweet anticipation.png’, ‘Today.txt’, ‘topics.txt’, ‘unnamed.jpg’]

Also, learn how to do exception handling in python.

8. Joining and Splitting Path

We must use platform-independent file and directory in python paths, so our program runs on every platform.
We use the submodule os.path for this.
join() in python joins path components and returns a path as a string. It adds appropriate separators (\ for
Windows and / for Unix)

>>> os.path.join('C:','Users','lifei','Desktop')

‘C:Users\\lifei\\Desktop’
Conversely, split() splits the path into components, removing the separator.

>>> os.path.split('C:Users\\lifei\\Desktop')

(‘C:Users\\lifei’, ‘Desktop’)

9. Checking if Python Directory Exists

It is possible to check whether a path exists. We use the exists() function for this. Also, this is in the os.path
submodule.

>>> os.path.exists('C:\\Users\\lifei\\Desktop')

True

>>> os.path.exists('C:\\Users\\lifei\\Desktop\\Myfolder')

False

>>> os.path.exists('C:\\Users\\lifei\\Desktop\\topics.txt')

True
Then, to check whether that path leads us to a directory, we use the isdir() function.

>>> os.path.isdir('C:\\Users\\lifei\\Desktop')

True

>>> os.path.isdir('C:\\Users\\lifei\\Desktop\\topics.txt')
False

10. Recursively Traversing a Directory in Python

The walk() function lets us recursively traverse a directory. This means that it returns the roots, subdirectories,
and files in a directory. You can traverse it using for loops in Python.

>>> for roots,dirs,files in os.walk('C:\\Users\\lifei\\Desktop\\Papers'):

print(roots,len(dirs),len(files))

C:\Users\lifei\Desktop\Papers 1 29
C:\Users\lifei\Desktop\Papers\Newfolder 0 1

>> for roots,dirs,files in os.walk('C:\\Users\\lifei\\Desktop\\Papers'):

print(roots,dirs,files)

C:\Users\lifei\Desktop\Papers [‘Newfolder’] [‘cs 15jun.pdf’, ‘sc 11jun.pdf’, ‘sc 12dec.pdf’, ‘sc 12jun.pdf’, ‘sc
13jun.pdf’, ‘sc 14jun.pdf’, ‘sc 15jun.pdf’, ‘sc 16dec.pdf’, ‘sc 16jun.pdf’, ‘sc 17jun.pdf’, ‘Syllabus.pdf’, ‘we
10jun.pdf’, ‘we 11jun.pdf’, ‘we 12jun.pdf’, ‘we 13jun.pdf’, ‘we 14jun.pdf’, ‘we 15jun.pdf’, ‘we 16dec.pdf’,
‘we 16jun.pdf’, ‘we 17jun.pdf’, ‘wn 10jun.pdf’, ‘wn 11jun.pdf’, ‘wn 13jun.pdf’, ‘wn 14jun.pdf’, ‘wn
15jun.pdf’, ‘wn 16dec.pdf’, ‘wn 16jun.pdf’, ‘wn 17jun.pdf’, ‘wn jun12.pdf’]

C:\Users\lifei\Desktop\Papers\Newfolder [] [‘readme.txt’]

Actually, these give us Python generator objects. This is why we can traverse on them.

>>> os.walk('C:\\Users\\lifei\\Desktop\\Papers')

<generator object walk at 0x05E0CE10>

11. Conclusion
What we discussed today are just a few methods that Python provides you to work with files and directories.
With these, we can create a Python directory, rename, and delete them. You can also traverse them, and check if
a path exists. Feel powerful yet? There’s more to come on our journey with Python.

Learn Python Itertools and Python Iterables with Examples


by DataFlair Team · September 26, 2018

1. Python Itertools and Python Iterables

All the constructs of Python programming, all the syntactic sugar. These are just a few good things about
Python. We’ve talked earlier of Iterators, Generators, and also a comparison of them. Today, we will talk
about Python iterables, examples of iterables in python, Python Itertools, and functions offered by Itertools in
python.

So, let’s start exploring Iterables & Itertools in Python Programming Langauge.
Introduction to Python Iterables and Python Itertools

2. What is Python Iterables?

An iterable in python is an object in python that can return an iterator. Using this python iterator, we can iterate
on every single element of the iterable.

To check what happens internally in an iterator, we’re going to use the ‘dis’ module to disassemble the code.
Once we import it, we call the dis() function. Before Preferring the example, let’s see python Syntax.

>>> import dis

>>> dis.dis('for _ in [1,2,3]:pass')

1 0 SETUP_LOOP 12 (to 14)

2 LOAD_CONST 4 ((1, 2, 3))

4 GET_ITER

>> 6 FOR_ITER 4 (to 12)

8 STORE_NAME 0 (_)

10 JUMP_ABSOLUTE 6

>> 12 POP_BLOCK

>> 14 LOAD_CONST 3 (None)

16 RETURN_VALUE
Here, GET_ITER is like invoking iter(). Likewise, FOR_ITER is to repeatedly call next() to get each element.

3. Example of Python Iterables

Okay, so let’s take an example before we begin.

>>> nums=[1,2,3]

>>> number=iter(nums)

>>> next(number)

>>> next(number)

>>> next(number)

>>> next(number)

Traceback (most recent call last):

File “<pyshell#224>”, line 1, in <module>

next(number)

StopIteration

In this code, ‘nums’ is a python iterables. Using the function iter(), we create an iterator ‘number’. This
relationship can be inferred from the following representation.

An Example of Java Iterables

An iterables in python has the methods len () and getitem ()


>>> a=[1,2,3]

>>> dir(a)

[‘ add ’, ‘ class ’, ‘ contains ’, ‘ delattr ’, ‘ delitem ’, ‘ dir ’, ‘ doc ’, ‘ eq__’,


‘ format ’, ‘ ge ’, ‘ getattribute ’, ‘ getitem ’, ‘ gt ’, ‘ hash ’, ‘ iadd ’, ‘ imul ’,
‘ init ’, ‘ init_subclass ’, ‘ iter ’, ‘ le ’, ‘ len ’, ‘ lt ’, ‘__mul ’, ‘ ne ’, ‘ new ’,
‘ reduce ’, ‘ reduce_ex ’, ‘ repr ’, ‘ reversed ’, ‘ rmul ’, ‘ setattr ’, ‘ setitem__’,
‘ sizeof ’, ‘ str ’, ‘ subclasshook ’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’,
‘remove’, ‘reverse’, ‘sort’]

>>> a. len ()

>>> a. getitem (2)

3
While len () returns the length of the python iterables, getitem () takes an index as an argument, and
returns the value at that position in the iterable in python.

4. More Iterables in Python

Most containers are python iterables. Let’s see some relationship using the built-in function issubclass().

>>> import collections

>>> issubclass(collections.Iterator,collections.Iterable)

True
#This means an iterator is a python iterable.

>>> issubclass(collections.Iterable,collections.Iterator)

False

>>> issubclass(collections.Generator,collections.Iterator)

True
#A generator is a Python iterator

>>> issubclass(collections.Generator,collections.Iterable)

True

#So, a generator is also an iterable in python

Let’s check for some more constructs.

>>> issubclass(collections.Set,collections.Iterable)
True

#A set is a python iterable

>>> issubclass(collections.UserDict,collections.Iterable)

True

>>> issubclass(collections.deque,collections.Iterable)

True

>>> issubclass(collections.deque,collections.Iterable)

True

>>> issubclass(collections.defaultdict,collections.Iterable)

True

>>> issubclass(collections.OrderedDict,collections.Iterable)

True

When we say that an iterator is a python iterable, we mean it. In the following code, we define an iterator, and
then call the iter() function on it.

>>> a=iter([1,2,3])

>>> b=iter(a)

>>> b

<list_iterator object at 0x0328BCB0>

>>> a

<list_iterator object at 0x0328BCB0>

>>> next(b)

>>> next(a)

>>> next(b)

3
>>> next(a)

Traceback (most recent call last):

File “<pyshell#242>”, line 1, in <module>

next(a)

StopIteration

What we get back is a python iterator. Hence, an iterator is python iterable. In this code, hence, b and a
reference to the same iterator. Thus, when we call next() on either one, the state is changed for both.

Why should you learn Python?

5. Python Itertools

Python itertools is a module we can use as a standard library for functional programming. We’ll see some of the
functions it offers. First, let’s talk about count().

Python Iterables- Functions of Python Iteratools

a. count() in Python Itertools


The function count() in python Itertools takes, as an argument, an integer number to begin count at. It then
counts infinitely, unless we break out of the for-loop using an if-statement.
>>> from itertools import count

>>> for i in count(7):

if i>14:

break

print(i)

10

11

12

13

14

Let’s try calling it without an argument and without a terminating if-condition.

>>> for i in count():

print(i)

10
11

12

13

14

15

16

17

18

19

20

21

22

23

24

Traceback (most recent call last):

File “<pyshell#265>”, line 2, in <module>

print(i)

KeyboardInterrupt

>>>

Here, we had to press Ctrl+C to interrupt this infinite iterator. We can also give it a positive/ negative interval as
a second argument.

>>> for i in count(2,2):

if i>10: break

print(i)

4
6

10

Now, let’s see another function.

b. cycle() in Python Itertool


Cycle() function in python itertools infinitely iterates over a python iterables, unless we explicitly break out of
the loop.

>>> from itertools import cycle

>>> c=0

>>> for i in cycle(['red','blue']):

if c>7:

break

print(i)

c+=1

red

blue

red

blue

red

blue

red

blue

c. repeat() in Python Itertool


This one repeats an object infinitely unless explicitly broken out of.

>>> from itertools import repeat

>>> c=0
>>> for i in repeat([1,2,3]):

if c>7:

break

print(i)

c+=1

[1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3]

We can also specify the number of times we want it to repeat, as a second argument.

>>> for i in repeat([1,2,3],4):

print(i)

[1, 2, 3] [1, 2, 3] [1, 2, 3]

So, this was all about Python Iterables and Itertools. Hope you like our explanation.

6. Conclusion

Now, we know that a Python iterables are an object that we can iterate on. Likewise, an iterator is a python
object that lets us iterate on an iterator. We also saw the relationships between various classes of a collection.
Finally, we took a brief look at the module ‘itertools’.

Still, have a confusion, feel free to approach us through the comment box!
See Also-

Python Itertools Tutorial – A Quick and Easy Guide


by DataFlair Team · September 26, 2018

1. Python Itertools Tutorial

In our write-up on Python Iterables, we took a brief introduction on the Python itertools module. This is what
will be the point of focus today’s Python Itertools Tutorial. Here, we will learn how to get infinite iterators &
Combinatoric Iterators by Python Itertools. Along with this, we will learn how Python Iterators terminating the
shortest input sequence.

So, let’s start exploring Python Itertools Tutorial.


Python Itertools Tutorial – A Quick and Easy Guide

2. Introduction to Python Itertools

The Python itertools module has functions for creating iterators for efficient looping. While some iterators are
infinite, some terminate on the shortest input sequence. Yet, some are combinatoric. Let’s first discuss infinite
iterators.

3. Infinite Iterators in Python

Some functions are capable of generating infinite iterators. In this Python Itertools tutorial, we will study the
following functions:

a. count([start=0, step=1])
count() may take two values- start and step. It then returns a sequence of values from start, with intervals the
size of step.

>>> from itertools import count

>>> for i in count(10,2):

print(i)

if i>25: break

10
12
14
16
18
20
22
24
26
Here’s count() with one argument:

>>> for i in count(2):

print(i)

2
3
4
5
6
7

Traceback (most recent call last):

File “<pyshell#166>”, line 2, in <module>

print(i)

KeyboardInterrupt

It takes a step of 1. If we call it without an argument, it starts with 0:

>>> for i in count():

print(i)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Traceback (most recent call last):

File “<pyshell#169>”, line 2, in <module>


print(i)

KeyboardInterrupt

b. cycle(iterable)
cycle() makes an iterator from elements from an iterable, and save a copy of each. Once exhausted, it returns
elements from that copy. This repeats indefinitely.

>>> from itertools import cycle

>>> for i in cycle(['red','green','blue']):

print(i)

red
green
blue
red
green
blue
red
green
blue
red
green
blue
red
green
blue

Traceback (most recent call last):

File “<pyshell#174>”, line 2, in <module>

print(i)

KeyboardInterrupt

cycle() can take any kind of an iterable. Refer to Python Iterables for the same.

c. repeat(elem [,n])
This will repeat element elem n-times or endlessly into the iterator.

>>> from itertools import repeat

>>> for i in repeat(‘Red’,3):


print(i)

Red
Red
Red

>>> for i in repeat('Red'):

print(i)

Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red

Traceback (most recent call last):

File “<pyshell#181>”, line 2, in <module>

print(i)

KeyboardInterrupt

4. Combinatoric Iterators in Python

Combinatorial pertains to the arrangement of, operation on, and selection of discrete mathematical elements.
Let’s now talk about combinatoric iterators.

a. product(*iterables, repeat=1)
product() returns the cartesian product of the input iterables. This is equivalent to a nested for-loop.

>>> for i in product([1,2,3],[4,5,6]):

print(i)

(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
This is equivalent to iterating over this generator object:

>>> for i in ((i,j) for i in [1,2,3] for j in [4,5,6]):

print(i)

(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
Let’s take a couple more examples.

>>> for i in product('AB','CD','EF'):

print(i)

(‘A’, ‘C’, ‘E’)


(‘A’, ‘C’, ‘F’)
(‘A’, ‘D’, ‘E’)
(‘A’, ‘D’, ‘F’)
(‘B’, ‘C’, ‘E’)
(‘B’, ‘C’, ‘F’)
(‘B’, ‘D’, ‘E’)
(‘B’, ‘D’, ‘F’)
Because of the repeat argument, the rightmost element advances with every iteration.

>>> for i in product('AB','CD',repeat=2):

print(i)

(‘A’, ‘C’, ‘A’, ‘C’)


(‘A’, ‘C’, ‘A’, ‘D’)
(‘A’, ‘C’, ‘B’, ‘C’)
(‘A’, ‘C’, ‘B’, ‘D’)
(‘A’, ‘D’, ‘A’, ‘C’)
(‘A’, ‘D’, ‘A’, ‘D’)
(‘A’, ‘D’, ‘B’, ‘C’)
(‘A’, ‘D’, ‘B’, ‘D’)
(‘B’, ‘C’, ‘A’, ‘C’)
(‘B’, ‘C’, ‘A’, ‘D’)
(‘B’, ‘C’, ‘B’, ‘C’)
(‘B’, ‘C’, ‘B’, ‘D’)
(‘B’, ‘D’, ‘A’, ‘C’)
(‘B’, ‘D’, ‘A’, ‘D’)
(‘B’, ‘D’, ‘B’, ‘C’)
(‘B’, ‘D’, ‘B’, ‘D’)

b. permutations(iterable,r=None)
permutations() returns r-length permutations of elements in the iterable.
It generates all possible permutations in lexicographic order, and there is no repetition of elements.

>>> from itertools import permutations

>>> for i in permutations('ABCD'):

print(i)

(‘A’, ‘B’, ‘C’, ‘D’)


(‘A’, ‘B’, ‘D’, ‘C’)
(‘A’, ‘C’, ‘B’, ‘D’)
(‘A’, ‘C’, ‘D’, ‘B’)
(‘A’, ‘D’, ‘B’, ‘C’)
(‘A’, ‘D’, ‘C’, ‘B’)
(‘B’, ‘A’, ‘C’, ‘D’)
(‘B’, ‘A’, ‘D’, ‘C’)
(‘B’, ‘C’, ‘A’, ‘D’)
(‘B’, ‘C’, ‘D’, ‘A’)
(‘B’, ‘D’, ‘A’, ‘C’)
(‘B’, ‘D’, ‘C’, ‘A’)
(‘C’, ‘A’, ‘B’, ‘D’)
(‘C’, ‘A’, ‘D’, ‘B’)
(‘C’, ‘B’, ‘A’, ‘D’)
(‘C’, ‘B’, ‘D’, ‘A’)
(‘C’, ‘D’, ‘A’, ‘B’)
(‘C’, ‘D’, ‘B’, ‘A’)
(‘D’, ‘A’, ‘B’, ‘C’)
(‘D’, ‘A’, ‘C’, ‘B’)
(‘D’, ‘B’, ‘A’, ‘C’)
(‘D’, ‘B’, ‘C’, ‘A’)
(‘D’, ‘C’, ‘A’, ‘B’)
(‘D’, ‘C’, ‘B’, ‘A’)
Here, we didn’t pass a second argument to it, so it printed tuples of length 4, which is the length of the iterable.
Now, let’s pass a value 3 to this code.

>>> for i in permutations('ABCD',3):

print(i)

(‘A’, ‘B’, ‘C’)


(‘A’, ‘B’, ‘D’)
(‘A’, ‘C’, ‘B’)
(‘A’, ‘C’, ‘D’)
(‘A’, ‘D’, ‘B’)
(‘A’, ‘D’, ‘C’)
(‘B’, ‘A’, ‘C’)
(‘B’, ‘A’, ‘D’)
(‘B’, ‘C’, ‘A’)
(‘B’, ‘C’, ‘D’)
(‘B’, ‘D’, ‘A’)
(‘B’, ‘D’, ‘C’)
(‘C’, ‘A’, ‘B’)
(‘C’, ‘A’, ‘D’)
(‘C’, ‘B’, ‘A’)
(‘C’, ‘B’, ‘D’)
(‘C’, ‘D’, ‘A’)
(‘C’, ‘D’, ‘B’)
(‘D’, ‘A’, ‘B’)
(‘D’, ‘A’, ‘C’)
(‘D’, ‘B’, ‘A’)
(‘D’, ‘B’, ‘C’)
(‘D’, ‘C’, ‘A’)
(‘D’, ‘C’, ‘B’)

c. combinations(iterable,r)
This returns subsequences of length r from the elements of the iterable.

>>> from itertools import combinations

>>> for i in combinations('ABCD',2):

print(i)

(‘A’, ‘B’)
(‘A’, ‘C’)
(‘A’, ‘D’)
(‘B’, ‘C’)
(‘B’, ‘D’)
(‘C’, ‘D’)
If you noticed, this only returns the tuples that are lexicographically ascending. Let’s take another example.

>>> for i in combinations(range(4),3):

print(i)

(0, 1, 2)
(0, 1, 3)
(0, 2, 3)
(1, 2, 3)

d. combinations_with_replacement(iterable, r)
This returns r-length subsequences of elements of the iterable, where individual elements may repeat.
>>> from itertools import combinations_with_replacement as cwr

>>> for i in cwr('ABCD',2):

print(i)

(‘A’, ‘A’)
(‘A’, ‘B’)
(‘A’, ‘C’)
(‘A’, ‘D’)
(‘B’, ‘B’)
(‘B’, ‘C’)
(‘B’, ‘D’)
(‘C’, ‘C’)
(‘C’, ‘D’)
(‘D’, ‘D’)
Any Doubt in the Python Itertools Tutorial? Please Comment.

5. Iterators Terminating on the Shortest Input Sequence

a. accumulate(iterable [,func])
This makes an iterator with accumulated sums (or accumulated results of a binary function specified).

>>> from itertools import accumulate

>>> for i in accumulate([0,1,0,1,1,2,3,5]):

print(i)

1
1
2
3
5
8
13
This prints the Fibonacci series. Let’s take another example.

>>> import operator

>>> for i in accumulate([1,2,3,4,5],operator.mul):

print(i)

1
2
6
24
120
>>> for i in accumulate([2,1,4,3,5],max):

print(i)

2
2
4
4
5

b. chain(*iterables)
chain() makes an iterator from elements of the first iterable, then from the second, and so on. It moves to the
next iterable as one iterable exhausts.

>>> from itertools import chain

>>> for i in chain('Hello','World','Bye'):

print(i)

H
e
l
l
o
W
o
r
l
d
B
y
e

c. chain.from_iterable(iterable)
This is an alternative constructor to chain(). It takes chained inputs from a single iterable argument, and
evaluates it lazily.

>>> for i in chain.from_iterable(['Hello','World','Bye']):

print(i)

H
e
l
l
o
W
o
r
l
d
B
y
e
You don’t need to additionally import anything for this.

d. compress(data, selectors)
This makes an iterator that filters elements, from data, for which selector values amount to True.

>>> from itertools import compress

>>> for i in compress('ABCDEF',[1,0,1,True,0,' ']):

print(i)

A
C
D
F

e. dropwhile(predicate,iterable)
As long as the predicate is True, it drops elements from the iterable. As soon as it is False, it starts returning
every element.

>>> from itertools import dropwhile

>>> for i in dropwhile(lambda x:x<7,[1,2,7,9,5,3,2,9]):

print(i)

7
9
5
3
2
9

f. filterfalse(predicate,iterable)
This makes an iterator that filters those elements out from the iterator, for which the predicate is True.

>>> from itertools import filterfalse

>>> for i in filterfalse(lambda x:x<7,[1,2,7,9,5,3,2,9]):

print(i)
7
9
9

>>> for i in filterfalse(lambda x:x%2,[1,2,7,9,5,3,2,9]):

print(i)

2
2

g. groupby(iterable,key=None)
This makes an iterator that takes the iterable, and returns consecutive keys and groups. These are sub-iterators
grouped by the key.

>>> from itertools import groupby

>>> for i,j in groupby('AAAAABBCCCCCDDDCCCBBA'):

print(list(j))

[‘A’, ‘A’, ‘A’, ‘A’, ‘A’] [‘B’, ‘B’] [‘C’, ‘C’, ‘C’, ‘C’, ‘C’] [‘D’, ‘D’, ‘D’] [‘C’, ‘C’, ‘C’] [‘B’, ‘B’] [‘A’]

h. islice(iterable,stop)
islice(iterable,start,stop [,step])
This makes an iterator that returns selected elements from the iterable.

>>> from itertools import islice

>>> for i in islice([1,2,3,4,5],2):

print(i)

1
2

>>> for i in islice([1,2,3,4,5],2,5):

print(i)

3
4
5

>>> for i in islice([1,2,3,4,5],0,5,2):

print(i)
1
3
5

i. starmap(function,iterable)
This makes an iterator that takes arguments from the iterable, and computes a function.

>>> from itertools import starmap

>>> for i in starmap(operator.sub,[(2,1),(7,3),(15,10)]):

print(i)

1
4
5

j. takewhile(predicate,iterable)
This makes an iterator which returns elements from the iterator as long as the predicate amounts to True. This is
in contrast to dropwhile().

>>> from itertools import takewhile

>>> for i in takewhile(lambda x:x<7,[1,2,7,9,5,3,2,9]):

print(i)

1
2

k. tee(n=2)
This splits an iterator into n independent iterators.

>>> from itertools import tee

>>> for i in tee([1,2,3,4,5,6,7],3):

for j in i: print(j)

print()

1
2
3
4
5
6
7
1
2
3
4
5
6
7

1
2
3
4
5
6
7

l. zip_longest(*iterables,fillvalue=None)
This makes an iterator by aggregating elements from each iterable. The fillvalue parameter tells it a value to fill
for the remaining places in the shorter iterable.

>>> from itertools import zip_longest

>>> for i in zip_longest('ABC','12345',fillvalue='*'):

print(i)

(‘A’, ‘1’)
(‘B’, ‘2’)
(‘C’, ‘3’)
(‘*’, ‘4’)
(‘*’, ‘5’)

>>> for i in zip_longest('ABC','12345','Hello',fillvalue='*'):

print(i)

(‘A’, ‘1’, ‘H’)


(‘B’, ‘2’, ‘e’)
(‘C’, ‘3’, ‘l’)
(‘*’, ‘4’, ‘l’)
(‘*’, ‘5’, ‘o’)

So, this was all about the Python Itertools Tutorial. Hope you like our explanation.

6. Conclusion

In this Python Itertools tutorial, we discussed three kinds of iterators generated by functions from the module
itertools. These are- infinite, combinatorics, and those which terminate on the shortest input sequence.
Furthermore, if you have any query, feel free to ask in the comment section.
Python Property – The Problem and Solution
by DataFlair Team · September 26, 2018

1. Python Property

In this tutorial on Python Property, you will learn how to use setters and getters using property. Moreover, we
will discuss the problems and solutions related to Python Property. Before we begin, take a look at Classes and
Objects.

So, let’s start Python Property Tutorial.

Python Property

2. Python Property – The Problem

Let’s first look at a Python class Song.

>>> class Song:

def init (self,title):

self.title=title

def show_title(self):

print(f"I'm listening to {self.title}")

We then create an object Teddy_Bear, and call show_title() on it.

>>> Teddy_Bear=Song('Teddy Bear')


>>> Teddy_Bear.show_title()

I’m listening to Teddy Bear


Here, we can get and set the title instance attribute this way:

>>> Teddy_Bear.title #getting the title

‘Teddy Bear’

>>> Teddy_Bear.title='TEDDY BEAR' #setting the title

>>> Teddy_Bear.title

‘TEDDY BEAR’
Checking the object’s dictionary, we find this:

>>> Teddy_Bear. dict

{‘title’: ‘TEDDY BEAR’}


The problem: What if clients begin accessing and modifying the variable ‘title’? That could cause all kinds of
chaos.

3. Python Property – A Possible Solution

Let’s do two things:

Define setter and getter functions. To set and get a value, we define the following functions:

>>> def get_title(self):

return self._title

>>> def set_title(self,title):

self._title=title.upper()

Here, get_title returns the title of the book, and set_title converts it to all caps (we saw this in

Python Built-in Functions)

Make the variable ‘title’ private. To do this, we use a leading underscore. After all these changes, our class
looks like this:

>>> class Song:

def init (self,title):

self.title=title

def show_title(self):
print(f"I'm listening to {self.title}")

def get_title(self):

return self._title

def set_title(self,title):

self._title=title.upper()

Well, the underscore doesn’t really make the variable private, because Python does not implement any such
restriction at its level. But these are the norms that we must follow.
But the problem with this solution is that we will need to update ‘Teddy_Bear.title’ to ‘Teddy_Bear.get_title()’
and ‘Teddy_Bear.title=’TEDDY BEAR’’ to ‘Teddy_Bear.set_title()’ in each occurrence. Real-world code,
clients may have thousands of lines where they implemented our class. In that case, it becomes tedious to
refactor every single occurrence. This makes it not backward-compatible. So, let’s look at a better option next.
Actually, we will just add a line to this approach, and it will work like magic.

4. Python Property – The Solution

To solve this problem, Python lends us the function property(). This is what this property of Python looks like:

property(fget, fset, fdel, doc)

Here, fget takes a getter, fset takes a setter, fdel takes a function to delete an attribute, and doc is a string. These
arguments, however, are optional. Here is a call to property() without any arguments:

>>> property()

<property object at 0x062CDDB0>

For fget, fset, and fdel, a property object has the following methods: getter(), setter(), and delete(). So, this is
equivalent to this:

>>> title=property() #make empty property

>>> title=title.getter(get_title) #assign fget

>>> title=title.setter(set_title) #assign fset

For our class Song, we add the following line:

>>> title=property(get_title,set_title)

This makes the class look like this:

>>> class Song:

def init (self,title):

self.title=title

def show_title(self):
print(f"I'm listening to {self.title}")

def get_title(self):

return self._title

def set_title(self,title):

self._title=title.upper()

title=property(get_title,set_title)

Now, let’s try accessing the title for the object Teddy_Bear.

>>> Teddy_Bear=Song('Teddy Bear')

>>> Teddy_Bear.title

‘TEDDY BEAR’

>>> Teddy_Bear.show_title()

I’m listening to TEDDY BEAR

This implementation is backward-compatible. This saves us from all the work of refactoring. Also note that it is
_title that holds the actual value of the book’s title. ‘title’ is just a property object that makes it happen.

5. Some Syntactic Sugar

We have seen the @-syntax in our lesson on Python Property Decorators. Here, we do this:

>>> class Song:

def init (self,title):

self.title=title

def show_title(self):

print(f"I'm listening to {self.title}")

@property

def title(self):

return self._title

@title.setter

def title(self,title):

self._title=title.upper()
Firstly, note that we removed the call to property. Next, we ditched the names ‘get_title’ and ‘set_title’, and
used ‘title’ instead. Finally, we used @property before the getter, and @title.setter before the setter.
Let’s access this now.

>>> Teddy_Bear=Song('Teddy Bear')

>>> Teddy_Bear.title

‘TEDDY BEAR’

>>> Teddy_Bear.show_title()

I’m listening to TEDDY BEAR

So, this was all about Python Property Tutorial. Hope you like ou explanation.

6. Conclusion

In this tutorial on Python Property, we learned about different problem and solutions of this problems.
Comment a doubt or leave a suggestion. We surely get back to you!

Python Sequence and Collections – Operations, Functions, Methods


by DataFlair Team · September 26, 2018

1. Python Sequence Tutorial

This blog is dedicated to a revision of the Python sequence and collections. In this Python Sequence Tutorial,
we will discuss 6 types of Sequence: String, list, tuples, Byte sequences, byte array, and range object.
Moreover, we will discuss Python sequence operations, functions, and methods. At last, we will cover python
collection: sets and dictionaries.

So, let’s start Python Sequence and Collections


Python Sequence and Collections – Operations, Functions, Methods

2. Python Sequence

So, what is a Python sequence, and how does it differ from a Python collection? A sequence is a group of items
with a deterministic ordering. The order in which we put them in is the order in which we get an item out from
them.

Python offers six types of sequences. Let’s discuss them.

a. Python Strings
A string is a group of characters. Since Python has no provision for arrays, we simply use strings. This is how
we declare a string:

Python Sequence or Collection

We can use a pair of single or double quotes. And like we’ve always said, Python is dynamically-typed. Every
string object is of the type ‘str’.

>>> type(name)

<class ‘str’>

To declare an empty string, we may use the function str():

>>> name=str()

>>> name

>>> name=str('Ayushi')

>>> name

‘Ayushi’

>>> name[3]

‘s’
To learn about strings in detail, read our chapter on Python Strings.

b. Python Lists
Since Python does not have arrays, it has lists. A list is an ordered group of items. To declare it, we use square
brackets.

>>> groceries=['milk','bread','eggs']

>>> groceries[1]
‘bread’

>>> groceries[:2]

[‘milk’, ‘bread’] A Python list can hold all kinds of items; this is what makes it heterogenous.

>>> mylist=[1,'2',3.0,False]

Also, a list is mutable. This means we can change a value.

>>> groceries[0]='cheese'

>>> groceries

[‘cheese’, ‘bread’, ‘eggs’] A list may also contain functions.

>>> groceries[0]='cheese'

>>> groceries

print(“Hi”)

>>> newlist=[sayhi,sayhi]

>>> newlist[0]

<function sayhi at 0x05907300>

>>> newlist[0]()

Hi
To learn more about lists, read up on Python Lists.

c. Python Tuples
A tuple, in effect, is an immutable group of items. When we say immutable, we mean we cannot change a single
value once we declare it.

>>> name=('Ayushi','Sharma')

>>> type(name)

<class ‘tuple’>
We can also use the function tuple().

>>> name=tuple(['Ayushi','Sharma'])

>>> name

(‘Ayushi’, ‘Sharma’)
Like we said, a tuple is immutable. Let’s try changing a value.
>>> name[0]='Avery'

Traceback (most recent call last):

File “<pyshell#594>”, line 1, in <module>

name[0]=’Avery’

TypeError: ‘tuple’ object does not support item assignment

To learn more about tuples, read Tuples in Python.

Also read up on Python namedtuple.

d. Bytes Sequences
The function bytes() returns an immutable bytes object. We dealt with this when we talked Built-in Functions in
Python. Let’s take a few examples.

>>> bytes(5)

b’\x00\x00\x00\x00\x00′

>>> bytes([1,2,3,4,5])

b’\x01\x02\x03\x04\x05′

>>> bytes('hello','utf-8')

b ‘hello’

Here, utf-8 is the encoding we used.

Since it is immutable, if we try to change an item, it will raise a TypeError.

>>> a=bytes([1,2,3,4,5])

>>> a

b’\x01\x02\x03\x04\x05′

>>> a[4]=3

Traceback (most recent call last):

File “<pyshell#46>”, line 1, in <module>

a[4]=3

TypeError: ‘bytes’ object does not support item assignment


e. Bytes Arrays
In that article, we discussed this one too. A bytesarray object is like a bytes object, but it is mutable. It returns
an array of the given byte size.

>>> a=bytearray(4)

>>> a

bytearray(b’\x00\x00\x00\x00′)

>>> a=bytearray(4)

>>> a

bytearray(b’\x00\x00\x00\x00\x01′)

>>> a[0]=1

>>> a

bytearray(b’\x01\x00\x00\x00\x01′)

>>> a[0]

1
Let’s try doing this on a list.

>>> bytearray([1,2,3,4])

bytearray(b’\x01\x02\x03\x04′)

Finally, let’s try changing a value.

>>> a=bytearray([1,2,3,4,5])

>>> a

bytearray(b’\x01\x02\x03\x04\x05′)

>>> a[4]=3

>>> a

bytearray(b’\x01\x02\x03\x04\x03′)

See? It is mutable.

f. range() objects
A range() object lends us a range to iterate on; it gives us a list of numbers.
>>> a=range(4)

>>> type(a)

<class ‘range’>

>>> for i in range(7,0,-1):

print(i)

7
6
5
4
3
2
1
We took an entire post on Range in Python.

3. Python Sequence Operations

Since we classify into sequences and collections, we might as well discuss the operations we can perform on
them. For simplicity, we will demonstrate these on strings.

Concatenation

Concatenation adds the second operand after the first one.

>>> 'Ayu'+'shi'

‘Ayushi’

Integer Multiplication

We can make a string print twice by multiplying it by 2.

>>> 'ba'+'na'*2

‘banana’

Membership

To check if a value is a member of a sequence, we use the ‘in’ operator.

>>> 'men' in 'Disappointment'

True
You can read more about these operations in Python Operators.

Python Slice
Sometimes, we only want a part of a sequence, and not all of it. We do it with the slicing operator.

>>> 'Ayushi'[1:4]

‘yus’

4. Python Sequence Functions

len()

A very common and useful function to pass a sequence to is len(). It returns the length of the Python sequence.

>>> len('Ayushi')

min() and max()

min() and max() return the lowest and highest values, respectively, in a Python sequence.

>>> min('cat')

‘a’

>>> max('cat')

‘t’
This comparison is based on ASCII values.

5. Python Sequence Methods

There are some methods that we can call on a Python sequence:

Python index()

This method returns the index of the first occurrence of a value.

>>> 'banana'.index('n')

Python count()

count() returns the number of occurrences of a value in a Python sequence.

>>> 'banana'.count('na')

>>> 'banana'.count('a')
3

6. Python Collections

Python collection, unlike a sequence, does not have a deterministic ordering. Examples include sets and
dictionaries. In a collection, while ordering is arbitrary, physically, they do have an order.

Every time we visit a set, we get its items in the same order. However, if we add or remove an item, it may
affect the order.

a. Python Set
A set, in Python, is like a mathematical set in Python. It does not hold duplicates. We can declare a set in two
ways:

>>> nums={2,1,3,2}

>>> nums

{1, 2, 3}

>>> nums=set([1,3,2])

>>> nums

{1, 2, 3}
A set is mutable.

>>> nums.discard(2)

>>> nums

{1, 3}
But it may not contain mutable items like lists, dictionaries, or other sets.

To learn more about sets, read up on Python Sets and Booleans.

Any doubt in Python Sequences and Collections yet? Please Comment.

b. Python Dictionaries
Think of a dictionary as a real-life dictionary. It holds key-value pairs, and this is how we declare it:

>>> a={'name':1,'dob':2}

Or, you could do:

>>> a=dict()

>>> a['name']=1
>>> a['dob']=2

>>> a

{‘name’: 1, ‘dob’: 2}
We have yet another way to create a dictionary- a dictionary comprehension.

>>> a={i:2**i for i in range(4)}

>>> a

{0: 1, 1: 2, 2: 4, 3: 8}
However, a key cannot be of an unhashable type.

>>> a={[1,2,3]:1,1:[1,2,3]}

Traceback (most recent call last):

File “<pyshell#629>”, line 1, in <module>

a={[1,2,3]:1,1:[1,2,3]}

TypeError: unhashable type: ‘list’

To know more about dictionaries, read up on Python Dictionaries. You should also check out defaultdict and
OrderedDict in Python.

So, this was all about the Python sequence and collections tutorial. Hope you like our explanation.

4. Conclusion

To conclude this Python Sequence and collection tutorial, we will say that a sequence has a deterministic
ordering, but a collection does not. Examples of sequences include strings, lists, tuples, bytes sequences, bytes
arrays, and range objects. Those of collections include sets and dictionaries.
If you have any query regarding Python Sequence and Collection Tutorial, Please Comment.

Learn Python Stemming and Lemmatization – Python NLTK


by DataFlair Team · September 28, 2018

1. Python Stemming and Lemmatization

In this Python Stemming tutorial, we will discuss Stemming and Lemmatization in Python Programming
Language– two basics when working with data science in Python. Moreover, we will discuss Python NLTK
and Python Stemming examples. Along with this, we will learn Python Stemming vs Lemmatization.

So, let’s begin Python Stemming and Lemmatization.


Python Stemming and Lemmatization – NLTK

2. Prerequisites for Python Stemming and Lemmatization

For our purpose, we will use the following library-

a. Python NLTK
Python NLTK is an acronym for Natural Language Toolkit. It is a set of libraries that let us perform Natural
Language Processing (NLP) on English with Python. It lets us do so in a symbolic and statistical way. It also
provides sample data and supports graphical representation.

Do you How Python Rename File – Single & Multiple Files With Example

You can install it using pip-


C:\Users\lifei>pip install nltk
Collecting nltk

Downloading

https://files.pythonhosted.org/packages/50/09/3b1755d528ad9156ee7243d52aa5cd2b809ef053a0f31b53d92853
dd653a/nltk-3.3.0.zip (1.4MB)
100% |████████████████████████████████| 1.4MB 669kB/s

Requirement already satisfied: six in c:\users\lifei\appdata\local\programs\python\python36\lib\site-packages


(from nltk) (1.11.0)

Installing collected packages: nltk


Running setup.py install for nltk … done
Successfully installed nltk-3.3
3. What is Python Stemming?

Python Stemming is the act of taking a word and reducing it into a stem. A stem is like a root for a word- that
for writing is writing. But this doesn’t always have to be a word; words like study, studies, and studying all stem
into the word studi, which isn’t actually a word.

Python Lemmatization and Stemming – Python NLTK

It is almost like these words are synonyms; this lets us normalize sentences and makes searching for words
easier and faster. The Python stemming algorithms we have are often based on rules applying to suffix-
stripping. The most common is the Porter-Stemmer, which has been around since 1979.
Read about Python Read And Write File – File Handling In Python

a. Python Stemming Individual Words


>>>import nltk

>>> from nltk.stem import PorterStemmer

>>> words=['write','writer','writing','writers']

>>> ps=PorterStemmer()

>>> for word in words:

print(f"{word}: {ps.stem(word)}")

Output-
write: write
writer: writer
writing: write
writers: writer
Now let’s try some more words.

>>> ps.stem('written')

‘written’

>>> ps.stem('wrote')

‘wrote’

>>> ps.stem('writable')

‘writabl’

>>> ps.stem('writes')

‘write’

b. Another Example of Python Stemming


Let’s try more words.

>>> ps.stem('game')

‘game’

>>> ps.stem('gaming')

‘game’

>>> ps.stem('gamed')

‘game’

>>> ps.stem('games')

‘game’
Let’s Explore Difference Between Method and Function in Python

c. Python Stemming an Entire Sentence


>>> from nltk.tokenize import word_tokenize

>>> nltk.download('punkt')

>>> sentence='I am enjoying writing this tutorial; I love to write and I have written 266 words so far. I
wrote more than you did; I am a writer.'

>>> words=word_tokenize(sentence)
>>> for word in words:

print(f"{word}: {ps.stem(word)}")

I: I
am: am
enjoying: enjoy
writing: write
this: thi
tutorial: tutori
;: ;
I: I
love: love
to: to
write: write
and: and
I: I
have: have
written: written
266: 266
words: word
so: so
far: far
.: .
I: I
wrote: wrote
more: more
than: than
you: you
did: did
;: ;
I: I
am: am
a: a
writer: writer
.: .

4. What is Python Lemmatization?

Python Lemmatization lets us group together inflected forms of a word. It links words with similar meanings to
one word and maps various words onto one root.
Let’s Discuss Python Regular Expressions | Python Regex Tutorial
What is Python Lemmatization?

a. Python Stemming vs Lemmatization


But how Python Lemmatization is different from stemming? While stemming can create words that do not
actually exist, Python lemmatization will only ever result in words that do. lemmas are actual words.

>>> ps.stem('indetify')

‘indetifi’

>>> lemmatizer.lemmatize('identify')

‘identify’

b. Python Lemmatization Examples


>>> from nltk.stem import WordNetLemmatizer

>>> lemmatizer=WordNetLemmatizer()

>>> nltk.download('wordnet')

>>> lemmatizer.lemmatize('dogs')

‘dog’

>>> lemmatizer.lemmatize('geese')

‘goose’
>>> lemmatizer.lemmatize('cacti')

‘cactus’

>>> lemmatizer.lemmatize('erasers')

‘eraser’

>>> lemmatizer.lemmatize('children')

‘child’

>>> lemmatizer.lemmatize('feet')

‘foot’
Let’s Learn Python Debugger with Examples

c. Using Pos
>>> lemmatizer.lemmatize('better',pos='a')

‘good’

Here, pos is a speech parameter, which is noun by default. This means Python will try to find the closest noun.

>>> lemmatizer.lemmatize('redder','a')

‘red’
Since, Python lemmatization considers whether a word is a noun, a verb, an adjective, an adverb, and so, Python
needs to find out about a word’s context.

So, this was all about Stemming and Lemmatization in Python & Python NLTK. Hope you like our explanation.

5. Conclusion
Hence, in this Python tutorial, we studied Python Stemming and Lemmatization. In addition, we studied NLTK,
an example of Stemming and Lemmatization in Python, and the difference between Python Stemming and
Lemmatization. Tell us what you think about this Python Lemmatization and Stemming tutorial, in the
comments Box.

Learn Aggregation and Data Wrangling with Python


by DataFlair Team · September 28, 2018

1. Objective

Our previous tutorial, we talked about Python Django. Today in this Data Wrangling tutorial, we will see
Python Aggregation and Data Wrangling with Python Programming Language. Moreover, we will discuss
prerequisites & reasons to use Data Wrangling with Python. In addition, we discuss Dropping Missing
Values, Grouping Data, Filtering Data, Pivoting Dataset, Shifted Datasets, Melted Data, Reducing into an
ABT, Concatenating Data, Exporting Data, and Python Aggregate.

So, let’s start Aggregation and Data Wrangling with Python.

Aggregation and Data Wrangling with Python

2. A Brief on DataFrames

A DataFrame in pandas is a 2-dimensional data structure which holds data in a tabular sense. This means it lets
us work in a context of rows and columns.
A dataframe is-

Mutable.

Capable of holding columns of different types.

Capable of performing arithmetic operations on rows and columns.

A holder of labeled axes for the rows and columns.

This is the DataFrame constructor we have-


pandas.DataFrame (data, index, columns, dtype, copy)
Read about Python Data File Formats – How to Read CSV, JSON, and XLS Files

3. Python Data Wrangling – Prerequisites

a. Python pandas
For aggregation and Data wrangling with Python, you will need the pandas’ library. It helps us with data
manipulation and analysis. It has data structures and allows operations that we can use to manipulate numerical
tables and time series.

Aggregation and Data Wrangling with Python

You can install it using the following command-

C:\Users\lifei>pip install pandas

At the time of writing, we use the version 0.23.1 of pandas.

b. Python NumPy
NumPy is another Python library that lets us handle large, multi-dimensional arrays and matrices. It also offers
various high-level mathematical functions to help us deal with these.

Aggregation and Data Wrangling with Python

To install this, you can try the following command in the command prompt-

C:\Users\lifei>pip install numpy

Do you How to Work with NoSQL Database in Python using PyMongo

c. Python DataFrames
For our purpose, we will need two dataframes. We can create a DataFrame using any data type like list,
dictionary, or series. We can also use a NumPy and array or another DataFrame to create it. Let’s use a
dictionary for now.

>>> one=pandas.DataFrame({

'emp_id':[1,2,3,4,5],

'dept_name':['duvet','bidet','footwear','clothing','electronics'], 'aisle':

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

>>> first=pandas.DataFrame(one)
>>> two=pandas.DataFrame({ 'emp_id':

[6,7,8,9,10],

'dept_name':['grocery','toys','laundry','frozen','stationery'], 'aisle':

[6,2,2,9,10]})

>>> second=pandas.DataFrame(two)

We also create a csv file with utf-8 format in Excel-

Aggregation and Data Wrangling with Python

We save this as supermarket.csv.

4. Why we need Data Wrangling with Python

Much data obtained from various sources are raw and unusable. This could be messy or incomplete. With data
wrangling with Python, we can perform operations on raw data to clean it out to an extent. Wrangling is
essential to data science. Let’s take a quick look at it.

5. Dropping Missing Values

As you can see, we have no aisle number for the frozen department in our csv file.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> three=pandas.read_csv('supermarket.csv')

>>> pandas.isnull(three).any()

emp_id False

dept_name False

aisle True

dtype: bool
This shows us that the ‘aisle’ column does have a missing value. The following command shows us the null
values in the file-

>>> three.head()

Aggregation and Data Wrangling with Python

Let’s Revise Python File I/O – Python Write to File and Read File
To drop this record, we use the following command-

>>> three.dropna()

Data Wrangling with Python – Dropping Missing Values

6. Grouping Data

The pandas gorupby() method returns a DataFrameGroupBy object. Calling the method value_counts() returns
the number of occurrences for each unique value in the column we specify.

>>> three.groupby('aisle').dept_name.value_counts()

Data Wrangling with Python – Grouping Data

This tells us about the correlation between aisle number and department name. This lets us discover trends in
data. In situations where we want to see trends by the city, we can group records by their geographical factors.

a. Finding unique values


For this, we can use the unique() method.

>>> two.aisle.unique()

array([ 6, 2, 9, 10], dtype=int64)

b. Length of dataframe
Then len() function gives us the length of the dataframe.

>>> len(two)

5
Do you know How I Used These 5 Steps to Learn Python for Data Science

7. Filtering Data

To gain a subset of data using some criterion, we can filter out parts we need.

Data Wrangling with Python – Filtering Data

a. Single Conditions
>>> two[two['aisle']>3]
Filtering Data – Single Conditions

>>> two[two['aisle']==2]

Filtering Data – Single Conditions

b. Multiple Conditions
>>> two[(two['aisle']==2) & (two['dept_name']=='toys')]

Filtering Data – Multiple Conditions

Let’s Read about Python Counter – Python Collections Type

c. Describing your Data


>>> (two[(two['aisle']==2) & (two['dept_name']=='toys')]).describe()

Filtering Data – Describing your Data

d. Filtering through Values


>>> two[two.aisle.isin([2,7,9])]
Filtering through Values

8. Pivoting Dataset

Pivoting lets us reshape a DataFrame by column/index values. In other words, it produces a pivot table.

>>> (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail()

Data Wrangling with Python – Pivoting Dataset

Providing a number to this tail() call will give us that many records.

>>> (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail(3)

Data Wrangling with Python – Pivoting Dataset

a. Shifting a pivoted dataset


We can shift the DataFrame’s index by a certain number of periods using the shift() method.
>>> (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail(3).shift(1)

Data Wrangling with Python – Shifting a pivoted dataset

Let’s Discuss Python Operator Overloading and Python Magic Methods

9. Melting Shifted Datasets

We can also melt a dataset (unpivot it).

>>> (two.melt(id_vars=['emp_id'],value_name='dept_name')).tail()

Data Wrangling with Python – Melting Shifted Datasets

10. Merging Melted Data


We can join melted DataFrames into one Analytical Base Table using the merge() function.

>>> melt1=two.melt(id_vars=['emp_id'],value_name='dept_name')

>>> melt2=two.melt(id_vars=['emp_id'],value_name='aisle')

>>> pandas.merge(melt1,melt2,on=['emp_id']).tail(1)
Aggregation and Data Wrangling with Python

11. Reducing into an ABT

Finally, to reduce this result into an analytical base table, we do the following:

>>> from functools import reduce

>>> base=two[['aisle','dept_name','emp_id']]

>>> feature=[base]+[melt1,melt2]

>>> abt=reduce(lambda left,right: pandas.merge(left,right,on=['emp_id']),[melt1,melt2])

>>> abt.tail(1)

Data Wrangling with Python – Reducing into an ABT

12. Concatenating Data

We can concatenate datasets along an axis using the concat() method.

>>> pandas.concat([one,two])
Concatenating Data

Have a look – Lessons on Python Sequences and Collections

13. Exporting Data

Finally, we can export our clean and filtered data to an Excel sheet.

>>> res=pandas.concat([one,two])

>>> res.to_excel('res.xlsx')

14. How Python Aggregate Data?

To apply aggregations on our datasets, we can use the Python numpy library along with pandas.

Learn Aggregation and Data Wrangling with Python

Let’s create a dataframe first.

>>> df=pandas.DataFrame(numpy.random.randn(7,3),

index=pandas.date_range('1/1/2000',periods=7),

columns=['A','B','C'])

>>> print(df)
How Python Aggregate Data

>>> print(df.rolling(window=3,min_periods=1))

Rolling [window=3,min_periods=1,center=False,axis=0]

Let’s explore The Best Article on Python’s Various Libraries

a. Python Aggregating an entire dataframe


We can aggregate an entire dataframe as-

>>> r=df.rolling(window=3,min_periods=1)

>>> r.aggregate(numpy.sum)

How Python Aggregate Data

As you can see, this keeps aggregating the previous value into itself. Another aggregation we can use is max-

>>> r.aggregate(numpy.max)
Python Aggregating an entire dataframe

Other aggregations include mean, median, and min.

b. Python Aggregating one column


To aggregate just one column, we can use its index.

>>> r['B'].aggregate(numpy.mean)

Python Aggregating one column

Let’s See the Python Property – The Problem and Solution

c. Python Aggregating multiple columns


We can also specify more than one column to aggregate.

>>> r[['B','C']].aggregate(numpy.sum)
Python Aggregating multiple columns

So, this was all about Aggregation and Data Wrangling with Python. Hope you like our explanation.

15. Conclusion

Hence, this was how to clean your data with wrangling and then how to apply aggregations on it. Did we miss
out on something in Aggregation and Data Wrangling with Python? Drop your suggestions in the comments.

Python Data Cleansing by Pandas & Numpy | Python Data


Operations
by DataFlair Team · September 28, 2018

1. Python Data Cleansing – Objective

In our last Python tutorial, we studied Aggregation and Data Wrangling with Python. Today, we will discuss
Python Data Cleansing tutorial, aims to deliver a brief introduction to the operations of data cleansing and how
to carry your data in Python Programming. For this purpose, we will use two libraries- pandas and numpy.
Moreover, we will discuss different ways to cleanse the missing data.
So, let’s start the Python Data Cleansing.
Python Data Cleansing by pandas & numpy | Python Data Operations

2. Python Data Cleansing – Prerequisites

As mentioned earlier, we will need two libraries for Python Data Cleansing – Python pandas and Python
numpy.

a. Pandas
Python pandas is an excellent software library for manipulating data and analyzing it. It will let us manipulate
numerical tables and time series using data structures and operations.

Python Data Cleansing – Python Pandas

You can install it using pip-

C:\Users\lifei>pip install pandas

Do You Know What is Exception Handling in Python Programming

b. Numpy
Python numpy is another library we will use here. It lets us handle arrays and matrices, especially those
multidimensional. It also provides several high-level mathematical functions to help us operate on these.

Python Data Cleansing – Python numpy

Use the following command in the command prompt to install Python numpy on your machine-
C:\Users\lifei>pip install numpy

3. Python Data Cleansing Operations on Data using NumPy

Using Python NumPy, let’s create an array (an n-dimensional array).

>>> import numpy as np

>>> np.array(['a','b','c','d','e'],ndmin=2)

array([[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]], dtype='<U1′)

>>> np.array([['a','b'],['c','d','e']])<span style="background-color: #fafafa;color: #333333;font-family:


Verdana, Geneva, sans-serif;font-size: 16px;font-weight: inherit"> </span>

array([list([‘a’, ‘b’]), list([‘c’, ‘d’, ‘e’])], dtype=object)

>>> np.array(['a','b','c','d','e'],ndmin=1)<span style="background-color: #fafafa;color: #333333;font-family:


Verdana, Geneva, sans-serif;font-size: 16px;font-weight: inherit"> </span>

array([‘a’, ‘b’, ‘c’, ‘d’, ‘e’], dtype='<U1′)

>>> np.array([1,2,7,9,8],dtype=complex)<span style="background-color: #fafafa;color: #333333;font-


family: Verdana, Geneva, sans-serif;font-size: 16px;font-weight: inherit"> </span>

array([1.+0.j, 2.+0.j, 7.+0.j, 9.+0.j, 8.+0.j])


While dtype lets us tell the interpreter of the data type to use, admin, lets us define the minimum dimension.
The following parameters will give us information about the array-

>>> a=np.array(['a','b',2,'3.0'])

>>> a

array([‘a’, ‘b’, ‘2’, ‘3.0’], dtype='<U3′)

>>> type(a)

<class 'numpy.ndarray'>

>>> a.ndim

>>> a.shape

(4,)

>>> a.size

4
>>> a.dtype

dtype('<U3')

We can also perform operations like:

>>> b=np.array([[1,2,3],[4,5,6]])

>>> b

array([[1, 2, 3],
[4, 5, 6]])

>>> b.flatten()

array([1, 2, 3, 4, 5, 6])

>>> b.reshape(3,2)

array([[1, 2],
[3, 4],
[5, 6]])

>>> b[:2,::2]

array([[1, 3],
[4, 6]])

>>> b-4

array([[-3, -2, -1],


[ 0, 1, 2]])

>>> b.sum()

21

>>> b-2*b

array([[-1, -2, -3],


[-4, -5, -6]])

>>> np.sort(np.array([[3,2,1],[5,2,4]]))

array([[1, 2, 3],
[2, 4, 5]])

4. Python Data Cleansing Operations on Data Using pandas

Pandas use three types to hold data- DataFrame, Panel, and Series.
Operations on Data Using Python pandas

a. DataFrame
Pandas DataFrame is a data structure that holds data in two dimensions- as rows and columns. We have the
following syntax-

pandas.DataFrame(data, index, columns, dtype, copy)

Now let’s try an example-

>>> import pandas as pd

>>> data={'Element':['Silver','Gold','Platinum','Copper'],'Atomic Number':[47,79,78,29]}

>>> frame=pd.DataFrame(data,index=['element 1','element 2','element 3','element 4'])

>>> frame

Python Pandas – DataFrame

Have a Look at Python Inheritance, Method Overloading and Method Overriding

b. Panel
Pandas panel holds data in three dimensions. Etymologically, the term panel data from one source for the name
pandas. A panel has the following syntax:

pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)

>>> data={'Red':pd.DataFrame(np.random.randn(4,2)),

'Blue':pd.DataFrame(np.random.randn(4,3))}

>>> pd.Panel(data)

<class ‘pandas.core.panel.Panel’>

Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)

Items axis: Blue to Red

Major_axis axis: 0 to 3

Minor_axis axis: 0 to 2

c. Series
Pandas Series holds data in one dimension, in a labeled format. The index is the set of axis labels we use.
It has the following syntax-

pandas.Series(data, index, dtype, copy)

Let’s take an example.

>>> data=np.array([1,2,3,3,4])

>>> pd.Series(data)

01
12
23
33
44
dtype: int32
Let’s take another example.

>>> pd.Series(np.array(['a','c','b']))

0a
1c
2b
dtype: object
Using these data structures, we can manipulate data in many ways-

>>> frame.iloc[0:2,:]
Python Data Cleansing by pandas & numpy | Python Data Operations

>>> frame.describe()

Python Pandas – Series “Describe”

>>> frame.rank()

Python Pandas – Series “Rank”

This is all for now; we will learn about the libraries pandas and numpy in their own tutorials.
Read about Python Iterables and Python Itertools with Examples

5. Python Data Cleansing

When some part of our data is missing, due to whichever reason, the accuracy of our predictions plummets. In
our article on data wrangling and aggregation, we discussed missing data and how to drop it. Let’s see how
we can deal with this issue.

In real-time situations like the comment section of our website. The name and email are mandatory, but the
input for ‘website’ can be left empty. Some users may not run a website to be eligible to fill in this information.
In ways like this and others, we may end up with missing data in some places. How should we go about with
this? Let’s find out.
Python Pandas will depict a missing value as NaN, which is short for Not a Number. Simply using the reindex()
method will fill in NaN for blank values.

>>> frame=pd.DataFrame(np.random.randn(4,3),index=[1,2,4,7],columns=['A','B','C'])

>>> frame.reindex([1,2,3,4,5,6,7])

Python Data Cleansing

a. Finding which columns have missing values


In the tutorial on wrangling, we saw how to find out which columns have missing values-

>>> frame=frame.reindex([1,2,3,4,5,6,7])

>>> frame['B'].isnull()

1 False

2 False

3 True

4 False

5 True

6 True

7 False

Name: B, dtype: bool

6. Ways to Cleanse Missing Data in Python

To perform a Python data cleansing, you can drop the missing values, replace them, replace each NaN with a
scalar value, or fill forward or backward.
Ways to Cleanse Missing Data in Python

a. Dropping Missing Values


You can exclude missing values from your dataset using the dropna() method.

>>> frame.dropna()

Ways for Python Data Cleansing – Dropping Missing Values

This defaults to dropping on axis=0, which excludes an entire row for an NaN value.
Do you know the Python Modules vs Packages

b. Replacing Missing Values


To replace each NaN we have in the dataset, we can use the replace() method.

>>> from numpy import NaN

>>> frame.replace({NaN:0.00})
Ways for Python Data Cleansing – Replacing Missing Values

This way, we can also replace any value that we find enough times in the dataset.

c. Replacing with a Scalar Value


We can use the fillna() method for this.

>>> frame.fillna(7)

Ways for Python Data Cleansing – Replacing with a Scalar Value

d. Filling Forward or Backward


If we supply a method parameter to the fillna() method, we can fill forward or backward as we need. To fill
forward, use the methods pad or fill, and to fill backward, use bfill and backfill.

>>> frame.fillna(method='pad')
Ways for Python Data Cleansing – Filling Forward or Backward

>>> frame.fillna(method='backfill')

Ways for Python Data Cleansing – Filling Forward or Backward

Follow the link to know about Python Property – The Problem and Solution

7. Python Data Cleansing – Other Operations

While cleaning data, we may also need to find out more about it and manipulate it. Below, we make use of
some of these operations.

>>> data={'Element':['Silver','Gold','Platinum','Copper'],'Atomic Number':[47,79,78,29]}

>>> frame=pd.DataFrame(data,index=['element 1','element 2','element 3','element 4'])

>>> frame
True

>>> frame.head()

Python Data Cleansing by pandas & numpy | Python Data Operations

Data Cleansing Operations in Python

>>> frame.tail(3)

a. Renaming Columns
To rename a column, you can use the rename() method.

>>> frame.rename(columns={'Atomic Number':'Number','Element':'Name'},inplace=True)

>>> frame

Let’s revise Python Multithreading: A Comprehensive Tutorial

b. Making Changes Stay


Also, throughout this tutorial “Python Data Cleansing”, the changes that we have made to the frames did not
actually modify them. To make this happen, you can set the inplace=True parameter.
So, this was all about Python Data Cleansing Tutorial. Hope you like our explanation.

8. Conclusion

Hence, in this Python Data Cleansing, we learned how data is Cleans In Python Programming Language for this
purpose, we used two libraries- pandas and numpy. Since data scientists spend 80% of their time cleaning and
manipulating data, that makes it an essential skill to learn with data science. Tell us what you think in the
comments below.

Python Multithreading – Threads, Locks, Functions of


Multithreading
by DataFlair Team · September 27, 2018

1. Python Multithreading Tutorial

In this lesson, we’ll learn to implement Python Multithreading with Example. We will use the module
‘threading’ for this. We will also have a look at the Functions of Python Multithreading, Thread – Local Data,
Thread Objects in Python Multithreading and Using locks, conditions, and semaphores in the with-statement in
Python Multithreading.

So, let’s start the Python Multithreading Tutorial.


Python Multithreading – Functions, Threads and Locks in Multithreading

2. What is Python Multithreading?

The module ‘threading’, for Python, helps us with thread-based parallelism. It constructs higher-level threading
interfaces on top of the lower level _thread module. Where _thread is missing, we can’t use threading. For such
situations, we have dummy_threading.
Learn about Data Analytics with R, Excel, and Tableau

3. Functions in Python Multithreading

We have the following functions in the Python Multithreading module:


Python Multithreading Tutorial – Functions of Multithreading in Python

a. active_count()
This returns the number of alive(currently) Thread objects. This is equal to the length the of the list that
enumerate() returns.

>>> threading.active_count()

b. current_thread()
Based on the caller’s thread of control, this returns the current Thread object. If this thread of control isn’t
through ‘threading’, it returns a dummy thread object with limited functionality.

>>> threading.current_thread()

<_MainThread(MainThread, started 14352)>

c. get_ident()
get_ident() returns the current thread’s identifier, which is a non-zero integer. We can use this to index a
dictionary of thread-specific data. Apart from that, it has no special meaning. When one thread exits and another
creates, Python recycles such an identifier.

>>> threading.get_ident()

14352

d. enumerate()
This returns a list of all alive(currently) Thread objects. This includes the main thread, daemonic threads, and
dummy thread objects created by current_thread(). This obviously doesn’t include terminated threads as well as
those that haven’t begun yet.

>>> threading.enumerate()

[<_MainThread(MainThread, started 14352)>, <Thread(SockThread, started daemon 9864)>

e. main_thread()
This method returns the main Thread object. Normally, it is that thread which started the interpreter.

>>> threading.main_thread()

<_MainThread(MainThread, started 14352)>

f. settrace(func)
settrace() traces a function for all threads we started using ‘threading’. The argument func passes to
sys.settrace() for each thread before it calls its run() method.

>>> def sayhi():

print("Hi")

>>> threading.settrace(sayhi)

>>>

g. setprofile(func)
This method sets a profile function for all threads we started from ‘threading’. It passes func to sys.setprofile()
for each thread before it calls its run() method.

>>> threading.setprofile(sayhi)

>>>

h. stack_size([size])
stack_size() returns the stack size of a thread when creating new threads. size is the stack size we want to use for
subsequently created threads. This must be equal to 0 or a positive integer of value at least 32,768 (32KiB).
When not specified, it uses 0. And if it doesn’t support changing thread stack size, it raises a RuntimeError.

When we pass an invalid stack size, it raises a ValueError, and does not modify it. The minimum stack size it
currently supports to guarantee enough stack space for the interpreter itself is 32KiB. Some platforms may need
a minimum stack size of greater than 32KiB. Others may need to allocate in multiples of system memory page
size.

>>> threading.stack_size()

Apart from functions, ‘threading’ also defines a constant.

i. TIMEOUT_MAX
This holds the maximum allowed value for this constant, the timeout parameter for blocking functions like
Lock.acquire(), Condition.wait(), RLock.acquire(), and others. If we denote a timeout greater than this, it raises
an OverflowError.

>>> threading.TIMEOUT_MAX

4294967.0

In Java, locks and condition variables are the basic behavior of every object. Whereas in Python, they are
individual objects. Here, the class Thread supports some of the functionality of class Thread in Java. However,
currently, we have no thread groups, priorities, and we cannot destroy, stop, suspend, resume, or interrupt
threads. When we implement the static methods from Java’s Thread, they map to module-level functions. This
way, ‘threading’ is much like Java’s threading model in design.
Next in the Python Multithreading tutorial is Thread – Local Data and Thread Objects.

4. Python Thread-Local Data

That data for which the values are thread-specific, is thread-local. To manage such data, we can create an
instance of local/a subclass, and then store attributes on it.

>>> mydata=threading.local()

>>> mydata.x=7

>>>

These instance values differ for each thread. We have the following class denoting thread-local data:

class threading.local

Next in Python Multithreading Tutorial is Thread Objects.

Learn More: R Debugging

5. Python Thread Objects

The Thread class that we mentioned earlier in this blog denotes an activity running in a separate thread of
control. We can represent this activity either by passing a callable object to the constructor, ot by overriding the
method run() in a subclass. You must make sure to not override other methods in a subclass, except for the
constructor. In short, only override a class’ init () and run() methods.

Once the interpreter creates a thread object, we must start its activity by calling its start() method. This will
invoke its run() method in a separate thread of control. Once this happens, we consider the thread to be ‘alive’.
When run() terminates normally or raising an exception we did not handle, it is no longer alive. To test whether
a thread is alive, we may use the method is_alive().
A thread may call another’s join() method. This will block the calling thread until the other terminates.

Threads have names, and we can pass these names to the constructor, and even read or modify them.

We can flag a thread as a ‘daemon thread’. This means that the whole program exits when only the daemon
threads remain. This initial value comes from the creating thread. We can set this flag via the property
‘daemon’, or via the constructor argument ‘daemon’. Daemons abruptly stop at shutdown, and they may not
properly release all the resources held. These resources may include open files, database transactions, and
others. To stop our threads gracefully, we must make them non-daemonic. It is also preferable to use a suitable
signaling process, like an Event.

The ‘main thread’ object pertains to the initial thread of control in our program; it isn’t a daemon.
Finally, it is possible that the interpreter creates ‘dummy thread objects’. These are ‘alien threads’ (threads of
control started outside ‘threading’, for ex, directly from C code). Such objects have limited functionality, and
are always live and daemonic. We cannot join() them. We can also never delete them since it is impossible to
detect when they terminate.

This is the class:


class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

Take note:

Always call the constructor with keyword arguments. It has the following arguments:

group must be None. Python reserves this for future extension when we implement a ThreadGroup class.

target is a callable object that run() will invoke. The default for this is None, which means it calls nothing.

name is the name of the thread. The default for this is “Thread-N”. Here, N is a small decimal number.

args is an argument tuple. It helps invoke the target. The default for this is ().

Kwargs is a dictionary holding keyword arguments. Even this helps invoke the target. The default for this is
{}.

daemon decides whether the thread is daemonic. When None, it inherits the daemonic property from the
current thread. The default for this is None.

Ensure that you invoke the base class constructor(Thread. init ()) first if the subclass overrides the
constructor.

Thread has the following methods:

Python Multithreading Tutorial – Python Thread Object

a. start()
This starts thread activity. For a thread object, we can call it maximum once; if we call it again, it raises a
RuntimeError. This lets run() for the object invoke in a separate thread of control.

>>> threading.Thread.start(threading.current_thread())
Traceback (most recent call last):

File “<pyshell#135>”, line 1, in <module>

threading.Thread.start(threading.current_thread())

RuntimeError: threads can only be started once

b. run()
This method explains the thread’s activity. It invokes the callable object we passed to the object’s constructor as
the target argument, if it exists. This is with keyword and sequential arguments from kwargs and args.

We can override run() in a subclass.

c. join(timeout=None)
For join() to work, we must wait until the thread terminates. Because when that happens, it blocks the calling
thread until the one on which we call join() terminates normally or via an exception we did not handle, or until
timeout occurs.

When you do provide a timeout (other than None), make sure it is a floating point number. This is so you can
pass a timeout in seconds or fractions.

So, what is the return value? Well, join() always returns None. Hence, you’ll need to call is_alive() after calling
join() to determine if a timeout happened. If we find out that it is indeed still alive, then we infer that the join()
call timed out.

However, if timeout is None, or if we did not pass it, this blocks the operation until the thread terminates. We
can join() a thread many times.

Finally, join() will raise a RuntimeError if we try to join the current thread, because that causes a deadlock. To
join() a thread before we start it also causes an error.

>>> threading.Thread.join(threading.current_thread())

Traceback (most recent call last):

File “<pyshell#138>”, line 1, in <module>

threading.Thread.join(threading.current_thread())

RuntimeError: cannot join current thread

d. name
This is a string we use for identification; it has no meaning. We can also give the same meaning to multiple
threads. The constructor sets the initial name.

>>> threading.Thread.name='First'
>>>

e. getName() and setName()


These are old getter and setter APIs for name. We use them directly as properties.

f. ident
If we started the thread, this returns its identifier. Otherwise, it returns None. Note that it is a non-zero integer,
like in the get_ident() function. Python may recycle identifiers when one thread exits and another creates. Such
identifiers exist even after a thread exits.

g. is_alive()
This returns whether the thread is alive. is_alive() returns true from just before run() starts until just after it
terminates.

>>> threading.Thread.is_alive(threading.current_thread())

True

h. daemon
daemon is a Boolean value that tells us whether the thread is a daemon. If it is, it returns True. We must set it
before we call start(). Otherwise, it raises a RuntimeError. Its initial value comes from the creating thread. The
main thread is not a daemon; hence, all threads in the main thread have a default of False for daemon.

When only daemon threads remain, the whole program exits.

i. isDaemon() and setDaemon()


These are old getter and setter APIs for daemon. You can use them directly as properties. With this lets move to
our next topic in Python Multithreading tutorial.

Learn More: R Packages

6. Python Lock Objects

A synchronization primitive, a primitive lock does not belong to a certain thread when locked. This is the
lowest-level synchronization primitive we currently have in Python, and we implement it using the extension
module _thread.

Such a lock can be in one of two states: ‘locked’ and ‘unlocked’. When we create a lock, it is in the ‘unlocked’
state. It also has two methods- acquire() and release(). When we want to lock it, acquire() changes its state to
‘locked’, and immediately returns it. If it was ‘locked’ instead, then acquire() blocks until another thread calls
release(). This changes the state to ‘unlocked’. Finally, acquire() resets it to ‘locked’, and then returns
immediately.

If you try to release a lock that is already unlocked, it raises a RuntimeError.


These locks also support the CMP(Context Management Protocol).

When acquire() blocks more than one thread, only one thread continues when release() resets the state to
‘unlocked’. Which one, you ask? Well, we can’t say.

Also, all methods execute atomically.

This is the class:

class threading.Lock

This call implements primitive lock objects. Once a thread acquires a lock, the interpreter blocks further
attempts to acquire it. Only after it releases, does any other thread have a chance in acquiring it. Any thread may
release a lock.

a. acquire(blocking=True, timeout=-1)
This method acquires a blocking or non-blocking lock. When blocking=True, it blocks until the lock unlocks.
Then, it changes its state to ‘locked’, and returns True. And when it is False, it does not block. A call with
blocking=True that blocks, immediately returns False. Otherwise, it sets the lock to ‘locked’ and returns True.

timeout is a floating-point argument. When it has a positive value, it blocks for a maximum of timeout number
of seconds; as long as the lock isn’t acquirable. When it is -1, it denotes an unbounded wait.

When blocking is False, we cannot specify timeout.

Also, if the lock acquires successfully, it returns True; otherwise, False, like when timeout expires.

b. release()
This method releases a lock. You can call it from any thread. This means that any thread can release a lock, no
matter which thread has acquired it.

When ‘locked’, release() resets it to ‘unlocked’, and returns. If other threads wait for it to unlock, only one gets
to continue once it unlocks.

When we call release() on an ‘unlocked’ lock, it raises a RuntimeError.

release() doesn’t return any value.

Any doubt yet in Python Multithreading Tutorial. Please Comment.

7. Python RLock Objects

RLock is very important topic when you learn Python Multithreading. An RLock is a reentrant lock. It is a
synchronization primitive that a certain thread can acquire again and again. It does so using concepts like
‘owning thread’ and ‘recursion level’, and locked/unlocked states. When locked, an RLock belongs to a certain
thread; but when unlocked, no thread owns it.
Now, how does this work? To lock, a thread calls acquire(). Now that this thread owns the lock, it returns. To
unlock it, a thread calls release(). It is also possible to nest acquire()/release() pairs. The outermost release()
resets the lock to the ‘unlocked’ state. It also lets another blocked thread to continue.

Reentrant locks also support CMP(Context Management Protocol).

This is the class:

class threading.RLock

RLock implements reentrant lock objects. Such a lock only release by the thread holding it. A thread can
acquire it again without blocking. However, it must release it once each time it acquires it.

It has two methods:

a. acquire(blocking=True, timeout=-1)
acquire() lets us acquire a blocking or non-blocking lock. Without arguments, if the thread already owns the
lock, this method ups the recursion level by one, and then returns. If it doesn’t already own it, and another
thread owns it, it blocks until the lock ‘unlocks’. And once unlocked, and if it does not belong to any other
thread, acquire() declares ownership and sets recursion level to 1, and then returns. If more than one thread
waits blocked, at once, only one will get ownership.
This method returns no value. Finally, when we set blocking to True, it does the same things we discussed, and
then returns True.

When blocking is False, however, it doe not block. When a call without arguments blocks, it returns False.
Otherwise, it does what it does for a call without arguments, and then returns True.
And when we call acquire() with timeout, which is a floating-point number, with a positive value, this blocks
for a maximum of timeout number of seconds, as long as we cannot acquire the lock. If a thread has acquired it,
it returns True; if timeout has elapsed, it returns False.

b. release()
This method releases a lock and decrements the recursion level. Once the decrement is 0, it resets the lock to the
‘unlocked’ state. This means no thread owns it. If other threads are blocked, only one of them may continue. If
the decrement is non-zero, the lock stays in the ‘locked’ state, and belongs to the calling thread.

You should only call release() when the calling thread actually owns the lock. If it is already ‘unlocked’, this
raises a RuntimeError.

release() returns no value.

Now lets come to Condition Objects in Python Multithreading.

8. Python Condition Objects


A condition variable always pertains to a lock, and we can pass it in, or it creates by default. When several such
condition variables must share a lock, we can pass it in. But we don’t need to exclusively track a lock; it is a
part of the condition object.
A condition variable follows CMP (Context Management Protocol) in that it uses the with-statement to acquire
the associated lock as long as the enclosed block is alive. acquire() and release() call the lock’s methods.

For other methods, we must call them with the associated lock the thread holds. Once wait() releases the lock, it
blocks until another thread wakes it up with a call to notify() or notify_all(). After this, wait() acquires the lock
again, and then returns. We can also specify a timeout.
While notify() awakens one waiting thread, if any, notify_all() awakens all threads waiting for the condition
variable. Note that these two methods do not release the lock. So, the threads awakened do not return from
wait() immediately. They return only when the calling thread for notify() or notify_all() gives up ownership for
the lock.

This is the class:

class threading.Condition(lock=None)

Condition implements condition variable objects. A condition variable lets any number of threads wait until
another thread notifies them.

If lock is not None, and we do pass it, make sure it’s a Lock or RLock object. This should also serve as the
underlying lock, otherwise this creates a new RLock object.

It has the following methods:


Python Multithreading Tutorial – Python Condition Object

a. acquire(*args)
This acquires the underlying lock. It calls the corresponding method on it, and returns what that method returns.

b. release()
This releases the underlying lock. It calls the corresponding method on it, and returns nothing.

c. wait(timeout=None)
This method waits until a timeout happens or until someone notifies it. If at the time of calling wait(), the
calling thread doesn’t own the lock, this raises a RuntimeError.
wait() releases the underlying lock, then blocks until a notify()/notify_all() call for the same condition variable
in another thread wakes it up, or until timeout happens. And once this happens, it acquires the lock again, and
then returns.

When we do pass timeout, and that isn’t None, make sure it’s a floating point number denoting a timeout for the
operation in seconds or fractions.

If the underlying lock is an RLock, its release() method doesn’t release it, because this doesn’t necessarily
unlock it if it was acquired multiple times recursively. So, what do we do? We use an internal interface of the
RLock class. This unlocks it even when it was recursively acquired many times. Then, we use another internal
interface to restore the recursion level when the thread acquires the lock again.

wait() returns False if timeout expires. Otherwise, it returns True.

d. wait_for(predicate, timeout=None)
This method waits until a condition becomes True. The predicate is a callable with a Boolean result. We may
provide a timeout to specify a maximum time to wait.
wait_for() is a utility method, and it can repeatedly make a call to wait() until the predicate satisfies, or until a
timeout happens. It returns the predicate’s last return value, and returns False if the method times out.

With this method, the same rules apply as do to wait(). When we call it, the lock must be held, and acquires
again on return. This evaluates the predicate with the lock held.

e. notify(n=1)
notify() wakes up a thread waiting on this condition, if there is any. When we call it, if the calling thread doesn’t
own the lock, this raises a RuntimeError. It wakes up a maximum of n threads that wait for the condition
variable. If no threads wait, then it is a no-operation(NOP).
If at least n threads wait, this implementation will wake exactly n threads up. But we can’t rely on this behavior.
An optimized implementation can occasionally wake more than n threads up.

f. notify_all()
This wakes up all threads that wait on this condition. So, this is like notify(), except that it wakes all waiting
threads instead of exactly one. If at the time of calling it, if the calling thread doesn’t own the lock, this raises a
RuntimeError.

9. Python Semaphore Objects

Early Dutch computer scientist Edsger W. Dijkstra invented one of the oldest synchronization primitives.
Instead of acquire() and release(), he used P() and V().

What is a semaphore? It is a primitive that lets us manage an internal counter. Each call to acquire() decrements,
and each call to release() decrements it. But let us tell you, the counter never goes below zero. When it is 0,
acquire() blocks, and waits until a thread makes a call to release().

Semaphores in Python Multithreading support CMP(Context Management Protocol).

This is the class we have:

class threading.Semaphore(value=1)

It implements semaphore objects. A semaphore holds an atomic counter denoting the count of release() calls
minus the count of acquire() calls, added to an initial value.acquire() blocks if needed until it can leave the
counter non-negative and still return. The default value for the counter is 1.

This class implements semaphore objects. A semaphore manages an atomic counter representing the number of
release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if
necessary until it can return without making the counter negative. If not given, value defaults to 1.

value can serve as an initial value for the internal counter. The default for this is 1. If we pass a value less than
0, this raises a ValueError.

It has the following methods:

a. acquire(blocking=True, timeout=None)
This acquires a semaphore. When we pass a timeout value other than None, it blocks for a maximum of timeout
seconds. If in that interval, acquire() doesn’t complete successfully, it returns False. Otherwise, it returns True.

When we call it without arguments, the following cases may be:

If, on entry, the internal counter is greater than zero, it decrements it by one, and then returns.

If, on entry, the internal counter is zero, it blocks until a call to release() wakes it up. Now that the counter is
greater than 0, it decrements it by 1, and then returns True. Each call to release() wakes exactly one
thread. We cannot say what order this happens in.

When we call it with blocking with a value of False, it doesn’t block. And if a call without arguments
blocks, then it returns False. Otherwise, it does the same as when called without arguments, and then
returns True.

b. release()
This method releases a semaphore, and increments the internal counter by 1. When, on entry, it is 0, and another
thread waits for it to grow again, it wakes that thread up.

We also have bounded semaphores:

class threading.BoundedSemaphore(value=1)

This class implements bounded semaphore objects. Such objects ensure that their current values do not exceed
their initial values. It this happens, this raises a ValueError. Mostly, semaphores guard resources with limited
capacity, for ex., a database server. Where the resource size is fixed, use a bounded semaphore. But if it releases
the semaphore way too many times, then you may have a bug in your code. The default for this is 1.

Let’s take an example. The main thread initializes the semaphore before spawning any worker threads:

>>> maxconnections=5

>>> pool_sema=threading.BoundedSemaphore(value=maxconnections)

Now that it is spawned, the worker threads call acquire() and release() when they must connect to the server:

>>>with pool_sema:

conn=connectdb()

try:

#use connection

finally:

conn.close()

Using a bounded semaphore lessens the chances of programming errors.

10. Python Event Objects

An extremely simple tool in Python Multithreading to communicate, it lets one thread play an event, and the
other must wait for it.

An event object deals an internal flag. The methods set() and clear() allow us to set and reset it to True and
False, respectively. Until flag is True, wait() blocks.

This is the class:

class threading.Event

This class implements event objects. An event handles a flag, and we can use the methods set() and clear() to set
and reset it to True and False, respectively. Initially, the flag is False. wait() blocks it until it becomes True.

It has the following methods:


a. is_set()

If the internal flag is True, it returns True.

b. set()

This method sets the internal flag to True, and wakes all threads waiting for it to become True. Once it is True,
waiting threads do not block at all.

c. clear()

This resets the internal flag to False. Eventually, waiting threads block until somebody calls set() to set the
internal flag to True yet again.

d. wait(timeout=None)

Until the internal flag is True, this method blocks. On entry, if it is True, it returns immediately. Otherwise, it
blocks until another thread makes a call to set() to set the flag to True, or until

timeout happens.

When timeout exists, and isn’t Now, make sure it’s a floating-point number denoting a timeout for the operation
in seconds or fractions.

It returns True only if the internal flag is True- either before the call to wait(), or after. This way, wait() always
returns True. However, if timeout exists and the operation times out, it returns False.

11. Python Timer Objects

Timer denotes an action that should run only after a given amount of time; it is a timer in Python
Multithreading. This is a subclass of Thread, and we can also use it to learn how to create our own threads.

When we call start() on a thread, a timer start with it. We can stop it before it begins, if we call cancel() on it.
Before executing, a timer waits for some interval; this may differ from the interval we specify.

Take an example:

>>> def hello():

print("Hello")

>>> t=threading.Timer(30.0,hello)

>>> t.start()

This is the class:

class threading.Timer(interval, function, args=None, kwargs=None)


It creates a timer that runs a function(with arguments args and kwargs) after interval seconds. When args is
None, it uses an empty list. This is the default. And when kwargs is None, it uses an empty dict. This is a
default too.

It has one method:

a. cancel()

This stops the timer, and then cancels its action. This only works if the timer is waiting.
Now the last in Python Multithreading is Barrier Objects.

12. Barrier Objects

Barrier is a simple synchronization primitive to a fixed number of threads that must wait for each other. Each
thread tries to pass the barrier by making a call to wait; it blocks until all threads have done this. Then, the
threads release simultaneously.

You can reuse a barrier any number of times for the same number of threads. Let’s take an example.

A way to synchronize a client and server thread is:

>>> b=threading.Barrier(2,timeout=5)

>>> def server():

start_server()

b.wait()

while True:

connection = accept_connection()

process_server_connection(connection)

>>> def client():

b.wait()

while True:

connection = make_connection()

process_client_connection(connection)

This is the class:

class threading.Barrier(parties, action=None, timeout=None)


Barrier creates a barrier object for parties number of threads. When we do pass action, it is a callable that a
thread will call when we release it. Finally, timeout is a default value for timeout if we don’t specify the same
for wait().

It has the following methods:

Python Multithreading Tutorial – Python Barrier Object

a. wait(timeout=None)
wait() passes the barrier. Once all thread parties have called wait(), they all release together. If we do pass a
value for timeout, it uses this one, no matter whether we provided a value for the same to the class constructor.

It returns any integer value from 0 to parties-1. This is different for each thread. You can use this to choose a
thread to do special housekeeping. Take an example:

>>> i=barrier.wait()

>>> if i==0:

#Only one thread must print this

print("passed the barrier")

If we provided an action to the constructor, one thread calls it before releasing. If this raises an error, the barrier
sinks into a ‘broken’ state. The same happens if the call times out.
Finally, wait() may raise a BrokenBarrierError if the barrier breaks or resets as a thread waits.

b. reset()
This function resets the barrier to its default, empty state. Any waiting threads receive a BrokenBarrierError.

reset() may need external synchronization if other threads with unknown states exist. If it breaks a barrier, just
create a new one.

c. abort()
abort() puts a barrier into a ‘broken’ state. Consequently, active/future calls to wait() fail with a
BrokenBarrierError. To avoid deadlocking an application, we may need to abort it. This is one use-case.

Try to create the barrier with a sensible value for timeout so it automatically guards against a thread going
haywire.

d. parties
This returns the number of threads we need to pass the barrier.

e. n_waiting
This returnd the number of threads that currently wait in the barrier.

f. broken
This is a Boolean value that is True if the barrier is in a ‘broken’ state.

Check out this exception that Barrier may raise:

exception threading.BrokenBarrierError

This is a subclass of RuntimeError, and it raises if a Barrier object resets or breaks.


Now lets learn using locks, conditions and semaphores in the with-statement in the last topic of out Python
Multithreading Tutorial.

13. Using locks, conditions, and semaphores in the with-statement

If an object in this module has acquire() and release(), we can use it as a context-manager for a with-statement.
When it enters the block, it calls acquire(), and when it exits it, it calls release().

This is the syntax for the same:

with some_lock:

#do something

This is the same as:

>>> some_lock.acquire()

>>> try:
#do something

finally:

some_lock.release()

We can currently use Lock, RLock, Condition, Semaphore, and BoundedSemaphore objects as context-
managers for with-statements.

So, this was all about Python Multithreading Tutorial. Hope you like our explanation.

14. Conclusion

This is all about the Python Multithreading with Example, Functions of Python Multithreading, Thread – Local
Data, Thread Objects in Python Multithreading and Using locks, conditions, and semaphores in the with-
statement in Python Multithreading. If you have a confusion, let us know in the comments. We will surely get
back to you!

Learn Python Regex Tutorial – Python Regular Expression Functions


by DataFlair Team · May 16, 2019

1. Python Regex Tutorial

Python Regular Expression is one of my favourite topics. Let’s delve into this without wasting a moment to
learn Python Regex Tutorial. Here, we will discuss Metacharacters, examples & functions of Python Regex.
Along with this, we will cover Python findall, Python multiline.

So, let’s start a short Python Regex Cheat Sheet.

Python Regular Expression / Python Regex


2. What is the Python Regular Expression (Regex)?

Essentially, a Python regular expression is a sequence of characters, that defines a search pattern. We can then
use this pattern in a string-searching algorithm to “find” or “find and replace” on strings. You would’ve seen
this feature in Microsoft Word as well.

In this Python Regex tutorial, we will learn the basics of regular expressions in Python. For this, we will use the
‘re’ module. Let’s import it before we begin.

>>> import re

3. Python Regex – Metacharacters

Each character in a Python Regex is either a metacharacter or a regular character. A metacharacter has a special
meaning, while a regular character matches itself. Python has the following metacharacters:

Metacharacter Description
^ Matches the start of the string
Matches a single character, except a newline
. But when used inside square brackets, a dot is matched
A bracket expression matches a single character from the ones inside it
[abc] matches ‘a’, ‘b’, and ‘c’
[] [a-z] matches characters from ‘a’ to ‘z’
[a-cx-z] matches ‘a’, ’b’, ’c’, ’x’, ’y’, and ‘z’
Matches a single character from those except the ones mentioned in the brackets[^abc] matches all
[^ ] characters except ‘a’, ‘b’ and ‘c’
() Parentheses define a marked subexpression, also called a block, or a capturing group
\t, \n, \r, \f Tab, newline, return, form feed
Matches the preceding character zero or more times
ab*c matches ‘ac’, ‘abc’, ‘abbc’, and so on
*
[ab]* matches ‘’, ‘a’, ‘b’, ‘ab’, ‘ba’, ‘aba’, and so on
(ab)* matches ‘’, ‘ab’, ‘abab’, ‘ababab’, and so on
Matches the preceding character minimum m times, and maximum n
{m,n} times a{2,4} matches ‘aa’, ‘aaa’, and ‘aaaa’
{m} Matches the preceding character exactly m times
Matches the preceding character zero or one times
? ab?c matches ‘ac’ or ‘abc’
Matches the preceding character one or one times
+ ab+c matches ‘abc’, ‘abbc’, ‘abbbc’, and so on, but not ‘ac’
The choice operator matches either the expression before it, or the one after
| abc|def matches ‘abc’ or ‘def’
Matches a word character (a-zA-Z0-9)
\w \W matches single non-word characters
\b Matches the boundary between word and non-word characters
Matches a single whitespace character
\s \S matches a single non-whitespace character
\d Matches a single decimal digit character (0-9)
A single backslash inhibits a character’s specialness
Examples- \. \\ \*
\
When unsure if a character has a special meaning, put a \ before it:
\@
$ A dollar matches the end of the string

A raw string literal does not handle backslashes in any special way. For this, prepend an ‘r’ before the pattern.
Without this, you may have to use ‘\\\\’ for a single backslash character. But with this, you only need r’\’.

Regular characters match themselves.

4. Rules for a Match

So, how does this work? The following rules must be met:

The search scans the string start to end.

The whole pattern must match, but not necessarily the whole string.

The search stops at the first match.

If a match is found, the group() method returns the matching phrase. If not, it returns None.

>>> print(re.search('na','no'))

None
Let’s look at about a couple important functions now.

5. Python Regular Expression Functions


We have a few functions to help us use Python regex.

a. match()
match() takes two arguments- a pattern and a string. If they match, it returns the string. Else, it returns None.
Let’s take a few Python regular expression match examples.

>>> print(re.match('center','centre'))

None

>>> print(re.match('...\w\we','centre'))

<_sre.SRE_Match object; span=(0, 6), match=’centre’>

b. search()
search(), like match(), takes two arguments- the pattern and the string to be searched. Let’s take a few examples.

>>> match=re.search('aa?yushi','ayushi')
>>> match.group()

‘ayushi’

>>> match=re.search('aa?yushi?','ayush ayushi')

>>> match.group()

‘ayush’

>>> match=re.search('\w*end','Hey! What are your plans for the weekend?')

>>> match.group()

‘weekend’

>>> match=re.search('^\w*end','Hey! What are your plans for the weekend?')

>>> match.group()

Traceback (most recent call last):

File “<pyshell#337>”, line 1, in <module>

match.group()

AttributeError: ‘NoneType’ object has no attribute ‘group’

Here, an AttributeError raised because it found no match. This is because we specified that this pattern should
be at the beginning of the string. Let’s try searching for space.

>>> match=re.search('i\sS','Ayushi Sharma')

>>> match.group()

‘i S’

>>> match=re.search('\w+c{2}\w*','Occam\'s Razor')

>>> match.group()

‘Occam’

It really will take some practice to get it into habit what the metacharacters mean. But since we don’t have so
many, this will hardly take an hour.

6. Python Regex Examples

Let’s try crafting a Python regex for an email address. Hmm, so what does one look like? It looks like this: abc-
def@ghi.com
Let’s try the following code:

>>> match=re.search(r'[\w.-]+@[\w-]+\.[\w]+','Please mail it to ayushiwasthere@gmail.com')

>>> match.group()

‘ayushiwasthere@gmail.com’

It worked perfectly!

Here, if you would have typed [\w-.] instead of [\w.-], it would have raised the following error:

>>> match=re.search(r'[\w-.]+@[\w-]+\.[\w]+','Please mail it to ayushiwasthere@gmail.com')

Traceback (most recent call last):

File “<pyshell#347>”, line 1, in <module>

match=re.search(r'[\w-.]+@[\w-]+\.[\w]+’,’Please mail it to ayushiwasthere@gmail.com’)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\re.py”, line 182, in search


return _compile(pattern, flags).search(string)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\re.py”, line 301, in _compile

p = sre_compile.compile(pattern, flags)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\sre_compile.py”, line 562, in compile

p = sre_parse.parse(p, flags)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py”, line 856, in parse

p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py”, line 415, in _parse_sub

itemsappend(_parse(source, state, verbose))

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py”, line 547, in _parse

raise source.error(msg, len(this) + 1 + len(that))

sre_constants.error: bad character range \w-. at position 1

This is because normally, we use a dash (-) to indicate a range.

7. Group Extraction

Let’s continue with the example on emails. What if you only want the username? For this, you can provide an
argument(like an index) to the group() method. Take a look at this:
>>> match=re.search(r'([\w.-]+)@([\w-]+)\.([\w]+)','Please mail it to ayushiwasthere@gmail.com')

>>> match.group()

‘ayushiwasthere@gmail.com’

>>> match.group(1)

‘ayushiwasthere’

>>> match.group(2)

‘gmail’

>>> match.group(3)

‘com’
Parentheses let you extract the parts you want. Note that for this, we divided the pattern into groups using
parentheses:

r'([\w.-]+)@([\w-]+)\.([\w]+)’

8. Python findall()

Above, we saw that Python regex search() stops at the first match. But Python findall() returns a list of all
matches found.

>>> match=re.findall(r'advi[cs]e','I could advise you on your poem, but you would disparage my advice')

We can then iterate on it.

>>> for i in match:

print(i)

advise

advice

>>> type(match)

<class ‘list’>

9. findall() with Files

We have worked with files, and we know how to read and write them. Why not make life easier by using
Python findall() with files? We’ll first use the os module to get to the desktop. Let’s see.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> f=open('Today.txt')

We have a file called Today.txt on our Desktop. These are its contents:

OS, DBMS, DS, ADA

HTML, CSS, jQuery, JavaScript

Python, C++, Java

This sem’s subjects

Now, let’s call findall().

>>> match=re.findall(r'Java[\w]*',f.read())

Finally, let’s iterate on it.

>>> for i in match:

print(i)

JavaScript

Java

10. findall() with Groups


We saw how we can divide a pattern into groups using parentheses. Watch what happens when we call Python
Regex findall().

>>> match=re.findall(r'([\w]+)\s([\w]+)','Ayushi Sharma, Fluffy Sharma, Leo Sharma, Candy Sharma')

>>> for i in match:

print(i)

(‘Ayushi’, ‘Sharma’)

(‘Fluffy’, ‘Sharma’)

(‘Leo’, ‘Sharma’)

(‘Candy’, ‘Sharma’)

11. Python Regex Options

The functions we discussed may take an optional argument as well. These options are:

a. Python Regular Expression IGNORECASE


This Python Regex ignore case ignores the case while matching.

Take this example of Python Regex IGNORECASE:

>>> match=re.findall(r'hi','Hi, did you ship it, Hillary?',re.IGNORECASE)

>>> for i in match:

print(i)

Hi

hi

Hi

b. Python MULTILINE
Working with a string of multiple lines, this allows ^ and $ to match the start and end of each line, not just the
whole string.

>>> match=re.findall(r'^Hi','Hi, did you ship it, Hillary?\nNo, I didn\'t, but Hi',re.MULTILINE)

>>> for i in match:

print(i)

Hi

c. Python DOTALL
.* does not scan everything in a multiline string; it only matches the first line. This is because . does not match a
newline. To allow this, we use DOTALL.

>>> match=re.findall(r'.*','Hi, did you ship it, Hillary?\nNo, I didn\'t, but Hi',re.DOTALL)

>>> for i in match:

print(i)

Hi, did you ship it, Hillary?

No, I didn’t, but Hi

12. Greedy vs Non-Greedy

The metacharacters *, +, and ? are greedy. This means that they keep searching. Let’s take an example.

>>> match=re.findall(r'(<.*>)','<em>Strong</em> <i>Italic</i>')

>>> for i in match:


print(i)

<em>

</em>

<i>

</i>

This gave us the whole string, because it greedily keeps searching. What if we just want the opening and closing
tags? Look:

print(i)

>>> match=re.findall(r'(<.*?>)','<em>Strong</em> <i>Italic</i>')

>>> for i in match:

print(i)

<em>

</em>

<i>

</i>

The .* is greedy, and the ? makes it non-greedy.

Alternatively, we could also do this:

>>> match=re.findall(r'</?\w+>','<em>Strong</em> <i>Italic</i>')

>>> for i in match:

print(i)

<em>

</em>

<i>

</i>

Here’s another example:

>>> match=re.findall('(a*?)b','aaabbc')
>>> for i in match:

print(i)

aaa

Here, the ? makes * non-greedy. Also, if we would have skipped the b after the ?, it would have returned an
empty string. The ? here needs a character after it to stop at. This works for all three- *?, +?, and ??.

Similarly, {m,n}? makes it non-greedy, and matches as few occurrences as possible.

13. Substitution

We can use the sub() function to substitute the part of a string with another. sub() takes three arguments-
pattern, substring, and string.

>>> re.sub('^a','an','a apple')

‘an apple’

Here, we used ^ so it won’t change apple to anpple. The grammar police approve.

14. Python Regex Applications

So, we learned so much about Python regular expressions, but where do we use them? They find use in these
places:

Search engines

Find and Replace dialogues of word processor and text editors

Text processing utilities like sed and AWK

Lexical analysis

This was all about the Python Regex Tutorial

15. Python Regex – Conclusion

These were the basics of Python regular expressions. Honestly, we think it is really cool to have such a tool in
hand. If you love English, try experimenting, and make a small project with it.

Furthermore, if you have a doubt in the Python Regex Tutorial, feel free to ask in the comments.

Python Debugger with Examples – Functions & Command Prompt


by DataFlair Team · September 27, 2018

1. Python Debugger Tutorial


In this Python Debugger tutorial, we will learn about pdb, the Python module for debugging your Python code.
Moreover, we will discuss commands and examples of Python debugger

So, let’s begin the Python Debugger Tutorial.

Python Debugger with Examples – Functions & Command Prompt

2. What is the Python Debugger?

The Python debugger is an interactive source code debugger for Python programs. It can set conditional
breakpoints and single stepping at the source line level. It also supports inspection of stack frames, source code
listing, and evaluation of arbitrary Python code in any stack frame’s context. Other facilities include post-
mortem debugging.

To aid us in our purpose, we have the ‘pdb’ module. We must import it.

>>> import pdb

Read: Python Regular Expressions | Python Regex Tutorial

3. Python Debugger Example


We’ll start with an example as we begin to explain what the Python debugger really does.
We get to the desktop, and create a file ‘demopdb.py’ with the following contents:

>>> import pdb

>>> x=8

>>> def power_of_itself(a):

return a**a

>>> pdb.set_trace()

>>> seven=power_of_itself(7)
>>> print(seven)

>>> three=power_of_itself(3)

>>> print(three)

First, let’s see what this code does. We first import pdb. Then, we define a variable ‘x’ with the value 8. Next,
we define a function ‘power_of_itself’ that returns a number to its own power.
Now here, we slip in a breakpoint in the code; we enter the debugger. The next statement is a call to
‘power_of_itself’, with the argument 7, storing the return value into the variable ‘seven’. After that, we print the
value of ‘seven’. Finally, we store the return value of power_of_itself(3) into the variable ‘three’, and print it
out.

Now, let’s save this as ‘demopdb.py’ on the Desktop, and switch to the command prompt(cmd).
Read: Python Frameworks

4. In the Command Prompt


Once in the Command Prompt(type cmd in Search), get to the Desktop using the ‘cd’ command:

Microsoft Windows [Version 10.0.16299.309]

(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\lifei>cd Desktop

Now, we run our program here using the ‘python’ command for the command prompt:

C:\Users\lifei\Desktop>python demopdb.py

> c:\users\lifei\desktop\demopdb.py(6)<module>()

-> seven=power_of_itself(7)

(Pdb)

As you can see, the prompt is at the first line after the breakpoint we set in our code. This is a call to
‘power_of_itself’ with the argument 7. Now we’re at the debugger, and we can type in some Python debugger
commands to proceed with the debugging.

(Pdb) l

1 import pdb

2 x=8

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()
6 -> seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb)

l, here, lists out the source code. It points at line 6, because that is where we’re at right now. Now, if we type
‘n’, it will execute until the next line in this function, or until it returns.

(Pdb) n

> c:\users\lifei\desktop\demopdb.py(7)<module>()

-> print(seven)

(Pdb)

As you can see, now, it’s at line 7, which prints the value of ‘seven’. It has executed line 6. At this point, we
decided we wanted to check some values.

(Pdb) seven

823543

(Pdb) x

(Pdb)

What if we had a variable ‘n’ in our code? Wouldn’t typing ‘n’ be considered a command, instead? For this,
we’d do ‘p n’.

To add another breakpoint, we use the command ‘b’.

(Pdb) b 8

Breakpoint 1 at c:\users\lifei\desktop\demopdb.py:8

(Pdb)

But that doesn’t mean we’re at line 8 now:

(Pdb) l
2 x=8

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 -> print(seven)

8 B three=power_of_itself(3)

9 print(three)

[EOF]

(pdb)

(Pdb) b

Num Type Disp Enb Where

1 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:8

(Pdb)

To clear all breaks, we type ‘cl’. And then when it asks for a confirmation, type ‘y’.

(Pdb) cl

Clear all breaks? y

Deleted breakpoint 1 at c:\users\lifei\desktop\demopdb.py:8

(Pdb)

We can also add a breakpoint at a function declaration:

(Pdb) b power_of_itself

Breakpoint 2 at c:\users\lifei\desktop\demopdb.py:3

(Pdb) b

Num Type Disp Enb Where

2 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:3

(Pdb)
When we type ‘c’, the command prompt starts execution, and keeps executing until it reaches the first
breakpoint. In our last step, we added a breakpoint at the declaration of ‘power_of_itself’. That is why it stops
here. Look:

(Pdb) l

1 import pdb

2 x=8

3 B def power_of_itself(a):

4 -> return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb)

We’re at line 4.
Okay, let’s see what commands we have for the Python debugger.
Read: Best Python Libraries

5. Python Debugger Commands for the Command Prompt

Let’s take a look at some of the most important Python debugger commands.

i. h(elp) [command]
Typing in ‘h’ will display help about all available Python debugger commands.

(Pdb) h

Documented commands (type help <topic>):


========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================

exec pdb

(Pdb)

ii. w(here)
‘w’ prints a stack trace, with the most-recent frame at the bottom.

(Pdb) w

c:\users\lifei\desktop\demopdb.py(8)<module>()

-> three=power_of_itself(3)

> c:\users\lifei\desktop\demopdb.py(4)power_of_itself()

-> return a**a

(Pdb)

iii. d(own) [count]


‘d’ moves the current frame count down by a certain number of levels, in the stack trace, to a newer frame. The
default for this count is 1.

iv. u(p) [count]


This moves the current frame count up by a certain number of levels, up in the stack trace, to an older frame.
Again, the default for this count is 1.

v. b(reak) [([filename:]lineno | function) [, condition]]


‘b’ informs us about the breakpoints in our code.

With a line number after it, it adds a breakpoint at the line number.

With a function name after it, it adds a breakpoint at the first line under the function declaration.

vi. tbreak [([filename:]lineno | function) [, condition]]


This adds a temporary breakpoint. When first hit, the command prompt removes it. Okay, time for a tea-break
(pun intended).

vii. cl(ear) [filename:lineno | bpnumber [bpnumber …]]


This clears all breakpoints from our code.
viii. disable [bpnumber [bpnumber …]]
The ‘disable’ command disables the breakpoints given as a list of breakpoint numbers. When we disable a
breakpoint, it stays in the list of breakpoints, and we can re-enable it when we want.

(Pdb) b 7

Breakpoint 1 at c:\users\lifei\desktop\demopdb.py:7

(Pdb) b

Num Type Disp Enb Where

1 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:7

(Pdb) b 9

Breakpoint 2 at c:\users\lifei\desktop\demopdb.py:9

(Pdb) b

Num Type Disp Enb Where

1 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:7

2 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:9

(Pdb) disable 2

Disabled breakpoint 2 at c:\users\lifei\desktop\demopdb.py:9

(Pdb) b

Num Type Disp Enb Where

1 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:7

2 breakpoint keep no at c:\users\lifei\desktop\demopdb.py:9

(Pdb)

enable [bpnumber [bpnumber …]]

This enables a breakpoint.

(Pdb) enable 2

Enabled breakpoint 2 at c:\users\lifei\desktop\demopdb.py:9

(Pdb) b
Num Type Disp Enb Where

1 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:7

2 breakpoint keep yes at c:\users\lifei\desktop\demopdb.py:9

(Pdb)

ix. ignore bpnumber [count]


This command sets the ignore count for the specified breakpoint number. The default is 0. When the ignore
count for a breakpoint is 0, it becomes active. Other times, the count decrements every time the prompt reaches
the breakpoint when it’s enabled, and when any associated condition is true.

(Pdb) ignore 1 2

Will ignore next 2 crossings of breakpoint 1.

(Pdb)

x. condition bpnumber [condition]


This command sets a new condition for the specified breakpoint, before it is honored. This may be an
expression which is true. Since it is an optional argument, if we don’t provide it, it makes the breakpoint
unconditional by removing any existing condition.

xi. commands [bpnumber]


‘commands’ specifies a list of commands for the breakpoint bpnumber.

(Pdb) commands 1

(com) p x

(com) end

(Pdb)

xii. s(tep)
This executes the current line, and stops at the first possible chance.
Let’s try executing this program step by step.

C:\Users\lifei\Desktop>python demopdb.py

> c:\users\lifei\desktop\demopdb.py(6)<module>()

-> seven=power_of_itself(7)

(Pdb) l
1 import pdb

2 x=8

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 -> seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

–Call–

> c:\users\lifei\desktop\demopdb.py(3)power_of_itself()

-> def power_of_itself(a):

(Pdb) l

1 import pdb

2 x=8

3 -> def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

> c:\users\lifei\desktop\demopdb.py(4)power_of_itself()

-> return a**a


(Pdb) l

1 import pdb

2 x=8

3 def power_of_itself(a):

4 -> return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

–Return–

> c:\users\lifei\desktop\demopdb.py(4)power_of_itself()->823543

-> return a**a

(Pdb) l

1 import pdb

2 x=8

3 def power_of_itself(a):

4 -> return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

> c:\users\lifei\desktop\demopdb.py(7)<module>()
-> print(seven)

(Pdb) l

2 x=8

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 -> print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

823543

> c:\users\lifei\desktop\demopdb.py(8)<module>()

-> three=power_of_itself(3)

(Pdb) l

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 -> three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

–Call–

> c:\users\lifei\desktop\demopdb.py(3)power_of_itself()

-> def power_of_itself(a):


(Pdb) l

1 import pdb

2 x=8

3 -> def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

> c:\users\lifei\desktop\demopdb.py(4)power_of_itself()

-> return a**a

(Pdb) l

1 import pdb

2 x=8

3 def power_of_itself(a):

4 -> return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

–Return–

> c:\users\lifei\desktop\demopdb.py(4)power_of_itself()->27
-> return a**a

(Pdb) l

1 import pdb

2 x=8

3 def power_of_itself(a):

4 -> return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 print(three)

[EOF]

(Pdb) s

> c:\users\lifei\desktop\demopdb.py(9)<module>()

-> print(three)

(Pdb) l

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 -> print(three)

[EOF]

(Pdb) s

27

–Return–

> c:\users\lifei\desktop\demopdb.py(9)<module>()->None

-> print(three)
(Pdb) l

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 print(seven)

8 three=power_of_itself(3)

9 -> print(three)

[EOF]

(Pdb)

n(next)

‘n’ continues executing until it reaches the current function’s next line, or until the function returns.
Any doubt yet in Python Debugger? Please Comment.

xiii. unt(il) [lineno]


When we run this command without an argument, it continues executing until it reaches a line with a number
greater than the current. When called with a line number, it continues executing until it reaches a line with a
number equal to or greater than that.
(Pdb) unt 8

823543

> c:\users\lifei\desktop\demopdb.py(8)<module>()

– > three=power_of_itself(3)

(Pdb)

r(eturn) – This continues executing until the current function returns.

xiv. c(ont(inue))
This continues executing, and only stops when it reaches a breakpoint.

xv. j(ump) lineno


This jumps to the specified line number. But this is only available in the bottom-most frame, so you can jump
back to execute code again.

xvi. l(ist) [first[, last]]


This lists the current file’s source code.
xvii. ll | longlist
This lists the source code for the current fram or function.

xviii. a(rgs)
This prints the arguments for the current function.

(Pdb) ll

1 import pdb

2 x=8

3 def power_of_itself(a):

4 return a**a

5 pdb.set_trace()

6 seven=power_of_itself(7)

7 -> print(seven)

8 three=power_of_itself(3)

9 print(three)

(Pdb) a

(Pdb) r

823543

> c:\users\lifei\desktop\demopdb.py(8)<module>()

-> three=power_of_itself(3)

(Pdb) r

–Call–

> c:\users\lifei\desktop\demopdb.py(3)power_of_itself()

-> def power_of_itself(a):

(Pdb) a

a=3

(Pdb)
xix p expression & pp expression
This prints the value of an expression.

(Pdb) p x

(Pdb)

This prints an expression’s value, but pretty-prints it using the pprint module.

Read: File Handling in Python

xxi. whatis expression


This prints the type of the specified expression.

(Pdb) whatis x

<class ‘int’>

(Pdb)

xxii. source expression


This tries to get the source code for the specified object, and displays it.

(Pdb) source power_of_itself

3 def power_of_itself(a):

4 return a**a

(Pdb)

xxiii. display [expression]


This displays an expression’s value if it changed.

(Pdb) display x

display x: 8

(Pdb) display

Currently displaying:

x: 8
(Pdb)

xxiv. undisplay [expression


This doesn’t display the specified expression in the current frame.

xxv. interact
This starts an interactive interpreter holding all current scope names in its global namespace.

(Pdb) interact

*interactive*

>>> 2+3

>>>

xxvi. alias [name [command]]


This command creates an alias called name that executes command.

(Pdb) alias y p x

(Pdb) y

(Pdb)

Here, we created an alias ‘y’ to print ‘x’.

xxvii. unalias name


This deletes the alias name.

(Pdb) unalias y

(Pdb) y

*** NameError: name ‘y’ is not defined

(Pdb)

xxviii. ! statement
This executes a single line of statement in the current stack frame’s context.
(Pdb) p x

(Pdb) !x=9

(Pdb) p x

(Pdb)

xxix. restart [args …]


This restarts the debugged Python program.

xxx. q(uit)
This quits the Python debugger.

(Pdb) q

Traceback (most recent call last):

File “demopdb.py”, line 6, in <module>

seven=power_of_itself(7)

File “demopdb.py”, line 6, in <module>

seven=power_of_itself(7)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\bdb.py”, line 48, in trace_dispatch

return self.dispatch_line(frame)

File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\bdb.py”, line 67, in dispatch_line

if self.quitting: raise BdbQuit

bdb.BdbQuit

C:\Users\lifei\Desktop>

6. Python Debugger Functions

The Python debugger also supports some functions.

a. run(statement, globals=None, locals=None)


This will execute statement under the debugger’s control.

>>> import pdb

>>> def add(a,b):

pdb.set_trace()

print("This will add the two values")

print(f"a={a}")

print(f"b={b}")

return a+b

print("Okay, bye")

>>> pdb.run('add')

> <string>(1)<module>()->None

(Pdb)

b. runeval(expression, globals=None, locals=None)


This evaluates expression under the debugger’s control.

c. runcall(function, *args, **kwds)


This calls function with the specified arguments.

d. set_trace()
This lets us enter the Python debugger at the calling stack frame.

e. post_mortem(traceback=None)
This enters us into post-mortem debugging of the specified traceback object.

f. pm()
This enters the post-mortem debugging of the traceback in sys.last_traceback.
Read: Python Packages

This was all on Python Debugger. Hope you like tutorial on Python Debugger.

7. Conclusion
Now that you know how to debug a program, we want you to try the Python debugger next time you face a
problem with your code. Don’t sit and expect to figure it out from scratch in a second; use the ‘pdb’. In this
Python Debugger tutorial if you face any query feel free to drop a comment.

Python Multiprocessing Module With Example


by DataFlair Team · June 13, 2019

1. Python Multiprocessing – Objective

Today, in this Python tutorial, we will see Python Multiprocessing. Moreover, we will look at the package and
structure of Multiprocessing in Python. Also, we will discuss process class in Python Multiprocessing and also
get information about the process. To make this happen, we will borrow several methods from the
multithreading module. Along with this, we will learn lock and pool class Python Multiprocessing.

So, let’s begin the Python Multiprocessing tutorial.

Python Multiprocessing Module With Example

2. What is Python Multiprocessing?

First, let’s talk about parallel processing. This is a way to simultaneously break up and run program tasks on
multiple microprocessors. In effect, this is an effort to reduce processing time and is something we can achieve
with a computer with two or more processors or using a computer network. We also call this parallel
computing.
You must learn about Python Modules
Okay, now coming to Python Multiprocessing, this is a way to improve performance by creating parallel code.
CPU manufacturers make this possible by adding more cores to their processors. In a multiprocessing system,
applications break into smaller routines to run independently. Take a look at a single processor system. Given
several processes at once, it struggles to interrupt and switch between tasks. How would you do being the only
chef in a kitchen with hundreds of customers to manage? You would have to be the one to execute every single
routine task from baking to kneading the dough.

a. Python Multiprocessing Package


Multiprocessing in Python is a package we can use with Python to spawn processes using an API that is much
like the threading module. With support for both local and remote concurrency, it lets the programmer make
efficient use of multiple processors on a given machine. Before we can begin explaining it to you, let’s take an
example of Pool- an object, a way to parallelize executing a function across input values and distributing input
data across processes. This is data parallelism (Make a module out of this and run it)-

from multiprocessing import Pool

def f(x):

return x*x

with Pool(5) as p:

print(p.map(f,[1,2,3]))

[1, 4, 9]
Want to find out how many cores your machine has? Try the cpu_count() method.

>>> import multiprocessing

>>> multiprocessing.cpu_count()

b. Structure of a Python Multiprocessing System


So what is such a system made of? We have the following possibilities:

A multiprocessor- a computer with more than one central processor.

A multi-core processor- a single computing component with more than one independent actual processing
units/ cores.

Do you know about Python Library


In either case, the CPU is able to execute multiple tasks at once assigning a processor to each task.

3. Python Multiprocessing Process Class

Let’s talk about the Process class in Python Multiprocessing first. This is an abstraction to set up another
process and lets the parent application control execution. Here, we observe the start() and join() methods. Let’s
first take an example.

import multiprocessing

from multiprocessing import Process


def testing():

print("Works")

def square(n):

print("The number squares to ",n**2)

def cube(n):

print("The number cubes to ",n**3)

if name ==" main__":

p1=Process(target=square,args=(7,))

p2=Process(target=cube,args=(7,))

p3=Process(target=testing)

p1.start()

p2.start()

p3.start()

p1.join()

p2.join()

p3.join()

print("We're done")

We saved this as pro.py on our desktop and then ran it twice from the command line. This is the output we got:

Process class in Python Multiprocessing

Python Multiprocessing – Process class

Let’s revise Python Class and object


Let’s understand this piece of code. Process() lets us instantiate the Process class. start() tells Python to begin
processing. But then if we let it be, it consumes resources and we may run out of those at a later point in time.
This is because it lets the process stay idle and not terminate. To avoid this, we make a call to join(). With this,
we don’t have to kill them manually. Join stops execution of the current program until a process completes. This
makes sure the program waits for p1 to complete and then p2 to complete. Then, it executes the next statements
of the program. One last thing, the args keyword argument lets us specify the values of the argument to pass.
Also, target lets us select the function for the process to execute.

4. Getting Information about Processes

a. Getting Process ID and checking if alive


We may want to get the ID of a process or that of one of its child. We may also want to find out if it is still
alive. The following program demonstrates this functionality:

import multiprocessing

from multiprocessing import Process

import os

def child1():

print("Child 1",os.getpid())

def child2():

print("Child 2",os.getpid())

if name ==" main__":

print("Parent ID",os.getpid())

p1=Process(target=child1)

p2=Process(target=child2)

p1.start()

p2.start()

p1.join()

alive='Yes' if p1.is_alive() else 'No'

print("Is p1 alive?",alive)

alive='Yes' if p2.is_alive() else 'No'

print("Is p2 alive?",alive)

p2.join()

print("We're done")
Getting Information

Getting Information

In Python multiprocessing, each process occupies its own memory space to run independently. It terminates
when the target function is done executing.
Have a look at Python Data Structures

b. Getting Process Name


We can also set names for processes so we can retrieve them when we want. This is to make it more human-
readable.

import multiprocessing

from multiprocessing import Process, current_process

import os

def child1():

print(current_process().name)

def child2():

print(current_process().name)

if name ==" main__":

print("Parent ID",os.getpid())

p1=Process(target=child1,name='Child 1')

p2=Process(target=child2,name='Child 2')

p1.start()

p2.start()

p1.join()
p2.join()

print("We're done")

Python Multiprocessing – Process name

As you can see, the current_process() method gives us the name of the process that calls our function. See what
happens when we don’t assign a name to one of the processes:

import multiprocessing

from multiprocessing import Process, current_process

import os

def child1():

print(current_process().name)

def child2():

print(current_process().name)

if name ==" main__":

print("Parent ID",os.getpid())

p1=Process(target=child1)

p2=Process(target=child2,name='Child 2')

p1.start()

p2.start()

p1.join()

p2.join()

print("We're done")

Getting process name


Well, the Python Multiprocessing Module assigns a number to each process as a part of its name when we
don’t.

5. Python Multiprocessing Lock

Just like the threading module, multiprocessing in Python supports locks. The process involves importing Lock,
acquiring it, doing something, and then releasing it. Let’s take a look.
Do you know about Python Dictionaries
In the following piece of code, we make a process acquire a lock while it does its job.

from multiprocessing import Process, Lock

lock=Lock()

def printer(item):

lock.acquire()

try:

print(item)

finally:

lock.release()

if name ==" main__":

items=['nacho','salsa',7]

for item in items:

p=Process(target=printer,args=(item,))

p.start()

Let’s run this code thrice to see what different outputs we get.
The lock doesn’t let the threads interfere with each other. The next process waits for the lock to release before it
continues.
7
salsa
nacho

nacho
salsa
7

7
nacho
salsa
6. Python Multiprocessing Pool Class

This class represents a pool of worker processes; its methods let us offload tasks to such processes. Let’s take an
example (Make a module out of this and run it).

from multiprocessing import Pool

def double(n):

return n*2

if name ==' main ':

nums=[2,3,6]

pool=Pool(processes=3)

print(pool.map(double,nums))

[4, 6, 12]
We create an instance of Pool and have it create a 3-worker process. map() maps the function double and an
iterable to each process. The result gives us [4,6,12].
Another method that gets us the result of our processes in a pool is the apply_async() method.
Let’s take a tour to Python Strings

from multiprocessing import Pool

def double(n):

return n*2

if name ==' main ':

pool=Pool(processes=3)

result=pool.apply_async(double,(7,))

print(result.get(timeout=1))

14
So, this was all in Python Multiprocessing. Hope you like our explanation.

7. Conclusion – Python Multiprocessing

Hence, in this Python Multiprocessing Tutorial, we discussed the complete concept of Multiprocessing in
Python. Moreover, we looked at Python Multiprocessing pool, lock, and processes. Now, you have an idea of
how to utilize your processors to their full potential. Feel free to explore other blogs on Python attempting to
unleash its power. See you again.
Python XML Parser – XML Processing with Python 3
by DataFlair Team · July 2, 2019

1. Python XML Parser


In this Python XML Parser Tutorial, we will study what is Python XML Processing. Moreover, we will study
the Python XML Parser Architecture and API and Python XML FIle. Along with this, we will learn Python
Parsing XML with DOM and SAX.

So, let’s start Python XML Parser Tutorial.

Python XML Parser – XML Processing with Python 3

2. What is XML?
Extensible Markup Language (XML) is a markup language which encodes documents by defining a set of rules
in both machine-readable and human-readable format.

Extended from SGML (Standard Generalized Markup Language), it lets us describe the structure of the
document. In XML, we can define custom tags. We can also use XML as a standard format to exchange
information.

Let’s revise Bitwise Operator in Python with Syntax and Example

3. Python XML Parser Architecture and API


Like we mentioned earlier, we will make use of two APIs to deal with Python XML Parser here- SAX and
DOM.
a. SAX (Simple API for XML)
When we have large documents or memory limitations, we can register callbacks for certain events. Then, we
can let the parser to parse the file as reading it from the disk. Because of this, it never stores the entire file in the
memory. It is read-only.

Let’s discuss Methods in Python – Classes, Objects and Functions in Python

b. DOM (Document Object Model API)


A W3C recommendation, DOM can represent all features of an XML document by reading an entire file into
memory and storing it hierarchically. This is the faster choice when working with large files. For a large project,
you can use both, as they complement each other. But using this on too many small files can suck your
resources up.
Read CGI Programming in Python with Functions and Modules

4. Python XML File


This is the Python XML file we’re going to use:

<?xml version="1.0"?>

<genre catalogue="Pop">

<song title="No Tears Left to Cry">

<artist>Ariana Grande</artist>

<year>2018</year>

<album>Sweetener</album>

</song>

<song title="Delicate">

<artist>Taylor Swift</artist>

<year>2018</year>

<album>Reputation</album>

</song>

<song title="Mrs. Potato Head">

<artist>Melanie Martinez</artist>

<year>2015</year>

<album>Cry Baby</album>
</song>

</genre>

We have saved it as songs.xml on our Desktop

Python XML Parser- XML File

This stores a collection of songs in the genre Pop. The ones we have currently include the songs No Tears left
To Cry, Delicate, and Mrs. Potato Head.

5. Python XML Parser with DOM


DOM stands for Document Object Model and is a cross-language API from the W3C that lets us access and
modify XML documents. We can use it for random-access applications. While with SAX, you can only ever
access one SAX element at once.

Let’s use the xml.dom module to load an XML document and create a minidom object. Then, using the parser()
method, we can create a DOM tree from this document.

Have a look at Data Structures in Python – Lists, Tuples, Sets, Dictionaries

>>> from xml.dom.minidom import parse

>>> import xml.dom.minidom

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> DOMTree = xml.dom.minidom.parse("songs.xml") #Opening the XML document


>>> genre=DOMTree.documentElement

>>> if genre.hasAttribute('catalogue'):

print(f'Root: {genre.getAttribute("catalogue")}')

Root: Pop

>>> songs=genre.getElementsByTagName('song') #Get all songs in the genre Pop

>>> for song in songs: #Print each song’s details

print('Song:')

if song.hasAttribute('title'):

print(f'Title: {song.getAttribute("title")}')

artist=song.getElementsByTagName('artist')[0]

print(f'Artist: {artist.firstChild.data}')

year=song.getElementsByTagName('year')[0]

print(f'Release Year: {year.firstChild.data}')

album=song.getElementsByTagName('album')[0]

print(f'Album: {album.firstChild.data}')

Output:
Song:
Title: No Tears Left to Cry
Artist: Ariana Grande
Release Year: 2018
Album: Sweetener
Song:
Title: Delicate
Artist: Taylor Swift
Release Year: 2018
Album: Reputation
Song:
Title: Mrs. Potato Head
Artist: Melanie Martinez
Release Year: 2015
Album: Cry Baby

6. Python XML Parser with SAX


SAX is a standard interface and will help you with event-driven XML parsing. You’ll need to subclass
xml.sax.ContentHandler to create a ContentHandler for this purpose. This will handle your tags and attributes
and will also serve methods for handling parsing events. The Python XML parser that owns it calls these
methods parsing the XML file.

Let’s look at some of these methods. When it begins parsing the file, it calls startDocument(), and calls
endDocument() when ending the parsing at the end of the file. Also, it passes the XML file’s character data as a
parameter to the characters(text) method.

At the start and end of each element, it calls the ContentHandler. If the Python XML parser is in namespace
mode, it calls methods startElementNS() and endElementNS(). Otherwise, it calls startElement(tag, attributes)
and endElement(tag), where a tag is the element tag and attributes is an Attributes object.

Learn about Python Iterables with examples

Now, in Python XML Processing, let’s take a look at few methods first.

Python XML Pearser- methods os parsing with SAX

a. make_parser()
This method creates and returns a parser of the first type the system can find. This is the syntax:

xml.sax.make_parser([parser_list])

It takes a list of parsers to be used.

b. parse()
This uses the following syntax:

xml.sax.parse(xmlfile,contenthandler[,errorhandler])
This creates a SAX parser and then uses it in parsing the document specified by the parameter xmlfile.
contenthandler is a ContentHandler object and errorhandler is a SAX ErrorHandler object.
Read Python Date and Time – Syntax and examples

c. parseString()
This method creates a SAX parser and parses an XML string. It has the following syntax:

xml.sax.parseString(xmlstring,contenthandler[,errorhandler])

The parameter xmlstring is the XML string to read from and the other two parameters are the same as above.
Example
Now, let’s take an example program to parse an XML document using SAX.

>>> import xml.sax

>>> class SongHandler(xml.sax.ContentHandler):

def init (self):

self.CurrentData=''

self.artist=''

self.year=''

self.album=''

def startElement(self,tag,attributes):

self.CurrentData=tag

if tag=='song':

print('Song:')

title=attributes['title']

print(f'Title: {title}')

def endElement(self,tag):

if self.CurrentData=='artist':

print(f'Artist: {self.artist}')

elif self.CurrentData=='year':

print(f'Year: {self.year}')

elif self.CurrentData=='album':

print(f'Album: {self.album}')
self.CurrentData=''

def Characters(self,content):

if self.CurrentData=='artist':

self.artist=content

elif self.CurrentData=='year':

self.year=content

elif self.CurrentData=='album':

self.album=content

>>> if name ==' main ':

parser=xml.sax.make_parser() #creating an XMLReader

parser.setFeature(xml.sax.handler.feature_namespaces,0) #turning off namespaces

Handler=SongHandler()

parser.setContentHandler(Handler) #overriding default ContextHandler

parser.parse('songs.xml')

Output:
Song:
Title: No Tears Left to Cry
Artist: Ariana Grande
Year: 2018
Album: Sweetener
Song:
Title: Delicate
Artist: Taylor Swift
Year: 2018
Album: Reputation
Song:
Title: Mrs. Potato Head
Artist: Melanie Martinez
Year: 2015
Album: Cry Baby

So, this was all about Python XML Parser tutorial. Hope you like our explanation.

7. Conclusion
Hence, we have a complete understanding of XML processing in Python 3. In addition, we studied Python
XML parser architecture and Python XML file. In addition, we studied 2 API for Python XML Parser that is
SAX and DOM. Also,. At last, we discussed methods of SAS XML Parser. Furthermore, if you have any query,
feel free to ask in the comment section.
Python CGI Programming Tutorial – Python CGI Module &
Functions
by DataFlair Team · September 27, 2018

1. Python CGI Tutorial

This Python CGI Programming tutorial deals with the ‘cgi’ module, the support module for CGI scripts in
Python. Moreover, we will discuss the functions of Python CGI Programming.

So, let’s begin Python CGI Programming Tutorial.

Python CGI Programming Tutorial – Python CGI Module & Functions

2. What is Python CGI Programming?


Python CGI stands for Common Gateway Interface. An HTTP server invokes a Python CGI script so it can
process user input that a user may submit through an HTML <FORM> or <ISINDEX> element.

Such a script usually lives in the server’s special cgi-bin directory. For a request, the server places information
about it- the client’s hostname, the requested URL, the query string, and other information- in the script’s shell
environment. It executes this script and sends the output back to the client.

The input for this script connects to the client, and server sometimes reads the form data this way. Other times,
it passes form data through a query string, which is a part of the URL. A query string holds data that doesn’t
conventionally fit a hierarchical path structure. For example: http://en.wikipedia.org/w/index.php?
title=Main_page&action=raw

Python CGI module handles situations and helps debug scripts. With the latest addition, it also lends us support
for uploading files from a form.
So, what does a Python CGI script output? It gives out two sections separated by a blank line. Of these, the first
section holds headers that instruct the client that a certain kind of data follows. Let’s try generating a minimal
header section in this Python CGI Programming Example.

>>> print("Content-Type: text/html")

>>> print()

The first statement tells the server that html code follows; the second(a blank line) indicates the end of the
header. And then, the second section is the HTML. This lets the client software display formatted text:

>>> print("<TITLE>CGI Script Output</TITLE>")

>>> print("<H1>This is our first CGI script</H1>")

>>> print("Hello, world!")

a. Python CGI Programming Architecture

Let’s take a look at the architecture of Python CGI Programming.

Python CGI Programming Architecture

3. Python cgi Module

Let’s begin with the Python cgi module. First, let’s import it.

>>> import cgi

>>>

Now, we type this to write a new scipt:


>>> import cgitb

>>> cgitb.enable()

>>>

What this does is that it activates an exception handler to display detailed reports in the web browser in event of
an error. If however, you want to save the reports to a file, you can:

>>> import cgitb

>>> cgitb.enable(display=0, logdir="/path/to/logdir")

We can do this while developing a script. These reports help us track down bugs faster. Once we’re confident
that our script works correctly, we can remove this.

Now, we use the class FieldStorage to work with the submitted form data. We can set the encoding keyword
parameter to the encoding defined for the document if the form holds non-ASCII characters. You’ll find this is
the <META> tag in the <HEAD> section of your HTML document. Alternatively, you may find it in the
Content-type header. FieldStorage reads form contents from the standard input or from the environment.
Instantiate it only once because it may consume standard input.

We can index a FieldStorage instance like a Python dictionary. This lets us test membership with the ‘in’
operator. We can then also call method keys() and function len(). Note that it ignores fields with empty string
values. If we want to keep them, we can provide a True value for the optional keyword parameter
keep_blank_values at the time of creating a FieldStorage instance.
Take a look at this code that ensures that fields ‘name’ and ‘addr’ are set to a non-empty string:

>>> form = cgi.FieldStorage()

>>> if "name" not in form or "addr" not in form:

print("<H1>Error</H1>")

print("Please fill in the name and addr fields.")

return

>>> print("<p>name:", form["name"].value)

>>> print("<p>addr:", form["addr"].value)

…further form processing here…

The fields that we access through form[key] are instances of FieldStorage. If there’s more than one field with
the same name, this retrieves a list of instances of FieldStorage or MiniField Storage. The getlist() method
returns a list of values:

>>> value = form.getlist("username")

>>> usernames = ",".join(value)


Consider the situation when a field denotes an uploaded file. When we access the value using the attribute
‘value’, or using the method getvalue(), it reads the entire file in memory as bytes. To test for an uploaded file,
we can test either the ‘filename’ attribute or the ‘file’ attribute. Then, we can read the data from the ‘file’
attribute before it automatically closes as a part of the garbage collection of the FieldStorage instance:

>>> fileitem = form["userfile"]

>>> if fileitem.file:

#It's an uploaded file; count lines

linecount = 0

>>> while True:

line = fileitem.file.readline()

if not line: break

linecount = linecount + 1

We can also use a FieldStorage object in a with-statement. It automatically closes it when done.
If an error occurs obtaining an uploaded file’s contents, like if a user interrupts form submission pressing Back
or Cancel, it sets the ‘done’ attribute for the object for the field to -1.

It is possible that the user uploads multiple files from one field. Then, the item is a dictionary-like FieldStorage
item. We can test its ‘type’ attribute to determine this. This should be multipart/form-data. We can recursively
iterate over it like the top-level form object.
When a user submits a form in the old format- as a query string, this makes the items instances of
MiniFieldStorage. This makes the attributes ‘list’, ‘file’, and ‘filename’ attributes equal to None. Forms
submitted through POST also have a query string holding both

FieldStorage and MiniFieldStorage items.

4. Functions of Python CGI Programming

For more control over your CGI programming in Python, you can use the following functions:

a. cgi.parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False)

This will parse a query in the environment. We can also make it do so from a file, the default for which is
sys.stdin.

b. cgi.parse_qs(qs, keep_blank_values=False, strict_parsing=False)

While this is deprecated, Python maintains it for backwards-compatibility. But we can use
urllib.parse.parse_qs() instead.

c. cgi.parse_qsl(qs, keep_blank_values=False, strict_parsing=False)


This is deprecated too, and is maintained for backwards-compatibility. You may use urllib.parse.parse_qsl()
instead.

d. cgi.parse_multipart(fp,pdict)

This function will parse input of type multipart/form-data for file uploads. The first argument takes the input
file, and the second takes a dictionary holding other parameters in the Content-Type header.

e. cgi.parse_header(string)

parse_header() passes a MIME header into a main value and into a dictionary of parameters.

f. cgi.test()

This function is a test CGI script, and we can use it as the main program. It will write minimal HTTP headers
and formats.

g. cgi.print_environ()

This formats the shell environment in HTML.

h. cgi.print_form(form)

This function formats a form in HTML.

i. cgi.print_directory()

This will format the current directory in HTML.

j. cgi.print_environ_usage()

This prints a list of all useful environment variables in HTML.

k. cgi.escape(s, quote=False)

escape() will convert characters ‘<’, ‘>’, and ‘&’ in the string ‘s’ to HTML-safe sequences. If you must display
text holding such characters, you may use this in HTML.

So, this was all on Python CGI Programming tutorial. Hope you like our explanation

5. Conclusion

Hence, we studied what is Python CGI Programming with Python CGI Module. In addition, we discussed
functions and architecture of CGI Programming in Python. Furthermore, if you have any query, feel free to aks
in the comment box.

Python Libraries – Python Standard Library & List of Important


Libraries
by DataFlair Team · June 27, 2019
1. Python Libraries

After Modules and Python Packages, we shift our discussion to Python Libraries. This Python Library
Tutorial, we will discuss Python Standard library and different libraries offered by Python Programming
Language: Matplotlib, scipy, numpy, etc.

So, let’s start the Python Libraries Tutorial.

Python Libraries – Python Standard Library & List of Important Libraries

2. What is the Python Libraries?

We know that a module is a file with some Python code, and a package is a directory for sub packages and
modules. But the line between a package and a Python library is quite blurred.

A Python library is a reusable chunk of code that you may want to include in your programs/ projects.
Compared to languages like C++ or C, a Python libraries do not pertain to any specific context in Python. Here,
a ‘library’ loosely describes a collection of core modules. Essentially, then, a library is a collection of modules.
A package is a library that can be installed using a package manager like rubygems or npm.

Learn: A Comprehensive Guide on Python Packages

3. Python Standard Library

The Python Standard Library is a collection of exact syntax, token, and semantics of Python. It comes bundled
with core Python distribution. We mentioned this when we began with an introduction.

It is written in C, and handles functionality like I/O and other core modules. All this functionality together
makes Python the language it is. More than 200 core modules sit at the heart of the standard library. This library
ships with Python. But in addition to this library, you can also access a growing collection of several thousand
components from the Python Package Index (PyPI). We mentioned it in the previous blog.

Learn: Python Tuples vs Lists – Comparison between Lists and Tuples

4. Important Python Libraries

Next, we will see twenty Python libraries list that will take you places in your journey with Python. These are
also the Python libraries for Data Science.

a. Matplotlib
Matplotlib helps with data analyzing, and is a numerical plotting library. We talked about it in Python for Data
Science.

Python Libraries Tutorial- matplotlib

b. Pandas
Like we’ve said before, Pandas is a must for data-science. It provides fast, expressive, and flexible data
structures to easily (and intuitively) work with structured (tabular, multidimensional, potentially heterogeneous)
and time-series data.
Python Libraries Tutorial – Pandas

c. Requests
Requests is a Python Library that lets you send HTTP/1.1 requests, add headers, form data, multipart files, and
parameters with simple Python dictionaries. It also lets you access the response data in the same way.

Python Libraries Tutorial- Requests

Learn: How to Install Python on Windows

d. NumPy
It has advanced math functions and a rudimentary scientific computing package.
Python Libraries Tutorial – NumPy

e. SQLAlchemy
Python Libraries Tutorial – SQLAIchemy Overview

SQLAlchemy is a library with well-known enterprise-level patterns. It was designed for efficient and high-
performing database-access.

f. BeautifulSoup
It may be a bit slow, BeautifulSoup has an excellent XML- and HTML- parsing library for beginners.
Python Libraries Tutorial – BeautifulSoup

g. Pyglet
Pyglet is an excellent choice for an object-oriented programming interface in developing games. In fact, it also
finds use in developing other visually-rich applications for Mac OS X, Windows, and Linux. In the 90s, when
people were bored, they resorted to playing Minecraft on their computers. Pyglet is the engine behind
Minecraft.

Python Libraries Tutorial – Pyglet

h. SciPy
Next up is SciPy, one of the libraries we have been talking so much about. It has a number of user-friendly and
efficient numerical routines. These include routines for optimization and numerical integration.
Python Libraries Tutorial- SciPy

Learn: 7 Reasons Why Should I Learn Python in 2018

i. Scrapy
If your motive is fast, high-level screen scraping and web crawling, go for Scrapy. You can use it for purposes
from data mining to monitoring and automated testing.
Python Libraries Tutorial- Scrapy

j. PyGame
PyGame provides an extremely easy interface to the Simple Directmedia Library (SDL) platform-independent
graphic, audio, and input libraries.

Python Libraries Tutorial – PyGame

k. Python Twisted
An event-driven networking engine, Twisted is written in Python, and licensed under the open-source MIT
license.
Python Libraries Tutorial – Twisted

l. Pillow
Pillow is a friendly fork of PIL (Python Imaging Library), but is more user-friendly. If you work with images,
Pillow is your best friend.

Python Libraries Tutorial- Pillow

m. pywin32
This provides useful methods and class for interaction with Windows, as the name suggests.
Python pywin32 Library

n. wxPython
It is a wrapper around wxWidgets for Python.
Python wxPython Library

o. iPython
iPython Python Library has an architecture that facilitates parallel and distributed computing. With it, you can
develop, execute, debug, and monitor parallel applications.
Python Library – iPython

Learn: Python Regular Expressions

p. Nose
Nose delivers an alternate test discovery and running process for unittest. This intends to mimic py.test’s
behavior as much as it can.
Python Nose Library

q. Flask
A web framework, Flask is built with a small core and many extensions.

Python Flask Library

r. SymPy
It is an open-source library for symbolic math. With very simple and comprehensible code that is easily
extensible, SymPy is a full-fledged Computer Algebra System (CAS). It is written in Python, and hence does
not need external libraries.

Python SymPy Library

s. Fabric
Along with being a library, Fabric is a command-line tool for streamlining the use of SSH for application
deployment or systems administration tasks. With it, you can execute local or remote shell commands,
upload/download files, and even prompt running user for input, or abort execution.
Python Fabric Library

t. PyGTK
PyGTK lets you easily create programs with a GUI (Graphical User Interface) with Python.

Python PyGTK Library

Learn:The Tremendous Python Career Opportunities in 2018

So, this was all about Python Libraries Tutorial. Hope you like our explanation,

5. Conclusion

Now you know which libraries to go for if you choose to extend a career in Python. Many of these help us with
data-science as well. Or if you wish to go out of your way, create your own library, and get it published with the
PyPI; help the community grow. Furthermore, if you have any query, please share with us!

Python Matplotlib Tutorial – Python Plotting For Beginners


by DataFlair Team · July 6, 2019

1. Python Matplotlib Tutorial – Objective

In our previous tutorial, Python Data Cleansing. Today, we’ll play around with Python Matplotlib Tutorial and
Python Plot. Moreover, we will discuss Pyplot, Keyword String, and Categorical Variables of Python Plotting.
At last, we will cover Line properties and some Python Matplotlib example.
So, let’s start Python Matplotlib Tutorial.

Python Matplotlib Tutorial – Python Plotting For Beginners

2. What is Python Matplotlib?

Working across platforms, when you want to conceive publication quality figures in hardcopy formats and
interactive environments, you use matplotlib. This is a library for 2-dimensional plotting with Python.
Some plots it will let us build are:

Plots

Histograms

Error charts

Power spectra

Bar charts

Scatter Plots

Some features of Python Plot supports-

Font properties

Axes properties

Line styles

Follow this link to know about Python Packages


3. Python Matplotlib Tutorial – Prerequisites

For our Python Matplotlib tutorial, we will need two Python libraries.

a. Python Matplotlib
We’ve already discussed this in section 2. To install it, you can use the following command-

C:\Users\lifei>pip install matplotlib

b. Pandas
Pandas is another Python library we will use here. It lets us manipulate and analyze data using data structures
and operations on numerical tables and time series.

Matplotlib Tutorial – Python Plotting

You can install it with the following command-

C:\Users\lifei>pip install pandas

4. Python Matplotlib Tutorial – Pyplot

It has a pyplot interface. This holds command-like functions that let us alter a figure.

a. plot()
You can use the plot() method to create a plot of points on the graph.

>>> import matplotlib.pyplot as plt

>>> plt.plot([2,3,4,5])

[<matplotlib.lines.Line2D object at 0x00FD5650>]

>>> plt.xlabel('Actual birth weight')

Text(0.5,0,’Actual birth weight’)

>>> plt.ylabel('Estimated birth weight')

Text(0,0.5,’Estimated birth weight’)

>>> plt.show()
Python Matplotlib Tutorial – Python Plotting

Here, the methods xlabel() and ylabel() let us set labels for the x and y-axes respectively. It takes values from
our list of parameters for the y-axis; for the x-axis, it uses 0.0, 1.0, 2.0, and 3.0 for the four points.
We can give it more than one list of parameters-

>>> plt.plot([2,3,4,5],[3,8,10,12])

[<matplotlib.lines.Line2D object at 0x0153EFF0>]

>>> plt.show()

Python Matplotlib Tutorial – Python Plotting


This takes the first list for x-axis and the second for the y-axis.

b. Formatting your Python Plot


A third argument will let you choose the color and the line type of the plot in Python Programming Language.
The default format string gives us a solid blue line, as we’ve seen in the examples so far. This is ‘b-‘. You see,
these strings are from MATLAB.

>>> plt.plot([2,3,4,5],[3,8,10,12],'gs')

[<matplotlib.lines.Line2D object at 0x01AD1050>]

>>> plt.axis([0,7,0,21])

[0, 7, 0, 21]

>>> plt.show()

Python Matplotlib Tutorial – Python Plotting

Formatting your Python PlotThe axis() method lets us set the viewport for the axes in terms of xmin, xmax,
ymin, and ymax. The format string ‘gs’ here gives us green squares. Similarly, we can plot green, red, and blue
orbs, squares, and triangles too. Let’s take another example.
Read about CGI Programming in Python with Functions and Modules

>>> import numpy as np

>>> t=np.arange(0,5,0.2)

>>> plt.plot(t,t,'r--',t,t**3,'b^',t,t**2,'gs')
[<matplotlib.lines.Line2D object at 0x0A66B670>, <matplotlib.lines.Line2D object at 0x0A66B7B0>,
<matplotlib.lines.Line2D object at 0x0A66BA90>]

>>> plt.show()

Matplotlib Tutorial – Python Plotting

5. Python Matplotlib Keyword Strings

Using a data keyword argument, we can produce Python plots with strings that relate to some variables. We can
access these variables with strings.

>>> data={'a':np.arange(50),

'c':np.random.randint(0,50,50),

'd':np.random.randn(50)}

>>> data['b']=data['a']+10*np.random.randn(50)

>>> data['d']=np.abs(data['d'])*100

>>> plt.scatter('a','b',c='c',s='d',data=data)

<matplotlib.collections.PathCollection object at 0x0A7E8AD0>

>>> plt.show()
Matplotlib Tutorial -Keyword Strings

6. Categorical Variables to Python Plotting

We can pass categorical variables to Python plotting functions.

>>> names=["Dingos","Wild Cats","Tigers"]

>>> values=[1,11,111]

>>> plt.figure(1,figsize=(9,3))

<Figure size 900×300 with 0 Axes>

>>> plt.subplot(131)

<matplotlib.axes._subplots.AxesSubplot object at 0x00FD5B30>

>>> plt.bar(names,values)

<BarContainer object of 3 artists>

>>> plt.subplot(132)

<matplotlib.axes._subplots.AxesSubplot object at 0x01889610>

>>> plt.scatter(names,values)

<matplotlib.collections.PathCollection object at 0x0A191AB0>

>>> plt.subplot(133)

<matplotlib.axes._subplots.AxesSubplot object at 0x0A171110>


>>> plt.plot(names,values)

[<matplotlib.lines.Line2D object at 0x0A1ADE30>]

>>> plt.suptitle('Varsity')

Text(0.5,0.98,’Varsity’)

>>> plt.show()

Python Matplotlib Tutorial – Python Plotting

Python Matplotlib Tutorial – Categorical Variables to Python Plotting


Do you know How Python Send Email Via SMTP | SMTP Server

7. Some Line Properties of Matplotlib

Using some keyword arguments, we can alter how a graph looks. Let’s look at some.
Python Matplotlib Tutorial – Line Properties

a. Linewidth
>>> plt.plot([1,2,3],[2,4,9],linewidth=4.0)

[<matplotlib.lines.Line2D object at 0x010D05D0>]

>>> plt.show()

Python Matplotlib Tutorial – Linewidth


b. Alpha
We can use the alpha channel to create prettier plots by softening colors.

>>> plt.plot([1,2,3],[2,4,9],alpha=5.5)

[<matplotlib.lines.Line2D object at 0x00BC9310>]

>>> plt.show()

Python Matplotlib Tutorial – Alpha

c. Antialiased
If you’ll look closely, the lines look quite smooth. But we can turn antialiasing off- this will show us the
aliasing in the lines.

>>> plt.plot([1,2,3],[2,4,9],antialiased=True)

[<matplotlib.lines.Line2D object at 0x087278B0>]

>>> plt.show()
Python Matplotlib Tutorial – Antialiased

Let’s read about Image Processing with SciPy and NumPy in Python

d. Color or c
You can set the color of the plot with this parameter.

>>> plt.plot([1,2,3],[2,4,9],color='Chartreuse')

[<matplotlib.lines.Line2D object at 0x0AE98D70>]

>>> plt.show()
Matplotlib Tutorial – Color

e. Dashes
>>> plt.plot([1,2,3],[2,4,9],dashes=[1,2,4,4])

[<matplotlib.lines.Line2D object at 0x099ED290>]

>>> plt.show()

Python Dashes

f. Linestyle or ls
You can choose the style of line you want for your plot.

>>> plt.plot([1,2,3],[2,4,9],linestyle='steps')

[<matplotlib.lines.Line2D object at 0x00B816D0>]

>>> plt.show()
Python Matplotlib Tutorial – Linestyle or ls

>>> plt.plot([1,2,3],[2,4,9],linestyle=':')

[<matplotlib.lines.Line2D object at 0x0AC892D0>]

>>> plt.show()

Matplotlib Tutorial – Python Linestyle or ls

Let’s explore Python Database Access – Python 3 MySQL

g. Marker
A marker will let you select what symbol you want to display at breakpoints and bends.

>>> plt.plot([1,2,3],[2,4,9],marker='+')

[<matplotlib.lines.Line2D object at 0x01018790>]

>>> plt.show()
Python Marker

h. Markeredgecolor
You can also decide on a color for your markers’ edges.

>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgecolor='brown')

[<matplotlib.lines.Line2D object at 0x01280110>]

>>> plt.show()

Matplotlib Tutorial – Markeredgecolor

i. Markeredgewidth
This lets us decide how thick we want our markers to be.

>>> plt.plot([1,2,3],[2,4,9],marker='+',markeredgewidth=0.4)
[<matplotlib.lines.Line2D object at 0x0AE54290>]

>>> plt.show()

Matplotlib Tutorial – Markeredgewidth

Let’s Learn Aggregation and Data Wrangling with Python

j. Markerfacecolor and Markersize


This will let you choose which color to fill in your marker and what size to keep it.

>>> plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0)

[<matplotlib.lines.Line2D object at 0x0AE9BC10>]

>>> plt.show()

Python Matplotlib Tutorial – Markerfacecolor and Markersize


k. Markevery
This parameter lets us decide at what parameters we want to put markers.

Python Markevery plt.plot([1,2,3],

[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0,markevery=2)

[<matplotlib.lines.Line2D object at 0x0103C8B0>]

>>> plt.show()

>>> plt.plot([1,2,3],

[2,4,9],marker=’.’,markerfacecolor=’orange’,markersize=13.0,markevery=3) [<matplotlib.lines.Line2D
object at 0x01025F10>]

>>> plt.show()
Matplotlib Tutorial – Python Markevery

l. Zorder
This lets us decide which plots will show up in the front and which to send to back.

>>> plt.plot([1,2,3],[2,4,9],zorder=1,linewidth=4)

[<matplotlib.lines.Line2D object at 0x01255E70>]

>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)

[<matplotlib.lines.Line2D object at 0x01255770>]

>>> plt.show()

Matplotlib Tutorial – Python Plotting

In this graph, in the area enclosed by the points (1,2) and (2,4), both plots cover the same points, but the one in
orange is in the front. This is because we give it a higher zorder. This is like z-index in CSS.
Let’s Know about Python Stemming and Lemmatization – NLTK

8. Showing a Grid in Python Plot

You can use the grid() method to toggle a grid in your plot.

>>> plt.grid(True)

>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)

[<matplotlib.lines.Line2D object at 0x00DC02B0>]


>>> plt.show()

Showing a Grid in Python Plot

So, this was all about Python Matplotlib Tutorial. Hope you like our explanation.

9. Conclusion
Hence, we have studied, Matplotlib, which is the Python Libraries used for Python Plot and much more.
Furthermore, if you have any information regarding, feel free to share with us.

Python SciPy Tutorial – What is SciPy & How to Install SciPy


by DataFlair Team · September 28, 2018

1. Python SciPy Tutorial – Objective

In our previous Python Library tutorial, we saw Python Matplotlib. Today, we bring you a tutorial on Python
SciPy. Here in this SciPy Tutorial, we will learn the benefits of Linear Algebra, Working of Polynomials, and
how to install SciPy. Moreover, we will cover the Processing Signals with SciPy, and Processing Images with
SciPy.
So, let’s start the Python SciPy Tutorial.
Python SciPy Tutorial – Linear Algebra, Benefits, Special Functions

2. What is SciPy in Python?

Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Here are some shorts on it:

Author: Travis Oliphant, Pearu Peterson, Eric Jones.

First Release: 2001

Stable Release: Version 1.1.0; May, 2018.

Written in: Fortran, C, C++, Python Programming Language.

Python SciPy has modules for the following tasks:

Optimization

Linear algebra

Integration

Interpolation

Special functions

FFT

Signal and Image processing

ODE solvers
And as we’ve seen, an important feature of the NumPy module is multidimensional arrays. This is what SciPy
uses too; it will work with NumPy arrays.
Do you know about Python NumPy?

3. Python SciPy Tutorial – SciPy Subpackages

In this Python SciPy Tutorial, we will study these following sub-packages of SciPy:

cluster- Hierarchical clustering.

constants- Physical constants and factors of conversion.

fftpack- Algorithms for Discrete Fourier Transform.

integrate- Routines for numerical integration.

interpolate- Tools for interpolation.

io- Input and Output tools for data.

lib- Wrappers to external libraries.

linalg- Routines for linear algebra.

misc- Miscellaneous utilities like image reading and writing.

ndimage- Functions for processing multidimensional images.

optimize- Algorithms for optimization.

signal- Tools for processing signal.

sparse- Algorithms for sparse matrices.

spatial- KD-trees, distance functions, nearest neighbors.

special- Special functions.

stats- Functions to perform statistics.

weave- Functionality that lets you write C++/C code in the form of multiline strings.

4. Python SciPy Tutorial – Benefits of SciPy

High-level commands and classes for visualizing and manipulating data.

Powerful and interactive sessions with Python.

Classes, and web and database routines for parallel programming.

Easy and fast.


Open-source.

5. Python SciPy Tutorial – How to Install SciPy?

You can use pip to install SciPy-

pip install scipy

You can also use conda for the same-

conda install –c anaconda scipy

Then, you can import SciPy as:

>>> import scipy

You will also want to interact with numpy here. Let’s import that too.

>>> import numpy

Let’s Revise Python Operators with Syntax and Examples


Finally, in some places, we will want to plot our results. We will use matplotlib for that; let’s import it.

>>> import matplotlib

6. SciPy Tutorial – Linear Algebra

For performing operations of linear algebra in SciPy, we will need to import linalg from scipy-

>>> from scipy import linalg


SciPy Tutorial – Linear Algebra

a. Solving a set of equations


Let’s suppose this is the set of equations we want to solve-
2x+3y=7
3x+4y=10
Let’s create input and solution arrays for this.

>>> A=numpy.array([[2,3],[3,4]])

>>> B=numpy.array([[7],[10]])

>>> linalg.solve(A,B)

array([[ 2.], [ 1.]])


This tells us that both equations work for x=2 and y=1. To check the results, we run the following command-

>>> A.dot(linalg.solve(A,B))-B

array([[ 0.], [ 0.]])


The output confirms the correctness of the computation.

b. Determinants of Square Matrices


To calculate the determinant for a square matrix, we can use the det() method.

>>>mat=numpy.array([[8,2],[1,4]])

>>>linalg.det(mat)

30.0

c. An inverse of a Square Matrix


For this, we use the inv() method.

>>> linalg.inv(mat)

array([[ 0.13333333, -0.06666667],


[-0.03333333, 0.26666667]])
Do you know the Steps to Install Python on Windows

d. Singular Value Decomposition


This uses the method svd().

>>> linalg.svd(mat)
(array([[-0.9610057 , -0.27652857],
[-0.27652857, 0.9610057 ]]),
array([ 8.52079729, 3.52079729]),
array([[-0.9347217 , -0.35538056],
[-0.35538056, 0.9347217 ]]))

e. LU Decomposition
To carry out LU decomposition, we can use the lu() method.

>>> linalg.lu(mat)

(array([[ 1., 0.],


[ 0., 1.]]), array([[ 1. , 0. ],
[ 0.125, 1. ]]), array([[ 8. , 2. ],
[ 0. , 3.75]]))

7. SciPy Tutorial – Working with Polynomials in SciPy

SciPy will let us work with polynomials using the poly1d type from numpy:

>>> from numpy import poly1d

>>> p=poly1d([3,2,4])

2
3x+2x+4

>>> p*p

4 3 2
9 x + 12 x + 28 x + 16 x + 16

>>> p(5) #Value of polynomial for x=5

89

>>> p.integ(k=6) #Integration

poly1d([ 1., 1., 4., 6.])

>>> p.deriv() #Finding derivatives

poly1d([6, 2])

8. SciPy Tutorial – Integration with SciPy


To be able to perform integration, we will import the integrate subpackage from scipy.

>>> from scipy import integrate


The quad() function will let us integrate a function; let’s use a lambda for this.

>>> integrate.quad(lambda x:x**2,0,4) #Last two arguments are lower and upper limits

(21.333333333333336, 2.368475785867001e-13)
Read about Python GUI Programming | Tkinter Tutorial 2018
You can use help() function to find out what else this subpackage can do for you. Here are some more
functions:

dblquad- Double integration

tplquad- Triple integration

nquad- n-dimensional integration

fixed_quad- Use Gaussian quadrature of order n to integrate func(x)

quadrature- Use Gaussian quadrature to integrate with certain tolerance

romberg- Romberg integration

quad_explain- Find out about quad

newton_cotes- Weights and coefficient of error with Newton-Cotes integration

IntegrationWarning- Warning for issues in integration

9. SciPy Tutorial – Vectorizing Functions

The class vectorizes will let us turn a regular Python function into a vectorized function. Let’s try this out.
We’ll define a function first. If the first argument is greater than the second, it returns the sum of the two.
Otherwise, it returns the product of the two.

def addmul(a,b):

if a>b:

return a+b

else:

return a*b

Now, let’s vectorize this.

>>> numpy.vectorize(addmul)([1,4,3,9],[2,3,7,8])

array([ 2, 7, 21, 17])

10. SciPy Tutorial – Fast Fourier Transforms


If you’re familiar with calculus, then there are chances are you may be familiar with Fourier Transforms. This
analysis lets you express a function as a sum of periodic components. It is possible to recover the signal from
this sum. Let’s take an example.

>>> from scipy.fftpack import fft

>>> N=600

>>> T=1.0/800.0

>>> x=np.linspace(0.0,N*T,N)

>>> y=numpy.sin(50.0*2.0*numpy.pi*x)+0.5*numpy.sin(80.0*2.0*numpy.pi*x)

>>> yf=fft(y)

>>> xf=numpy.linspace(0.0,1.0/(2.0*T),N//2)

>>> import matplotlib.pyplot as plt

>>> plt.plot(xf, 2.0/N * numpy.abs(yf[0:N//2]))

[<matplotlib.lines.Line2D at 0x7fbb78d65210>]

>>> plt.grid()

>>> plt.show()

SciPy Tutorial – Fast Fourier Transforms

Let’s have a look at Python Function Arguments


11. SciPy Tutorial – Special Functions of SciPy

The special module holds various transcendental functions that will help us with multiple tasks in mathematics.
To import it, use the following command-

>>> from scipy import special

We make use of the various functions:

SciPy Tutorial – Special Functions of SciPy

a. ERF
This function calculates the area under a Gaussean curve. We use the erf() function.
b.Gamma
This calculates the Gamma. The function for this is gamma().
c. Gammaln
This calculates the log of Gamma. We use the function gammaln().
d.eppilj
The elliptic function we have is eppilj().
e. jn
This is the Nth order Bessel function.
Let’s Explore Python Functions with Syntax and Examples

12. SciPy Tutorial – Processing Signals with SciPy

SciPy will also help you with signal processing. Let’s take an example.
SciPy Tutorial – Processing Signals with SciPy

a. Using FFT to resample


We can resample a function to n points in a time domain interval. We can use the function resample() for this.

>>> t=numpy.linspace(-10,10,200)

>>> y=numpy.sin(t)

>>> from scipy import signal

>>> signal.resample(y,100)

array([ 0.25070765, 0.44580125, 0.12797317, 0.00253488, -0.24973473,


-0.39831149, -0.60510157, -0.72607055, -0.8701493 , -0.9336745 ,
-0.99979334, -0.98973944, -0.97222903, -0.88612051, -0.79126725,
-0.63978568, -0.48541023, -0.29029666, -0.10319111, 0.10642378,
0.29461537, 0.48697311, 0.64470867, 0.79055976, 0.89136943,
#More output here

b. Removing Linear Trends


The detrend() function will remove the linear element from the signal; this gives us a transient solution.

>>> t=numpy.linspace(-10,10,200)

>>> y=numpy.sin(t)+t

>>> signal.detrend(y)

array([ 0.76761293, 0.67843367, 0.58464135, 0.48715987, 0.38695036,


0.28500148, 0.18231944, 0.07991785, -0.02119249, -0.12001383,
-0.21557153, -0.30692387, -0.39317157, -0.47346689, -0.54702214,
-0.61311766, -0.67110907, -0.7204338 , -0.76061672, -0.79127497,
-0.81212183, -0.82296958, -0.82373141, -0.81442233, -0.79515896,
#More output here
Read more about Image Processing with SciPy and NumPy in Python

13. SciPy Tutorial – Processing Images with SciPy


With SciPy, you can use ndimage to process images. Some of the possible transitions are opening and closing
images, geometrical transformation(shape, resolution, orientation), image filtering, and filters like erosion and
dilation. You can import it as:

>>> from scipy import ndimage

Let’s look at some functions we have:

a. Shift

This function will shift the image along the x and y coordinates. The function is shift(image,(x,y)).
b. Rotate
When you want to rotate your image to an angle, use the rotate(image, angle) function.
c. Zoom
With zoom(image, magnitude), you can zoom in or out on an image.
d. Median Filter
To filter an image with a Median filter, you can use median_filter(image, argument).
e. Gaussian Filter
And to filter with a Gaussian filter, you use gaussian_filter(image, argument).
f. Opening an Image in Binary
For this, you use the function binary_opening(image)
g. Closing an Image
To close this image, make a call to binary_closing(opened_image)
h. Binary Erosion
For this task, we use the function binary_erosion(image)
i. Binary Dilation
And to perform dilation, call binary_dilation(image)

14. SciPy Tutorial – Optimizing


For algorithms that will optimize, we need the optimize package. Import it this way-

>>> from scipy import optimize

Let’s take a demo piece of code to explain this.

>>> from scipy.optimize import least_squares

>>> def func1(x):

return numpy.array([10 * (x[1] - x[0]**2), (1 - x[0])])

>>> input=numpy.array([2, 2])

>>> res=least_squares(func1, input)


>>> res

active_mask: array([ 0., 0.])


cost: 0.0
fun: array([ 0., 0.])
grad: array([ 0., 0.])
jac: array([[-20.00000015, 10. ],
[ -1. , 0. ]])
message: ‘`gtol` termination condition is satisfied.’
nfev: 4
njev: 4
optimality: 0.0
status: 1
success: True
x: array([ 1., 1.])

15. SciPy Tutorial – Working with Stats sub-packages of SciPy

The stats subpackage lets us work with statistics. Let’s take a few examples.
from scipy.stats import norm.

SciPy Tutorial – Working with Stats sub-packages of SciPy

a. CDF Removing Linear Trends


>>> norm.cdf(numpy.array([1,-1.,3,1,0,4,-6,2]))

This function computes the cumulative distribution at the points we mention.


Have a Look at Python Packages Comprehensive Guide
b. PPF
For the Percent Point Function, we use the function ppf().

>>> norm.ppf(0.5)

0.0

>>> norm.ppf(2.3)

nan

c. RVS
For a random variate sequence, use rvs().

>>> norm.rvs(size=7)

array([-1.42745064, -0.07120524, -0.88890304, 1.34584415, 1.24426647,


-1.57490259, 0.24552959])

d. Binomial Distribution
For this, we use the cdf() function from uniform.

>>> from scipy.stats import uniform

>>> uniform.cdf([0,1,2,3,4,5,6,7],loc=1,scale=3)

array([ 0. , 0. , 0.33333333, 0.66666667, 1. ,


1. , 1. , 1. ])
So, this was all Python SciPy Tutorial. Hope you like our explanation.

16. Conclusion

Hence, in this SciPy tutorial, we studied introduction to Scipy with all its benefits and Installation process. At
last, we discussed several operations used by Python SciPy like Integration, Vectorizing Functions, Fast Fourier
Transforms, Special Functions, Processing Signals, Processing Images, Optimize package in SciPy. Still, you
have a query, feel free to share with us.

Python NumPy Tutorial – NumPy ndarray & NumPy Array


by DataFlair Team · September 28, 2018

1. Python NumPy Tutorial – Objective

In our last Python Library tutorial, we studied Python SciPy. Now we are going to study Python NumPy. In
this NumPy tutorial, we are going to discuss the features, Installation and NumPy ndarray. Moreover, we will
cover the data types and array in NumPy.
So, let’s begin the Python NumPy Tutorial.
Python NumPy Tutorial – NumPy ndarray & NumPy Array

2. What is NumPy?

A library for Python, NumPy lets you work with huge, multidimensional matrices and arrays. Along with that, it
provides a gamut of high-level functions to perform mathematical operations on these structures. Here is a short
brief about it:

Author- Travis Oliphant

First Release- 1995 (Released as Numeric; Changed to NumPy in 2006)

Stable Release- June, 2018

Written in- Python Programming, C

Python NumPy is cross-platform and BSD-licensed. We often use it with packages like Matplotlib and SciPy.
This can be seen as an alternative to MATLAB. The term ‘Numpy’ is a portmanteau of the words ‘NUMerical’
and ‘PYthon’.
Do you know about Python Matplotlib

3. Numpy Tutorial – Features of Numpy

In this Python NumPy Tutorial, we are going to study the feature of NumPy:

NumPy stands on CPython, a non-optimizing bytecode interpreter.

Multidimensional arrays.

Functions and operators for these arrays.


Python Alternative to MATLAB.

ndarray- n-dimensional arrays.

Fourier transforms and shapes manipulation.

Linear algebra and random number generation.

4. Numpy Tutorial – How to Install NumPy?


You can use pip to install numpy-

pip install numpy

Then you can import it as-

>>> import numpy as np

5. Numpy Tutorial – NumPy ndarray

This is one of the most important features of numpy. ndarray is an n-dimensional array, a grid of values of the
same kind. A tuple of nonnegative integers indexes this tuple. An array’s rank is its number of dimensions.
Let’s take a few examples.

>>> a=np.array([1,2,3])

>>> type(a)

<class ‘numpy.ndarray’>

>>> a.shape

(3,)

>>> a[0],a[2]

(1, 3)

>>> a[1]=5

>>> a

array([1, 5, 3])
Do you know the Different Steps to Learn Python for Data Science
As you can see, the array’s shape is (3,). What happens when we build an array of more than one dimension?
Let’s see.

>>> b=np.array([[2,7,9],[5,1,3]])

>>> b
array([[2, 7, 9],
[5, 1, 3]])

>>> b[0,1]

>>> b.shape

(2, 3)

>>> b.size

a. How to Create NumPy Array?


The following lines of code create a few more arrays:

>>> np.arange(7) #This is like range in Python

array([0, 1, 2, 3, 4, 5, 6])

>>> np.random.random((3,3)) #Fills in random values

array([[0.56074267, 0.67303599, 0.65973007],


[0.37222497, 0.13230271, 0.40858618],
[0.74455771, 0.52119999, 0.6927821 ]])

>>> np.ones((2,3))

array([[1., 1., 1.],


[1., 1., 1.]])

>>> np.zeros((1,2))

array([[0., 0.]])

>>> np.eye(3) #Identity matrix

array([[1., 0., 0.],


[0., 1., 0.],
[0., 0., 1.]])

>>> np.full((3,2),7) #Matrix of constants

array([[7, 7],
[7, 7],
[7, 7]])

>>> np.linspace(1,2,4) #4 values spaced evenly between, and including, 1 and 2.


array([1. , 1.33333333, 1.66666667, 2. ])

>>> np.empty([2,3]) #Empty array

array([[1., 0., 3.],


[0., 4., 0.]])

b. Some Parameters
>>> np.array([1,3,4],ndmin=3) #Minimum dimension

array([[[1, 3, 4]]])

>>> np.array([1,3,4],dtype=complex) #Data type

array([1.+0.j, 3.+0.j, 4.+0.j])


Read about Serialization in Python with Example

6. Numpy Tutorial – Data Types

As we’ve said before, a NumPy array holds elements of the same kind. If while creating a NumPy array, you do
not specify the data type, NumPy will decide it for you. We have the following data types-
bool_, int_, intc, intp, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float_, float16, float32, float64,
complex_, complex64, complex128
We can confirm:

>>> np.dtype(np.int32)

dtype(‘int32’)

>>> np.dtype('i4')

dtype(‘int32’)

>>> np.dtype('i8')

dtype(‘int64’)

7. Functions of NumPy Array

Let’s take a look at all that we can do to an array and what more we can find out about it.

>>> a=np.array([[1,2,3],[4,5,6]])

>>> a.reshape(3,2)

array([[1, 2],
[3, 4],
[5, 6]])

>>> a.ndim #Number of array dimensions


2

>>> np.array([[1,2,3],[4,5,6]]).itemsize #Length of each element in bytes

>>> np.array([[1,2,3],[4,5,6]],dtype=np.int8).itemsize

>>> a

array([[1, 2, 3],
[4, 5, 6]])

>>> a.flags

C_CONTIGUOUS: True
F_CONTIGUOUS: False
OWNDATA: True
WRITEABLE: True
ALIGNED: True
WRITEBACKIFCOPY: False
UPDATEIFCOPY: False

8. Numpy Array Indexing

It is possible to slice you NumPy arrays- with multiple slices for multidimensional arrays.

>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])

>>> b=a[:2,1:3]

>>> b

array([[2, 3],
[5, 6]])

>>> a[1,2]

>>> b[0,0]=79

>>> a

array([[ 1, 79, 3],


[ 4, 5, 6],
[ 7, 8, 9]])
Read about Python Forensics – Hash Function, Virtualization
As you can see, changes to a slice modify an original.
>>> a[1,:]

array([4, 5, 6])

>>> a[1:2,:]

array([[4, 5, 6]])

>>> a[:,1]

array([79, 5, 8])

Python NumPy Tutorial – NumPy Array Index

a. Integer Indexing
It is possible to create an array from another.

>>> a=np.array([[1,2],[3,4],[5,6]])

>>> a[[0,1,2],[0,1,0]] #Prints elements at [0,0], [1,1], and [2,0]

array([1, 4, 5])
Let’s pick elements-

>>> a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

>>> b = np.array([0, 2, 0, 1])

>>> b

array([0, 2, 0, 1])

>>> a[np.arange(4), b]
array([ 1, 6, 7, 11])

>>> a[np.arange(4), b]+=10

>>> a

array([[11, 2, 3],
[ 4, 5, 16],
[17, 8, 9],
[10, 21, 12]])

b. Boolean Indexing
This will let you pick elements that satisfy a condition.

>>> a=np.array([[1,2],[3,4],[5,6]])

>>> boolean=(a>3)

>>> boolean

array([[False, False],
[False, True],
[ True, True]])

>>> a[boolean]

array([4, 5, 6])
Let’s Read about Python Zipfile – Benefits, Modules, Objects

9. Mathematical Functions on Arrays in NumPy


Let’s now look at some mathematical functions to call on arrays.

>>> a=np.array([[1,2,3],[4,5,6]])

>>> b=np.array([[7,8,9],[10,11,12]])

>>> np.add(a,b) #a+b does the same

array([[ 8, 10, 12],


[14, 16, 18]])

>>> np.subtract(a,b) #Same as a-b

array([[-6, -6, -6],


[-6, -6, -6]])

>>> np.multiply(a,b) #a*b works too


array([[ 7, 16, 27],
[40, 55, 72]])

>>> np.divide(a,b) #Same as a/b

array([[0.14285714, 0.25 , 0.33333333],


[0.4 , 0.45454545, 0.5 ]])

>>> np.sqrt(a) #Produces square root

array([[1. , 1.41421356, 1.73205081],


[2. , 2.23606798, 2.44948974]])

>>> a=np.array([[1,2],[3,4]])

>>> np.sum(a)

10

>>> np.sum(a,axis=0) #Sum of each column

array([4, 6])

>>> np.sum(a,axis=1) #Sum of each row

array([3, 7])
To transpose this matrix:

>>> a.T

array([[1, 3],
[2, 4]])

>>> np.array([1,3,2]).T #NOP

array([1, 3, 2])
Some functions that operate on a larger context-

>>> x=np.array([[1,2],[3,4]])

>>> y=np.array([[5,6],[7,8]])

>>> v=np.array([9,10])

>>> w=np.array([11,12])

>>> v.dot(w) #Same as np.dot(v,w)

219

>>> x.dot(v)
array([29, 67])

>>> x.dot(y)

array([[19, 22],
[43, 50]])
So, this was all about Python NumPy Tutorial. Hope you like our explanation.

10. Python NumPy Tutorial – Conclusion

Hence, in this Python NumPy Tutorial we studied, how to install NumPy, NumPy ndarray. In addition, we
discussed NumPy Array with its Functions and data types. This sums it up for NumPy. Got anything else to
add? Drop it in the comments below.

Python Pandas Tutorial – Learn Pandas in Python (Advance)


by DataFlair Team · September 28, 2018

1. Python Pandas Tutorial

In our last Python Library tutorial, we discussed Python Scipy. Today, we will look at Python Pandas
Tutorial. In this Pandas tutorial, we will learn the exact meaning of Pandas in Python. Moreover, we will see the
features, installation, and dataset in Pandas. Along with this, we will discuss Pandas data frames and how to
manipulate the dataset in python Pandas. Also, we will discuss Pandas examples and some terms as ranking,
series, panels.

So, let’s start the Python Pandas Tutorial.

Python Pandas Tutorial 2018 | Learn Pandas in Python


2. What is Pandas in Python?

As discussed above, you can use pandas to manipulate and analyze data. With the data structures and operations
it has to offer, you can play around with time series and numerical tables.

Python Pandas Tutorial

Let’s take a look at some bullet points about this-

Author: Wes McKinney

First Release: version 0.23.2; July, 2018

Written in: Python

Pandas is under a three-clause BSD license and is free to download, use, and distribute. Etymologically, the
term is a portmanteau of the words “panel” and “data”. What this means is that you need to supervise data sets
multiple times for one individual.
Do you know about Python Multiple Inheritance

3. Python Pandas Tutorial – Pandas Features


Here, in this Python pandas Tutorial, we are discussing some Pandas features:

Inserting and deleting columns in data structures.

Merging and joining data sets.

Reshaping and pivoting data sets.

Aligning data and dealing with missing data.

Manipulating data using integrated indexing for DataFrame objects.

Performing split-apply-combine on data sets using the group by engine.

Manipulating high-dimensional data in a data structure with a lower dimension using hierarchical axis
indexing.

Subsetting, fancy indexing, and label-based slicing data sets that are large in size.

Generating data range, converting frequency, date shifting, lagging, and other time-series functionality.
Reading from files with CSV, XLSX, TXT, among other formats.

Arranging data in an order ascending or descending.

Filtering data around a condition.

Analyzing time series.

Iterating over a data set.

With Python Pandas, it is easier to clean and wrangle with your data. Pandas Features like these make it a great
choice for data science and analysis. Using it with libraries like NumPy and Matplotlib makes it all the more
useful.
Do you know about NumPy a Python Library

4. How to Install Pandas?

Below, given are steps to install Pandas in Python:


a. Installing Pandas
To install pandas, you can use pip-
pip install pandas
b. Importing Pandas
Now let’s import this using an alias-

>>>import pandas as pd

This lets us enjoy the liberty of mentioning pandas as pd.


c. Importing a Dataset
You can use the function read_csv() to make it read a CSV file. Let’s import the furniture dataset.
Let’s discuss Python File Format

>>> furniture=pd.read_csv('furniture.csv')

>>> furniture

Python Pandas Tutorial – Importing a Dataset in Pandas

5. Python Pandas Tutorial – Dataset in Pandas

Following are the Pandas dataset, let’s discuss them in detail:


a. Column names
The following command will give us all the column names-

>>> furniture.columns
Index([‘Unnamed: 0’, ‘Product’, ‘Brand’, ‘Cost’], dtype=’object’)
We can slice it-

>>> furniture.columns[0:2]

Index([‘Unnamed: 0’, ‘Product’], dtype=’object’)


b. Data types

>>> furniture.dtypes

Unnamed: 0 int64
Product object
Brand object
Cost int64
dtype: object
Read Python namedtuple
To find out more about data types, read up on NumPy with Python. Let’s find out the data types of one column.

>>> furniture['Brand'].dtypes

dtype(‘O’)
O denotes an object.
c. Shape
To find out what shape your data set is, you can use the shape tuple-

>>> furniture.shape

(5, 4)
Number of rows-

>>> furniture.shape[0]

5
Number of columns-

>>> furniture.shape[1]

4
d. Individual rows
The head() method will give us the first 5 rows of the data set, but we can also choose to print fewer or more.

>>> furniture.head(3)

Python Pandas Tutorial – Individual rows

>> furniture.tail(2)
Python Pandas Tutorial – Individual rows

e. Unique values
We can use the unique() function when we want to see what categories in the data set are unique.
Let’s discuss Python Defaultdict

>>> furniture.index.unique()

Int64Index([0, 1, 2, 3, 4], dtype=’int64′)


And to find out how many, we make a call to nunique().

>>> furniture.index.nunique()

6. Python Pandas Tutorial – DataFrames

A DataFrame is an essential data structure with pandas. It lets us deal with data in a tabular fashion. The rows
are observations and columns are variables.
We have the following syntax for this-

pandas.DataFrame( data, index, columns, dtype, copy)

Such a data structure is-

Mutable

Variable columns

Labeled axes

Capable of performing arithmetic operations on columns and rows

a. Creating a DataFrame
Let’s see how we can create a DataFrame.

>>> df=pd.DataFrame({'company':['Amazon','Apple','Google','Facebook','Microsoft'],

'CEO':['Jeff Bezos','Tim Cook','Sundar Pichai','Mark Zuckerberg','Satya Nadella'],

'Founded':[1994,1976,1998,2004,1975]})

>>> df
Python Pandas Tutorial – Creating a DataFrame

b. Setting Indexes for a DataFrame


Now this indexes the dataframe as integers starting at 0. But we can put labels on these. Let’s see how we can
index it based on which company came first.

>>> df.index=['Third','Second','Fourth','Fifth','First']

>>> df

Python Pandas Tutorial – Setting Indexes for a DataFrame

c. Indexing a DataFrame
A column-
Let’s learn about python collections

>>> df['company']

Third Amazon
Second Apple
Fourth Google
Fifth Facebook
First Microsoft
Name: company, dtype: object
This prints out a Series. Now to print out a DataFrame, we can:

>>> df[['company']]

company
Third Amazon
Second Apple
Fourth Google
Fifth Facebook
First Microsoft

>>> df[['company','Founded']]
Indexing a DataFrame

d. Slicing a DataFrame
It is possible to slice a DataFrame to retrieve rows from it.

>>> df[0:3]

Python Pandas Tutorial – Slicing a DataFrame

e. More data selection operations


Using loc and iloc, you can select certain rows in a data set. loc uses string indices; iloc uses integers.

>>> df.loc[['Second','Fifth']]

Python Pandas Tutorial – More data selection operations

>>> df.iloc[3]

Pandas Tutorial

Getting more than one column-

>>> df.iloc[:,1:4]
Python Pandas Tutorial

7. Pandas Tutorial – Manipulating the Datasets

So far, we’ve seen how we can find out more about a dataset (and also, how to set indexes to it, okay). Now
let’s see what we can do to it.
Let’s explore Python Jobs
a. Changing the data type
Let’s use the furniture dataset for this.

>>> furniture.Cost=furniture.Cost.astype(float)

>>> furniture

Python Pandas Tutorial – Changing the data type

b. Creating a frequency distribution


For this purpose, we have the method value_counts().

>>> furniture.index=['A','B','A','A','C']

>>> furniture.index.value_counts(ascending=True)

C 1
B 1
A 3
dtype: int64
c. Creating a crosstab
A crosstab creates a bivariate frequency distribution.
Learn more about Python read & write File

>>> pd.crosstab(furniture.index,furniture.Brand)
Python Pandas Tutorial – Creating a crosstab

d. Choosing one column as index


You can choose one of the columns in your dataset to index others.

>>> df.set_index('company',inplace=True)

>>> df

Pandas Tutorial -Choosing one column as index

To reset this, you can:

>>> df.reset_index(inplace=True)

>>> df

Pandas Tutorial – Choosing one column as index

e. Sorting data
For this, we use the function sort_values().

>>> furniture.sort_values('Cost',ascending=False)
Python Pandas Tutorial – Sorting data

f. Renaming variables
Let’s rename the variable ‘company’ to ‘Company’.

>>> df.columns=['Company','CEO','Founded']

>>> df

Python Pandas Tutorial – Renaming variables

Or we can:
Do you know about Python Data Science

>>> furniture.rename(columns={'Product':'Category'},inplace=True)

>>> furniture

Renaming variables

g. Dropping rows and columns


It is possible to drop any number of rows and columns you want.

>>> furniture.drop('Cost',axis=1)

Dropping rows and columns

h. Creating new variables


Now, let’s add 10% of the cost to itself and find out the gross amount.

>>> furniture['Gross']=furniture.eval('Cost+(Cost*(0.1))')

>>> furniture
Creating New Variables

8. Pandas Tutorial – Describing a Dataset

Here, with the describe() method, we can find out information about a dataset- min, max, mean, count, and
more.

>>> furniture.describe()

Pandas Tutorial – Describing a Dataset

>>> furniture.Gross.max()

55000.0

9. Pandas Tutorial – groupby Function

Generally, this operation lets you group data on a variable.

>>> furniture.groupby('Category').Gross.min()

Pandas Tutorial – groupby Function


agg() lets us find out different values like count and min.
Have a look at Python Modules vs packages

>>> furniture.groupby('Category').Gross.agg(['count','min','max','mean'])

Group by function in pandas

10. Python Pandas Tutorial – Filtering

Now, you can perform filtering in two ways-

>>> furniture[furniture.index==2]

Python Pandas – Filtering

>>> furniture.loc[furniture.index==2,:]

And then of course, you can group conditions. Or:

>>> furniture[furniture.index.isin([1,3])]

Filtering in Groupby

11. Missing Values in Pandas

Basically, isnull() will tell her if a column misses a value or more.

>>> furniture.isnull()
Missing Values in Pandas

Similarly, notnull() returns False for an NaN.


Number of missing values-

>>> furniture.isnull().sum()

Missing Values in Pandas

To drop a missing value, you can use dropna(), and to fill it, use fillna().
Learn about python Regular expressions

12. Python Pandas Tutorial – Ranking

Now, to rank every variable according to its value, we can use rank().

>>> furniture.rank()

Python Pandas – Ranking

13. Python Pandas Tutorial – Concatenating DataFrames

So, with the concat() method, we can concatenate two or more DataFrames.

>>> pd.concat([df,furniture])
Python Pandas – Concatenating DataFrames

Let’s see what happens when we concatenate this with df.

>>> pd.concat([df,furniture,df])

Concatenating DataFrames in Pandas

14. Python Pandas Tutorial – Series

Now, another important data structure in pandas is a Series. This is a one-dimensional array; it is labeled and
can hold more than one kind of data.

>>> pd.Series([2,4,'c'])

0 2
1 4
2 c
dtype: object

>>> pd.Series({1:'a',2:'b'})
1 a
2 b
dtype: object
Read python packages

15. Python Pandas Tutorial – Panels


Finally, we come to panels. A panel holds data in 3 dimensions. As we said above, the term ‘pandas’ comes as a
portmanteau of words “panel” and “data”. Declaration for a panel takes in three parameters- items, major_axis,
and minor_axis.

>>> import numpy as np

>>> pd.Panel(np.random.rand(2,4,5))

<class 'pandas.core.panel.Panel'>

Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)


Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
So, this was all in Python pandas Tutorial. Hope you like our explanation.

16. Conclusion

Hence, in this Python Pandas Tutorial, we learn Pandas in Python. Moreover, we discussed Pandas example,
features, installation, and data sets. Also, we saw Data frames and the manipulation of data sets. Still, if any
doubt regarding Pandas in Python, ask in the comment tab.

Python PyQt5 Tutorial – Example and Applications


by DataFlair Team · March 1, 2019

1. PyQt5 Tutorial
Today, we will explore Python PyQt5 Tutorial. In this PyQt5 tutorial of Python, we will learn a Python binding
of Qt, which is a cross-platform GUI toolkit. Moreover, we will see how to plot various GUI elements like
buttons, windows, and input dialogs. We will talk about version PyQt5, which isn’t backwards-compatible with
version PyQt4. Also, we will look at PyQt Applications.
Python PyQt5 Tutorial – Example and Applications

Do you know about Python Iterators

2.What is PyQt5?
PyQt is a Python binding of Qt, a cross-platform GUI toolkit. This is a free software by Riverbank Computing
and implements over 440 classes and more than 6000 functions and methods. Some of these are-

PyQt5 Tutorial – What is PyQt5

A set of GUI widgets.

An XML parser.

QScintilla, a rich-text editor widget based on Scintilla.

SVG support.

Data-aware widgets.
Classes to access SQL databases.

Classes to embed ActiveX controls on Windows.

Learn about Python Built-in Functions

a. Modules in PyQt5
We have the following modules available in PyQt5:

QtQml

QtQtuick

QtCore

QtGui

QtPrintSupport

GUI toolkitQtWidgets

QGLContext

QGLFormat

QGLWidget

QtWidget

QtWebKit

QtWebKitWidgets

b. PyQt5 Installation
For this, you need to install PyQt5. You can use pip for this-

pip install pyqt5

To import it in the IDLE, you can do the following-

>>> import PyQt5

3.How to Create a Window in Python PyQt5?


Let’s take a simple example of PyQt5 in Python to create an empty window on our screen.
Let’s discuss Python Comment
>>> import sys

>>> from PyQt5.QtWidgets import QApplication, QWidget

>>> app=QApplication(sys.argv)

>>> root=QWidget()

>>> root.resize(320,240)

>>> root.setWindowTitle('Hello, world!')

>>> root.show()

Creating a Window in Python

To enter the mainloop for the application, we do the following-

sys.exit(app.exec_())

Another way to create a window will be:

>>> import sys

>>> from PyQt5.QtWidgets import QApplication, QWidget

>>> from PyQt5.QtGui import QIcon

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'
self.left=10

self.top=10

self.width=640

self.height=480

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

sys.exit(app.exec_())

Creating a Window in Python

4.Python PyQt5 – Adding a Status Bar


It is possible to add a status bar to our window.

>>> import sys


>>> from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow #Imported one more module

>>> from PyQt5.QtGui import QIcon

>>> class App(QMainWindow):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=640

self.height=480

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

self.statusBar().showMessage('In progress') #Added

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

We add this to the main window and the showMessage() method prints it out to it.
Have a look at Python Directory
Adding a Status Bar in Python

5.PyQt5 Tutorial – Adding Buttons


Using the QPushButton class, we can add buttons to our screen.

>>> from PyQt5.QtWidgets import QApplication,QWidget,QPushButton

>>> from PyQt5.QtCore import pyqtSlot

>>> import sys

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=640

self.height=480
self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

button=QPushButton('Click me',self)

button.setToolTip('Thank you for thinking about me')

button.move(100,70)

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

Adding Buttons

6. PyQt5 Tutorial – Signals and Slots


On an event- like a user click- a PyQt5 widget can emit a signal. For a button click (a signal), we can specify an
action (a slot).
>>> from PyQt5.QtWidgets import *

>>> import sys

>>> class Dialog(QDialog):

def slot_method(self):

print("Calling the slot")

def init (self):

super(Dialog,self). init ()

button=QPushButton("Click me")

button.clicked.connect(self.slot_method)

mainLayout=QVBoxLayout()

mainLayout.addWidget(button)

self.setLayout(mainLayout)

self.setWindowTitle("Hello, world!")

>>> if name ==' main ':

app=QApplication(sys.argv)

dialog=Dialog()

>>> dialog.exec_()

Calling the slot

Signals and Slots

7. PyQt5 Tutorial – Message Boxes


Let’s ask the user if he likes cookies.
Do you know about Python Multiple inheritance

>>> from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QMessageBox

>>> import sys

>>> from PyQt5.QtCore import pyqtSlot

>>> from PyQt5.QtGui import QIcon

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=400

self.height=250

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

buttonReply=QMessageBox.question(self, 'Hello', "Do you like cookies?", QMessageBox.Yes |


QMessageBox.No, QMessageBox.No)

if buttonReply==QMessageBox.Yes:

print("Yeah")

else: print("Nah")

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()
Message Boxes

When we click yes, it prints Yeah, closes this message box and then opens a new window.

Message Boxes in Python

8.PyQt5 Tutorial – Adding a Textbox


For this, we use the methods setText() and text(); the widget is QLineEdit.

>>> from PyQt5.QtWidgets import


QMainWindow,QApplication,QWidget,QPushButton,QAction,QLineEdit,QMessageBox

>>> import sys

>>> from PyQt5.QtGui import QIcon

>>> from PyQt5.QtCore import pyqtSlot

>>> class App(QMainWindow):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=400

self.height=140

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

self.textbox=QLineEdit(self)
self.textbox.move(30,30)

self.textbox.resize(280,40)

self.button=QPushButton('Click me',self)

self.button.move(15,85)

self.button.clicked.connect(self.on_click)

self.show()

@pyqtSlot()

def on_click(self):

textboxValue=self.textbox.text()

QMessageBox.question(self, 'Hello, world!', "Confirm: "+textboxValue, QMessageBox.Ok,


QMessageBox.Ok)

self.textbox.setText("...")

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

Adding a Textbox

Adding a Textbox
You must learn Python OrderDict

9. PyQt5 Tutorial – Creating a Menu


Now let’s create a menu with headings

>>> import sys

>>> from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton,


QAction

>>> from PyQt5.QtGui import QIcon

>>> from PyQt5.QtCore import pyqtSlot

>>> class App(QMainWindow):

def init (self):

super(). init ()

self.title = 'Hello, world!’'

self.left = 10

self.top = 10

self.width = 640

self.height = 400

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

mainMenu=self.menuBar()

fileMenu=mainMenu.addMenu('File')

editMenu=mainMenu.addMenu('Edit')

viewMenu=mainMenu.addMenu('View')

searchMenu=mainMenu.addMenu('Search')

toolsMenu=mainMenu.addMenu('Tools')

helpMenu=mainMenu.addMenu('Help')

exitButton=QAction(QIcon('exit24.png'), 'Exit', self)


exitButton.setShortcut('Ctrl+Q')

exitButton.setStatusTip('Exit application')

exitButton.triggered.connect(self.close)

fileMenu.addAction(exitButton)

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

Creating a Menu

10. PyQt5 Tutorial – Setting Absolute Position


You can put your widgets anywhere you want on the screen.
Let’s discuss Python Collections module

>>> import sys

>>> from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel

>>> from PyQt5.QtGui import QIcon

>>> class App(QMainWindow):

def init (self):


super(). init ()

self.title = 'Hello, world!'

self.left = 10

self.top = 10

self.width = 440

self.height = 280

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left, self.top, self.width, self.height)

label = QLabel('Pink',self)

label.move(50,50)

label2 = QLabel('Blue',self)

label2.move(100,100)

label3 = QLabel('Green',self)

label3.move(150,150)

label4 = QLabel('Lavender',self)

label4.move(200,200)

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()
Setting Absolute Position

11. PyQt5 Tutorial – Creating Input Dialogs


It is possible to add an input dialog.

>>> import sys

>>> from PyQt5.QtWidgets import QApplication,QWidget,QInputDialog,QLineEdit

>>> from PyQt5.QtGui import QIcon

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=300

self.height=250

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)
self.getDouble()

self.show()

def getDouble(self):

d,okPressed=QInputDialog.getDouble(self,'Get double','Value',9,0.5,100,9.5)

if okPressed:

print(d)

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

Creating Input Dialogs

12.PyQt5 Tutorial – Loading Images


To load an image into your window, you can try the following-
Let’s revise Python Closure

>>> import sys

>>> from PyQt5.QtWidgets import QApplication,QWidget,QLabel

>>> from PyQt5.QtGui import QIcon,QPixmap

>>> import os

>>> os.chdir('C:\\Users\\Ram\\Desktop')

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10
self.width=640

self.height=480

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

label=QLabel(self)

pixmap=QPixmap('df.png')

label.setPixmap(pixmap)

self.resize(pixmap.width(),pixmap.height())

self.show()

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

Loading Images

13.PyQt5 Tutorial – Color Dialogs


A color dialog is one that will let you choose a color from a color picker.

>>> import sys

>>> from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QColorDialog

>>> from PyQt5.QtGui import QIcon

>>> from PyQt5.QtCore import pyqtSlot

>>> from PyQt5.QtGui import QColor

>>> class App(QWidget):


def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=320

self.height=200

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

button=QPushButton('Open color dialog', self)

button.setToolTip('This opens the color dialog')

button.move(10,10)

button.clicked.connect(self.on_click)

self.show()

@pyqtSlot()

def on_click(self):

openColorDialog(self)

>>> def openColorDialog(self):

color=QColorDialog.getColor()

if color.isValid():

print(color.name())

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()
Color Dialogs

Color Dialogs

And then when we pick a color and click on ‘OK’, it gives us the HTML code of that color.

Color Dialogs

14.PyQt5 Tutorial – Font Dialogs


Now to choose a font-
Know more about Python File I/O

>>> import sys

>>> from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFontDialog

>>> from PyQt5.QtGui import QIcon

>>> from PyQt5.QtCore import pyqtSlot

>>> class App(QWidget):

def init (self):

super(). init ()

self.title='Hello, world!'

self.left=10

self.top=10

self.width=320

self.height=200

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left,self.top,self.width,self.height)

button=QPushButton('The font dialog', self)

button.setToolTip('Opens the font dialog')

button.move(50,50)

button.clicked.connect(self.on_click)

self.show()

@pyqtSlot()

def on_click(self):

print('Click')

openFontDialog(self)

>>> def openFontDialog(self):

font,ok=QFontDialog.getFont()
if ok:

print(font.toString())

>>> if name ==' main ':

app=QApplication(sys.argv)

ex=App()

PyQt5 Tutorial – Font Dialogs

Font Dialogs
Font Dialogs

15.Python PyQt Applications


Here’s a list of applications that make use of PyQt-

Dropbox- File-hosting service.

Spyder- Python IDE.

Calibre- e-book management application.

Leo- Outliner and literate programming editor.

OpenShot- Video-editing program.

Veusz- Scientific plotting application.

Orange- Data-mining and visualization framework.

Kodos- Regular-expression debugger.

Eric Python IDE.

Anki- Spaced repetition flashcard program.

Ninja-IDE- Extensible open-source Python IDE.

qt-recordMyDesktop- Qt4 frontend for recordMyDesktop.

Let’s learn about Python Libraries


So, this was all about PyQt5 Tutorial. Hope you like our explanation.

16.Conclusion
Hence, in this Python PyQt5 Tutorial, we saw how to create GUI elements with PyQt5. Once you get the
syntax, it seems easy to work with PyQt5. Tell us what you think in the comments below.

Python Array Module – How to Create and Import Array in Python


by DataFlair Team · September 28, 2018

1. Python Array Module – Objective


Today in this Python Array Tutorial, we will learn about arrays in Python Programming. Here, we will discuss
how Python array import module and how can we create Array. Along with this, we will cover the Python
Array Class Modules and Data Items.

Python Array Module – Data Items & Modules

2. What is a Python Array Module?

Python array module gives us an object type that we can use to denote an array. This is a collection of a type of
values. In a way, this is like a Python list, but we specify a type at the time of creation.
Do you Know about Python Tuples
Here’s a list of such type codes-

Type Code C Type Python Type Minimum size (bytes)

b signed char int 1

B unsigned char int 1

Unicode character;
u Py_UNICODE 2
deprecated since Python 3.3

h signed short int 2

H unsigned short int 2

i signed int int 2

I unsigned int int 2

l signed long int 4

L unsigned long int 4


q signed long long int 8

Q unsigned long long int 8

f float float 4

d double float 8

3. How to Import Python Array Module?

If a Python array is installed on your machine, you can import it as:

>>> import array

4. How to Create a Python Array?

You can create an array using the following piece of code-

class array.array(typecode[,initializer])

This creates a new array with items of the type specified by the type code. You can optionally provide an
initializer value- a list.
Read about Python Strings with Functions & Operations
Let’s try creating an array in Python.

>>> arr=array.array('i',[1,3,4])

>>> arr

array(‘i’, [1, 3, 4])

>>> array.array('u', 'hello \u2641')

array(‘u’, ‘hello ♁‘)

5. Python Array Class – Data Items

The class array has the following data items-


Python Arrays Class – Data Items

a. array.typecodes
This is a string with all available type codes- the ones in the table above.

>>> array.typecodes

‘bBuhHiIlLqQfd’

b. array.typecode
This gives us the type code character we used when creating the array in Python.

>>> arr.typecode

‘i’

c. array.itemsize
This returns the number of bytes one item from the Python array takes internally.

>>> arr.itemsize

4
Let’s know about Python Web Framework – A Detailed List with Explanations

6. Python Array Class – Methods

Now, which methods does Array Class support? Here you go:
Python Array Class – Methods

a. array.append(x)
This appends the item x to the array.

>>> arr.append(2)

>>> arr

array(‘i’, [1, 3, 4, 2])

b. array.buffer_info()
This returns a tuple that holds the address in memory and the length of elements in the buffer that holds the
contents of the array.

>>> arr.buffer_info()

(43560864, 4)

c. array.byteswap()
This performs an operation of bytes wap on an array.

>>> arr.byteswap()

>>> arr
array(‘i’, [16777216, 50331648, 67108864, 33554432])

d. array.count(x)
Let’s find out how many 3s there are in our Python array.

>>> arr=array.array('i',[1,3,2,4,3,5])

>>> arr.count(3)

e. array.extend(iterable)
This attaches the iterable to the end of the array in Python.

>>> arr.extend([7,9,8])

>>> arr

array(‘i’, [1, 3, 2, 4, 3, 5, 7, 9, 8])


But if you add another array, make sure it is the same type. The following code throws an error.

>>> arr.extend(array.array('u',['H','e','l','l','o']))

Traceback (most recent call last):


File “<pyshell#19>”, line 1, in <module>
arr.extend(array.array(‘u’,[‘H’,’e’,’l’,’l’,’o’]))
TypeError: can only extend with array of same kind

f. array.fromlist(list)
This appends item from a list to the Python arrays.

>>> arr.fromlist([9,0])

>>> arr

array(‘i’, [1, 3, 2, 4, 3, 5, 7, 9, 8, 9, 0])

g. array.fromunicode(s)
This appends the Unicode string to the one we call it on- this should be Unicode too.

>>> unicodearr=array.array('u','Hello')

>>> unicodearr

array(‘u’, ‘Hello’)
>>> unicodearr.fromunicode(' world')

>>> unicodearr

array(‘u’, ‘Hello world’)


Follow this link to know more about Python Itertools

h. array.index(x)
This returns the index for the first occurrence of x in the Python array.

>>> arr=array.array('i',[1,3,2,4,3,5])

>>> arr.index(3)

i. array.insert(I,x)
>>> arr.insert(2,7)

>>> arr

array(‘i’, [1, 3, 7, 2, 4, 3, 5])


This inserts the element 7 at index 2.

j. array.pop(i)
This lets us drop the element at the position i.

>>> arr.pop(2)

k. array.remove(x)
This will let you remove the first occurrence of an element from the Python array.

>>> arr.remove(3)

>>> arr

array(‘i’, [1, 2, 4, 3, 5])

l. array.reverse()
This reverses the Python array.

>>> arr.reverse()
>>> arr

array(‘i’, [5, 3, 4, 2, 1])

m. array.tobytes()
This returns a representation in bytes of the values of the array in Python. This is the same as array.tostring(),
which is deprecated.

>>> arr.tobytes()

b’\x05\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00′

n. array.tolist()
This converts the array into a list.

>>> arr.tolist()

[5, 3, 4, 2, 1]
Do you know the difference between Python Tuples and Lists

o. array.tounicode()
This converts an array to a Unicode string. You need a Unicode array for this.

>>> unicodearr.tounicode()

‘Hello world’

7. Python Array – More Information

Python Arrays are space-efficient collections of numeric values that are uniformly-typed. You can:
Python Arrays Module – More Information

a. How to Index an Array in Python?


>>> arr

array(‘i’, [5, 3, 4, 2, 1])

>>> arr[1]

b. Slice an array
>>> arr[1:4]

array(‘i’, [3, 4, 2])

c. Concatenate two arrays in Python


>>> arr+arr

array(‘i’, [5, 3, 4, 2, 1, 5, 3, 4, 2, 1])

d. Multiply an array by a constant


>>> arr*2
array(‘i’, [5, 3, 4, 2, 1, 5, 3, 4, 2, 1])
So, this was all about Python Arrays Tutorial. Hope you like our explanation

8. Conclusion
Hence, while Python does not have arrays as a primary data structure, it does provide a module to let us work
with arrays. Tell us what you think in the comments below.

Python Database Access – Python 3 MySQL


by DataFlair Team · January 11, 2019

1. Python Database Programming

In this Python Database tutorial, we will see how to perform Python database access. We also will see how to
create a database in Python 3 and perform operations like insert, read, update, delete, commit, rollback, and
disconnect. We will also see how to deal with errors in Python Database Access. Moreover, we will discuss
the PyMySQL and Installation and the benefits of Python database access.
So, let’s begin Python Database Access.

Python Database Access – Python 3 MySQL

2. Python Database Access

A database is a collection of tables related to each other via columns. For most real-world projects, a database is
a must. We can use SQL (Structured Query Language) to create, access, and manipulate data. We can also make
use of normalization to avoid redundancy of data.
Do you know How Python Copy a File – 9 Simple & Quick Ways
For database programming, Python supports many database servers-
MySQL, Oracle, PostgreSQL, SQLite, Sybase, Microsoft SQL Server, mSQL, Microsoft Access, and many
more. It also supports Data Query Statements, Data Definition Language (DDL), and Data Manipulation
Language (DML). The standard database interface for Python is Python DB-API. For that, we have the module
MySQLdb for MySQL. This is independent of database engines; so we can write Python scripts to access any
database engine. However, this isn’t compatible with Python 3.
So, in this Python Database Access tutorial, we use the module PyMySQL.

3. Advantages of Database Programming with Python

With Python, we have the following benefits:

Platform-independent

Faster and more efficient

Portable

Support for relational database systems

Easy to migrate and port database application interfaces

Support for SQL cursors

It handles open and closed connections

4. PyMySQL and Installation

PyMySQL implements the Python Database API 2.0. In this Python Database tutorial, we will use it to connect
to a MySQL database server from Python. We have the following requirements to install PyMySQL-
a. Python (any of)

CPython>=2.6 or >=3.3

PyPy>=4.0

IronPython 2.7

Follow this link to know How Python Rename File – Single & Multiple Files
b. MySQL(any of)

MySQL>=4.1

MariaDB>=5.1

To install it, run the following command in the command prompt-

C:\Users\lifei>pip install PyMySQL

Collecting PyMySQL

Using cached
https://files.pythonhosted.org/packages/2f/be/4310bb405eb83b615cf9bd4501942d9ff000d8b9372ce84e920facb
f5c36/PyMySQL-0.9.0-py2.py3-none-any.whl
Collecting cryptography (from PyMySQL)
Downloading
https://files.pythonhosted.org/packages/67/62/67faef32908026e816a74b4b97491f8b9ff393d2951820573599c10
5cc32/cryptography-2.2.2-cp36-cp36m-win_amd64.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 596kB/s
Collecting idna>=2.1 (from cryptography->PyMySQL)
Downloading
https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b8
76165/idna-2.7-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 1.3MB/s
Collecting asn1crypto>=0.21.0 (from cryptography->PyMySQL)
Using cached
https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd
06222/asn1crypto-0.24.0-py2.py3-none-any.whl
Collecting six>=1.4.1 (from cryptography->PyMySQL)
Let’s Read Python Zipfile – Benefits, Modules, Objects in Zipfiles in Python
Using cached
https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe305
6e86a/six-1.11.0-py2.py3-none-any.whl
Collecting cffi>=1.7; platform_python_implementation != “PyPy” (from cryptography->PyMySQL)
Downloading
https://files.pythonhosted.org/packages/2f/85/a9184548ad4261916d08a50d9e272bf6f93c54f3735878fbfc9335ef
d94b/cffi-1.11.5-cp36-cp36m-win_amd64.whl (166kB)
100% |████████████████████████████████| 174kB 568kB/s
Collecting pycparser (from cffi>=1.7; platform_python_implementation != “PyPy”->cryptography-
>PyMySQL)
Using cached
https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09d
f6ed/pycparser-2.18.tar.gz
Installing collected packages: idna, asn1crypto, six, pycparser, cffi, cryptography, PyMySQL
Running setup.py install for pycparser … done
Successfully installed PyMySQL-0.9.0 asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.2.2 idna-2.7 pycparser-
2.18 six-1.11.0
Also, make sure to install a database server on your machine. In this tutorial, we use MySQL. We download it
from here-
dev.mysql.com/downloads/mysql

5. Connecting Python Database

Now that you’ve installed everything, let’s begin connecting to the database. Let’s create a database first.

a. How to Create Python Database?


mysql> create database demo;
Query OK, 1 row affected (0.21 sec)
mysql> use demo;
Database changed
mysql> create user ‘ayushi’@’localhost’ IDENTIFIED BY ‘yourpassword’
-> ;
Query OK, 0 rows affected (0.21 sec)
mysql> grant all on demo.* to ‘ayushi’@’localhost’;
Query OK, 0 rows affected (0.22 sec)
mysql> create table student(fname varchar(20), lname varchar(20), age int, enrolment_no varchar(12));
Query OK, 0 rows affected (0.62 sec)

b. How to Connect Python Database?


>>> import pymysql

>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo") #This saves a connection object


into db

>>> cursor=db.cursor()

>>> cursor.execute("SELECT VERSION()")

>>> print(f"You're running version {cursor.fetchone()}")

You’re running version (‘8.0.11’,)

>>> db.close() #Closing the database connection

A cursor is an object that submits different SQL statements to the database server. A cursor returns a result set
object.
Let’s Know Python Tools – 4 Major Utilities of Python

6. How to Create Tables in Python Database?

Now let’s take a look at all operations one by one, starting with creating a table.

>>> import pymysql

>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")

caching sha2: succeeded by fast path.

>>> cursor=db.cursor()

>>> cursor.execute("DROP TABLE IF EXISTS student") #This drops the table and replaces it

>>> query="""CREATE TABLE

student( fname VARCHAR(20), lname

VARCHAR(20),

age INT, enrolment_no VARCHAR(12))"""

>>> cursor.execute(query)

>>> db.close()
7. How to Insert a Record in Python Database?
Let’s try inserting a record in ‘student’.

>>> import pymysql

>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")

caching sha2: succeeded by fast path.

>>> cursor=db.cursor()

>>> query='INSERT INTO student VALUES("Ayushi","Sharma",22,"0812CS141028")'

>>> try:

cursor.execute(query)

db.commit() #Commit writing to the database

except:

db.rollback() #Rollback the transaction if not complete

>>> db.close()

Let’s check if this makes any changes to the database. In the command prompt:

mysql> select * from student;

+--------+--------+------+--------------+

| fname | lname | age | enrolment_no |

+--------+--------+------+--------------+

| Ayushi | Sharma | 22 | 0812CS141028 |

+--------+--------+------+--------------+

1 row in set (0.00 sec)

8. How to Read Records in Python Database?

Now how can we fetch values from a database? Let’s take an example to fetch records of students from
‘student’ that are older than 22. We have added another record for this purpose.

>>> import pymysql

>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")
caching sha2: succeeded by fast path.

>>> cursor=db.cursor()

>>> query="select * from student where age>22"

>>> try:

cursor.execute(query)

resultset=cursor.fetchall() #To fetch all records that satisfy

for record in resultset:

fname=record[0]

lname=record[1]

age=record[2]

enrolment_no=record[3]

print(f"Student: {fname} {lname}; Enrolment: {enrolment_no}; Age: {age}")

except:

print("Sorry, we encountered a problem")

Student: Megha Sharma; Enrolment: 0812CS141015; Age: 24

>>> db.close()

We have the following methods and attributes-

fetchone()– This fetches the immediate next row from the result set of the query.

fetchall()– This fetches the entire result set; it will exclude the records already extracted.

rowcount– This is an attribute. It returns an integer denoting the number of records that a call to execute()
affected.

Have a look at XML Processing in Python 3 | XML Parser

9. How to Update Records in Python Database?

To update an existing record, we can simply use an SQL query for the same.

>>> import pymysql


>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")

caching sha2: succeeded by fast path.

>>> cursor=db.cursor()

>>> query="update student set age=age+1 where age<=22"

>>> try:

cursor.execute(query)

db.commit()

except:

db.rollback()

>>> db.close()

Let’s see if this has made any changes to the actual database. In your command prompt:

mysql> select * from student;

+--------+--------+------+--------------+

| fname | lname | age | enrolment_no |

+--------+--------+------+--------------+

| Ayushi | Sharma | 23 | 0812CS141028 |

| Megha | Sharma | 24 | 0812CS141015 |

+--------+--------+------+--------------+

2 rows in set (0.00 sec)

10. How to Delete Records in Python Database?

We can also delete records from a database using Python.

>>> import pymysql

>>> db=pymysql.connect("localhost","ayushi","swaysway7!","demo")

caching sha2: succeeded by fast path.

>>> cursor=db.cursor()
>>> query="delete from student where age>23"

>>> try:

cursor.execute(query)

db.commit()

except:

db.rollback()

>>> db.close()

And in the command prompt:

mysql> select * from student;

+--------+--------+------+--------------+

| fname | lname | age | enrolment_no |

+--------+--------+------+--------------+

| Ayushi | Sharma | 23 | 0812CS141028 |

+--------+--------+------+--------------+

1 row in set (0.00 sec)

11. Commit, Rollback, and Disconnecting


A commit command tells the database to finalize the write to the database. A rollback lets us revert changes and
get back to a previous state. For committing, you can use commit(), and for rollback, you can use rollback().
Let’s discuss Python Network Programming | Python Socket Programming
After we’re done working with the database, we should close the database to release resources. We use close()
for this. If you don’t get any of this, we suggest reading up on the basic properties of transactions in databases.

12. Errors in Transactions


When holding a transaction, you may come across ten different kinds of errors:
Python Database Access – Python 3 MySQL

a. Error
This is the base class for errors and a subclass to StandardError.

b. InterfaceError
This is a subclass to Error and Python uses it for errors relating to the module for database access.

c. DatabaseError
This is a subclass to Error and Python uses it for database errors.

d. OperationalError
This is a subclass of DatabaseError. When Python loses connection to a database, it throws this error.
This may happen when we haven’t selected a database.
Let’s Explore Python Web Framework – A Detailed List with Explanations

e. DataError
This is a subclass of DatabaseError. Python uses this when there is an error in the data.

f. InternalError
This is a subclass of DatabaseError. Python uses this for errors internal to the module we use for the database
access.
g. IntegrityError
Also a subclass of DatabaseError. Python uses this for cases where there can be damage to relational integrity.
This may happen when you try to enter duplicate records in the database.

h. ProgrammingError
This is a subclass of DatabaseError. Errors like bad table names cause this.
This may happen when we try to create a duplicate database.

i. NotSupportedError
A subclass of DatabaseError. When we attempt to call functionality that it doesn’t support, Python raises this
error.

j. Warning
This is a subclass of StandardError. Python uses this for non-fatal issues.
So, this was all about Python Database Access. Hope you like our explanation.

13. Conclusion

Hence, now you know how to access a database using Python with MySQL. In addition, we saw how to
perform Python database access and how to create a database in Python 3 and also perform operations like
insert, read, update, delete, commit, rollback, and disconnect. At last, we cover how to deal with errors in
Python Database Access and PyMySQL and Installation and the benefits of Python database access. That’s all
for today. Drop any queries in the comments below.

Python 3 Extension Programming with C & Others Languages


by DataFlair Team · September 17, 2018

1. Objective

In our previous Python tutorial, we have studied Python Database Access. Here, we will see Python 3
Extension Programming with C & Others Languages. Moreover, we will study the structure of Python
Extension Module and setup.py Script. Along with this, we will discuss how to import Python 3 Extension
Programming and how to write extensions for Python.
So, let’s begin Python 3 Extension Programming.
Python 3 Extension Programming with C & Others Languages

2. Python 3 Extension Programming

Python 3 extension Programming, is any code that we write in any other language like C, C++, or Java
Programming Language. We can import or integrate it into a Python script. So, this tutorial is essentially one
on how to write and import extensions for Python. Extensions let Python communicate with other languages.
The .dll files (dynamically linked libraries) you may see on your Windows or the. So, files you notice on your
Unix are libraries. In effect, extension modules are libraries.
Read about Python Collections Module in detail

3. Structure of Python 3 Extension Module

A Python 3 extension Programming module will have the following parts:

Header file- python.h

An initialization function.

Functions in other languages.

A table to map the names of the functions.

Let’s look at each of this one by one.


Python 3 Extension Programming with C & Others Languages

a. Header File- python.h


Let’s write Python 3 extension for C. So, we must include this header file in our C source file. We put this
include before all others. We also succeed this with the Python functions we want to call. This header file lets
us access the internal Python API.
In the Python header, all types and functions begin with the prefix ‘Py’/’PY’. For parsing data between Python
and C, we have the Python object pointer. This is the PyObject. Here’s an example:
static PyObject* myFunc(PyObject* self)
This header file also has some other functions:

PyArg_ParseTuple(args, format, …)- This gets arguments from Python.

Py_BuildValue(format, …)- This turns values into PyObject pointers.

PyModule_Create(moduleDef)- This initializes the module; wraps method pointers using module
definitions.

Follow this link to know Python Built-In Functions with Syntax and Examples
For functions that return nothing, we use the value Py_None.
The PyMethodDef contains the binding information. This structure ends with terminating NULL and 0 values.

b. Initialization Function
When the interpreter loads your extension module, it calls this function. This is the last part of the Python 3
extension. You should name this as following- if you call your module ‘Sound’, then name this function
‘initSound’. It will look like this:

PyMODINIT_FUNC initModule() {
Py_InitModule3(func, module_methods, docstring);

Here, we have three parameters-

func- The function to export

module_methods- Mapping table

docstring- Comment

c. C Functions to Call
We can use one of three forms to return a Python object:

static PyObject *MyFunction( PyObject *self, PyObject *args );

static PyObject *MyFunctionWithKeywords(PyObject *self,

PyObject *args,
PyObject *kw);

static PyObject *MyFunctionWithNoArgs( PyObject *self );

When we use the Py_RETURN_NONE macro that the Python headers have to offer, we can have a function
return None. This is equivalent to void in C. These are static functions. Here’s an example-

static PyObject * module_func(PyObject * self, PyObject * args)

char * input;

char * result;

PyObject * ret;

//Parsing arguments if(!

PyArg_ParseTuple(args, "s", &input))

{ return NULL;

//Running actual function

result=hello(input);

//Building a Python object from this string


ret=PyString_FromString(result);

free(result);

return ret;

d. Symbol/ Mapping Table


You need to register the function(s) in a symbol table for a module. For Python, all functions live in a module-
even C functions.
This is a kind of PyMethodDef:

static PyMethodDef module_methods[]={

{"my_func", (PyCFunction)module_func, METH_NOARGS, NULL},

{NULL, NULL, 0, NULL}

};

Let’s Explore Python Modules vs Packages

i. Parameters
Now here, we have four parameters-

Function name- How the interpreter presents it; here, it is my_func

Function address- Here, it is (PyCFunction)module_func

Flag- This can be of three kinds:

METH_VARARGS
METH_NOARGS- No arguments
Bitwise OR with METH_KEYWORDS- For working with keyword arguments

Docstring- When you don’t want to provide one, you can use NULL.

We terminate this with a sentinel holding the NULL and 0. In the example, we have used {NULL, NULL, 0,
NULL}.
In this table, we also put pointers to the C functions.

4. setup.py Script
After writing a Python-callable function, registering it in the module’s symbol table, and writing an
initialization function, we write a setup.py script.
from distutils.core import setup, Extension
#Extension module for C/ C++
extension_mod=Extension(“hello”, [“hellomodule.c”, “hello.c”])
setup(name=”hello”, ext_modules=[extension_mod])

5. Wrapping

A wrapper is a function that calls another. Here, a wrapper binds a Python object to a C function. Let’s see a
couple of different ways to do this. So far, we’ve only seen how to do this manually.

Python 3 Extension Programming with C & Others Languages

a. SWIG
SWIG is an acronym for Simple Wrapper Interface Generator. While it supports many languages, let’s consider
Python for now.
i. A makefile
all:
swig -python -c++ -o _swigdemo_module.cc swigdemo.i
python setup.py build_ext –inplace
Read about Python Datetime Module with Quick Examples
ii. SWIG wrapper file
%module swigdemo
%{
#include <stdlib.h>
#include “hello.h”
%}
%include “hello.h”
iii. setup.py script
from distutils.core import setup, Extension
extension_mod = Extension(“_swigdemo”, [“_swigdemo_module.cc”, “hello.c”])
setup(name = “swigdemo”, ext_modules=[extension_mod])

b. Pyrex
Pyrex is a hybrid of C and Python. Let’s try wrapping with this.
i. .pyx file
cdef extern from “hello.h”:
char * hello(char *str) #This takes the symbol ‘hello’ from hello.h.
def hello_fn(str):
return hello(str)
ii. setup.py file
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
setup(
name=”hello”,
ext_modules=[ Extension(“hellomodule”, [“hellomodule.pyx”, “hello.c”]) ],
cmdclass={‘build_ext’: build_ext}
)

6. How to Import Python 3 Extension


You can import an extension like you’d normally import a module in Python–
import module_func
print(module_func.my_func())
So, this was all about Python 3 Extension Programming. Hope you like our explanation.

7. Conclusion

Hence, in this Python Extend tutorial, we saw how to perform extension programming using C and Python. In
addition, we learned how to import Python 3 Extension Programming and how to write extensions for Python.
That’s all for today. Got a topic in Python you would like us to cover? Let us know in the comments.

How to Work with NoSQL Database in Python using PyMongo


by DataFlair Team · September 27, 2018

1. Python NoSQL Database

In our last Python tutorial, we studied Python Database Access. Here, in this Python NoSQL Database
tutorial, we will study the working of NoSQL Database in Python Programming Language. In addition, we will
discuss the need and benefits of Python NoSQL Database. Along with this will study different types of NoSQL
and different function like how to insert into, update, and delete data from a NoSQL database. We will use
MongoDB for this. At last, we will cover the NoSQL vs SQL.

So, let’s start the NoSQL Database in Python.


How to Work with NoSQL Database in Python using PyMongo

2. What is NoSQL Database?

Before beginning NoSQL Database in Python, let’s find out about NoSQL.
NoSQL expands to “Not Only SQL”. It lends us a way to store and retrieve data that we can model in forms
other than relational (tables). NoSQL databases largely find use in applications involving big data and real-time
uses. The reason we call them “Not Only SQL” is because they may support query languages that are SQL-like.
We can use NoSQL to store data in forms like key-value, document, columnar, and graph. When working with
large sets of distributed data, we use NoSQL.
Do you How to Copy a File n Python Programming Language

3. Need for NoSQL Database in Python

So, why do we need NoSQL?

Instead of large monolithic servers and storage infrastructure, organizations now use cloud computing,
open-source software, and commodity servers.

Projects now adopt agile approaches instead of long waterfall traditions (Agility).

There is a need to work with large volumes of data that changes type often. These types include
unstructured, semi-structured, structured, and polymorphic data.

The size of audiences has grown exponentially over the years (Scalability).

4. Database Types with NoSQL

As we’ve discussed before, we have four different types of data we can model with NoSQL Database in
Python-
How to Work with NoSQL Database in Python using PyMongo

a. Document Databases
In a document database, each key pairs with a document. A document is a complex data structure and can hold
any of the following- key-value pairs, key-array pairs, nested documents. These store semi-structured data.
We use these for applications like content management and mobile application data handling.

b. Graph Stores
A graph store holds knowledge about data networks. One such network is social connections. In a graph store, a
node is like a record in a relational database, and an edge is like a connection between nodes. This lets it
represent data relationships better. We use these for applications like CRM and reservation systems. Some
examples are Neo4J and Giraph.

c. Key-Value Stores
A key-value store holds key-value pairs in its structure. This is the simplest NoSQL database. We use these in
applications like session management and caching in web applications. Some examples include Riak, Redis,
and Berkeley DB.

d. Wide-Column Stores
When we want to store columns together working with queries over large datasets, we can use wide-column
stores. We also find these in SQL databases and they query large volumes faster. Some examples are
Cassandra, HBase, and Google BigTable.
Read About Python Forensics – Hash Function, Virtualization

5. Benefits of Using NoSQL Database


This are the following advantages of NoSQL Database in Python, let’s discuss them one by one:

Easy and flexible object-oriented programming.

Large volumes of rapidly changing data- unstructured, structured, or semi-structured.

Scale-out architecture that is geographically distributed.

Agile sprints, frequent code pushes, quick schema iteration.

6. NoSQL vs SQL

So, how are these two different? Let’s see.

Schemas are typically dynamic for NoSQL but fixed for SQL.

NoSQL can have different kinds of database, as discussed, but SQL has only one.

NoSQL came around in the late 2000s, SQL has been here since 1970s.

For SQL, the data storage model is individual records, but for NoSQL, this depends on the database type.

NoSQL has horizontal scaling; SQL has vertical.

For NoSQL, development is open-source; for SQL, it is a combination of open-source and closed-source.

The NoSQL DB used determines support for ACID transactions; SQL supports ACID transactions.

Examples of NoSQL databases are MongoDB, Cassandra, Neo4J, and HBase; Those for SQL databases
are MySQL, Oracle, Microsoft SQL Server, and Postgres.

7. Installing the Prerequisites of NoSQL Database in Python

In this Python NoSQL Database tutorial, we use the library pymongo. It is the official driver published by
Mongo developers. You can install it this way:
C:\Users\lifei>pip install pymongo
Collecting pymongo
Downloading
https://files.pythonhosted.org/packages/0f/54/ec07858c625460027536aefe8bbe1d0f319b62b5884ec8650e1c264
9dccb/pymongo-3.7.0-cp36-cp36m-win_amd64.whl (309kB)
100% |████████████████████████████████| 317kB 392kB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.7.0
This installs version 3.7.0, which is the latest version of pymongo at the time of writing the tutorial.
You will also need to install the MongoDB database.
Follow this link to know more about MongoDB Database

8. Operations Perform in NoSQL Database in Python


How to Work with NoSQL Database in Python using PyMongo

a. Insert Operation
To insert data into a NoSQL database in Python 3, we use the insert() method. This is the code we use in the
command prompt (You could also run a script or simply use the interpreter):

C:\Users\lifei>python

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

>>> from pymongo import MongoClient

>>> from pprint import pprint

>>> client=MongoClient() #Choose client

>>> db=client.test #Connect to DB

>>> student=db.student

>>> student_record={

... 'Name':'Ayushi Sharma',

... 'Enrolment':'0875CS191003',

... 'Age':'22'}

>>> result=student.insert_one(student_record)
>>> pprint(student.find_one({‘Age’:’22’}))

{u’Age’: u’22’,
u’Enrolment’: u’0875CS191003’,
u’Name’: u’Ayushi Sharma’,
u’_id’: ObjectId(‘7cyz7c2e72f5uh7829011e36’)}
Have a look The Best Article on Python’s Various Libraries

b. Update Operation
Now to update this data, we use the update() method.

>>> from pymongo import MongoClient

>>> from pprint import pprint

>>> client=MongoClient() #Choose client

>>> db=client.test #Connect to DB

>>> student=db.student

>>> db.student.update_one({'Age':'22'},

... {'$set': {'Name':'Ayushi Sharma',

.. 'Enrolment':'0875CS191003',

... 'Age':'23'}}) #Choosing the record to update

>>> pprint(student.find_one({‘Age’:’23’})

{u’Age’: u’23’,
u’Enrolment’: u’0875CS191003’,
u’Name’: u’Ayushi Sharma’,
u’_id’: ObjectId(‘7cyz7c2e72f5uh7829011e36’)}

c. Delete Operation
Now to delete this record, we can use the delete() method.

>>> from pymongo import MongoClient

>>> from pprint import pprint

>>> client=MongoClient() #Choose client

>>> db=client.test #Connect to DB

>>> student=db.student
>>> db.student.delete_one({‘Age’:’23’})

>>> pprint(student.find_one({‘Age’:’23’})

None
Here, Python prints None because it can’t find that record in the database.
So, this was all about NoSQL Database in Python. Hope you like our explanation.

9. Conclusion

Hence, this gets you started with the basics of using NoSQL databases in Python using PyMongo. Leave your
queries in the comments below.

How to Work with Relational Database with Python


by DataFlair Team · September 27, 2018

1. Python Relational Database

In our last Python Database tutorial, we checked how to work with NoSQL databases with Python. In the
same way today we talk about working of Relational Database with Python Programming Language.
Moreover, we will read some important prerequisites of Relational Database and how to read a relation table.
Along with this, we will get to know how to delete an entire table and a single row in relation database with
Python.
So, let start working with Relational Database with Python.

How to Work with Relational Database with Python

2. Prerequisites for Relational Database


For this demo, we need three things- sqlalchemy, pandas, and a CSV file.

Prerequisites for Relational Database

a. SQLAlchemy

How to Work with Relational Database with Python

SQLAlchemy is an open-source toolkit and an object-relational mapper. You can download this using pip-
C:\Users\lifei>pip install sqlalchemy
Requirement already satisfied: sqlalchemy in c:\users\lifei\appdata\local\programs\python\python36\lib\site-
packages (1.2.9)
Note- If your import works for the command line and not for the IDLE, then you may be running different
versions of Python in the two. You can add this to the path in your IDLE-

>>> sys.path.append('C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages')

b. Pandas

sqlalchemy, pandas, and a CSV file

Pandas is a Python library for data manipulation and analysis. You can download it using pip-
C:\Users\lifei>pip install pandas
Collecting pandas
Let’s Read a New Topic – Python 3 Extension Programming with C & Others Languages
Downloading
https://files.pythonhosted.org/packages/ad/0b/f65a63cccc5e37a7af983fc87a8c9e56feb4998d8877f8f2af21f40bc
f3c/pandas-0.23.1-cp36-cp36m-win_amd64.whl (10.5MB)
100% |████████████████████████████████| 10.5MB 385kB/s
Collecting pytz>=2011k (from pandas)
Downloading
https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b8
98779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 385kB/s
Collecting numpy>=1.9.0 (from pandas)
Downloading
https://files.pythonhosted.org/packages/0d/b7/0c804e0bcba6505f8392d042d5e333a5e06f308e019517111fbc776
7a0bc/numpy-1.14.5-cp36-none-win_amd64.whl (13.4MB)
100% |████████████████████████████████| 13.4MB 461kB/s
Collecting python-dateutil>=2.5.0 (from pandas)
Downloading
https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d168
2825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
100% |████████████████████████████████| 215kB 504kB/s
Requirement already satisfied: six>=1.5 in c:\users\lifei\appdata\local\programs\python\python36\lib\site-
packages (from python-dateutil>=2.5.0->pandas) (1.11.0)
Installing collected packages: pytz, numpy, python-dateutil, pandas
Successfully installed numpy-1.14.5 pandas-0.23.1 python-dateutil-2.7.3 pytz-2018.5

c. A Sample CSV File

A Sample CSV File

To work with the relational databases, we download a sample CSV file from the web. Here’s what it looks like:

Prerequisites for Relational Database- A Sample CSV FileLet’s Explore Python Database
Access – Python 3 MySQL

3. Reading a Relational Table


Now let’s try reading from this Relational table
>>> from sqlalchemy import create_engine

>>> import pandas

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")

>>> engine=create_engine('sqlite:///:memory:') #Create db engine

>>> data.to_sql('data_table',engine) #Store dataframe as table

>>> print(pandas.read_sql_query('SELECT * FROM data_table',engine))

This produces the following output:


Reading a Relational Table – Outputv

4. How to Insert Values in Relational Database with Python?

Now to add another row of content into Relational Database with Python, we use the following code:

>>> from sqlalchemy import create_engine

>>> from pandas.io import sql

>>> import pandas

>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")

>>> engine=create_engine('sqlite:///:memory:') #Create db engine


>>> data.to_sql('data_table',engine) #Store dataframe as table

>>> sql.execute('INSERT INTO data_table VALUES(?,?,?,?,?,?,?,?,?,?,?)',\

engine,params=[(9,11,’Xerox 198’,'Frederick Jones',752,-321.57,2.2,9.8,'Nunavut','Paper',0.37)])

<sqlalchemy.engine.result.ResultProxy object at 0x00FA3530>

>>> print(pandas.read_sql_query('SELECT * FROM data_table',engine))

This produces the following output:


index 1 Eldon Base for stackable storage shelf, platinum \
0 0 2 1.7 Cubic Foot Compact “Cube” Office Refrigera…
1 1 3 Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl
2 2 4 R380
3 3 5 Holmes HEPA Air Purifier
4 4 6 G.E. Longer-Life Indoor Recessed Floodlight Bulbs
5 5 7 Angle-D Binders with Locking Rings, Label Holders
6 6 8 SAFCO Mobile Desk Side File, Wire Frame
7 7 9 SAFCO Commercial Wire Shelving, Black
8 8 10 Xerox 198
9 9 11 Xerox 198
Muhammed MacIntyre 3 -213.25 38.94 35 Nunavut \
0 Barry French 293 457.81 208.16 68.02 Nunavut
1 Barry French 293 46.71 8.69 2.99 Nunavut
2 Clay Rozendal 483 1198.97 195.99 3.99 Nunavut
3 Carlos Soltero 515 30.94 21.78 5.94 Nunavut
4 Carlos Soltero 515 4.43 6.64 4.95 Nunavut
5 Carl Jackson 613 -54.04 7.30 7.72 Nunavut
6 Carl Jackson 613 127.70 42.76 6.22 Nunavut
7 Monica Federle 643 -695.26 138.14 35.00 Nunavut
8 Dorothy Badders 678 -226.36 4.98 8.33 Nunavut
9 Frederick Jones 752 -321.57 2.20 9.80 Nunavut
Storage & Organization 0.8
0 Appliances 0.58
Have a Look at Difference Between Method and Function in Python

5. How to Delete Values in Relational Database with Python?

Relational Database with Python can delete values raw vise and delete the entire table. So, let’s see how it
works.

a. Deleting the Entire Table


Now finally, we want to delete the entire file. How do we do that? Let’s find out.

>>> from sqlalchemy import create_engine

>>> from pandas.io import sql


>>> import pandas

>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")

>>> engine=create_engine('sqlite:///:memory:') #Create db engine

>>> data.to_sql('data_table',engine) #Store dataframe as table

>>> sql.execute('DELETE FROM data_table',engine)

<sqlalchemy.engine.result.ResultProxy object at 0x00E6D9F0>

>>> print(sql.read_sql_query('SELECT * FROM data_table',engine))

Empty DataFrame
Columns: [index, 1, Eldon Base for stackable storage shelf, platinum, Muhammed MacIntyre, 3, -213.25,
38.94, 35, Nunavut, Storage & Organization, 0.8] Index: []

b. Deleting a Single Row


Now we don’t have column names in this CSV file, but we can use the first value to delete a row this way-

>>> sql.execute('DELETE FROM data_table where 35=(?)',engine,params=[(9.80)])

<sqlalchemy.engine.result.ResultProxy object at 0x00FA3170>

>>> print(sql.read_sql_query('SELECT * FROM data_table',engine))

index 1 Eldon Base for stackable storage shelf, platinum \


0 0 2 1.7 Cubic Foot Compact “Cube” Office Refrigera…
1 1 3 Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl
2 2 4 R380
3 3 5 Holmes HEPA Air Purifier
4 4 6 G.E. Longer-Life Indoor Recessed Floodlight Bulbs
5 5 7 Angle-D Binders with Locking Rings, Label Holders
6 6 8 SAFCO Mobile Desk Side File, Wire Frame
7 7 9 SAFCO Commercial Wire Shelving, Black
8 8 10 Xerox 198

Muhammed MacIntyre 3 -213.25 38.94 35 Nunavut \


0 Barry French 293 457.81 208.16 68.02 Nunavut
1 Barry French 293 46.71 8.69 2.99 Nunavut
2 Clay Rozendal 483 1198.97 195.99 3.99 Nunavut
3 Carlos Soltero 515 30.94 21.78 5.94 Nunavut
4 Carlos Soltero 515 4.43 6.64 4.95 Nunavut
5 Carl Jackson 613 -54.04 7.30 7.72 Nunavut
6 Carl Jackson 613 127.70 42.76 6.22 Nunavut
7 Monica Federle 643 -695.26 138.14 35.00 Nunavut
8 Dorothy Badders 678 -226.36 4.98 8.33 Nunavut
Storage & Organization 0.8
0 Appliances 0.58
1 Binders and Binder Accessories 0.39
2 Telephones and Communication 0.58
3 Appliances 0.50
4 Office Furnishings 0.37
5 Binders and Binder Accessories 0.38
6 Storage & Organization NaN
7 Storage & Organization NaN
8 Paper 0.38
Follow this link for Python GUI Programming – Tkinter Tutorial
So, this was all about the Working of Relational Database with Python. Hope you like our explanation.

6. Conclusion
Hence, in this Python Relation Database tutorial, we learned about working of a relational database with
Python. Now you can read CSV files, insert data into them, and delete data from them using simple SQL
queries. Got any questions? Drop them in the comments below.

Python Web Framework – A Detailed List of Web Frameworks


in Python
by DataFlair Team · September 26, 2018

1. Python Web Framework

If your mission with Python is to develop for the Web, Python will help you with that with its range of
frameworks. Today in Python Web Framework tutorial, we will briefly discuss Web Frameworks offered by
Python Programming Langauge: Full-Stack Frameworks in Python, and Non- Full-Stack Frameworks in
Python.

So, let’s talk about Python Web Framework Tutorial.


Python Web Framework – A Detailed List of Web Frameworks in Python

2. What is a Python Web Framework?

Python Web framework is a collection of packages or modules that allow developers to write Web applications
or services. With it, developers don’t need to handle low-level details like protocols, sockets or process/thread
management.

Python web framework will help you with:

Interpreting requests (getting form parameters, handling cookies and sessions,..)

Producing responses (presenting data as HTML or in other formats,..)

Storing data persistently (and other things)

Now, let’s look at the most useful and famous Python web framework to help you with Web development.

Learn: Python Tuples vs Lists – Comparison between Lists and Tuples

3. Python Full-Stack Frameworks

A full-stack framework in Python is one which attempts to provide a complete solution for applications. It
attempts to supply components for each layer in the stack.

a. Django
Django Python is a framework for perfectionists with deadlines. With it, you can build better Web apps in much
less time, and in less code. Django is known for how it focusses on automating. It also believes in the DRY
(Don’t Repeat Yourself) principle.

Django was originally developed for content-management systems, but is now used for many kinds of web
applications. This is because of its templating, automatic database generation, DB access layer, and automatic
admin interface generation. It also provides a web server for development use.

Giant companies that use Django Python are- Instagram, Pinterest, Disqus, Mozilla, The Washington Times,
and Bitbucket. In fact, when we think of the terms ‘framework’ and ‘Python’, the first thing that comes to our
minds is Django.
We will see more on Django in another lesson.

b. TurboGears

Python TurboGears – Python Web Framework


With TurboGears, you can create a database-driven, ready-to-extend application in just a few minutes.

It is an MVC web framework with ORM with real multi-database support and support for horizontal data
partitioning. It also has a widget system to simplify the development of AJAX apps. You may additionally
install its template engine Kajiki.

Learn: How to Install Python on Windows

TurboGears is a microframework and a full-stack solution. It’s PyPI package is called tg.devtools.

c. web2py

Python Web Framework – Python web2py

With web2py, you can develop, deploy, debug, test, administer the database, and maintain applications via the
provided web interface. It has no configuration files, and you can even run it off a USB drive.

web2py uses the MVC built-in ticketing system to manage errors.

d. CubicWeb
CubicWeb is a semantic web application framework that features a query language and a selection+view
mechanism. It also features multiple databases, security, workflows, and reusable components.

e. Django-hotsauce
Django-hotsauce is a general-purpose web toolkit that sits on top of Django and other frameworks. It is an
interactive Pythonic API that will let you create scalable web applications using the WSGI 1.0 spec. It also
provides native bindings for the Schevo DBMS, Durus, ZODB, and Authkit projects.

Learn: 7 Reasons Why Should I Learn Python in 2018

f. Giotto
A strict MVC framework that strictly separates Model, View and Controller elements, Giotto makes sure that
designers, Web developers, and sysadmins can work independently. It also includes controller modules that
allow you to build applications on top of the web, irc or the command line.These are all the most popular
Python web framework.

g. Grok
Grok was built on the existing Zope 3 libraries. It aims to provide an easier learning curve, and a more agile
development experience by emphasizing on convention over configuration and DRY (Don’t Repeat Yourself).
h. Pylons

Python Web Framework – Python Pylons

Pylons is a lightweight Web framework aiming at flexibility and rapid development. With the best ideas from
Ruby, Python, and Perl, it makes for a structured, but extremely flexible Python Web framework. With Pylons,
Web development is fast, flexible, and easy. Pylons is built on top of Paste. But after being merged with
Pyramid to form the Pylons project, it is in maintenance-only status.

i. Reahl
You can use Reahl to develop web applications in pure Python. However, you may use, customize, or compose
widgets in usual Python code. These widgets portray certain server-side and client-side behaviors.

j. wheezy.web
Wheezy is a lightweight, high performance, and high concurrency WSGI web framework. Its key features
include routing, model update/validation, authentication/authorization, content caching with dependency,
middleware, and more. With these, we can build modern, efficient web.

k. Zope2

Python Web Framework – Python Zope

Zope2 is rightly the granddaddy of Python web frameworks, it has been a family of networks. It is a web
framework and a general-purpose application server. Today, it is primarily used for CMS. We also have Zope3,
which is a standalone framework and a collection of related libraries.

l. Tornado

Python Web Framework – Tornado


While Tornado isn’t that famous, it is great with non-blocking I/O. You can scale it to handle tens of thousands
of open connections. It makes for a perfect framework for long polling, WebSockets, and other usages needing
a continuous connection. Officially, Tornado only supports Linux and BSD OS (Windows and Mac OS X- only
for development). Tornado finds its origin in the FriendFeed project, which now belongs to Facebook.

4. Non-Full-Stack Frameworks in Python


A Python non full-stack framework will provide the base application server. This either runs as its own
independent process, upon Apache, or in other environments. Let’s look at the most popular ones.

a. Python Bottle
Bottle is a simple and fast microframework that you can use to create small Web applications. It provides
request-dispatching routes with URL-parameter support, templates, key/value databases, and a build-in HTTP
server. It also offers adapters for third-party WSGI/HTTP-server and template engines. This is all in a single
file; there are no dependencies except the Python Standard Library.

b. CherryPy

Python Web Framework – Python CherryPy

It is a pythonic, object-oriented HTTP framework. A web application powered by CherryPy is a standalone


Python application that embeds its own multi-threaded web server.
In a way, CherryPy is a way between the programmer and the problem. It also supports various web servers like
Apache, IIS, and so. CherryPy will let you launch multiple HTTP servers at once.

c. Python Flask

Python Web Framework – Python Flask


Like we’ve said before, Flask is a microframework for Python. It includes a built-in development server, and
unit-testing support. It is also fully Unicode-enabled with RESTful request-dispatching and WSGI compliance.
Learn: Python Regular Expressions
Flask will be useful when you want to develop small, simple applications. With it, you can operate your
database however you like- using SQLAlchemy or whatever. A goof Flask example is it is used by LinkedIn
and Pinterest.

d. Hug

Python Web Framework – Python Hug

Hug is among the fastest web frameworks for Python. With it, you can build APIs. It supports several API
versions, automatic API documentation, and annotation-powered validation. It is built on top of another JSON
framework, Falcon.

e. Pyramid

Python Web Framework – pyramid-positive

Unlike a few that we discussed so far, Pyramid is a framework for large applications. It is flexible; a Pyramid
web application starts from a single-file module, and evolves into an ambitious project. You can say that it
makes real-world Web application development and deployment more fun, predictable, and productive.
Actually, Pyramid is a Pylons project.

f. Albatross
It is a small, flexible Python toolkit that lets you develop highly stateful Web applications. Albatross deploys to
CGI, FastCGI, and ModPython servers.

g. Circuits
Circuits is much like CherryPy, but is a highly efficient web framework to develop standalone multiprocess
applications. It supports concurrency, asynchronous I/O components, and is event-driven.
h. Falcon

Python Web Framework – Python Falcon

A microframework for small applications, app backends, and higher-level frameworks, Falcon encourages to
follow the concept of REST. It is among the fastest web frameworks for Python and is used by EMC, Hurricane
Electric, OpenStack, Opera Software, Wargaming, and others.

i. Growler

Python Web Framework – Python Growler

Growler is built on top of asyncio, and is inspired by Connect and Express frameworks for Node.js. If you want
ORM or templating, you must install it manually. It handles requests by passing through a middleware chain.

j. MorePath
MorePath is a flexible, model-driven web framework. It supports REST, and focusses on reusability and
extensibility.

k. Pycnic
Pycnic is among the fastest web frameworks for Python for developing JSON APIs. The framework is object-
oriented, and optimized for JSON APIs. It only includes tools for creating Web APIs that leave a lighter
footprint.

l. Sanic
Python Web Framework – Python Sanic

Sanic is a flask-like framework, but it is fast. It supports asynchronous request handlers, and makes code non-
blocking and speedy.
Learn: The Tremendous Python Career Opportunities in 2018

So, this was all about Python Web Framework Tutorial. Hope you like our explanation.

5. Conclusion

So, these are the most famous Python web framework for Python Web development. How you like the python
web development frameworks. Tell us, which one is your favourite? OR If you have any query regarding the
Python web framework, please comment.

Learn Python Flask Tutorial – A Web Framework for Python


by DataFlair Team · September 27, 2018

1. Python Flask Tutorial


In this Python tutorial, we will learn the basics of Python Flask. Moreover, in this Python Flask Tutorial. we
will discuss web framework for Python. Along with this we will learn set up the environment, some rules and
URL building for Python Flask. At last, we will see HTTP methods in Python Flask.

So, let’s begin the Python Flask tutorial.


Learn Python Flask Tutorial – A Web Framework for Python

2. What is Python Flask?


A micro web framework itself written in Python, Flask is BSD-license. It was developed by Armin Ronacher,
and is by Pocco- an international group of Python enthusiasts. It is based on the Werkzeug toolkit and Jinja2
template engine. Now, the latest stable version is 1.0, released in April 2018. Top applications that use it
include Pinterest, LinkedIn, and the community page for Flask.

Learn Python Flask Tutorial – What is Python Flask

Since it does not need any tools or libraries, we can also call it a micro-framework. It also has no database
abstraction layer or form validation. But we can find these features as extensions. These include form
validation, upload handling, object-relational mappers, open authentication technologies, and common
framework tools.
Here, some names around Python Flask include:

WSGI (Web Server Gateway Interface)– A standard for Python web application development.

Werkzeug– A WSGI toolkit implementing requests, response objects, and other utility functions.

Jinja 2– A Python templating engine.

3. Python Flask Tutorial – Setting Up the Environment


Hence, to get started with Python Flask, we’ll first need to set up an environment that supports it.

a. Prerequisites for installing Python Flask


Basically, you’ll need Flask 2.6 or higher to be able to install Flask.

b. Install virtualenv
Next, you’ll need virtualenv to build a virtual Python environment. This is to create multiple environments at
once to avoid compatibility issues between versions of libraries.
The command we use to install it is:

C:\Users\lifei>pip install virtualenv

Requirement already satisfied: virtualenv in

c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages (15.1.0)

Since we already have it installed, it states so.

Now, create a new environment in a new folder:

C:\Users\lifei>cd Desktop

C:\Users\lifei\Desktop>mkdir projectx

C:\Users\lifei\Desktop>cd projectx

C:\Users\lifei\Desktop\projectx>virtualenv venv

Using base prefix ‘c:\\users\\lifei\\appdata\\local\\programs\\python\\python36-32’

New python executable in C:\Users\lifei\Desktop\projectx\venv\Scripts\python.exe

Installing setuptools, pip, wheel…done.

C:\Users\lifei\Desktop\projectx>

Then activate the corresponding environment:


C:\Users\lifei\Desktop\projectx>venv\scripts\activate

(venv) C:\Users\lifei\Desktop\projectx>

Finally, install Flask (You don’t necessarily have to be in the virtual environment for this):
(venv) C:\Users\lifei\Desktop\projectx>pip install flask
Collecting flask

Downloading

https://files.pythonhosted.org/packages/55/b1/4365193655df97227ace49311365cc296e74b60c7f5c63d23cd3
0175e2f6/Flask-1.0-py2.py3-none-any.whl (97kB)

100% |████████████████████████████████| 102kB 143kB/s

Collecting itsdangerous>=0.24 (from flask)

Downloading

https://files.pythonhosted.org/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab2211
65194b2d4/itsdangerous-0.24.tar.gz (46kB)

100% |████████████████████████████████| 51kB 67kB/s

Collecting Jinja2>=2.10 (from flask)

Downloading

https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659
220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)

100% |████████████████████████████████| 133kB 33kB/s

Collecting click>=5.1 (from flask)

Downloading

https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd7
00775a77/click-6.7-py2.py3-none-any.whl (71kB)

100% |████████████████████████████████| 71kB 36kB/s

Collecting Werkzeug>=0.14 (from flask)

Downloading

https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04
fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)

100% |████████████████████████████████| 327kB 33kB/s


Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)

Downloading

https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4b
fcbdf4172b/MarkupSafe-1.0.tar.gz

Building wheels for collected packages: itsdangerous, MarkupSafe

Running setup.py bdist_wheel for itsdangerous … done

Stored in directory:

C:\Users\lifei\AppData\Local\pip\Cache\wheels\2c\4a\61\5599631c1554768c6290b08c02c72d7317910374c
a602ff1e5

Running setup.py bdist_wheel for MarkupSafe … done

Stored in directory:

C:\Users\lifei\AppData\Local\pip\Cache\wheels\33\56\20\ebe49a5c612fffe
1c5a632146b16596f9e64676768661e4e46

Successfully built itsdangerous MarkupSafe

Installing collected packages: itsdangerous, MarkupSafe, Jinja2, click, Werkzeug, flask Successfully installed
Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-6.7 flask-1.0 itsdangerous-0.24

(venv) C:\Users\lifei\Desktop\projectx>

Let’s explore Python Iterables and Python Itertools with Examples

4. Python Flask Tutorial – Beginning with Python Flask


Now, in Python Flask tutorial, let’s begin with a program that says Hello.
from flask import Flask

>>> from flask import Flask

>>> app=Flask( name__)

>>> @app.route('/')

def sayhello():

return 'Hello'

>>> if name ==' main ':

app.debug=True
app.run()

We save this as demo.py in the folder project. Then, we get to the command prompt to run this.

C:\Users\lifei>cd Desktop/projectx

C:\Users\lifei\Desktop\projectx>python demo.py

* Serving Flask app “demo” (lazy loading)

* Environment: production

WARNING: Do not use the development server in a production environment.

Use a production WSGI server instead.

* Debug mode: on

* Restarting with stat

* Debugger is active!

* Debugger PIN: 174-045-654

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

127.0.0.1 – – [29/Apr/2018 15:00:03] “GET / HTTP/1.1” 200 –

127.0.0.1 – – [29/Apr/2018 15:00:04] “GET /favicon.ico HTTP/1.1” 404 –

From here, we take the address 127.0.0.1:5000 and paste it into the address bar of our browser:

Python Flask Tutorial – Beginning with Python Flask


Here, the Flask() constructor takes an argument, the current module’s name: name .
route() is a decorator function that decides which URL to allow to call the associated function. Its syntax is:

app.route(rule,options)

While rule denotes the URL binding with the function, options hold parameters to be forwarded to this rule
object. Here, we bind the URL ‘/’ to the function sayhello(). So, getting to where that URL point takes us to
where this function executes. Take a look at this code:
From flask import Flask

>>> from flask import Flask

>> app=Flask( name )

>>> @app.route('/')

def sayhello():

return 'Hello'

>>> @app.route('/hi')

def sayhi():

return 'Hi'

>>> if name ==' main ':

app.debug=True

app.run()

When we go to the following URL:

http://127.0.0.1:5000/hi

The browser displays:

Hi

So, you can see all this activity on your command prompt side by side. We can also use the app.add_url_rule()
function for this:

>>> def salut():

return 'Buna'

>>> app.add_url_rule('/salut','buna',salut)
Python Flask Tutorial – Beginning with Python Flask

Now, when we set debug to True, the server automatically reloads on changes to the code. Otherwise, we’d
have to restart it manually for each change.

And finally, the method run() runs the application on the local development server. This may take up to four
arguments:

run(host, port, debug, options)

The default hostname to listen to is 127.0.0.1 (the localhost); we can set it to 0.0.0.0 to avail a server externally.
The default port is 5000. debug is false by default, and we can forward the options to the underlying Werkzeug
server.

5. Python Flask Tutorial – Variable Rules in Python Flask


It is also possible to dynamically build a URL. We put the variable parts in angular brackets in the rule, and
pass them as parameters to the associated function:
From flask import Flask

>>> from flask import Flask

>>> app=Flask( name__)

>>> @app.route('/')

def sayhello():

return 'Hello'

>>> @app.route('/<one>add<two>')

def add(one,two):

return f"{one}+{two}={int(one)+int(two)}"

>>> if name ==' main ':


app.debug=True

app.run()

And in the browser:

Python Flask Tutorial – Variable Rules in Python Flask

Read Python Built-In Functions with Syntax and Examples


And another one:

Python Flask Tutorial – Variable Rules in Python Flask

So, we can also use the following converters- int, float, path:

>>> @app.route('/<int:one>add<int:two>')

def add(one,two):

return f"{one}+{two}={one+two}"

>>> @app.route('/<float:one>add<float:two>')

def add(one,two):

return f"{one}+{two}={one+two}"
Python Flask Tutorial – Variable Rules in Python Flask

Werkzeug’s routing model ensures the uniqueness of these URLs, and also that they maintain the standards laid
by Apache.
Ending a rule with a trailing slash will make it work with and without it. But not doing so, it won’t work with a
trailing slash.
From flask import Flask

>>> from flask import Flask

>>> app=Flask( name__)

>>> @app.route('/')

def sayhello():

return 'Hello'

>>> @app.route('/hi/')

def sayhi():

return 'Hi'

>>> if name ==' main ':

app.debug=True

app.run()

If we remove the trailing slash, the snippet of code remains:

>>> @app.route('/hi')

def sayhi(): return 'Hi'

And this happens:


Python Flask Tutorial – Variable Rules in Python Flask

6. URL Building for Python Flask


To dynamically build a URL according to need, we use two functions: redirect() and url_for(). We need to
import these as well.
Let’s explore Python Modules

>>> from flask import Flask, url_for,redirect

>>> app=Flask( name__)

>>> @app.route('/')

def sayhello():

return 'Hello'

>>> @app.route('/<name>')

def sayhi(name):

return f'Hi, {name}'

>>> @app.route('/panel/<salutation>')

def saysomething(salutation):

if salutation=='guest':

return redirect(url_for('sayhello'))

else:

return redirect(url_for('sayhi',name=salutation))

>>> if name ==' main ':

app.debug=True
app.run()

The function saysomething() takes a salutation as an argument. If this is ‘guest’, it executes function sayhello()
and prints Hello. If this is anything else, it prints hi to that string. For instance, getting to
127.0.0.1:5000/panel/ayushi takes us to 127.0.0.1:5000/ayushi and prints this:
Hi, ayushi.

7. Python Flask Tutorial – HTTP Methods


Now, we discuss different HTTP methods to retrieve data from a URL.

a. GET

The GET method sends data to the server unencrypted.

b. HEAD

HEAD is like GET, except that it has no response body.

c. POST

The server does not cache the HTML form data that this sends.

d. PUT

This uses the uploaded content to replace current representations of the target resource.

e. DELETE

This removes current representations of the target resource suggested by a URL.


Basically, the default route takes GET requests, but we can alter this by using the route() decorator with a
method’s argument.

Now, take the following Python code:

>>> from flask import Flask,url_for,redirect, request

>>> app=Flask( name__)

>>> @app.route('/success/<name>')

def success(name):

return f'Hello, {name}'

>>> @app.route('/login',methods=['POST','GET'])

def login():
if request.method=='POST':

user=request.form['firstname']

return redirect(url_for('success',name=user))

else:

user=request.args.get('firstname')

return redirect(url_for('success',name=user))

>>> if name ==' main ':

app.debug=True

app.run()

And this is the form we have:

<head></head>

<body>

<form action = “http://localhost:5000/login” method = “post”>

<input type=”text” name=”firstname”/><br/>

<input type=”submit” value=”submit”/> </form>

</body>

When you open this HTML page in the browser:

Python Flask Tutorial – HTTP Methods


Python Flask Tutorial – HTTP Methods

Learn Python Flask Tutorial – HTTP METHODS

Revise Python Inheritance, Method Overloading and Method Overriding


So, this was all about Python Flask tutorial. Hope you like our explanation.

8. Conclusion
Hence, this is just a brief introduction to get you started with Python Flask. Here, we studied basics of Python
Flask, setting up the environment for Flask. Moreover, we studied how to begin with Python Flask and its
variable rules. Along with this, we saw how to build URL for Python Flask. At last, we discussed how to
retrieve data from a URL by HTTP methods. Furthermore, if you have any query, leave your doubts in the
comment section.

Best Python Django Tutorial For Beginners – With Project Structure


by DataFlair Team · August 5, 2019

1. Objective

In our last tutorial, we discussed How to Work with Relational Database with Python. In this Python Django
tutorial for beginners, we will see how to install Django Python. Moreover, we will study the history of Python
Django and MVT Pattern. Along with this, we will cover Python Django features and how to create an
application in Python Django. At last, we will cover the structure of the project in Python Django.
So, let’s start the Python Django Tutorial.

Best Python Django Tutorial For Beginners

2. Python Django Tutorial – What is Django?

Django is a high-level Python framework. It is free and open-source, written in Python itself, and follows the
model-view-template architectural pattern. We can use it to develop quality web applications faster and easier.
Since developing for the web needs a set of similar components, you can use a framework. This way, you don’t
have to reinvent the wheel. These tasks include authentication, forms, uploading files, management panels, and
so.

Best Python Django Tutorial For Beginners

3. Python Django Tutorial – Install Django

To work with Django on your system,


C:\Users\lifei>pip install Django
Collecting Django
Let’s Explore Unique Features of Python Programming Language
Downloading
https://files.pythonhosted.org/packages/ab/15/cfde97943f0db45e4f999c60b696fbb4df59e82bbccc686770f4e44c
9094/Django-2.0.7-py3-none-any.whl (7.1MB)
100% |████████████████████████████████| 7.1MB 610kB/s
Requirement already satisfied: pytz in c:\users\lifei\appdata\local\programs\python\python36\lib\site-packages
(from django) (2018.5)
Installing collected packages: django
Successfully installed django-2.0.7

4. Python Django Tutorial – Serving a Request for a Website

Let’s first find out, in layman’s terms, what happens when your server receives a request for a website. The
request is passed to Django and that tries to analyze this request. The urlresolver tries to match the URL against
a list of patterns. It performs this match from top to bottom. If it can find a match, it passes the request to the
view, which is the associated function.
The function view can check if the request is allowed. It also generates a response, and then Django sends it to
the user’s web browser.

5. Python Django Tutorial – History

Adrian Holovaty and Simon Willison created Django in the fall of 2003 at the Lawrence Journal-World
newspaper

Django publicly released under a BSD license in July 2005; named after guitarist Django Reinhardt

Today, Django is an open-source project with contributors around the world

6. Python Django Tutorial – MVT Pattern

MVC stands for Model-View-Controller. We use this when we want to develop applications with user
interfaces. MVT stands for Model-View-Template. A template is an HTML file mixed with DTL (Django
Template Language). Django takes care of the Controller part, which is the software code and controls the
interaction between the other two parts- Model and View. When a user requests for a resource, Django acts as a
controller and checks if it is available. If the URL maps, View interacts with the Model and renders a Template.
Python Django sends back a Template to the user as a response.
Read about Python Decision Making Statements with Syntax and Examples

Best Python Django Tutorial For Beginners


The model helps us handle database. View executes business logic and interacts with Model to carry data. It
also renders Template. Template handles the user interface and is a presentation layer.
The Model class holds essential fields and methods. For each model class, we have a table in the database.
Model is a subclass of django.db.models.Model. Each field here denotes a database field. With Django, we have
a database-abstraction API that lets us perform CRUD (Create-Retrieve-Update-Delete) operations on mapped
tables.

7. Python Django Tutorial – Features of Django

When working with Python Django, you can expect the following Django Features-

Best Python Django Tutorial – Features of Django

a. Scalability
When you need to scale your system, you can simply add more web nodes to your Django. That is, you can
scale it horizontally. Two products that use Django’s scalability are Disqus and Instagram.
Do you know the Errors and Exceptions in Python Programming

b. Portability
The portability of Python makes for a portable Django too. Various platforms include Windows, Linux, and
MacOS.

c. Security
Python Django ensures some arrangements for security too. One of these is that it stores hashed passwords in
cookies.

d. Versatility
Python Django will work with formats like HTML, JSON, XML, among others. It also supports many different
client-side frameworks. So, we can use it to build anything including regular websites and social networks.

e. Packages
Django Programming has the foundation of thousands of additional packages.

f. Ease of Use
Features like the built-in admin interface make it easy to build with Django. It is also fully functional and finds
it easy to switch databases.

8. Prerequisites to Creating a Project in Django

In this Python Django Tutorial, we will study the prerequisites to create a project in Python Django.

a. Starting Project
Use the following command in the command prompt to begin your project-

C:\Users\lifei\Desktop>django-admin startproject project0

Then move to this folder.

C:\Users\lifei\Desktop>cd project0

C:\Users\lifei\Desktop\project0>

b. Running the Server


You could apply these migrations before you start your server-

C:\Users\lifei\Desktop\project0>python manage.py migrate

Now, start the server-

C:\Users\lifei\Desktop\project0>python manage.py runserver

Performing system checks…

Have a look at Python Function Arguments with Types, Syntax, and Examples

System check identified no issues (0 silenced).

July 08, 2018 – 21:51:39


Django version 2.0.7, using settings ‘project0.settings’

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

If you see something like this, it means you have successfully installed Django:

Python Django Tutorial – Prerequisites to Creating a Project

c. Setting up a database
For our project, we have set up MySQL: http://www.mysql.com/
Python also supports other database products like Oracle, SQLite 3, MongoDB, PostgreSQL, and
GoogleAppEngine Datastore.

d. Web Server
While Django has its own lightweight web server, you can also use your own- like Apache.

9. Django Project Structure


This is what your project directory will look like:
Python Django Tutorial – Project Structure

Do you know the difference Between Python Generator vs Iterator

a. manage.py
This script lets us interact with our project using the command line. Facilities include starting up the server and
syncing to the database.

Python Django Tutorial – manage.py

b. Project folder
This holds the packages for our project-
Python Django Tutorial – Project folder

init .py- A Python package.

settings.py- This holds website settings like database configuration details.

urls.py- This holds links in your project and the function(s).

wsgi.py- This deploys our project over WSGI and helps the app communicate with the web server. WSGI
stands for Web Server Gateway Interface.

pycache - This directory holds files like init .cpython-36.pyc, settings.cpython-36.pyc,


urls.cpython-36.pyc, and wsgi.cpython-36.pyc.

In settings.py, DEBUG is set to True. This is okay at the time of deployment, but you should set it to False
when working with a live project. This is because this gives out information about errors in your project.
Read about Python Modules vs Packages
Another construct you will find in this file is-

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.sqlite3',

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

c. db.sqlite3
This is the database file for your project and has the extension .sqlite3.

10. Creating an Application


Let’s learn how to create an application in Django by this python Django Tutorial. Projects are modular. A
contact form is one such application, and you can borrow it from one project for another. You can start an
application this way:
C:\Users\lifei\Desktop\project0>python manage.py startapp app0
This creates the following contents in the directory project0:

Python Django Tutorial – Creating an Application

Python Django Tutorial – How to Create an Application

Migrations- This holds another init .py file.

init .py- This is a Python package.

admin.py- This lets us make the app modifiable in the admin interface.

apps.py- This holds the names of all your applications.

models.py- This holds all application models.

tests.py- This holds unit tests for the project.

views.py- This holds the application views for the project.

Let’s prepare our self with Top 35 Python Interview Questions and Answers
In your settings.py, you can add your app name in the INSTALLED_APPS construct-
INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'app0'

11. Python Django Admin Interface

Here, in this Python Django Tutorial, we are going to see how to use Django Admin Interface?
Using the Django admin interface, you can perform CRUD operations on the model. This interface depends on
the django.countrib model; it is enabled by default. You can find it as ‘django.contrib.admin’, in
INSTALLED_APPS in settings.py.
To access this interface, you can try one of two methods-

127.0.0.1:8000/admin

localhost:8000/admin

You get something like this:


Python Django Tutorial – Admin Interface

Do you know What is Serialization in Python with Example


a. Creating a Superuser
To create a superuser, you can push in the following command-

python manage.py createsuperuser

Once you’re done creating a superuser, use this username and password to login to the dashboard.

Python Django Tutorial – How to Create a Super use


12. How to Create Simple Project in Django?

We can create a simple view using the following code-

>>> from django.http import HttpResponse

>>> def hello(request):

return HttpResponse("Hello")

We save this file as views.py in the inner project0 directory.


This is how we can import it-

>>> os.chdir('C:\\Users\\lifei\\Desktop\\project0')

>>> from project0.views import hello

a. Mapping to a URL
Now in the inner project0 directory, you have a file urls.py. It looks something like this-

“””project0 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:

https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:

Function views

Add an import: from my_app import views

Add a URL to urlpatterns: path(”, views.home, name=’home’)

Class-based views

Add an import: from other_app.views import Home

Add a URL to urlpatterns: path(”, Home.as_view(), name=’home’)

Including another URLconf

Import the include() function: from django.urls import include, path

Add a URL to urlpatterns: path(‘blog/’, include(‘blog.urls’))

“””
Follow this link to know about Python Flask: A Web Framework for Python
>>> from django.contrib import admin

>>> from django.urls import path

>>> urlpatterns =

[ path('admin/',

admin.site.urls),

In this, the urlpatterns tuple maps URLs to views. Add this line of import to urls.py:
from project0.views import hello

And then add this value to your urlpatterns tuple:

path('hello/',hello),

Best Python Django Tutorial – Mapping to a URL

Now, go to the following address- http://127.0.0.1:8000/hello/


Best Python Django Tutorial For Beginners

So, this was all about Python Django Tutorial. Hope you like our explanation.

13. Conclusion

Hence, in this Python Django Tutorial, we get started with Django, a very common framework with Python.
Here, we studied History & Features of Django. In addition, we cover the MVT Pattern, Prerequisites to create a
Project, and many more. Still, you have a query regarding Python Django Tutorial, leave comments below.

Python Forensics | Hash Function, Virtualization & much more


by DataFlair Team · September 27, 2018

1. Python Forensics

Investigating and analyzing to gather and preserve evidence from a computing device lies at the heart of cyber
forensics. This should be presentable in a court of law. With its wide applications, Python also finds good use
in digital forensics. With it, we can extract evidence, gather data, and also encrypt passwords. It will help us
preserve the integrity of evidence. In this tutorial on Python Forensics, we will learn Naming Conventions,
Hash Functions, Cracking an Encryption, Virtualization, Network Forensics, Dshell and Scapy, Searching,
Indexing, Python Imaging Library and Mobile Forensics with Detailed Explanation.
Python Forensics

If you’re new to Python, however, you should begin today with A Python Introduction and then if you face
any queries on Python Forensics, please comment. So, let’s begin with the Python Forensics.

2. Introduction to Computational Forensics

A quantitative approach to the methodology of the forensic sciences, Computational Forensics(CF) helps study
and solve problems in various forensic disciplines. This is using computer-based modeling, computer
simulation, analysis, and recognition.
On the basis of pattern evidence, such as toolmarks, fingerprints, shoeprints, and documents, it makes use of a
gamut of objects, processes, and substances. It also involves physiological and behavioral patterns, DNA,
digital evidence and crime scenes.
We can make use of algorithms dealing with signal and image processing, computer vision, computer graphics,
data mining, data visualization, statistical pattern recognition, machine learning, and robotics.
But how is this different from computer forensics? While computer forensics studies digital evidence,
computational forensics deals with various types of evidence.

3. Naming Conventions for a Basic Python Forensics Application

In order to follow Python Forensics guidelines to build a basic application, we must follow certain naming
conventions and patterns. Take a look at the following table:

Naming Convention Example


Constants Uppercase; words separated by underscores SPEED_LIMIT
Local variable camelCase with optional underscores currentSpeed
Global variable Prefix gl_with camelCase with optional underscores gl_maximumSpeed
Function PascalCase with optional underscores; active voice ConvertToMilesPerHour(…)
Object Prefix ob_ with camelCase ob_mySpeedrecorder
Module Prefix _ with camelCase _speedRecorder
Class Prefix class_ with PascalCase; keep it brief class_SpeedSystem

Consider a hashing algorithm to encrypt data. This is one-way and takes as input a stream of binary data. Now
considering real-life situations, this could be a password or a file, or even binary or other kinds of digital data.
The algorithm takes this input and produces a message digest(md). These digests are unique, and no two inputs
will generate the same. Take a demo:

import sys,string,md5

print("Enter full name")

line=sys.stdin.readline()

line=line.rstrip()

md5_object=md5.new()

md5_object.update(line)

print(md5_object.hexdigest())

exit

This program makes use of the md5 hashing algorithm. It takes your full name, encrypts it, and secures it. Next
in the Python Forensics tutorial we introduce you to the concept of Hash Functions.

4. Python Hash Functions

A hash function maps a large amount of data to a fixed value, into a specified length. An input always delivers
the same output. This is a hash sum and it holds a characteristic with specific information.

Since it is practically impossible to revert a hash function, you’ll rarely find a third-party attack (like brut-force)
on it. This is why we also call it a one-way cryptographic algorithm.
Take a look at this code:

>>> import uuid

>>> import hashlib

>>> def hash_password(password):

salt = uuid.uuid4().hex

return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt

>>> def check_password(hashed_password, user_password):

password, salt = hashed_password.split(':')

return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()


>>> new_pass = input('Enter required password ')

Please enter required password ayushi

>>> hashed_password = hash_password(new_pass)

>>> print('The string to store in the db is: ' + hashed_password)

The string to store in the db is:


b1076bdba4cd3f71b927a7d43b8c0c6b767cf0b310c2371a192572f7f671f271:17de37c5292f4bbc88e74acca7cde
fb2

>>> old_pass = input('Enter new password ')

Re-enter new password ayu$hi

>>> if check_password(hashed_password, old_pass):

print(‘You entered the correct password’)

else:

print(‘Passwords do not match’)

Passwords do not match


This is the flowchart for this code:
Python Forensics – Hash Function

In the ideal cryptographic hash function:

We can easily compute the hash value for any given input

It is infeasible to generate the original input from a given hash value

It is infeasible to modify the input without changing the hash value

It is infeasible to find two different inputs with the same hash value

Read: Functions in Python Programming Language

5. How to Crack an Encryption in Python?

We must crack the text data we fetch during analysis and evidence. Let’s discuss some basic cryptographic
terminology before that.

Plain text is the original message in human-readable format. Cipher text is what an encryption algorithm turns
this pain text into. Consider the Caesar cipher by Julius Caesar to save the secret text from his enemies. Here,
we take each letter in the plain text and shift it three places in the alphabet. It will turn each A to a D, each B to
an E, and so on.
Python Forensics – Cracking an Encryption

The kinds of pattern evidences we use are:

Tire Tracks and Marks

Impressions

Fingerprints

We crack the vector data in such biometric data to collect fool-proof evidence. Let’s take an example.

>>> import sys

>>> def decrypt(k,cipher):

plaintext=''

for each in cipher:

p = (ord(each)-k) % 126

if p < 32:

p+=95

plaintext += chr(p)

print(plaintext)

>>> cipher = input('Enter message: ')

Enter message: Ayushi


>>> for i in range(1,95,1):

decrypt(i,cipher)

g
f

}~

|}

{|

z{

yz

xy

wx

vw

u
uv

tu

~s

~st

}r

}rs

~|

~|q

~|qr

}{

}{p

}{pq

|z

|zo

|zop

{y

{yn

{yno

~
~z

~zx

~zxm

~zxmn

}y

}yw

}ywl

}ywlm

|x

|xv

|xvk

|xvkl

{w

{wu

{wuj

{wujk

zv

zvt

zvti

zvtij

>>>

Read: Packages in Python


6. Virtualization

When we emulate IT systems like servers, workstations, networks, and storage, it is an act of virtualization. It is
creating a virtual instance of such a resource. The hypervisor helps emulate such virtual hardware.

Python Forensics – Virtualization

So what do we use it for in computational forensics? Here’s where we use it:

For each investigation, we can use the workstation in a validated state.

Attaching the dd image of a drive as a secondary drive on a virtual machine, we can recover data.

We can also use the machine as a recovery software to gather evidences.

This is how we can create a virtual machine using Python:


Step 1
Let’s call our machine ‘dummy’. Each VM shall have at least 512 MB of memory, expressed in bytes.

vm_memory = 512 * 1024 * 1024

Step 2
Attach this VM to the default cluster.

vm_cluster = api.clusters.get(name = "Default")

Step 3
Boot the VM from the virtual HDD.

vm_os = params.OperatingSystem(boot = [params.Boot(dev = "hd")])

We then combine all options into a VM parameter object. Finally, we call the add method of the vms collection
to the VM.
Let’s take an example.

from ovirtsdk.api import API #importing API library


from ovirtsdk.xml import params

try: #Api credentials is required for virtual machine

api = API(url = "https://HOST",

username = "Ayushi",

password = "abc123",

ca_file = "ca.crt")

vm_name = "dummy"

vm_memory = 512 * 1024 * 1024 #calculating the memory in bytes

vm_cluster = api.clusters.get(name = "Default")

vm_template = api.templates.get(name = "Blank")

#assigning the parameters to operating system

vm_os = params.OperatingSystem(boot = [params.Boot(dev = "hd")])

vm_params = params.VM(name = vm_name,

memory = vm_memory,

cluster = vm_cluster,

template = vm_template

os = vm_os)

try:

api.vms.add(vm = vm_params)

print("Virtual machine '%s' added." % vm_name #output if it is successful)

except Exception as ex:


print("Adding virtual machine '%s' failed: %s" % (vm_name, ex))

api.disconnect()

except Exception as ex:

The output:
Virtual machine ‘dummy’ added

7. Network Forensics in Python

In modern Python Forensics network environments, investigating can face a number of difficulties. Imagine
responding to a breach support, investigating insider activities, validating regulatory compliances, or
performing assessments pertaining to vulnerability. Let’s discuss some basic terminology for network
programming.

Python Forensics – Network Forensics

Client- The part of the client-server architecture that runs on a personal computer and workstation.

Server- The part of the client-server architecture that provides services to different computer programs in
the same or other computers.

WebSockets- A protocol between the client and the server that runs over a persistent TCP connection. Using
this, it is possible to send bi-directional messages between the TCP socket connection.

Using these protocols, we can validate information sent to or received by third-party users. But we must also
secure the channel since we use encryption.
Let’s take a look at a program that a client uses for handshaking:
>>> import socket

# create a socket object

>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# get local machine name

>>> host = socket.gethostname()

>>> port = 8080

# connection to hostname on the port.

>>> s.connect((host, port))

# Receive no more than 1024 bytes

>>> tm = s.recv(1024)

>>> print("The client waits for connection")

>>> s.close()

Output:
The client waits for connection
Before moving forward with the Python Forensics tutorial, you should read up on Python Modules.

8. Python Scapy & Dshell

a. Python Dshell
This is a network forensic analysis toolkit based on Python. The US Army Research Laboratory developed it
and released it open-source in 2014. This toolkit makes forensic investigation easy. We have the following
decoders in Dshell:

dns: Extract DNS-related queries

reservedips: Identify solutions for DNS problems

large-flows: List netflows

rip-http: Extract files from HTTP traffic

Protocols: Identify non-standard protocols

You can access the clone repository on GitHub:


https://github.com/USArmyResearchLab/Dshell

b. Python Scapy
A Python-based tool to analyze and manipulate network traffic, you can find Scapy here:
http://www.secdev.org/projects/scapy/
With Scapy, you can analyze packet manipulation. You can also decode and capture packets of a wide number
of protocols. Unlike Dshell, it provides a detailed report about network traffic to the investigator. It can also use
third-party tools or OS fingerprinting to plot.

import scapy, GeoIP #Imports scapy and GeoIP toolkit

from scapy import *

geoIp = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) #locates the Geo IP address

def locatePackage(pkg):

src = pkg.getlayer(IP).src #gets source IP address

dst = pkg.getlayer(IP).dst #gets destination IP address

srcCountry = geoIp.country_code_by_addr(src) #gets Country details of source

dstCountry = geoIp.country_code_by_addr(dst) #gets country details of destination

print src+"("+srcCountry+") >> "+dst+"("+dstCountry+")\n"

Output:
D:\Python code>python dshell.py
src INDIA >> dst USA

9. Python Forensics – Searching

With a keyword from the message, we can search for evidence. And with some expertise and knowledge, we
know what to search in a file, and also what to search in deleted files.
Python helps us with this with its standard library modules. With searching, we can find answers to questions
like ‘who’, ‘what’, ‘where’, and ‘when’.
Let’s take a Python example to find a substring.

>>> s1="He came by for blood and milk"

>>> s2="blood"

>>> s1.find(s2)

15

>>> s1.find(s2,10)

15

>>> s1.find(s2,20)

-1
10. Python Forensics – Indexing

In Python Forensics, with indexing, we can gather potential evidence from a file, a disk image, a network trace,
or a memory snapshot.
Through indexing, we can search a keyword, and carry out interactive searching using the index to rapidly
locate keywords. We can also use it to list keywords in a sorted list.

>>> groceries=['rope','milk','knife']

>>> groceries.index('knife')

>>> groceries.index('rope')

>>> s1.index(s2)

15

11. Python Imaging Library

Both simple data structures like databases and complex ones like JPEG images hold data. We can access the
simple ones using simple desktop tools, and the complex ones using sophisticated programming tools.

With the PIL, we can process images using the Python interpreter. It supports a gamut of file formats; download
the source files from:

http://www.pythonware.com/products/pil/

This is how we extract data from images:


Python Forensics – Imaging Library

Let’s demonstrate this on an image of penguins.


Python Forensics – Penguin Demonstration

Use PIL to open this image:


from PIL import Image

>>> im = Image.open('Capture.jpeg', 'r')

>>> pix_val = list(im.getdata())

>>> pix_val_flat = [x for sets in pix_val for x in sets]

>>> print pix_val_flat

This notes necessary points, including pixel values.


Output:
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255]

12. Mobile Forensics in Python

Although considered non-standard in digital investigations, we can make use of smartphones to ease our
process.
Through proper investigation in Python Forensics, we may search for received calls or calls made. We can also
extract messages, photos, and other evidence. Let’s see how to get through a lockscreen to extract data.
In Android, we can use a PIN or an alphanumeric password. This can be between 4 and 16 digits/characters.
The smartphone stores this inside a file password.key in /data/system. Android stores a salted SHA1-hashsum
and MD5-hashsum of this password. Let’s try processing this.

>>> public byte[] passwordToHash(String password) {

if (password == null) {

return null;

String algo = null;

byte[] hashed = null;

try {
byte[] saltedPassword = (password + getSalt()).getBytes();

byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);

byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);

hashed = (toHex(sha1) + toHex(md5)).getBytes();

} catch (NoSuchAlgorithmException e) {

Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);

return hashed;

We can’t crack this using a dictionary attack, since the hashed password is in a salt file. This is a string of a 64-
bit random integer represented hexadecimally. This is all about Python Forensics.

13. Conclusion

It only comes with practice, so don’t forget to sharpen your blades. An investigation is only as good as an
investigator. Let us know of any queries in the comments on this article on Python Forensics.

Python Network Programming – What is Socket Programming in


Python
by DataFlair Team · September 27, 2018

1. Python Socket Programming


In this Python tutorial, we are going to study Python Network Programming. With Python, we can access an
operating system’s socket support. This will let you implement clients and servers for connection-oriented and
connectionless protocols. Moreover, this Python 3 tutorial gave you an understanding of Socket programming
in Python with vocabulary and examples. Along with this we will learn Python socket Module and Python
socket Methods.

So, let’s begin Networking in Python 3.


Python 3 Network Programming

2. Introduction to Python Network Programming


To learn Python Network Programming, first begin with Python Socket Programming.
Consider a bidirectional communication channel. Its end-points are what we call sockets. Sockets may
communicate in one of the following ways:

Within a process

Between processes on the same machine

Between processes on different machines

Learn Python Closure – Nested Functions and Nonlocal Variables

3. Python Socket Vocabulary


Let’s take a look at all we talk about when we talk sockets.
Python 3 Network Programming- Python Socket vocublury

a. Domain
For transport, we use protocols like AF_INET, PF_INET, PF_UNIX, and PF_X25 among others. This family of
protocols is the domain.

b. Type
Communication between two endpoints may typically be of type SOCK_DGRAM for connectionless protocols
and SOC_STREAM for connection-oriented ones.

c. Protocol
This identifies the protocol used within a domain and type. This is typically zero.

d. Port
Servers listen to one or more ports for client calls. But what values can a port take? A Fixnum port number, a
service name, or a string holding the port number.

e. Hostname
A hostname is what identifies a network interface. This can be a string holding a hostname, a dotted-quad
address, or an IPv6 address. This can also be a zero-length string, an Integer, or a string “<broadcast>”.
We can implement a socket over different channel types- like TCP and UDP. We can also use the socket library
to handle transport.
Read Python Modules Vs Packages

4. Python Socket Module


Let’s first import the Python socket module for this.

>>> import socket

>>>

Now, we can use the socket.socket(socket_family,socket_type,protocol=0) function to create a socket.

>>> mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

>>> mysocket

<socket.socket fd=1524, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>

Here, socket_family may take one of the values AF_UNIX and AF_INET. socket_type may be
SOCK_STREAM or SOCK_DGRAM. protocol defaults to zero.

5. Python Socket Methods


Now, we may call any of these methods on this object we just created.

Python Network Programming- Python Socket Methods

a. Server Socket Methods


i. s.bind()
This binds the address to the socket. This address holds the hostname and the port number pair.

ii. s.listen()
This starts the TCP listener.

iii. s.accept()
This method passively accepts the TCP client connection, and blocks until the connection arrives.

b. Client socket methods

i. s.connect()
This actively initiates TCP server connection.
And

c. General socket methods

i. s.send()
This sends the TCP message.

ii. s.sendto()
This sends the UDP message.

iii. s.recv()
This receives the TCP message.

iv. s.recvfrom()
This receives the UDP message.

v. s.close()
This method closes the socket.

vi. socket.gethostname()
This returns the hostname.

a. Examples-Server

Let’s first try implementing a simple server.


>>> import socket

>>> myserver=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

>>> host=socket.gethostname()

>>> port=9999

>>> myserver.bind((host,port))

>>> myserver.listen(5) #This asks for permission on Windows

>>> while True:

myclient,addr=myserver.accept()

print(f"Connected to {str(addr)}")

myclient.send(msg.encode("ascii"))

myclient.close()

b. Examples-Client

Now, let’s try implementing a client.

>>> import socket

>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

>>> host=socket.gethostname()

>>> port=9999

>>> s.connect((host,port))

>>> msg=s.recv(1024)

>>> s.close()

>>> print(msg.decode("ascii"))

6. Other Internet Modules


There are some other modules, now let us work with networks:

httplib, urllib, xmlrpclib- For the protocol HTTP, dealing with web pages, on port 80.

nntplib- For protocol NNTP, dealing with Usenet news, on port 119.

ftplib, urllib– For protocol FTP, dealing with file transfers, on port 20.
smtplib– For protocol SMTP, dealing with sending email, on port 25.

poplib- For protocol POP3, for fetching email, on port 110.

imaplib- For protocol IMAP4, for fetching email, on port 143.

telnetlib- For protocol Telnet, for dealing with command lines, on port 23.

gopherlib, urllib– For protocol Gopher, for dealing with document transfers, on port 70.

7. Exceptions Thrown by Socket Programming in Python


The socket module may throw one of the following exceptions:

a. exception socket.error

This represents a socket-related error.

b. exception socket.herror

This represents an address-related error.

c. exception socket.gaierror

This represents an address-related error.

d. exception socket.timeout

This occurs when a socket times out.

So, this was all about Python Network Programming in today’s Python Tutorial. Hope you like our explanation.

8. Conclusion
Hence, we discussed basics of Python Network programming. Basically, an introduction to Python Socket
Programming, Python Socket Vocabulary. Moreover, we discussed Socket Module and Socket methods with
the help of examples. Lastly, we saw other internet Modules and Exceptions thrown by Python Socket.
Furthermore, if you have any query, feel free to ask in the comment Section.

Image Processing with SciPy and NumPy in Python


by DataFlair Team · November 16, 2018

1. Objective

In this Python tutorial, we will use Image Processing with SciPy and NumPy. We will deal with reading and
writing to image and displaying image. We will cover different manipulation and filtering images in Python.
Along, with this we will discuss extracting features.
So, let’s discuss Image Processing with SciPy and NumPy.

Image Processing with SciPy and NumPy

2. Prerequisite for Image Processing with SciPy and NumPy

For image processing with SciPy and NumPy, you will need the libraries for this tutorial. We checked in the
command prompt whether we already have these:
Let’s Revise Range Function in Python – Range() in Python

C:\Users\lifei>pip show scipy

Name: scipy
Version: 1.0.0
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author: SciPy Developers
Author-email: scipy-dev@python.org
License: BSD
Location: c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages
Requires: numpy
Required-by:

C:\Users\lifei>pip show numpyName: numpy

Version: 1.14.1
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: numpy-discussion@python.org
License: BSD
Location: c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages
Requires:
Required-by: scipy, pandas, matplotlib
If you don’t already have them installed, use the following commands:

pip install scipy

pip install numpy

Also, some methods like imsave() did not show up with scipy in our interpreter. So, we used pip to install
Pillow. Works for us.

C:\Users\lifei>pip install Pillow

Collecting Pillow
Downloading
https://files.pythonhosted.org/packages/bc/6d/40ad1421a015c3710b329e99842ece044492cc5ff51159b5b23558
000aad/Pillow-5.1.0-cp36-cp36m-win32.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 499kB/s
Installing collected packages: Pillow
Successfully installed Pillow-5.1.0

C:\Users\lifei>pip install matplotlib

Collecting matplotlib
Downloading
https://files.pythonhosted.org/packages/c6/e0/1f98872856dcbd042220a142b5a2b4cb9a8f0efe959ae20f6cb73bd
9f3e6/matplotlib-2.2.2-cp36-cp36m-win32.whl (8.5MB)
100% |████████████████████████████████| 8.5MB 448kB/s
Requirement already satisfied: numpy>=1.7.1 in c:\users\lifei\appdata\local\programs\python\python36-
32\lib\site-packages (from matplotlib) (1.14.1)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
Downloading
https://files.pythonhosted.org/packages/6a/8a/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b0
26a50/pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 61kB 222kB/s
Collecting cycler>=0.10 (from matplotlib)
Downloading
https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c8
34e61/cycler-0.10.0-py2.py3-none-any.whl
Requirement already satisfied: pytz in c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-
packages (from matplotlib) (2018.3)
Collecting kiwisolver>=1.0.1 (from matplotlib)
Downloading
https://files.pythonhosted.org/packages/fd/59/8742e2c77c852e09f0d409af42ccc4165120943ba3b52d57a3ddc56
cb0ca/kiwisolver-1.0.1-cp36-none-win32.whl (44kB)
100% |████████████████████████████████| 51kB 610kB/s
Requirement already satisfied: six>=1.10 in c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-
packages (from matplotlib) (1.11.0)
Requirement already satisfied: python-dateutil>=2.1 in c:\users\lifei\appdata\local\programs\python\python36-
32\lib\site-packages (from matplotlib) (2.7.2)
Requirement already satisfied: setuptools in c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-
packages (from kiwisolver>=1.0.1->matplotlib) (39.2.0)
Installing collected packages: pyparsing, cycler, kiwisolver, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0
Let’s get to the Desktop:

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

Read About Methods in Python – Classes, Objects and Functions in Python

3. How to Read and Write to Image in Python?


Before we start image processing with SciPy and NumPy, we must be able to open it. This is how we opn it:

>>> from scipy import misc

>>> f=misc.face()

>>> misc.imsave('demo.png',f)

Image Processing with SciPy and NumPy

imsave needs you to have the library PIL installed in your system. It lets you save an array as an image. This
creates an image on our Desktop.

Now, we import pyplot from matplotlib.

>>> import matplotlib.pyplot as plt

>>> plt.imshow(f)

<matplotlib.image.AxesImage object at 0x0864E050>

>>> plt.show()

This makes the following image of a raccoon face show up:


Image Processing with SciPy and NumPy – Reading and Writing to Images

Read About Python Sets and Booleans with Syntax and Examples

a. Creating a Python numpy array


Continuing where we left:

>>> f=misc.imread('demo.png')

>>> type(f)

<class 'numpy.ndarray'>

>>> f.shape #This gives us the size of the array. The image is 1024x768.

(768, 1024, 3)
>>> f.dtype

dtype('uint8') #This means it is an 8ibit image (0-255)

b. Opening a RAW file


A RAW file is one with the extension .raw. It is a camera image file holding minimally processed data from an
image sensor. The sensor can be of a digital camera, a motion picture film scanner, or an image scanner.
Let’s first create such a file.

>>> f.tofile('demo.raw')

Open a raw file

This creates the following icon on the Desktop:

Now, we import another module- np.

>>> import np

>>> fromraw=np.fromfile('demo.raw',dtype=np.uint8)

>>> fromraw.shape

(2359296,)

>>> fromraw.shape=(768, 1024, 3)

For much larger data, we can use memory mapping:

>>> memmap=np.memmap('demo.raw',dtype=np.uint8,shape=(768,1024,3))

This reads data from the file but does not load it into memory.
Let’s Read About Python Date and Time – Syntax and examples

c. Working on more than one image at once


>>> for i in range(7):

im=np.random.randint(0,256,10000).reshape((100,100))

misc.imsave('random_%02d.png'%i,im)

>>> from glob import glob


>>> filelist=glob('random*.png')

>>> filelist.sort()

Get to your Desktop to find the following image files:

SciPy and NumPy

Let’s Learn Python Strings with String Functions and String Operations
4. How to Display an Image in Python?

>>> f1=misc.face(gray=True) #For a grayscale image

>>> plt.imshow(f1,cmap=plt.cm.gray)

<matplotlib.image.AxesImage object at 0x0919E810>

We can use min and max values to increase contrast:

>>> plt.imshow(f1,cmap=plt.cm.gray,vmin=30,vmax=200)

<matplotlib.image.AxesImage object at 0x0919E9B0>

>>> plt.axis('off') #This removes axes and ticks

(-0.5, 1023.5, 767.5, -0.5)

a. Drawing Contour Lines


We call the contour() method to draw contour lines on the image.

>>> plt.contour(f1,[50,200])

<matplotlib.contour.QuadContourSet object at 0x019954F0>

Image Processing with SciPy and NumPy – Drawing Contour Lines

b. Interpolation
Adjusting the ‘interpolation’ argument, we can adjust the intensity variation. By setting it to ‘bilinear’, we get
smooth intensity variations, and by setting it to ‘nearest’, we get a fine inspection of intensity variations.
Do You Know About Python Decision Making Statements with Syntax and Examples

>>> plt.imshow(f1[320:340,510:530],cmap=plt.cm.gray,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x01F2FB90>

>>> plt.imshow(f1[320:340,510:530],cmap=plt.cm.gray,interpolation='nearest')
<matplotlib.image.AxesImage object at 0x019954B0>

Image Processing with SciPy and NumPy – Interpolation

5. Basic Manipulations for Images in Python

Let’s look at images as arrays and use numpy to handle them.

Image Processing with SciPy and NumPy – Manipulations for Images

>>> face=misc.face(gray=True)

>>> face[0,40]

127
>>> face[10:13,20:23]

array([[141, 153, 145],

[133, 134, 125],

[ 96, 92, 94]], dtype=uint8)

>>> face[100:120]=255

>>> lx,ly=face.shape

>>> X,Y=np.ogrid[0:lx,0:ly]

>>> mask=(X-lx/2)**2+(Y-ly/2)**2>lx*ly/4

>>> face[mask]=0

>>> face[range(400),range(400)]=255

This gives us the following result:

Image Processing with SciPy and NumPy

Have a Look at Python Generators and Generator Expressions in Detail

a. Statistical Information
We can use the max() and min()functions to return the maximum and minimum along a given axis. The
function mean() returns the average of the array elements along given axis.

>>> face=misc.face(gray=True)

>>> face.mean()

113.48026784261067

>>> face.max()
250

>>> face.min()

b. Geometrical Transformations
We can rotate, crop, and flip an image using scipy.

>>> face=misc.face(gray=True)

>>> lx,ly=face.shape

>>> crop=face[lx//4:-lx//4,ly//4:-ly//4]

>>> flip=np.flipud(face)

>>> from scipy import ndimage

>>> rotate=ndimage.rotate(face,45)

>>> rotate_noreshape=ndimage.rotate(face,45,reshape=False)

Image Processing with SciPy and NumPy – Geometrical Transformations

Read More About Errors and Exceptions in Python Programming

6. How to Filter an Image in Python?

We can also blur/smooth, sharpen, and denoise images. Through local filters, we can use a function of values of
the neighboring pixels to replace the value of a pixel.
Image Processing with SciPy and NumPy – Filtering Images

a. Blurring/Smoothing Effect
We can either use a Gaussian filter or a uniform filter:

>>> face=misc.face(gray=True)

>>> blurred=ndimage.gaussian_filter(face,sigma=3)

>>> way_blurred=ndimage.gaussian_filter(face,sigma=5)

>>> local_mean=ndimage.uniform_filter(face,size=11)

Image Processing with SciPy and NumPy – Blurring/Smoothing

b. Sharpening Effect
We can sharpen a blurred image as:

>>> face=misc.face(gray=True).astype(float)

>>> blurred=ndimage.gaussian_filter(face,3)
Do you know Comparison Between Python Iterators and Generators
We can also add a Laplacian approximation to increase the weight of the edges:

>>> filter_blurred=ndimage.gaussian_filter(blurred,1)

>>> alpha=30

>>> sharpened=blurred+alpha*(blurred-filter_blurred)

c. Denoising Effect
>>> f=misc.face(gray=True)

>>> f=f[230:290,220:320]

>>> noisy=f+0.4*f.std()*np.random.random(f.shape)

This gives us a noisy face. To smoothe noise and the edges, we use a Gaussian filter:

>>> gauss_denoised=ndimage.gaussian_filter(noisy,2)

To preserve the edges, we use a median filter:

>>> median_denoised=ndimage.median_filter(noisy,3)

Image Processing with SciPy and NumPy – Denoising

For figures with straight boundaries and low curvature, a median filter provides a better result:

>>> im=np.zeros((20,20))

>>> im[5:-5,5:-5]=1

>>> im=ndimage.distance_transform_bf(im)

>>> im_noise=im+0.2*np.random.randn(*im.shape)

>>> im_med=ndimage.median_filter(im_noise,3)
Image Processing with SciPy and NumPy – Denoising

Do You Know About Python Comment, Indentation and Statement

7. Extracting Features

We can also extract features in our images like detecting edges and carry out segmentation. Let’s see how.

a. Edge Detection
>>> im=np.zeros((256,256))

>>> im[64:-64,64:-64]=1

>>> im=ndimage.rotate(im,15,mode='constant')

>>> im=ndimage.gaussian_filter(im,8)

For high-intensity variations, we can use Sobel, a gradient operator-

Image Processing with SciPy and NumPy – Edge Detection

>>> sx=ndimage.sobel(im,axis=0,mode='constant')
>>> sy=ndimage.sobel(im,axis=1,mode='constant')

>>> sob=np.hypot(sx,sy)

b. Segmentation
>>> n,l=10,256

>>> im=np.zeros((l,l))

>>> np.random.seed(1)

>>> points=l*np.random.random((2,n**2))

>>> im[(points[0]).astype(np.int),(points[1]).astype(np.int)]=1

>>> im=ndimage.gaussian_filter(im,sigma=l/(4.*n))

>>>

>>> mask=(im>im.mean()).astype(np.float)

>>> mask+=0.1*im

>>> img=mask+0.2*np.random.randn(*mask.shape)

>>>

>>> hist,bin_edges=np.histogram(img,bins=60)

>>> bin_centers=0.5*(bin_edges[:-1]+bin_edges[1:])

>>>

>>> binary_img=img>0.5

Image Processing with SciPy and NumPy


Let’s Explore Python Modules vs Packages
So, this was all about Image Processing with SciPy and NumPy. Hope you like our
explanation.

8. Conclusion

Hence, we learned about Image Processing with SciPy and NumPy. In addition, we talked about Prerequisite for
image processing, Reading and Writing to an image, manipulation in images. At last, we studied Filtering
images: blurring, sharping etc. Furthermore, if you have doubt, feel free to ask in the comment section.

Python Send Email Via SMTP | SMTP Server


by DataFlair Team · September 27, 2018

1. Objective
In this tutorial of Python Programming Language, we will learn how Python send Email via SMTP Server.
Moreover, we will look at Python SMTP Server. Along with this, we will also discuss the working of SMTP
server for sending a mail in Python. Also, we will look at Python send Email example. We will use the Python
module smtplib for this. At last, we are going to discuss how Python send HTML Email using SMTP.
So, let’s discuss the process of sending mail in Python via SMTP.

Sending Mail with Python 3 via SMTP

2.What is SMTP Server?


Simple Mail Transfer Protocol is an application layer protocol in the OSI model. It lets a user send mail to
another. Since this is a push protocol, we can use it to send a mail. At the receiver, this mail is retrieved using
protocols POP (Post Office Protocol) and IMAP (Internet Message Access Protocol).
Python Send Email- SMTP Server

We need to start a server that is always listening for a request. As a client, we open a TCP connection to this
server and then send the mail. When the server listens for a TCP connection from a client, it initiates a
connection on port 587.
Have a look at Python Packages Comprehensive Guide

3.Python Send Email Using smtplib Module


We’ll use the smtplib module for sending Python Email. This is our Python SMTP script to send mail:

>>> import smtplib

>>> sender='thesender@gmail.com'

>>> receiver='whicheverreceiver@gmail.com'

>>> password=’<put your password here>'

>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)

>>> smtpserver.ehlo()

>>> smtpserver.starttls()

>>> smtpserver.ehlo

>>> smtpserver.login(sender,password)

>>> msg='Subject:Demo\nThis is a demo

>>> smtpserver.sendmail(sender,receiver,msg)
>>> print('Sent')

>>> smtpserver.close()

Save this as a .py script, and run this to find this output:

>>>

RESTART:

C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py Sent

This may make your Gmail ask you for access to less secure apps if you’re using Gmail. You will need to turn
this ON temporarily for this to work.
Python Send Email using smtplib Module

4. How Does SMTP Work?


First, we import smtplib.

>>> import smtplib

Then, we set three strings- the sender’s and receiver’s email addresses and the sender’s password. Put your
password for your mail ID in the string for a password.

>>> sender='thesender@gmail.com'

>>> receiver='whicheverreceiver@gmail.com'

>>> password=’<put your password here>'

Then, we create a server object using smptlib.SMTP(). We use the port 587 here, and the domain
smtp.gmail.com.

>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)

On this, we call the ehlo() method for Extended Hello. This lets the server identify the client, and also tells it
that it must use the ESMTP (Extended SMTP) Protocol.
Read Python Datetime Module with Quick Examples

>>> smtpserver.ehlo()

Then, we call starttls() to use a transport layer security connection.

>>> smtpserver.starttls()

And then we call ehlo() again.

>>> smtpserver.ehlo

Then, we call the login() method on this server object. To this, we pass the parameters sender and password to
let the client gain access from the Gmail server to send a mail.

>>> smtpserver.login(sender,password)

Then, we decide the message to send. To set the subject for the message, we do:

>>> msg='Subject:Demo\nThis is a demo'

Now, we call the method sendmail() to send the mail in Python. To this, we pass sender, receiver, and msg.

>>> smtpserver.sendmail(sender,receiver,msg)

Finally, we print a success receipt to the client and then exit.


>>> print('Sent')

>>> smtpserver.close()

Also, if you type in the incorrect password, you will come across this exception:
Explore Python Variable Scope – Local, Global, Built-in, Enclosed

>>>

RESTART:

C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py Traceback (most recent call last):


File “C:/Users/lifei/AppData/Local/Programs/Python/Python36-32/demomail.py”, line 10, in <module>
smtpserver.login(sender,password) File

“C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 729, in login raise


last_exception File “C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 720, in
login initial_response_ok=initial_response_ok) File

“C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py”, line 641, in auth raise


SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b’5.7.8 Username and
Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials
z62sm38593861pff.57 – gsmt

5. How to Send HTML Email


in Python?
For sending an HTML Email you can also configure your message to be able to embed simple HTML code in
it.
Import smtplib

>>> sender='thesender@gmail.com'

>>> receiver=’whicheverreceiver@gmail.com'

>>> password='<your password>'

>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)

>>> smtpserver.ehlo()

>>> smtpserver.starttls()

>>> smtpserver.ehlo

>>> smtpserver.login(sender,password)
>>> msg="""From: Ayushi

To: Ruchi

MIME-Version: 1.0

Content-type: text/html

Subject:Demo

This is a demo<br/><p align="center">Hi</p><hr/>"""

>>> smtpserver.sendmail(sender,receiver,msg)

>>> print('Sent')

>>> smtpserver.close()

So, we receive this email in the receiving inbox.

SMTP- Sending a HTML E-mail


So, this was all about Python send Email using SMTP server. Hope you like our explanation.
Learn Python Closure – Nested Functions and Nonlocal Variables

6.Conclusion
Hence, in this Python Send Email tutorial, we learn how can we send an email using Python with SMTP
module. Hence, now we have an understanding of SMTP server in Python. Moreover, we discussed the process
of sending a mail using smtplib module. Finally, we saw how to send an HTML email. Furthermore, if you have
any query, feel free to ask in the comment section.

What is Python GUI Programming – Python Tkinter Tutorial


by DataFlair Team · September 27, 2018

1. Python GUI Programming


In this Python tutorial, we will discuss different interfaces that we can use to develop a Python GUI (graphical
user interface). Also, we will see many options for Python GUI Programming, of these, we will focus on Python
Tkinter. Moreover, in this Python Tkinter tutorial for Python 3, we will discuss Tkinter with its 19 kinds of
widgets. Along with this, we are going to learn the Tkinter download process.

So, let’s begin Python GUI Tkinter.

Python GUI Programming with Python Tkinter

2. Alternatives For Python GUI Programming


Following are options for Python GUI Programming. We may use one of the following alternatives for
programming a Python GUI:
Python Lambda Expressions with Syntax and Examples

Options For Python GUI Programming

a. Tkinter
Python ships with the Tk GUI toolkit. Tkinter is an interface to this.

b. PyQt
A Python binding of the cross-platform GUI toolkit Qt, PyQt is implemented as a plug-in for Python.

c. wxPython
wxWidgets is a cross-platform GUI API for Python; wxPython is a wrapper for this.

3. What is the Python Tkinter?


Tkinter in Python GUI Programming is standard Python GUI library. It gives us an object-oriented interface to
the Tk GUI toolkit.

So, let’s start Python graphics, let’s create a simple GUI application:

Import the module Tkinter.


Create the main window for the application.

Add a widget or more.

Enter the main event loop to deal with an event when it is triggered.

7 Reasons Why Should You Learn Python in 2018

>>> import tkinter

>>> top=tkinter.Tk()

>>> top.mainloop()

Now, this creates the following window:

Python GUI Tkinter- Creating Simple GUI Application

4. Python Tkinter Widgets


Tkinter in Python GUI Programming provides 19 kinds of widgets (controls)- buttons, labels, and more. Let’s
discuss these Python Tkinter widgets in detail.

a. Button
You can add a button to your application.

>>> from tkinter import *

>>> top=Tk()

>>> top.geometry('600x200'
''

>>> def helloCallBack():

msg=messagebox.showinfo("Hello, User","Hello, Guest")

>>> B=Button(top,text="Initiate",command=helloCallBack)

>>> B.place(x=300,y=100)

Python GUI Tkinter- Adding Button to application

Now, Clicking on the Initiate button gives us this:

Python GUI Tkinter

b. Canvas
A Canvas in Tkinter Python GUI Programming will let you draw shapes- lines, polygons, ovals, and more.
Explore Python Functions with Syntax and Examples

>>> from tkinter import *

>>> from tkinter import messagebox


>>> top=Tk()

>>> C=Canvas(top,bg="cyan",height=100,width=100)

>>> coord=10,50,90,70

>>> arc=C.create_arc(coord,start=0,extent=97,fill='black')

>>> line=C.create_line(10,10,50,70,fill='white')

>>> C.pack()

Python GUI Tkinter- Canvas

c. Checkbutton
A Checkbutton is an option that a user can choose to select or leave. They can select as many checkboxes at
once as they want to.
Explore 13 Unique Features of Python Programming Language

>>> from tkinter import *

>>> top=Tk()

>>> CheckVar1=IntVar()

>>> CheckVar2=IntVar()

>>> C1=Checkbutton

>>> C1=Checkbutton(top,text="Pizza",variable=CheckVar1,onvalue=1,offvalue=0,height=5,width=20)

>>> C2=Checkbutton(top,text="Fries",variable=CheckVar2,onvalue=1,offvalue=0,height=5,width=20)

>>> C1.pack()

>>> C2.pack()
Python GUI Tkinter- Checkbutton

d. Entry
Next, this lets us add a single-line input tray for taking input from the user.

>>> from tkinter import *

>>> top=Tk()

>>> L1=Label(top,text="Enrolment Number")

>>> L1.pack(side=LEFT)

>>> E1=Entry(top,bd=3)

>>> E1.pack(side=RIGHT)

Python GUI Tkinter- Entry

e. Frame
Generally, a frame is a container widget to help hold other widgets.

>>> from tkinter import *

>>> top=Tk()

>>> frame=Frame(top)

>>> frame.pack()

>>> frametwo=Frame(top)
>>> frametwo.pack(side=BOTTOM)

>>> redbutton=Button(frame,text="One",fg="red")

>>> redbutton.pack(side=LEFT)

>>> bluebutton=Button(frame,text="Two",fg="blue")

>>> bluebutton.pack(side=LEFT)

>>> greenbutton=Button(frametwo,text="Three",fg="green")

>>> greenbutton.pack(side=BOTTOM)

Python GUI Tkinter- Frame

f. Label
Basically, this lets us add a single-line caption for other widgets. We saw this in section 4d.

g. Listbox
This widget lets us add a list of options for the user to choose from.

>>> from tkinter import *

>>> top=Tk()

>>> LB1=Listbox(top)

>>> LB1.insert(1,"Hindi")

>>> LB1.insert(2,"Romanian")

>>> LB1.insert(3,"English")

>>> LB1.insert(4,"Gujarati")

>>> LB1.pack()
Python GUI Tkinter- Listbox

h. Menu button
This lets us add a menu button anywhere in our application.
Python Number Types and Their Conversion Functions

>>> from tkinter import *

>>> top=Tk()

>>> mb=Menubutton(top,text="style",relief=RAISED)

>>> mb.grid()

>>> mb.menu=Menu(mb,tearoff=0)

>>> mb["menu"]=mb.menu

>>> balayageVar=IntVar()

>>> sombreVar=IntVar()

>>> mb.menu.add_checkbutton(label='Balayage',variable=balayageVar)

>>> mb.menu.add_checkbutton(label='Sombre',variable=sombreVar)

>>> mb.pack()
Python GUI Tkinter- Add a Menu Button

Clicking on this button does this:

An output of Adding a Menu Button

i. Menu
This lets us put all kinds of menus in our application.

>>> from tkinter import *

>>> def donothing():

filewin=Toplevel(top)

button=Button(filewin,text="This does nothing")

button.pack()

>>> top=Tk()

>>> menubar=Menu(top)

>>> filemenu=Menu(menubar,tearoff=0)

>>> filemenu.add_command(label="New",command=donothing)

>>> filemenu.add_command(label="Open",command=donothing)

>>> filemenu.add_command(label="Save",command=donothing)

>>> filemenu.add_command(label="Close",command=donothing)

>>> filemenu.add_separator()

>>> filemenu.add_command(label="Exit",command=top.quit)

>>> menubar.add_cascade(label="File",menu=filemenu)

>>> editmenu=Menu(menubar,tearoff=0)
>>> editmenu.add_command(label="Undo",command=donothing)

>>> editmenu.add_separator()

>>> editmenu.add_command(label="Cut",command=donothing)

>>> editmenu.add_command(label="Copy",command=donothing)

>>> editmenu.add_command(label="Paste",command=donothing

>>> editmenu.add_command(label="Delete",command=donothing)

>>> menubar.add_cascade(label="Edit",menu=editmenu)

>>> top.config(menu=menubar)

>>> top.mainloop()

Python GUI Tkinter- Message

Read Loops in Python with Syntax and Examples

k. Radiobutton
When a user has a few choices of radio buttons, they may only choose one at once.

>>> from tkinter import *

>>> def sel():


selection=f"Enjoy your {var.get()}"

label.config(text=selection)

>>> top=Tk()

>>> var=StringVar()

>>> R1=Radiobutton(top,text="pizza slice",variable=var,value='pizza',command=sel

>>> R1.pack(anchor=W)

>>> R2=Radiobutton(top,text="burger",variable=var,value='burger',command=sel)

>>> R2.pack(anchor=W)

>>> R3=Radiobutton(top,text="fries",variable=var,value='fries',command=sel)

>>> R3.pack(anchor=W)

>>> label=Label(top)

>>> label.pack()

Python GUI Programming with Tkinter- Radiobutton

l. Scale
Scale widget provides a slider.

>>> from tkinter import *

>>> def sel():

selection=f"Volume: {str(var.get())}"

label.config(text=selection)

>>> top=Tk()
>>> var=DoubleVar()

>>> scale=Scale(top,variable=var)

>>> scale.pack(anchor=CENTER)

>>> button=Button(top,text="Get scale value",command=sel)

>>> button.pack(anchor=CENTER)

>>> label=Label(top)

>>> label.pack()

Python GUI Tkinter Programming- Scale

m. Scrollbar
This adds scrolling capability to other widgets like lists.

Have a Look At Python Decision Making Statements with Syntax

>>> from tkinter import *

>>> top=Tk()

>>> scrollbar=Scrollbar(top)

>>> scrollbar.pack(side=RIGHT,fill=X)

>>> scrollbar.pack(side=RIGHT,fill=Y)

>>> list=Listbox(top,yscrollcommand=scrollbar.set)

>>> for line in range(22):


list.insert(END,f"Line {str(line)}")

>>> list.pack(side=LEFT,fill=BOTH)

>>> scrollbar.config(command=list.yview)

Python 3 GUI Tkinter- Scrollbar

n. Text
This displays text in multiple lines.

>>> from tkinter import *

>>> top=Tk()

>>> text=Text(top)

>>> text.insert(INSERT,"One.")

>>> text.insert(END,"Two..Three...")

>>> text.pack()

>>> text.tag_add("first","1.0","1.4")

>>> text.tag_add("second","1.10","1.14")

>>> text.tag_config("first",background="cyan",foreground="black")

>>> text.tag_config("second",background="black",foreground="white")
Python GUI Tkinter- Text

o. Toplevel
This provides a separate window container.

>>> from tkinter import *

>>> main=Tk()

>>> main.title("My Piano")

''

>>> new=Toplevel()

>>> new.title("New Song")

''

Tkinter Tutorial Python 3- Toplevel

p. Spinbox
Basically, Spinbox is like an entry field, except it lets us select from a fixed number of values.

Python Variables and Data Types with Syntax and Examples

>>> from tkinter import *


>>> top=Tk()

>>> s=Spinbox(top,from_=0,to=18)

>>> s.pack()

Spinbox

Tkinter Widgets in Python GUI Programming

q. PanedWindow
This is a container widget that can hold any number of panes horizontally or vertically.

>>> from tkinter import *

>>> w1=PanedWindow()

>>> w1.pack(fill=BOTH, expand=1)

>>> left=Entry(w1,bd=5)

>>> w1.add(left)

>>> w2=PanedWindow(w1,orient=VERTICAL)

>>> w1.add(w2)

>>> top=Scale(w2,orient=HORIZONTAL)

>>> w2.add(top)

>>> bottom=Button(w2,text="OK")

>>> w2.add(bottom)
Python GUI Tkinter- PanedWindow

r. LabelFrame
So, this is a container widget holding complex window layouts.

>>> from tkinter import *

>>> top=Tk()

>>> labelframe=LabelFrame(top,text="Lyrics")

>>> labelframe.pack(fill="both",expand="yes")

>>> left=Label(labelframe,text="No lyrics available")

>>> left.pack()

Python GUI Tkinter- LabelFrame

s. tkMessageBox
This widget displays a message box.

>>> from tkinter import *

>>> from tkinter import messagebox

>>> top=Tk()

>>> def clickhere():

messagebox.showinfo("Click here","You just got owned")

>>> button=Button(top,text="Click here",command=clickhere)


>>> button.place(x=50,y=77)

tkMessageBox in Tkinter

Python GUI Programming with Tkinter

So, this was all about Python GUI Programming. Hope you like our explanation of Tkinter Tutorial in Python 3.
Data Structures in Python – Lists, Tuples, Sets, Dictionaries

5. Conclusion
Hence, we completed the Python Tkinter tutorial. Now, it is time to get started with Python GUI programming.
In addition, we saw Python GUI Programming and alternative for Python GUI Programming. Moreover, we
discussed Tkinter and widgets. At last, we learned different kind of widgets that Tinker provides. Furthermore,
for any query regarding Python GUI Programming with Tkinter, feel free to ask in the comment section.

Python Statistics – Python p-Value, Correlation, T-test, KS Test


by DataFlair Team · February 22, 2019

1. Python Statistics
In this Python Statistics tutorial, we will learn how to calculate the p-value and Correlation in Python.
Moreover, we will discuss T-test and KS Test with example and code in Python Statistics.

So, let’s start the Python Statistics Tutorial.

Python Statistics – p-Value, Correlation, T-test, KS Test

2. p-value in Python Statistics


When talking statistics, a p-value for a statistical model is the probability that when the null hypothesis is true,
the statistical summary is equal to or greater than the actual observed results. This is also termed ‘probability
value’ or ‘asymptotic significance’.

Do you know about Python Decorators

The null hypothesis states that two measured phenomena experience no relationship to each other. We denote
this as H or H0. One such null hypothesis can be that the number of hours spent in the office affects the amount
of salary paid. For a significance level of 5%, if the p-value falls lower than 5%, the null hypothesis is
invalidated. Then it is discovered that the number of hours you spend in your office will not affect the amount
of salary you will take home. Note that p-values can range from 0% to 100% and we write them in decimals. A
p-value for 5% will be 0.05.
A smaller p-value bears more significance as it can tell you that the hypothesis may not explain the observation
fairly. If one or more of these probabilities turn out to be less than or equal to α, the level of significance, we
reject the null hypothesis. For a true null hypothesis, p can take on any value between 0 and 1 with equal
likeliness. For a true alternative hypothesis, p-values likely fall closer to 0.

Python p-Value

3. T-test in Python Statistics


Let’s talk about T-tests. Such a test tells us whether a sample of numeric data strays or differs significantly from
the population. It also talks about two samples- whether they’re different. In other words, it gives us the
probability of difference between populations. The test involves a t-statistic. For small samples, we can use a T-
test with two samples.

Python Statistics – Python T-test

Let’s discuss Python Generators


a. One-sample T-test with Python
Let’s try this on a single sample. The test will tell us whether means of the sample and the population are
different. Consider the voting populace in India and in Gujarat. Does the average age of Gujarati voters differ
from that of the population? Let’s find out.

>>> import numpy as np

>>> import pandas as pd

>>> import scipy.stats as stats

>>> import matplotlib.pyplot as plt

>>> import math

>>> np.random.seed(6)

>>> population_ages1=stats.poisson.rvs(loc=18,mu=35,size=150000)

>>> population_ages2=stats.poisson.rvs(loc=18,mu=10,size=100000)

>>> population_ages=np.concatenate((population_ages1,population_ages2))

>>> gujarat_ages1=stats.poisson.rvs(loc=18,mu=30,size=30)

>>> gujarat_ages2=stats.poisson.rvs(loc=18,mu=10,size=20)

>>> gujarat_ages=np.concatenate((gujarat_ages1,gujarat_ages2))

>>> population_ages.mean()

43.000112

>>> gujarat_ages.mean()

39.26

>>> stats.ttest_1samp(a=gujarat_ages,popmean=population_ages.mean())

Ttest_1sampResult(statistic=-2.5742714883655027, pvalue=0.013118685425061678)
Now this value of -2.574 tells us how aberrant the sample mean is from the null hypothesis.

b. Two-sample T-test With Python


Such a test tells us whether two data samples have different means. Here, we take the null hypothesis that both
groups have equal means. We don’t need a known population parameter for this.
Let’s revise Recursion in Python

>>> np.random.seed(12)
>>> maharashtra_ages1=stats.poisson.rvs(loc=18,mu=33,size=30)

>>> maharashtra_ages2=stats.poisson.rvs(loc=18,mu=13,size=20)

>>> maharashtra_ages=np.concatenate((maharashtra_ages1,maharashtra_ages2))

>>> maharashtra_ages.mean()

42.26

>>> stats.ttest_ind(a=gujarat_ages,b=maharashtra_ages,equal_var=False)

Ttest_indResult(statistic=-1.4415218453964938,
pvalue=0.1526272389714945)
The value of 0.152 tells us there’s a 15.2% chance that the sample data is such far apart for two identical
groups. This is greater than the 5% confidence level.

c. Paired T-test With Python


When you want to check how different samples from the same group are, you can go for a paired T-test. Let’s
take an example.

>>> np.random.seed(11)

>>> before=stats.norm.rvs(scale=30,loc=250,size=100)

>>> after=before+stats.norm.rvs(scale=5,loc=-1.25,size=100)

>>> weight_df=pd.DataFrame({"weight_before":before,

"weight_after":after,

"weight_change":after-before})

>>> weight_df.describe()

Paired Sample T-test

>>> stats.ttest_rel(a=before,b=after)
Ttest_relResult(statistic=2.5720175998568284, pvalue=0.011596444318439857)
So, we see we have just 1% chances to find such huge differences between samples.
Do you know about Python Errors

4. KS Test in Python Statistics


This is the Kolmogorov-Smirnov test. It lets us test the hypothesis that the sample is a part of the standard t-
distribution. Let’s take an example.

>>> stats.kstest(x,'t',(10,))

KstestResult(statistic=0.023682909426459897, pvalue=0.6289865281325614)

>>> stats.kstest(x,'norm')

KstestResult(statistic=0.019334747291889, pvalue=0.8488119233062457)
Pay attention to the p-values in both cases.

a. Two samples
What we saw above was the KS test for one sample. Let’s try two.

>>> stats.ks_2samp(gujarat_ages,maharashtra_ages)

Ks_2sampResult(statistic=0.26, pvalue=0.056045859714424606)

5. Correlation in Python Statistics


This is a statistical relationship between two random variables (or bivariate data). This can be causal. It is a
measure of how close two variables are to holding a linear relationship to each other. One such example will be
the correlation between demand and supply for a product whose supply is limited.
Have a look at Exception Handling in Python
Correlation can denote a predictive relationship that we can exploit. To measure the degree of correlation, we
can use constants like ρ or r. Benefits of correlation-

Predicting one quantity from another

Discovering the existence of a causal relationship

Foundation for other modeling techniques

a. Example of Correlation In Python


Let’s take an example.

>>> df=pd.read_csv('furniture.csv',index_col='Serial',parse_dates=True)
>>> df['Gross']=df.Cost+df.Cost*10

>>> df.describe()

Python Statistics – Correlation With Python

>>> df.corr()

Example of Python Correlation

This gives us how each column correlates to another. You can also calculate the covariance in the following
way-

>>> df.cov()

Cost Gross
Cost 3.131608e+08 3.444769e+09
Gross 3.444769e+09 3.789246e+10

b. Plotting Correlation in Python


Let’s use seaborn to plot the correlation between columns of the ‘iris’ dataset.
Let’s revise Python Iterator

>>> import seaborn as sn

>>> df1=sn.load_dataset('iris')

>>> sn.pairplot(df,kind='scatter')

<seaborn.axisgrid.PairGrid object at 0x06294090>

>>> plt.show()
Plotting Correlation in Python

c. Saving the Results


We can export the result of a correlation as a CSV file.

>>> d=df1.corr()

>>> d.to_csv('iriscorrelation.csv')

This gives us the following CSV file-

Python Statistics – Saving the Results

So, this was all in Python Statistics. Hope you like our explanation.

6. Conclusion: Python Statistics


Hence, in this Python Statistics tutorial, we discussed the p-value, T-test, correlation, and KS test with Python.
To conclude, we’ll say that a p-value is a numerical measure that tells you whether the sample data falls
consistently with the null hypothesis. Correlation is an interdependence of variable quantities. Still, if any doubt
regarding Python Statistics, ask in the comment tab.

Python Descriptive Statistics – Measuring Central Tendency &


Variability
by DataFlair Team · September 28, 2018

1. Objective

In our last tutorial, we studied Python Charts. Today, we will learn about Python Descriptive Statistics. In this
Python Statistics tutorial, we will discuss what is Data Analysis, Central Tendency in Python: mean, median,
and mode. Moreover, we will discuss Python Dispersion and Python Pandas Descriptive Statistics. Along with
this, we will cover the variance in Python and how to calculate the variability for a set of values.

So, let’s begin the Python Descriptive Statistics Tutorial.

Python Descriptive Statistics – Measuring Central Tendency & Variability

2. Data Analysis

With data analysis, we use two main statistical methods- Descriptive and Inferential.

Descriptive statistics uses tools like mean and standard deviation on a sample to summarize data.

Inferential statistics, on the other hand, looks at data that can randomly vary, and then draw conclusions
from it.
Some such variations include observational errors and sampling variation.
Do you know about Python Collection Module

3. Descriptive Statistics in Python

Python Descriptive Statistics process describes the basic features of data in a study. It delivers summaries on the
sample and the measures and does not use the data to learn about the population it represents.
Under descriptive statistics, fall two sets of properties- central tendency and dispersion. Python Central
tendency characterizes one central value for the entire distribution. Measures under this include mean, median,
and mode. Python Dispersion is the term for a practice that characterizes how apart the members of the
distribution are from the center and from each other. Variance/Standard Deviation is one such measure of
variability.

4. Python Descriptive Statistics – Central Tendency in Python

We have seen what central tendency or central location is. Now let’s take a look at all the functions Python
caters to us to calculate the central tendency for a distribution. For this, let’s import the Python statistics
module.

>>>import statistics as st

Python Descriptive Statistics – Central Tendency

a. mean()
This function returns the arithmetic average of the data it operates on. If called on an empty container of data, it
raises a StatisticsError.

>>> nums=[1,2,3,5,7,9]
>>> st.mean(nums)

4.5

>>> st.mean([-2,-4,7]) #Negative numbers

0.3333333333333333

>>> from fractions import Fraction as fr

>>> st.mean((fr(3,4),fr(5,7),fr(2,1))) #Fractions

Fraction(97, 84)

>>> st.mean({1:"one",2:"two",3:"three"}) #Keys from a dictionary

2
Do you know the difference between Python Modules vs Packages

b. mode()
This function returns the most common value in a set of data. This gives us a great idea of where the center lies.

>>> nums=[1,2,3,5,7,9,7,2,7,6]

>>> st.mode(nums)

>>> st.mode(['A','B','b','B','A','B'])

‘B’

c. median()
For data of odd length, this returns the middle item; for that of even length, it returns the average of the two
middle items.

>>> st.median(nums) #(5+6)/2

5.5

d. harmonic_mean()
This function returns the harmonic mean of the data. For three values a, b, and c, the harmonic mean is-
3/(1/a + 1/b +1/c)
It is a measure of the center; one such example would be speed.

>>> st.harmonic_mean([2,4,9.7])
3.516616314199396
For the same set of data, the arithmetic mean would give us a value of 5.233333333333333.

e. median_low()
When the data is of an even length, this provides us the low median of the data. Otherwise, it returns the middle
value.

>>> st.median_low([1,2,4])

>>> st.median_low([1,2,3,4])

f. median_high()
Like median_low, this returns the high median when the data is of an even length. Otherwise, it returns the
middle value.

>>> st.median_high([1,2,4])

>>> st.median_high([1,2,3,4])

3
Let’s Learn CGI Programming in Python with Functions and Modules

g. median_grouped()
This function uses interpolation to return the median of grouped continuous data. This is the 50th percentile.

>>> st.median([1,3,3,5,7])

>>> st.median_grouped([1,3,3,5,7],interval=1)

3.25

>>> st.median_grouped([1,3,3,5,7],interval=2)

3.5

5. Python Descriptive Statistics – Dispersion in Python

Dispersion/spread gives us an idea of how the data strays from the typical value.
Python Descriptive Statistics – Dispersion

a. variance()
This returns the variance of the sample. This is the second moment about the mean and a larger value denotes a
rather spread-out set of data. You can use this when your data is a sample out of a population.

>>> st.variance(nums)

7.433333333333334

b. pvariance()
This returns the population variance of data. Use this to calculate variance from an entire population.

>>> st.pvariance(nums)

6.69

c. stdev()
This returns the standard deviation for the sample. This is equal to the square root of the sample variance.

>>> st.stdev(nums)

2.7264140062238043
Read about Python Namespace and Variable Scope – Local and Global Variables

d. pstdev()
This returns the population standard deviation. This is the square root of population variance.
>>> st.pstdev(nums)

2.5865034312755126
The statistics module defines one exception-
exception statistics.StatisticsError
This is a subclass of ValueError.

6. pandas with Descriptive Statistics in Python

We can do the same things using pandas too-

>>> import pandas as pd

>>> df=pd.DataFrame(nums)

>>> df.mean()

0 4.9
dtype: float64
Follow this to know more about Python Pandas

>>> df.mode()

07

>>> df.std() #Standard deviation

0 2.726414
dtype: float64

>>> df.skew()

0 -0.115956 #The distribution is symmetric


dtype: float64
A value less than -1 is skewed to the left; that greater than 1 is skewed to the right. A value between -1 and 1 is
symmetric.

So, this was all about Python Descriptive Statistics Tutorial. Hope you like our explanation.

7. Conclusion

Hence, we studied Python Descriptive Statistics, in which we learned Central Tendency & Dispersion used in
Python Statistics Module. In addition, we used the statistics and pandas modules for this. Did you find it easy to
grasp? Leave your suggestions below.

Python Probability Distributions – Normal, Binomial, Poisson,


Bernoulli
by DataFlair Team · September 28, 2018

1. Python Probability Distributions – Objective

After studying Python Descriptive Statistics, now we are going to explore 4 Major Python Probability
Distributions: Normal, Binomial, Poisson, and Bernoulli Distributions in Python. Moreover, we will learn how
to implement these Python probability distributions with Python Programming.

How to Implement Python Probability Distributions

2. What is Python Probability Distribution?

A probability distribution is a function under probability theory and statistics- one that gives us how probable
different outcomes are in an experiment. It describes events in terms of their probabilities; this is out of all
possible outcomes. Let’s take the probability distribution of a fair coin toss. Here, heads take a value of X=0.5
and tails gets X=0.5 too.
Two classes of such a distribution are discrete and continuous. The former represented by a probability mass
function and the latter by a probability density function.
Do you know about Python Namedtuple

3. How to Implement Python Probability Distributions?


Let’s implement these types of Python Probability Distributions, let’s see them:

a. Normal Distribution in Python


Python normal distribution is a function that distributes random variables in a graph that is shaped as a
symmetrical bell. It does so by arranging the probability distribution for each value. Let’s use Python numpy
for this.

>>> import scipy.stats

>>> import numpy as np

>>> import matplotlib.pyplot as plt

>>> np.random.seed(1234)

>>> samples=np.random.lognormal(mean=1.,sigma=.4,size=10000)

>>> shape,loc,scale=scipy.stats.lognorm.fit(samples,floc=0)

>>> num_bins=50

>>> clr="#EFEFEF"

>>> counts,edges,patches=plt.hist(samples,bins=num_bins,color=clr)

>>> centers=0.5*(edges[:-1]+edges[1:])

>>> cdf=scipy.stats.lognorm.cdf(edges,shape,loc=loc,scale=scale)

>>> prob=np.diff(cdf)

>>> plt.plot(centers,samples.size*prob,'k-',linewidth=2)

[<matplotlib.lines.Line2D object at 0x0359E890>]

>>> plt.show()
Implement Python Probability Distributions – Normal Distribution in Python

b. Binomial Distribution in Python


Python binomial distribution tells us the probability of how often there will be a success in n independent
experiments. Such experiments are yes-no questions. One example may be tossing a coin.
Let’s explore SciPy Tutorial – Linear Algebra, Benefits, Special Functions

>>> import seaborn

>>> from scipy.stats import binom

>>> data=binom.rvs(n=17,p=0.7,loc=0,size=1010)

>>> ax=seaborn.distplot(data,

kde=True,

color='pink',

hist_kws={"linewidth": 22,'alpha':0.77})

>>> ax.set(xlabel='Binomial',ylabel='Frequency')

[Text(0,0.5,’Frequency’), Text(0.5,0,’Binomial’)]

>>> plt.show()

Implement Python Probability Distributions – Binomial Distribution in Python


c. Poisson Distribution in Python
Python Poisson distribution tells us about how probable it is that a certain number of events happen in a fixed
interval of time or space. This assumes that these events happen at a constant rate and also independent of the
last event.

>>> import numpy as np

>>> s=np.random.poisson(5, 10000)

>>> import matplotlib.pyplot as plt

>>> plt.hist(s,16,normed=True,color='Green')

(array([5.86666667e-03, 3.55200000e-02, 8.86400000e-02, 1.48906667e-01,


1.91573333e-01, 1.81440000e-01, 1.56160000e-01, 1.16586667e-01,
6.65600000e-02, 3.90400000e-02, 2.06933333e-02, 9.06666667e-03,
3.84000000e-03, 2.13333333e-03, 5.33333333e-04, 1.06666667e-04]), array([ 0. , 0.9375, 1.875 , 2.8125, 3.75 ,
4.6875, 5.625 ,
6.5625, 7.5 , 8.4375, 9.375 , 10.3125, 11.25 , 12.1875,
13.125 , 14.0625, 15. ]), <a list of 16 Patch objects>)
Read about What is Python Interpreter – Environment, Invoking & Working

>>> plt.show()

Implement Python Probability Distributions – Poisson Distribution in Python

d. Bernoulli Distribution in Python


Python Bernoulli Distribution is a case of binomial distribution where we conduct a single experiment. This is
a discrete probability distribution with probability p for value 1 and probability q=1-p for value 0. p can be for
success, yes, true, or one. Similarly, q=1-p can be for failure, no, false, or zero.
>>> s=np.random.binomial(10,0.5,1000)

>>> plt.hist(s,16,normed=True,color='Brown')

(array([0.00177778, 0.02311111, 0. , 0.08711111, 0. ,


0.18666667, 0. , 0.33777778, 0.45155556, 0. ,
0.37688889, 0. , 0.224 , 0. , 0.07466667,
0.01422222]), array([0. , 0.5625, 1.125 , 1.6875, 2.25 , 2.8125, 3.375 , 3.9375,
4.5 , 5.0625, 5.625 , 6.1875, 6.75 , 7.3125, 7.875 , 8.4375,
9. ]), <a list of 16 Patch objects>)
Do you know about Python Django Tutorial For Beginners

>>> plt.show()

Implement Python Probability Distributions – Bernoulli Distribution in Python

So, this was all about Python Probability Distribution. Hope you like our explanation.

4. Conclusion
Hence, we studied Python Probability Distribution and its 4 types with an example. In addition, we learned how
to implement these Python probability distributions. Furthermore, if you have any doubt, feel free to ask in the
comment section.

Python Linear Regression | Chi-Square Test In Python


by DataFlair Team · September 28, 2018

1. Python Linear Regression – Object


Today, in this Python tutorial, we will discuss Python Linear Regression and Chi-Square Test in Python.
Moreover, we will understand the meaning of Linear Regression and Chi-Square in Python. Also, we will look
at Python Linear Regression Example and Chi-square example.

So, let’s start with Python Linear Regression.

Python Linear Regression | Chi-Square Test In Python

2. Python Linear Regression


Linear regression is a way to model the relationship that a scalar response(a dependent variable) has with
explanatory variable(s)(independent variables). Depending on whether we have one or more explanatory
variables, we term it simple linear regression and multiple linear regression in Python.

Do you know about Python SciPy

To model relationships, we use linear predictor functions with unknown model parameters; we call these
linear models in Python.

We will use Seaborn to plot a Python linear regression here.

a. Python Linear Regression Example


Let’s take a simple example of Python Linear Regression.

>>> import seaborn as sn

>>> import matplotlib.pyplot as plt

>>> sn.set(color_codes=True)
>>> tips=sn.load_dataset('tips')

>>> ax=sn.regplot(x='total_bill',y='tip',data=tips)

>>> plt.show()

Python Linear Regression Example

b. How to Customize the Color in Python Linear Regression?


Now let’s color it green.
Have a look at Python NumPy

>>> import numpy as np

>>> np.random.seed(7)

>>> mean,cov=[3,5],[(1.3,.8),(.8,1.1)]

>>> x,y=np.random.multivariate_normal(mean,cov,77).T

>>> ax=sn.regplot(x=x,y=y,color='g')

>>> plt.show()
Customizing the colour in Linear regression in Python Programming Language

c. Plotting with Pandas Series, Customizing Markers


Now, we’ll use two Python Pandas Series to plot Python linear regression.

>>> import pandas as pd

>>> x,y=pd.Series(x,name='x'),pd.Series(y,name='y')

>>> ax=sn.regplot(x=x,y=y,marker='*')

Customizing the color in Python Linear regression


d. Setting a Confidence Interval
To set the confidence interval, we use the ci parameter. The confidence interval is a range of values that make it
probable that a parameter’s value lies within it.
Let’s discuss Python Heatmap

>>> ax=sn.regplot(x=x,y=y,ci=68)

>>> plt.show()

Setting a Confidence Interval

e. Adding Jitter
You can add some jitter in the x or y directions.

>>> ax=sn.regplot(x='size',y='total_bill',data=tips,y_jitter=.1,color='g')

>>> plt.show()
Adding Jitter in Python Linear Regression

f. Plotting With a Continuous Variable Divided into Discrete Bins


Let’s revise the Python Charts
Let’s create 5 bins and make the plot.

>>> ax=sn.regplot(x=x,y=y,x_bins=5)

>>> plt.show()

Plotting With a Continuous Variable Divided into Discrete Bins


3. What is the Chi-Square Test?
This is a statistical hypothesis test that uses a chi-squared distribution as a sampling distribution for the test
statistic when we have a true null hypothesis. In other words, it is a way to assess how a set of observed values
fits in with the values expected in theory- the goodness of fit.
You must know about Python Data Science
The test tells us whether, in one or more categories, the expected frequencies differ significantly from the
observed frequencies. We also write it as χ2 test. In this test, we classify observations into mutually exclusive
classes. A null hypothesis tells us how probable it is that an observation falls into the corresponding class. With
this test, we aim to determine how likely an observation made is, while assuming that the null hypothesis is true.
This Chi-Square test tells us whether two categorical variables depend on each other.

a. Python Chi-Square Example


Let’s take an example.

>>> from scipy import stats

>>> import numpy as np

>>> import matplotlib.pyplot as plt

>>> x=np.linspace(0,10,100)

>>> fig,ax=plt.subplots(1,1)

>>> linestyles=['--','-.',':','-']

>>> degrees_of_freedom=[1,3,7,5]

>>> for df,ls in zip(degrees_of_freedom,linestyles):

ax.plot(x,stats.chi2.pdf(x,df),linestyle=ls)<span style="font-family: Verdana, Geneva, sans-serif"> </span>

[<matplotlib.lines.Line2D object at 0x060314D0>]


[<matplotlib.lines.Line2D object at 0x06031590>]
[<matplotlib.lines.Line2D object at 0x060318B0>]
[<matplotlib.lines.Line2D object at 0x06031B50>]

>>> plt.xlim(0,7)

(0, 7)

>>> plt.ylim(0,0.5)

(0, 0.5)
Let’s discuss Python Compilers
>>> plt.show()

This code plots four line plots for us-

Python Chi-Square Example

b. scipy.stats.chisquare
This calculates a one-way chi-square test for us. It has the following syntax-

scipy.stats.chisquare(f_obs,f_exp=None,ddof=0,axis=0)

Consider the null hypothesis that the categorical data in question has the given frequencies. The Chi-square test
tests this.
It has the following parameters-

f_obs: array_like- In this, we specify the observed frequencies in every category

f_exp: array_like, optional- This holds the expected frequencies in every category; each category is
equally likely by default

ddof: int, optional- This holds the adjustment value to the degrees of freedom for the p-value

axis: int or None, optional- This is the axis of the broadcast result of f_obs and f_exp; we apply the test
along with this

It has the following return values-

chisq: float or ndarray- This is the chi-squared test statistic

p: float or ndarray- This is the p-value of the test


Do you know about Python Geographic maps
This is the formula for the chi-square statistic-
sum((observed-expected)2/expected)

c. Examples of scipy.stats.chisquare
Let’s take a few simple examples.

>>> from scipy.stats import chisquare

>>> chisquare([6,8,6,4,2,2])

Power_divergenceResult(statistic=6.285714285714286, pvalue=0.27940194154949133)

Providing expected frequencies

>>> chisquare([6,8,6,4,2,2],f_exp=[6,6,6,6,6,8])

Power_divergenceResult(statistic=8.5, pvalue=0.13074778927442537)

2D observed frequencies

>>> data=np.array([[6,8,6,4,2,2],[12,10,6,11,10,12]]).T

>>> chisquare(data)

Power_divergenceResult(statistic=array([6.28571429, 2.44262295]), pvalue=array([0.27940194,


0.78511028]))

Setting axis to None

>>> chisquare(np.array([[6,8,6,4,2,2],[12,8,6,10,7,8]]),axis=None)

Power_divergenceResult(statistic=14.72151898734177, pvalue=0.1956041745113551)
Learn Python Scatter Plot

>>> chisquare(np.array([[6,8,6,4,2,2],[12,8,6,10,7,8]]).ravel())

Power_divergenceResult(statistic=14.72151898734177, pvalue=0.1956041745113551)

Altering the degrees of freedom

>>> chisquare([6,8,6,4,2,2],ddof=1)

Power_divergenceResult(statistic=6.285714285714286, pvalue=0.17880285265458937)

Calculating p-values by broadcasting the chi-squared statistic with ddof

>>> chisquare([6,8,6,4,2,2],ddof=[0,1,2])
Power_divergenceResult(statistic=6.285714285714286, pvalue=array([0.27940194, 0.17880285,
0.09850749]))

So, this was all in Python Linear Regression. Hope you like our explanation of Python Chi-Square Test.

4. Conclusion
Hence, in this Python Statistics tutorial, we discussed Python Linear Regression and Python Chi-Square Test.
Moreover, we saw the example of Python Linear Regression and chi-square test. Still, if any doubt regarding
Python Linear Regression, ask in the comment tab.

Python Histogram | Python Bar Plot (Matplotlib & Seaborn)


by DataFlair Team · September 28, 2018

1. Objective
Today, we will see how can we create Python Histogram and Python Bar Plot using Matplotlib and Seaborn
Python libraries. Moreover, in this Python Histogram and Bar Plotting Tutorial, we will understand
Histograms and Bars in Python with the help of example and graphs.

So, let’s understand the Histogram and Bar Plot in Python.

Python Histogram | Python Bar Plot (Matplotlib & Seaborn)

2. Python Histogram
A histogram is a graph that represents the way numerical data is represented. The input to it is a numerical
variable, which it separates into bins on the x-axis. This is a vector of numbers and can be a list or a DataFrame
column. A higher bar represents more observations per bin. Also, the number of bins decides the shape of the
histogram.
Do you know about Python Packages

a. Example of Python Histogram


Let’s begin with a simple Matplotlib Histogram Example.

>>> import seaborn as sn

>>> df=sn.load_dataset(‘iris’)

>>> sn.distplot(df['sepal_length'])

<matplotlib.axes._subplots.AxesSubplot object at 0x07837230

>>> import matplotlib.pyplot as plt

>>> plt.show()

Python Matplotlib Histogram Example

>>> sn.distplot(df['sepal_length'],bins=25)

<matplotlib.axes._subplots.AxesSubplot object at 0x07837230>


>>> plt.show()

Python Matplotlib Histogram Example

To plot this without Seaborn, we can do the following-

>>> import numpy as np

>>> from matplotlib import colors

>>> from matplotlib.ticker import PercentFormatter

>>> np.random.seed(19720810)

>>> N=100000

>>> n_bins=20

>>> x=np.random.randn(N)

>>> y=.7*x+np.random.randn(100000)+7

>>> fig,axs=plt.subplots(1,2,sharey=True,tight_layout=True)

>>> axs[0].hist(x,bins=n_bins)

>>> axs[1].hist(y,bins=n_bins)

>>> plt.show()
Example – Matplotlib Histogram in Python

b. Displaying Only The Histogram


We can choose to show or hide the Python Histogram, the rug, and the kernel density. Let’s try displaying only
the Python Histogram for now.
Let’s revise Python Web Framework

>>> sn.distplot(a=df['sepal_length'],hist=True,kde=False,rug=False)

<matplotlib.axes._subplots.AxesSubplot object at 0x0955C310>

>>> plt.show()
Displaying Only The Histogram

c. Displaying Histogram, Rug, and Kernel Density


Now let’s try displaying all three.

>>> sn.distplot(a=df['sepal_length'],hist=True,kde=True,rug=True)

<matplotlib.axes._subplots.AxesSubplot object at 0x09526EB0>

>>> plt.show()
Displaying Histogram, Rug, and Kernel Density

d. Customizing the rug


Let’s set the rug to red.
Let’s learn about Python Datetime Module

>>> sn.distplot(a=df['sepal_length'],rug=True,rug_kws={'color':'r','alpha':0.35,'linewidth':5})

<matplotlib.axes._subplots.AxesSubplot object at 0x07B18BD0>

>>> plt.show()

Customizing the rug

e. Customizing the density distribution


Using keywords for kernel density, we can customize the density distribution.

>>> sn.distplot(a=df['sepal_length'],kde=True,kde_kws={'color':'r','alpha':0.35,'linewidth':5})

<matplotlib.axes._subplots.AxesSubplot object at 0x07E5B7D0>

>>> plt.show()
Customizing the density distribution

f. Vertical Python Histogram


Now let’s try making a vertical Python Histogram.
Let’s learn about Python Numpy

>>> sn.distplot(df['sepal_length'],color='lightpink',vertical=True)

<matplotlib.axes._subplots.AxesSubplot object at 0x07DCD6D0>

>>> plt.show()
Vertical Python Histogram

g. Python Histogram with multiple variables


We can view together the histograms for multiple numeric variables.

>>> sn.distplot(df['sepal_length'],color='skyblue',label='Sepal length')

<matplotlib.axes._subplots.AxesSubplot object at 0x0849ABB0>

>>> sn.distplot(df['sepal_width'],color='lightpink',label='Sepal width')

<matplotlib.axes._subplots.AxesSubplot object at 0x0849ABB0>

>>> plt.show()

Multiple variables with Histogram in Python

3. Python Bar Plot


A bar plot in Python, also known as a bar chart, represents how a numerical variable relates to a categorical
variable.
Let’s have a look at Python Pandas

a. Example of Python Bar Plot


Let’s take a quick Matplotlib Bar Chart Example.

>>> import numpy as np


>>> import matplotlib.pyplot as plt

>>> marks=[79,45,22,89,95]

>>> bars=('Roll 1','Roll 2','Roll 3','Roll 4','Roll 5')

>>> y=np.arange(len(bars))

>>> plt.bar(y,marks,color=’g’)

<BarContainer object of 5 artists>

>>> plt.xticks(y,bars)

([<matplotlib.axis.XTick object at 0x0942EFD0>, <matplotlib.axis.XTick object at 0x0942E3B0>,


<matplotlib.axis.XTick object at 0x0942E2B0>, <matplotlib.axis.XTick object at 0x079E60B0>,
<matplotlib.axis.XTick object at 0x079E62F0>], <a list of 5 Text xticklabel objects>)

>>> plt.show()

Example of Python Bar Plot

b. Setting a Different Color for Each Bar


Let’s try five different colors for the bars.

>>> plt.bar(y,marks,color=['cyan','skyblue','lightpink','brown','black'])

<BarContainer object of 5 artists>

>>> plt.xticks(y,bars)
([<matplotlib.axis.XTick object at 0x0947B570>, <matplotlib.axis.XTick object at 0x0947B170>,
<matplotlib.axis.XTick object at 0x0946EC90>, <matplotlib.axis.XTick object at 0x094954B0>,
<matplotlib.axis.XTick object at 0x09495850>], <a list of 5 Text xticklabel objects>)

>>> plt.show()

Python Bar Chart – Setting Different Color For Each Bar

c. Setting Border Color


And now for the border color, we use the parameter edgecolor.
Learn Python Data Science Tutorial

>>> plt.bar(y,marks,color=(0.2,0.4,0.2,0.7),edgecolor='deeppink')

<BarContainer object of 5 artists>

>>> plt.xticks(y,bars)

([<matplotlib.axis.XTick object at 0x094A7FB0>, <matplotlib.axis.XTick object at 0x094A7BB0>,


<matplotlib.axis.XTick object at 0x094A7770>, <matplotlib.axis.XTick object at 0x09426050>,
<matplotlib.axis.XTick object at 0x094261F0>], <a list of 5 Text xticklabel objects>)

>>> plt.show()
Python Bar Plot – Setting Border Color

d. Horizontal Python Bar Plot


How about a horizontal bar Plot?

>>> plt.barh(y,marks)

<BarContainer object of 5 artists>

>>> plt.yticks(y,bars)

([<matplotlib.axis.YTick object at 0x084DFC70>, <matplotlib.axis.YTick object at 0x09409B90>,


<matplotlib.axis.YTick object at 0x09409CF0>, <matplotlib.axis.YTick object at 0x09413D70>,
<matplotlib.axis.YTick object at 0x09413790>], <a list of 5 Text yticklabel objects>)

>>> plt.show()
Horizontal Python Bar Plot

e. Adding Title and Axis Labels


Let’s call it Sample graph, with roll numbers on the x axis and marks on the y axis.
Do you know Python Interpreter environment

>>> plt.bar(y,marks,color=(0.5,0.1,0.5,0.6))

<BarContainer object of 5 artists>

>>> plt.title('Sample graph')

Text(0.5,1,’Sample graph’)

>>> plt.xlabel('Roll numbers')

Text(0.5,0,’Roll numbers’)

>>> plt.ylabel('Marks')

Text(0,0.5,’Marks’)

>>> plt.ylim(0,100)

(0, 100)

>>> plt.xticks(y,bars)

([<matplotlib.axis.XTick object at 0x07E991F0>, <matplotlib.axis.XTick object at 0x07E99FD0>,


<matplotlib.axis.XTick object at 0x07E999F0>, <matplotlib.axis.XTick object at 0x07E5B1B0>,
<matplotlib.axis.XTick object at 0x07DF2CF0>], <a list of 5 Text xticklabel objects>)
Let’s discuss Python Data File Formats

>>> plt.show()

Adding Title and Axis Labels in Python Bar Plot

So, this was all in Python Histogram and Bar Plot using Matplotlib library. Hope you like our explanation.

4. Conclusion
Hence, in this Python Histogram tutorial, we conclude two important topics with plotting- histograms and bar
plots in Python. While they seem similar, they’re two different things. Moreover, we discussed example of
Histogram in Python and Python bar Plotting example. Still, if any doubt regarding Python Bar Plot, ask in the
comment tab.

Python Charts – Bubble, 3D Charts with Properties of Chart


by DataFlair Team · September 28, 2018

1. Python Charts – Objective

In our last tutorial, we studied Python Array Module. Today, we will take a quick look at Python Charts. Here,
we will cover the Python Bubble Charts and 3D charts in Python. Then, we’ll learn about the properties of
Python charts and will try to style those charts in Python programming.
Python Charts – Bubble, 3D Charts with Properties of Chart

2. Prerequisites for Python Charts

For this Python Chart tutorial, we will import three libraries- matplotlib, numpy, and pandas.
You can install these Python Libraries using the following commands-

pip install matplotlib

pip install numpy

pip install pandas

You can import these as-

>>> from matplotlib import pyplot as plt

>>> import numpy as np

>>> import pandas as pd

Do you know about Python Modules vs Packages

3. Python Bubble Charts


First, let’s talk about bubble charts in Python. Such a chart is a scatter plot with an extra dimension, which
makes it apparently 3-dimensional. This means larger bubbles denotes higher values. Let’s build a Python
bubble plot.
Python Charts – Python Bubble Charts

>>> x=np.random.rand(30)

>>> y=np.random.rand(30)

>>> z=np.random.rand(50)

>>> plt.scatter(x,y,s=z*777) #s is for global size

<matplotlib.collections.PathCollection object at 0x0863FAD0>

>>> plt.show()
Python Charts – Python Bubble Charts

a. Setting bubble color


Using the color parameter, you can assign a color to your bubbles chart.

>>> plt.scatter(x,y,s=z*777,c='Chartreuse')

<matplotlib.collections.PathCollection object at 0x0A258DB0>

>>> plt.show()

Python Charts – Setting bubble color


You can even assign random colors to different bubbles chart in Python.

>>> colors=np.random.rand(30)

>>> plt.scatter(x,y,s=z*777,c=colors,alpha=0.5) #alpha for transparency

<matplotlib.collections.PathCollection object at 0x0A295EF0>


Read about Python Datetime Module with Quick Examples

>>> plt.show()

Python Charts – Setting bubble color

b. Setting bubble shape


You can also choose what shape to apply to your bubbles chart.

>>> plt.scatter(x,y,s=z*3001,marker='D')

<matplotlib.collections.PathCollection object at 0x0A2D0E70>

>>> plt.show()
Python Charts – Setting bubble shape

>>> plt.scatter(x,y,s=z*3001,marker='<',color='brown')

<matplotlib.collections.PathCollection object at 0x09E15A50>

>>> plt.show()

Setting bubble shape

>>> plt.scatter(x,y,s=z*3001,marker='*',color='pink')

<matplotlib.collections.PathCollection object at 0x09E508F0>


Have a Look at Python Forensics – Hash Function, Virtualization & much more
>>> plt.show()

Setting bubble shape

>>> plt.scatter(x,y,s=z*3001,marker='8',color='lavender')

<matplotlib.collections.PathCollection object at 0x08B9AC90>

>>> plt.show()

Setting bubble shape

c. Setting the edges for your Python bubbles charts


You can also set edges to your Python bubbles chart.

>>> plt.scatter(x,y,s=z*4000,c="beige",alpha=0.4,linewidth=7)

<matplotlib.collections.PathCollection object at 0x094035B0>


Do you know how to Implement Python Switch Case Statement

>>> plt.show()

Setting the edges for your bubbles charts

>>> plt.scatter(x,y,s=z*4000,c="beige",linewidth=7,edgecolors='brown')

<matplotlib.collections.PathCollection object at 0x0A1EADD0>

>>> plt.show()
Setting the edges for your bubbles charts

d. Mapping a color to a plot


Let’s try mapping greys to it.

>>>plt.scatter(x,y,s=z*2000,c=x,cmap="Greys",alpha=0.4,edgecolors="grey",linewidth=2)

<matplotlib.collections.PathCollection object at 0x08AB91F0>

>>> plt.show()

Mapping a color to a plot

4. Python 3D Charts
It is possible to render your Python charts in three dimensions. Here’s how we do that with Python 3D charts.
Let’s Learn Python Database Access – Python 3 MySQL
a. Python 3D Scatterplots
Here, we use the mplot3D toolkit from Python Matplotlib.

>>> from mpl_toolkits.mplot3d import Axes3D

>>> df=pd.DataFrame({'X':range(1,111),'Y':np.random.randn(110)*13+range(1,111),
'Z':(np.random.randn(110)*13+range(1,111))*2})

>>> fig=plt.figure()

>>> ax=fig.add_subplot(121,projection='3d')

>>> ax.scatter(df['X'],df['Y'],df['Z'],c='pink',s=60)

<mpl_toolkits.mplot3d.art3d.Path3DCollection object at 0x0A126CF0>

>>> ax.view_init(27,200)

>>> plt.show()

Python 3D Scatterplots

5. Python Charts Properties

Let’s demonstrate these online Python graphs.


Python Bubble Charts

>>> x=np.arange(0,7)

>>> from scipy import sin

>>> y=sin(x)

>>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x0BEE0150>]

>>> plt.show()
Properties of Charts in Python

a. Titling the graph and labeling the axes


Using the title(), xlabel(), and ylabel() methods, we can do the needful.

>>> plt.title('Sine Wave')

Text(0.5,1,’Sine Wave’)

>>> plt.xlabel('x')

Text(0.5,0,’x’)

>>> plt.ylabel('y')

Text(0,0.5,’y’)

>>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x043BDC70>]

>>> plt.show()
Titling the graph and labeling the axes

Let’s Discuss Python Data File Formats – How to Read CSV, JSON, and XLS Files

b. Setting line color and type


To set the line color, you use a Unicode character in the parameter list-

>>> plt.plot(x,y,'g')

[<matplotlib.lines.Line2D object at 0x045E6FF0>]

Setting line color and type


Now to set the line type, we can-

>>> plt.plot(x,y,'*')

[<matplotlib.lines.Line2D object at 0x0A0A4350>]

>>> plt.show()

Setting line color and type

Now, what if we wanted to set both at once?

>>> plt.plot(x,y,'*g')

[<matplotlib.lines.Line2D object at 0x0A10B810>]


Follow this link to know about Python Data Cleansing by Pandas & Numpy

>>> plt.show()
Charts in Python- Setting line color and type

‘g*’ gives us the same output.

6. Styling your Python Chart

Apart from fiddling with the properties of your charts in Python, you can also style it in a few different ways.
Let’s see how.

Styling your Python Chart

a. Adding Annotations
It is possible to drop in a label in your charts in Python wherever you want.

>>> x=np.arange(0,7)

>>> y=sin(x)

>>> plt.plot(x,y)

[<matplotlib.lines.Line2D object at 0x076DCB90>]

>>> plt.annotate(xy=[3.5,0],s='The curve') #You can add the annotations before plotting if you want

Text(3.5,0,’The curve’)

>>> plt.show()

Styling your Chart- Adding Annotations

Let’s Read about Python File I/O – Python Write to File and Read File

b. Adding Legends
>>> x=np.arange(0,7)

>>> y=sin(x)

>>> z=np.cos(x)

>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x07D24530>]

>>> plt.plot(x,z)

[<matplotlib.lines.Line2D object at 0x07D248F0>]

>>> plt.legend(['Sine','Cosine'],loc=3)

<matplotlib.legend.Legend object at 0x07D249D0>

>>> plt.show()

Styling your Chart – Adding Legends

7. How to Save Python Charts File?

Finally, let’s see what you can do when you want to save a Python chart you’ve just created.

>>> plt.scatter(x,y,s=z*3001,marker='*',color='pink')

<matplotlib.collections.PathCollection object at 0x07D402F0>

>>> plt.savefig('pinkstars.pdf',format='pdf')
Python Charts File

How to Save Python Charts File Now we want to save this as a png. There is no need to make another call to
plot(); simply save it.
Do you know about Python Multiple Inheritance – Python MRO
Saving Python Charts File

So, this was all about Python Charts Tutorial. Hope you like our explanation.

8. Conclusion

Hence, with this, we sum up our Python Charts tutorial on bubble charts and 3D charts in Python. Now you also
know how to style Charts in Python, to make them aesthetically better and also aid understanding. Are there any
more topics you would like us to write on? Let us know in the comments below.

Create Python Scatter Plot & Python BoxPlot (Using Matplotlib)


by DataFlair Team · September 28, 2018

1. Python Scatter & BoxPlot

In our Last tutorial, we discussed Python Charts – Bubble & 3D Charts. Today, we will talk Python Scatter
Plot. In addition, we will learn how to draw a Scatter Plot in Python Programming. Moreover, we will cover
how to create Python Box Plot using Matplotlib.
Let’s begin the Python Scatter Plot.
How to Create Python Scatter Plot & Python BoxPlot

2. How to Create Python BoxPlot Using Matplotlib?

Python box plot tells us how distributed a dataset is. Another use is to analyze how distributed data is across
datasets. Such a plot creates a box-and-whisker plot and summarizes many different numeric variables. Let’s
first take an example so we can explain its structure better.

>>> import matplotlib.pyplot as plt

>>> np.random.seed(10)

>>> one=np.random.normal(100,10,200)

>>> two=np.random.normal(80, 30, 200)

>>> three=np.random.normal(90, 20, 200)

>>> four=np.random.normal(70, 25, 200)

>>> to_plot=[one,two,three,four]

>>> fig=plt.figure(1,figsize=(9,6))

>>> ax=fig.add_subplot(111)

>>> bp=ax.boxplot(to_plot)

>>> fig.savefig('boxplot.png',bbox_inches='tight')

Top 35 Python Interview Questions and Answer


This saves the following figure-
Creating Python BoxPlot (Using Matplotlib)

Structure-

The box denotes the dataset’s quartiles.

The whiskers extend and denote the rest of the distribution.

A function of the inter-quartile range determine the points that are outliers.

The input to this can be a list, a NumPy array, a pandas Series object, an array, a list of vectors, a long-form
DataFrame, or a wide-form DataFrame.
Let’s take another example.

>>> ax = sn.boxplot(x="day", y="total_bill", hue="smoker",data=tips, palette="Set3")

>>> plt.show()
Creating Python BoxPlot (Using Matplotlib)

3. How to Create a Python Scatter Plot?

Python Scatter Plot, let us denote how two or more objects related to each other.
How to Create Scatter Plot in Python?

It also lets us identify outliers- values that stray from all others.

>>> np.random.seed(19680801)

>>> N=50

>>> x=y=colors=np.random.rand(N)

>>> area = (30 * np.random.rand(N))**2

>>> plt.scatter(x, y, s=area, c=colors, alpha=0.5)

<matplotlib.collections.PathCollection object at 0x095BB6D0>


Do you know how to Implement Python Switch Case Statement

>>> plt.show()

Creating Scatter Plot in Python using plt.plot

a. Python Scatter Plot using plt.plot


>>> x=np.linspace(0,10,30)

>>> from scipy import sin

>>> y=np.sin(x)

>>> plt.plot(x,y,'o',color='purple')

[<matplotlib.lines.Line2D object at 0x080D3890>]

>>> plt.show()
Creating Scatter Plot in Python using plt.plot

>>> plt.plot(x,y,'-ok')

[<matplotlib.lines.Line2D object at 0x08094F30>]

>>> plt.show()

Python Scatter Plot using plt.plot


>>> plt.plot(x,y,'-p',color='green',

markersize=15,linewidth=4,

markerfacecolor='white',

markeredgecolor='gray',

markeredgewidth=1)

[<matplotlib.lines.Line2D object at 0x0811A4B0>]


Do you know how to Python Rename File – Single & Multiple Files

>>> plt.show()

Draw Python Scatter Plots | Python BoxPlot (Using Matplotlib)

b. More than one plot


>>> plt.plot(np.random.rand(20),'*')

[<matplotlib.lines.Line2D object at 0x08158A30>]

>>> plt.plot(np.random.rand(20),'o')

[<matplotlib.lines.Line2D object at 0x08158D90>]D

>>> plt.show()
Draw More than one Scatter Plot in Python

So, this was all about Python Scatter Plot. Hope you like our explanation.

4. Conclusion

Hence, we learned how to create Python box plots and scatter plot with matplotlib. Stay tuned for more charts.
Leave your opinions in the comments below.

Python Heatmap | Word Cloud Python with Example


by DataFlair Team · September 28, 2018

1. Python Heatmap & Word Cloud


Next in our series of graphs and plots with Python is Python Heatmaps and Word Cloud. Moreover, we will see
what is Python Heatmap and what is Python Word Cloud. Also, we will discuss Python heatmap example and
Word Cloud Python Example.
So, let’s start with creating a Python Heatmap.
Python Heatmap |Python Word Cloud – Example

2. How to Create a Heatmap in Python?


So, what is a heat map? A way of representing data as a matrix of values. Basically, using different colors to
represent data, it gives you a general view of the numerical data. Some manipulations when working with
heatmaps. Python Heatmap includes normalizing the matrices, performing cluster analysis, choosing a color
palette, and permuting rows and columns to place similar values nearby.
Do you know about Python Numpy

a. A Simple Python Heatmap Example


First, let’s make a simple heat map to get an idea of what it is.

>>> import seaborn as sn

>>> import numpy as np

>>> import pandas as pd

>>> df=pd.DataFrame(np.random.random((7,7)),columns=['a','b','c','d','e','f','g'])

>>> sn.heatmap(df)

<matplotlib.axes._subplots.AxesSubplot object at 0x078DAA10>

>>> import matplotlib.pyplot as plt


>>> plt.show()

Python Heatmap example

Here, we create a DataFrame, and then call the heatmap() method on it borrowing from seaborn.

b. Annotating your Python heatmap


You can add an annotation to every cell of your Python heatmap.

>>> sn.heatmap(df,annot=True,annot_kws={'size':7})

<matplotlib.axes._subplots.AxesSubplot object at 0x078529D0>

>>> plt.show()
Python Heatmap Annotation

Here, annot_kws lets us set the size of the annotations with the ‘size’ parameter. We set it to 7 for this demo.

c. Adding Grid Lines


It is possible to add grid lines to your Python heatmap. In the following piece of code, we add pink grid lines of
thickness 2.5.
Let’s discuss Python Scipy

>>> sn.heatmap(df,linewidths=2.5,linecolor='pink')

<matplotlib.axes._subplots.AxesSubplot object at 0x07AFD970>

>>> plt.show()
rid lines in Python Heatmap

d. Removing X or Y labels
In the next piece of code, we remove the x tick labels from the map.

>>> sn.heatmap(df,xticklabels=False)

<matplotlib.axes._subplots.AxesSubplot object at 0x07B5F0D0>

>>> plt.show()

Removing labels in Python Heatmap

e. Removing the color bar


The vertical bar at the extreme right of this Python Heatmap tells us what values the colors represent. But we
can choose to not display it.
Let’s revise Python Array Module

>>> sn.heatmap(df,cbar=False)

<matplotlib.axes._subplots.AxesSubplot object at 0x08083090>

>>> plt.show()

Removing color bar in Heatmap

f. Keeping only a few labels


Basically, when there are too many cells/labels, there may be overlapping. To avoid this, you can give a value x
to the xticklabels parameter. It then shows labels every x labels.

>>> sn.heatmap(df,xticklabels=2)

<matplotlib.axes._subplots.AxesSubplot object at 0x082E4C70>

>>> plt.show()
Keeping few labels inPython heatmap

g. Choosing a color theme for your heatmap


Let’s try a green.

>>> sn.heatmap(df,cmap='Greens')

<matplotlib.axes._subplots.AxesSubplot object at 0x0AF5D510>


Read Python Descriptive Statistics

>>> plt.show()

Heatmap – Color Theme

Now, let’s try blue and purple.

>>> sn.heatmap(df,cmap='BuPu')
<matplotlib.axes._subplots.AxesSubplot object at 0x0B0CFAB0>

>>> plt.show()

Heatmap Python – Color theme

Yellow, green, and blue-

>>> sn.heatmap(df,cmap='YlGnBu')

<matplotlib.axes._subplots.AxesSubplot object at 0x0B26F1D0>

>>> plt.show()

Heatmap in Python – Color Theme

Red, yellow, blue-


>>> sn.heatmap(df,cmap='RdYlBu')

<matplotlib.axes._subplots.AxesSubplot object at 0x09DF3A70>


Have a look at Python Interpreter

>>> plt.show()

Heatmap Python – Color theme

h. Plotting a discrete heatmap

Plotting a discrete heatmap

For discrete data, you can choose to plot it with a Python heatmap.
i. Normalizing a column
So, consider the following piece of code-

>>> df=pd.DataFrame(np.random.randn(7,7)*4+3)

>>> df[1]=df[1]+37

>>> sn.heatmap(df,cmap='plasma')

<matplotlib.axes._subplots.AxesSubplot object at 0x09E1FC90>

>>> plt.show()

Normalizing a column in heatmap Python

Now, in this plot, 1 has considerably higher values. To get around this, we normalize it.

>>> df_norm=(df-df.mean())/df.std()

>>> sn.heatmap(df_norm,cmap='plasma')

<matplotlib.axes._subplots.AxesSubplot object at 0x07EDEDD0>


Do you know about Python Matplotlib

>>> plt.show()
Normalizing a column in heatmap Python

3. How to Create a Word Cloud Python?


A word cloud in Python visually represents text data. Also called a tag cloud, it uses different font sizes and
colors to highlight the importance of each word. This way, the most prominent terms will come across to the
user. We will use the word cloud library here.

a. A simple Word Cloud Python Example


>>> from wordcloud import WordCloud

>>> text=("Python Python Python C Java JavaScript jQuery jQuery R Python Python SQL HTML Lisp
Java Ruby jQuery Python Python Django Scala Python JavaScript jQuery")

>>> wordcloud=WordCloud(width=500,height=500,margin=1).generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x09D1CC50>

>>> plt.axis('off')

(-0.5, 499.5, 499.5, -0.5)

>>> plt.margins(x=0,y=0)

>>> plt.show()
Word Cloud Python Example

b. Setting the font size


Now, you can set a maximum and minimum font size for your Word cloud Python.

>>> wordcloud=WordCloud(width=500,height=500,max_font_size=30,
min_font_size=10,margin=1).generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x0832CB70>

>>> plt.axis('off')

(-0.5, 499.5, 499.5, -0.5)


Learn Aggregation and Data Wrangling with Python

>>> plt.margins(x=0,y=0)

>>> plt.show()
Setting the font size in Word Cloud Python

c. Limit the number of words


Now, let’s see what happens if we don’t call the axis() and margins() methods.

>>> wordcloud=WordCloud(width=500,height=500,max_words=4,margin=1).generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x07EDECD0>

>>> plt.show()

Python Word cloud – Limit the number of words

d. Exclude some words


Generally, it is possible to use only some words from the text.

>>> wordcloud=WordCloud(width=500,height=500,stopwords=['Java','Django']).generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x09E14030>

>>> plt.axis('off')

(-0.5, 499.5, 499.5, -0.5)

>>> plt.margins(x=0,y=0)

>>> plt.show()

Python Word cloud – Exclude some words

e. Change the background


Now, you can set the background to a certain color.
Want to learn about Python Django

>>> wordcloud=WordCloud(height=500,width=500, background_color='darkturquoise').generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x079B7190>

>>> plt.axis('off') <span style="font-weight: 400"> </span>

(-0.5, 499.5, 499.5, -0.5)

>>> plt.margins(x=0,y=0)

>>> plt.show()
Change the background in Word cloud Python

f. Setting word color


Now, how about changing the color of words?

>>> wordcloud=WordCloud(height=500,width=500,background_color='white',
colormap='plasma').generate(text)

>>> plt.imshow(wordcloud,interpolation='bilinear')

<matplotlib.image.AxesImage object at 0x080AA830>

>>> plt.axis('off')

(-0.5, 499.5, 499.5, -0.5)

>>> plt.show()

Setting word color in Python Word Cloud

g. Shaping a word cloud


Now, it is possible to set a Python word cloud for a custom shape. Let’s use a diamond:
Shaping a word cloud Python

>>> from PIL import Image

>>> mask=np.array(Image.open('diamond.png'))

>>> wordcloud=WordCloud(mask=mask).generate(text)

>>> plt.imshow(wordcloud)

<matplotlib.image.AxesImage object at 0x07852630>


Let’s explore Python data File formats

>>> plt.axis('off')

(-0.5, 488.5, 438.5, -0.5)

>>> plt.show()

Shaping a word cloud Python

So, this was all in Python Heatmap. Hope you like our explanation of Word Cloud Python.
4. Conclusion
Hence in this Python Heatmap tutorial, we discussed what is heat map and how to create a Python Heatmap.
Moreover, we discussed Word Cloud Python. In this, we saw what is Word cloud and how to make
Word Cloud? Also, we saw the Word Cloud Python Example. So this is how we create heat maps and word
clouds in Python. For this, we used the libraries matplotlib and word cloud in this tutorial. Still, if any
doubt regarding Python Heatmap, ask in the comment tab.

Python Geographic Maps & Graph Data


by DataFlair Team · September 28, 2018

1. Objective
Today, in this Python tutorial, we will discuss Python Geographic Maps and Graph Data. Moreover, we will
see how to handle geographical and graph data using Python and its libraries. We will use Matplotlib and
Cartopy among other libraries to plot Geographic Maps and Graph Data.
So, let’s start Exploring Python Geographic Maps.

Python Geographic Maps & Graph Data

2. Prerequisites for Python Geographic Maps and Graph Data


Do you know about Python Heatmap
We need the following libraries for this Python Geographic Maps and Graph Data-

a. Cartopy
Python Geographic Maps – Cartopy

Cartopy is a Python package for cartography. It will let you process geospatial data, analyze it, and produce
maps. As a Python package, it uses NumPy, PROJ.4, and Shapely, and stands on top of Matplotlib. Some of its
key features-

Object-oriented projection definitions.

Publication quality maps.

Ability to transform points, lines, polygons, vectors, and images.

You can install it using pip-

pip install Cartopy

Note that you may need to install Microsoft Visual C++ Build Tools 14.0 or higher for this.
You can import it as-

>>> import cartopy

b. Other modules
There are some other modules we will use here-

pip install Matplotlib

>>> import matplotlib.pyplot as plt

We will import other modules on the way as we’ll need them.


3. Python Geographic Maps
Python will let us draw Geographical maps. Let’s see how.

a. A simple map
Let’s first simply draw a map and fill it in later.

>>> import cartopy.crs as ccrs

>>> ax=plt.axes(projection=ccrs.PlateCarree()) #Using the PlateCarree projection

>>> ax.coastlines() #Display the coastlines

<cartopy.mpl.feature_artist.FeatureArtist object at 0x06DA3DF0>

>>> plt.show()

Python Geographic Maps – Simple Map

b. Other projections
>>> ax=plt.axes(projection=ccrs.Orthographic())

>>> ax.stock_img() #Add the stock world map image to the plot

<matplotlib.image.AxesImage object at 0x07427410>


Learn Python data Science environment Setup

>>> plt.show()
Python Geographic Maps

We have several other projections like Mollweide, Robinson, Sinusoidal, and Gnomonic among many others.

c. Adding data
Let’s try to plot Romania to Indore, India in a map.

>>> ax=plt.axes(projection=ccrs.AlbersEqualArea())

>>> ax.stock_img()

<matplotlib.image.AxesImage object at 0x077466B0>

>>> ro_lon,ro_lat=25,46 #Coordinates/ longitude and latitude

>>> ind_lon,ind_lat=75.8,22.7

>>> plt.plot([ro_lon,ind_lon],[ro_lat,ind_lat],
color='green',linewidth=2,marker='*',transform=ccrs.Geodetic(),) #Green line

[<matplotlib.lines.Line2D object at 0x077469B0>]

>>> plt.plot([ro_lon,ind_lon],[ro_lat,ind_lat], color='gray',linestyle='--',transform=ccrs.PlateCarree(),)


#Gray, dashed line

[<matplotlib.lines.Line2D object at 0x07746E10>]


Let’s revise Python Pandas

>>> plt.text(ro_lon-3,ro_lat-12,'Romania',

horizontalalignment='right',
transform=ccrs.Geodetic()) #Text- Romania

Text(22,34,’Romania’)

>>> plt.text(ind_lon+3,ind_lat-12,'Indore',

horizontalalignment='right',

transform=ccrs.Geodetic()) #Text- Indore

Text(78.8,10.7,’Indore’)

>>> plt.show()

Python Geographic Maps – Adding Data

The Geodetic coordinate system is spherical, but since we use AlbersEqualArea, that makes the green line
appear straight on the plot. Similarly, the gray line is a PlateCarree but appears spherical.
Now, let’s try adding sky blue pins to a map.
Let’s discuss Python Scipy Tutorial

>>> fig=plt.figure(figsize=(16,12))

>>> ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree())

>>> ax.set_extent((10,144,70,-30))

>>> ax.stock_img()

<matplotlib.image.AxesImage object at 0x06D75D90>

>>> ax.coastlines()
<cartopy.mpl.feature_artist.FeatureArtist object at 0x06D75C70>

>>> ax.tissot(facecolor='skyblue',alpha=0.6)

<cartopy.mpl.feature_artist.FeatureArtist object at 0x06D7E210>

>>> plt.show()

Python Geographic maps – Adding Data

d. Contour plots
A contour plot represents a 3D surface on a 2D format by plotting contours (constant z slices). Let’s try making
one.
Learn Aggregation with Python

>>> from netCDF4 import Dataset as netcdf_dataset

>>> import numpy as np

>>> from cartopy import config

>>> import os

>>> fname=os.path.join(config["repo_data_dir"],
'netcdf', 'HadISST1_SST_update.nc'

>>> dataset=netcdf_dataset(fname)

>>> sst=dataset.variables['sst'][0,:,:]

>>> lats=dataset.variables['lat'][:]

>>> lons=dataset.variables['lon'][:]

>>> ax=plt.axes(projection=ccrs.PlateCarree())

>>> plt.contourf(lons,lats,sst,60,

transform=ccrs.PlateCarree())

<matplotlib.contour.QuadContourSet object at 0x0B39E830>

>>> ax.coastlines()

<cartopy.mpl.feature_artist.FeatureArtist object at 0x0BC6BBD0>

>>> plt.show()

Python Geographic Maps – Contour Plus

4. Python Graph Data


Moving on to graph data, let’s see how Python will let us represent a compressed sparse graph. This is
abbreviated as CSGraph. Let’s talk about a few concepts it encompasses.
Let’s discuss how to work with NoSQL database
a. Sparse graphs
A sparse graph is a set of nodes that are linked together. Such a graph can represent anything of social network
connections to points in a high-dimensional distribution.
To represent such data, we can use a sparse matrix G. Let’s keep it size NxN. The value of the connection
between any two notes i and j will be G[i,j]. Now a sparse graph will hold zeros for most of its members. This
means that for most of its nodes, there exist only a few connections.
Some algorithms we use for this are-

Isomap- This is a manifold learning algorithm that needs to find the shortest paths in a graph

Hierarchical clustering- This is a clustering algorithm and is based on minimum spanning trees

Spectral decomposition- This is a projection algorithm and is based on sparse graph laplacians

b. Example – Word Ladder


This is a word game by Lewis Carroll. In this game, players switch letters in words to get from one word to
another; they do this one letter at once. Let’s take an example.
wade-> fade-> faze-> gaze-> gate-> date-> hate
In cases like such, when we want to find the shortest possible path from one word to another, we can use the
sparse graph submodule.
Do you Know about Python Data File Formats

>>> wordlist="hello how are you can you read this without punctuation do you think this is something what
is the meaning of life".split()

>>> wordlist=[word for word in wordlist if len(word) == 3]

>>> wordlist=[word for word in wordlist if word[0].islower()]

>>> wordlist=[word for word in wordlist if word.isalpha()]

>>> wordlist = list(map(str.lower, wordlist))

>>> len(wordlist)

>>> import numpy as np

>>> wordlist=np.asarray(wordlist)

>>> wordlist.dtype

dtype(‘<U3’)

>>> wordlist.sort()

>>> i1=wordlist.searchsorted('wade')
>>> i2=wordlist.searchsorted('hate')

>>> wordlist[i1]

‘you’

>>> wordlist[i2]

‘how’

>>> wordlist

array([‘are’, ‘can’, ‘how’, ‘the’, ‘you’, ‘you’, ‘you’], dtype='<U3′)


So, this was all in python Geographic Maps. hope you like our explanation of Python Graphs Data.

5. Conclusion
Hence, in this python Geographic Maps tutorial, we discussed graph plotting with Python. Moreover, we
discussed Python Graph Data. Some of the libraries we used were Cartopy and Matplotlib.

Python Time Series Analysis – Line, Histogram, Density Plotting


by DataFlair Team · September 28, 2018

1. Time Series Analysis in Python


In this Python tutorial, we will learn about Python Time Series Analysis. Moreover, we will see how to plot
the Python Time Series in different forms like the line graph, Python histogram, density plot, autocorrelation
plot, and lag plot.

So, let’s begin the Python Time Series Analysis.


Python Time Series Analysis – Line, Histogram, Density Plotting

2. What is Time Series in Python?


Consider a sequence of points of data. Suppose we look at the rate of Dollar($) to Indian Rupee. We can link
each point of data with a timestamp. Let’s try plotting for this rate over a period of one week. Let’s use
Python pandas for this.

a. Data to Use in Python Time Series Analysis


We save the following data in a CSV file–
Date, Rate
18-07-2018, 68.625
19-07-2018, 68.9453
20-07-2018, 68.745
21-07-2018, 68.747
22-07-2018, 68.7415
23-07-2018, 68.9449
24-07-2018, 68.9486
We save this as dollartorupee.csv.
Do you know about Python NumPy?

b. Plotting a Python Line Chart/Graph


Let’s use this data to plot a simple line graph with this.

>>> from pandas import Series

>>> from matplotlib import pyplot


>>> series=Series.from_csv('dollartorupee.csv',header=0)

>>> series.plot()

<matplotlib.axes._subplots.AxesSubplot object at 0x031948D0>

>>> pyplot.show()

Plotting a Line Graph in Time Series Python Analysis

We can use a line style with this-


Learn more about Python SciPy

>>> series.plot(style='k.')

<matplotlib.axes._subplots.AxesSubplot object at 0x033969B0>

>>> pyplot.show()
Plotting a Line Graph

3. Plotting a Python Histogram


Now to plot a Python histogram, we can try the hist() method.

>>> series.hist()

<matplotlib.axes._subplots.AxesSubplot object at 0x05D683D0>

>>> pyplot.show()

Plotting a Python Histogram in Time Series Python Analysis

4. Plotting a Density Plot in Python Time Series


What if we want to find out the density of the rate values for the entire week?

>>> series.plot(kind='kde')

<matplotlib.axes._subplots.AxesSubplot object at 0x03194C70>


Let’s discuss Python Interpreter

>>> pyplot.show()

Plotting a Density Plot in Time Series Python Analysis

5. Autocorrelation Plot in Python Time Series


This gives us how the elements of the series correlate to each other.

>>> from pandas.tools.plotting import autocorrelation_plot

>>> autocorrelation_plot(series)

<matplotlib.axes._subplots.AxesSubplot object at 0x08C5CA90>

>>> pyplot.show()
Autocorrelation Plot in Time Series Python Analysis

6. Plotting a Lag Plot in Python Time Series


Such a plot tells us whether a time series is random. If you can identify a structure in the plot, the data isn’t
random.
Do you know about Python Matplotlib

>>> from pandas.tools.plotting import lag_plot

>>> lag_plot(series)

<matplotlib.axes._subplots.AxesSubplot object at 0x08EF4FF0>

>>> pyplot.show()
Plotting a Lag Plot in Time Series in Python Analysis

So, this was all in Time Series Analysis in Python. Hope you like our explanation.

7. Conclusion
Hence, in this Python Time Series tutorial, we discussed what is Time Series, Time Series Analysis in Python
and plotting in Python Time Series Analysis. With this, we conclude our tutorial on time series. Now you know
how to plot it in different forms. Got any questions? Leave them in the comments below.
See also –

Unit Testing With Python Unittest – Example & Working


by DataFlair Team · September 29, 2018

1. Python Unittesting

In this Python Unittest tutorial, we will learn how to set up unit tests for our Python code. For this, we will use
the module Unittest in Unit Testing with Python. Right before leaving, we will also introduce you to pytest,
another module for the same thing. Moreover, we will discuss Python Unittest example and the working. Also,
we will see Python Unit Testing Framework and assert.

So, let’s start Unit Testing with Python Unittest Tutorial.


Unit Testing With Python Unittest – Example & Working

2. What is Python Unittest?

Python Unittest is a Python Unit-Testing framework. Inspired by JUnit, it is much like the unit testing
frameworks we have with other languages. Here are some features it supports-

Test automation

Sharing setup and shutdown code for tests

Aggregating tests into collections

Independence of tests from the framework

Do you know about Python Decorators

a. Concepts in an object-oriented way for Python Unittest


Test fixture- the preparation necessary to carry out test(s) and related cleanup actions.

Test case- the individual unit of testing.

A Test suite- collection of test cases, test suites, or both.

Test runner- component for organizing the execution of tests and for delivering the outcome to the user.

In this Python Unittest tutorial, we will use the unittest module to demonstrate our examples to you.

3. Python Unittest Example


In the following example of Unittest in Python, we will take a simple function that calculates the modulus 3 of a
value.

>>> import unittest

>>> def modthree(x): #defining the function

return x%3

>>> class Tests(unittest.TestCase):

def test(self): #test method

self.assertEqual(modthree(4),1)

>>> if name ==' main ':

unittest.main()

.
———————————————————————-
Ran 1 test in 0.010s
OK
Did you see the output?
Have a look at Python Classes and Objects

a. One More Example of Python Unittest


Now let’s try testing for string methods; we won’t need a function for this.

>>> class TestStringMethods(unittest.TestCase):

def test_lstrip(self): #testing for left stripping

self.assertEqual(' hello '.lstrip(),'hello ')

def test_isupper(self): #testing for isupper

self.assertTrue('HELLO'.isupper())

self.assertFalse('HELlO'.isupper())

def test_split(self): #testing for split

self.assertEqual('Hello World'.split(),['Hello','World'])

with self.assertRaises(TypeError):

'Hello World'.split(2)

>>> if name ==' main ':


unittest.main()

….
———————————————————————-
Ran 4 tests in 0.031s
OK

4. How Python Unittest Works?

So we’ve seen this Unit Testing with Python works without much effort. But how does this happen behind the
scenes? Let’s find out.

a. Subclassing unittest.TestCase
Consider the following line-

class TestStringMethods(unittest.TestCase):

Here, we subclass unittest.TestCase. What we mean is we make our class TestStringMethods inherit from the
class unittest.TestCase. Then, we define three methods, the names for which begin with ‘test’:
Let’s learn about Python String
test_lstrip()
test_isupper()
test_split()

b. Python Unittest Assert Methods


Now, let’s take a look at what methods we can call within Unit testing with Python:

assertEqual()- Tests that the two arguments are equal in value.

assertNotEqual()- Tests that the two arguments are unequal in value.

assertTrue()- Tests that the argument has a Boolean value of True.

assertFalse()- Tests that the argument has a Boolean value of False.

assertIs()- Tests that the arguments evaluate to the same object.

assertIsNot()- Tests that the arguments do not evaluate to the same object.

assertIsNone()- Tests that the argument evaluates to none.

assertIsNotNone()- Tests that the argument does not evaluate to none.

assertIn()- Tests that the first argument is in the second.

assertNotIn()- Tests that the first argument is not in the second.

assertIsInstance()- Tests that the first argument (object) is an instance of the second (class).
assertRaises()- Tests that Python raises an exception when we call the callable with positional/ keyword
arguments we also passed to this method.

You must read Python Assert Statements

assertRaisesRegex()- Tests that regex matches on the string representation of the exception raised; similar
to assertRaises().

assertWarns()- Tests that Python triggers a warning when we call the callable with positional/ keyword
arguments we also passed to this method.

assertWarnsRegex()- Tests that regex matches on the message for the triggered warning; similar to
assertWarns().

assertLogs()- Tests that Python has logged at least one message on the logger or a child of the logger;
ensures this is with at least the level we mention.

assertAlmostEqual()- Tests that the first and second arguments have approximately equal values.

assertNotAlmostEqual()- Tests that the first and second arguments do not have approximately equal
values.

assertGreater()- Tests that the first argument is greater than the second.

assertGreaterEqual()- Tests that the first argument is greater than or equal to the second.

assertLess()- Tests that the first argument is lesser than the second.

assertLessEqual()- Tests that the first argument is lesser than or equal to the second.

assertRegex()- Tests that a regex search matches the text.

assertNotRegex()- Tests that a regex search does not match the text.

assertCountEqual()- Tests that the first argument, which is a sequence, contains the same as does the
second.

assertMultiLineEqual()- Tests that the first argument, which is a multiline string, is equal to the second.

assertSequenceEqual()- Tests that two sequences are equal.

assertListEqual()- Tests that two lists are equal.

assertTupleEqual()- Tests that two lists are equal.

assertSetEqual()- Tests that two sets are equal.

assertDictEqual()- Tests that two dictionaries are equal.

Now that we’ve discussed all these, you can go check the code once again. We used the methods assertEqual(),
assertTrue(), assertFalse(), and assertRaises().
c. unittest.main()
This delivers a command-line interface to the test script. The output suggests whether the tests ran okay or
failed.

5. Tests That Fail in Python Unittesting

What happens if a test fails? To make this happen, we refer to a string variable that doesn’t already exist.
Let’s revise Python Directory and Files

>>> class TestStringMethods(unittest.TestCase):

def test_lstrip(self):

self.assertEqual(' hello '.lstrip(),'hello ')

def test_isupper(self):

self.assertTrue('HELLO'.isupper())

self.assertFalse('HELlO'.isupper())

def test_split(self):

self.assertEqual('Hello World'.split(),['Hello','World'])

with self.assertRaises(TypeError):

s.split(2)

>>> if name ==' main ':

unittest.main()

..E.
======================================================================
ERROR: test_split ( main .TestStringMethods)
———————————————————————-
Traceback (most recent call last):
File “<pyshell#21>”, line 10, in test_split
NameError: name ‘s’ is not defined
———————————————————————-
Ran 4 tests in 0.016s
FAILED (errors=1)
You can see the error in the output. One of the tests failed and returned an error. It did so because we did not
define a string s.

6. Python Unittest – Testing With pytest


It was fun working with Python Unittest. But before leaving, we want to introduce you to pytest, a framework
that makes it fun to write small tests. But that doesn’t limit it- we can scale it to support complex functional
testing for applications and libraries.
First, install pytest with Python pip-
pip install pytest
You don’t have to import this in the IDLE; we create the following Python file on our Desktop-

def modthree(x):

return x%3

def test_value():

assert(modthree(4)==1)

We save this as demo.py. Then, we open the command line and get to the desktop. After that, we run a test-

Python Unittest – Testing with pytest

Let’s make a test fail.


Do you know about Python Counter

def modthree(x):

return x%3

def test_value():

assert(modthree(4)==2)
Python Unittest – pytest Testing in Python

So, this was all in Unit Testing with Python Unittest. Hope you like our explanation.

7. Conclusion – Python Unittest

Hence, in this Python Unittest tutorial, we discussed Unit Testing with Python. Moreover, we saw Python
Unittest example and working. Also, we discussed Python Unit Testing frameworks and test case example with
Python Unittest assert. We hope you can run your own tests for your code. In this tutorial, we saw how to do
that with the Python Unittest and pytest modules. Furthermore, if you feel any difficulty while understanding
the concept of Unit Testing with Python Unittest, feel free to ask with us through comments.

Python Logging Module – Explore Logging File & Levels


by DataFlair Team · September 29, 2018

1. Python Logging – Objective

In this Python tutorial, we will discuss how to perform Python Logging. Moreover, we will use the
Python logging module for this. Also, we will discuss debug, set level and error in Python Logging.

So, let’s start the Python Logging Tutorial.

Python Logging Tutorial – Levels & Examples

2. What is Logging in Python?

Logging, in software applications, is a way to track events. Before we can proceed, telling you more about it,
we want to exemplify.
>>> import logging

>>> logging.warning('This is a warning')

WARNING:root:This is a warning
Basically, Logging is a module with the Python Standard Library ever since version 2.3. Effectively, logging is
a way to track events occurring when we run a piece of software. As a developer, you add logging calls to your
code denoting the occurrence of certain events. Purposes of logging in Python are two-
Have a look at the Python Library.

Diagnostic Logging- To record events that revolve around the application’s operation.

Audit Logging- To record events for business analysis.

3. Python Logging to File

What if we wanted to save these messages to a text file instead of throwing them to the Logging console?

>>> import logging

>>> logging.basicConfig(filename='demolog.log',level=logging.DEBUG)

>>> logging.warning('This is a warning'); logging.warning('You may run into issues with your code')

Python Logging to Files

We pass a filename argument to the logging.basicConfig() method. Here, we call our file demolog.log. Such a
file is one we can consult over time.

a. Python Logging Levels – Severity


To the basicConfig() method above, we passed the severity level logging.DEBUG. This is the importance the
developer ascribes to an event. We have several other values-

DEBUG- Information for problem diagnostics only.

INFO- The program runs as expected.

WARNING- To indicate that something went wrong.

ERROR- This means the software no longer functions.

CRITICAL- For a very serious error.


Let’s revise the Python Zip File
Here, WARNING is the default logging level; this ignores other messages.
With this default, nothing shows up for a call to info().

>>> import logging

>>> logging.warning('You are warned')

WARNING:root:You are warned

>>> logging.info('Deal with it')

>>>

4. Displaying Date/Time For Python Logging

To enable the time of logging in Python, you can use the following piece of Python code-
logging.basicConfig(format=’%(asctime)s %(message)s’)

>>> import logging

>>> logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)

>>> logging.info('Began to log')

2018-08-21 16:09:28,100 Began to log

>>> logging.warning('This is a warning'); logging.warning('Your code could run into issues')

2018-08-21 16:10:07,071 This is a warning


2018-08-21 16:10:07,121 Your code could run into issues
Have a look at Python 3 extension

a. Setting a Python Logging Format


Let’s see Logging Formatter example in Python-

>>> import logging

>>> logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG,datefmt='%m/%d/%Y


%I:%M:%S %p')

>>> logging.warning('Does this work')

08/21/2018 05:02:01 PM Does this work

5. Python Logging Functions

logging.info() or logging.debug() for the detailed output of events that occur during normal operation of a
program.
warnings.warn() issues a warning for a runtime event if the issue is avoidable.

logging.warning() issues a warning for a runtime event if we need to note the event even when the client can
do nothing about it.

logging.error(), logging.exception(), or logging.critical() report the suppression of an error without raising


an exception.

6. Logging Variable Data


Let’s take a few more examples of Logging in Python before we can bid goodbye for the day. It is possible to
use a format string to describe an event and then append variable data as arguments. Let’s take an example,
shall we?

>>> logging.warning('%s before %s','Service','self')

WARNING:root:Service before self


Do you know about Python Packages?
So, this was all in Python Logging Tutorial. Hope you like our explanation.

7. Conclusion – Logging in Python

With this, we conclude our tutorial on Logging in Python. We saw the logging module, levels of severity, how
to log to a file, and how to display date/time for Python Logging. We also learned how to log variable data
and took a look at which function to call and when. Still, if you have any confusion, ask in the comment.

Python Slice Constructor – Python Slice String & Slicing Tuple


by DataFlair Team · July 4, 2019

1. Python Slice – Objective


Today, in this Python Tutorial, we will discuss Python Slice. First, we will see the meaning of Slicing in
Python. Moreover, we will learn Python Slice() function with syntax and example. Also, we will see Python
String and Tuples Slicing. At last. we will discuss indexing to create Slice in Python.
Python Slice | Python Slice() Constructor

2. What is Python Slice?

On our way up the learning curve for Python, we only need to deal with Python iterables not so huge. But
when building an application with it, not always do we need to retrieve the entire iterable. In such cases, slicing
is useful as it lets us choose what to see and focus on. This aids readability and implements abstraction.

Let’s read about Copy in Python

To slice a iterable, we use the slicing operator, that is [ ]. To separate the start, stop, and step values, we use the
colon ( : ).
Say you want to declare a list with the values 1,2,3,4,5, and only want to see values 2,3, and 4. You can slice the
list for that.

>>> list=[1,2,3,4,5]

>>> list[1:4]

[2, 3, 4]

3. The Python Slice() Function

slice() is a constructor that creates a Python Slice object to represent the set of indices that range(start, stop,
step) specifies. With this, we can slice a sequence like a string, a tuple, a list, a range object, or a bytes object.
These are all objects that support sequence protocols and implement getitem () and len ().
The slice() function returns a Python Slice Object.
Have a look at different Python Function

a. The Syntax of Python Slice


Let’s talk about the syntax of Slicing in Python first:

slice(stop)

slice(start, stop, step)

What are these parameters? Let’s see:

start- Where to begin slicing (Optional)

stop- Where to stop slicing+1

step- How much to increment between each index (Optional)

You’ll see that we have two syntaxes. When we provide only one parameter value, it takes it to be the stop
value. This means to start and step are set to None.

b. Python Slice Example


Let’s take a simple example of Python Slicing.

>>> slice(3)

slice(None, 3, None)
We can index this as:

>>> l=[1,2,3,4,5]

>>> l[slice(3)]

[1, 2, 3]
Note that it displays values at the indices 0, 1, and 2. It stops at index 3.

4. Python String Slice

We can slice a string in Python using the Python slice() method. We can also specify the interval. Slicing a
string may give us a substring when the step size is 1.
You must learn about Python String

>>> s='helloworld'

>>> s[slice(1,6,2)]

‘elw’

With positive indices-

Like in the previous example, we use positive indices here.

>>> s='helloworld'
>>> s[slice(1,6)]

‘ellow’

With negative indices-

But like we’ve seen with lists earlier, we can pass negative indices too. These are what it traverses from the
right.
To read the same value right to left, we do:

>>> s='helloworld'

>>> s[slice(-5,-10,-1)]

‘wolle’
Confused? Here’s how we traverse right to left:

Python String Slice

We index everything from letters to digits and from spaces to characters.


Do you know about Python Decorators

5. Python Slicing Tuples


Now let’s take a look at performing Python Slicing on tuples. Check the following code:

Positive Indices-

>>> t=(1,2,3,4,5)

>>> t[slice(2,4)]

(3, 4)

Negative Indices-

Let’s traverse right to left.

>>> t[slice(-1,-5,-2)]

(5, 3)
6. Indexing to Create Python Slice

We have often sliced lists using [ : ]. Let’s try that one more time.

Indexing to Create Python Slicing

Remember the previous Python Slicing example? Now take a look at this-

>>> t[-1:-5:-2]

(5, 3)
So we concur that slicing is a way to choose indices for elements to show. What slice() really does is give us
indices for those. We can use the slice() function as a subscript.
Let’s revise Python Lambda Expressions
When indexing, what happens when we do not pass one or more of the values?

>>> t[:3] #From 0 to 2

(1, 2, 3)

>>> t[3:] #From 3 to end

(4, 5)
>>> t[:] #From beginning to end

(1, 2, 3, 4, 5)

a. Extended Python Slices with a step value


>>> t[::-1] #Reverse

(5, 4, 3, 2, 1)

>>> t[::-2] #Reverse with step=2

(5, 3, 1)

>>> t[:5:2] #Upto index 5, with step=2

(1, 3, 5)

>>> t[:5:-1] #Index 5 to end (already ahead of that), right to left; results in empty tuple

()
Do you know about Methods in Python

>>> t

(1, 2, 3, 4, 5)

>>> t[len(t)::-3] #End to front, step=3 right to left

(5, 2)

b. Resizing Lists in Python Slices


>>> l=[1,2,3,4,5]

>>> l[1:4]

[2, 3, 4]

>>> l[1:4]=[2,3,3.5,4]

>>> l

[1, 2, 3, 3.5, 4, 5]
The length of the slice on the right should be equal to that on the left.

c. Deleting Python Slices


We can also use the del keyword to delete a slice.

>>> del l[::4]


>>> l

[2, 3, 3.5, 5]
Let’s revise Python Data Structures
So, this was all in Python Slice. Hope you like our explanation.

7. Conclusion

Hence, in this Python Slice Tutorial, we saw the meaning of Slicing in Python. Moreover, we discussed Python
Slice() object and Python Slice function. Also, we learned about Python Slice String. Still, if any confusion in
Python Slice, ask freely in the comments. See you tomorrow with a new topic in Python. Till then, keep
practicing.

Python Subprocess Module | Subprocess vs Multiprocessing


by DataFlair Team · September 29, 2018

1. Objective – Python Subprocess Module

Last, we talked about Multiprocessing in Python. Today, we will see Python Subprocess Module. Moreover,
we will discuss Subprocess vs Multiprocessing in Python. Also, we will learn call, run, check call, check output,
communicate, and popen in Subprocess Module in Python. At last, we are going to understand all with the help
of syntax and example.
So, let’s start the Python Subprocess Module tutorial.

Python Subprocess Module | Subprocess vs Multiprocessing

2. Python Subprocess vs Multiprocessing


Seems like both help us facilitate concurrency or parallel programming. So what sets them apart?
You must check Python Generator vs Iterator

Subprocess- The subprocess module comes in handy when we want to run and control other programs that
we can run with the command line too. It lets us integrate external programs into Python code.

Subprocess vs Multiprocessing

Multiprocessing- The multiprocessing module is something we’d use to divide tasks we write in Python
over multiple processes. This lets us make better use of all available processors and improves
performance. This module has an API of the likes of the threading module.

3. What is Python Subprocess Module?

Are you through telling between the two? Okay. Time to tell you about subprocess. This module lets you spawn
new processes, connect to their input/error/output pipes, and acquire their return codes. It finds its proposal in
PEP 324 for version 2.4 and replaces the following modules/ functions in Python:
Have a look at Python Modules

os.system

os.spawn and related functions

os.popen and related functions

popen2*

commands*

4. Python Subprocess Call()


The call() function from the subprocess module lets us run a command, wait for it to complete, and get its return
code.

a. Syntax
It has the following syntax-

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

b. Examples
Let’s take a few simple examples of Subprocess Call in Python.
Let’s revise Python Operators

>>> subprocess.call('exit 1',shell=True)

>>> subprocess.call('ls -l',shell=True)

1
Since we set the shell to True, this function treats this as a complete command and runs it. This is a command
that lists out all files and folders in the current directory. Note that 1 is the return code, not the output of the
command’s execution. Here, it marks success.

5. Python Subprocess run()

Like call(), this function runs a command and returns a CompletedProcess instance.
Have a look at Python Decision Making Statements
a. Syntax
It has the following syntax-

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False,


shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None,
env=None)

b. Examples
Time for some examples of Python Subprocess run().

>>> subprocess.run(['ls','-l'],shell=True)

CompletedProcess(args=[‘ls’, ‘-l’], returncode=1)


This is the same command we saw in call(). Note how we mention shell=True; also note this returns a
CompletedProcess instance.
You must read about Python Variables

6. Python Subprocess check_call()

A call to this function runs the command with the arguments, waits for it to complete, then gets the return code.
If zero, it returns, else it raises CalledProcessError. Such an object holds the return code in the returncode
attribute.
a. Syntax
We have the following syntax-
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None,
timeout=None)

b. Examples
Let’s take a look at Python Subprocess check_call example

>>> subprocess.check_call('true',shell=True)

Traceback (most recent call last):


File “<pyshell#3>”, line 1, in <module>
subprocess.check_call(‘true’,shell=True)
File “C:\Users\Ayushi\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”, line 328, in
check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘true’ returned non-zero exit status 1.
For the false command, it always returns an error.
Do you know about Python Syntax and Semantics

7. Python Subprocess check_output()

This function runs the command with the arguments and returns the output. So far, the output was bound to the
parent process and we couldn’t retrieve it.
For a non-zero return code, it raises a CalledProcessError which has the return code in the returncode attribute.
a. Syntax
It has the following syntax-

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None,


errors=None, universal_newlines=False, timeout=None)

b. Examples
Now, the example of Python Subprocess check_output

>>> subprocess.check_output(["echo","Hello World!"],shell=True)

b'”Hello World!”\r\n’
In this example, we print the string Hello World! to the console.
Have a look at Python Exceptional Handling

8. Python Subprocess Communicate()

This interacts with the process and sends data to stdin. It reads data from stdout and stderr until it reaches the
end-of-file and waits for the process to terminate. What it returns is a tuple (stdout_data, stderr_data).
a. Syntax
Take a look at the syntax-

Popen.communicate(input=None, timeout=None)

b. Examples
Below is the example of Python Subprocess Communicate

>>> p=subprocess.Popen(["echo","hello world"],stdout=subprocess.PIPE,shell=True)


>>> p.communicate()

(b'”hello world”\r\n’, None)


Here, we use Popen to execute a child program in a new process. We will see this next. Meanwhile, we read the
input and output from the process using communicate(). Here, stdout is the process output. In case there’s an
error, we populate stderr.
Let’s revise Python Multithreading

9. Python Subprocess Popen()

Popen is a constructor from the subprocess class that executes a child program in a new process. This class uses
the Windows CreateProcess() function and Popen() lets us start a process.
a. Syntax
We have the following syntax-

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None,


preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *,
encoding=None, errors=None, text=None)

b. Examples
Let’s try the echo command with this Python Subprocess Popen example.

>>> proc=subprocess.Popen(['echo','"to stdout"'],stdout=subprocess.PIPE,shell=True)

>>> stdout_value=proc.communicate()[0]

>>> repr(stdout_value)

‘b\'”\\\\”to stdout\\\\””\\r\\n\”
Let’s take a tour to Python Network Programming
So, this was all in Python Subprocess Module. Hope you like our explanation.

10. Conclusion – Module in Python Subprocess

Hence, in this Python Subprocess Module, we saw the difference between subprocess and multiprocessing. You
are also no longer uninitiated to conventional functions from subprocess, the likes of call(), run(),
check_output(), and Popen(). Also, we understood the complete concept with the help of syntax and examples.
Is this explanation helps you, give us your feedback in the comments?

Python sys Module – Important Functions


by DataFlair Team · March 2, 2019

1. Objective
In our last Python tutorial, we discussed Python Subprocess. Today, we will discuss Python sys
Module. Moreover, we learn about functions like version, displayhook, stderr, and more. Also, we will see how
to import sys in Python.
So, let’s start the Python sys Module tutorial.

Python sys Module – Important Functions

2. What is Python sys Module?


Let’s get to know it first. The sys module in Python lets us access system-specific parameters and functions. It
gives us information about constants, functions, and methods of the interpreter.
To find out more about it, you can try one of two functions:

Summarized information- constants, functions, methods:

>>> dir(sys)

Detailed information-

>>> help(sys)

Some things you can do with it are:


You must take a look of Python os Module

>>> import sys #Get version information

>>> sys.version

‘3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]’

>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel=’final’, serial=0)

>>> sys.getrecursionlimit() #Get maximal recursion depth

1000

>>> sys.setrecursionlimit(1500) #Set maximal recursion depth

>>> sys.getrecursionlimit()

1500
Let’s revise CGI Programming in Python with functions

3. Command-line Arguments in Python sys Module


We can store the command-line arguments we pass to a script in a list we call sys.argv. The first item of this is
the name of the script; the arguments are next. Save the following code in a script:

import sys

print(sys.argv)

for i in range(len(sys.argv)):

if i==0:

print("The function is",sys.argv[0])

else: print("Argument:",sys.argv[i])

C:\Users\Ayushi\Desktop>py sysdemo.py 2 3
[‘sysdemo.py’,’2’,’3’]
The function is sysdemo.py
Argument:2
Argument:3

4. Changing the Output Behavior of the Shell


We’ve so often used the Python shell interactively and even as a calculator. But what if we wanted to change
how it delivers the output? Well, we can; we simply rebind sys.displayhook to a callable object.
Let’s revise Python Zipfile

>>> import sys

>>> x=42

>>> x

42

>>> print(x)
42

>>> def show(x):

print("Output:",x)

>>> sys.displayhook=show

>>> x

Output: 42

>>> print(x)

42
Output: None

5. Ways to Read and Write in Python sys Module


We can also use the readline() method to get input from the user:
Do you know about Python array Module

>>> print("Type in value: ",sys.stdin.readline()[:-1])

23

Type in value: 23
The following piece of code lets us print to the screen:

>>> sys.stdout.write('Way to write')

Way to write12

>>> sys.stdout.write('Way to write\n')

Way to write
13
Notice that it gives us the number of characters, which is why it gives us 13 instead of 12 when we give it a \n
newline character too at the end.

6. Getting Names of Modules


sys.modules in Python gives us a dictionary of the names of the modules existing in the current shell.

>>> import sys

>>> sys.modules
sys Module in Python

7. Investigating the Path in Python sys Module


sys.path in Python will give you a list of paths it will search in whenever you make an import.
You must read about Python Packages

>>> sys.path

[”, ‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\idlelib’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\lib’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\scrapy-1.5.1-
py3.7.egg’, ‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-
packages\\service_identity-17.0.0-py3.7.egg’,
‘C:\\Users\\Ayushi\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\parsel-1.5.0-
py3.7.egg’]
You can add a path to this with the append() method-

>>> sys.path.append('C:\\Users\\Ayushi\\Desktop')

Now, when you make a call to sys.path in Python, you can see this location in the list.
Have a look at Python Modules vs Packages

8. Getting the Copyrights in Python sys Module


sys.copyright in Python displays the copyright information on the currently-installed version of Python.

>>> print(sys.copyright)

Copyright (c) 2001-2018 Python Software Foundation.


All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
Let’s revise the Python Datetime Module

9. Exiting Current Flow of Execution in Python sys Module


sys.exit in Python lets the interpreter abruptly exit the current flow of execution.

>>> sys.exit

Exiting Current Flow of Execution

You can use a try-except block to catch this call-

>>> try:

sys.exit(1)

except SystemExit:

pass

10. Getting Reference Count and Platform


The getrefcount function gives us the count of references to an object where used. When in a program, this
value drops to 0, Python cleans up the memory for this variable. Let’s take an example.
Have a look at the Python Multiprocessing Module

>>> a=7

>>> sys.getrefcount(a)

37
To get the name of the platform we’re running Python on, we make a call to sys.platform in Python:

>>> sys.platform

‘win32’

11. Redirecting Output in Python sys Module


Instead of delivering the output to the console, you can log into a text file.

>>> import sys

>>> print('Hello') #Prints normally


Hello
Let’s revise deep and shallow copy in Python

>>> saveout=sys.stdout

>>> fsock=open('out.log','w')

>>> sys.stdout=fsock

>>> print('Message to log')

>>> sys.stdout=saveout

>>> fsock.close()

Redirecting Output in Python

12. Redirecting Error Information in Python sys Module


Using sys.stderr and a text file in Python, we can log error information to the text file. See how:

>>> import sys

>>> fsock=open('error.log','w') #Opening the file

>>> sys.stderr=fsock #Redirecting standard error by assigning file object of file to stderr

>>> raise Exception('this is an error')

>>> fsock.close()
Redirecting Error Information in Python sys Module

Note that this traceback doesn’t show up in the log file until we close its file object in Python (which, in this
case, is fsock).
Read – Python Assert Statements
Another way we use sys.stderr is as follows:

>>> for i in range(3):

sys.stderr.write('Hello')

Hello5
Hello5
Hello5
Unlike stdout, stderr does not add carriage returns.
A similar function flush() lets us flush write buffers.

13. More Functions in Python sys Module


The purpose of this tutorial is to get you started with the sys module; there is so much more to it.
Let’s discuss Python Unit Testing
To get a list of all the built-in functions and methods we have for sys, try the calling the dir() function on it.
Python sys Module – Functions

Try them out, won’t you?


So, this was all in Python sys Module. Hope you like our explanation.

14. Conclusion – sys Module in Python


Hence, today in this Python sys module tutorial, we took a brief look at various functions and methods available
with the Python sys module, including argv, stdin, stdout, stderr, setrecursionlimit, and exit. We also discussed
the meaning of import sys in Python. Still, if you have any doubt regarding Python sys Module, ask in the
comment tab.
See also –

Python for Machine Learning – Know the importance in Machine


Learning Projects
by DataFlair Team · September 13, 2019

Machine learning is nothing but using data to make a machine to make intelligent decisions. It is based on
recognizing and learning through patterns in data. Intelligent algorithms are then built by extracting, processing,
defining, cleaning, arranging and then understanding the data. To perform such tedious tasks, why should one
use Python? The answer is simple – It is easy to understand! Adding Python to the implementation process has
helped engineers to validate various ideas. and that’s why today DataFlair has come up with a new topic – The
importance of Python for Machine Learning.

Before we start with the tutorial, it is recommended to save the link Python Master Guide

Machine Learning with Python

Consider using Python for machine learning. Now all the questions like- How can these experiences be made
real? Or What programming language will be best for this conversion? All of these will vanish away. Python
offers all the skillsets that are required for a machine learning or AI project – stability, flexibility and a large
number of tools. Python helps developers to be productive and confident about the product that they are
manufacturing, from the stages of development to deployment and till the maintenance stage. These add to the
popularity of the Python language.

Why Python for Machine Learning?

Below are some of the reasons which will tell you why everyone is using Python for machine learning –

1. Simple and consistent


Python’s simple syntax allows developers to write codes that are reliable, concise and readable. This allows
them to only struggle with the solution of the problem and not the code syntax, increasing the productivity of
the overall process of development. This simplicity becomes appealing to other developers and urges them to
learn python. It’s greater human understanding makes it easier for coders/developers to invent various
functional models.

Multiple developments and Collaborative implementation type of projects majorly use Python as the language
for code. Prototypes are built faster as the complex machine learning tasks and the testing process can be done
quickly. While we find the reason behind greater functionality- frameworks, libraries, and extensions are the
reasons that come back as responses.
Now, another reason to learn Python for machine learning is the amazing libraries and frameworks of Python.

2. Libraries and Frameworks


To help developers in the development process, a number of python libraries and frameworks get involved. It is
nothing but a pre-written code that can be used to solve a common programming problem. Python has a rich
bank of libraries for machine learning, some of them are- TensorFlow, Keras, and Scikit-learn, Numpy, Scipy,
Pandas, Seaborn, etc. In these NumPy and Scipy are specifically for scientific and advance computing
respectively. Pandas generally used for Data analysis and Seaborn specifically for data visualization. With these
pre-defined codes the speed of development increase to an appreciable level.

3. Platform Independence
It basically means one can freely shift from one machine to another without making changes to the actual code
(or with minimal changes). This functionality is allowed by python’s framework. This is also one of the keys to
the popularity of the python language. It supports many platforms such as- Windows, macOS, and Linux. Most
of the companies use their own machine containing powerful GPUs to train their machine learning models.
Python being platform-independent can make this overall training very cheap and easier.

4. Great Community Base


The fact is well that Python’s community has grown across the globe, and especially in the world of machine
learning and data science. There are active communities that contribute to the large exchange of information
which involves solutions to problems. For any problem you come across, chances are very high that someone
out there has already gone through that same problem and solved it successfully. Hence you can find guidance
and bits of advice at any level of doubt. You won’t be the only one who went through it. Also, you may come to
know some of the best results according to your specifications, all you need to do it to turn to the huge python
community.

Summary

Machine learning has made a massive effect on the current world we are living in, with new applications
emerging all the time. Developers are choosing Python at every step of problem-solving. While there are
programming languages other than python that be used for AI projects, but python is cutting edges of all, with
significant considerations. According to the people who practice python, they believe – python is a language
that is well suited for machine learning and AI. And if you are still wondering why, read this twice! And choose
python for your next AI project.

Python Machine Learning Tutorial – Tasks and Applications


by DataFlair Team · September 28, 2018

1. Python Machine Learning Tutorial


In this Python Machine Learning Tutorial, we will introduce you to machine learning with Python. Moreover,
we will discuss Python Machine Learning tasks, steps, and applications. Then, we will take a look at 10 tech
giants that adapt Python Machine Learning to improve what they do.

So, let’s start the Python Machine Learning Tutorial.


Python Machine Learning Tutorial – Tasks and Applications

2. Introduction to Machine Learning With Python


In this Python Machine Learning Tutorial, Machine Learning also termed ML. It is a subset of AI (Artificial
Intelligence) and aims to grants computers the ability to learn by making use of statistical techniques. It deals
with algorithms that can look at data to learn from it and make predictions.
Do you know about statistics in Python

3. Tasks in Machine Learning Using Python


With Python Machine Learning, we divide the tasks of Machine Learning Algorithms in Python into two
broad categories- Supervised and Unsupervised.
Python Machine Learning Tutorial – Tasks of Machine learning

a. Supervised Learning
Here, a learning signal/ feedback is available to the system; we give it to sample data to learn from. The
computer holds example inputs and desired outputs with the goal of learning a general rule that maps inputs to
outputs. One such example of Python Machine Learning will be to search for images on Facebook using
keywords centered around the contents of the image. Under Supervised Learning, we have the following kinds
of Python machine Learning-

Semi-Supervised Learning- The computer receives an incomplete training signal. This is a training set
with some target outputs missing.

Active Learning- The computer can secure training labels for only some instances. It also needs to make an
optimal choice of objects to secure labels.

Reinforcement Learning- In this, the training data comes as feedback on how a program acts in a dynamic
environment. Examples of this include driving a vehicle or playing against an opponent.

Steps involved in Supervised Machine Learning-

Training

testing

Among many Supervised Machine Learning Algorithms for beginners we observe, here we list some-
Let’s discuss Machine Learning Applications

Decision trees
Support Vector Machines

Naïve Bayes

k-nearest neighbor

Linear regression

b. Unsupervised Learning
In unsupervised learning, the Python Machine Learning Algorithm receives no labels; we only give the machine
a set of inputs. It must rely on itself to find structure in its input. This kind of learning can be a goal or a means
toward future learning. We can classify unsupervised learning as-

Clustering- The act of grouping data inherently. One example of this will be to group consumers by their
shopping habits so they can target the right consumers to advertise.

Association- In association, we identify rules explaining large sets of our data. One example will be to
associate books around author/ category.

Of the many Unsupervised Machine Learning Algorithms, we observe, here are a couple-

K-means clustering

Hierarchical clustering

4. Steps in Python Machine Learning


We follow the following steps in Machine Learning Using Python-

Collecting data.

Filtering data.

Analyzing data.

Training algorithms.

Testing algorithms.

Using algorithms for future predictions.

5. Applications of Python Machine Learning


Where does machine learning with Python come to use? Let’s learn Applications of Machine Learning with
Python:
Python Machine Learning Tutorial – Applications of Machine learning

a. Fighting and filtering webspam and malware


With rule-based spam filtering, latest tricks by spammers can go unnoticed. e-mail clients make use of machine
learning to ensure its spam filters stay updated. Other than that, imagine getting to Google and searching for
something only to find irrelevant listings right at the top. To fight these situations, Google uses ‘deep learning’,
a neural network that takes data from users and from NLP, and determines the nature of the email in question.
Some spam-filtering techniques under ML are Multi-Layer Perceptron and C 4.5 Decision Tree Induction.
Let’s have a look at Python ML Techniques

b. Refining search-engine results


Suppose you went up to Google and typed in the keywords “DIY lampshade”. If you visit one or more of the
top listings and stay for a while, Google assumes it did a good job serving your request. If, however, you end up
on the third page and have not visited any result, Google knows it could have done better. So, it improves
search results next time.

c. Virtual Personal Assistants


With assistants like Siri, Alexa, and Google Now, the term virtual personal assistant needs no explanation. This
help finds information for you, make calls, set alarms, and check the weather among all other things they can
do. And to make this easy for you, all they need you to do is use your voice and command them to do it for you.
When you’ve got your hands filthy, or if you’ve just woken up and do not wish to lay your eyes on the light of a
screen, this comes in handy. Not to forget the huge importance of this for those handicapped.
How you involve with them helps them collect and refine that information. This is machine learning and this is
how they generate better results next time.
d. Social Media Services
On social media, facilities like ‘People You May Know’ and ‘Face Recognition’ work via machine learning.
Considering your activity like the profiles you visit, the people you befriend, the people you tag, Facebook
curates a list of suggestions for you to enrich your experience and make you stay.

e. Online customer support


Some websites will pop a live chat option up to make your stay in case you need a query to be answered. For
some, it isn’t live but is a chatbot. Such a bot pulls information from the website and delivers it to the customer.
The machine learning algorithms make it possible to improve this experience.
Let’s discuss Train and Test Set in Python ML

f. Product recommendations
Shopping giants like Jabong and Amazon curate a list of products similar to the ones you’re visiting. They also
mail you shopping suggestions. This is machine learning behind the scenes; it pays attention to your past
purchases, wishlist, cart contents, brand preferences, and so.

g. Online fraud detection


Companies like PayPal use ML to fight against issues like money laundering. They compare millions of
transactions to differentiate between those legitimate and illegitimate.

h. Video Surveillance
With ML, video surveillance systems can detect a possible crime ahead of it. Risque behavior like people
standing motionless for a while monitoring a situation, napping on a bench, and following another individual
can alert human attendants. When this can prevent a mishap and save a life, incidents like these help improve
such surveillance services.
Let’s know why we should learn Machine Learning

i. Automatic Translation
ML makes it possible to translate text from one language to another. The algorithm learns how words fit
together and use that to improve the translation. This is also possible to text on images. This is done with neural
networks to identify letters in the images. It translates the text and then puts it back onto the picture.

6. Companies Using Python Machine Learning


Of many others, the following 10 companies make use of machine learning tools and technologies to grow and
improve their functions.
Python Machine Learning Tutorial – Companies

a. Apple
Apple was the first to ship a voice assistant on a smartphone. And with HomePod, it aspires to take this a step
further.

Python Machine Learning Tutorial – Apple

With the rising competition, it is the technology and the end user that benefits. Apple paid $200 to purchase
Lattice Data, which can convert unstructured data into a structured form using ML. It also develops in-house
machine learning systems.

b. Google
Python Machine Learning Tutorial – Google

Google offers, to developers, multiple cloud-based services. One of these is the Google Cloud AI machine
learning tools. Recently, Google launched an AI chatbot that will answer messages for you. This is like a
sophisticated auto-response email.

c. Microsoft

Python Machine Learning Tutorial – Microsoft

Microsoft purchased LinkedIn a few years ago at $26 billion and has lately been the third-biggest spender on
acquisitions. Maluuba, a Canadian tech company that houses a very impressive deep learning research lab for
Natural Language Understanding.

d. Twitter

Python Machine Learning Tutorial – Twitter

Ever since Facebook changed its algorithm to favor posts from friends and family over news articles from
reputed sources, Twitter’s profitability has raised. Here, machine learning makes it possible to find out what
people might be interested in and curate content for them.

e. Intel

Python Machine Learning Tutorial – Intel

Intel is the largest chipmaker in the world. In the last few years, it acquired Nervana Systems (manufacturer of
chips for data center servers) at a capital of $400 million. Nervana chips can transfer data at around 2.4
terabytes per second at a low latency.
Have a look at the advantages & disadvantages of Machine learning
f. Baidu

Python Machine Learning Tutorial – Baidu

Baidu is a Chinese search giant and takes a keen interest in Natural Language Processing. It also aims to
develop a functioning voice-activated search facility. Recently, it acquired Kitt.ai, which has a portfolio of
chatbots and voice-based applications. Very easily, Baidu is the 10th largest spender on acquisitions.

g. IBM

Python Machine Learning Tutorial – IBM

Back in the 1990s, IBM challenged Garry Kasparov, Russia’s greatest chess player, to a match against Deep
Blue, a computer by IBM. Kasparov won the first match and flunked the next few. Later, computer Watson AI
beat contestants on the quiz show Jeopardy!. More recently, the machine won the ancient board game ‘Go’ in a
recent human-vs-machine contest.

h. Salesforce

Python Machine Learning Tutorial – Salesforce

Salesforce is the sixth-largest buyer of AI companies over the last five years, CB Insights claims. Recently, it
said it had a year of ‘Einstein’ technology- one that analyzes each aspect of a customer’s relationship with a
company.

i. Pindrop
Python Machine Learning Tutorial – Pindrop

Pindrop claims to present a pioneering technology for recognizing fraudulent activity over the phone channel. In
what it calls ‘phoneprinting’, for every call, it analyzes 1,300 unique call features and creates an audio
fingerprint for each. Such features include noise, location, number history, and call type. It flags suspicious calls
and can spot ID spoofing, voice distortion, and social engineering.

j. Qubit

Python Machine Learning Tutorial- Qubit

Qubit has an AI-powered personalized shopping app, Aura. This has a database of products in a range of
categories like fashion, clothing, and cosmetics. Pending patents suggest an Instagram-like feed of product
images.
So, this was all in Python Machine Learning Tutorial. Hope you like our explanation of Machine Learning
Python Course.

7. Python Machine Learning Tutorial – Conclusion


Hence, in this Python Machine Learning Tutorial, we discussed what is Python Machine Learning and tasks in
Python and Machine Learning. Moreover, we discussed applications of Python Machine Learning. Also, we
saw companies using Machine Learning with Python. By now, we realize machine learning is powerful. Let’s
delve into the world of ML and learn something new. Still, if you have any doubt regarding Python Machine
Learning, ask in the comment tab.

Python Machine Learning Environment Setup – Installation Process


by DataFlair Team · February 18, 2019

1. Python Machine Learning Environment Setup


In this tutorial of Python Machine Learning Environment Setup, we will show you the way with setting your
machine up for a Machine Learning environment with Python. Moreover, for Python Machine Learning
installation, we will see the process to install Python and the needed Python Libraries such as NumPy, SciPy,
Matplotlib etc.
So, let’s start the Python Machine Learning Environment Setup.
2. How to Install Python?
Before anything else, you will need to Install Python on your machine. So, here are the steps to install Python.

a. Plain download
You can download from the official website-
https://www.python.org/downloads/
At the time of writing, the latest version of Python is Python 3.7. This is what the installer looks like-
For a detailed guide on how to set up Python for your machine, read up on A Step-by-Step Guide to Install
Python for Windows.

b. Installing with Anaconda


Or you can install it with Anaconda-
https://www.anaconda.com/download/
This downloads Anaconda for us. Now to install it, we run the installer-
Installing With Anaconda

Installing With Anaconda


Installing With Anaconda

Python Machine Learning Environment Setup

And for the final options-


Installing Anaconda

And then, it installs Anaconda-

Installing Anaconda
3. Starting and Updating Anaconda
To confirm that your Anaconda environment is up to date, follow these steps-
1.Open Anaconda Prompt and type the following-
(base) C:\Users\Ayushi>conda -V

conda 4.5.8
2.Now the following command tells you about your Python environment-
(base) C:\Users\Ayushi>python -V

Python 3.6.5 :: Anaconda, Inc


3.Then, to confirm that the conda environment you hold is up to date-
Have a look at the future of Machine Learning

(base) C:\Users\Ayushi>conda update conda

Solving environment: done


# All requested packages already installed.

(base) C:\Users\Ayushi>conda update anaconda

Solving environment: done


# All requested packages already installed.

4. Installing Needed Python Libraries


Working with machine learning, we will need a few Python Libraries often. Here’s a list of some of those-

a. NumPy

Python Libraries – NumPy

NumPy, a Python library that will let you handle multi-dimensional arrays and matrices. It also offers multiple
high-level mathematical functions to operate on these.
You can install it as-

conda install numpy

b. SciPy
Python Libraries – SciPy

SciPy is a scientific- and technical- computing Python library that is free and open-source. Install it as-

conda install scipy

c. Matplotlib

Python Library – Matplotlib

Matplotlib, a Python library for plotting and has NumPy as its numerical mathematics extension. Install it as-

conda install matplotlib

d. pandas

Python Library – Pandas

pandas is a software library for Python that comes in handy when working with data manipulation and analysis.
Install it with the following command-

conda install pandas

e. statsmodels

Python Library – StatsModels


This is a Python package that lets users explore data, estimate statistical models, and carry out statistical tests.
You can install it as-

conda install statsmodels

f. seaborn

Python Library – Seaborn

This is a Python library for data visualization and is based on Matplotlib. It offers a high-level interface for
plotting statistical graphs that are attractive and informative. You can install it with the following command-

conda install seaborn

g. scikit-learn

Python Library – Scikiy Learn

This is a free software ML library for Python. It offers facilities like classification, regression, and clustering
algorithms.
Let’s discuss Machine Learning Algorithms
To find out which version of a library you’re using, you can run the following piece of code in the interpreter-

>>> import scipy

>>> scipy. version

‘1.1.0’
So, this was all in Python Machine Learning Environment Setup. Hope you like our explanation.

5. Conclusion
Hence, in this Python Machine Learning Environment Setup, we discussed how to install Python. Moreover, we
saw the starting and updating of Anaconda. Also, in this Python machine Learning Environment Setup, we
learned about the need libraries. If you follow all the steps mentioned above, you will successfully set up a
machine learning environment for Python at a beginner level. Still, if any doubt regarding Python Machine
Learning Environment Setup, ask in the comment tab. We will definitely get back to you.

Data Preprocessing, Analysis & Visualization – Python Machine


Learning
by DataFlair Team · September 28, 2018

1. Objective
Today in this Python Machine Learning Tutorial, we will discuss Data Preprocessing, Analysis &
Visualization. Moreover in this Data Preprocessing in Python machine learning we will look at rescaling,
standardizing, normalizing and binarizing the data. Also, we will see different steps in Data Analysis,
Visualization and Python Data Preprocessing Techniques.

So, let’s start machine Learning with Python Data Preprocessing.

Data Preprocessing, Analysis & Visualization – Python Machine Learning

2. Data Preprocessing in Python Machine Learning


Machine Learning algorithms don’t work so well with processing raw data. Before we can feed such data to
an ML algorithm, we must preprocess it. In other words, we must apply some transformations on it. With data
preprocessing, we convert raw data into a clean data set.
Some ML models need information to be in a specified format. For instance, the Random Forest algorithm does
not take null values. To preprocess data, we will use the library scikit-learn or sklearn in this tutorial.

3. Python Data Preprocessing Techniques


Let’s talk about seven such techniques for Data Preprocessing in Python Machine Learning.
Let’s have a look at data Structure in Python

a. Rescaling Data
For data with attributes of varying scales, we can rescale attributes to possess the same scale. We rescale
attributes into the range 0 to 1 and call it normalization. We use the MinMaxScaler class from scikit-learn. Let’s
take an example.

>>> import pandas, scipy, numpy

>>> from sklearn.preprocessing import MinMaxScaler

>>> df=pandas.read_csv( 'http://archive.ics.uci.edu/ml/machine-learning-databases/wine-


quality/winequality-red.csv ',sep=';')

>>> array=df.values

>>> #Separating data into input and output components

>>> x=array[:,0:8]

>>> y=array[:,8]

>>> scaler=MinMaxScaler(feature_range=(0,1))
>>> rescaledX=scaler.fit_transform(x)

>>> numpy.set_printoptions(precision=3) #Setting precision for the output

>>> rescaledX[0:5,:]

This gives us values between 0 and 1. Rescaling data proves of use with neural networks, optimization
algorithms and those that use distance measures like k-nearest neighbors and weight inputs like regression.

b. Standardizing Data
With standardizing, we can take attributes with a Gaussian distribution and different means and standard
deviations and transform them into a standard Gaussian distribution with a mean of 0 and a standard deviation
of 1. For this, we use the StandardScaler class. Let’s take an example.
Do you know about Python Statistics

>>> from sklearn.preprocessing import StandardScaler

>>> scaler=StandardScaler().fit(x)

>>> rescaledX=scaler.transform(x)

>>> rescaledX[0:5,:]

c. Normalizing Data
In this task, we rescale each observation to a length of 1 (a unit norm). For this, we use the Normalizer class.
Let’s take an example.

>>> from sklearn.preprocessing import Normalizer

>>> scaler=Normalizer().fit(x)

>>> normalizedX=scaler.transform(x)

>>> normalizedX[0:5,:]
Normalizing Data in Data Preprocessing

d. Binarizing Data
Using a binary threshold, it is possible to transform our data by marking the values above it 1 and those equal to
or below it, 0. For this purpose, we use the Binarizer class. Let’s take an example.
Learn about Python Data Science Environment Setup

>>> from sklearn.preprocessing import Binarizer

>>> binarizer=Binarizer(threshold=0.0).fit(x)

>>> binaryX=binarizer.transform(x)

>>> binaryX[0:5,:]

This marks 0 over all values equal to or less than 0, and marks 1 over the rest. When you want to turn
probabilities into crisp values, this functionality comes handy.

Binarizing Data in Data Preprocessing

e. Mean Removal
We can remove the mean from each feature to center it on zero.

>>> from sklearn.preprocessing import scale

>>> data_standardized=scale(df)

>>> data_standardized.mean(axis=0)
array([ 3.555e-16, 1.733e-16, -8.887e-17, -1.244e-16, 3.910e-16,
-6.221e-17, 4.444e-17, 2.364e-14, 2.862e-15, 6.754e-16,
1.066e-16, 8.887e-17])

>>> data_standardized.std(axis=0)

array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

f. One Hot Encoding


When dealing with few and scattered numerical values, we may not need to store these. Then, we can perform
One Hot Encoding. For k distinct values, we can transform the feature into a k-dimensional vector with one
value of 1 and 0 as the rest values.

>>> from sklearn.preprocessing import OneHotEncoder

>>> encoder=OneHotEncoder()

>>> encoder.fit([[0,1,6,2],

[1,5,3,5],

[2,4,2,7],

[1,0,4,2]

])

OneHotEncoder(categorical_features=’all’, dtype=<class ‘numpy.float64’>,


handle_unknown=’error’, n_values=’auto’, sparse=True)

>>> encoder.transform([[2,4,3,4]]).toarray()

array([[0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0.]])

g. Label Encoding
Some labels can be words or numbers. Usually, training data is labelled with words to make it readable. Label
encoding converts word labels into numbers to let algorithms work on them. Let’s take an example.
Let’s discuss Python Packages

>>> from sklearn.preprocessing import LabelEncoder

>>> label_encoder=LabelEncoder()

>>> input_classes=['Havells','Philips','Syska','Eveready','Lloyd']

>>> label_encoder.fit(input_classes)

LabelEncoder()
>>> for i,item in enumerate(label_encoder.classes_):

print(item,'-->',i)

Eveready –> 0
Havells –> 1
Lloyd –> 2
Philips –> 3
Syska –> 4
This gives us a set of numeric labels that map to these words. Let’s confirm this:

>>> labels=['Lloyd','Syska','Philips']

>>> label_encoder.transform(labels)

array([2, 4, 3], dtype=int32)

>>> label_encoder.inverse_transform(label_encoder.transform(labels))

array([‘Lloyd’, ‘Syska’, ‘Philips’], dtype='<U8′)

4. Analyzing Data in Python Machine Learning


Assuming that you have loaded your dataset using pandas (which if you haven’t, refer to Python Pandas
Tutorial to learn how), let’s find out more about our data.

a. Describing the dataset


Using the method describe(), we can find out parameters like count, mean, std, and max.

>>> df.describe()

Analyzing Data in Python Machine Learning

b. Shape of the dataset


Simply, the shape tuple will give us the dimensions of the dataset.
>>> df.shape

(1599, 12)

c. Extracting data from the dataset


Now if we want only the first ten rows from the dataset, we can call the head() method on it. To this, we can
pass it the argument 10.

>>> df.head(10)

Extracting data from the dataset – Data Analyzing

d. Performing operations around a variable


We can perform certain operations on a variable. For instance, here, we demonstrate how to group data on a
variable. For this, we use the groupby() function.
Have a look at Python NumPy

>>> df.groupby('quality').size()

Performing operations around a variable in data Analyzing

5. Visualizing Data-Univariate Plots in Python Machine Learning


Finally, when we want to visualize data as plots and charts to learn more about it, we can use pandas with
Matplotlib. We will discuss two kinds of plots- univariate and multivariate.
A univariate plot suggests we’re only examining one variable.
a. Histograms
Since histograms group data into bins and give us an idea of how many observations each bin holds, this is a
good way to visualize data for ML. The shapes of the bins tell us whether an attribute is Gaussian, skewed,
or has an exponential distribution. It also hints us about outliers.

>>> import matplotlib.pyplot as plt

>>> df.hist()

>>> plt.show()

Histograms in Python Machine Learning

Perhaps the attributes ‘total sulfur dioxide’, ‘free sulfur dioxide’, and ‘residual sugar’ have an exponential
distribution. Attributes ‘density’, ‘pH’, ‘fixed acidity’, and ‘Volatile acidity’ have Gaussian or nearly Gaussian
distributions.
Let’s discuss Python Descriptive Statistics

b. Density Plots
A density plot appears to be an abstracted histogram. Each bin has a smooth curve drawn through its top. Your
eyes can rest now.

>>> df.plot(kind='density',subplots=True,sharex=False)

>>> plt.show()
Density Plots in Python Machine Learning

Density Plot in Python Machine Learning

This gives us a clearer idea.

c. Box and Whisker Plots


A box plot summarizes how each attribute is distributed. It also draws a line for the median and a box around
the 25th and 75th percentiles. Whiskers tell us how the data is spread, and the dots outside the whiskers give
candidate outlier values. Let’s plot this for our dataset.
>>> df.plot(kind='box',subplots=True,sharex=False,sharey=False)

fixed acidity AxesSubplot(0.125,0.11;0.0545775×0.77)


volatile acidity AxesSubplot(0.190493,0.11;0.0545775×0.77)
citric acid AxesSubplot(0.255986,0.11;0.0545775×0.77)
residual sugar AxesSubplot(0.321479,0.11;0.0545775×0.77)
chlorides AxesSubplot(0.386972,0.11;0.0545775×0.77)
free sulfur dioxide AxesSubplot(0.452465,0.11;0.0545775×0.77)
total sulfur dioxide AxesSubplot(0.517958,0.11;0.0545775×0.77)
density AxesSubplot(0.583451,0.11;0.0545775×0.77)
pH AxesSubplot(0.648944,0.11;0.0545775×0.77)
sulphates AxesSubplot(0.714437,0.11;0.0545775×0.77)
alcohol AxesSubplot(0.77993,0.11;0.0545775×0.77)
quality AxesSubplot(0.845423,0.11;0.0545775×0.77)
dtype: object

>>> plt.show()

Box and Whisker Plots in Python Machine Learning

Here, attributes like ‘total sulfur dioxide’, ‘sulphates’, and ‘residual sugar’ appear skewed toward smaller
values.
Do you know about Python Machine Learning Techniques

6. Visualizing Data-Multivariate Plots in Python Machine Learning


A multivariate analysis examines more than two variables. For two variables, we call it bivariate.

a. Correlation Matrix Plot


Such a plot denotes how changes between two variables relate. Two variables that change in the same direction
are positively correlated. A change in opposite directions implies negative correlation. Let’s plot a correlation
matrix.

>>> correlations=df.corr()

>>> fig=plt.figure()

>>> ax=fig.add_subplot(111)

>>> cax=ax.matshow(correlations,vmin=-1,vmax=1)

>>> fig.colorbar(cax)

<matplotlib.colorbar.Colorbar object at 0x086D7A30>

>>> ticks=numpy.arange(0,9,1)

>>> ax.set_xticks(ticks)

>>> ax.set_yticks(ticks)

>>> plt.show()

Correlation Matrix Plot in Visualizing Data

This matrix is symmetrical around a left diagonal.

b. Scatterplot Matrix
Scatterplot matrices depict how two variables relate as dots in two dimensions. Plotting all scatterplots for a
data together in one place results in a scatterplot matrix. These plots can spot structured relationships between
variables. Let’s take an example.
Let’s learn about Python Compilers

>>> pandas.plotting.scatter_matrix(df)

Scatterplot Matrix in Data Visualizing

This is symmetrical too. The left diagonal has histograms of the attributes because it doesn’t make much sense
to plot an attribute’s scatterplot with itself.

So, this was all in Python machine Learning Data Preprocessing, Visualizing and Analyzing. Hope you like our
explanation

7. Conclusion
Hence, in this Python Machine Learning Tutorial, we discussed Machine Learning with Python data
Preprocessing. Also, we discussed the Data Analysis and Data Visualization for Python Machine Learning. We
saw rescaling, normalizing, binarizing, and standardizing the data in Python machine Learning Data
Preprocessing. Still, if you have any doubt regarding Data Preprocessing, ask in the comment tab.

Train and Test Set in Python Machine Learning – How to Split


by DataFlair Team · July 10, 2019

1. Objective

In our last session, we discussed Data Preprocessing, Analysis & Visualization in Python ML. Now, in this
tutorial, we will learn how to split a CSV file into Train and Test Data in Python Machine Learning.
Moreover, we will learn prerequisites and process for Splitting a dataset into Train data and Test set in Python
ML.

So, let’s begin How to Train & Test Set in Python Machine Learning.

Train and Test Set in Python Machine Learning – How to Split

2. Training and Test Data in Python Machine Learning


As we work with datasets, a machine learning algorithm works in two stages. We usually split the data around
20%-80% between testing and training stages. Under supervised learning, we split a dataset into a training data
and test data in Python ML.

Train and Test Set in Python Machine Learning

a. Prerequisites for Train and Test Data


We will need the following Python libraries for this tutorial- pandas and sklearn.
We can install these with pip-

pip install pandas


pip install sklearn

We use pandas to import the dataset and sklearn to perform the splitting. You can import these packages as-

>>> import pandas as pd

>>> from sklearn.model_selection import train_test_split

>>> from sklearn.datasets import load_iris

Do you Know about Python Data File Formats – How to Read CSV, JSON, XLS

3. How to Split Train and Test Set in Python Machine Learning?

Following are the process of Train and Test set in Python ML. So, let’s take a dataset first.

How to Split Train and Test Set in Python Machine Learning

a. Loading the Dataset


Let’s load the forestfires dataset using pandas.

>>> data=pd.read_csv('forestfires.csv')

>>> data.head()
Train and Test Set in Python Machine Learning

b. Splitting
Let’s split this data into labels and features. Now, what’s that? Using features, we predict labels. I mean using
features (the data we use to predict labels), we predict labels (the data we want to predict).

>>> y=data.temp

>>> x=data.drop('temp',axis=1)

Temp is a label to predict temperatures in y; we use the drop() function to take all other data in x. Then, we split
the data.

>>> x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

>>> x_train.head()

Train and Test Set in Python Machine Learning

>>> x_train.shape

(413, 12)
Do you Know How to Work with Relational Database with Python

>>> x_test.head()

Train and Test Set in Python Machine Learning

>>> x_test.shape
(104, 12)
The line test_size=0.2 suggests that the test data should be 20% of the dataset and the rest should be train data.
With the outputs of the shape() functions, you can see that we have 104 rows in the test data and 413 in the
training data.

c. Another Example
Let’s take another example. We’ll use the IRIS dataset this time.

>>> iris=load_iris()

>>> x,y=iris.data,iris.target

>>> x_train,x_test,y_train,y_test=train_test_split(x,y,

train_size=0.5,

test_size=0.5,

random_state=123)

>>> y_test

array([1, 2, 2, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 2, 2, 2, 0, 0, 1, 0, 0, 2,
0, 2, 0, 0, 0, 2, 2, 0, 2, 2, 0, 0, 1, 1, 2, 0, 0, 1, 1, 0, 2, 2,
2, 2, 2, 1, 0, 0, 2, 0, 0, 1, 1, 1, 1, 2, 1, 2, 0, 2, 1, 0, 0, 2,
1, 2, 2, 0, 1, 1, 2, 0, 2])

>>> y_train

array([1, 1, 0, 2, 2, 0, 0, 1, 1, 2, 0, 0, 1, 0, 1, 2, 0, 2, 0, 0, 1, 0,
0, 1, 2, 1, 1, 1, 0, 0, 1, 2, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0,
1, 2, 2, 2, 2, 0, 1, 0, 1, 1, 0, 1, 2, 1, 2, 2, 0, 1, 0, 2, 2, 1,
1, 2, 2, 1, 0, 1, 1, 2, 2])
Let’s explore Python Machine Learning Environment Setup

4. Plotting of Train and Test Set in Python

We fit our model on the train data to make predictions on it. Let’s import the linear_model from sklearn, apply
linear regression to the dataset, and plot the results.

>>> from sklearn.linear_model import LinearRegression as lm

>>> model=lm().fit(x_train,y_train)

>>> predictions=model.predict(x_test)

>>> import matplotlib.pyplot as plt


>>> plt.scatter(y_test,predictions)

<matplotlib.collections.PathCollection object at 0x0651CA30>

>>> plt.xlabel('True values')

Text(0.5,0,’True values’)

>>> plt.ylabel('Predictions')

Text(0,0.5,’Predictions’)
Read about Python NumPy – NumPy ndarray & NumPy Array

>>> plt.show()

Train and Test Set in Python Machine Learning

0.9396299518034936
So, this was all about Train and Test Set in Python Machine Learning. Hope you like our explanation.

5. Conclusion

Today, we learned how to split a CSV or a dataset into two subsets- the training set and the test set in Python
Machine Learning. We usually let the test set be 20% of the entire data set and the rest 80% will be the training
set. Furthermore, if you have a query, feel to ask in the comment box.

4 Machine Learning Techniques with Python


by DataFlair Team · September 28, 2018

1. Python Machine Learning Techniques


In our last session, we discussed Train and Test Set in Python ML. Here, In this Machine Learning
Techniques tutorial, we will see 4 major Machine Learning Techniques with Python: Regression, Classification,
Clustering, and Anomaly Detection.
So, let’s look at Python Machine Learning Techniques.

4 Machine Learning Techniques with Python

2. Machine Learning Techniques vs Algorithms

While this tutorial is dedicated to Machine Learning techniques with Python, we will move over to algorithms
pretty soon. But before we can begin focussing on techniques and algorithms, let’s find out if they’re the same
thing.

A technique is a way of solving a problem. This is quite generic as a term. But when we say we have an
algorithm, we mean we have an input and we desire certain output from it. We have clearly defined what steps
to follow to get there. We will go the lengths to say an algorithm may make use of multiple techniques to get to
the output.
Do you know Applications of Machine Learning
Now that we have distinguished between the two, let’s find out more about Machine Learning techniques.

3. Machine Learning Techniques with Python


Python Machine Learning Techniques are 4 types, let’s discuss them:

a. Machine Learning Regression


The dictionary will tell you that to regress is to return to a former state- one that is often less developed. In
books of statistics, you will find regression to be a measure of how one variable’s mean and corresponding
values of other values relate to each other. But let’s talk about it how you will see it.
Also, Read Python Linear Regression & Chi-Square Test

Python Machine Learning Techniques – Machine Learning Regression

i. Regressing to the Mean


Francis Galton, Charles Darwin’s half-cousin, observed sizes of sweet peas over generations. What he
concluded was that letting nature do its job will result in a range of sizes. But if we selectively breed sweet peas
for size, it makes for larger ones. With nature at the steering wheel, even bigger peas begin to produce smaller
offsprings with time. We have a certain size for peas that varies, but we can map these values to a specific line
or curve.

ii. Another Example- Monkeys and Stocks


In 1973, Burton Malkiel, Princeton University Professor put a claim in his book. A Random Walk Down
Wall Street, which was a bestseller, insisted that a blindfolded monkey could do an equally good job as
experts at selecting a portfolio by throwing darts at a newspaper’s financial pages. In such stock-picking
competitions, monkeys have beaten pros. But this was for once or twice. With enough events, the monkeys’
performance declines; it regresses to the mean.
Python Machine Learning Techniques – Monkeys and Stocks

iii. What is Machine Learning Regression?


In this plot, the line best fits all the data marked by the points. Using this line, we can predict what values we
will find for x=70 (with a degree of uncertainty).

Machine Learning Techniques with Python – What is Machine Learning Regression

As a Machine Learning technique, regression finds its foundation in supervised learning. We use it to predict a
continuous and numerical target and begins by working on the data set values we already know. It compares
known and predicted values and labels the difference between the expected and predicted values as the
error/residual.
iv. Types of Regression in Machine Learning
We generally observe two kinds of regression-

Linear Regression- When we can denote the relationship between a target and a predictor in a straight line,
we use linear regression-

y=P1x+P2+e

Non-Linear Regression- When we observe a non-linear relationship between a target and a predictor, we
cannot denote it as a straight line.

b. Machine Learning Classification

i. What is Machine Learning Classification?


Classification is a data mining technique that lets us predict group membership for data instances. This uses
labelled data in advance and falls under supervised learning. This means we train data and expect to predict its
future. By ‘prediction’, we mean we classify data into the classes they can belong. We have two kinds of
attributes available-

Output Attribute- Aka Dependent attribute.

Input Attribute- Aka Independent attribute.

ii. Methods of Classification


Decision Tree Induction- We build a decision tree from the class labelled tuples. This has internal nodes,
branches, and leaf nodes. The internal nodes denote the test on an attribute, the branches- the test
outcome, and the leaf nodes- the class label. The two steps involved are learning and testing, and these
are fast.

Rule-based Classification- This classification is based on a set of IF-THEN rules. A rule is denoted as-

IF condition THEN conclusion

Classification by Backpropagation- Neural network learning, often called connectionist learning, builds
connections. Backpropagation is a neural-network learning algorithm, one of the most popular ones. It
iteratively processes data and compares the target value with the results to learn.

Lazy Learners- In a lazy learner approach, the machine stores the training tuple and waits for a test tuple.
This supports incremental learning. This contrasts with the early learner approach.

iii. ML Classification Example


Let’s take an example. Consider we’re here to teach you about different kinds of codes. We present to you ITF
Barcodes, Code 93 Barcodes, QR codes, Aztecs, and data matrices among others. Once through most of the
examples, it is now your turn to identify the kind of code it is when we show you one. This is supervised
learning and we use parts of the examples of both- training and testing.
Notice how some stars of each type end up on the other side of the curve.
Machine Learning Techniques with Python – ML Classification Example

c. Clustering
Clustering is an unsupervised classification. This is an exploratory data analysis with no labelled data available.
With clustering, we separate unlabeled data into finite and discrete sets of data structures that are natural and
hidden. We observe two kinds of clustering-

Hard Clustering- One object belongs to a single cluster.

Soft Clustering- One object may belong to multiple clusters.

In clustering, we first select features, then design the clustering algorithm and then validate the clusters. Finally,
we interpret the results.
a. Example
Recall the example in section b.iii. You could group these codes together. QR code, Aztec, and Data Matrix
would be in a group, we could call this 2D Codes. ITF Barcodes and Code 39 Barcodes would group into a ‘1D
Codes’ category. This is what a cluster looks like-
Machine Learning Techniques with Python – Clustering

d. Anomaly Detection
An anomaly is something that deviates from its expected course. With machine learning, sometimes, we may
want to spot an outlier. One such example would be to detect a dentist bill 85 fillings per hour. This amounts to
42 seconds per patient. Another would be to find a particular dentist bill only on Thursdays. Such situations
raise suspicion and anomaly detection is a great way to highlight these anomalies since this isn’t something
we’re looking for specifically.
So, this was all about Machine Learning Techniques with Python. Hope you like our explanation.

4. Conclusion

Hence, in this tutorial, we learned about four techniques of machine learning with Python- Regression,
Classification, Clustering, and Anomaly Detection. Furthermore, if you have any query, feel free to ask in the
comment box.

17 Top Applications of Machine Learning with Python


by DataFlair Team · September 28, 2018

1. Objective
In our last tutorial, we discuss Machine learning Techniques with Python. Today, we dedicate this Python
Machine Learning tutorial to learn about the applications of Machine Learning with Python Programming.
Let’s take a look at the areas where Machine is used in the industry.
So, start the Applications of Machine Learning with Python.
17 Top Applications of Machine Learning with Python

2. Why Python for Machine Learning Used?

Before we proceed to the applications of Machine Learning with Python, you’re probably asking yourself- why
Python? Among tools like R programming and SAS, here’s why we’ll go with Python-
Follow this link to know about Machine Learning

a. Simple
The sole reason why Python is often chosen as an introductory language to programming is its simplicity. It is
simple, yet powerful. Python is easy to write, and simple to understand. This behaviour of its makes it intuitive.
Situations like getting your code from another developer that uses third-party components mean you need very
little cognitive overhead. It is also true that code is read more often than it is written. Therefore, simplicity
serves to be a great asset to Python.

b. Huge Set of Relevant Libraries


Python has a wide collection of libraries for machine learning purposes. These include Python NumPy, SciPy,
scikit-learn, and many more. These are good with all intrinsic tasks of machine learning.

scikit-learn- Good for data mining, data analysis, and machine learning.

pylearn2- More flexible than scikit-learn.

PyBrain- Modular ML library with flexible, easy, and powerful ML algorithms and predefined
environments to test and compare algorithms.
Orange- Open-source data visualization and analysis, has components for machine learning, has extensions
for biometrics and text mining, has features for data analytics, supports data mining through visual
programming or Python scripting.

PyML- The Interactive object-oriented framework for machine learning, written in Python.

Milk- Machine learning toolkit, has SVMs, k-NN, random forests, decision trees, performs feature
selection.

Shogun- Machine learning toolbox, focuses on large-scale kernel methods and SVMs.

Tensorflow- High-level Neural Network Library.

Follow this link to know about Tensorflow Tutorial

3. Applications of Machine Learning with Python

17 Top Applications of Machine Learning with Python

a. Virtual Personal Assistants


Names like Siri and Alexa bring to mind the capabilities of virtual assistants. We can ask Siri to make a call for
you or play music. You can request Alexa for today’s weather forecast. You can even set an alarm or send an
SMS. What makes this easier on you is that you only need to speak to it and it will listen to your command.
This comes in handy for those differently abled. Such assistants take note of how you interact with them and use
that to make your next experience with them better.
Applications of Machine Learning with Python

b. Social Media Services


By now, you would have noticed several features of Facebook- ‘People You May Know’ and ‘Face
Recognition’. It uses machine learning to monitor your activity- what profiles you visit, which people to send
requests to, which ones you accept requests of, the people you tag, among much more. With this, Facebook
hopes to provide you with a richer experience on its platform so you will use it regularly.

Applications of Machine Learning with Python

Do you know the Reasons Why Should we Learn Python

c. Online Customer Support


Websites like educators and shopping platforms will often pop a live chat up to help you with your questions. A
visitor with a head full of questions is more likely to leave than stay and possibly make a purchase. Some
websites use a chatbot instead to pull information to the website and try to address the customer’s queries.
Applications of Machine Learning with Python

d. Online Fraud Detection


If you’re familiar with PayPal, you realize your trust with it. It uses machine learning to stand in defence against
illegal acts like money laundering. By comparing millions of transactions, it can find out which ones are
illegitimate.

Applications of Machine Learning with Python

e. Product Recommendations
Shopping platforms like Amazon and Jabong notice what products you look at and suggest similar products to
you. If this gets a favourite product across to you and results in a purchase you make with them, it’s a win for
them. For this, it also uses your wishlist and cart contents.
Applications of Machine Learning with Python

Read about Python Functions with Syntax and Examples

f. Refining Search Engine Results


The searches you make in search engines like Google monitor your response. Do you visit a top listing and stick
around for a while? Do you get to the third page and leave without clicking any link? Google makes note of the
findings and aims to improve your search next time.

Applications of Machine Learning with Python

g. Fighting Web Spam


Many email clients use rule-based spam filtering. Spammers develop new tricks to get around this. So, clients
like Gmail use machine learning to keep their spam filters updated. This is also a problem with Google search
results and other search engines. Common spam-filtering techniques are Multi-Layer Perceptron and C 4.5
Decision Tree Induction.

Applications of Machine Learning with Python


h. Automatic Translation
Machine Learning lets us translate text into another language. The ML algorithm for this figures how words fit
together and then uses this information to improve the quality of a translation. With this, we can also translate
the text on images using neural networks to identify letters.

Applications of Machine Learning with Python

Let’s Explore Python PyQt5 Tutorial

i. Video Surveillance
Some crimes can be avoided by sensing them way before they can happen. Behaviour, like standing motionless,
napping on a bench, and following another individual, can alert human attendants via a video surveillance
system.

Applications of Machine Learning with Python

j. Predicting Music Choices


Products like Genius by Apple Music monitors what you listen to. Later, it can suggest you a list of songs you
are likely to prefer. It also picks songs from your playlist to create libraries that sound good together.

Applications of Machine Learning with Python


k. Drug Discovery and Disease Diagnosis
With ML algorithms, we can perform the following tasks-

Applications of Machine Learning with Python

Initial screening of drug compounds.

Predicting success rate based on biological factors.

R&D technologies like Next-Generation Sequencing.

Understand disease processes.

Design effective treatments for diseases.

Personalizing drug combinations.

Produce cheaper drugs with improved replication.

Research and develop diagnostics and therapeutic treatments.

Read about Python Statistics – p-Value, Correlation, T-test, KS Test

l. Face Recognition
Facilities like face detection are often something we see with Facebook. When we want to tag a photo,
Facebook automatically suggests us a few names. Most of the times, the first name is accurate for the face it has
detected. This has machine learning to credit.
Applications of Machine Learning with Python

m. Pricing Insurance Plans


Machine Learning can detect if a driver is likely to cause a large-loss case during the term of insurance. This
lets insurance firms price insurance plans accordingly.

n. Autonomous, Self-Driving Cars


These cars receive data on nearby objects and their sizes and speeds via sensors. Based on how they behave, it
categorizes objects as cyclists, pedestrians, and other cars among others. It uses this data to compare stored
maps to current conditions. Such cars make use of Machine Vision algorithms.
Applications of Machine Learning with Python

Do you know the Python Data Science Environment Setup

4. More Machine Learning Applications


Other than what we just mentioned, we can use Machine Learning for the following purposes-

Identifying human genes that predispose people to cancer.

Identifying what consumers respond to.

Trading stocks and derivatives.

Packet inspection for anti-virus software.

Delayed aeroplane flights.

Factory maintenance diagnostics.

Behavioural advertisement for products.

So, this was all about Applications of Machine Learning with Python. Hope you like our explanation.

5. Conclusion

Hence, we conclude our tutorial on applications of machine learning with Python. Got more to add? Feel free to
drop it in the comments below.

8 Machine Learning Algorithms in Python – You Must Learn


by DataFlair Team · September 28, 2018

1. Objective
Previously, we discussed the techniques of machine learning with Python. Going deeper, today, we will learn
and implement 8 top Machine Learning Algorithms in Python.

Let’s begin the journey of Machine Learning Algorithms in Python Programming.

Machine Learning Algorithms in Python – You Must LEARN

2. Machine Learning Algorithms in Python

Followings are the Algorithms of Python Machine Learning:

a. Linear Regression
Linear regression is one of the supervised Machine learning algorithms in Python that observes continuous
features and predicts an outcome. Depending on whether it runs on a single variable or on many features, we
can call it simple linear regression or multiple linear regression.

This is one of the most popular Python ML algorithms and often under-appreciated. It assigns optimal weights
to variables to create a line ax+b to predict the output. We often use linear regression to estimate real values like
a number of calls and costs of houses based on continuous variables. The regression line is the best line that fits
Y=a*X+b to denote a relationship between independent and dependent variables.
Do you know about Python Machine Learning Environment Setup
Let’s plot this for the diabetes dataset.

>>> import matplotlib.pyplot as plt

>>> import numpy as np


>>> from sklearn import datasets,linear_model

>>> from sklearn.metrics import mean_squared_error,r2_score

>>> diabetes=datasets.load_diabetes()

>>> diabetes_X=diabetes.data[:,np.newaxis,2]

>>> diabetes_X_train=diabetes_X[:-30] #splitting data into training and test sets

>>> diabetes_X_test=diabetes_X[-30:]

>>> diabetes_y_train=diabetes.target[:-30] #splitting targets into training and test sets

>>> diabetes_y_test=diabetes.target[-30:]

>>> regr=linear_model.LinearRegression() #Linear regression object

>>> regr.fit(diabetes_X_train,diabetes_y_train) #Use training sets to train the model

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

>>> diabetes_y_pred=regr.predict(diabetes_X_test) #Make predictions

>>> regr.coef_

array([941.43097333])

>>> mean_squared_error(diabetes_y_test,diabetes_y_pred)

3035.0601152912695

>>> r2_score(diabetes_y_test,diabetes_y_pred) #Variance score

0.410920728135835

>>> plt.scatter(diabetes_X_test,diabetes_y_test,color ='lavender')

<matplotlib.collections.PathCollection object at 0x0584FF70>

>>> plt.plot(diabetes_X_test,diabetes_y_pred,color='pink',linewidth=3)

[<matplotlib.lines.Line2D object at 0x0584FF30>]

>>> plt.xticks(())

([], <a list of 0 Text xticklabel objects>)

>>> plt.yticks(())

([], <a list of 0 Text yticklabel objects>)


>>> plt.show()

Machine Learning Algorithms in Python – Linear Regression

Python Machine Learning – Data Preprocessing, Analysis & Visualization

b. Logistic Regression
Logistic regression is a supervised classification is unique Machine Learning algorithms in Python that finds its
use in estimating discrete values like 0/1, yes/no, and true/false. This is based on a given set of independent
variables. We use a logistic function to predict the probability of an event and this gives us an output between 0
and 1.
Although it says ‘regression’, this is actually a classification algorithm. Logistic regression fits data into a logit
function and is also called logit regression. Let’s plot this.

>>> import numpy as np

>>> import matplotlib.pyplot as plt

>>> from sklearn import linear_model

>>> xmin,xmax=-7,7 #Test set; straight line with Gaussian noise

>>> n_samples=77

>>> np.random.seed(0)

>>> x=np.random.normal(size=n_samples)

>>> y=(x>0).astype(np.float)

>>> x[x>0]*=3

>>> x+=.4*np.random.normal(size=n_samples)
>>> x=x[:,np.newaxis]

>>> clf=linear_model.LogisticRegression(C=1e4) #Classifier

>>> clf.fit(x,y)

>>> plt.figure(1,figsize=(3,4))

<Figure size 300x400 with 0 Axes>

>>> plt.clf()

>>> plt.scatter(x.ravel(),y,color='lavender',zorder=17)

<matplotlib.collections.PathCollection object at 0x057B0E10>

>>> x_test=np.linspace(-7,7,277)

>>> def model(x):

return 1/(1+np.exp(-x))

>>> loss=model(x_test*clf.coef_+clf.intercept_).ravel()

>>> plt.plot(x_test,loss,color='pink',linewidth=2.5)

[<matplotlib.lines.Line2D object at 0x057BA090>]

>>> ols=linear_model.LinearRegression()

>>> ols.fit(x,y)

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

>>> plt.plot(x_test,ols.coef_*x_test+ols.intercept_,linewidth=1)

[<matplotlib.lines.Line2D object at 0x057BA0B0>]

>>> plt.axhline(.4,color='.4')

<matplotlib.lines.Line2D object at 0x05860E70>

>>> plt.ylabel('y')

Text(0,0.5,’y’)

>>> plt.xlabel('x')

Text(0.5,0,’x’)

>>> plt.xticks(range(-7,7))
>>> plt.yticks([0,0.4,1])

>>> plt.ylim(-.25,1.25)

(-0.25, 1.25)

>>> plt.xlim(-4,10)

(-4, 10)

>>> plt.legend(('Logistic Regression','Linear Regression'),loc='lower right',fontsize='small')

<matplotlib.legend.Legend object at 0x057C89F0>


Do you know How to Split Train and Test Set in Python Machine Learning

>>> plt.show()

Machine Learning Algorithms in Python -Logistic

c. Decision Tree
A decision tree falls under supervised Machine Learning Algorithms in Python and comes of use for both
classification and regression- although mostly for classification. This model takes an instance, traverses the tree,
and compares important features with a determined conditional statement. Whether it descends to the left child
branch or the right depends on the result. Usually, more important features are closer to the root.

Decision Tree, a Machine Learning algorithms in Python can work on both categorical and continuous
dependent variables. Here, we split a population into two or more homogeneous sets. Let’s see the algorithm for
this-
>>> from sklearn.cross_validation import train_test_split

>>> from sklearn.tree import DecisionTreeClassifier

>>> from sklearn.metrics import accuracy_score

>>> from sklearn.metrics import classification_report

>>> def importdata(): #Importing data

balance_data=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-'+

'databases/balance-scale/balance-scale.data',

sep= ',', header = None)

print(len(balance_data))

print(balance_data.shape)

print(balance_data.head())

return balance_data

>>> def splitdataset(balance_data): #Splitting data

x=balance_data.values[:,1:5]

y=balance_data.values[:,0]

x_train,x_test,y_train,y_test=train_test_split( x,y,t

est_size=0.3,random_state=100)

return x,y,x_train,x_test,y_train,y_test

>>> def train_using_gini(x_train,x_test,y_train): #Training with giniIndex

clf_gini = DecisionTreeClassifier(criterion = "gini",

random_state = 100,max_depth=3, min_samples_leaf=5)

clf_gini.fit(x_train,y_train)

return clf_gini

>>> def train_using_entropy(x_train,x_test,y_train): #Training with entropy

clf_entropy=DecisionTreeClassifier(

criterion = "entropy", random_state = 100,


max_depth = 3, min_samples_leaf = 5)

clf_entropy.fit(x_train,y_train)

return clf_entropy

>>> def prediction(x_test,clf_object): #Making predictions

y_pred=clf_object.predict(x_test)

print(f"Predicted values: {y_pred}")

return y_pred

>>> def cal_accuracy(y_test,y_pred): #Calculating accuracy

print(confusion_matrix(y_test,y_pred))

print(accuracy_score(y_test,y_pred)*100)

print(classification_report(y_test,y_pred))

>>> data=importdata()

625
(625, 5)
01234
0B1111
1R1112
2R1113
3R1114
4R1115

>>> x,y,x_train,x_test,y_train,y_test=splitdataset(data)

>>> clf_gini=train_using_gini(x_train,x_test,y_train)

>>> clf_entropy=train_using_entropy(x_train,x_test,y_train)

>>> y_pred_gini=prediction(x_test,clf_gini)
Machine Learning Algorithms in Python – Decision Tree

>>> cal_accuracy(y_test,y_pred_gini)

[[ 0 6 7] [ 0 67 18] [ 0 19 71]] 73.40425531914893

Machine Learning Algorithms in Python – Decision Tree

>>> y_pred_entropy=prediction(x_test,clf_entropy)

Machine Learning Algorithms in Python – Decision Tree

>>> cal_accuracy(y_test,y_pred_entropy)

[[ 0 6 7] [ 0 63 22] [ 0 20 70]] 70.74468085106383

Machine Learning Algorithms in Python – Decision Tree Algorithm i

Let’s explore 4 Machine Learning Techniques with Python

d. Support Vector Machines (SVM)


SVM is a supervised classification is one of the most important Machines Learning algorithms in Python, that
plots a line that divides different categories of your data. In this ML algorithm, we calculate the vector to
optimize the line. This is to ensure that the closest point in each group lies farthest from each other. While you
will almost always find this to be a linear vector, it can be other than that.
In this Python Machine Learning tutorial, we plot each data item as a point in an n-dimensional space. We have
n features and each feature has the value of a certain coordinate.
First, let’s plot a dataset.

>>> from sklearn.datasets.samples_generator import make_blobs

>>> x,y=make_blobs(n_samples=500,centers=2,

random_state=0,cluster_std=0.40)

>>> import matplotlib.pyplot as plt

>>> plt.scatter(x[:,0],x[:,1],c=y,s=50,cmap='plasma')

<matplotlib.collections.PathCollection object at 0x04E1BBF0>

>>> plt.show()

Machine Learning Algorithms in Python – SVM

>>> import numpy as np

>>> xfit=np.linspace(-1,3.5)

>>> plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='plasma')

<matplotlib.collections.PathCollection object at 0x07318C90>

>>> for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:

yfit = m * xfit + b

plt.plot(xfit, yfit, '-k')

plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none',


color='#AFFEDC', alpha=0.4)

[<matplotlib.lines.Line2D object at 0x07318FF0>] <matplotlib.collections.PolyCollection object at


0x073242D0>
[<matplotlib.lines.Line2D object at 0x07318B70>] <matplotlib.collections.PolyCollection object at
0x073246F0>
[<matplotlib.lines.Line2D object at 0x07324370>] <matplotlib.collections.PolyCollection object at
0x07324B30>

>>> plt.xlim(-1,3.5)

(-1, 3.5)

>>> plt.show()

Machine Learning Algorithms in Python – Support Vector Machine

Follow this link to know about Python PyQt5 Tutorial

e. Naive Bayes
Naive Bayes is a classification method which is based on Bayes’ theorem. This assumes independence between
predictors. A Naive Bayes classifier will assume that a feature in a class is unrelated to any other. Consider a
fruit. This is an apple if it is round, red, and 2.5 inches in diameter. A Naive Bayes classifier will say these
characteristics independently contribute to the probability of the fruit being an apple. This is even if features
depend on each other.
For very large data sets, it is easy to build a Naive Bayesian model. Not only is this model very simple, it
performs better than many highly sophisticated classification methods. Let’s build this.

>>> from sklearn.naive_bayes import GaussianNB

>>> from sklearn.naive_bayes import MultinomialNB

>>> from sklearn import datasets

>>> from sklearn.metrics import confusion_matrix


>>> from sklearn.model_selection import train_test_split

>>> iris=datasets.load_iris()

>>> x=iris.data

>>> y=iris.target

>>> x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)

>>> gnb=GaussianNB()

>>> mnb=MultinomialNB()

>>> y_pred_gnb=gnb.fit(x_train,y_train).predict(x_test)

>>> cnf_matrix_gnb = confusion_matrix(y_test, y_pred_gnb)

>>> cnf_matrix_gnb

array([[16, 0, 0],
[ 0, 18, 0],
[ 0, 0, 11]], dtype=int64)

>>> y_pred_mnb = mnb.fit(x_train, y_train).predict(x_test)

>>> cnf_matrix_mnb = confusion_matrix(y_test, y_pred_mnb)

>>> cnf_matrix_mnb

array([[16, 0, 0],
[ 0, 0, 18],
[ 0, 0, 11]], dtype=int64)

f. kNN (k-Nearest Neighbors)


This is a Python Machine Learning algorithms for classification and regression- mostly for classification. This
is a supervised learning algorithm that considers different centroids and uses a usually Euclidean function to
compare distance. Then, it analyzes the results and classifies each point to the group to optimize it to place with
all closest points to it. It classifies new cases using a majority vote of k of its neighbors. The case it assigns to a
class is the one most common among its K nearest neighbors. For this, it uses a distance function.

i. Training and testing on the entire dataset


>>> from sklearn.datasets import load_iris

>>> iris=load_iris()

>>> x=iris.data

>>> y=iris.target
>>> from sklearn.linear_model import LogisticRegression

>>> logreg=LogisticRegression()

>>> logreg.fit(x,y)

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,


intercept_scaling=1, max_iter=100, multi_class=’ovr’, n_jobs=1,
penalty=’l2′, random_state=None, solver=’liblinear’, tol=0.0001,
verbose=0, warm_start=False)

>>> logreg.predict(x)

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

>>> y_pred=logreg.predict(x)

>>> len(y_pred)

150

>>> from sklearn import metrics

>>> metrics.accuracy_score(y,y_pred)

0.96

>>> from sklearn.neighbors import KNeighborsClassifier

>>> knn=KNeighborsClassifier(n_neighbors=5)

>>> knn.fit(x,y)

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights=’uniform’)

>>> y_pred=knn.predict(x)

>>> metrics.accuracy_score(y,y_pred)

0.9666666666666667

>>> knn=KNeighborsClassifier(n_neighbors=1)
>>> knn.fit(x,y)

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=1, p=2,
weights=’uniform’)

>>> y_pred=knn.predict(x)

>>> metrics.accuracy_score(y,y_pred)

1.0

ii. Splitting into train/test


>>> x.shape

(150, 4)

>>> y.shape

(150,)

>>> from sklearn.cross_validation import train_test_split

>>> x.shape

(150, 4)

>>> y.shape

(150,)

>>> from sklearn.cross_validation import train_test_split

>>> x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4,random_state=4)

>>> x_train.shape

(90, 4)

>>> x_test.shape

(60, 4)

>>> y_train.shape

(90,)

>>> y_test.shape

(60,)
>>> logreg=LogisticRegression()

>>> logreg.fit(x_train,y_train)

>>> y_pred=knn.predict(x_test)

>>> metrics.accuracy_score(y_test,y_pred)

0.9666666666666667

>>> knn=KNeighborsClassifier(n_neighbors=5)

>>> knn.fit(x_train,y_train)

KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,


metric_params=None, n_jobs=1, n_neighbors=5, p=2,
weights=’uniform’)

>>> y_pred=knn.predict(x_test)

>>> metrics.accuracy_score(y_test,y_pred)

0.9666666666666667

>>> k_range=range(1,26)

>>> scores=[]

>>> for k in k_range:

knn = KNeighborsClassifier(n_neighbors=k)

knn.fit(x_train, y_train)

y_pred = knn.predict(x_test)

scores.append(metrics.accuracy_score(y_test, y_pred))

>>> scores

[0.95, 0.95, 0.9666666666666667, 0.9666666666666667, 0.9666666666666667, 0.9833333333333333,


0.9833333333333333, 0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9833333333333333, 0.9833333333333333, 0.9833333333333333, 0.9833333333333333,
0.9833333333333333, 0.9833333333333333, 0.9833333333333333, 0.9666666666666667,
0.9833333333333333, 0.9666666666666667, 0.9666666666666667, 0.9666666666666667,
0.9666666666666667, 0.95, 0.95]

>>> import matplotlib.pyplot as plt

>>> plt.plot(k_range,scores)
[<matplotlib.lines.Line2D object at 0x05FDECD0>]

>>> plt.xlabel('k for kNN')

Text(0.5,0,’k for kNN’)

>>> plt.ylabel('Testing Accuracy')

Text(0,0.5,’Testing Accuracy’)

>>> plt.show()

Machine Learning Algorithms in Python – k-Nearest Neighbors

Read about Python Statistics – p-Value, Correlation, T-test, KS Test

g. k-Means
k-Means is an unsupervised algorithm that solves the problem of clustering. It classifies data using a number of
clusters. The data points inside a class are homogeneous and heterogeneous to peer groups.

>>> import numpy as np

>>> import matplotlib.pyplot as plt

>>> from matplotlib import style


>>> style.use('ggplot')

>>> from sklearn.cluster import KMeans

>>> x=[1,5,1.5,8,1,9]

>>> y=[2,8,1.7,6,0.2,12]

>>> plt.scatter(x,y)

<matplotlib.collections.PathCollection object at 0x0642AF30>

>>> x=np.array([[1,2],[5,8],[1.5,1.8],[8,8],[1,0.6],[9,11]])

>>> kmeans=KMeans(n_clusters=2)

>>> kmeans.fit(x)

KMeans(algorithm=’auto’, copy_x=True, init=’k-means++’, max_iter=300,


n_clusters=2, n_init=10, n_jobs=1, precompute_distances=’auto’,
random_state=None, tol=0.0001, verbose=0)

>>> centroids=kmeans.cluster_centers_

>>> labels=kmeans.labels_

>>> centroids

array([[1.16666667, 1.46666667],
[7.33333333, 9. ]])

>>> labels

array([0, 1, 0, 1, 0, 1])

>>> colors=['g.','r.','c.','y.']

>>> for i in range(len(x)):

print(x[i],labels[i])

plt.plot(x[i][0],x[i][1],colors[labels[i]],markersize=10)

[1. 2.] 0
[<matplotlib.lines.Line2D object at 0x0642AE10>] [5. 8.] 1
[<matplotlib.lines.Line2D object at 0x06438930>] [1.5 1.8] 0
[<matplotlib.lines.Line2D object at 0x06438BF0>] [8. 8.] 1
[<matplotlib.lines.Line2D object at 0x06438EB0>] [1. 0.6] 0
[<matplotlib.lines.Line2D object at 0x06438FB0>] [ 9. 11.] 1
[<matplotlib.lines.Line2D object at 0x043B1410>]
>>> plt.scatter(centroids[:,0],centroids[:,1],marker='x',s=150,linewidths=5,zorder=10)

<matplotlib.collections.PathCollection object at 0x043B14D0>

>>> plt.show()

Machine Learning Algorithms in Python – K-Means

Have a Look at Python Descriptive Statistics – Measuring Central Tendency

h. Random Forest
A random forest is an ensemble of decision trees. In order to classify every new object based on its attributes,
trees vote for class- each tree provides a classification. The classification with the most votes wins in the forest.

>>> import numpy as np

>>> import pylab as pl

>>> x=np.random.uniform(1,100,1000)

>>> y=np.log(x)+np.random.normal(0,.3,1000)

>>> pl.scatter(x,y,s=1,label='log(x) with noise')

<matplotlib.collections.PathCollection object at 0x0434EC50>


>>> pl.plot(np.arange(1,100),np.log(np.arange(1,100)),c='b',label='log(x) true function')

[<matplotlib.lines.Line2D object at 0x0434EB30>]

>>> pl.xlabel('x')

Text(0.5,0,’x’)

>>> pl.ylabel('f(x)=log(x)')

Text(0,0.5,’f(x)=log(x)’)

>>> pl.legend(loc='best')

<matplotlib.legend.Legend object at 0x04386450>

>>> pl.title('A basic log function')

Text(0.5,1,’A basic log function’)

>>> pl.show()

Machine Learning Algorithms in Python – Random Forest

>>> from sklearn.datasets import load_iris


>>> from sklearn.ensemble import RandomForestClassifier

>>> import pandas as pd

>>> import numpy as np

>>> iris=load_iris()

>>> df=pd.DataFrame(iris.data,columns=iris.feature_names)

>>> df['is_train']=np.random.uniform(0,1,len(df))<=.75

>>> df['species']=pd.Categorical.from_codes(iris.target,iris.target_names)

>>> df.head()

sepal length (cm) sepal width (cm) … is_train species


0 5.1 3.5 … True setosa
1 4.9 3.0 … True setosa
2 4.7 3.2 … True setosa
3 4.6 3.1 … True setosa
4 5.0 3.6 … False setosa
[5 rows x 6 columns]

>>> train,test=df[df['is_train']==True],df[df['is_train']==False]

>>> features=df.columns[:4]

>>> clf=RandomForestClassifier(n_jobs=2)

>>> y,_=pd.factorize(train['species'])

>>> clf.fit(train[features],y)

RandomForestClassifier(bootstrap=True, class_weight=None, criterion=’gini’,


max_depth=None, max_features=’auto’, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=2,
oob_score=False, random_state=None, verbose=0,
warm_start=False)

>>> preds=iris.target_names[clf.predict(test[features])]

>>> pd.crosstab(test['species'],preds,rownames=['actual'],colnames=['preds'])

preds setosa versicolor virginica


actual
setosa 12 0 0
versicolor 0 17 2
virginica 0 1 15
So, this was all about Machine Learning Algorithms in Python Tutorial. Hope you like our explanation.

3. Conclusion

Hence, today we discussed eight important Python Machine Learning Algorithms. Which one do you think
bears the most potential? Drop your suggestions in the comments below. We will surely get back to you!

Deep Learning With Python Tutorial For Beginners – DNN & ANN
by DataFlair Team · September 28, 2018

1. Deep Learning With Python


Today, we will see Deep Learning with Python Tutorial. Deep Learning, a Machine Learning method that has
taken the world by awe with its capabilities. In this Python Deep Learning Tutorial, we will discuss the meaning
of Deep Learning With Python. Also, we will learn why we call it Deep Learning. Moreover, this Python Deep
learning Tutorial will go through artificial neural networks and Deep Neural Networks, along with deep
learning applications.
So, let’s start Deep Learning with Python.

Deep Learning With Python Tutorial For Beginners – 2018

2. What is Deep Learning with Python?

a. Deep Learning Definition


To define it in one sentence, we would say it is an approach to Machine Learning. To elaborate, Deep Learning
is a method of Machine Learning that is based on learning data representations (or feature learning) instead of
task-specific algorithms. We also call it deep structured learning or hierarchical learning, but mostly, Deep
Learning. For feature learning, we observe three kinds of learning- supervised, semi-supervised, or
unsupervised.
The patterns we observe in biological nervous systems inspires vaguely the deep learning models that exist.

b. Characteristics of Deep Learning With Python


Some characteristics of Python Deep Learning are-

They use a cascade of layers of nonlinear processing units to extract features and perform transformation;
the output at one layer is the input to the next.

These learn in supervised and/or unsupervised ways (examples include classification and pattern analysis
respectively).

These learn multiple levels of representations for different levels of abstraction.

Do you read Machine Learning With Python

3. Deep Learning With Python – Why Deep Learning?


Deep Learning uses networks where data transforms through a number of layers before producing the output.
This is something we measure by a parameter often dubbed CAP. The Credit Assignment Path depth tells us a
value one more than the number of hidden layers- for a feedforward neural network. But we can safely say that
with Deep Learning, CAP>2. Each layer takes input and transforms it to make it only slightly more abstract and
composite.

4. Deep Learning With Python – (ANN)


Now, let’s talk about neural networks. An Artificial Neural Network is a connectionist system. It is a
computing system that, inspired by the biological neural networks from animal brains, learns from examples.
When an ANN sees enough images of cats (and those of objects that aren’t cats), it learns to identify another
image of a cat.

a. Structure
An Artificial Neural Network is nothing but a collection of artificial neurons that resemble biological ones.
Synapses (connections between these neurons) transmit signals to each other. A postsynaptic neuron processes
the signal it receives and signals the neurons connected to it further.
Have a look at Machine Learning vs Deep Learning
Deep Learning With Python – Structure of Artificial Neural Networks

A neuron can have state (a value between 0 and 1) and a weight that can increase or decrease the signal strength
as the network learns. We see three kinds of layers- input, hidden, and output. There may be any number of
hidden layers. Typically, such networks can hold around millions of units and connections. Note that this is still
nothing compared to the number of neurons and connections in a human brain.

5. Deep Learning With Python – Deep Neural Networks


A Deep Neural Network is but an Artificial Neural Network with multiple layers between the input and the
output. At each layer, the network calculates how probable each output is. A DNN will model complex non-
linear relationships when it needs to. With extra layers, we can carry out the composition of features from lower
layers.
Deep Neural Networks

Typically, a DNN is a feedforward network that observes the flow of data from input to output. It never loops
back. Deep Neural Network creates a map of virtual neurons and assigns weights to the connections that hold
them together. It multiplies the weights to the inputs to produce a value between 0 and 1. When it doesn’t
accurately recognize a value, it adjusts the weights. This is to make parameters more influential with an ulterior
motive to determine the correct mathematical manipulation so we can fully process the data.
Two kinds of ANNs we generally observe are-

Recurrent Neural Networks- Where data can flow in any direction. We use concepts like LSTM (Long
Short-Term Memory) from these in areas like language modeling.

Convolutional Deep Neural Networks- A deep, feedforward ANN. We use these in areas like analyzing
visual imagery, computer vision, and acoustic modeling for ASR (Automatic Speech Recognition)

6. Deep Learning With Python – Deep Learning Applications


We observe the use of Deep Learning with Python in the following fields-

Automatic speech recognition.

Image recognition.

Visual art processing.

Natural Language Processing (NLP).

Drug discovery and toxicology.

Customer Relationship Management (CRM).


Recommendation systems.

Bioinformatics.

Mobile advertising.

Image Restoration.

Financial fraud detection.

For more applications, refer to 20 Interesting Applications of Deep Learning with Python.
Before we bid you goodbye, we’d like to introduce you to Samantha, an AI from the movie Her.

Deep Learning With Python tutorial

In the film, Theodore, a sensitive and shy man writes personal letters for others to make a living. Samantha is an
OS on his phone that Theodore develops a fantasy for. What starts with a friendship takes the form of love.

Python Deep Learning Tutorial

Will deep learning get us from Siri to Samantha in real life? Well, at least Siri disapproves.

Deep Learning Tutorial Python

So, this was all in Deep Learning with Python tutorial. Hope you like our explanation.

7. Conclusion
Hence, in this Deep Learning Tutorial Python, we discussed what exactly deep learning with Python means.
Also, we saw artificial neural networks and deep neural networks in Deep Learning With Python Tutorial.
Moreover, we discussed deep learning application and got the reason why Deep Learning. See you again with
another tutorial on Deep Learning. Furthermore, if you have any query regarding Deep Learning With Python,
ask in the comment tab.

Python Deep Learning Environment Setup – Python Text Editor &


Hardware
by DataFlair Team · September 28, 2018

1. Python Deep Learning Environment Setup


Moving on from introducing Deep Learning, now, we guide you to set up your machine for your journey with
Deep Learning using Python. So, today in this Deep Learning with Python Tutorial, we will see Python Deep
Learning Environment Setup. Moreover, we will see how to install Python. Also, we will learn about Python
Libraries such as NumPy, Keras, TensorFlow, Matplotlib, Theano. After this, we will look at Python Hardware
i.e. GPU and CPU. Next, in Python Deep Learning Environment Setup Tutorial, we also discuss the Python
Text Editor and Python Pip.
You will have every necessary library installed, an environment created, and you will be able to implement
deep learning.
So, let’s begin Python Deep Learning Environment Setup.

Python Deep Learning Environment Setup

2. How to Install Python?


Before moving to Python Deep Learning Environment Setup, you will need to install Python. You can
download it from Python’s official website-
https://www.python.org/downloads/
The latest version at the time of writing is 3.7.

How to Install Python?

a. Installing with Anaconda Python


Alternatively, you can get Python with Anaconda:
https://www.anaconda.com/download/

Install Anaconda Python

In both cases of installing Python and Anaconda Python, choose the configurations that support your machine.
For a detailed guide on how to install Python on Windows, read A Step-by-Step Guide to Install Python
Windows.
3. Python Libraries
Now, in Python Deep Learning Environment Setup, first, take a look at what libraries we will need for deep
learning with Python. We will show you how to install these Python Libraries.

a. Python NumPy

Python NumPy

NumPy is a Python library for working on large, multi-dimensional arrays and matrices. Python NumPy also
supports many high-level mathematical functions to operate on these.
To install this, use Python pip-

pip install numpy

b. Keras Python

Keras Python

Keras is a neural network library for Python that is open-source and written in Python. Python Keras can run
on top of TensorFlow, Theano Python, or Microsoft Cognitive Toolkit.
You can install it with Python pip-

pip install keras

c. Theano Python
Theano Python

Theano is a Python library for numerical computation. Python Theano uses a NumPy-esque syntax to express
computation; it compiles these to run efficiently on CPU or GPU architectures.
Install it using Python pip-

pip install theano

d. TensorFlow Python

TensorFlow Python

TensorFlow is a Python library by Google that aims at fast numerical computing. We can use TensorFlow
Python to create Deep Learning models either directly or by using wrapper libraries.

e. Python Matplotlib

Python Matplotlib

Matplotlib is a Python library for 2D plotting and can work together with NumPy. Install it with Python pip-

pip install matplotlib

4. Python Text Editor


Python Text Editor – Jupyter Notebook

For Python Deep Learning Environment Setup, we recommend you to use the Jupyter Notebook. It observes the
following benefits:

Thoroughly record steps and results.

Isolate code by splitting the file into cells.

Automatic terminal statements below code cells.

Ability to write Markdown, LaTeX, and Python code in one file.

Have a look at Python Deep Learning Computational Graphs


You can later save your code as a .pTensorFlowy file.

Jupyter Notebook
5. Python Hardware
To run your deep learning code in Python, you can use Python Hardware either a CPU or a GPU.

a. Central Processing Unit (CPU)


CPUs perform narrow and serially. You would need at least a recent quad-core CPU to not bottleneck the GPU.

b. Graphics Processing Unit (GPU)


Although a GPU is meant for video simulation and gaming, they have a parallel architecture. This makes them
suitable for vector and matrix operations.
So, this was all in Python Deep Learning Environment Setup. Hope you like our explanations.

6. Conclusion
Hence, in this Python Deep Learning Environment Setup Tutorial, we discussed Python Installation, Python
Libraries i.e. NumPy, Keras, TensorFlow, Matplotlib, Theano. Moreover, we discussed Python text editor and
Python Hardware such as GPU and CPU. Still, if any doubt regarding Deep Learning with Python, ask in the
comment tab.

20 Interesting Applications of Deep Learning with Python


by DataFlair Team · September 28, 2018

1. Top Python Deep Learning Applications


Today, in this Deep Learning with Python Tutorial, we will see Applications of Deep Learning with Python. In
this tutorial, we will discuss 20 major applications of Python Deep Learning. So far, we have seen what Deep
Learning is and how to implement it. Now let’s find out all that we can do with deep learning using Python- its
applications in the real world.
So, let’s start exploring Applications of Deep Learning with Python.
20 Interesting Applications of Deep Learning with Python

2. Applications of Deep Learning WIth Python


Below, we are discussing 20 best applications of deep learning with Python, that you must know. Let’s discuss
them one by one:

i. Restoring Color in B&W Photos and Videos


With Deep Learning, it is possible to restore color in black and white photos and videos. This can give a new
life to such media. The ACM Digital Library is one such project that colorizes grayscale images combining
global priors and local image features. This is based on Convolutional Neural Networks.

Applications of Deep Learning with Python – Restoring Color


The Deep Learning network learns patterns that naturally occur within photos. This includes blue skies, white
and gray clouds, and the greens of grasses. It uses past experience to learn this. Although sometimes, it can
make mistakes, it is efficient and accurate most of the times.
Do you know about important Deep Learning Terminologies?

ii. Pixel Restoration

Applications of Deep Learning with Python – Pixel Restoration

With deep learning, we can even zoom into a video beyond its resolution. In 2017, researchers from Google
Brain trained a Deep Learning network to predict faces from their low-resolution images. The Pixel Recursive
Super Resolution works on photos to enhance their resolution to a great extent.

iii. Describing Pictures


Applications of Deep Learning with Python – Describing Pictures

By now, you have noticed how Facebook can tag photos and Google can label them for easier search. Deep
Learning can describe all the elements in a picture. A deep learning network can identify many areas in an
image and can describe each area in words. This is using accurate English grammar.

iv. Changing Gaze in Photos

Applications of Deep Learning – Changing Gaze in photos

A Deep Learning network can alter the direction in which a person looks in a picture.

v. Real-Time Analysis of Behavior


Applications of Deep Learning with Python – Analysis of Behavior

Deep Learning networks can recognize and describe pictures, we know that. But they can also analyze poses of
people in these pictures. They can get real-time insights about behaviors of people, cars, and other objects.

vi. Translation

Python deep learning – Translation

It is now possible to translate text on images in real-time. The Google Translate app can do this- you hold your
camera on an object and a deep learning network OCRs the image to translate it.

vii. Generating Pictures of Galaxies and Volcanoes


Applications of Deep Learning with Python – Galaxies and Volcanoes

Using Deep Learning with Python, astronomers can create pictures of volcanoes and galaxies.
Have a look at Deep Learning vs machine Learning

viii. Creating New Images

Applications of Deep Learning with Python – Creating New Images


Pix2Pix taught a deep learning network to perform activities like creating real street scenes from colored blobs,
creating a map from a real aerial picture, fill colors between edges of objects, and even turn day scenes into
night scenes.

ix. Searching for Text in Images and Videos

Searching for text in images and videos

The Oxford Visual Geometry group can search for text in pictures and videos using deep learning. It searches
for text in BBC News videos. Check out http://zeus.robots.ox.ac.uk/textsearch/#/search/
We searched for ‘Glitter’ and it did a less than perfect job, but is accurate pretty much most of the time.

x. Outperforming Humans in Computer Games


Outperforming Humans in Computer Games

The Deep Learning community trains humans to beat humans at games like Space Invaders, Pong, and Doom.
The computers learned the rules on their own by playing for a few hours.
xi. Robotics

Applications Deep Learning With Python – Robotics

With the capabilities of Deep Learning, robots can get up when they fall, carry out tasks that need them to be
gentle, and even react to the people who push them around.

xii. Self-Driving Cars


Applications of Deep Learning with Python – Self Driving Cars

One name we’ve all heard is the Google Self-Driving Car. Such vehicles can differentiate objects, people, and
road signs. These also make use of the lidar technology.

xiii. Generating Voice


Applications of Deep Learning With Python – Generating Voice

Deep Learning networks like WaveNet by Google and Deep Speech by Baidu can automatically generate voice.
They can learn to mimic human voices so they can improve over time.
Let’s revise Python Applications

xiv. Composing Music


Like the previous application, we can train a deep learning network to produce music compositions. The
computer learns the patterns and statistics of artists and creates a unique piece.

xv. Restoring Sound in Videos


Applications of Deep Learning with Python – Restoring Sound in Videos

Deep learning makes it possible to restore sound in muted videos. The computer can add sounds like scratching
objects with a drumstick. This uses supervised learning. Apart from this, software like LipNet can read
people’s lips with 93% success.

xvi. Handwriting

Handwriting in Python Deep Learning

With deep learning, computers can not only produce digital text and art, it can handwrite. A computer can have
its own handwriting. You can try it out here-
http://www.cs.toronto.edu/~graves/handwriting.cgi

xvii. Deep Dreaming


Applications of Deep Learning with Python – Deep Dreaming

This Python Deep Learning Application can enhance features in images. Deep Dreaming makes the computer
hallucinate on the top of an image. This results in dreamy images.

xviii. Inventing and Hacking own Crypto


Google Brain has devised two neural networks- one to generate a cryptographic algorithm to protect their
messages. The other attempts to crack this. It performed well at devising, but not so much at hacking it.

xix. Deep Learning Networks Creating Deep


Learning Networks
Applications of Deep Learning with Python – Deep Learning Networks

Deep Learning products like Neural Complete can produce new deep learning networks. It is written in Python
and is trained to generate code in Python.
Let’s discuss Python Machine Learning Tutorial

xx. Writing Wikipedia articles, computer code, math papers,


and Shakespeare
Long Short-Term Memory (LSTM) is an architecture that can generate Wikipedia-like articles, fake math papers,
and much more. Not all the times does this make sense, but there will be progress.
So, this was all in Applications of Deep Learning With Python. Hope you like our explanation.

3. Conclusion
Hence, in this Python Deep Learning Tutorial, we have tried to bring to you some of the most interesting
Applications of Deep Learning with Python. Still, if you feel any query, you can ask in the comment tab.

11 Deep Learning With Python Libraries and Frameworks


by DataFlair Team · September 17, 2018

1. Deep Learning With Python Libraries & Frameworks


Today, in this Deep Learning with Python Libraries and Framework Tutorial, we will discuss 11 libraries and
frameworks that are a go-to for Deep Learning with Python. In this Deep Learning with Python Libraries, we
will see TensorFlow, Keras, Apache mxnet, Caffe, Theano Python and many more.
A library is a collection of modules that implement the related functionality. A framework defines inversion of
control- it manages the flow of control and the flow of data.
So, let’s begin Deep Learning with Python Libraries and Framework.

11 Deep Learning With Python Libraries and Frameworks

2. TensorFlow Python
TensorFlow is an open-source library for numerical computation, for which it uses data flow graphs. The
Google Brain Team researchers developed this with the Machine Intelligence research organization by Google.
TensorFlow is open-source and available to the public. It is also good for distributed computing.

TensorFlow Python in Python Deep Learning Libraries

Do you know about Python Machine Learning?


You can install it using pip with conda Python-

pip install tensorflow


3. Keras Python

Keras in Deep Learning With Python Libraries and Frame work

A minimalist, modular Neural Network library, Keras uses Theano or TensorFlow as a backend. It makes it
easy and faster to experiment and implement ideas into results.
Have a look at Machine Learning Frameworks
Keras has algorithms for optimizers, normalization, and activation layers. It also deals with Convolutional
Neural Networks. It lets you build sequence-based and graph-based networks. One limitation is that it doesn’t
support multi-GPU environments for training a network in parallel.
You can install it with Python pip-

pip install keras

4. Apache mxnet

Apache mxnet in Deep Learning with Python Libraries

mxnet delivers an amazing number of language bindings for languages like C++, Python, R, JavaScript, and
more. It does great with distributed computing and lets us train a network across CPU/GPU machines. The only
downside is that we need a little more code to run an experiment in it.
Install it using Python pip-

pip install mxnet

Let’s discuss more in Python Libraries

5. Caffe
Caffe in Python Deep Learning Libraries and Framework

Caffe is a deep learning framework that is fast and modular. This isn’t a library but provides bindings into
Python. Caffe can process nearly 60 million images per day on a K40 GPU. However, it isn’t as easy to turn
hyperparameters with it programmatically.

6. Theano Python

Deep Learning With Python Libraries and Framework – Theano

Without NumPy, we couldn’t have SciPy, scikit-learn, and scikit-image. Similarly, Theano serves as a base for
many. It is a library that will let you define, optimize, and evaluate mathematical expressions that involve
multidimensional arrays. It is tightly integrated with NumPy and transparently uses the GPU.
Theano can act as a building block for scientific computing. Install it with Python pip-

pip install theano

Let’s discuss Python machine Learning Algorithms

7. Microsoft Cognitive Toolkit


Microsoft Cognitive Toolkit

The Microsoft Cognitive Toolkit is a unified Deep Learning toolkit. It describes neural networks using a
directed graph in computational steps.
You can install cntk using Python pip-

pip install cntk

8. PyTorch

Deep Learning With Python Libraries and Framework – PyTorch

PyTorch is a Tensor and Dynamic neural network in Python. It observes strong GPU acceleration, is open-
source, and we can use it for applications like natural language processing. You can refer to this link to install
PyTorch-
https://pytorch.org/#pip-install-pytorch

9. Eclipse DeepLearning4J
Deep Learning With Python Libraries and Framework – Eclipse DeepLearning4J

DeepLearning4J is a deep learning programming library by Eclipse. It is written for Java and the JVM; It is
also a computing framework for good support with deep learning algorithms.

10. Lasagne

Deep Learning With Python Libraries and Framework – Lasagne

Lasagne is a lightweight Python library that helps us build and train neural networks in Theano. You can install
it using Python pip-

pip install lasagne

11. nolearn

Deep Learning With Python Libraries and Framework – nolearn

nolearn wraps Lasagna into an API that is more user-friendly. All code it holds is compatible with scikit-learn.
We can use it for applications like Deep Belief Networks (DBNs).
Do you know about Python Machine Learning Techniques
Install it using Python pip-

pip install nolearn


12. PyLearn2

Pylearn2

PyLearn2 is a machine learning library with most functionality built on top of Theano. It is possible to write
PyLearn2 plugins making use of mathematical expressions. Theano optimizes and stabilizes these for us and
compiles them to the backend we want.
So, this was all in Deep Learning with Python Libraries and Framework. Hope you like our explanation.

13. Conclusion: Deep Learning With Python Libraries & Frameworks


Hence, today in this Deep Learning with Python Libraries and Framework tutorial, we discussed 11 libraries
and frameworks for you to get started with deep learning. Each Deep Learning Python Library and Framework
has its own benefits and limitations. Moreover, in this, we discussed PyTorch, TensorFlow, Keras, Theano etc.
That’s all for today. we will come back with the new tutorial of Deep Learning With Python. Tell us about your
experience with us on Deep Learning with Python Libraries and Framework through comments.

Deep Neural Networks With Python – Deep Belief Networks


by DataFlair Team · September 29, 2018

1. Deep Neural Networks With Python


In this Deep Learning with Python tutorial, we will learn about Deep Neural Networks with Python and the
challenges they face. Moreover, we will see types of Deep Neural Networks and Deep Belief Networks.
So, let’s start Deep Neural Networks Tutorial.
Deep Neural Networks With Python

2. Define Deep Neural Network with Python?


Before finding out what a deep neural network in Python is, let’s learn about Artificial Neural Networks.

a. Artificial Neural Networks


An ANN (Artificial Neural Network) is inspired by the biological neural network. It can learn to perform
tasks by observing examples, we do not need to program them with task-specific rules. An ANN can look at
images labeled ‘cat’ or ‘no cat’ and learn to identify more images itself.

Such a network is a collection of artificial neurons- connected nodes; these model neurons in a biological brain.
A connection is like a synapse in a brain and is capable of transmitting signals from one artificial neuron to
another. This neuron processes the signal it receives and signals to more artificial neurons it is connected to.
Deep Neural Networks With Python – ANN

This way, we can have input, output, and hidden layers.


Some applications of Artificial Neural Networks have been Computer Vision, Speech Recognition, Machine
Translation, Social Network Filtering, Medical Diagnosis, and playing board and video games.
Do you know about Python machine Learning

b. Deep Neural Networks


Coming back, a Deep Neural Network is an ANN that has multiple layers between the input and the output
layers. Such a network sifts through multiple layers and calculates the probability of each output.

A DNN is capable of modeling complex non-linear relationships.

3. Structure of Deep Neural Network


A DNN is usually a feedforward network. This means data from the input layer flows to the output layer
without looping back.
Structure of deep Neural Networks with Python

Such a network with only one hidden layer would be a non-deep(or shallow) feedforward neural network.
But in a deep neural network, the number of hidden layers could be, say, 1000. But it must be greater than 2
to be considered a DNN.

Have a look at Python Machine Learning Algorithms

A DNN creates a map of virtual neurons and randomly assigns weights to the connections between these
neurons. It multiplies the weights with the inputs to return an output between 0 and 1. If it fails to recognize a
pattern, it uses an algorithm to adjust the weights.

4. Types of Deep Neural Networks with Python


Broadly, we can classify Python Deep Neural Networks into two categories:

a. Recurrent Neural Networks- RNNs

Deep Neural Networks with Python – Recurrent Neural Networks(RNNs)


A Recurrent Neural Network is a sort of ANN where the connections between its nodes form a directed graph
along a sequence. An RNN can use its internal state/ memory to process input sequences. Thus we can use it for
tasks like unsegmented, connected handwriting recognition and speech recognition. Kinds of RNN-

Finite Impulse Recurrent Network- A Directed Acyclic Graph (DAG) that we can replace with a strictly
feedforward neural network.

Infinite Impulse Recurrent Network- A Directed Cyclic Graph that we cannot unroll.

Do you know about Neural Networks Algorithms

A basic RNN is a network of neurons held into layers where each node in a layer connects one-way (and
directly) to every other node in the next layer. In an RNN, data can flow in any direction. We make use of
LSTM (Long Short-Term Memory) and use RNNs in applications like language modeling.

b. Convolutional Neural Network (CNN or ConvNet)

Deep Neural Networks with Python – Convolutional Neural Network (CNN or ConvNet)

A CNN is a sort of deep ANN that is feedforward. We use it for applications like analyzing visual imagery,
Computer Vision, acoustic modeling for Automatic Speech Recognition (ASR), Recommender Systems, and
Natural Language Processing (NLP).
A CNN uses multilayer perceptrons for minimal preprocessing. In such a network, the connectivity pattern
between neurons mimics how an animal visual cortex is organized. A CNN learns the filters and thus needs
little preprocessing. It has the following architecture-

Deep Neural Networks with Python – Architecture of CNN

5. Challenges to Deep Neural Networks


Two major challenges faced by Deep Neural Networks with Python –

a. Overfitting

Challenges to Deep Neural Networks with Python

Since a DNN possesses added layers of abstraction, it can model rare dependencies in the training data. To fight
this, we can-
Have a look at train and test set in Python ML

Use regularization methods like Ivakhnenko’s unit pruning, weight decay, or sparsity.

Using dropout regularization to randomly omit units from hidden layers when training.

Using methods like cropping and rotating to augment data; to enlarge smaller training sets.
b. Computation Time
To sweep through the parameter space (size, learning rate, initial weights) may lead to a need for more
computational resources and time. To battle this, we can-

Perform Batching to compute the gradient to multiple training examples at once.

Use many-core architectures for their large processing capabilities and suitability for matrix and vector
computations.

6. Deep Belief Networks


Before we can proceed to exit, let’s talk about one more thing- Deep Belief Networks. A DBN is a sort of deep
neural network that holds multiple layers of latent variables or hidden units. Such a network observes
connections between layers rather than between units at these layers.

Deep Belief Networks

If we train a DBN on a set of examples without supervision, we can let it learn to reconstruct input
probabilistically. You can call the layers feature detectors. After this, we can train it with supervision to carry
out classification.
Let’s discuss Python Deep Learning Environment Setup

So, this was all in Deep Neural Networks with Python. Hope you like our explanation.

7. Conclusion
In this Python Deep Neural Networks tutorial, we looked at Deep Learning, its types, the challenges it faces,
and Deep Belief Networks. Leave your suggestions and queries in the comments.

Computational Graphs in Deep Learning With Python


by DataFlair Team · September 29, 2018

1. Computational Graphs – Objective


In this Deep Learning With Python tutorial, we will tell you about computational graphs in Deep Learning.
We will show you how to implement those Computational graphs with Python. Moreover, while implementing
Deep Learning Computational Graphs in Python, we will look at dynamics and Forward-Backward
Propagation.

So, let’s begin Computation Graphs in Deep Learning With Python.

Computational Graphs in Deep Learning With Python

2. Deep Learning Computational Graphs


In fields like Cheminformatics and Natural Language Understanding, it is often useful to compute over data-
flow graphs. Computational Graph form an integral part of Deep Learning. Not only do they help us simplify
working with large datasets, they’re simple to understand. So in this tutorial, we will introduce them to you and
then show you how to implement them using Python. For this, we will use the Dask library from Python PyPI.
Do you know about Python Library

3. What are Computational Graphs in Deep Learning?


A computational graph is a way to represent a mathematical function in the language of graph theory. Nodes are
input values or functions for combining them; as data flows through this graph, the edges receive their weights.

What is Computational Graph

So we said we have two kinds of nodes- input nodes and function nodes. Outbound edges from input nodes bear
the input value, and those from function nodes bear the composite of the weights of the inbound edges. The
graph above represents the following expression:
f(x,y,z)=(x+y)*z
Have a look at deep Learning vs Machine Learning
Of the five nodes, the leftmost three are input nodes; the two on the right are function nodes. Now if we’d have
to compute f(1,2,3), we’d get the following computation graph-

Computational Graphs Deep Learning

4. Need of Computational Graph


Well, this was a simple computational graph with 5 nodes and 5 edges. But even simpler deep neural networks
observe hundreds of thousands of nodes and edges- say, more than one million? In such a case, it would be
practically impossible to calculate a function expression for it. Then, computational graphs come in handy.
Such graphs also help us describe backpropagation more precisely.

5. Computational Graphs Example – Composite Function


We take an example of the function f(x)=esin(x**2). Let’s decompose this-
f(x)=ex
g(x)=sin x
h(x)=x2
f(g(h(x)))=eg(h(x))
Let’s revise the Python Machine Learning Tutorial
We have the following computational graph for this-

Example of Computational Graphs

6. Visualizing a Computation Graph in Python


Now let’s use the Dask library to produce such a graph.

>>> from dask import delayed,compute

>>> import dask

>>> @delayed

def square(num):

print("Square function:",num)

print()

return num*num

>>> @delayed

def sum_list(args):

print("Sum_list function:",args)

return sum(args)

>>> items=[1,2,3]

>>> computation_graph = sum_list([square(i) for i in items])

>>> computation_graph.visualize()
<IPython.core.display.Image object>

>>> computation_graph.visualize(filename='sumlist.svg')

<IPython.core.display.SVG object>

Visualizing Computational Graphs in Python

Note that for this code to work, you will need to perform three tasks-

Install the dask library with pip:

pip install dask

Download and unzip the Windows packages for Dask-

https://graphviz.gitlab.io/_pages/Download/Download_windows.html

Add the first line of code to your User Path and the next to your system path in environment variables:

C:\Users\Ayushi\Desktop\graphviz-2.38\release\bin (Use your own path for bin)


C:\Users\Ayushi\Desktop\graphviz-2.38\release\bin\dot.exe

7. Dynamic Deep Learning Python Computational Graphs


DCGs suffer from the issues of inefficient batching and poor tooling. When each data in a data set has its type
or shape, it becomes a problem to have the neural network batch such data with a static graph. As a
workaround, we use an algorithm we call Dynamic Batching.
In other words, a Dynamic Computational Graph is a mutable directed graph with operations as vertices and
data as edges. In effect, it is a system of libraries, interfaces, and components. These deliver a flexible,
programmatic, runtime interface that lets us construct and modify systems by connecting operations.
Have a look at the Python Machine Learning Environment Set up

8. Forward and Backward Propagation in Computational Graphs


First, let’s talk about forward propagation. Here, we loop over nodes in a topological order. In other words, we
pass the values of the variables in the forward direction (left to right). Given a node’s inputs, we compute its
value.
In backward propagation, however, we start at a final goal node and loop over the nodes in a reverse topological
order. Here, we compute the derivatives of the final goal node value with respect to each edge’s tail node.
So, this was all in Computational Graphs Deep Learning With Python. Hope you like our explanation.

9. Conclusion: Computational Graphs


Hence, we wind up computational graphs for deep learning with Python. Moreover, we discussed
Computational Graphs Propagation and implementing graphs in Python. Furthermore, if you have any query,
feel free to ask in the comment box.

Python AI Tutorial | Artificial Intelligence Programming Python


by DataFlair Team · September 29, 2018

1. Python AI Tutorial
Today, in this Python AI Tutorial, we will take on an introduction to Artificial Intelligence. Moreover, in this
Artificial Intelligence Programming, we will see AI Problems, Tools in AI, and Artificial Intelligence
approaches.

So, let’s start the Python AI Tutorial.


Python AI Tutorial | Artificial Intelligence Programming Python

2. What is Artificial Intelligence?


Artificial Intelligence, often dubbed AI, is the intelligence a machine demonstrates. With machine intelligence,
it is possible to give a device the ability to discern its environment and act to maximize its chances of success in
achieving its goals. In other words, AI is when a machine can mimic cognitive functions like learning and
problem-solving.
“AI is whatever hasn’t been done yet.”

As we said, an AI takes in its environment and acts to maximize its chances of success in achieving its goals. A
goal can be simple or complex, explicit or induced. It is also true that many algorithms in AI can learn from
data, learn new heuristics to improve and write other algorithms.

Do you know about AI Algorithms

One difference to humans is that AI does not possess the features of human commonsense reasoning and folk
psychology. This makes it end up making different mistakes than a human would.

3. Python AI Tutorial – AI Problems


When simulating or creating AI, we may run into problems around the following traits-
Python AI Tutorial – AI Problems

a. Reasoning and Problem Solving


Earlier, algorithms mimicked step-by-step reasoning that humans display. AI research later introduced methods
to work with incomplete and uncertain information. However, as the problems grew larger, these algorithms
became exponentially slower. Humans often use fast, intuitive judgments instead of a step-by-step deduction.

b. Knowledge Representation
Some expert systems accumulate esoteric knowledge from experts. A comprehensive commonsense knowledge
base holds many things including- objects, properties, categories, relations between objects, situations, events,
states, time, causes, effects, knowledge about knowledge, and other domains. When we talk about ontology, we
talk about what exists. Under knowledge representation, we observe the following domains-
Have a look at Robotics and Artificial Intelligence

Default reasoning; Qualification problem

The breadth of commonsense knowledge

The subsymbolic form of some commonsense knowledge

c. Planning
An intelligent agent should be capable of setting goals, achieving them, and visualizing the future. Assuming it
is the only system in the world, an agent can be certain of their actions’ consequences. If there are more actors,
the agent should be able to reason under uncertainty. For this, it should be able to assess its environment, make
predictions, evaluate predictions, and adapt according to its assessment. With multi-agent planning, we observe
multiple agents cooperate and compete to achieve a goal.

d. Learning
AI is related to Machine Learning in some way. We have often talked about unsupervised learning- the ability
to take a stream of input and find patterns in it. This includes classification and numerical regression. We
classify things into categories and produce a function that describes how inputs and outputs relate and change
each other. These function approximators.

e. Natural Language Processing


NLP is an area of Computer Science that gives machines the ability to read the human language and understand
it. With it, we can retrieve information, mine text, answer questions, and translating using machines. We use
strategies like keyword spotting and lexical affinity.

f. Perception
With machine perception, we can take input from sensors like cameras, microphones, and lidar to recognize
objects. We can use it for applications like speech recognition, facial recognition, and object recognition. We
can also analyze visual input with Computer Vision.

g. Motion and Manipulation


With AI, we can develop advanced robotic arms and more for modern factories. These can use the experience to
learn to deal with friction and gear slippage. The term Motion Planning means dividing a task into primitives
like individual joint movements.

h. Social Intelligence
“Should I go to bed, Siri?”, I ask as I reach home from a busy day. “I think you should sleep on it”, Siri quips
back. Affective Computing, an umbrella term, encompasses systems that can recognize, interpret, process, or
simulate human affects/ emotions. In this domain, we have observed textual sentiment analysis and multimodal
affect analysis. The aim is to allow AI to understand others’ motives and emotional states to predict their
actions. It can mimic human emotion and expressions to appear sensitive and interact with humans. A robot
with rudimentary social skills is Kismet, developed at MIT by Dr. Cynthia Breazeal.

Do you know expert systems in Artificial Intelligence to Solve Problems

i. General Intelligence
Lately, many AI researchers have begun working on tractable narrow AI applications like a medical diagnosis.
The future could hold machines with Artificial General Intelligence(AGI) that combines such narrow skills.
Google’s DeepMind will be an example of this.

4. Python AI Tutorial – Approaches


We observe four different approaches to AI-

Python AI Tutorial – Approaches

a. Cybernetics and Brain Simulation


Some machines exist that use electronic networks to display rudimentary intelligence.

b. Symbolic
This approach considers reducing human intelligence to symbolic manipulation. This includes cognitive
simulation, logic-based, anti-logic or scruffy, and knowledge-based approaches.

c. Sub-Symbolic
For processes of human cognition like perception, robotics, learning, and pattern recognition, sub-symbolic
approaches came into picture. This includes approaches like embodied intelligence and computational
intelligence and soft computing.
Let’s discuss Data Science vs AI vs Machine Learning

d. Statistical Learning
Statistical learning techniques like HMM and neural networks deliver better accuracy in practical domains like
data mining. Limitations of HMM include that it cannot model the infinite possible combinations of natural
language.

5. Python AI Tutorial – Artificial Intelligence Tools


In this section of the Python AI Tutorial, we will study the different tools used in Artificial Intelligence:

Python AI Tutorial – Artificial Intelligence Tools

a. Search and Optimization


To intelligently search through possible solutions and use reasoning to do so is a tool for AI. For real-world
problems, simple exhaustive searches rarely suffice. This is because these have really large search spaces. This
leads to a slow search or one that never ends. To get around this, we can use heuristics.

b. Logic
AI research uses different forms of logic. Propositional logics use truth functions like ‘or’ and ‘not’. The fuzzy
set theory holds a degree of truth (values between 0 and 1) to vague statements. First-order logic adds
quantifiers and predicates. Fuzzy logic helps with control systems to contribute vague rules.

c. Probabilistic Methods for Uncertain Reasoning


We often use tools like Bayesian networks for reasoning, learning, planning, and perception. We can also use
probabilistic algorithms to filter, predict, smoothen, and explain streams of data.

d. Classifiers and Statistical Learning Methods


Classifiers and controllers work together. Consider an object. If it is shiny, the classifier knows it is a diamond.
If it is shiny, the controller picks it up. But before inferring an action, a controller classifies conditions. As a
function, a classifier matches patterns to find the closest match. Supervised learning puts each pattern into a
predefined class.
e. Artificial Neural Networks
ANNs are collections of nodes that are interconnected- inspired by the huge network of neurons in the human
brain.

Python AI Tutorial – Artificial Neural Networks

Under these, we have categories like feedforward neural networks and recurrent neural networks. We will take
up ANNs as a separate topic in another tutorial.

f. Evaluating Progress
Since AI is general purpose, there is no way to find out which domains it excels in. Games are a good
benchmark to assess progress. Some of these include AlphaGo and StarCraft.

6. Python AI Tutorial – Applications of Artificial Intelligence


Like we said, AI is pretty general-purpose. Here are a few domains it finds use in-

Automotive

Healthcare

Video games

Military

Finance and Economics

Art

Auditing

Advertising

Follow the link to learn more about AI Applications

So, this was all in Python AI Tutorial. Hope you like our explanation.

7. Conclusion
Hence, in this Python AI Tutorial, we learned about artificial intelligence. We talked about its problems,
approaches, tools, and applications. That’s all for today; we’ll be back with more. Tell us about your experience
of Python AI Tutorial in the comments.

NLTK Python Tutorial (Natural Language Toolkit)


by DataFlair Team · November 2, 2018

1. NLTK Python Tutorial

In our last session, we discussed the NLP Tutorial. Today, in this NLTK Python Tutorial, we will learn to
perform Natural Language Processing with NLTK. We will perform tasks like NLTK tokenize, removing stop
words, stemming NLTK, lemmatization NLTK, finding synonyms and antonyms, and more.

So, let’s start NLTK Python Tutorial.


NLTK Python Tutorial (Natural Language Toolkit)

2. What is NLTK?

NLTK stands for Natural Language Toolkit. This is a suite of libraries and programs for symbolic and statistical
NLP for English. It ships with graphical demonstrations and sample data. First getting to see the light in 2001,
NLTK hopes to support research and teaching in NLP and other areas closely related. These include Artificial
Intelligence, empirical linguistics, cognitive science, information retrieval, and Machine Learning.

3. How to Install NLTK?

To install NLTK, you can use Python pip-

pip install nltk

Then to import it, you can type in the interpreter-


You must know about Python Libraries

>>> import nltk

Finally, to install packages from NLTK, you need to use its downloader. Try this-

>>> nltk.download()
NLTK Python Tutorial – How to Install NLTK?

You can download all packages or choose the ones you wish to download. Explore the ‘All Packages’ tab.
Have a look at Python Packages

4. NLTK Python Tutorial – NLTK Tokenize Text


Before processing the text in NLTK Python Tutorial, you should tokenize it. What we mean is you should split
it into smaller parts- paragraphs to sentences, sentences to words. We have two kinds of tokenizers- for
sentences and for words.

a. NLTK Sentence Tokenizer


Let’s try tokenizing a sentence.

>>> text="Today is a great day. It is even better than yesterday. And yesterday was the best day ever."

>>> from nltk.tokenize import sent_tokenize

>>> sent_tokenize(text)

[‘Today is a great day.’, ‘It is even better than yesterday.’, ‘And yesterday was the best day ever.’]
Let’s try another? We’ll use an exchange of dialogues this time.
>>> sent_tokenize("Hi, how are you? I'm good, you? Great!")

[‘Hi, how are you?’, “I’m good, you?”, ‘Great!’]


Okay, one more in NLTK Python Tutorial. How about we use words like Mrs. and Martinez’s in the text? Will
the tokenizer be able to catch it?

>>> nltk.sent_tokenize("Last night, I went to Mrs. Martinez's housewarming. It was a disaster.")

[“Last night, I went to Mrs. Martinez’s housewarming.”, ‘It was a disaster.’]

Tokenizing for a language other than English-

>>> sent_tokenize("Enchanté, comment allez-vous? Tres bien. Mersi, et vous?","french")

[‘Enchanté, comment allez-vous?’, ‘Tres bien.’, ‘Mersi, et vous?’]

Issues while tokenizing-

Do you know about Python NumPy


One issue we face while tokenizing is abbreviations-

>>> sent_tokenize("She holds an MDS. in Oral Pathology")

[‘She holds an MDS.’, ‘in Oral Pathology’]


Whoops! That was supposed to be one complete sentence it split into two.

b. NLTK Word Tokenizer


First, let’s tokenize text in NLTK Python Tutorial.

>>> nltk.word_tokenize(text)

[‘Today’, ‘is’, ‘a’, ‘great’, ‘day’, ‘.’, ‘It’, ‘is’, ‘even’, ‘better’, ‘than’, ‘yesterday’, ‘.’, ‘And’, ‘yesterday’,
‘was’, ‘the’, ‘best’, ‘day’, ‘ever’, ‘.’]
Now, let’s try it for Mrs. Martinez’s housewarming.

>>> nltk.word_tokenize("Last night, I went to Mrs. Martinez's housewarming. It was a disaster.")

[‘Last’, ‘night’, ‘,’, ‘I’, ‘went’, ‘to’, ‘Mrs.’, ‘Martinez’, “‘s”, ‘housewarming’, ‘.’, ‘It’, ‘was’, ‘a’, ‘disaster’,
‘.’]
Notice how it judged Mrs. to be one word but Martinez’s to be two? NLTK uses PunktSentenceTokenizer for
this.
Have a look at Python Charts

5. Find Synonyms From NLTK WordNet

WordNet is an NLP database with synonyms, antonyms, and brief definitions. We downloaded this with the
NLTK downloader.

>>> from nltk.corpus import wordnet


>>> syn=wordnet.synsets('love')

>>> syn

[Synset(‘love.n.01’), Synset(‘love.n.02’), Synset(‘beloved.n.01’), Synset(‘love.n.04’), Synset(‘love.n.05’),


Synset(‘sexual_love.n.02’), Synset(‘love.v.01’), Synset(‘love.v.02’), Synset(‘love.v.03’),
Synset(‘sleep_together.v.01’)]
Let’s choose the first member from this-

>>> syn[0].definition()

‘a strong positive emotion of regard and affection’

>>> syn[0].examples()

[‘his love for his work’, ‘children need a lot of love’]


But love isn’t the only thing we’re chasing after. Do you know the meaning of life? 42?

>>> syn=wordnet.synsets('life')

>>> syn[0].definition()

‘a characteristic state or mode of living’

>>> syn[0].examples()

[‘social life’, ‘city life’, ‘real life’]


Nope, still doesn’t answer our questions. But Python does a good job. Okay, one more example.

>>> syn=wordnet.synsets('AI')

>>> syn

[Synset(‘army_intelligence.n.01’), Synset(‘artificial_intelligence.n.01’), Synset(‘three-toed_sloth.n.01’),


Synset(‘artificial_insemination.n.01’)]

>>> syn[1].definition()

‘the branch of computer science that deal with writing computer programs that can solve problems
creatively’

>>> syn[1].examples()

[‘workers in AI hope to imitate or duplicate intelligence in computers and robots’]


Let’s revise Python Linear Regression

To get the list of synonyms:

>>> synonyms=[]

>>> for syn in wordnet.synsets('AI'):


for lemma in syn.lemmas():

synonyms.append(lemma.name())

>>> synonyms

[‘Army_Intelligence’, ‘AI’, ‘artificial_intelligence’, ‘AI’, ‘three-toed_sloth’, ‘ai’, ‘Bradypus_tridactylus’,


‘artificial_insemination’, ‘AI’]

6. Find Antonyms From NLTK WordNet

To get the list of antonyms, we first need to check the lemmas- are there antonyms?

>>> from nltk.corpus import wordnet

>>> antonyms=[]

>>> for syn in wordnet.synsets('depressed'):

for l in syn.lemmas():

if l.antonyms():

antonyms.append(l.antonyms()[0].name())

>>> antonyms

[‘elate’]
One more?

>>> for syn in wordnet.synsets('ugly'):

for l in syn.lemmas():

if l.antonyms():

antonyms.append(l.antonyms()[0].name())

>>> antonyms

[‘beautiful‘]

7. NLTK Python Tutorial – Stemming NLTK

We have talked of stemming before this. Check Stemming and Lemmatization with Python. Well, stemming
involves removing affixes from words and returning the root. Search engines like Google use this to efficiently
index pages. The most common algorithm for stemming is the PorterStemmer. Let’s take an example.

>>> stemmer.stem('loving')

‘love’
>>> stemmer.stem('trainee')

‘traine’
By now, you have figured we don’t settle for just one example. So here’s more.

>>> stemmer.stem('syllabi')

‘syllabi’

>>> stemmer.stem('alibi')

‘alibi’

>>> stemmer.stem('formulae')

‘formula’
You must read Python Heatmap

>>> stemmer.stem('criteria')

‘criteria’

>>> stemmer.stem('believes')

‘believ’

>>> stemmer.stem('writes')

‘write’

>>> stemmer.stem('writing')

‘write’

>>> stemmer.stem('write')

‘write’
NLTK Python Tutorial – Stemming

Other algorithms include Lancaster and Snowball.

a. Stemming Words from Other Languages


>>> from nltk.stem import SnowballStemmer

>>> SnowballStemmer.languages

(‘arabic’, ‘danish’, ‘dutch’, ‘english’, ‘finnish’, ‘french’, ‘german’, ‘hungarian’, ‘italian’, ‘norwegian’,
‘porter’, ‘portuguese’, ‘romanian’, ‘russian’, ‘spanish’, ‘swedish’)

>>> rom_stemmer=SnowballStemmer('romanian')

>>> rom_stemmer.stem('englezească') #English

‘englez’

>>> rom_stemmer.stem('cocoș') #Cocks

‘cocoș’

>>> rom_stemmer.stem('frigidere') #Refrigerators

‘frigid’
Let’s discuss Python Deep Learning

>>> rom_stemmer.stem('frigider') #Refrigerator

‘frigider’

>>> rom_stemmer.stem('goi') #Empty- plural

‘goi’
>>> rom_stemmer.stem('goale') #Empty- plural

‘goal’

>>> rom_stemmer.stem('frumoasă') #Beautiful- female

‘frumoas’

>>> rom_stemmer.stem('frumoase') #Beautiful- plural

‘frumoas’

>>> rom_stemmer.stem('frumoși') #Beautiful- plural

‘frumoș’
Psst., please let us know if you catch an incorrect depiction of the language Romanian!

8. Python NLTK Tutorial – Lemmatizing NLTK Using WordNet

If you noticed, some words that stemming gave us weren’t actual words you could look up in the dictionary. So
we come to lemmatizing- this will return real words. Let’s do this too.
Do you know about Python Statistics

>>> from nltk.stem import WordNetLemmatizer

>>> lemmatizer=WordNetLemmatizer()

>>> lemmatizer.lemmatize('believes')

‘belief’
Stemming gave us ‘believ’ for this; lemmatizing gave us ‘belief’. See the clear difference? Where stemming
returned ‘thi’, see what lemmatizer gives us:

>>> lemmatizer.lemmatize('this')

‘this’
With ‘believes’, to work with a verb instead of a noun, use the ‘pos’ argument-

>>> lemmatizer.lemmatize('believes',pos='v')

‘believe’
And now, how about some adjectives?

>>> lemmatizer.lemmatize('crossing',pos='a') #adjective

‘crossing’

>>> lemmatizer.lemmatize('crossing',pos='v') #verb

‘cross’
>>> lemmatizer.lemmatize('crossing',pos='n') #noun

‘crossing’

>>> lemmatizer.lemmatize('crossing',pos='r') #adverb

‘crossing’
Since lemmatizing gives us better results within context, it is often slower than stemming.
Let’s take a tour to Python SciPy

9. Python NLTK Tutorial – NLTK Stop Words

We can filter NLTK stop words from text before processing it.

>>> from nltk.corpus import stopwords

>>> stopwords.words('english')

[‘i’, ‘me’, ‘my’, ‘myself’, ‘we’, ‘our’, ‘ours’, ‘ourselves’, ‘you’, “you’re”, “you’ve”, “you’ll”, “you’d”,
‘your’, ‘yours’, ‘yourself’, ‘yourselves’, ‘he’, ‘him’, ‘his’, ‘himself’, ‘she’, “she’s”, ‘her’, ‘hers’, ‘herself’,
‘it’, “it’s”, ‘its’, ‘itself’, ‘they’, ‘them’, ‘their’, ‘theirs’, ‘themselves’, ‘what’, ‘which’, ‘who’, ‘whom’,
‘this’, ‘that’, “that’ll”, ‘these’, ‘those’, ‘am’, ‘is’, ‘are’, ‘was’, ‘were’, ‘be’, ‘been’, ‘being’, ‘have’, ‘has’,
‘had’, ‘having’, ‘do’, ‘does’, ‘did’, ‘doing’, ‘a’, ‘an’, ‘the’, ‘and’, ‘but’, ‘if’, ‘or’, ‘because’, ‘as’, ‘until’,
‘while’, ‘of’, ‘at’, ‘by’, ‘for’, ‘with’, ‘about’, ‘against’, ‘between’, ‘into’, ‘through’, ‘during’, ‘before’,
‘after’, ‘above’, ‘below’, ‘to’, ‘from’, ‘up’, ‘down’, ‘in’, ‘out’, ‘on’, ‘off’, ‘over’, ‘under’, ‘again’,
‘further’, ‘then’, ‘once’, ‘here’, ‘there’, ‘when’, ‘where’, ‘why’, ‘how’, ‘all’, ‘any’, ‘both’, ‘each’, ‘few’,
‘more’, ‘most’, ‘other’, ‘some’, ‘such’, ‘no’, ‘nor’, ‘not’, ‘only’, ‘own’, ‘same’, ‘so’, ‘than’, ‘too’, ‘very’,
‘s’, ‘t’, ‘can’, ‘will’, ‘just’, ‘don’, “don’t”, ‘should’, “should’ve”, ‘now’, ‘d’, ‘ll’, ‘m’, ‘o’, ‘re’, ‘ve’, ‘y’,
‘ain’, ‘aren’, “aren’t”, ‘couldn’, “couldn’t”, ‘didn’, “didn’t”, ‘doesn’, “doesn’t”, ‘hadn’, “hadn’t”, ‘hasn’,
“hasn’t”, ‘haven’, “haven’t”, ‘isn’, “isn’t”, ‘ma’, ‘mightn’, “mightn’t”, ‘mustn’, “mustn’t”, ‘needn’,
“needn’t”, ‘shan’, “shan’t”, ‘shouldn’, “shouldn’t”, ‘wasn’, “wasn’t”, ‘weren’, “weren’t”, ‘won’,
“won’t”, ‘wouldn’, “wouldn’t”]

>>> from nltk.corpus import stopwords

>>> text="Today is a great day. It is even better than yesterday. And yesterday was the best day ever!"

>>> stopwords=set(stopwords.words('english'))

>>> from nltk.tokenize import word_tokenize

>>> words=word_tokenize(text)

>>> wordsFiltered=[]

>>> for w in words:

if w not in stopwords:

wordsFiltered.append(w)
>>> wordsFiltered

[‘Today’, ‘great’, ‘day’, ‘.’, ‘It’, ‘even’, ‘better’, ‘yesterday’, ‘.’, ‘And’, ‘yesterday’, ‘best’, ‘day’, ‘ever’,
‘!’]

10. NLTK Python Tutorial – Speech Tagging

NLTK can classify words as verbs, nouns, adjectives, and more into one of the following classes:

Python NLTK Tutorial – Speech Tagging

>>> import nltk

>>> from nltk.tokenize import PunktSentenceTokenizer

>>> text='I am a human being, capable of doing terrible things'

>>> sentences=nltk.sent_tokenize(text)

>>> for sent in sentences:

print(nltk.pos_tag(nltk.word_tokenize(sent)))

[(‘I’, ‘PRP’), (‘am’, ‘VBP’), (‘a’, ‘DT’), (‘human’, ‘JJ’), (‘being’, ‘VBG’), (‘,’, ‘,’), (‘capable’, ‘JJ’), (‘of’,
‘IN’), (‘doing’, ‘VBG’), (‘terrible’, ‘JJ’), (‘things’, ‘NNS’)]
Let’s revise Python Matplotlib
Match the arguments at position 1 with the table to figure out the output.
So, this was all in NLTK Python Tutorial. Hope you like our explanation.

11. Conclusion – NLTK Python Tutorial

Hence, in this NLTK Python Tutorial, we discussed the basics of Natural Language Processing with Python
using NLTK. Moreover, we discussed tokenizing, stemming, lemmatization, finding synonyms and antonyms,
speech tagging, and filtering out stop words. Still, if you have any query regarding NLTK Python Tutorial, ask
in the comment tab. You can also share your experience of reading this article through comments.
Python Speech Recognition – Artificial Intelligence
by DataFlair Team · September 29, 2018

1. Python Speech Recognition

Welcome to our Python Speech Recognition Tutorial. In this tutorial of AI with Python Speech Recognition,
we will learn to read an audio file with Python. We will make use of the speech recognition API to perform this
task. Moreover, we will discuss reading a segment and dealing with noise.

So, let’s start the Python Speech recognition Tutorial.

Python Speech Recognition – Artificial Intelligence

2. What is Python Speech Recognition?

From systems facilitating single speakers and limited vocabularies of around a dozen words, to systems that
recognize from multiple speakers and possess huge vocabularies in various languages, we have come a long
way. What we do here is- we convert speech from physical sound to electrical signals using a microphone.
Then, we use an analogue-to-digital converter to convert this to digital data. Finally, we use multiple models to
transcribe audio to text. In the Hidden Markov Model (HMM), we divide the speech signal into 10-
millisecond fragments.
Do you know about Recursion in Python

a. Available APIs in Python Speech Recognition


With Python, we have several APIs available:

apiai
assemblyai

google-cloud-speech

pocketsphinx

SpeechRecognition

watson-developer-cloud

wit

Some Python packages like wit and apiai offer more than just basic speech recognition. Here, though, we will
demonstrate SpeechRecognition, which is easier to use. This hard-codes a default API key for the Google Web
Speech API.

b. Supported File Types in Python Speech Recognition


WAV- PCM/LPCM format

AIFF

AIFF-C

FLAC

c. Prerequisites for Python Speech Recognition


You can use pip to install this-

pip install SpeechRecognition

To test the installation, you can import this in the interpreter and check the version-

>>> import speech_recognition as sr

>>> sr. version

‘3.8.1’

We also download a sample audio from here-

http://www.voiptroubleshooter.com/open_speech/american.html

3. Reading an Audio File in Python

a. The Recognizer class


First, we make an instance of the Recognizer class.
>>> r=sr.Recognizer()

With Recognizer, we have a method for each API-

recognize_bing()- Microsoft Bing Speech

recognize_google()- Google Web Speech API

recognize_google_cloud()- Google Cloud Speech

recognize_houndify()- Houndify

recognize_ibm()- IBM Speech to Text

recognize_sphinx- CMU Sphinx

recognize_wit()- Wit.ai

Exempting recognize_sphinx(), you need an Internet connection for anything else you’re working with.

You must read the Python web framework

b. Capturing data with record()


We can have the context manager open the file and read its contents, then record it into an AudioData instance.

>>> demo=sr.AudioFile('demo.wav')

>>> with demo as source:

audio=r.record(source)

To confirm this, try:

>>> type(audio)

<class ‘speech_recognition.AudioData’>

c. Recognizing Speech in the Audio


Finally, you can call recognize_google() to perform the transcription.

>>> r.recognize_google(audio)

“The Purge can use within The Smurfs the sheet without playback Mount delivery date habitat of a Vow
these days it’s okay microwave devices are installed in Windows to use of lemons next find the password
on the site that the houses such hard core in a garbage for the study core exercises talking is hard disk”

Well, you can read audio of a different language using the language parameter-

r.recognize_google(audio,language='ro-RO') #for Romanian


4. Reading a Segment of Audio

When you only want to read a part of your audio file, you can use the arguments offset– telling it where to begin
(in seconds), and duration– telling it how long to listen.

Let’s take a tour of Python Datetime Module

>>> with demo as source:

audio=r.record(source,offset=4,duration=3)

>>> r.recognize_google(audio)

‘clear the sheet without me back’

Note that this caused issues at the extremes. It heard ‘murfs’, which it translated to ‘clear’. It also heard ‘me
back’ instead of ‘playback’ because of the noise in the audio.

If we set the offset to 3.3,

>>> with demo as source:

audio=r.record(source,offset=3.3,duration=3)

>>> r.recognize_google(audio)

‘clear the sheet with Ok’

But check what happens when we set the offset to 2.5-

>>> with demo as source:

audio=r.record(source,offset=2.5,duration=3)

>>> r.recognize_google(audio)

‘National thanks’

5. Python Speech Recognition – Dealing with Noise

Okay, let’s face it. There will always be noise, no matter how professional appliances you use to record your
audio. So let’s better learn to deal with it. The method adjust_for_ambient_noise() reads the first second of a file
stream to calibrate the recognizer to the audio’s noise level. This often consumes that part of the audio, and it
doesn’t make it to the transcription.

Do you know about Python Property

>>> with demo as source:

r.adjust_for_ambient_noise(source)
audio=r.record(source,offset=2.5,duration=3)

>>> r.recognize_google(audio)

‘clear the sheet’


We can provide this an argument for how long it should listen for noise so it can calibrate the recognizer. Let’s
see how it produces two entirely different outputs for a difference as low as 0.005-

>>> with demo as source:

r.adjust_for_ambient_noise(source,duration=0.51)

audio=r.record(source,offset=2.5,duration=3)

>>> r.recognize_google(audio)

‘National thanks’

>>> with demo as source:

r.adjust_for_ambient_noise(source,duration=0.515)

audio=r.record(source,offset=2.5,duration=3)

>>> r.recognize_google(audio)

‘clear the sheet’

As you can see, adjust_for_ambient_noise() is definitely not a miracle worker. To get around this, you can use
an audio-editing software like Audacity to preprocess the audio.

6. Working With Microphones

To be able to work with your own voice with speech recognition, you need the PyAudio package. You can
install it with pip-

pip install PyAudio

Or you can download and install the binaries with pip. Download link-

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

Then:

pip install [file_name_for_binary]

For example:

pip install PyAudio-0.2.11-cp37-cp37m-win32.whl


a. The Microphone class
Like Recognizer for audio files, we will need Microphone for real-time speech data. Since we installed new
packages, let’s exit our interpreter and open another session.

>>> import speech_recognition as sr

>>> r=sr.Recognizer()

Now, let’s create an instance of Microphone.

>>> mic=sr.Microphone()

Microphone has a static method to list out all microphones available-

>>> sr.Microphone.list_microphone_names()

[‘Microsoft Sound Mapper – Input’, ‘Microphone (Realtek High Defini’, ‘Microsoft Sound Mapper –
Output’, ‘Speakers (Realtek High Definiti’, ‘Primary Sound Capture Driver’, ‘Microphone (Realtek
High Definition Audio)’, ‘Primary Sound Driver’, ‘Speakers (Realtek High Definition Audio)’, ‘Speakers
(Realtek High Definition Audio)’, ‘Microphone (Realtek High Definition Audio)’, ‘Speakers (Realtek HD
Audio output)’, ‘Line In (Realtek HD Audio Line input)’, ‘Microphone (Realtek HD Audio Mic input)’,
‘Stereo Mix (Realtek HD Audio Stereo input)’]

Now it is possible to select a certain microphone by its device index with likes of the following piece of code-

>>> mic=sr.Microphone(device_index=3)

But let’s stick with the default for now.

Get the best guide for Python Career

b. Capturing Microphone Input


With the context manager, we capture input using the listen() method.

>>> with mic as source:

audio=r.listen(source)

You shall now speak into your microphone. When it detects silence, it stops listening. It then displays the
interpreter prompt (>>>).

>>> r.recognize_google(audio)

decease a test
You can call the adjust_for_ambient_noise() method with Microphone too.

>>> with mic as source:

r.adjust_for_ambient_noise(source)
audio=r.listen(source)

>>> r.recognize_google(audio)

this is a test

c. Unintelligible Speech
When Python cannot match some audio to text, it raises an UnknownValueError exception.

Get prepare yourself for upcoming Python Interview

>>> r.recognize_google(audio)

Traceback (most recent call last):


File “<pyshell#7>”, line 1, in <module>
r.recognize_google(audio)
File “C:\Users\Ram\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\speech_recognition\ init .py”, line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get(“alternative”, [])) == 0: raise
UnknownValueError()
speech_recognition.UnknownValueError

Some pieces of audio that would lead to this will be- coughing sounds, gagging sounds, hand claps, and tongue
clicks.

So, this was all in Python Speech Recognition. Hope you like our explanation.

7. Conclusion – Python Speech Recognition

Did you see how easy it was to recognize speech with Python? The APIs made it possible. Well, why we stuffed
this into the AI tutorial doesn’t need explanation. Python Speech recognition forms an integral part of Artificial
Intelligence. What would Siri or Alexa be without it?. So, in conclusion to this Python Speech Recognition, we
discussed Speech Recognition API to read an Audio file in Python. Moreover, we saw reading a segment and
dealing with noise in Speech Recognition Python tutorial. You can freely tell us the reading experience of this
article through comments.

NLP Tutorial AI with Python | Natural Language Processing


by DataFlair Team · September 29, 2018

1. NLP Tutorial – Objective


Today, in our journey of Artificial Intelligence with Python, we will discuss NLP Tutorial, we will discuss the
rudiments of Natural Language Processing. We will start our NLP tutorial with NLP definition and a brief
introduction. Then, we will move towards components, libraries, and benefits of natural processing language.
Also, we will discuss Natural Language Processing Applications, NLP examples, and tools.
So, let’s start NLP Tutorial.
NLP Tutorial AI with Python | Natural Language Processing

2. Introduction to Natural Language Processing


Natural Language Processing is casually dubbed NLP. It is a field of AI that deals with how computers and
humans interact and how to program computers to process and analyze huge amounts of natural language data.
This faces some challenges like speech recognition, natural language understanding, and natural language
generation.
Let’s have a look at Python AI Tutorial
Well, NLP is all about developing applications and services that can understand human languages.

3. NLP Tutorial – What is NLP?


In layman terms, NLP is a way for computers to analyze human language and derive useful meaning from it. It
lets you organize and structure knowledge to let you perform the following tasks-

Automatic Summarization

Translation

Named Entity Recognition

Relationship Extraction

Sentiment Analysis

Speech Recognition

Topic Segmentation
NLP analyzes text and allows machines to understand how we speak. It considers the hierarchical structure of
language and performs tasks like correcting the grammar, converting speech to text, and translating between
languages. In computer science, it is a hard problem.
Have a look at Neural Network in Artificial Intelligence
“What do words mean, how do they link together, and what meaning do they make?”
The greatest challenge to NLP is to accurately judge the intention of words keeping in mind the ambiguity of
the language.

4. NLP Tutorial – Components of NLP


While talking of NLP in NLP Tutorial, we come across two main Components of NLP-

NLP Tutorial – Components of NLP

a. Natural Language Understanding (NLU)


Natural Language Understanding revolves around machine reading comprehension. This is an AI-hard problem.
An NLU system needs the following components-

Lexicon, Parser, and Grammar rules.

Semantic theory- to guide comprehension.

b. Natural Language Generation (NLG)


NLG is concerned with generating natural language. It uses a machine representation system like a knowledge
base or a logical form. You can think of it as a translator between data and natural language representation; this
is the opposite or NLU. This involves three tasks-
Text Planning- To extract relevant content from the knowledge base.

Sentence Planning- To choose appropriate words, form meaningful phrases, and set sentence tone.

Text Realization- To map the sentence plan into sentence structure.

5. NLP Tutorial – Benefits of NLP


Among the numerous benefits of NLP, here, we list out a few-

To improve the efficiency of the documentation process.

NLP is used to improve the accuracy of the documentation process.

To identify pertinent information from large databases.

Together with Machine Learning, we don’t need to hand-code large sets of rules.

6. NLP Tutorial – Libraries for NLP


Many open-source libraries let us work with Natural Language Programming. Some of those are-

Natural Language ToolKit (NLTK)- Written in Python; allows modules for processing text, classifying,
tokenizing, stemming, parsing, tagging, and more.

Apache OpenNLP- Machine Learning toolkit; allows for tokenizers, sentence segmentation, part-of-speech
tagging, chunking, parsing, named entity extraction, and more.

Stanford NLP Suite- Tools for part-of-speech tagging, named entity recognizer, sentiment analysis,
conference resolution system, and more.

Gate NLP Library.

MALLET- Java package for latent dirichlet allocation, clustering, topic modeling, information extraction,
document classification, and more.

7. NLP Tutorial – Glossary in NLP


Talking of NLP, we talk:
NLP Tutorial – Glossary in NLP

a. Phonology
Study of organizing sound systematically.
b. Morphology
Study of constructing words from primitive meaningful units.
c. Morpheme
Primitive unit of meaning in a language.
d. Syntax
Arranging words to form a sentence; determining the structural role of words in sentences and phrases.
e. Semantics
Studying the meanings of words and combining them to make meaningful phrases and sentences.
f. Pragmatics
Using and understanding sentences in various situations; determining how this affects sentence interpretation.
g. Discourse
Understanding how a sentence can affect the next.
h. World Knowledge
General knowledge about the world.
Do you know about Search Algorithms in Artificial Intelligence

8. NLP Tutorial – Tasks in NLP


With Natural Language Processing, we carry out five different tasks-
NLP Tutorial – Tasks in NLP

a. Lexical Analysis
Lexical analysis deals with identifying and analyzing word structure. We divide the whole chunk of text into
paragraphs, sentences, and words.

b. Syntactic Analysis
Also called parsing, it involves analyzing words in sentences for grammar and rearranging them to determine
how they relate to each other. It rejects sentences like “The apple eats the girl”.

c. Semantic Analysis
This deals with extracting the dictionary meanings from text. It also maps syntactic structures and objects in the
task domain to check for meaningfulness. It rejects statements like “tall stub”.

d. Discourse Integration
It analyzes the previous sentence to guess the meaning of the current sentence and the one after it.

e. Pragmatic Analysis
This reinterprets the statement to ensure it determines correctly what the statement means. It tries to retrieve
aspects of the language that requires knowledge of the real world.
Have a look at Python Machine Learning Tutorial
9. NLP Tutorial – NLP Applications
With NLP, we can do the following-

Summarizing blocks of text.

Creating chatbots.

Machine translation.

Fighting spam.

Extracting information.

Automatically generating keyword tags.

Identifying types of entities extracted.

Identifying the sentiment of a string with sentiment analysis.

Reducing words to their roots.

Summarizing.

Question-answering.

Customer service.

Market analysis.

So, this was all in NLP Tutorial. Hope you like our explanation of Natural Processing Language.

10. Conclusion: NLP Tutorial


And with this, we conclude our introduction to Natural Language Processing with Python. In this Natural
language Processing Tutorial, we discussed NLP Definition, AI natural language processing, and example of
NLP. Moreover, we talked about its fundamentals, components, benefits, libraries, terminologies, tasks, and
applications. Next, we will demonstrate the use of NLTK to implement NLP with Python. Still, if any doubt
regarding NLP Tutorial, ask in the comment tab.

What is Heuristic Search – Techniques & Hill Climbing in AI


by DataFlair Team · November 22, 2018

1. Objective – Heuristic Search

In this Python AI tutorial, we will discuss the rudiments of Heuristic Search, which is an integral part
of Artificial Intelligence. We will talk about different techniques like Constraint Satisfaction Problems,
Hill Climbing, and Simulated Annealing. Also, we will implement CSP in Python.
So, let’s begin Heuristic Search in AI Tutorial.
Heuristic Search in Artificial Intelligence – Python

First, let’s revise the Artificial Intelligence Tutorial

2. What is a Heuristic Search?

A Heuristic is a technique to solve a problem faster than classic methods, or to find an approximate solution
when classic methods cannot. This is a kind of a shortcut as we often trade one of optimality, completeness,
accuracy, or precision for speed. A Heuristic (or a heuristic function) takes a look at search algorithms. At each
branching step, it evaluates the available information and makes a decision on which branch to follow. It does
so by ranking alternatives. The Heuristic is any device that is often effective but will not guarantee work in
every case.

You must take a look at NLP Tutorial

So why do we need heuristics? One reason is to produce, in a reasonable amount of time, a solution that is good
enough for the problem in question. It doesn’t have to be the best- an approximate solution will do since this is
fast enough. Most problems are exponential. Heuristic Search let us reduce this to a rather polynomial number.
We use this in AI because we can put it to use in situations where we can’t find known algorithms.
We can say Heuristic Techniques are weak methods because they are vulnerable to combinatorial explosion.

3. Heuristic Search Techniques in Artificial Intelligence

Briefly, we can taxonomize such techniques of Heuristic into two categories:


Heuristic Search Techniques in Artificial Intelligence

a. Direct Heuristic Search Techniques in AI


Other names for these are Blind Search, Uninformed Search, and Blind Control Strategy. These aren’t always
possible since they demand much time or memory. They search the entire state space for a solution and use an
arbitrary ordering of operations. Examples of these are Breadth First Search (BFS) and Depth First Search
(DFS).

Do you know about NLTK Python

b. Weak Heuristic Search Techniques in AI


Other names for these are Informed Search, Heuristic Search, and Heuristic Control Strategy. These are
effective if applied correctly to the right types of tasks and usually demand domain-specific information. We
need this extra information to compute preference among child nodes to explore and expand. Each node has a
heuristic function associated with it. Examples are Best First Search (BFS) and A*.
Before we move on to describe certain techniques, let’s first take a look at the ones we generally observe.
Below, we name a few.

Best-First Search

A* Search

Bidirectional Search

Tabu Search

Beam Search

Simulated Annealing
Hill Climbing

Constraint Satisfaction Problems

4. Hill Climbing in Artifical Intelligence

First, let’s talk about Hill Climbing in Artifical Intelligence. This is a heuristic for optimizing problems
mathematically. We need to choose values from the input to maximize or minimize a real function. It is okay if
the solution isn’t the global optimal maximum.

Heuristic Search Techniques – Hill Climbing

Let’s discuss Python Speech Recognition

One such example of Hill Climbing will be the widely discussed Travelling Salesman Problem- one where we
must minimize the distance he travels.

a. Features of Hill Climbing in AI


Let’s discuss some of the features of this algorithm (Hill Climbing):

It is a variant of the generate-and-test algorithm

It makes use of the greedy approach

This means it keeps generating possible solutions until it finds the expected solution, and moves only in the
direction which optimizes the cost function for it.

b. Types of Hill Climbing in AI


Heuristic Search – Types of Hill Climbing in Artifical Intelligence

Simple Hill Climbing- This examines one neighboring node at a time and selects the first one that
optimizes the current cost to be the next node.

Steepest Ascent Hill Climbing- This examines all neighboring nodes and selects the one closest to the
solution state.

Stochastic Hill Climbing- This selects a neighboring node at random and decides whether to move to it or
examine another.

Let’s revise Python Unit testing

Let’s take a look at the algorithm for simple hill climbing.

Evaluate initial state- if goal state, stop and return success. Else, make initial state current.

Loop until the solution reached or until no new operators left to apply to current state:

a.Select new operator to apply to the current producing new state.

b.Evaluate new state:


If a goal state, stop and return success.

If better than the current state, make it current state, proceed.

Even if not better than the current state, continue until the solution reached.

Exit.

c. Problems with Hill Climbing in AI


We usually run into one of three issues-

Local Maximum- All neighboring states have values worse than the current. The greedy approach means
we won’t be moving to a worse state. This terminates the process even though there may have been a
better solution. As a workaround, we use backtracking.

Plateau- All neighbors to it have the same value. This makes it impossible to choose a direction. To avoid
this, we randomly make a big jump.

Ridge- At a ridge, movement in all possible directions is downward. This makes it look like a peak and
terminates the process. To avoid this, we may use two or more rules before testing.

Do you know about Python Assert Statements

5. Constraint Satisfaction Problems (CSP)

A constraint is nothing but a limitation or a restriction. Working with AI, we may need to satisfy some
constraints to solve problems. Let’s try solving a problem this way, shall we?

Let’s talk of a magic square. This is a sequence of numbers- usually integers- arranged in a square grid. The
numbers in each row, each column, and each diagonal all add up to a constant which we call the Magic
Constant. Let’s implement this with Python.

def magic_square(matrix):

size=len(matrix[0])

sum_list=[]

for col in range(size): #Vertical sum

sum_list.append(sum(row[col] for row in matrix))

sum_list.extend([sum(lines) for lines in matrix])#Horizontal sum

result1=0

for i in range(0,size):

result1+=matrix[i][i]

sum_list.append(result1)

result2=0

for i in range(size-1,-1,-1):

result2+=matrix[i][i]

sum_list.append(result2)
if len(set(sum_list))>1:

return False

return True

Now let’s run this code.

>>> magic_square([[1,2,3],[4,5,6],[7,8,9]])

False
This is not a magic square. The numbers in the rows/columns/diagonals do not add up to the same value. Let’s
try another list of lists.

Have a look at AI Neural Networks

>>> magic_square([[2,7,6],[9,5,1],[4,3,8]])

True

Heuristic Search – Magic

Since the values add up to the constant 15 in all directions, surely, this is a magic square!
6. Simulated Annealing Heuristic Search

In metallurgy, when we slow-cool metals to pull them down to a state of low energy gives them exemplary
amounts of strength. We call this annealing. While high temperatures observe much random movement, low
temperatures notice little randomness.

In AI, we take a cue from this to produce something called simulated annealing. This is a way of optimization
where we begin with a random search at a high temperature and reduce the temperature slowly. Eventually, as
the temperature approaches zero, the search becomes pure greedy descent. At each step, this processes
randomly selects a variable and a value. It accepts the assignment only when it is an improvement or doesn’t
lead to more conflict. If not, it checks if the temperature is much worse than the current assignment to accept the
assignment with some probability.

Do you know about Python ternary Operators

An annealing schedule defines how the temperature drops as the search progress. A very common schedule is
geometric cooling. If we begin with a temperature of 10 and multiply by 0.97 after every step, then after 100
steps, we’re left with a temperature of 0.48.

7. Best-First Search (BFS) Heuristic Search

Often dubbed BFS, Best First Search is an informed search that uses an evaluation function to decide which
adjacent is the most promising before it can continue to explore. Breadth- and Depth- First Searches blindly
explore paths without keeping a cost function in mind. Things aren’t the same with BFS, though. Here, we use a
priority queue to store node costs. Let’s understand BFS Heuristic Search through pseudocode.

Define list OPEN with single node s– the start node.

IF list is empty, return failure.

Remove node n (node with best score) from list, move it to list CLOSED.

Expand node n.

IF any successor to n is the goal node, return success and trace path from goal node to s to return the
solution.

FOR each successor node:

Apply evaluation function f.

IF the node isn’t in either list, add it to list OPEN.

Loop to step 2.

Let’s learn about Python Slice

So, this was all in Heuristic Search Techniques in AI. Hope you like our explanation.
8. Conclusion – Heuristic Search Techniques

Hence, in this Python AI tutorial, we discussed the Heuristic Search in AI. While we named a few techniques
that fall under that, we also discussed, in brief, those like BFS, Hill Climbing, Simulated Annealing, and CSP.
We also implemented CSP in Python. Still, if you have any query in Heuristic Search Techniques, feel free to
ask in the comment tab.
See also –

Python Genetic Algorithms With Artificial Intelligence


by DataFlair Team · November 21, 2018

1. Objective – Python Genetic Algorithms


In our last Python AI tutorial, we discussed AI Python Logic Programming. Today, we will see AI Python
Genetic Algorithms. In this Python Genetic Algorithms tutorial, we will learn the actual meaning of the Genetic
Algorithm. Also, we will look at the benefits, limitations, and applications of Genetic Algorithms with Python.
At last, we will see Python Genetic Algorithm example.
So, let’s start the Python Genetic Algorithms tutorial.

Python Genetic Algorithms With AI

2. What are Genetic Algorithms With Python?


A Genetic Algorithm (GA) is a metaheuristic inspired by natural selection and is a part of the class of
Evolutionary Algorithms (EA). We use these to generate high-quality solutions to optimization and search
problems, for which, these use bio-inspired operators like mutation, crossover, and selection. In other words,
using these, we hope to achieve optimal or near-optimal solutions to difficult problems. Such algorithms
simulate natural selection.
Have a look at Python Machine Learning Algorithms
For any problem, we have a pool of possible solutions. These undergo processes like recombination and
mutation to bear new children over generations. The search space is the set of all possible solutions and values
the inputs may take. In optimization, we try to find within this search space the point or set of points that gives
us the optimal solution. Each individual is like a string of characters/integers/floats and the strings are like
chromosomes.

What are Genetic Algorithms With Python

The fitness value (from a fitness function) for a candidate tells us how close it is to the optimal solution. This is
on the lines of Darwin’s theory of ‘Survival of the Fittest’ and is how we keep producing better (evolving)
individuals/ solutions over generations before reaching a criterion where to stop. These algorithms work in four
steps:

Individuals in population compete for resources, mate

Fittest individuals mate to create more offsprings than others

Fittest parent propagates genes through generation; parents may produce offsprings better than either parent

Each successive generation evolves to suit its ambience

Since the population size is constant, some individuals must die to make room for newer ones. We arrive at a
situation of convergence when the difference between offsprings produced by the current and ancestral
populations is no longer significant. Then, the algorithm converges to a set of solutions for the problem.
Do you know about Python NLTK

3. Operators of Python Genetic Algorithms


To evolve through the generations, an algorithm may make use of one of many operators. Some of those are:

Operators in Python Genetic Algorithms

a. Selection Operator
It prefers individuals with better fitness scores and lets them pass genes on to successive generations.

b. Crossover Operator
This lets individuals mate. We apply the selection operator to select two individuals, and randomly choose
crossover sites. We then exchange the genes at these sites- this produces an entirely new individual.
Crossover Operator in Python Genetic Algorithms

You must know about Python Ternary Operators

c. Mutation Operator
In mutation, we insert random genes in offsprings to maintain diversity and avoid premature convergence.

Mutation Operator in Python Genetic Algorithms

4. Python Genetic Algorithm Example


Let’s try to build a Genetic Algorithm in Python that can play something like Guess the Number better than us
humans. This is a game where I randomly select a number between 1 and 10 (both inclusive) and you guess
what number I have picked.
Is it 7? No
Is it 3? No
Is it 6? No
Is is 2? Yes
Seems like no big deal, but when we start talking about 100 or 1000 numbers, it quickly becomes a problem.
How do we improve our guesses? What can we do but depend on sheer luck? This ultimately turns into a
mechanical process. Maybe we could decide if a certain guess is closer to the solution in a certain direction?
Is it 7? Lower
Is it 1? Higher
Is it 5? Lower
Is it 4? Lower
Is it 3? Yes
Possession of such domain knowledge means we can get to the solution faster. To make informed and improved
guesses, the algorithms make use of random exploration of the problem space. Along with that, they also use
evolutionary processes like mutation and crossover (see above). Without experience in the problem domain,
they also try out things humans never would dare attempt.
Have a look at Python AI Heuristic Search
Okay, now let’s try implementing this in Python. Bear in mind that the fitness value for a candidate is how close
it is to the optimal. Here, it means how many letters match what it should be- “Hello World!”. Let’s start with
building a gene set.

>>> geneSet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."

>>> target="Hello World!"

Now, let’s generate a guess.


>>> import random

>>> def gen_parent(length):

genes=[]

while len(genes)<length:

sampleSize=min(length-len(genes),len(geneSet))

genes.extend(random.sample(geneSet,sampleSize))

return ''.join(genes)

Here, random.sample() chooses sampleSize random elements from geneSet. Now, let’s do something to
calculate the fitness value.

>>> def get_fitness(guess):

return sum(1 for expected,actual in zip(target,guess) if expected==actual)

With zip(), we can iterate over two lists at once. Now, let’s perform mutation.

>>> def mutate(parent):

index=random.randrange(0,len(parent))

childGenes=list(parent)

newGene,alternate=random.sample(geneSet,2)

childGenes[index]=alternate if newGene==childGenes[index] else newGene

return ''.join(childGenes)

This is to convert the parent into an array with list(parent). Then, we replace 1 letter with one that we randomly
select from geneSet. Finally, we recombine the result into a string with .join(). To avoid wasted guesses, this
uses an alternate replacement if the randomly selected newGene is equal to that we expect it to replace. The
next function lets us display what is happening.

>>> def display(guess):

timeDiff=datetime.datetime.now()-startTime

fitness=get_fitness(guess)

print("{}\t{}\t{}".format(guess,fitness,timeDiff))

We’re all set. Let’s begin with the main program now.

>>> random.seed()
>>> startTime=datetime.datetime.now()

>>> bestParent=gen_parent(len(target))

>>> bestFitness=get_fitness(bestParent)

>>> display(bestParent)

umaBL.WdlUYj 1 0:00:51.469944
And now, a loop to generate a guess, request its fitness, compare that to that of the previous best guess, and
keep the guess with the better fitness.

>>> while True:

child=mutate(bestParent)

childFitness=get_fitness(child)

if bestFitness>=childFitness:

continue

display(child)

if childFitness>=len(bestParent):

break

bestFitness=childFitness

bestParent=child

umaBL.WolUYj 2 0:00:55.974065

umaBL.WollYj 3 0:00:56.032069
umaBL.WollY! 4 0:00:56.044069
umlBL.WollY! 5 0:00:56.061070
HmlBL.WollY! 6 0:00:56.078071
HelBL.WollY! 7 0:00:56.086072
HellL.WollY! 8 0:00:56.095072
HellL.WorlY! 9 0:00:56.105073
Hello.WorlY! 10 0:00:56.112073
Hello.World! 11 0:00:56.127074
Did you see how the fitness gradually developed?

5. Benefits of Python Genetic Algorithms


Genetic Algorithms in Python observe the following advantages:

No need for derivative information


Faster and more efficient than traditional methods

Good parallel capabilities

Deliver a list of good solutions instead of just one

Optimize continuous as well as discrete functions and multi-objective problems

Always deliver a solution and improves that with time

Useful for very large search spaces with many parameters involved

Let’s revise the Python AI Speech Recognition

6. Limitations of Python Genetic Algorithms


With all those benefits, we also have certain limitations in Genetic Algorithms with Python-

Not suitable for simple problems with available derivative information

Stochastic; no guarantee of the result solution being optimal

Frequent calculation of fitness value is computationally expensive for some problems

No guarantee of convergence to the optimal solution if not implemented properly

7. Applications of Python Genetic Algorithms


Finally, let’s talk about where we typically use such Genetic Algorithms with Python.

Recurrent Neural Network

Mutation testing

Code breaking

Filtering and signal processing

Learning fuzzy rule base

So, this was all in Python Genetic Algorithms. Hope you like our explanation.

8. Conclusion
Today, we learned about Python Genetic Algorithms and their operators- selection, crossover, and mutation. We
talked about the fitness function and took an example problem to demonstrate such Genetic Algorithms Python.
Finally, we learned of benefits, limitations, and applications of Python Genetic Algorithms. Still, if you have
any doubt, feel free to ask in the comment tab.
AI – Python Computer Vision Tutorial with OpenCV
by DataFlair Team · September 29, 2018

1. Objective – Python Computer Vision

In this Python tutorial, we will talk about Python Computer Vision and OpenCV. Moreover, we’ll see how to
use Python to do basic tasks with OpenCV. Also, we will see detecting edges, drawing with Python OpenCV,
detecting faces, and eye detection.
So, let’s start the Python Computer Vision tutorial.

2. What is Computer Vision in Python?

Computer Vision is a field of multiple disciplines that care about how computers can gain high-level
understanding from digital images/videos. This is an attempt to automate tasks that the human visual system is
able to perform. This is a process of acquiring, processing, analyzing, and understanding digital images, and
extracting high-dimensional data from real world (to produce numerical/symbolic information.)
You must read Python AI Tutorial
Typical tasks involved Python Computer Vision are:

Recognition

Motion Analysis

Scene Reconstruction

Image Restoration

Fields related to Python Computer Vision:


Artificial Intelligence

Solid-state Physics

Neurobiology

Signal Processing

Statistics, Optimization, Geometry

Some of the applications of Python Computer Vision:

Automatic inspection in manufacturing applications

Assisting humans in identification tasks (eg, species identification system)

Controlling processes (eg, an industrial robot)

Detecting events (eg, visual surveillance)

Interaction (eg, input to the device for computer-human interaction)

Modeling objects/ environments (eg, medical image analysis)

Navigation (eg, autonomous vehicle)

Organizing information (eg, indexing databases of images and image sequences)

3. OpenCV Python Computer Vision

Gary Bradsky started OpenCV at Intel in 1999. While it supports a gamut of languages like C++, Python, and
more, and OpenCV-Python is an API for OpenCV to unleash the power of Python and the OpenCV C++ API at
once.
Learn more about Python Library
For Python, this is a library of bindings with the aim to solve computer vision problems. This library uses
NumPy and all its array structures convert to and from NumPy arrays. This also means we can integrate it
easily with other libraries like SciPy and Matplotlib (these make use of NumPy).

a. Install OpenCV Python


Before you can install OpenCV, make sure you have Python and NumPy installed on your machine.
You can download the wheel for OpenCV here (unofficially), so you don’t run into some DLL Hell:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
Then, you can install this file using pip:
pip install [path_of_wheel_file]

b. Importing OpenCV in Python


Get to the IDLE and import OpenCV:

>>> import cv2


You can also check which version you have:

>>> cv2. version

‘3.4.3’

4. Python Computer Vision – Working with Images


Now that we’ve successfully installed OpenCV, let’s get started with it.
Have a look at Image Processing with Python SciPy & NumPy

Note that prior to this, we have moved to the directory that holds this image.
We can also pass a value for a flag, which is the second argument-

cv2.IMREAD_COLOR- To load a color image neglecting existing transparency (default flag)

cv2.IMREAD_GRAYSCALE- To load a grayscale image

cv2.IMREAD_UNCHANGED- To load an image including an alpha channel

We can pass integers 1, 0, or -1.

>>> img=cv2.imread('py.jpg',0)

If you pass an incorrect image path, this gives us no error, but print(img) gives us None.
Let’s revise Python Data Structures

b. Displaying Images in Python


The function/method cv2.imshow() lets us display an image in a window which fits itself to the size of the
image. The first argument is the window name- a string; the second is the image.

>>> img=cv2.imread('py.jpg')

>>> cv2.imshow('Python',img)

How about we display this in grayscale?

Python Computer Vision – Displaying Images in Python

Notice that it let us have two windows at once because we didn’t try to name them the same thing.
Working in scripts, a call to waitKey(0) is beneficial. This is a keyboard-binding function/method with time in
milliseconds. This function waits for certain milliseconds for a keyboard event, within which, if we press any
key, the program continues. When we pass 0, we make it wait indefinitely for a keystroke. We can also make it
wait for specific keys.
Let’s discuss Python Rename File
cv2.destroyAllWindows() is another function/method to destroy all windows we created. cv2.destroyWindow()
destroys a specific window.

c. Writing Images in Python


For this, we have the function/method cv2.imwrite(). The first argument is the file name and the second is the
image to save.

>>> cv2.imwrite('pygray.png',img)

True
This saves the image in grayscale with the name ‘pygray.png’ in the current directory. This image is in the PNG
format.
<matplotlib.image.AxesImage object at 0x0584C630>

>>> plt.xticks([]),plt.yticks([])

(([], <a list of 0 Text xticklabel objects>), ([], <a list of 0 Text yticklabel objects>))

>>> plt.show()
(35, 10, 0)

>>> img[y,x]=(0,0,255) #Setting pixel color to red; BGR scheme

>>> region_of_interest=img[y:y+50,x:x+50] #Region of interest at (x,y) of dimensions 50x50

>>> cv2.imshow('image',img)
True

>>> cv2.imshow('edges',cv2.imread('edges_py.jpg'))

True
Let’s revise Pythpn Regular Expressions

>>> cv2.imshow('edges',cv2.imread('edges_py.jpg'))

>>> import numpy as np

>>>
fd=cv2.CascadeClassifier('C:\\Users\\Ayushi\\Downloads\\opencv\\sources\\data\\haarcascades_cuda\\ha
arcascade_frontalface_default.xml')

>>> img=cv2.imread('mel.jpg')
>>> gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Converting it to grayscale

>>> faces=fd.detectMultiScale(gray,1.3,5) #Performing the detection

>>> for (x,y,w,h) in faces: img=cv2.rectangle(img,(x,y),

(x+w,y+h),(255,0,0),3)

>>> cv2.imwrite('face_mel.jpg',img)

True

True
Here, you can see that it detected three eyes! one of which is her lips. Anyway, this is accurate many times, we
happened to stumble upon one of the pictures that make the exception. Tell us in the comments below if this has
happened to you.
So, this was all in Python Computer Vision Tutorial. Hope you like our explanation.

10. Conclusion

Hence, in this Python Computer Vision tutorial, we discussed the meaning of Computer Vision in Python AI.
Also, we saw drawing with OpenCV, Detecting Edges, and Faces. Moreover, we learned eye detection in
Computer Vision Python. Is this explanation helpful to you? Give your feedback in the comments.

AI – Python Logic Programming With Example


by DataFlair Team · September 29, 2018

1. Objective – Python Logic Programming

Previously with AI, we have learned about Computer Vision, Today, we will see AI with Python Logic
Programming. We will learn how to match mathematical expressions and how to check for and generate prime
numbers. Also, we will see the example of Python Logic Programming.

So, let’s start the Python Logic Programming Tutorial.


AI – Python Logic Programming With Example

2. What is Logic Programming in Python?

Python Logic programming is a programming paradigm that sees computation as automatic reasoning over a
database of knowledge made of facts and rules. It is a way of programming and is based on formal logic. A
program in such a language is a set of sentences, in logical form, one that expresses facts and rules about a
problem domain. Among others, Datalog is one such major logic programming language family.
You must read about NLP in Python AI

a. Structure of Python Logic Programming


Let’s talk about facts and rules. Facts are true statements- say, Bucharest is the capital of Romania. Rules are
constraints that lead us to conclusions about the problem domain. These are logical clauses that express facts.
We use the following syntax to write a rule (as a clause):
H :- B1, …, Bn.
We can read this as:
H if B1 and … and Bn.
Here, H is the head of the rule and B1, …, Bn is the body. A fact is a rule with no body:
H.
An example would be:
Have a look at Python Computational Graphs
fallible(X) :- human(X)
Every logic program needs facts based on which to achieve the given goal. Rules are constraints that get us to
conclusions.

b. Logic and Control


Think of an algorithm as a combination of logic and control.
Algorithm=Logic+Control
In a pure logic programming language, the logic component gets to the solution alone. We can, however, vary
the control component for other ways to execute a logic program.

3. Getting Started With Python Logic Programming

Gearing up for logic programming with Python, we will install a couple of packages. Let’s use pip for this.

Kanren- It lets us express logic as rules and facts and simplifies making code for business logic.

>>> pip install kanren

SymPy- This is a Python library for symbolic mathematics. It is nearly a full-featured Computer Algebra
System.

>>> pip install sympy

Let’s revise the Python Library

4. Python Logic Programming Example

With logic programming, we can compare expressions and find out unknown values. Consider the following
piece of code:

>>> from kanren import run,var,fact

>>> from kanren.assoccomm import eq_assoccomm as eq

>>> from kanren.assoccomm import commutative,associative

>>> add='add' #Defining operations

>>> mul='mul'

>>> fact(commutative,mul) #Addition and multiplication are commutative and associative

>>> fact(commutative,add)

>>> fact(associative,mul)

>>> fact(associative,add)

>>> a,b,c=var('a'),var('b'),var('c') #Defining variables

>>> #2ab+b+3c is the expression we have'

>>> expression=(add, (mul, 2, a, b), b, (mul, 3, c))

>>> expression=(add,(mul,3,-2),(mul,(add,1,(mul,2,3)),-1)) #Expression

>>> expr1=(add,(mul,(add,1,(mul,2,a)),b),(mul,3,c)) #Expressions to match


>>> expr2=(add,(mul,c,3),(mul,b,(add,(mul,2,a),1)))

>>> expr3=(add,(add,(mul,(mul,2,a),b),b),(mul,3,c))

>>> run(0,(a,b,c),eq(expr1,expression)) #Calls to run()

((3, -1, -2),)

>>> run(0,(a,b,c),eq(expr2,expression))

((3, -1, -2),)


Do you know about Python Web Framework

>>> run(0,(a,b,c),eq(expr3,expression))

()
You’ll see that the third expression gives us nothing. It is mathematically the same, but structurally different.

5. Checking for Prime Numbers in Python Logic Programming

If we have a list of numbers, we can find out which ones are prime and also generate such numbers. Let’s see
how.

>>> from kanren import isvar,run,membero

>>> from kanren.core import success,fail,goaleval,condeseq,eq,var

>>> from sympy.ntheory.generate import prime,isprime

>>> import itertools as it

>>> def prime_test(n): #Function to test for prime

if isvar(n):

return condeseq([(eq,n,p)] for p in map(prime,it.count(1)))

else:

return success if isprime(n) else fail

>>> n=var() #Variable to use

>>> set(run(0,n,(membero,n,(12,14,15,19,21,20,22,29,23,30,41,44,62,52,65,85)),(prime_test,n)))

{41, 19, 29, 23}

>>> run(7,n,prime_test(n))
(2, 3, 5, 7, 11, 13, 17)
Let’s take a tour to Python Charts
So, this was all in Python Logic Programming. Hope you like our explanation.

6. Conclusion
Hence, in this Python AI Logic Programming tutorial, we discussed the meaning of logic programming in
Python. Moreover, we saw the example of Python Logic Programming. Also, we discussed the checking for
Prime Numbers. Still, if you have any doubt regarding Python Logic Programming, ask in the comment tab.

Top 10 Best Python Book for Beginners & Experienced(Latest)


by DataFlair Team · December 19, 2018

1. List of Best Python Books

In this Python tutorial, we have collected the information on some best Python book for beginners and
experienced. We have also mentioned the brief introduction of each book of Python. This will help you to select
the best Python books as per your requirement. In addition, it covers books which are good for beginners as
well as experience holders.

To get an in-depth understanding of Python, books are the best way to learn the Python. Python is one of the
famous programming languages. Also, it’s used by major organizations and corporations. To do work in a quick
and efficient manner, learning Python is very important. Yet, it is a cross-platform computer language. It is
generally used by giant corporations.

So, let’s start exploring the best Python Book. Follow all the links for better understanding.

Top 10 Best Python Book for Beginners & Experienced(Latest)

2. List of Some Prominent Python Programming Books

Below, we are discussing some main books for Python, which will help both Python experienced and freshers.
Let’s discuss them in detail:

a. Learning Python
Best Python Programming Book – Learning Python

While it comes to Python programming, this python books covers a lot of basic areas. At a beginner’s level, it is
a very nice book for gaining a better insight into the programming with Python variables and operators in
Python. Also, can be used to derive a strong foundation for the subject matter.
Moreover, it covers important areas of Python such as Python 2.X and 3.X. Hence, it is a very important book.
If you want to gain an in-depth understanding, it is quite a simple book for it. In addition, it is an ideal way to
begin, as a new programmer or a professional developer(in other languages). This is the Best Python Book for
Beginners.

b. Python Cookbook
Best Book to learn Python for Beginners – Python Cookbook

Python Cookbook is a ticket to all the crucial areas of Python language. Also, introduces techniques that has a
huge scope, or which are much needed. It includes a simple string concentration to that of recursive decent
parsers writing.
There are some chapters in the book, on the subject generators and iteration techniques. On several fronts, it
also carries a practical approach. Moreover, this books is Packed with efficient and necessary chapters. Must
read for those, who want to become good at Python programming language.
Do you know what is Python Machine Learning Techniques

c. Python for Data Analysis


Best Book for Python Programming – Python For Data Analysis

While we talk about cleaning, influencing, processing, and gathering of data, this book is a complete guide.
Basically, this book revolves around the area of data-intensive applications. Also, contains an excellent
introduction to data analysis issues. Basically, for learning the various tools, it is turned into the best Python
book.
Have a look at best Python Glossary to understand it well
In addition, there are some exceptional solutions for effectively analyzing the data, in this book. It is very easy
to read and understand. Moreover, this book is very good for professional programmers and researchers. Also,
who are already well-versed with the basics, they can go for this book.

d. Python Essential Reference


Best Book to Learn Python – Python Essential Reference

While it comes to gaining the most out of Python, it is an exceptional reference. It includes a clear-cut
understanding of the Python Introduction. Also, shares what is happening in the programming language.
Moreover, it provides a detailed overview of the Python Programming language with syntax in Python.
Basically, it helps in gaining a practical knowledge of the subject. Although we can say this book is the one that
programmers need to read first. In addition, this book also includes the omitted sections of Python 2.

e. Python Programming: An Introduction to Computer Science


Advanced Python Book – Python Programming

This book is a key to python for beginners and new python programmers. To the world of computer science, it
is a perfect introduction. There are some foundational concepts of computer science in a form that is clear and
easy to understand. This book is all about theory and its practical application. Also provides the strong
foundation on the subject.
Let’s find some interesting concepts of Python Library
Furthermore, it touches essential subjects like Introduction and Structure of Computer programs. This book is
popular for its clarity in explaining the various concepts related to Python programming. In addition, it is a book
which has already gained huge popularity in the market.

f. Python in a Nutshell
Best Data Science Book – Python in a Nutshell

On Python programming language, it acts as a very easy guide. When it comes to areas like official library and
language references, it is a perfect source. Those who already have their fundamentals on Python strong, they
should opt for this book.
Regarding the subject, it deals with many advanced and complicated areas. Moreover, it explains the core of
python language clearly. Also, includes interpretations of various python programmers. Even if someone
thinking to start coding, this book will be the perfect one to go for.

g. Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers


and Security Engineers
Python 3 Book – Violent Python

If we talk about best introduction on Python programming Violent Python is one. It provides clear-cut
understanding about the various areas of the language like tools and concepts. Hence, this book is a great guide
to Python language.
In addition, when it comes to security concepts, it is the best book to read. Also, deals with Python forensics,
tool integration for complicated protocols like SMB. Those programmers who already have a good
understanding of the Python language, this book is apt to use.

h. Learn Python in one day and learn it well


Best python book for experienced programmers – Learn Python in one day and Learn it well

Basically, the best way to learn anything is by doing it. Same is with Python. If you want to learn python well,
start doing python installation. At the end of this book, there is a complete project to practice the applications
of all the concepts taught in the whole book.

Most importantly, working on the project will help you to master the language. Also gives you an immense
sense of achievement. Moreover, this book will also help you to retain the knowledge of a language.

i. Python Pocket Reference


Best python book 2018 – Python Pocket Reference

The book “Python Pocket Reference” includes both Python 3.4 and Python 2.7. Basically, it is the convenient
pocket guide for perfect for job quick reference. In this book, there is concise, need to know information on
Python types and prominent Python tools.
In addition, it includes special methods, built-in functions, and exceptions. Also includes commonly used
Standard library Python modules. Moreover, this book of Python lets you pinpoint exactly what you need.

j. Python Machine Learning


Best python book 2018 – Python Pocket Reference

While it comes to use python for critical questions of data, go for Python Machine Learning. This book is
good for both. Even if you are a beginner or you are a master. It also enhances your Python Knowledge, if you
are already a Python Programmer. Ultimately, it is an unmissable resource for learning Python.

So, these were all the Best Python Book for beginners and Experienced. Hope you like our explanation.

3. Conclusion

As a result, we have seen so many good Python books. Since there is enough description mentioned above for
each book of Python. It will help you to select what is good for you. Hence, choose the best up for your
requirements. Hope this article will definitely calm your search for best books for Python. Keep Reading, Keep
Learning.

7 Reasons Why Should I Learn Python Programming (Latest)


by DataFlair Team · January 12, 2019

1. Why Should I Learn Python

The 21st century; oh, what a time to be alive! With the world at your fingertips, it is easier than ever to dream
big. But the question is- where to begin? With a wide range of programming languages to choose from to begin
with, this article isn’t a gimmick for Python. Through this piece of writing, we hope to open you up to the
realities of the world of Python. We will let you know the reasons why should I learn Python programming,
what are the benefits of learning Python, what can I do with Python programming language and how can I start
a career in Python Programming.

Why should I learn Python

2. Python Programming Langauge

Before you begin to read the advantages of learning Python, it is advisable to read up on An Introduction to
Python. This will give you an insight into what we’re really talking about here.

3. 7 Reasons Why should I learn Python?

Reason.1 Perfect For Rookies


Determined to be simple, Python is actually very beginner-friendly. Its conciseness, expressiveness, and
likeness to English, all contribute to its popularity as a choice for rookies. Its readability simply complements
this. If you still haven’t made your mind, we suggest you take a look at our tutorial on Python Syntax to see
what it’s like to be a Python person. Also, it is interpreted. Due to this, you aren’t stuck in your code until you
reach the problem.

Reason.2 Community
Like we’ve said earlier, Python has a large community which wants to see it grow continuously. Since it is
open-source, people from the community are always at work trying to improve it.
A large community also means availability of a large number of libraries, modules, and packages to help you
with your project. This, in turn, further contributes to the growth of the community.
To quote the statistics, Python has the:

5th Largest StackOverflow Community

If you’ve ever googled up a programming question, you’ve likely stumbled upon this website. It has 85.9k
followers in its Python community, and around over 500k questions. These questions are also third-most likely
among other languages to be answered.

4th Most-Used Language at GitHub

GitHub is an extremely popular platform for version-control repository hosting service. It has over 950 projects
in Python with over 500 stars.

3rd Largest Meetup Community

An online social networking service aiding group meetings, Meetup.com has more than 1300 groups for Python,
with a total of more than 608k members. This makes it its third largest community.

Reason.3 Career Opportunities


Even though it is a very easy language to begin with, Python opens a lot of doors for you. In the professional
world, Python and Ruby developers earn the second highest grub, next to Swift. In India, the average package
of a Python developer with around 4 years of experience is approximately 5 lacs per annum. Python also finds
use in cyber security. This is why Python is worth learning.

Reason.4 Python in Web Development


In our article on Introduction to Python, we discussed several frameworks for Python. Many of these were
meant for web development. These were Django, Pylons, web2py, and Flask. Of these, Django and Flask are
quite impressive.
Giants like Instagram and Pinterest have made use of Django. Likewise, Flask is tiny, but highly customizable.
Certainly, Python is a great scripting language.

Reason.5 Python in Artificial Intelligence and Machine Learning


In the same article, we also talked about frameworks like SciPy and NumPy for these purposes. In a time
without Python, MATLAB would rule the AI & Machine Learning world. But with Python, it is possible to
carry out complex computations using a single import statement followed by a function call. Today, Python is
preferred as the language for computer science research. With its speed and flexibility, it is here to stay.

Reason.6 Raspberry Pi
One of the most interesting parts of learning Python is Raspberry Pi. A microcomputer the size of a credit card,
it is capable of a lot. With it, you can explore the word of technical DIYs (Do It Yourselves). These include
making robots, arcade machines, remote-controlled toys, and even cameras. And with Sonic Pi, you can even
make music.

Reason.7 Startups and Corporates- Python for Both


Python is literally everywhere, be it a startup or a unicorn. From conciseness to capability, its perfect blend of
uniqueness and flexibility make it a suitable choice for any project. It gets the best of both worlds. Giants like
Google, Disney, and NASA are always on the lookout for talented Python professionals. I hope now you will
not wonder why should you learn Python and start learning it.

So, this was all about 7 Reasons Why Should I Learn Python. Hope you like our explanation.

4. Conclusion

We just looked at seven reasons why should I learn Python. To further aid you with your decision, we suggest
you take a look at Python Advantages and Disadvantages. And if you would like to add to the list, we would
love to hear from you in the comments.

59 Python Glossary of Terms You Must Know


by DataFlair Team · September 26, 2018

1. Python Glossary

In this Python Glossary tutorial, we list important terminologies of Python that you will come across as you
proceed to embrace it. Let’s begin.

Python Glossary

2. >>>
This is the default prompt of the Python interactive shell. We have seen this a lot in our examples.
3. …
The default prompt of the Python interactive shell when entering code under an indented block or within a pair
of matching delimiters. Delimiters may be parentheses, curly braces, or square brackets.
This is also called the ellipsis object.
4. 2to3
While most of the applications existing today have their base in Python 2.x, the future belongs to Python 3.x.
But 2.x code isn’t completely compatible with 3.x. Interestingly, we have a tool available that will help us
convert Python 2.x code to Python 3.x.
2to3 handles the incompatibilities, detecting them by parsing the source and traversing the parse tree. The
standard library has this as lib2to3.
5. Abstract Base Class
An abstract base class provides a way to define interfaces. This way, it complements duck typing. For this, we
have the module abc. It introduces virtual subclasses (classes that are recognized by isinstance() and
issubclass(), but do not inherit from another class. Python has several built-in ABCs for data structures (use the
collections.abc module), numbers (use the numbers module), or streams (use the io module). You can also
import finders and loaders (use the importlib.abc module). And to create our own ABCs, we use the abc
module.
6. Python Argument
An argument is a value we pass to a function or a method when calling it. In Python, we have the following
kinds of arguments:
a. Default Arguments
When defining a function, we can provide default values for arguments. This way, when we call it without any
missing arguments, the default values will fill in for them. Default arguments can only follow non-default ones.
>>> def sayhello(name='User'):

print(f"Hello, {name}")

>>> sayhello('Ayushi')

Hello, Ayushi

>>> sayhello()

Hello, User
b. Keyword Arguments Python
Keyword arguments pertain to calling a function. When we then call the function, we can pass it arguments in
any order.

>>> def subtract(a,b):

return b-a

>>> subtract(3,2)

-1

>>> subtract(b=2,a=3)

-1
c. Arbitrary Arguments
When we don’t know how many arguments we’ll get, we use an asterisk to denote an arbitrary argument.

>>> def sum_all(*nums):

total=0

for i in nums:

total+=i

return total

>>> sum_all(1,2,3,4)

10

>>> sum_all(1,2,3)

6
d. Positional Arguments Python
These are regular arguments that aren’t keyword arguments. Python Positional Argument Example.

>>> def add(a,b):


return a+b

>>> add(3,4)

7
We use a * before an iterable if we must pass it as an argument to a function.

>>> add(*(3,4))

7
For more on arguments to functions, read on Python Function Arguments.
7. Asynchronous Context Manager
ACM is an object that controls the environment observed in an async with statement. It does so by defining
aenter () and aexit ().
8. Asynchronous Python Generator
We have seen about Generators in Python. They let us yield one object at a time.
An asynchronous generator is a function that returns an asynchronous generator iterator. We define it with
‘async def’, and it contains ‘yield’ expressions to produce a series of values. We can use these values in an
async for-loop.
Such an asynchronous generator function may contain await expressions, and async for and async with
statements.
9. Asynchronous Generator Iterator
An asynchronous generator function creates an asynchronous generator iterator.
When we call this iterator using the anext () method, it returns an awaitable object. This object executes the
function’s body until the next yield expression.
Actually, each yield suspends processing temporarily. It remembers the location execution state, and the local
variables and pending try statements. On resuming with another awaitable returned by anext (), the
generator iterator picks up where it left off.
10. Asynchronous Iterable
It is an object that we can use in an async for statement. It must return an asynchronous iterator from its
aiter () method.
Any Doubt yet in Python Glossary? Please Comment.
11. Asynchronous Iterator
An asynchronous iterator is an object that implements aiter () and anext () methods. anext () must
return an awaitable object. async for resolves the awaitable returned from the iterator’s anext () method
until it raises a StopAsyncIteration exception.
12. Attribute
An attribute is a value an object holds. We can access an object’s attributes using the dot operator (.). In our
examples, we have done this as following:
orange.color
13. Awaitable
Any object in Python that we can use in an await expression is an awaitable. It can be a coroutine or any object
with an await () method.
14. BDFL
Who other than Guido Van Rossum, the creator of Python, deserves to be called Benevolent Dictator For Life?
15. Binary File
A file object that is able to read and write bytes-like objects is a binary file. When we open a file in a binary
mode, we use the modes ‘rb’, ‘wb’, or ‘rb+’.
More on File I/O.
16. Bytes-like Object
Any object that supports the Buffer Protocol, and is able to export a C-contiguous buffer, is a bytes-like object.
Examples include bytes, bytearray, and array.array objects. It also includes many common memoryview
objects.
We can use such objects for operations that deal with inary data (compression, saving to a binary file, sending
over a socket, and more)
17. Bytecode
As you know, Python compiles its source code into bytecode. It is the internal representation of a Python
program in the CPython interpreter. When we talked earlier of .pyc files, we mentioned that bytecode is cached
into them. This lets the files execute faster the second time since they don’t need to recompile.
In essence, bytecode is like an intermediate language that runs on a virtual machine. This virtual machine
converts it into machine code for the machine to actually execute it on. However, one bytecode will not run on a
different virtual machine.
If you’re interested in finding out about bytecode instructions, you can refer to the official documentation for
the dis module.
18. Python Class
A class, in Python, is a template for creating user-defined objects. It is an abstract data type, and acts as a
blueprint for objects of a kind while having no values itself.
To learn how to create and use a class, refer to Classes in Python.
19. Coercion
When we carry out operations like 2+3.7, the interpreter implicitly converts one data type to another. Here, it
converts 2 to 2.0 (int to float), and then adds, to it, 3.7. This is called coercion, and without it, we would have to
explicitly do it this way:

>>> float(2)+3.7

5.7
20. Complex Number
A complex number is made of real and imaginary parts. In Python, we use ‘j’ to represent the imaginary part.

>>> type(2+3.7j)

<class ‘complex’>
An imaginary number is a real multiple of -1(the imaginary unit). To work with complex equivalents of the
math module, we use cmath. For more on complex numbers, read up on Python Numbers.
These Python Glossary terms are very important to know before you dive into learning Python.
21. Context Manager
The context manager is an object that controls the environment observed in a with-statement. It does so with the
enter () and exit () methods.
22. Coroutine
A subroutine enters at one point and exits at another. A coroutine is more generalized, in that it can enter, exit,
and resume at many different points. We implement them with the async def statement.
23. Coroutine Function
A coroutine function is simply a function that returns a coroutine object. We may define such a function with
the async def statement, and it may contain the keywords await, async for, and async with.
24. CPython
CPython is the canonical implementation of Python in C. It is the one distributed on python.org.
25. Python Decorator
A decorator is a function that returns another function, or wraps it. It adds functionality to it without modifying
it. For a simple, detailed view on decorators, refer to Python Decorators.
26. Descriptor
If an object defines methods get (), set (), or delete (), we can call it a descriptor. On looking up an
attribute from a class, the descriptor attribute’s special binding behavior activates. Using a.b looks up the object
‘b’ in the class dictionary for ‘a’. If ‘b’ is a descriptor, then the respective descriptor methods is called.
27. Python Dictionary
A dictionary is an associative array that holds key-value pairs. Think of a real-life dictionary. Any object with
hash () and eq () methods can be a key.
28. Dictionary View
A dictionary view is an object returned from dict.keys(), dict.values(), or dict.items(). This gives us a dynamic
view on the dictionary’s entries. So, when the dictionary changes, the view reflects those changes.
29. Docstring
A docstring is a string literal that we use to explain the functionality of a class, function, or module. It is the first
statement in any of these constructs, and while the interpreter ignores them, it retains them at runtime. We can
access it using the doc attribute of such an object. You can find out more about docstrings in Python
Comments.
30. Duck-Typing
We keep saying that Python follows duck-typing. But what does this mean? This means that Python does not
look at an object’s type to determine if it has the right interface. It simply calls or uses the method or attribute.
“If it looks and quacks like a duck, it must be a duck.”
This improves flexibility by allowing polymorphic substitution. With duck-typing, you don’t need tests like
type() or isinstance(); instead, you use hasattr() tests or EAFP programming.
31. EAFP Programming
EAFP stands for Easier to Ask for Forgiveness than Permission.
This means that Python assumes the existence of valid keys or attributes, and catches exceptions on falsity of
the assumption. When we have too many try and except statements in our code, we can observe this nature of
Python.
Other languages like C follow LBYL (Look Before You Leap).
32. Python Expression
An expression is a piece of code that we can evaluate to a value. It is an aggregation of expression elements like
literals, names, attribute access, operators, or function calls. All of these return a value. An if-statement is not an
expression, and neither is an assignment, because these do not return a value.
33. Extension Module
An extension module is one written in C or C++, using Python’s C API to interact with the core, and with user
code.
34. f-string
An f-string is a formatted string literal. To write these, we precede a string with the letter ‘f’ or ‘F’. This lets us
put in values into a string.

>>> name,surname='Ayushi','Sharma'

>>> print(f"I am {name}, and I am a {surname}")

I am Ayushi, and I am a Sharma


For more on f-strings, read up on Python Strings.
35. File Object
A file object, in Python, is an object that exposes a file-oriented API to an underlying resource. Such an API has
methods such as read() and write().
We also call them file-like objects or streams, and have three categories:
Raw binary files
Buffered binary files
Text files
The canonical way to create a file object is to use the open() function. For help with reading and writing files,
refer to Reading and Writing Files in Python.
36. File-Like Object
Like we said earlier, it is a synonym for file objects.
37. Finder
The finder is an object that attempts to find the loader for a module that we are importing.
With Python 3.3 and above, we have two types of finders:
Meta path finders- to use with sys.meta_path
Path entry finders- to use with sys.path_hooks
38. Floor Division
Floor division is division that rounds the result down to the nearest integer. For this, we use the // operator.

>>> 18//4

>>> -18//4 #-4.5 is rounded down to -5

-5
These are some of the terminologies from our Python Glossary. We have Python Glossary Part II as well for
more Python Glossaries. Link is Provided at the end of this article.
39. Python Function
A function is a sequence of statements that may return a value to the caller. It may take zero or more arguments.
For more on functions, read up Functions in Python.
40. Function Annotation
An annotation to a function is an arbitrary metadata value associated with a parameter or return value. We can
access a function’s annotations using the annotations attribute. And while Python itself does not assign a
meaning to an annotation, third-party libraries or tools make use of them.
41. future
Interestingly, in Python, we have a pseudo-module available that lets us enable new language features that
aren’t yet compatible with the current interpreter.

>>> import future

>>> future .division

_Feature((2, 2, 0, ‘alpha’, 2), (3, 0, 0, ‘alpha’, 0), 8192)

>>> future .absolute_import

_Feature((2, 5, 0, ‘alpha’, 1), (3, 0, 0, ‘alpha’, 0), 16384)


42. Garbage Collection
Memory must be freed when it isn’t needed anymore. Using reference counting and a cyclic garbage collector
that can detect and break reference cycles, Python collects its garbage. We can use the gc module additionally.
43. Python Generator
A generator is a function that ‘yields’ values one by one. It returns a generator iterator. We can use this
function with a for-loop to retrieve one value at a time.
For more on generators, refer to Generators in Python.
44. Generator Iterator
A generator iterator is an object created by a generator function.
45. Generator Expression
It is an expression that returns an iterator. Below is an example of the same.
>>> sum(i**2 for i in range(7))
91
46. Generic Function
A generic function is made of multiple functions that implement the same operation for different types. The
dispatch algorithm decides which implementation to use during a call.
47. GIL
GIL stands for Global Interpreter Lock. This is the mechanism that the CPython interpreter uses to assure that
only one thread executes Python bytecode at a time. This makes the object model implicitly safe against
concurrent access, and this simplifies CPython.
48. Hashable
If an object has a fixed hash value for its entire lifetime, and is comparable to other objects, it is hashable. Two
equal hashable objects have the same hash values.
While a dictionary itself is unhashable, it cannot hold unhashable types like itself. In fact, all immutable types
are hashable. Mutables like lists are dictionaries are not. User-defined objects are hashable.
49. IDLE
The IDLE is an Integrated DeveLopment Environment for Python. It is a basic editor and interpreter
environment that ships with Python.
50. Immutable
Any object with a fixed value is an immutable. Examples include numbers, strings, and tuples. If you must
change a value, you need to create a new object. In places where we need a constant hash value, like a key in a
dictionary, we use immutables.
51. Import Path
A list of locations searched by the path-based finder to import modules. During an import, this list comes from
sys.path. For subpackages, it may come from the parent package’s path__ attribute.
52. Importing
Importing is the process through which we make the Python code in one module available to another.
53. Importer
The importer is an object that finds and loads a module. Hence, it is both- a finder and a loader object.
54. Interactive
Being of an interpreted nature, Python lets you enter statements/expressions at the integer prompt, and
immediately execute them and see results.
55. Interpreted
We couldn’t highlight this more when we say Python is an interpreted language. However, because it does have
a bytecode compiler, the distinction is a bit blurry. The source files can run without explicitly creating an
executable. While this makes Python faster to develop/debug, it often results in slower execution.
56. Interpreter Shutdown
When we shut down the interpreter, it gradually releases all allocated resources. These include modules and
different critical internal structures. Alongside, it makes several calls to the garbage collector. This may trigger
execution of code in user-defined destructors or in weakref callbacks. Since the resources it relies on may not
function anymore during the shutdown phase, the code executed can encounter various exceptions.
57. Python Iterable
Any object that can return its members one at a time is an iterable. Examples include lists, strings, tuples, dicts,
and file objects.
For more on iterables, read up on Python Iterables.
58. Python Iterator
An iterator is an object that represents a stream of data. We can define an iterator using the iter()
function/method, and get one object at a time with the next() function/method.
For a detailed introduction to iterators, refer to Python Iterators.
59. Key Function
It is a callable that returns a value that we can use for sorting or ordering. We also call it a collation function.
Functions like max(), min(), and sorted() make use of them.
60. Keyword Argument
Refer to section 6 for this.
This is all about the Python Glossary Part I. For more More Python Glossary see Python Glossary Part II.
61. Conclusion: Python Glossary

Here, we discussed 59 common Python Glossary of terms we see in Python. Stay tuned for more, and feel free
to ask a doubt. If you have any query regarding Python Glossary Tutorial, Please Comment. We hope you like
the Python Glossary Tutorial.

50 Python Glossary of Terms to Understand Python – II


by DataFlair Team · September 27, 2018

1. Python Glossary of Terms Part II

Welcome to the Part II of the Python Glossary tutorial. We have already discussed 59 Python Glossary of
terms in last article and today we will discuss some more Python terminologies.

50 Python Glossary of Terms to Understand Python – II

2. Lambda
A lambda is an anonymous inline function made of a single expression. When we call the function, it evaluates
the expression and returns it.
3. LBYL
Look Before You Leap is a coding style that explicitly tests for pre-conditions before making calls or lookups.
If your code has too many if-statements, you know it follows LBYL.
4. List
A list is a sequence of objects. This sequence is mutable, and can be heterogeneous.
5. List Comprehension
A list comprehension is a compact way to create a list.

>>> a=[i**2 for i in range(7)]

>>> a

[0, 1, 4, 9, 16, 25, 36] Refer to List Comprehension in Python.


6. Loader
A loader is an object that loads a module. A loader object must define the method load_module(), and a finder
returns a loader.
7. Mapping
A container object, Mapping supports arbitrary key lookups. It also implements the methods specified in the
MutableMapping or Mapping abstract base classes. Examples- dict, collections.defaultdict,
collections.OrderedDict and collections.Counter.
Refer to defaultdict, OrderedDict, and Counter.
8. Meta Path Finder
The meta path finder is a finder that a search of sys.meta_path returns. It is different from a path entry finder.
9. Metaclass
Meta means beyond. This way, a metaclass is a class of a class. A regular class definition creates a name, a
dictionary for it, and a list of its base classes. The metaclass is the one that takes these three arguments, and
actually creates the class.
While most object-oriented languages implement it by default, Python lets us create our own metaclasses.
Although we don’t always need this, this can provide for elegant solutions in logging attribute access, adding
thread-safety, tracking object creation, implementing singletons, and other tasks.
10. Method
A method is simply a function we define inside a class body. We provide ‘self’ as the first parameter to each
method; it is how it understands that it must work with this object. For more on methods, read up on Python
Methods.
11. Method Resolution Order
MRO is the order in which the interpreter searches base classes when we look a member up.
For a detailed explanation on MRO, refer to Multiple Inheritance in Python.
12. Module
A module is any object that is an organizational unit of Python code. While we can import modules provided by
the Python, we can also create our own. Refer to Python Modules.
13. Module Spec
A namespace that contains the import-related information when we load a module is module spec. It is an
instance of importlib.machinery.ModuleSpec. There is a lot more terms in this Python Glossary of terms
tutorial.
14. MRO
Read section 70 for this.
15. Mutable
Unlike immutables, a mutable can change its value. However, it keeps its id(). Examples include lists and
dictionaries.
16. Namedtuple
A namedtuple lets us access a value using a label instead of an index. We import the namedtuple() function
from the module collections.
For more on this, refer to Namedtuples in Python.
17. Namespace
Implemented as dictionaries, namespaces hold variables. We have local, global, and nested namespaces in
objects. They prevent name collisions and provide modularity. For more on namespaces, you should read
Namespaces in Python.
18. Namespace Package
It is a PEP 420 package that contains subpackages. In fact, that is all it does. It has no physical representation,
not even a init .py file.
19. Nested Scope
In Python, we can refer to a variable in an enclosing definition. An inner function can refer to variables in the
outer function. But when we say refer, we mean it can access that variable to read it, but cannot modify it. Refer
to Namespaces in Python for this.
20. Object
An object is an instance of a class. It has attributes(state) and methods(behavior). It is the ultimate base class of
any class in Python.
For a tutorial on objects, you should read Python Objects.
21. Package
A package is a container for modules and other subpackages. Each package is like a directory, except for the
fact that it has a init .py file. We usually keep that empty.
Read Python Packages.
22. Parameter
When we define a function or a method, we name entities that specify arguments that the function can accept
when we call it.
For a list of types of parameters, see Python Function Arguments.
23. Path Entry
A path entry is a single location on the import path. The path-based finder consults it to find modules to import.
For any doubt or suggestion in Python Glossary of Terms, Please Comment.
24. Path Entry Finder
The path entry finder is a finder that a callable returns on sys.path_hooks. It knows how to locate modules if it
knows a path entry.
25. Path Entry Hook
It is a callable on the sys.path_hook list. If it knows how to find modules on a specific path entry, it returns a
path entry finder.
26. Path Based Finder
This is a default meta path finder, and it searches for an import path for modules.
27. Path-Like Object
A path-like object is an object that represents a system file path. It can either be a str or bytes object that
represents a path, or an object that implements the os.PathLike protocol.
28. Portion
A portion is a set of files, in a single directory, that contribute to a namespace package.
29. Positional Argument
See section 6d in Part I.
30. Provisional API
An API purposely excluded from the standard library’s backwards-compatibility guarantees is a provisional
API.
31. Python 3000
Python 3000 is a nickname for the Python 3.x release line. We also call it Py3k.
32. Pythonic
A common Python idiom is to iterate over a list using a for-loop directly instead of using a counter.
Programmers in another language would do this:

>>> nums=[1,2,3,4,5,6,7]

>>> for i in range(len(nums)):

print(nums[i])

1
2
3
4
5
6
7
A Pythonic approach to this would be:

>>> for num in nums:


print(num)

1
2
3
4
5
6
7
So, any idea or piece of code that follows idioms of Python are Pythonic.
33. Qualified Name
A dotted name that shows us the path from a module’s global scope to a class, method, or function defined in
that module. The attribute qualname will tell us.

>>> class A:

class B:

class C:

def sayhi(self):

pass

>>> A. qualname

‘A’

>>> A.B. qualname

‘A.B’

>>> A.B.C. qualname__

‘A.B.C’

>>> A.B.C.sayhi. qualname

‘A.B.C.sayhi’
34. Reference Count
This is the number of references to an object. A deallocated object’s reference count drops to 0. The function
getrefcount() from the module to find out.

>>> from sys import getrefcount

>>> a=7

>>> getrefcount(a)

36
>>> getrefcount(a)

36

>>> a=8

>>> getrefcount(a)

126

>>> a=9

>>> getrefcount(a)

65
35. Regular Package
A regular package is a traditional package- a directory with a init .py file.
36. slots
slots is a declaration inside a class, that pre-declares space for instance attributes, and eliminates instance
dictionaries. This way, it saves memory.
37. Sequence
A sequence is an iterable which accesses elements efficiently with integer indices. It also has the methods
getitem () and len (). Examples of sequences include lists, strings, tuples, and bytes.
For more on sequences, refer to Sequences in Python.
38. Single Dispatch
It is a kind of a generic function dispatch where a single argument’s type decides the implementation.
39. Slice
A slice is an object that holds a part of a collection. For this, we use the slicing operator [].
40. Special Method
A special method is one that Python calls implicitly to execute an operation on a type, say, addition. Such a
method has two leading and two trailing underscores.
See Operator Overloading in Python.
Few more Python Glossary of terms and you will be aware of every Python terminology used.
41. Statement
A statement is a block of code. Actually, it can be an expression or a keyword.
42. Struct Sequence
A struct sequence is a tuple with named elements. So, they are like namedtuples, but lack methods like _make()
and _asdict().
43. Text Encoding
We use codecs to encode Unicode strings to bytes.
44. Text File
A text file is one that can read and write str objects. To open a file in text mode, we may use ‘r’ or ‘w’ modes.
45. Triple-Quoted String
A triple-quoted string is delimited by three sets of single/double quotes. We can use it to easily work with
single/double quotes without trouble, or to create a docstring.
46. Type
The type function, when we call it on an object, tells us what type of a Python object it is.

>>> type([12,3])

<class ‘list’>
47. Universal Newlines
This is a way to interpret text streams where the following end a line:
‘\n’ (Unix)
‘\r\n’ (Windows)
‘\r’ (Macintosh)
48. Variable Annotation
A variable annotation is a type metadata value. It is associated with a class attribute or a module global variable.
Third-party libraries make use of them.
49. Virtual Environment
A virtual environment is a cooperatively isolated runtime environment. It allows users and applications to
exclusively install and upgrade Python distribution packages.
For more on virtual environments, read Python Virtual Environments.
50. Virtual Machine
A VM is a computer that is entirely defined in software. The virtual machine for Python executes the bytecode
that the bytecode compiler emits.
51. Zen of Python
Try this:

>>> import this

The Zen of Python, by Tim Peters


Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
This lists out the design principles and philosophies of the language.
This is all for Python Glossary of terms.
52. Conclusion: Python Glossary of terms
In today’s tutorial on Python Glossary of terms we learned 50 Python terminologies that will help you when you
learn and work on Python. If you like the Python Glossary tutorial, Please Comment.

Scala vs Python – Difference between Python and Scala


by DataFlair Team · January 16, 2019

1. Scala vs Python
To get the best of your time and efforts, you must choose wisely what tools you use. For this purpose, today, we
compare two major languages, Scala vs Python for data science and other uses to understand which of python
vs Scala for spark is best option for learning.

2. Introduction

But before we begin with the comparison, we must make small talk about Scala.
An object-oriented programming language, Scala was designed by Martin Odersky in 2004. Scala gets its name
as a portmanteau of ‘scalable’ and ‘language’, in that it can scale according to the number of users. In Scala,
everything is an expression. Today, it finds application in data analytics using Apaché Spark. Understand
difference between java vs scala for more learning.
For an introduction to Python, if you haven’t begun your Python journey yet, read up on our Introduction to
Python.
Now that we’re through the introductions, let’s begin comparing scala vs python.

3. Performance

The first factor that we’ll use for comparison is performance. We’ve talked earlier about how being a
dynamically-typed language creates extra work for the interpreter at run time. It has to decide the types of data
at run time. Scala, however, uses the JVM, and is therefore 10 times faster than Python. When there’s a lot to
process, you should consider going with Scala instead.
Winner– Scala

4. Simplicity

We couldn’t be clearer when we say Python is perfect for rookies. Its extremely easy and English-like syntax
contributes to its popularity. Although bundled with a bunch of syntactic sugars, Scala isn’t as easy to master.
However, for concurrent and scalable systems like SoundCloud and Twitter, Python falls short. This is the main
Point in Scala vs Python.
Winner– Python
5. Concurrency

With its list of asynchronous libraries and reactive cores, is a great choice when you want to implement
concurrency. Python, on the other hand, does not support true multithreading. Although, it does support
heavyweight process forking. With it, only one thread is active at a time. So whenever a new code is deployed,
more processes must be restarted, which increases the memory overhead.
Winner– Scala

6. Type Safety

We’ve often said this- Python is a dynamically-typed language. This means you don’t need to declare the data
type in python while declaring it. It follows the duck-typing principle. “If it looks like a duck, swims like a
duck, and quacks like a duck, then it probably is a duck”. While this is easy on the programmers, it slows the
applications down. Contrarily, Scala appears to be dynamically-typed, but is statically-typed. The compiler will
detect errors at compile time.
We see that refactoring Scala code is easier, whereas doing that to Python code may create more bugs than it
solves. So, while Python is a good choice for smaller ad-hoc experiments, Scala fares better for large products.
Winner– It’s a tie.

7. Productivity and Ease of Use

While Scala isn’t as verbose as Java, it definitely isn’t as concise as Python. Python is a clear winner in this case
with its user-friendliness and expressivity.
Winner– Python

8. Advanced Features

While Scala has several existential types, macros, and implicits, its syntax may make it difficult to experiment
with them. Frameworks and libraries, however, allow you to make good use of these features.
Python, on the other hand, has enough data science tools and libraries for Machine Learning and Natural
Language Processing. SparkMLib is one such library for machine learning on big data.
Winner– It’s a tie.
This was all on Scala vs Python.

9. Conclusion

In comparing Python vs Scala, we measured them over a range of factors. These included performance,
simplicity,

Python vs Java – Who Will Conquer 2019?


by DataFlair Team · April 19, 2019

Python Vs Java – The hottest battle of the era. Every beginner want to know which programming language will
have a bright future? According to statistics, Java is losing its charm and Python is rising. But, no one will tell
you which one is beneficial. In this blog, we will discuss the differences between Java and Python and let you
decide which one is more useful.
Python Vs Java – A Battle for the Best

Let’s deep dive into the differences.

1. Hello World Example


To rigorously compare Python and Java, we first compare the first program in any programming language- to
print “Hello World”.

Java

public class HelloWorld

public static void main(String[] args)

System.out.println("Hello World");

Python

Now, let’s try printing the same thing in Python.

print(“Hello World”)
As you can see, what we could do with 7 lines of code in Java, we can do with 1 line in Python.

Let’s further discuss parts of this program and other things.

2. Syntax
A striking characteristic of Python is simple python syntax. Let’s see the syntax difference between Python and
Java.

2.1 Semicolon
Python statements do not need a semicolon to end, thanks to its syntax.

>>> x=7

>>> x=7;

But it is possible to append it. However, if you miss a semicolon in Java, it throws an error.

class one

public static void main (String[] args)

int x=7;

System.out.println(x)

Compilation error #stdin compilation error #stdout 0.09s 27828KB

Main.java:10: error: ‘;’ expected

System.out.println(x)

1 error

2.2 Curly Braces and Indentation


The major factor of Python Vs Java is curly braces and indentation. In Java, you must define blocks using
semicolons. Without that, your code won’t work. But Python has never seen a sight of curly braces. So, to
define blocks, it mandates indentation. Some lazy programmers don’t go the extra mile to indent their code, but
with Python, they have no other choice. This indentation also aids readability. But you must compliment the
indentation with a colon ending the line before it.

>>> if 2>1:

print("Greater")

Greater

This code would break if we added curly braces.

>>> if 2>1:

SyntaxError: expected an indented block

Now, let’s see if we can skip the curly braces and indentation in Java.

class one

public static void main (String[] args)

{ if(2>

1)

System.out.println("2");

Success #stdin #stdout 0.07s 27792KB

Here, we could skip the braces because it’s a single-line if-statement. Indentation isn’t an issue here. This is
because when we have only one statement, we don’t need to define a block. And if we have a block, we define
it using curly braces. Hence, whether we indent the code or not, it makes no difference. Let’s try that with a
block of code for the if-statement.

class one

public static void main (String[] args)

{
if(2<1)

System.out.println("2");

System.out.println("Lesser");

Success #stdin #stdout 0.09s 28020KB

Lesser

As you can see here, 2 isn’t less than 1, so the if statement’s body isn’t executed. Since we don’t have curly
braces here, only the first print statement is considered to be its body. This is why here, only the second
statement is executed; it isn’t in the if’s body.

2.3 Parentheses
Starting Python 3.x, a set of parentheses is a must only for the print statement. All other statements will run with
or without it.

>>> print("Hello")

Hello

>>> print "Hello"

SyntaxError: Missing parentheses in call to ‘print’

This isn’t the same as Java, where you must use parentheses.

2.4 Comments
Comments are lines that are ignored by the interpreter. Java supports multiline comments, but Python does
not. The following are comments in Java.

//This is a single-line comment

/*This is a multiline comment

Yes it is*/

Now, let’s see what a comment looks like in Python.

>>> #This is a comment

Here, documentation comments can be used in the beginning of a function’s body to explain what

it does. These are declared using triple quotes (“””).


>>> """

This is a docstring

"""

'\n\tThis is a docstring\n'

These were the syntax comparison in Python Vs Java, let’s discuss more.

3. Dynamically Typed
One of the major differences is that Python is dynamically-typed. This means that we don’t need to declare the
type of the variable, it is assumed at run-time. This is called Duck Typing. If it looks like a duck, it must be a
duck, mustn’t it?

>>> age=22

You could reassign it to hold a string, and it wouldn’t bother.

>>> age='testing'

In Java, however, you must declare the type of data, and you need to explicitly cast it to a different type when
needed. A type like int can be casted into a float, though, because int has a shallower range.

class one

public static void main (String[] args)

int x=10;

float z;

z=(float)x;

System.out.println(z);

Success #stdin #stdout 0.09s 27788KB

10.0

However then, at runtime, the Python interpreter must find out the types of variables used. Thus, it must work
harder at runtime.
Java, as we see it, is statically-typed. Now if you declare an int an assign a string to it, it throws a type
exception.

class one

public static void main (String[] args)

int x=10;

x="Hello";

Compilation error #stdin compilation error #stdout 0.09s 27920KB

Main.java:12: error: incompatible types: String cannot be converted to int

x="Hello";

1 error

4. Verbosity/ Simplicity
Attributed to its simple syntax, a Python program is typically 3-5 times shorter than its counterpart in Java. As
we have seen earlier, to print “Hello World” to the screen, you need to write a lot of code in Java. We do the
same thing in Python in just one statement. Hence, coding in Python raises programmers’ productivity because
they need to write only so much code needed. It is concise.

To prove this, we’ll try to swap two variables, without using a third, in these two languages. Let’s begin with
Java.

class one

public static void main (String[] args)

int x=10,y=20;

x=x+y;
y=x-y;

x=x-y;

System.out.println(x+" "+y);

Success #stdin #stdout 0.1s 27660KB

20 10

Now, let’s do the same in Python.

>>> a,b=2,3

>>> a,b=b,a

>>> a,b

(3, 2)

As you can see here, we only needed one statement for swapping variables a and b. The statement before it is
for assigning their values and the one after is for printing them out to verify that swapping has been performed.
This is a major factor of Python vs Java.

5. Speed
When it comes to speed, Java is the winner. Since Python is interpreted, we expect them to run slower than their
counterparts in Java. They are also slower because the types are assumed at run time. This is extra work for the
interpreter at runtime. The interpreter follows REPL (Read Evaluate Print Loop). Also, the IDLE has built-in
syntax highlighting, and to get the previous and next commands, we press Alt+p and Alt+n respectively.

However, they also are quicker to develop, thanks to Python’s brevity. Therefore, in situations where speed is
not an issue, you may go with Python, for the benefits it offers more than nullify its speed limitations. However,
in projects where speed is the main component, you should go for Java. An example of such a project is where
you may need to retrieve data from a database. So if you ask Python Vs Java as far as speed is concerned, Java
wins.

6. Portability
Both Python and Java are highly portable languages. But due to the extreme popularity of Java, it wins this
battle. The JVM (Java Virtual Machine) can be found almost everywhere. In the Python Vs Java war of
Portability, Java wins.

7. Database Access
Like we’ve always said, Python’s database access layers are weaker than Java’s JDBC (Java DataBase
Connectivity). This is why it isn’t used in enterprises rarely use it in critical database applications.
8. Interpreted
With tools like IDLE, you can also interpret Python instead of compiling it. While this reduces the program
length and boosts productivity, it also results in slower overall execution.

9. Easy to Use
Now because of its simplicity and shorter code, and because it is dynamically-typed, Python is easy to pick up.
If you’re just stepping into the world of programming, beginning with Python is a good choice. Not only is it
easy to code, but it is also easy to understand. Readability is another advantage. However, this isn’t the same as
Java. Because it is so verbose, it takes some time to really get used to it.

10. Popularity and Community


If we consider the popularity and community factor for Python vs Java, we see that for the past few decades,
Java has been the 2nd most popular language (TIOBE Index). It has been here since 1995 and has been the
‘Language of the Year’ in the years 2005 and 2015. It works on a multitude of devices- even refrigerators and
toasters.

Python, in the last few years, has been in the top 3, and was titled ‘Language of the Year’ in years 2007, 2010,
and 2018. Python has been here since 1991. Can we just say it is the easiest to learn? It is a great fit as an
introductory programming language in schools. Python is equally versatile with applications ranging from data
science and machine learning to web development and developing for Raspberry Pi.

While Java has one large corporate sponsor- Oracle, Python is open-source (CPython) and observes distributed
support.

11. Use Cases


Python: Data Science, Machine Learning, Artificial Intelligence, and Robotics, Websites, Games, Computer
Vision (Facilities like face-detection and color-detection), Web Scraping (Harvesting data from websites), Data
Analysis, Automating web browsers, Scripting, Scientific Computing

Java: Application servers, Web applications, Unit tests, Mobile applications, Desktop applications, Enterprise
applications, Scientific applications, Web and Application Servers, Web Services, Cloud-based applications,
IoT, Big Data Analysis, Games

12. Best for


While Python is best for Data Science, AI, and Machine Learning, Java does best with embedded and cross-
platform applications.

13. Frameworks
Python: Django, web2py, Flask, Bottle, Pyramid, Pylons, Tornado, TurboGears, CherryPy, Twisted

Java: Spring, Hibernate, Struts, JSF (Java Server Faces), GWT (Google Web Toolkit), Play!, Vaadin, Grails,
Wicket, Vert.x

14. Preferability for Machine Learning and Data Science


Python is easier to learn and has simpler syntax than Java. It is better for number crunching, whereas Java is
better for general programming. Both have powerful ML libraries- Python has PyTorch, TensorFlow, scikit-
learn, matplotlib, and Seaborn, and Java has Weka, JavaML, MLlib, and Deeplearning4j

Similarities of Python and Java


Besides differences, there are some similarities between Python and Java:

In both languages, almost everything is an object

Both offer cross-platform support

Strings are immutable in both- Python and Java

Both ship with large, powerful standard libraries

Both are compiled to bytecode that runs on virtual machines

This was all about the difference between Python vs Java Tutorial.

Summary

So, after all, that we’ve discussed here in Python vs Java Tutorial, we come to conclude that both languages
have their own benefits. It really is up to you to choose one for your project. While Python is simple and
concise, Java is fast and more portable. While Python is dynamically-typed, Java is statically-typed. Both are
powerful in their own realms, but we want to know which one you prefer. Furthermore, if you have any
query/question, feel free to share with us!

So now you know which out of java and python is best for your project, install python on Windows if you are
willing to go ahead with Python.

R Vs Python – The most trending debate of aspiring Data Scientists


by DataFlair Team · August 27, 2019

Now, it’s the time for a battle of two most demanding programming languages that is R vs Python. We will go
deep in understanding the differences between the two languages. And, I assure you that you will not have any
confusion left after completing this article i.e. R vs Python – the most trending debate of aspiring data scientists.

R vs Python
To explore everything about R vs Python, first, you must know what exactly R and Python are. Below is the
brief introduction to both the languages. Have a look –

What is R?
R is a programming language that is specifically for statistical computing and graphical computation. R project
is an extension of the S language that was developed at Bell Laboratories by John Chambers and his colleagues.
It is a language and environment for statistical computing and graphics.

Data Analysts, Data Scientists, Statisticians make use of R programming language to analyze the data and carry
out statistical analysis through graphs and other types of visualizations. R allows the users to perform analysis
on large datasets. This language is continuously growing with thousands of packages that can be used readily
for many applications.

There are various statistical and graphical techniques that R provides such as linear and non-linear modeling,
statistical testing, time-series analysis, classification, regression, clustering and many more.

The main strength of R is its well-publicized quality plots that we can produce using its many functionalities
and packages. The user also retains full control over the design choices in their graphics.

To learn more about R programming language check DataFlair’s R Tutorial Series for Free

R Environment
R is a comprehensive suite of various facilities and functionalities that we can use for data manipulation,
calculations as well as graphical display. These facilities comprise of –

It provides efficient data handling as well as a storage facility.

It provides a suite of operators which we can utilize to perform calculations on arrays and in particular
higher-dimensional matrices.
The abundance of graphical facilities and functionalities geared towards data analysis provide various
display exports in different formats.

It provides a large, coherent and integrated collection of several tools that we can utilize for data analysis.

It is a well developed, simple as well as an efficient programming language that involves loops, conditions,
recursive functions as well as several input-output functionalities.

What is Python?
Python is an interpreter based, high level and an object-oriented programming language that consists of
dynamic semantics. Python is built-in high-level data structures that we can combine with dynamic typing as
well as dynamic binding to make it very attractive for Rapid Application Development. We can also avail it for
scripting or an adhesive language to combine several components into one framework.

Python is simple, easy to learn and also provides emphasis on readability that significantly reduces the overall
cost of program maintenance. Python has an easy learning curve and advocates for program modularity as well
as code reusability. The Python interpreter, as well as the extensive standard library, are present in the binary
form that do not charge for major platforms and provides free distribution.

Programmers who are new to programming often fall in love with Python due to its vast functionalities and ease
of learning. Since Python has no compilation step, the provision of the edit-test-debug cycle is significantly fast.
In Python, the process of debugging programs is incredibly easy. Unlike other languages like C and C++, an
inappropriate input will never lead to a segmentation fault. When an interpreter finds an error, it raises an
exception to the user right after we enter the input. If however, the program does catch the exception, the
interpreter provides a stack trace. All of these features added with the massive libraries for a variety of purposes
make Python an efficient programming language. Using a source-level debugger one can easily inspect local as
well as global variables.

Python Tutorial Package – A complete list of 240+ Free Python Tutorials to become a pro

Difference between R and Python

Below we will discuss R vs Python on the basis of definition, responsibilities, career opportunities, advantages,
and disadvantages –

R Vs Python – Definition
R

It was in particular, geared towards addressing the statistical techniques.

R has several graphical libraries like ggplot2 and plotly which make it highly popular owing to quality
reports and images that we can generate.

We can use R for several statistical functions like regression, clustering, classification, statistical testing, etc.
Therefore, R has a variety of supported operations for machine learning and statistical tasks.

While R suffers from a steep learning curve, it proves to be an ideal language for expressing statistical
methodologies, allowing the data scientists to wrangle the data as per their requirements.
R is an open-source programming language meaning that users can actively participate in contributing
towards the growing language as well as avail the many benefits without any license.

Being an interpreter based language, R can run the script immediately and provide output to the user. This
allows ease of code debugging.

Python

It is a multi-purpose programming language that we can utilize for a variety of tasks.

Python has a library for every task. From web application development to penetration testing and data
science, Python has a solution for everything.

It provides an ample number of libraries that we can avail for various machine learning operations as well as
advanced deep learning methodologies.

Python provides a smooth learning curve that makes it highly popular among novices as well as experienced
programmers who want to transition into development using Python as their primary language. Due to
the vast support libraries, it is becoming the go to choice for many operations as the Python users
perceive the language to be the one solution to many problems.

Python is also an open-source programming language that has many open-source projects in which the users
can actively contribute.

We can parse the Python scripts and can also run them immediately. Furthermore, we can save Python as
compiled programs that can be referenced by other programs.

R Vs Python – Responsibilities
Responsibilities of an R Developer are as follows –

An R professional is responsible for providing simulations, carry out data analysis, visualization using a
variety of R packages.

An R developer provides assistance in the design and development of the core architecture that will be used
in the statistical model.

The developer also coordinates with the end-user for the design and development of the solution to the
analytics problems that address the project proposals.

Another key responsibility is to provide data access, manipulate information and report the functions.

Creation of key procedures for integrating R programming with data sources and delivery systems that
export the data file in several formats.

An R developer is responsible for providing assistance to the developers for executing their plans while
keeping in mind the client requirements.

R programmers have to build a code that consists of a qualitative variable as well as manipulation tasks with
several statistical procedures.
Want to become the next Data Scientist? If yes, then you can’t afford to miss these top R
Interview Questions.

Responsibilities of a Python Programmer are as follows –

A Python Developer must be able to implement server-side logic to carry out server-based computations.

Development of asset management software is another key responsibility of a Python programmer.

In order to integrate different software systems, a Python developer must be able to develop a scalable
adhesive code.

Writing reusable and testable code for better operational quality is one of the main functions of a Python
developer.

Data Security and protection of software is also one of the requirements of a software solution. Therefore, a
Python developer must be able to address that.

A Python programmer must be able to implement event-driven programming.

Another important job role is to carry out efficient unit testing and debugging.

R Vs Python – Career Opportunities


Some of the industries that hire R developers are –

Academia

Finance

Banking

Healthcare

Manufacturing

E-commerce

Some of the companies hiring R developers are IBM, Airbnb, Uber, Twitter as well as several public statistical
institutes.

The average salary of an R developer is $67,244.

Discover various career opportunities and scope of R Programming

Some industries that are hiring Python developers are –

Software Development

Manufacturing
Robotics

Embedded Systems

Automation and Testing

Ethical Hacking

Some of the companies hiring Python developers are Google, Amazon, Dropbox, Quora, Reddit, Pinterest and
many more.

The average salary earned by a Python developer is $117,155 per

year. You must check the Future of Python Now!!

R Vs Python – Advantages and Disadvantages

Advantages of R
The main advantage of R is its open-source nature. You can, therefore, work with R without any licence or
payment of fees. Being open-source, you can also contribute towards the customisation of R packages,
newer development as well as resolution of issues.

R provides exemplary support for data wrangling. The packages like dplyr, readr are capable of
transforming messy data into a structured form.

With over 10,000 packages in its CRAN repository, R facilitates libraries that are diverse and every field
that uses data can make use of them.

R has some essential features for graph plotting and aesthetic enhancement of graphs. There are popular
libraries such as ggplot2 and plotly that offer a wide range of graph customization options to the users.

It is a platform independent language that can execute programs on Windows, Linux and Mac.

R is a specific language that is used for statistical modeling. It is the primary tool for creating statistical
tools for data science. This gives R an essential advantage over other programming languages like
Python.

R is constantly evolving. It provides various state of the art features that keep on updating it whenever any
new algorithm is released.

It has an active and engaging community. There are various online forums in R that provide help and
support to the R programmers. Furthermore, there are various bootcamps and online seminars that
provide active education to aspiring R programmers.

Disadvantages of R
The R programming language shares its roots with a much older programming language called S. Because
of this, R lacks most of the features of a modern programming language like support for dynamic or 3D
graphics.
R requires its objects to be stored in physical memory. As compared with other statistical tools, R requires
more memory for its programs. Since R requires the entire data to be loaded into its memory, it is not a
good option when dealing with Big Data.

Since R stems from much older technology, the basic capabilities like security were not native to R. This
restricts R as web applications cannot embed it or its usage as a backend computation language like
Java, Python or Node.js.

R poses a steep learning curve. People who had a background in statistics would find it ideal to use R.
Therefore, for people who are starting afresh in data science may find R a difficult language to adapt to.

Packages in R tend to be slower than other competing languages like Python and MATLAB.

Most of the R algorithms are implemented on different packages. This decentralization of packages makes it
difficult to apply algorithms on problems without prior knowledge of the required package.

Advantages of Python
Just like R, Python is open-source. You can use Python for free. Furthermore, you can change, customise
and contribute towards Python libraries.

Python is a general-purpose programming language that facilitates its usage over diverse tasks. Areas
software development, robotics, embedded systems, automation, etc. make heavy use of python.

Python comprises of state of the art APIs like TensorFlow, pytorch, keras, numpy that are extremely useful
in building artificial neural networks.

It is a user-friendly programming language. This is one of the main reasons as to why Python is the standard
programming language in universities.

Python is secure. These server-side computations involve Python as it provides various frameworks for the
development of web applications.

Python is apt at handling large datasets. It can load data files much faster and can also work with Big Data
ecosystems.

Disadvantages of Python
Being an interpreter based language, Python is slower than other languages like C, C++, and Java.

Python lags behind R when it comes to statistical analysis. Python though may have improved a lot but it
still lacks certain statistical packages as compared to R.

The dynamically typed nature of Python makes it vulnerable to runtime errors.

Python, when compared with JDBC has an underdeveloped database access layer.

Tasks that require heavy memory suffer from Python. The flexible data-types in Python contribute towards
its high memory consumption.

Summary
Here comes the conclusion of R vs Python. We discussed all the major points for understanding the difference
between R and Python. Hope now you are able to pick the best one to start for becoming a data scientist.

Still having a doubt? Comment below. DataFlair is always here to help you. Here is another popular comparison
or R vs Python vs SAS. Do check and share your valuable feedback with us.

Coffee-Python-Popcorn-Netflix! (how Netflix is using Python)


by DataFlair Team · June 12, 2019

The next time you’re streaming on Netflix, while you are thinking what to watch next please do take a minute to
thank popular programming language Python and the developers who use it for much of the experience.

Netflix – This internet entertainment giant is a huge proponent of Python, using the language primarily for data
analysis and back end services. It allows their software engineers to choose what language to code in, and have
noticed a large upsurge in the number of Python applications.

Quoting the Python developers at Netflix, “the language is used through the “full content lifecycle”, from
security tools to its recommendation algorithms, and its proprietary content distribution network (CDN) Open
Connect”
Python at Netflix.

How Python is helping Netflix to grow

Open Connect Network


Actually, it is imprecise but still, thinking about Netflix infrastructure, it is everything that happens before you
press Play on your remote control!

What plan do you have? what have you watched so we can recommend new titles to you? or what do you want
to watch? takes place in Amazon Web Services (AWS), whereas everything that happens afterward i.e., video
streaming takes place in the Open Connect network.
The network devices that are fundamental for a large portion of the CDN are managed by Python applications.
Applications track the inventory of network gear: what devices, of which models, with which hardware
components, located in which sites.

“We are proud to say that our team’s tools are built primarily in Python,” the team writes.

Demand Engineering
Netflix’s demand engineering team brings flexibility into the network by introducing regional content and
balancing the distribution of Netflix’s traffic.

Certain types of tools give Netflix a strong front stand are — Numpy and Scipy for numerical analysis, Boto3
for AWS infrastructure, rq for running asynchronous workloads, Flask APIs are used as a wrapper around the
synchronization tools above.
Netflix uses Python to build custom extensions to the Jupyter server that allows engineers to manage tasks like
logging, archiving, publishing and cloning notebooks.

Core
Meanwhile, the big data synchronization team provides services and tools for scheduling. There are usually
thousands of signals after an alert which wait for their analysis. To aid this Netflix’s CORE team uses many
Python statistical and mathematical libraries that again include Numpy, Scipy, ruptures, and Pandas. On top of
that, Python is also typically used for automation tasks, data exploration and cleaning, and visualization.

Learn about Python Libraries in detail in just 7 mins

Insight Engineering
This was all about the main “Demand engineering”. Except this, there is an Insight Engineering team. It is
responsible for building and operating the tools for operational insight, alerting diagnostics, and auto-
remediation. As the demand for Python is increasing at this pace, the team supports Python clients for most of
their services. The Python frameworks Gunicorn, Flask, Flask-RESTPlus were also used to create Netflix’s
Winston and Bolt diagnostic and therapeutic platforms.

Information Security
To name a few high leverage goals for Netflix: security automation, risk classification, auto-remediation, and
vulnerability identification — all accomplished using Python. One of the most active open-source projects-
security monkey. It is used for monitoring AWS, Google Cloud Platform, OpenStack, and GitHub for applying
changes to assets. Repokid allows the use of Python for helping with IAM (Identity and Access Management)
permission tuning. Whereas Lemur is used to help generate TLS certificates. Netflix also uses the Diffy
forensics triage tool, which is built entirely using Python.

Machine Learning Infrastructure


All the machine learning training models from recommendation algorithms to artwork personalization to
marketing algorithms, Netflix relies extensively on all of these in Python. Many applications are powered by
Metaflow, a Python framework that makes it easy to execute ML projects from the first model to the final
product. CPU cores, millions of computational tasks and handling hundreds of millions of data points in
memory, Netflix depends on Python code for fetching it.
Learn everything about Machine Learning at a single place – Free 100+ Machine Learning tutorials

Notebooks
Many of the components of the balancing service are written in Python. Starting with scheduler, which uses
Jupyter Notebooks with papermill to provide molded job types like Spark. This allows users to have an easy
way to express work that needs to be executed.

Internally, there are event-driven platforms that are fully written in Python. It helps in defining conditions to
filter events, and actions to react or route them. As a result of this, microservices are segregated and visibility is
provided into everything that happens on the data platform.

Partner Ecosystem
The Partner Ecosystem group is expanding its use of Python for testing Netflix applications on devices. Python
is the core of a new CI infrastructure, including balancing servers, controlling Spinnaker, test case querying and
filtering, and scheduling tests run on devices and containers. The additional post-run analysis is being done in
Python using TensorFlow to determine which tests are most likely to show problems on which devices.

Netflix Animation and NVFX


Python is the industry standard for all of the major applications used to create Animated and VFX content. All
of the integrations with Maya and Nuke are in Python, and the bulk of Shotgun tools are also in Python. Netflix
is also getting hands-on tooling in the cloud, and anticipate deploying many of their custom Python AMIs.

The reason that the services are written in Python is due to the extremely active development community, and
the rich variety of third-party libraries available to solve nearly any given problem.

Not only Netflix but many major companies are also using Python. What are you waiting for? When will you
start using it? Start learning Python by Yourself NOW!!

Conclusion

Netflix, being a $12 billion earmarked entertainment company with over 148 million paid memberships in over
190 countries which enjoy TV series, documentaries and feature films across a wide variety of genres and
languages. So you can watch as much as you want, anytime, anywhere, on any Internet-connected screen. You
can play, pause and resume watching, all without any commercial or any commitment!

Hope you liked the article. Share your feedback through comments.

Coffee-Python-Popcorn-Netflix! (how Netflix is using Python)


by DataFlair Team · June 12, 2019

The next time you’re streaming on Netflix, while you are thinking what to watch next please do take a minute to
thank popular programming language Python and the developers who use it for much of the experience.

Netflix – This internet entertainment giant is a huge proponent of Python, using the language primarily for data
analysis and back end services. It allows their software engineers to choose what language to code in, and have
noticed a large upsurge in the number of Python applications.
Quoting the Python developers at Netflix, “the language is used through the “full content lifecycle”, from
security tools to its recommendation algorithms, and its proprietary content distribution network (CDN) Open
Connect”
Python at Netflix.

How Python is helping Netflix to grow

Open Connect Network


Actually, it is imprecise but still, thinking about Netflix infrastructure, it is everything that happens before you
press Play on your remote control!

What plan do you have? what have you watched so we can recommend new titles to you? or what do you want
to watch? takes place in Amazon Web Services (AWS), whereas everything that happens afterward i.e., video
streaming takes place in the Open Connect network.

The network devices that are fundamental for a large portion of the CDN are managed by Python applications.
Applications track the inventory of network gear: what devices, of which models, with which hardware
components, located in which sites.

“We are proud to say that our team’s tools are built primarily in Python,” the team writes.

Demand Engineering
Netflix’s demand engineering team brings flexibility into the network by introducing regional content and
balancing the distribution of Netflix’s traffic.

Certain types of tools give Netflix a strong front stand are — Numpy and Scipy for numerical analysis, Boto3
for AWS infrastructure, rq for running asynchronous workloads, Flask APIs are used as a wrapper around the
synchronization tools above.
Netflix uses Python to build custom extensions to the Jupyter server that allows engineers to manage tasks like
logging, archiving, publishing and cloning notebooks.
Core
Meanwhile, the big data synchronization team provides services and tools for scheduling. There are usually
thousands of signals after an alert which wait for their analysis. To aid this Netflix’s CORE team uses many
Python statistical and mathematical libraries that again include Numpy, Scipy, ruptures, and Pandas. On top of
that, Python is also typically used for automation tasks, data exploration and cleaning, and visualization.

Learn about Python Libraries in detail in just 7 mins

Insight Engineering
This was all about the main “Demand engineering”. Except this, there is an Insight Engineering team. It is
responsible for building and operating the tools for operational insight, alerting diagnostics, and auto-
remediation. As the demand for Python is increasing at this pace, the team supports Python clients for most of
their services. The Python frameworks Gunicorn, Flask, Flask-RESTPlus were also used to create Netflix’s
Winston and Bolt diagnostic and therapeutic platforms.

Information Security
To name a few high leverage goals for Netflix: security automation, risk classification, auto-remediation, and
vulnerability identification — all accomplished using Python. One of the most active open-source projects-
security monkey. It is used for monitoring AWS, Google Cloud Platform, OpenStack, and GitHub for applying
changes to assets. Repokid allows the use of Python for helping with IAM (Identity and Access Management)
permission tuning. Whereas Lemur is used to help generate TLS certificates. Netflix also uses the Diffy
forensics triage tool, which is built entirely using Python.

Machine Learning Infrastructure


All the machine learning training models from recommendation algorithms to artwork personalization to
marketing algorithms, Netflix relies extensively on all of these in Python. Many applications are powered by
Metaflow, a Python framework that makes it easy to execute ML projects from the first model to the final
product. CPU cores, millions of computational tasks and handling hundreds of millions of data points in
memory, Netflix depends on Python code for fetching it.

Learn everything about Machine Learning at a single place – Free 100+ Machine Learning tutorials

Notebooks
Many of the components of the balancing service are written in Python. Starting with scheduler, which uses
Jupyter Notebooks with papermill to provide molded job types like Spark. This allows users to have an easy
way to express work that needs to be executed.

Internally, there are event-driven platforms that are fully written in Python. It helps in defining conditions to
filter events, and actions to react or route them. As a result of this, microservices are segregated and visibility is
provided into everything that happens on the data platform.

Partner Ecosystem
The Partner Ecosystem group is expanding its use of Python for testing Netflix applications on devices. Python
is the core of a new CI infrastructure, including balancing servers, controlling Spinnaker, test case querying and
filtering, and scheduling tests run on devices and containers. The additional post-run analysis is being done in
Python using TensorFlow to determine which tests are most likely to show problems on which devices.

Netflix Animation and NVFX


Python is the industry standard for all of the major applications used to create Animated and VFX content. All
of the integrations with Maya and Nuke are in Python, and the bulk of Shotgun tools are also in Python. Netflix
is also getting hands-on tooling in the cloud, and anticipate deploying many of their custom Python AMIs.

The reason that the services are written in Python is due to the extremely active development community, and
the rich variety of third-party libraries available to solve nearly any given problem.

Not only Netflix but many major companies are also using Python. What are you waiting for? When will you
start using it? Start learning Python by Yourself NOW!!

Conclusion

Netflix, being a $12 billion earmarked entertainment company with over 148 million paid memberships in over
190 countries which enjoy TV series, documentaries and feature films across a wide variety of genres and
languages. So you can watch as much as you want, anytime, anywhere, on any Internet-connected screen. You
can play, pause and resume watching, all without any commercial or any commitment!

Hope you liked the article. Share your feedback through comments.

Python Infographic – A Complete Learning Path for Beginners


by DataFlair Team · March 5, 2019

How to Start Learning Python?

Do you often wonder about how to start learning Python? What are its advantages and disadvantages? What is
the career scope in Python? The best books to learn Python? Here, we will answer all your queries with this
Python Infographic.

In use widely as an introductory programming language, Python is powerful. From simple syntax and
conciseness to extensive libraries and substantial frameworks, Python has it all. This also makes it suitable for
domains like machine learning and data science. Being open-source and popular, it observes a large community
always looking to bettering it.
Python Infographic
Why Should You Learn Python?

There are a number of reasons which can influence you to learn Python.

Python supports multiple programming paradigms

Vast community and better support

Python has a huge set of libraries and tools

Specially designed for better code readability

Python has the future in Big data, Artificial Intelligence, networking, etc.

Where can Python be used?

Python is an object-oriented programming language used in many sectors like:

Desktop applications

Website programming development

GUI application development

Software testing

Artificial Intelligence

Machine Learning

Data Science

Deep learning

It can be used as a scripting language for blender, open office, SIMP, etc

Hope this answered all your questions. Python is an emerging programming language in the IT Industry; its
popularity and demand are increasing day by day. You can check the Latest Python Career Opportunities and
Job trends in this Python Tutorial.

Python is also great for data science and machine learning. Here, we can say that 99.4% of websites using
Python use Python 2; only 0.6% of them use Python 3. It is also interesting to note that the support for Python
2.x ends on January 1, 2020.

So, what are you waiting for? Start learning Python today and enter a new world of Data Science! We have a
series of Python tutorials, start your learnings from here – Python Tutorials Home
Python Features Infographic – What Makes Python Most Craved
Language?
by dfadteam1 · April 4, 2019

A language is by what it can do for you, and by what you can do with it. No two languages are the same; in this
blog – Python Features Infographic, you will see what makes Python any special.

Since its first appearance in 1990, Python has made quite a name for itself with its simplicity and power. Not
only is it easy to read and code in, but it is also often the preference of many professionals when it comes to
domains like Data Science and Machine Learning. This makes it a good fit for an introductory programming
language in schools.

Why is it called Python?


The most intriguing fact about Python is its name. The name of this language was influenced by the British
comedy series “Monty Python’s Flying Circus”. The series was aired on BBC during the 1970s and Guido Van
Rossum (the creator of Python) wanted the name of the language to be short and mysterious, one that would
capture everyone’s attention.

Python Features Infographic

Python offers many features; this escalates its demand in the IT industry. A large number of programmers and
developers across the world express their interest in it. Thanks to its English-like syntax, it is easy to read and
understand. It is also easy to code in; this boosts productivity as it lets the developer focus on what to do rather
than on how to do it. It has a multitude of powerful libraries like scikit-learn and NumPy. With a very large
community at its heart.

See The Latest Career Options in Python Programming Language

What is it that makes Python so powerful and popular? As they say, the world’s best camera is no more than a
toy in the hands that have no idea what to do with them. To truly harvest the power of something, you must
introduce yourself to the tools it gives you. To aid with the same, we have put together the following
infographic, hoping to deliver quick insights into what you’re in for.

And that’s all about Python Features Infographic. Did we forget to mention something? Will you be picking
Python for your next project? Tell us in the comments below.

Python Career Infographic – Latest Jobs and Salaries


by DataFlair Team · March 5, 2019

Are you looking to learn Python? If yes, then you must be eager to find out about Python careers, jobs, and
salary. So, here, we provide you with this Python career infographic, in which you will find each and every
detail you need to know. Moreover, you will find accurate statistics and interesting facts about Python to push
you further.
Career Opportunities in Python are growing very rapidly across the world. If you become an expert in Python,
you can find multiple career options. In this Python career infographic, you will find all the career options you
can choose from.

Python Career Infographic

Python is the easiest programming language for any newbie who wants to enter the IT world. It is used in web
development, scripting, application development, and many others, and is preferred in domains like Data
Science, Machine Learning, and Artificial Intelligence. Just because of these features many people dream their
career in Python technology. Hope that with the above Python Career Infographic, you understand that Python
has tremendous job opportunities with good salary packages.

In terms of career choice, Python offers diversity. You can start as a Python developer or programmer, and then
switch to data science. So, learn Python, practice hard by solving problems, and become an expert. You can also
refer to our Python series which covers the list of topics that you should learn if you must master Python.
Wishing you a bright career ahead.

Wish to crack your upcoming Python interview? Prepare with us – Most asked Python Interview Questions

If you still have doubts and want to ask anything about Python careers, comment below; we will definitely do
our best to help you. And don’t forget to give feedback for the Python career infographic above.
Intermediate Python Project – Driver Drowsiness Detection System
with OpenCV & Keras
by DataFlair Team · October 1, 2019

Now, we will be working on an intermediate-level Python project. The project we will be making is the
drowsiness detecting device. A countless number of people drive on the highway day and night. Taxi drivers,
bus drivers, truck drivers and people traveling long-distance suffer from lack of sleep. Due to which it becomes
very dangerous to drive when feeling sleepy. Majority of the accidents happen due to the drowsiness of the
driver. So, to prevent these accidents we will build a system using Python, OpenCV, and Keras which will alert
the driver when he feels sleepy.

You can also check out other Python project ideas with source code by DataFlair from this list:

Driver Drowsy Safety Alert System Python

Project Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age Gender Detection Python Project

Breast Cancer Classification Python Project

Intermediate Python Project on Drowsy Driver Alert System


Drowsiness detection is a safety technology that can prevent accidents that are caused by drivers who fell asleep
while driving.

Driver Drowsiness Detection System Python Project – Objective


To build a drowsiness detection system that will detect drowsiness if a person’s eyes are closed for a few
seconds. This system will alert the driver when drowsiness is detected.

Driver Drowsiness Detection System – About the Intermediate Python Project


In this Python project, we will be using OpenCV for gathering the images from webcam and feed them into a
Deep Learning model which will classify whether the person’s eyes are ‘Open’ or ‘Closed’. The approach we
will be using for this Python project is as follows :

Step 1 – Take image as input from a camera.

Step 2 – Detect the face in the image and create a Region of Interest (ROI).

Step 3 – Detect the eyes from ROI and feed it to the classifier.
Step 4 – Classifier will categorize whether eyes are open or closed.

Step 5 – Calculate score to check whether the person is drowsy.

The Dataset
The dataset used for this model is created by us. To create the dataset, we wrote a script that captures eyes from
a camera and stores in our local disk. We separated them into their respective labels ‘Open’ or ‘Closed’. The
data was manually cleaned by removing the unwanted images which were not necessary for building the model.
The data comprises around 7000 images of people’s eyes under different lighting conditions. After training the
model on our dataset, we have attached the final weights and model architecture file “models/cnnCat2.h5”.

Now, you can use this model to classify if a person’s eye is open or closed.

The Model Architecture


The model we used is built with Keras using Convolutional Neural Networks (CNN). A convolutional neural
network is a special type of deep neural network which performs extremely well for image classification
purposes. A CNN basically consists of an input layer, an output layer and a hidden layer which can have
multiple numbers of layers. A convolution operation is performed on these layers using a filter that performs 2D
matrix multiplication on the layer and filter.

The CNN model architecture consists of the following layers:

Convolutional layer; 32 nodes, kernel size 3

Convolutional layer; 32 nodes, kernel size 3

Convolutional layer; 64 nodes, kernel size 3

Fully connected layer; 128 nodes

The final layer is also a fully connected layer with 2 nodes. In all the layers, a Relu activation function is used
except the output layer in which we used Softmax.

Prerequisites
The requirement for this Python project is a webcam through which we will capture images. You need to have
Python (3.6 version recommended) installed on your system, then using pip, you can install the necessary
packages.

OpenCV – pip install opencv-python (face and eye detection).

TensorFlow – pip install tensorflow (keras uses TensorFlow as backend).

Keras – pip install keras (to build our classification model).

Pygame – pip install pygame (to play alarm sound).

Want to master the Python Programming skills?


Get enrolled with Certified Python Training Course

Python Project on Steps for Performing Driver Drowsiness Detection


Download the Python project source code from the zip and extract the files in your system:

Python Project Zip File

The contents of the zip are:

The “haar cascade files” folder consists of the xml files that are needed to detect objects from the image. In
our case, we are detecting the face and eyes of the person.

The models folder contains our model file “cnnCat2.h5” which was trained on convolutional neural
networks.

We have an audio clip “alarm.wav” which is played when the person is feeling drowsy.

“Model.py” file contains the program through which we built our classification model by training on our
dataset. You could see the implementation of convolutional neural network in this file.

“Drowsiness detection.py” is the main file of our project. To start the detection procedure, we have to run
this file.

Let’s now understand how our algorithm works step by step.

Step 1 – Take Image as Input from a Camera

With a webcam, we will take images as input. So to access the webcam, we made an infinite loop that will
capture each frame. We use the method provided by OpenCV, cv2.VideoCapture(0) to access the camera and
set the capture object (cap). cap.read() will read each frame and we store the image in a frame variable.

Step 2 – Detect Face in the Image and Create a Region of Interest (ROI)

To detect the face in the image, we need to first convert the image into grayscale as the OpenCV algorithm for
object detection takes gray images in the input. We don’t need color information to detect the objects. We will
be using haar cascade classifier to detect faces. This line is used to set our classifier face =
cv2.CascadeClassifier(‘ path to our haar cascade xml file’). Then we perform the detection using faces =
face.detectMultiScale(gray). It returns an array of detections with x,y coordinates, and height, the width of the
boundary box of the object. Now we can iterate over the faces and draw boundary boxes for each face.

for (x,y,w,h) in faces:

cv2.rectangle(frame, (x,y), (x+w, y+h), (100,100,100), 1 )

Step 3 – Detect the eyes from ROI and feed it to the classifier

The same procedure to detect faces is used to detect eyes. First, we set the cascade classifier for eyes in leye and
reye respectively then detect the eyes using left_eye = leye.detectMultiScale(gray). Now we need to extract
only the eyes data from the full image. This can be achieved by extracting the boundary box of the eye and then
we can pull out the eye image from the frame with this code.

l_eye = frame[ y : y+h, x : x+w ]

l_eye only contains the image data of the eye. This will be fed into our CNN classifier which will predict if eyes
are open or closed. Similarly, we will be extracting the right eye into r_eye.

Step 4 – Classifier will Categorize whether Eyes are Open or Closed

We are using CNN classifier for predicting the eye status. To feed our image into the model, we need to
perform certain operations because the model needs the correct dimensions to start with. First, we convert the
color image into grayscale using r_eye = cv2.cvtColor(r_eye, cv2.COLOR_BGR2GRAY). Then, we resize
the image to 24*24 pixels as our model was trained on 24*24 pixel images cv2.resize(r_eye, (24,24)). We
normalize our data for better convergence r_eye = r_eye/255 (All values will be between 0-1). Expand the
dimensions to feed into our classifier. We loaded our model using model = load_model(‘models/cnnCat2.h5’)
. Now we predict each eye with our model
lpred = model.predict_classes(l_eye). If the value of lpred[0] = 1, it states that eyes are open, if value of
lpred[0] = 0 then, it states that eyes are closed.

Step 5 – Calculate Score to Check whether Person is Drowsy

The score is basically a value we will use to determine how long the person has closed his eyes. So if both eyes
are closed, we will keep on increasing score and when eyes are open, we decrease the score. We are drawing the
result on the screen using cv2.putText() function which will display real time status of the person.

cv2.putText(frame, “Open”, (10, height-20), font, 1, (255,255,255), 1, cv2.LINE_AA )

A threshold is defined for example if score becomes greater than 15 that means the person’s eyes are closed for
a long period of time. This is when we beep the alarm using sound.play()

The Source Code of our main file looks like this:

import cv2

import os

from keras.models import load_model


import numpy as np

from pygame import mixer

import time

mixer.init()

sound = mixer.Sound('alarm.wav')

face = cv2.CascadeClassifier('haar cascade files\haarcascade_frontalface_alt.xml')

leye = cv2.CascadeClassifier('haar cascade files\haarcascade_lefteye_2splits.xml')

reye = cv2.CascadeClassifier('haar cascade files\haarcascade_righteye_2splits.xml')

lbl=['Close','Open']

model = load_model('models/cnncat2.h5')

path = os.getcwd()

cap = cv2.VideoCapture(0)

font = cv2.FONT_HERSHEY_COMPLEX_SMALL

count=0

score=0

thicc=2

rpred=[99]

lpred=[99]

while(True):

ret, frame = cap.read()

height,width = frame.shape[:2]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25))

left_eye = leye.detectMultiScale(gray)

right_eye = reye.detectMultiScale(gray)

cv2.rectangle(frame, (0,height-50) , (200,height) , (0,0,0) , thickness=cv2.FILLED )

for (x,y,w,h) in faces:

cv2.rectangle(frame, (x,y) , (x+w,y+h) , (100,100,100) , 1 )

for (x,y,w,h) in right_eye:

r_eye=frame[y:y+h,x:x+w]

count=count+1

r_eye = cv2.cvtColor(r_eye,cv2.COLOR_BGR2GRAY)

r_eye = cv2.resize(r_eye,(24,24))

r_eye= r_eye/255

r_eye= r_eye.reshape(24,24,-1)

r_eye = np.expand_dims(r_eye,axis=0)

rpred = model.predict_classes(r_eye)

if(rpred[0]==1):

lbl='Open'

if(rpred[0]==0):

lbl='Closed'

break
for (x,y,w,h) in left_eye:

l_eye=frame[y:y+h,x:x+w]

count=count+1

l_eye = cv2.cvtColor(l_eye,cv2.COLOR_BGR2GRAY)

l_eye = cv2.resize(l_eye,(24,24))

l_eye= l_eye/255

l_eye=l_eye.reshape(24,24,-1)

l_eye = np.expand_dims(l_eye,axis=0)

lpred = model.predict_classes(l_eye)

if(lpred[0]==1):

lbl='Open'

if(lpred[0]==0):

lbl='Closed'

break

if(rpred[0]==0 and lpred[0]==0):

score=score+1

cv2.putText(frame,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)

# if(rpred[0]==1 or lpred[0]==1):

else:

score=score-1

cv2.putText(frame,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)

if(score<0):

score=0
cv2.putText(frame,'Score:'+str(score),(100,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)

if(score>15):

#person is feeling sleepy so we beep the alarm

cv2.imwrite(os.path.join(path,'image.jpg'),frame)

try:

sound.play()

except: # isplaying = False

pass

if(thicc<16):

thicc= thicc+2

else:

thicc=thicc-2

if(thicc<2):

thicc=2 cv2.rectangle(frame,(0,0),(width,height),

(0,0,255),thicc) cv2.imshow('frame',frame)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

cv2.destroyAllWindows()

Python Project Example


Let’s start our project and see the working of our project. To start the project, you need to open a command
prompt, go to the directory where our main file “drowsiness detection.py” exists. Run the script with this
command.

python “drowsiness detection.py”

It may take a few seconds to open the webcam and start detection.
Example Screenshot:

Output Screenshot:

Closed Eye Detection


Open Eyes Detection
Sleep Alert

Summary

In this Python project, we have built a drowsy driver alert system that you can implement in numerous
ways. We used OpenCV to detect faces and eyes using a haar cascade classifier and then we used a CNN
model to predict the status. You can try the results of this project using your webcam.

Preparing for your next Python Interview?

Cracking Python Interview made easy with Top Python Interview Questions

If you feel any confusion regarding Python Project on Driver Drowsiness Detection System, share your queries
in the comments.

For more interesting Python projects, keep visiting DataFlair and keep learning.

Advanced Python Project – Detecting Fake News with Python


by DataFlair Team · September 20, 2019

Advanced Python Project – Detecting Fake News


Do you trust all the news you hear from social media? All news are not real, right? So how will you detect the
fake news? The answer is Python. By practicing this advanced python project of detecting fake news, you will
easily make a difference between real and fake news. Before moving ahead in this advanced Python project, get
aware of the terms related to it like fake news, tfidfvectorizer, PassiveAggressive Classifier.

Also, I like to add that DataFlair has published a series of Python Projects where you will get interesting and
open-source advanced python projects. Do check, and then share your experience through comments. Here is
the list of top 5 Python projects –

Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age and Gender Detection Python Project

Breast Cancer Classification Python Project

What is Fake News?


A type of yellow journalism, fake news encapsulates pieces of news that may be hoaxes and is generally spread
through social media and other online media. This is often done to further or impose certain ideas and is often
achieved with political agendas. Such news items may contain false and/or exaggerated claims, and may end up
being viralized by algorithms, and users may end up in a filter bubble.

What is a TfidfVectorizer?
TF (Term Frequency): The number of times a word appears in a document is its Term Frequency. A higher
value means a term appears more often than others, and so, the document is a good match when the term is part
of the search terms.

IDF (Inverse Document Frequency): Words that occur many times a document, but also occur many times in
many others, may be irrelevant. IDF is a measure of how significant a term is in the entire corpus.

The TfidfVectorizer converts a collection of raw documents into a matrix of TF-IDF features.

What is a PassiveAggressiveClassifier?
Passive Aggressive algorithms are online learning algorithms. Such an algorithm remains passive for a correct
classification outcome, and turns aggressive in the event of a miscalculation, updating and adjusting. Unlike
most other algorithms, it does not converge. Its purpose is to make updates that correct the loss, causing very
little change in the norm of the weight vector.

Missed Important Python Topics???

Check the latest Python Tutorials List with 240+ Tutorials

Detecting Fake News with Python – Objective


To build a model to accurately classify a piece of news as REAL or FAKE.

Detecting Fake News with Python – About the Python Project


This advanced python project of detecting fake news deals with fake and real news. Using sklearn, we build a
TfidfVectorizer on our dataset. Then, we initialize a PassiveAggressive Classifier and fit the model. In the end,
the accuracy score and the confusion matrix tell us how well our model fares.

The Dataset
The dataset we’ll use for this python project- we’ll call it news.csv. This dataset has a shape of 7796×4. The
first column identifies the news, the second and third are the title and text, and the fourth column has labels
denoting whether the news is REAL or FAKE. The dataset takes up 29.2MB of space and you can download
it here.

Advanced Python Project – Prerequisites


You’ll need to install the following libraries with pip:

pip install numpy pandas sklearn

You’ll need to install Jupyter Lab to run your code. Get to your command prompt and run the following
command:

C:\Users\DataFlair>jupyter lab

You’ll see a new browser window open up; create a new console and use it to run your code. To run multiple
lines of code at once, press Shift+Enter.

Steps for detecting fake news with Python


Follow the below steps for detecting fake news and complete your first advanced Python Project –

Make necessary imports:

import numpy as np

import pandas as pd

import itertools

from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import PassiveAggressiveClassifier

from sklearn.metrics import accuracy_score, confusion_matrix


Screenshot:

2. Now, let’s read the data into a DataFrame, and get the shape of the data and the first 5 records.

#Read the data

df=pd.read_csv('D:\\DataFlair\\news.csv')

#Get shape and head

df.shape

df.head()

Output Screenshot:
CRACK INTERVIEW IN THE FIRST ATTEMPT!!!

Check frequently asked Python Interview Questions with Answers

3. And get the labels from the

DataFrame. #DataFlair - Get the

labels labels=df.label

labels.head()

Output Screenshot:

4. Split the dataset into training and testing

sets. #DataFlair - Split the dataset

x_train,x_test,y_train,y_test=train_test_split(df['text'], labels, test_size=0.2, random_state=7)


Screenshot:

5. Let’s initialize a TfidfVectorizer with stop words from the English language and a maximum document
frequency of 0.7 (terms with a higher document frequency will be discarded). Stop words are the most common
words in a language that are to be filtered out before processing the natural language data. And a
TfidfVectorizer turns a collection of raw documents into a matrix of TF-IDF features.

Now, fit and transform the vectorizer on the train set, and transform the vectorizer on the test set.

#DataFlair - Initialize a TfidfVectorizer

tfidf_vectorizer=TfidfVectorizer(stop_words='english', max_df=0.7)

#DataFlair - Fit and transform train set, transform test set

tfidf_train=tfidf_vectorizer.fit_transform(x_train)

tfidf_test=tfidf_vectorizer.transform(x_test)

Screenshot:

6. Next, we’ll initialize a PassiveAggressiveClassifier. This is. We’ll fit this on tfidf_train and y_train.

Then, we’ll predict on the test set from the TfidfVectorizer and calculate the accuracy with accuracy_score()
from sklearn.metrics.

#DataFlair - Initialize a PassiveAggressiveClassifier

pac=PassiveAggressiveClassifier(max_iter=50)

pac.fit(tfidf_train,y_train)

#DataFlair - Predict on the test set and calculate accuracy


y_pred=pac.predict(tfidf_test)

score=accuracy_score(y_test,y_pred)

print(f'Accuracy: {round(score*100,2)}%')

Output Screenshot:

7. We got an accuracy of 92.82% with this model. Finally, let’s print out a confusion matrix to gain insight into
the number of false and true negatives and positives.

#DataFlair - Build confusion matrix

confusion_matrix(y_test,y_pred, labels=['FAKE','REAL'])

Output Screenshot:

So with this model, we have 589 true positives, 587 true negatives, 42 false positives, and 49 false negatives.

Summary
Today, we learned to detect fake news with Python. We took a political dataset, implemented a TfidfVectorizer,
initialized a PassiveAggressiveClassifier, and fit our model. We ended up obtaining an accuracy of 92.82% in
magnitude.

Hope you enjoyed our first advanced level Python Project. Keep visiting DataFlair for more interesting Python
data science and machine learning projects.

Python Mini Project – Speech Emotion Recognition with librosa


by DataFlair Team · September 20, 2019

Python Mini Project

Speech emotion recognition, the best ever python mini project. The best example of it can be seen at call
centers. If you ever noticed, call centers employees never talk in the same manner, their way of pitching/talking
to the customers changes with customers. Now, this does happen with common people too, but how is this
relevant to call centers? Here is your answer, the employees recognize customers’ emotions from speech, so
they can improve their service and convert more people. In this way, they are using speech emotion recognition.
So, let’s discuss this project in detail.

Speech emotion recognition is a simple Python mini-project, which you are going to practice with DataFlair.
Before, I explain you the terms related to this mini python project, make sure you bookmarked the complete
list of Python Projects.

Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age Gender Detection Python Project

Breast Cancer Classification Python Project

What is Speech Emotion Recognition?


Speech Emotion Recognition, abbreviated as SER, is the act of attempting to recognize human emotion and
affective states from speech. This is capitalizing on the fact that voice often reflects underlying emotion through
tone and pitch. This is also the phenomenon that animals like dogs and horses employ to be able to understand
human emotion.

SER is tough because emotions are subjective and annotating audio is challenging.

What is librosa?
librosa is a Python library for analyzing audio and music. It has a flatter package layout, standardizes interfaces
and names, backwards compatibility, modular functions, and readable code. Further, in this Python mini-
project, we demonstrate how to install it (and a few other packages) with pip.

What is JupyterLab?
JupyterLab is an open-source, web-based UI for Project Jupyter and it has all basic functionalities of the Jupyter
Notebook, like notebooks, terminals, text editors, file browsers, rich outputs, and more. However, it also
provides improved support for third party extensions.

To run code in the JupyterLab, you’ll first need to run it with the command prompt:

C:\Users\DataFlair>jupyter lab

This will open for you a new session in your browser. Create a new Console and start typing in your code.
JupyterLab can execute multiple lines of code at once; pressing enter will not execute your code, you’ll need to
press Shift+Enter for the same.

Speech Emotion Recognition – Objective


To build a model to recognize emotion from speech using the librosa and sklearn libraries and the RAVDESS
dataset.

Speech Emotion Recognition – About the Python Mini Project


In this Python mini project, we will use the libraries librosa, soundfile, and sklearn (among others) to build a
model using an MLPClassifier. This will be able to recognize emotion from sound files. We will load the data,
extract features from it, then split the dataset into training and testing sets. Then, we’ll initialize an
MLPClassifier and train the model. Finally, we’ll calculate the accuracy of our model.

The Dataset
For this Python mini project, we’ll use the RAVDESS dataset; this is the Ryerson Audio-Visual Database of
Emotional Speech and Song dataset, and is free to download. This dataset has 7356 files rated by 247
individuals 10 times on emotional validity, intensity, and genuineness. The entire dataset is 24.8GB from 24
actors, but we’ve lowered the sample rate on all the files, and you can download it here.

Prerequisites
You’ll need to install the following libraries with pip:

pip install librosa soundfile numpy sklearn pyaudio

If you run into issues installing librosa with pip, you can try it with conda.

Steps for speech emotion recognition python projects


1. Make the necessary imports:

import librosa

import soundfile

import os, glob, pickle

import numpy as np
from sklearn.model_selection import train_test_split

from sklearn.neural_network import MLPClassifier

from sklearn.metrics import accuracy_score

Screenshot:

2. Define a function extract_feature to extract the mfcc, chroma, and mel features from a sound file. This
function takes 4 parameters- the file name and three Boolean parameters for the three features:

mfcc: Mel Frequency Cepstral Coefficient, represents the short-term power spectrum of a sound

chroma: Pertains to the 12 different pitch classes

mel: Mel Spectrogram Frequency

Learn more about Python Sets and Booleans

Open the sound file with soundfile.SoundFile using with-as so it’s automatically closed once we’re done. Read
from it and call it X. Also, get the sample rate. If chroma is True, get the Short-Time Fourier Transform of X.

Let result be an empty numpy array. Now, for each feature of the three, if it exists, make a call to the
corresponding function from librosa.feature (eg- librosa.feature.mfcc for mfcc), and get the mean value. Call the
function hstack() from numpy with result and the feature value, and store this in result. hstack() stacks arrays in
sequence horizontally (in a columnar fashion). Then, return the result.

#DataFlair - Extract features (mfcc, chroma, mel) from a sound file

def extract_feature(file_name, mfcc, chroma, mel):


with soundfile.SoundFile(file_name) as sound_file:

X = sound_file.read(dtype="float32")

sample_rate=sound_file.samplerate

if chroma:

stft=np.abs(librosa.stft(X))

result=np.array([])

if mfcc:

mfccs=np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)

result=np.hstack((result, mfccs))

if chroma:

chroma=np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)

result=np.hstack((result, chroma))

if mel:

mel=np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)

result=np.hstack((result, mel))

return result

Screenshot:
3. Now, let’s define a dictionary to hold numbers and the emotions available in the RAVDESS dataset, and a
list to hold those we want- calm, happy, fearful, disgust.

#DataFlair - Emotions in the RAVDESS dataset

emotions={

'01':'neutral',

'02':'calm',

'03':'happy',

'04':'sad',

'05':'angry',

'06':'fearful',

'07':'disgust',

'08':'surprised'

#DataFlair - Emotions to observe

observed_emotions=['calm', 'happy', 'fearful', 'disgust']

Screenshot:
Facing Failure in Interview?

Prepare with DataFlair – Frequently Asked Python Interview Questions

4. Now, let’s load the data with a function load_data() – this takes in the relative size of the test set as
parameter. x and y are empty lists; we’ll use the glob() function from the glob module to get all the pathnames
for the sound files in our dataset. The pattern we use for this is: “D:\\DataFlair\\ravdess data\\Actor_*\\*.wav”.
This is because our dataset looks like this:

Screenshot:
So, for each such path, get the basename of the file, the emotion by splitting the name around ‘-’ and extracting
the third value:

Screenshot:

Using our emotions dictionary, this number is turned into an emotion, and our function checks whether this
emotion is in our list of observed_emotions; if not, it continues to the next file. It makes a call to extract_feature
and stores what is returned in ‘feature’. Then, it appends the feature to x and the emotion to y. So, the list x
holds the features and y holds the emotions. We call the function train_test_split with these, the test size, and a
random state value, and return that.

#DataFlair - Load the data and extract features for each sound file

def load_data(test_size=0.2):

x,y=[],[]

for file in glob.glob("D:\\DataFlair\\ravdess data\\Actor_*\\*.wav"):

file_name=os.path.basename(file)

emotion=emotions[file_name.split("-")[2]]

if emotion not in observed_emotions:

continue

feature=extract_feature(file, mfcc=True, chroma=True, mel=True)

x.append(feature)

y.append(emotion)

return train_test_split(np.array(x), y, test_size=test_size, random_state=9)

Screenshot:

5. Time to split the dataset into training and testing sets! Let’s keep the test set 25% of everything and use the
load_data function for this.

#DataFlair - Split the dataset

x_train,x_test,y_train,y_test=load_data(test_size=0.25)

Screenshot:
6. Observe the shape of the training and testing datasets:

#DataFlair - Get the shape of the training and testing datasets

print((x_train.shape[0], x_test.shape[0]))

Screenshot:

7. And get the number of features extracted.

#DataFlair - Get the number of features

extracted print(f'Features extracted:

{x_train.shape[1]}')

Output Screenshot:

8. Now, let’s initialize an MLPClassifier. This is a Multi-layer Perceptron Classifier; it optimizes the log-loss
function using LBFGS or stochastic gradient descent. Unlike SVM or Naive Bayes, the MLPClassifier has an
internal neural network for the purpose of classification. This is a feedforward ANN model.

#DataFlair - Initialize the Multi Layer Perceptron Classifier

model=MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,),


learning_rate='adaptive', max_iter=500)

Screenshot:
9. Fit/train the model.

#DataFlair - Train the model

model.fit(x_train,y_train)

Output Screenshot:

10. Let’s predict the values for the test set. This gives us y_pred (the predicted emotions for the features in the
test set).

#DataFlair - Predict for the test set

y_pred=model.predict(x_test)

Screenshot:

11. To calculate the accuracy of our model, we’ll call up the accuracy_score() function we imported from
sklearn. Finally, we’ll round the accuracy to 2 decimal places and print it out.

#DataFlair - Calculate the accuracy of our model

accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)

#DataFlair - Print the accuracy

print("Accuracy: {:.2f}%".format(accuracy*100))

Output Screenshot:
Summary

In this Python mini project, we learned to recognize emotions from speech. We used an MLPClassifier for this
and made use of the soundfile library to read the sound file, and the librosa library to extract features from it. As
you’ll see, the model delivered an accuracy of 72.4%. That’s good enough for us yet.

Hope you enjoyed the mini python project.

Python Machine Learning Project – Detecting Parkinson’s Disease


with XGBoost
by DataFlair Team · September 20, 2019

In our list of Python projects, detecting Parkinson’s disease with python is on the 3rd position. In this Python
Machine learning project, we will build a model using which we can accurately detect the presence of
Parkinson’s disease in one’s body.

Here are some more Python Machine Learning Projects which you can bookmark for practicing later –

Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age and Gender Detection Python Project

Breast Cancer Classification Python Project

So, let’s start the Python Machine Learning Project with the introduction of terms used –

Detecting Parkinson’s Disease – Python Machine Learning Project

What is Parkinson’s Disease?


Parkinson’s disease is a progressive disorder of the central nervous system affecting movement and inducing
tremors and stiffness. It has 5 stages to it and affects more than 1 million individuals every year in India. This is
chronic and has no cure yet. It is a neurodegenerative disorder affecting dopamine-producing neurons in the
brain.

What is XGBoost?
XGBoost is a new Machine Learning algorithm designed with speed and performance in mind. XGBoost stands
for eXtreme Gradient Boosting and is based on decision trees. In this project, we will import the XGBClassifier
from the xgboost library; this is an implementation of the scikit-learn API for XGBoost classification.

Detecting Parkinson’s Disease with XGBoost – Objective


To build a model to accurately detect the presence of Parkinson’s disease in an individual.

Detecting Parkinson’s Disease with XGBoost – About the Python Machine


Learning Project
In this Python machine learning project, using the Python libraries scikit-learn, numpy, pandas, and xgboost,
we will build a model using an XGBClassifier. We’ll load the data, get the features and labels, scale the
features, then split the dataset, build an XGBClassifier, and then calculate the accuracy of our model.

Dataset for Python Machine Learning Project


You’ll need the UCI ML Parkinsons dataset for this; you can download it here. The dataset has 24 columns and
195 records and is only 39.7 KB.

Prerequisites
You’ll need to install the following libraries with pip:

pip install numpy pandas sklearn xgboost

You’ll also need to install Jupyter Lab, and then use the command prompt to run it:

C:\Users\DataFlair>jupyter lab

This will open a new JupyterLab window in your browser. Here, you will create a new console and type in your
code, then press Shift+Enter to execute one or more lines at a time.

Steps for Detecting Parkinson’s Disease with XGBoost


Below are some steps required to practice Python Machine Learning Project –

1. Make necessary imports:

import numpy as np
import pandas as pd

import os, sys

from sklearn.preprocessing import MinMaxScaler

from xgboost import XGBClassifier

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

Screenshot:

2. Now, let’s read the data into a DataFrame and get the first 5 records.

#DataFlair - Read the data

df=pd.read_csv('D:\\DataFlair\\parkinsons.data')

df.head()

Output Screenshot:

3. Get the features and labels from the DataFrame (dataset). The features are all the columns except ‘status’, and
the labels are those in the ‘status’ column.

#DataFlair - Get the features and labels

features=df.loc[:,df.columns!='status'].values[:,1:]
labels=df.loc[:,'status'].values

Screenshot:

4. The ‘status’ column has values 0 and 1 as labels; let’s get the counts of these labels for both- 0 and 1.

#DataFlair - Get the count of each label (0 and 1) in labels

print(labels[labels==1].shape[0], labels[labels==0].shape[0])

Output Screenshot:

We have 147 ones and 48 zeros in the status column in our dataset.

5. Initialize a MinMaxScaler and scale the features to between -1 and 1 to normalize them. The MinMaxScaler
transforms features by scaling them to a given range. The fit_transform() method fits to the data and then
transforms it. We don’t need to scale the labels.

#DataFlair - Scale the features to between -1 and 1

scaler=MinMaxScaler((-1,1))

x=scaler.fit_transform(features)

y=labels

Screenshot:

6. Now, split the dataset into training and testing sets keeping 20% of the data for testing.

#DataFlair - Split the dataset

x_train,x_test,y_train,y_test=train_test_split(x, y, test_size=0.2, random_state=7)

Screenshot:
7. Initialize an XGBClassifier and train the model. This classifies using eXtreme Gradient Boosting- using
gradient boosting algorithms for modern data science problems. It falls under the category of Ensemble
Learning in ML, where we train and predict using many models to produce one superior output.

#DataFlair - Train the model

model=XGBClassifier()

model.fit(x_train,y_train)

Output Screenshot:

8. Finally, generate y_pred (predicted values for x_test) and calculate the accuracy for the model. Print it out.

# DataFlair - Calculate the accuracy

y_pred=model.predict(x_test)

print(accuracy_score(y_test, y_pred)*100)

Output Screenshot:

Summary
In this Python machine learning project, we learned to detect the presence of Parkinson’s Disease in individuals
using various factors. We used an XGBClassifier for this and made use of the sklearn library to prepare the
dataset. This gives us an accuracy of 94.87%, which is great considering the number of lines of code in this
python project.

Hope you enjoyed this Python project. We have already provided you the links for more interesting Python
Projects at the top of the blog.

Interesting Python Project of Gender and Age Detection with


OpenCV
by DataFlair Team · September 19, 2019

Today, we are introducing our fourth python project that is gender and age detection with OpenCV. It is very
interesting and one of my favorite project. DataFlair has published more interesting python projects on the
following topics with source code –

Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age and Gender Detecting Python Project

Breast Cancer Classification Python Project

If these projects are helping you then please share your feedback with us. It will motivate us, to provide more
such useful content you.

Without talking much, let’s start the gender and age detection Python Project.

Gender and Age Detection Python Project

First introducing you with the terminologies used in this advanced python project of gender and age detection –

What is Computer Vision?


Computer Vision is the field of study that enables computers to see and identify digital images and videos as a
human would. The challenges it faces largely follow from the limited understanding of biological vision.
Computer Vision involves acquiring, processing, analyzing, and understanding digital images to extract high-
dimensional data from the real world in order to generate symbolic or numerical information which can then be
used to make decisions. The process often includes practices like object recognition, video tracking, motion
estimation, and image restoration.

What is OpenCV?
OpenCV is short for Open Source Computer Vision. Intuitively by the name, it is an open-source Computer
Vision and Machine Learning library. This library is capable of processing real-time image and video while also
boasting analytical capabilities. It supports the Deep Learning frameworks TensorFlow, Caffe, and PyTorch.
What is a CNN?
A Convolutional Neural Network is a deep neural network (DNN) widely used for the purposes of image
recognition and processing and NLP. Also known as a ConvNet, a CNN has input and output layers, and
multiple hidden layers, many of which are convolutional. In a way, CNNs are regularized multilayer
perceptrons.

Gender and Age Detection Python Project- Objective


To build a gender and age detector that can approximately guess the gender and age of the person (face) in a
picture using Deep Learning on the Adience dataset.

Gender and Age Detection – About the Project


In this Python Project, we will use Deep Learning to accurately identify the gender and age of a person from a
single image of a face. We will use the models trained by Tal Hassner and Gil Levi. The predicted gender may
be one of ‘Male’ and ‘Female’, and the predicted age may be one of the following ranges- (0 – 2), (4 – 6), (8 –
12), (15 – 20), (25 – 32), (38 – 43), (48 – 53), (60 – 100) (8 nodes in the final softmax layer). It is very difficult
to accurately guess an exact age from a single image because of factors like makeup, lighting, obstructions, and
facial expressions. And so, we make this a classification problem instead of making it one of regression.

The CNN Architecture


The convolutional neural network for this python project has 3 convolutional layers:

Convolutional layer; 96 nodes, kernel size 7

Convolutional layer; 256 nodes, kernel size 5

Convolutional layer; 384 nodes, kernel size 3

It has 2 fully connected layers, each with 512 nodes, and a final output layer of softmax type.

To go about the python project, we’ll:

Detect faces

Classify into Male/Female

Classify into one of the 8 age ranges

Put the results on the image and display it

The Dataset
For this python project, we’ll use the Adience dataset; the dataset is available in the public domain and you can
find it here. This dataset serves as a benchmark for face photos and is inclusive of various real-world imaging
conditions like noise, lighting, pose, and appearance. The images have been collected from Flickr albums and
distributed under the Creative Commons (CC) license. It has a total of 26,580 photos of 2,284 subjects in eight
age ranges (as mentioned above) and is about 1GB in size. The models we will use have been trained on this
dataset.

Prerequisites
You’ll need to install OpenCV (cv2) to be able to run this project. You can do this with pip-

pip install opencv-python

Other packages you’ll be needing are math and argparse, but those come as part of the standard Python library.

Steps for practicing gender and age detection python project


1. Download this zip. Unzip it and put its contents in a directory you’ll call gad.

The contents of this zip are:

opencv_face_detector.pbtxt

opencv_face_detector_uint8.pb

age_deploy.prototxt

age_net.caffemodel

gender_deploy.prototxt

gender_net.caffemodel

a few pictures to try the project on

For face detection, we have a .pb file- this is a protobuf file (protocol buffer); it holds the graph definition and
the trained weights of the model. We can use this to run the trained model. And while a .pb file holds the
protobuf in binary format, one with the .pbtxt extension holds it in text format. These are TensorFlow files. For
age and gender, the .prototxt files describe the network configuration and the .caffemodel file defines the
internal states of the parameters of the layers.

2. We use the argparse library to create an argument parser so we can get the image argument from the
command prompt. We make it parse the argument holding the path to the image to classify gender and age for.

3. For face, age, and gender, initialize protocol buffer and model.

4. Initialize the mean values for the model and the lists of age ranges and genders to classify from.

5. Now, use the readNet() method to load the networks. The first parameter holds trained weights and the
second carries network configuration.

6. Let’s capture video stream in case you’d like to classify on a webcam’s stream. Set padding to 20.

7. Now until any key is pressed, we read the stream and store the content into the names hasFrame and frame.
If it isn’t a video, it must wait, and so we call up waitKey() from cv2, then break.
8. Let’s make a call to the highlightFace() function with the faceNet and frame parameters, and what this
returns, we will store in the names resultImg and faceBoxes. And if we got 0 faceBoxes, it means there was no
face to detect.
Here, net is faceNet- this model is the DNN Face Detector and holds only about 2.7MB on disk.

Create a shallow copy of frame and get its height and width.

Create a blob from the shallow copy.

Set the input and make a forward pass to the network.

faceBoxes is an empty list now. for each value in 0 to 127, define the confidence (between 0 and 1).
Wherever we find the confidence greater than the confidence threshold, which is 0.7, we get the x1, y1,
x2, and y2 coordinates and append a list of those to faceBoxes.

Then, we put up rectangles on the image for each such list of coordinates and return two things: the shallow
copy and the list of faceBoxes.

9. But if there are indeed faceBoxes, for each of those, we define the face, create a 4-dimensional blob from the
image. In doing this, we scale it, resize it, and pass in the mean values.

10. We feed the input and give the network a forward pass to get the confidence of the two class. Whichever is
higher, that is the gender of the person in the picture.

11. Then, we do the same thing for age.

12. We’ll add the gender and age texts to the resulting image and display it with imshow().

Python Project Examples for Gender and Age Detection


Let’s try this gender and age classifier out on some of our own images now.

We’ll get to the command prompt, run our script with the image option and specify an image to classify:

Python Project Example 1

Output:
Python Project Example 2

Output:

Python Project Example 3

Output:
Python Project Example 4

Output:

Python Project Example 5


Output:

Python project Example 6

Output:

Summary

In this python project, we implemented a CNN to detect gender and age from a single picture of a face. Did you
finish the project with us? Try this on your own pictures. Check more cool projects in python with source code
published by DataFlair.

If you enjoyed the above python project, do comment and let us know your thoughts.
Project in Python – Breast Cancer Classification with Deep Learning
by DataFlair Team · September 21, 2019

If you want to master Python programming language then you can’t skip projects in Python. After publishing 4
advanced python projects, DataFlair today came with another one that is Breast Cancer Classification project in
Python. To crack your next Python Interview, practice these projects thoroughly and if you face any confusion,
do comment, DataFlair is always ready to help you.

Before we begin this Breast Cancer Classification Project in Python, let me provide you the list of advanced
python projects published by DataFlair –

Detecting Fake News Python Project

Speech Emotion Recognition Python Project

Detecting Parkinson’s Disease Python Project

Age and Gender Detecting Python Project

Breast Cancer Classification Python Project

Breast Cancer Classification Project in Python

Get aware with the terms used in Breast Cancer Classification project in Python

What is Deep Learning?


An intensive approach to Machine Learning, Deep Learning is inspired by the workings of the human brain
and its biological neural networks. Architectures as deep neural networks, recurrent neural networks,
convolutional neural networks, and deep belief networks are made of multiple layers for the data to pass
through before finally producing the output. Deep Learning serves to improve AI and make many of its
applications possible; it is applied to many such fields of computer vision, speech recognition, natural language
processing, audio recognition, and drug design.

What is Keras?
Keras is an open-source neural-network library written in Python. It is a high-level API and can run on top of
TensorFlow, CNTK, and Theano. Keras is all about enabling fast experimentation and prototyping while
running seamlessly on CPU and GPU. It is user-friendly, modular, and extensible.

Breast Cancer Classification – Objective


To build a breast cancer classifier on an IDC dataset that can accurately classify a histology image as benign or
malignant.

Breast Cancer Classification – About the Python Project


In this project in python, we’ll build a classifier to train on 80% of a breast cancer histology image dataset. Of
this, we’ll keep 10% of the data for validation. Using Keras, we’ll define a CNN (Convolutional Neural
Network), call it CancerNet, and train it on our images. We’ll then derive a confusion matrix to analyze the
performance of the model.

IDC is Invasive Ductal Carcinoma; cancer that develops in a milk duct and invades the fibrous or fatty breast
tissue outside the duct; it is the most common form of breast cancer forming 80% of all breast cancer diagnoses.
And histology is the study of the microscopic structure of tissues.

The Dataset
We’ll use the IDC_regular dataset (the breast cancer histology image dataset) from Kaggle. This dataset holds
2,77,524 patches of size 50×50 extracted from 162 whole mount slide images of breast cancer specimens
scanned at 40x. Of these, 1,98,738 test negative and 78,786 test positive with IDC. The dataset is available in
public domain and you can download it here. You’ll need a minimum of 3.02GB of disk space for this.

Filenames in this dataset look like this:

8863_idx5_x451_y1451_class0

Here, 8863_idx5 is the patient ID, 451 and 1451 are the x- and y- coordinates of the crop, and 0 is the class
label (0 denotes absence of IDC).

Prerequisites
You’ll need to install some python packages to be able to run this advanced python project. You can do this
with pip-

pip install numpy opencv-python pillow tensorflow keras imutils scikit-learn matplotlib

Steps for Advanced Project in Python – Breast Cancer Classification


1. Download this zip. Unzip it at your preferred location, get there.

Screenshot:

2. Now, inside the inner breast-cancer-classification directory, create directory datasets- inside this, create
directory original:

mkdir datasets

mkdir datasets\original
3. Download the dataset.

4. Unzip the dataset in the original directory. To observe the structure of this directory, we’ll use the tree
command:

cd breast-cancer-classification\breast-cancer-classification\datasets\original

tree

Output Screenshot:

We have a directory for each patient ID. And in each such directory, we have the 0 and 1 directories for images
with benign and malignant content.

config.py:
This holds some configuration we’ll need for building the dataset and training the model. You’ll find this in the
cancernet directory.

import os

INPUT_DATASET = "datasets/original"

BASE_PATH = "datasets/idc"

TRAIN_PATH = os.path.sep.join([BASE_PATH, "training"])


VAL_PATH = os.path.sep.join([BASE_PATH, "validation"])

TEST_PATH = os.path.sep.join([BASE_PATH, "testing"])

TRAIN_SPLIT = 0.8

VAL_SPLIT = 0.1

Screenshot:

Here, we declare the path to the input dataset (datasets/original), that for the new directory (datasets/idc), and
the paths for the training, validation, and testing directories using the base path. We also declare that 80% of the
entire dataset will be used for training, and of that, 10% will be used for validation.

build_dataset.py:
This will split our dataset into training, validation, and testing sets in the ratio mentioned above- 80% for
training (of that, 10% for validation) and 20% for testing. With the ImageDataGenerator from Keras, we will
extract batches of images to avoid making space for the entire dataset in memory at once.

from cancernet import config

from imutils import paths

import random, shutil, os


originalPaths=list(paths.list_images(config.INPUT_DATASET))

random.seed(7)

random.shuffle(originalPaths)

index=int(len(originalPaths)*config.TRAIN_SPLIT)

trainPaths=originalPaths[:index]

testPaths=originalPaths[index:]

index=int(len(trainPaths)*config.VAL_SPLIT)

valPaths=trainPaths[:index]

trainPaths=trainPaths[index:]

datasets=[("training", trainPaths, config.TRAIN_PATH),

("validation", valPaths, config.VAL_PATH),

("testing", testPaths, config.TEST_PATH)

for (setType, originalPaths, basePath) in datasets:

print(f'Building {setType} set')

if not os.path.exists(basePath):

print(f'Building directory {base_path}')

os.makedirs(basePath)

for path in originalPaths:

file=path.split(os.path.sep)[-1]
label=file[-5:-4]

labelPath=os.path.sep.join([basePath,label])

if not os.path.exists(labelPath):

print(f'Building directory {labelPath}')

os.makedirs(labelPath)

newPath=os.path.sep.join([labelPath, file])

shutil.copy2(inputPath, newPath)

Screenshot:
In this, we’ll import from config, imutils, random, shutil, and os. We’ll build a list of original paths to the
images, then shuffle the list. Then, we calculate an index by multiplying the length of this list by 0.8 so we can
slice this list to get sublists for the training and testing datasets. Next, we further calculate an index saving 10%
of the list for the training dataset for validation and keeping the rest for training itself.

Now, datasets is a list with tuples for information about the training, validation, and testing sets. These hold the
paths and the base path for each. For each setType, path, and base path in this list, we’ll print, say, ‘Building
testing set’. If the base path does not exist, we’ll create the directory. And for each path in originalPaths, we’ll
extract the filename and the class label. We’ll build the path to the label directory(0 or 1)- if it doesn’t exist yet,
we’ll explicitly create this directory. Now, we’ll build the path to the resulting image and copy the image here-
where it belongs.
5. Run the script

build_dataset.py: py

build_dataset.py

Output Screenshot:

cancernet.py:
The network we’ll build will be a CNN (Convolutional Neural Network) and call it CancerNet. This network
performs the following operations:

Use 3×3 CONV filters

Stack these filters on top of each other

Perform max-pooling

Use depthwise separable convolution (more efficient, takes up less memory)

from keras.models import Sequential

from keras.layers.normalization import BatchNormalization

from keras.layers.convolutional import SeparableConv2D

from keras.layers.convolutional import MaxPooling2D

from keras.layers.core import Activation

from keras.layers.core import Flatten

from keras.layers.core import Dropout

from keras.layers.core import Dense

from keras import backend as K

class CancerNet:

@staticmethod
def build(width,height,depth,classes):
model=Sequential()

shape=(height,width,depth)

channelDim=-1

if K.image_data_format()=="channels_first":

shape=(depth,height,width)

channelDim=1

model.add(SeparableConv2D(32, (3,3), padding="same",input_shape=shape))

model.add(Activation("relu"))

model.add(BatchNormalization(axis=channelDim))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Dropout(0.25))

model.add(SeparableConv2D(64, (3,3), padding="same"))

model.add(Activation("relu"))

model.add(BatchNormalization(axis=channelDim))

model.add(SeparableConv2D(64, (3,3), padding="same"))

model.add(Activation("relu"))

model.add(BatchNormalization(axis=channelDim))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Dropout(0.25))

model.add(SeparableConv2D(128, (3,3), padding="same"))

model.add(Activation("relu"))

model.add(BatchNormalization(axis=channelDim))

model.add(SeparableConv2D(128, (3,3), padding="same"))

model.add(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))

model.add(SeparableConv2D(128, (3,3), padding="same"))

model.add(Activation("relu"))

model.add(BatchNormalization(axis=channelDim))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(256))

model.add(Activation("relu"))

model.add(BatchNormalization())

model.add(Dropout(0.5))

model.add(Dense(classes))

model.add(Activation("softmax"))

return model

Screenshot:
Screenshot:
We use the Sequential API to build CancerNet and SeparableConv2D to implement depthwise convolutions.
The class CancerNet has a static method build that takes four parameters- width and height of the image, its
depth (the number of color channels in each image), and the number of classes the network will predict
between, which, for us, is 2 (0 and 1).

In this method, we initialize model and shape. When using channels_first, we update the shape and the channel
dimension.

Now, we’ll define three DEPTHWISE_CONV => RELU => POOL layers; each with a higher stacking and a
greater number of filters. The softmax classifier outputs prediction percentages for each class. In the end, we
return the model.

train_model.py:
This trains and evaluates our model. Here, we’ll import from keras, sklearn, cancernet, config, imutils,
matplotlib, numpy, and os.

import matplotlib

matplotlib.use("Agg")

from keras.preprocessing.image import ImageDataGenerator

from keras.callbacks import LearningRateScheduler

from keras.optimizers import Adagrad

from keras.utils import np_utils

from sklearn.metrics import classification_report

from sklearn.metrics import confusion_matrix


from cancernet.cancernet import CancerNet

from cancernet import config

from imutils import paths

import matplotlib.pyplot as plt

import numpy as np

import os

NUM_EPOCHS=40; INIT_LR=1e-2; BS=32

trainPaths=list(paths.list_images(config.TRAIN_PATH))

lenTrain=len(trainPaths)

lenVal=len(list(paths.list_images(config.VAL_PATH)))

lenTest=len(list(paths.list_images(config.TEST_PATH)))

trainLabels=[int(p.split(os.path.sep)[-2]) for p in trainPaths]

trainLabels=np_utils.to_categorical(trainLabels)

classTotals=trainLabels.sum(axis=0)

classWeight=classTotals.max()/classTotals

trainAug = ImageDataGenerator(

rescale=1/255.0,

rotation_range=20,

zoom_range=0.05,

width_shift_range=0.1,

height_shift_range=0.1,

shear_range=0.05,

horizontal_flip=True,

vertical_flip=True,
fill_mode="nearest")

valAug=ImageDataGenerator(rescale=1 / 255.0)

trainGen =

trainAug.flow_from_directory( config.TRAIN_P

ATH,

class_mode="categorical",

target_size=(48,48),

color_mode="rgb",

shuffle=True,

batch_size=BS)

valGen = valAug.flow_from_directory(

config.VAL_PATH,

class_mode="categorical",

target_size=(48,48),

color_mode="rgb",

shuffle=False,

batch_size=BS)

testGen = valAug.flow_from_directory(

config.TEST_PATH,

class_mode="categorical",

target_size=(48,48),

color_mode="rgb",

shuffle=False,

batch_size=BS)

model=CancerNet.build(width=48,height=48,depth=3,classes=2)

opt=Adagrad(lr=INIT_LR,decay=INIT_LR/NUM_EPOCHS)

model.compile(loss="binary_crossentropy",optimizer=opt,metrics=["accuracy"])

M=model.fit_generator(
trainGen,

steps_per_epoch=lenTrain//BS,

validation_data=valGen,

validation_steps=lenVal//BS,

class_weight=classWeight,

epochs=NUM_EPOCHS)

print("Now evaluating the model")

testGen.reset()

pred_indices=model.predict_generator(testGen,steps=(lenTest//BS)+1)

pred_indices=np.argmax(pred_indices,axis=1)

print(classification_report(testGen.classes, pred_indices,
target_names=testGen.class_indices.keys()))

cm=confusion_matrix(testGen.classes,pred_indices)

total=sum(sum(cm))

accuracy=(cm[0,0]+cm[1,1])/total

specificity=cm[1,1]/(cm[1,0]+cm[1,1])

sensitivity=cm[0,0]/(cm[0,0]+cm[0,1])

print(cm)

print(f'Accuracy: {accuracy}')

print(f'Specificity: {specificity}')

print(f'Sensitivity: {sensitivity}')

N = NUM_EPOCHS

plt.style.use("ggplot")

plt.figure()

plt.plot(np.arange(0,N), M.history["loss"], label="train_loss")

plt.plot(np.arange(0,N), M.history["val_loss"], label="val_loss")

plt.plot(np.arange(0,N), M.history["acc"], label="train_acc")

plt.plot(np.arange(0,N), M.history["val_acc"], label="val_acc")

plt.title("Training Loss and Accuracy on the IDC Dataset")


plt.xlabel("Epoch No.")

plt.ylabel("Loss/Accuracy")

plt.legend(loc="lower left")

plt.savefig('plot.png')

Screenshot:

Screenshot:
Screenshot:
In this script, first, we set initial values for the number of epochs, the learning rate, and the batch size. We’ll get
the number of paths in the three directories for training, validation, and testing. Then, we’ll get the class weight
for the training data so we can deal with the imbalance.

Now, we initialize the training data augmentation object. This is a process of regularization that helps generalize
the model. This is where we slightly modify the training examples to avoid the need for more training data.
We’ll initialize the validation and testing data augmentation objects.

We’ll initialize the training, validation, and testing generators so they can generate batches of images of size
batch_size. Then, we’ll initialize the model using the Adagrad optimizer and compile it with a
binary_crossentropy loss function. Now, to fit the model, we make a call to fit_generator().

We have successfully trained our model. Now, let’s evaluate the model on our testing data. We’ll reset the
generator and make predictions on the data. Then, for images from the testing set, we get the indices of the
labels with the corresponding largest predicted probability. And we’ll display a classification report.

Now, we’ll compute the confusion matrix and get the raw accuracy, specificity, and sensitivity, and display all
values. Finally, we’ll plot the training loss and accuracy.
Output Screenshot:

Output Screenshot:

Output:
Summary

In this project in python, we learned to build a breast cancer classifier on the IDC dataset (with histology images
for Invasive Ductal Carcinoma) and created the network CancerNet for the same. We used Keras to implement
the same. Hope you enjoyed this Python project.

150+ Python Interview Questions and Answers for Freshers [Latest]


by DataFlair Team · September 27, 2019

DataFlair is devoted to help Python learners become successful in their Python careers. That’s why we are
publishing an interesting and helpful series of Python Interview Questions and Answers. In this series, you will
get 150+ Python Interview Questions and Answers in 2 different parts, that covers

Python Interview Questions and Answers for Beginners – Part 1

Python Interview Questions and Answers for Experienced – Part 2

Starting with our first part that is Python Interview Questions and Answers for freshers. I have divided this blog
into different categories.

Common Python Interview Questions and Answers

Frequently asked Python Interview Questions and Answers


Basic Python Programming Interview Questions and Answers

Top Python Interview Questions and Answers

Technical Python Interview Questions and Answers

Python OOPS Interview Questions and Answers

Open-ended Python Interview Questions for Beginners

Common Python Interview Questions and Answers


The reason I am sharing these interview questions is that you can revise all your basic concepts. As you are a
beginner, the interviewer will surely check your understanding of Python Fundamentals. Let’s start exploring
the basic Python Interview Questions and Answers –

Q.1. What are the key features of Python?

If it makes for an introductory language to programming, Python must mean something. These are its qualities:

Interpreted

Dynamically-typed

Object-oriented

Concise and simple

Free

Has a large community

Q.2. Differentiate between lists and tuples.

The major difference is that a list is mutable, but a tuple is immutable. Examples:

>>> mylist=[1,3,3]

>>> mylist[1]=2

>>> mytuple=(1,3,3)

>>> mytuple[1]=2

Traceback (most recent call last):

File “<pyshell#97>”, line 1, in <module>

mytuple[1]=2

TypeError: ‘tuple’ object does not support item assignment


Python Tuples vs Lists – A Detailed Comparison

Q.3. Explain the ternary operator in Python.

Unlike C++, we don’t have ?: in Python, but we have this:

[on true] if [expression] else [on false]

If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.

Below is how you would use it:

>>> a,b=2,3

>>> min=a if a<b else b

>>> min

>>> print("Hi") if a<b else print("Bye")

Hi

Q.4. What are negative indices?

Let’s take a list for this.

>>> mylist=[0,1,2,3,4,5,6,7,8]

A negative index, unlike a positive one, begins searching from the right.

>>> mylist[-3]

This also helps with slicing from the back:

>>> mylist[-6:-1]

[3, 4, 5, 6, 7]

Q.5. Is Python case-sensitive?

A language is case-sensitive if it distinguishes between identifiers like myname and Myname. In other words, it
cares about case- lowercase or uppercase. Let’s try this with Python.

>>> myname='Ayushi'

>>> Myname
Traceback (most recent call last):

File “<pyshell#3>”, line 1, in <module>

Myname

NameError: name ‘Myname’ is not defined

As you can see, this raised a NameError. This means that Python is indeed case-sensitive.

Q.6. How long can an identifier be in Python?

According to the official Python documentation, an identifier can be of any length. However, PEP 8 suggests
that you should limit all lines to a maximum of 79 characters. Also, PEP 20 says ‘readability counts’. So, a very
long identifier will violate PEP-8 and PEP-20.

Apart from that, there are certain rules we must follow to name one:

It can only begin with an underscore or a character from A-Z or a-z.

The rest of it can contain anything from the following: A-Z/a-z/_/0-9.

Python is case-sensitive, as we discussed in the previous question.

Keywords cannot be used as identifiers. Python has the following keywords:

and def False import not True


as del finally in or try
assert elif for is pass while
break else from lambda print with
class except global None raise yield
continue exec if nonlocal return

Learn everything about Python Identifiers

Q.7. How would you convert a string into lowercase?

We use the lower() method for this.

>>> 'AyuShi'.lower()

‘ayushi’

To convert it into uppercase, then, we use upper().

>>> 'AyuShi'.upper()

‘AYUSHI’

Also, to check if a string is in all uppercase or all lowercase, we use the methods isupper() and islower().
>>> 'AyuShi'.isupper()

False

>>> 'AYUSHI'.isupper()

True

>>> 'ayushi'.islower()

True

>>> '@yu$hi'.islower()

True

>>> '@YU$HI'.isupper()

True

So, characters like @ and $ will suffice for both cases

Also, istitle() will tell us if a string is in title case.

>>> 'The Corpse Bride'.istitle()

True

Q.8. What is the pass statement in Python?

There may be times in our code when we haven’t decided what to do yet, but we must type something for it to
be syntactically correct. In such a case, we use the pass statement.

>>> def func(*args):

pass

>>>

Similarly, the break statement breaks out of a loop.

>>> for i in range(7):

if i==3: break

print(i)

2
Finally, the continue statement skips to the next iteration.

>>> for i in range(7):

if i==3: continue

print(i)

Hope you have read all the basic Python Interview Questions and Answers. Now, let’s move towards the second
part of the blog – Most asked Python Interview Questions and Answers for freshers

Frequently Asked Python Interview Questions and Answers for Freshers


While solving or answering these questions, if you feel any difficulty, comment us. DataFlair is always ready to
help you.

Q.9. Explain help() and dir() functions in Python.

The help() function displays the documentation string and help for its argument.

>>> import copy

>>> help(copy.copy)

Help on function copy in module copy:

copy(x)

Shallow copy operation on arbitrary Python objects.

See the module’s doc__ string for more info.

The dir() function displays all the members of an object(any kind).

>>> dir(copy.copy)

[‘ annotations ’, ‘ call ’, ‘ class ’, ‘ closure ’, ‘ code ’, ‘ defaults ’, ‘ delattr ’,


‘ dict ’, ‘ dir ’, ‘__doc ’, ‘ eq ’, ‘ format ’, ‘ ge ’, ‘ get ’, ‘ getattribute ’,
‘ globals ’, ‘ gt ’, ‘ hash ’, ‘ init ’, ‘ init_subclass ’, ‘ kwdefaults ’, ‘ le ’, ‘ lt ’,
‘ module ’, ‘ name ’, ‘ ne ’, ‘ new ’, ‘ qualname ’, ‘ reduce ’, ‘ reduce_ex ’,
‘ repr ’, ‘ setattr ’, ‘ sizeof ’, ‘ str ’, ‘ subclasshook ’]
Q.10. How do you get a list of all the keys in a dictionary?

Be specific in these type of Python Interview Questions and Answers.

For this, we use the function keys().

>>> mydict={'a':1,'b':2,'c':3,'e':5}

>>> mydict.keys()

dict_keys([‘a’, ‘b’, ‘c’, ‘e’])

Wait!! Have you developed any Python Project yet?

Practice with Top Python Projects with source code and become job-ready

Q.11. What is slicing?

Slicing is a technique that allows us to retrieve only a part of a list, tuple, or string. For this, we use the slicing
operator [].

>>> (1,2,3,4,5)[2:4]

(3, 4)

>>> [7,6,8,5,9][2:]

[8, 5, 9]

>>> 'Hello'[:-1]

‘Hell’

Q.12. How would you declare a comment in Python?

Unlike languages like C++, Python does not have multiline comments. All it has is octothorpe (#). Anything
following a hash is considered a comment, and the interpreter ignores it.

>>> #line 1 of comment

>>> #line 2 of comment

In fact, you can place a comment anywhere in your code. You can use it to explain your code.

Q.13. How will you check if all characters in a string are alphanumeric?

For this, we use the method isalnum().

Q.14. How will you capitalize the first letter of a string?

Simply using the method capitalize().


>>> 'ayushi'.capitalize()

‘Ayushi’

>>> type(str.capitalize)

<class ‘method_descriptor’>

However, it will let other characters be.

>>> '@yushi'.capitalize()

‘@yushi’

>>> 'Ayushi123'.isalnum()

True

>>> 'Ayushi123!'.isalnum()

False

Other methods that we have include:

>>> '123.3'.isdigit()

False

>>> '123'.isnumeric()

True

>>> 'ayushi'.islower()

True

>>> 'Ayushi'.isupper()

False

>>> 'Ayushi'.istitle()

True

>>> ' '.isspace()

True

>>> '123F'.isdecimal()

False
Q.15. We know Python is all the rage these days. But to be truly accepting of a great technology, you
must know its pitfalls as well. Would you like to talk about this?

Of course. To be truly yourself, you must be accepting of your flaws. Only then can you move forward to work
on them. Python has its flaws too:

Python’s interpreted nature imposes a speed penalty on it.

While Python is great for a lot of things, it is weak in mobile computing, and in browsers.

Being dynamically-typed, Python uses duck-typing (If it looks like a duck, it must be a duck). This can raise
runtime errors.

Python has underdeveloped database access layers. This renders it a less-than-perfect choice for huge
database applications.

And then, well, of course. Being easy makes it addictive. Once a Python-coder, always a Python coder.

Q.16. With Python, how do you find out which directory you are currently in?

To find this, we use the function/method getcwd(). We import it from the module os.

>>> import os

>>> os.getcwd()

‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32’

>>> type(os.getcwd)

<class ‘builtin_function_or_method’>

We can also change the current working directory with chdir().

>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> os.getcwd()

‘C:\\Users\\lifei\\Desktop’

Q.17. How do you insert an object at a given index in Python?

Let’s build a list first.

>>> a=[1,2,4]

Now, we use the method insert. The first argument is the index at which to insert, the second is the value to
insert.

>>> a.insert(2,3)

>>> a

[1, 2, 3, 4]

Q.18. And how do you reverse a list?

Using the reverse() method.

>>> a.reverse()

>>> a

[4, 3, 2, 1]

You can also do it via slicing from right to left:

>>> a[::-1]

>>> a

[1, 2, 3, 4]

This gives us the original list because we already reversed it once. However, this does not modify the original
list to reverse it.

Q.19. What is the Python interpreter prompt?

This is the following sign for Python Interpreter:

>>>

If you have worked with the IDLE, you will see this prompt.

Q.20. How does a function return values?

A function uses the ‘return’ keyword to return a value. Take a look:


>>> def add(a,b):

return a+b

Q.21. How would you define a block in Python?

For any kind of statements, we possibly need to define a block of code under them. However, Python does not
support curly braces. This means we must end such statements with colons and then indent the blocks under
those with the same amount.

>>> if 3>1:

print("Hello")

print("Goodbye")

Hello

Goodbye

Q.22. Why do we need break and continue in Python?

Both break and continue are statements that control flow in Python loops. break stops the current loop from
executing further and transfers the control to the next block. continue jumps to the next iteration of the loop
without exhausting it.

Q.23. Will the do-while loop work if you don’t end it with a semicolon?

Trick question! Python does not support an intrinsic do-while loop. Secondly, to terminate do-while loops is a
necessity for languages like C++.

Q.24. In one line, show us how you’ll get the max alphabetical character from a string.

For this, we’ll simply use the max function.

>>> max('flyiNg')

‘y’

The following are the ASCII values for all the letters of this string-

f- 102

l- 108

y- 121

i- 105

N- 78
g- 103

By this logic, try to explain the following line of code-

>>> max('fly{}iNg')

‘}’

(Bonus: } – 125)

Q.25. What is Python good for?

Python is a jack of many trades, check out Applications of Python to find out more.

Meanwhile, we’ll say we can use it for:

Web and Internet Development

Desktop GUI

Scientific and Numeric Applications

Software Development Applications

Applications in Education

Applications in Business

Database Access

Network Programming

Games, 3D Graphics

Other Python Applications

Q.26. Can you name ten built-in functions in Python and explain each in brief?

Ten Built-in Functions, you say? Okay, here you go.

complex()- Creates a complex number.

>>> complex(3.5,4)

(3.5+4j)

eval()- Parses a string as an expression.

>>> eval('print(max(22,22.0)-min(2,3))')

20
filter()- Filters in items for which the condition is true.

>>> list(filter(lambda x:x%2==0,[1,2,0,False]))

[2, 0, False]

format()- Lets us format a string.

>>> print("a={0} but b={1}".format(a,b))

a=2 but b=3

hash()- Returns the hash value of an object.

>>> hash(3.7)

644245917

hex()- Converts an integer to a hexadecimal.

>>> hex(14)

‘0xe’

input()- Reads and returns a line of string.

>>> input('Enter a number')

Enter a number7

‘7’

len()- Returns the length of an object.

>>> len('Ayushi')

locals()- Returns a dictionary of the current local symbol table.

>>> locals()

{‘ name ’: ‘ main__’, ‘ doc ’: None, ‘ package ’: None, ‘ loader ’: <class


‘_frozen_importlib.BuiltinImporter’>, ‘ spec__’: None, ‘ annotations ’: {}, ‘ builtins ’: <module
‘builtins’ (built-in)>, ‘a’: 2, ‘b’: 3}

open()- Opens a file.

>>> file=open('tabs.txt')

Q.27. What will the following code output?


>>> word=’abcdefghij’

>>> word[:3]+word[3:]

The output is ‘abcdefghij’. The first slice gives us ‘abc’, the next gives us ‘defghij’.

Q.28. How will you convert a list into a string?

We will use the join() method for this.

>>> nums=['one','two','three','four','five','six','seven']

>>> s=' '.join(nums)

>>> s

‘one two three four five six seven’

Q.29. How will you remove a duplicate element from a list?

We can turn it into a set to do that.

>>> list=[1,2,1,3,4,2]

>>> set(list)

{1, 2, 3, 4}

Q.30. Can you explain the life cycle of a thread?

To create a thread, we create a class that we make override the run method of the thread class. Then, we
instantiate it.
A thread that we just created is in the new state. When we make a call to start() on it, it forwards the threads
for scheduling. These are in the ready state.

When execution begins, the thread is in the running state.

Calls to methods like sleep() and join() make a thread wait. Such a thread is in the waiting/blocked state.

When a thread is done waiting or executing, other waiting threads are sent for scheduling.

A running thread that is done executing terminates and is in the dead state.

Basic Python Program Interview Questions and Answers


Q.31. What is a dictionary in Python?

A python dictionary is something I have never seen in other languages like C++ or Java programming. It holds
key-value pairs.

>>> roots={25:5,16:4,9:3,4:2,1:1}

>>> type(roots)

<class ‘dict’>

>>> roots[9]

A dictionary is mutable, and we can also use a comprehension to create it.

>>> roots={x**2 for x in range(5,0,-1)}

>>> roots

{25: 5, 16: 4, 9: 3, 4: 2, 1: 1}

Q.32. Explain the //, %, and ** operators in Python.

The // operator performs floor division. It will return the integer part of the result on division.

>>> 7//2

Normal division would return 3.5 here.

Similarly, ** performs exponentiation. a**b returns the value of a raised to the power b.

>>> 2**10
1024

Finally, % is for modulus. This gives us the value left after the highest achievable division.

>>> 13%7

>>> 3.5%1.5

0.5

Q.33. What do you know about relational operators in Python.

Relational operators compare values.

Less than (<) If the value on the left is lesser, it returns True.

>>> 'hi'<'Hi'

False
Greater than (>) If the value on the left is greater, it returns True.

>>> 1.1+2.2>3.3

True

This is because of the flawed floating-point arithmetic in Python, due to hardware dependencies.

Less than or equal to (<=) If the value on the left is lesser than or equal to, it returns True.

>>> 3.0<=3

True
Greater than or equal to (>=) If the value on the left is greater than or equal to, it returns True.

>>> True>=False

True

Equal to (==) If the two values are equal, it returns True.

>>> {1,3,2,2}=={1,2,3}

True

Not equal to (!=) If the two values are unequal, it returns True.

>>> True!=0.1

True

>>> False!=0.1

True

You will surely face a question from Python Operators. There are chances that question may be in an
indirect way. Prepare yourself for it with the best guide – Python Operators

Q.34. What are assignment operators in Python?

We can combine all arithmetic operators with the assignment symbol.

>>> a=7

>>> a+=1
>>> a

>>> a-=1

>>> a

>>> a*=2

>>> a

14

>>> a/=2

>>> a

7.0

>>> a**=2

>>> a

49.0

>>> a//=3

>>> a

16.0

>>> a%=4

>>> a

0.0

Q.35. Explain logical operators in Python.

We have three logical operators- and, or, not.

>>> False and True

False

>>> 7<7 or True

True
>>> not 2==2

False

Q.36. What are membership operators?

With the operators ‘in’ and ‘not in’, we can confirm if a value is a member in another.

>>> 'me' in 'disappointment'

True

>>> 'us' not in 'disappointment'

True

Q.37. Explain identity operators in Python.

The operators ‘is’ and ‘is not’ tell us if two values have the same identity.

>>> 10 is '10'

False

>>> True is not False

True

Q.38. Finally, tell us about bitwise operators in Python.

These operate on values bit by bit.

AND (&) This performs & on each bit pair.

>>> 0b110 & 0b010


2

OR (|) This performs | on each bit pair.

>>> 3|2

XOR (^) This performs an exclusive-OR operation on each bit pair.

>>> 3^2

Binary One’s Complement (~) This returns the one’s complement of a value.

>>> ~2

-3

Binary Left-Shift (<<) This shifts the bits to the left by the specified amount.

>>> 1<<2

Here, 001 was shifted to the left by two places to get 100, which is binary for 4.

Binary Right-Shift (>>)

>>> 4>>2

Q.39. What data types does Python support?

Python provides us with five kinds of data types:

Numbers – Numbers use to hold numerical values.

>>> a=7.0

>>>

Strings – A string is a sequence of characters. We declare it using single or double quotes.

>>> title="Ayushi's Book"

Lists – A list is an ordered collection of values, and we declare it using square brackets.

>>> colors=['red','green','blue']
>>> type(colors)

<class ‘list’>

Tuples – A tuple, like a list, is an ordered collection of values. The difference. However, is that a tuple is
immutable. This means that we cannot change a value in it.

>>> name=('Ayushi','Sharma')

>>> name[0]='Avery'

Traceback (most recent call last):

File “<pyshell#129>”, line 1, in <module>

name[0]=’Avery’

TypeError: ‘tuple’ object does not support item assignment

Dictionary – A dictionary is a data structure that holds key-value pairs. We declare it using curly braces.

>>> squares={1:1,2:4,3:9,4:16,5:25}

>>> type(squares)

<class ‘dict’>

>>> type({})

<class ‘dict’>

We can also use a dictionary comprehension:

>>> squares={x:x**2 for x in range(1,6)}

>>> squares

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Don’t miss the complete guide for Python Data Types and Variables

Q.40. What is a docstring?

A docstring is a documentation string that we use to explain what a construct does. We place it as the first thing
under a function, class, or a method, to describe what it does. We declare a docstring using three sets of single
or double-quotes.

>>> def sayhi():

"""
The function prints Hi

"""

print("Hi")

>>> sayhi()

Hi

To get a function’s docstring, we use its doc attribute.

>>> sayhi. doc

‘\n\tThis function prints Hi\n\t’

A docstring, unlike a comment, is retained at runtime.

Q.41. How would you convert a string into an int in Python?

If a string contains only numerical characters, you can convert it into an integer using the int() function.

>>> int('227')

227

Let’s check the types:

>>> type('227')

<class ‘str’>

>>> type(int('227'))

<class ‘int’>

Q.42. How do you take input in Python?

For taking input from the user, we have the function input(). In Python 2, we had another function raw_input().

The input() function takes, as an argument, the text to be displayed for the task:

>>> a=input('Enter a number')

Enter a number7

But if you have paid attention, you know that it takes input in the form of a string.

>>> type(a)

<class ‘str’>
Multiplying this by 2 gives us this:

>>> a*=2

>>> a

’77’

So, what if we need to work on an integer instead?

We use the int() function for this.

>>> a=int(input('Enter a number'))

Enter a number7

Now when we multiply it by 2, we get this:

>>> a*=2

>>> a

14

Q.43. What is a function?

When we want to execute a sequence of statements, we can give it a name. Let’s define a function to take two
numbers and return the greater number.

>>> def greater(a,b):

return a is a>b else b

>>> greater(3,3.5)

3.5

Q.44. What is recursion?

When a function makes a call to itself, it is termed recursion. But then, in order for it to avoid forming an
infinite loop, we must have a base condition.

Let’s take an example.

>>> def facto(n):

if n==1: return 1

return n*facto(n-1)

>>> facto(4)
24

Q.45. What does the function zip() do?

One of the less common functions with beginners, zip() returns an iterator of tuples.

>>> list(zip(['a','b','c'],[1,2,3]))

[(‘a’, 1), (‘b’, 2), (‘c’, 3)]

Here, it pairs items from the two lists and creates tuples with those. But it doesn’t have to be lists.

>>> list(zip(('a','b','c'),(1,2,3)))

[(‘a’, 1), (‘b’, 2), (‘c’, 3)]

Q.46. How do you calculate the length of a string?

This is simple. We call the function len() on the string we want to calculate the length of.

>>> len('Ayushi Sharma')

13

Q.47. Explain Python List Comprehension.

The list comprehension in python is a way to declare a list in one line of code. Let’s take a look at one such
example.

>>> [i for i in range(1,11,2)]

[1, 3, 5, 7, 9]

>>> [i*2 for i in range(1,11,2)]

[2, 6, 10, 14, 18]

Q.48. How do you get all values from a Python dictionary?

We saw previously, to get all keys from a dictionary, we make a call to the keys() method. Similarly, for values,
we use the method values().

>>> 'd' in {'a':1,'b':2,'c':3,'d':4}.values()

False

>>> 4 in {'a':1,'b':2,'c':3,'d':4}.values()

True

Q.49. What if you want to toggle case for a Python string?


We have the swapcase() method from the str class to do just that.

>>> 'AyuShi'.swapcase()

‘aYUsHI’

Let’s apply some concepts now, shall we? Questions 50 through 52 assume the string ‘I love Python’. You need
to do the needful.

Q.50. Write code to print only upto the letter t.

>>> i=0

>>> while s[i]!='t':

print(s[i],end=’’)

i+=1

I love Py

Q.51. Write code to print everything in the string except the spaces.

>>> for i in s:

if i==' ': continue

print(i,end='')

IlovePython

Q.52. Now, print this string five times in a row.

>>> for i in range(6):

print(s)

I love Python

I love Python

I love Python

I love Python

I love Python

I love Python

Okay, moving on to more domains to conquer.


Q.53. What is the purpose of bytes() in Python?

bytes() is a built-in function in Python that returns an immutable bytes object. Let’s take an example.

>>> bytes([2,4,8])

b’\x02\x04\x08′

>>> bytes(5)

b’\x00\x00\x00\x00\x00′

>>> bytes('world','utf-8')

b’world’

Q.54. What is a control flow statement?

A Python program usually starts to execute from the first line. From there, it moves through each statement just
once and as soon as it’s done with the last statement, it transactions the program. However, sometimes, we may
want to take a more twisted path through the code. Control flow statements let us disturb the normal execution
flow of a program and bend it to our will.

Q.55. Create a new list to convert the following list of number strings to a list of

numbers. nums=[‘22’,’68’,’110’,’89’,’31’,’12’]

We will use the int() function with a list comprehension to convert these strings into integers and put them in a
list.

>>> [int(i) for i in nums]

[22, 68, 110, 89, 31, 12]

Q.56. Given the first and last names of all employees in your firm, what data type will you use to store it?

I can use a dictionary to store that. It would be something like this-

{‘first_name’:’Ayushi’,’second_name’:’Sharma’

Top Python Interview Questions and Answers


Q.57. How would you work with numbers other than those in the decimal number system?

With Python, it is possible to type numbers in binary, octal, and hexadecimal.

Binary numbers are made of 0 and 1. To type in binary, we use the prefix 0b or 0B.

>>> int(0b1010)

10
To convert a number into its binary form, we use bin().

>>> bin(0xf)

‘0b1111’

Octal numbers may have digits from 0 to 7. We use the prefix 0o or 0O.

>>> oct(8)

‘0o10’

Hexadecimal numbers may have digits from 0 to 15. We use the prefix 0x or 0X.

>>> hex(16)

‘0x10’

>>> hex(15)

‘0xf’

DataFlair’s latest article on Python Numbers with Examples

Q.58. What does the following code output?

>>> def extendList(val, list=[]):

list.append(val)

return list

>>> list1 = extendList(10)

>>> list2 = extendList(123,[])

>>> list3 = extendList('a')

>>> list1,list2,list3

([10, ‘a’], [123], [10, ‘a’])

You’d expect the output to be something like this:

([10],[123],[‘a’])

Well, this is because the list argument does not initialize to its default value ([]) every time we make a call to
the function. Once we define the function, it creates a new list. Then, whenever we call it again without a list
argument, it uses the same list. This is because it calculates the expressions in the default arguments when we
define the function, not when we call it.
Q.59. How many arguments can the range() function take?

The range() function in Python can take up to 3 arguments. Let’s see this one by one.

a. One argument

When we pass only one argument, it takes it as the stop value. Here, the start value is 0, and the step value is +1.

>>> list(range(5))

[0, 1, 2, 3, 4]

>>> list(range(-5))

[]

>>> list(range(0))

[]

b. Two arguments

When we pass two arguments, the first one is the start value, and the second is the stop value.

>>> list(range(2,7))

[2, 3, 4, 5, 6]

>>> list(range(7,2))

[]

>>> list(range(-3,4))

[-3, -2, -1, 0, 1, 2, 3]

c. Three arguments

Here, the first argument is the start value, the second is the stop value, and the third is the step value.

>>> list(range(2,9,2))

[2, 4, 6, 8]

>>> list(range(9,2,-1))

[9, 8, 7, 6, 5, 4, 3]

Q.60. What is PEP 8?


PEP 8 is a coding convention that lets us write more readable code. In other words, it is a set of
recommendations.

Q.61. How is Python different from Java?

Following is the comparison of Python vs Java –

Java is faster than Python

Python mandates indentation. Java needs braces.

Python is dynamically-typed; Java is statically typed.

Python is simple and concise; Java is verbose

Python is interpreted

Java is platform-independent

Java has stronger database-access with JDBC

Python vs Java, the most commonly asked python interview question for freshers.

Learn it in detail – Python vs Java for Interview

Q.62. What is the best code you can write to swap two numbers?

I can perform the swapping in one statement.

>>> a,b=b,a

Here’s the entire code, though-


>>> a,b=2,3

>>> a,b=b,a

>>> a,b

(3, 2)

Q.63. How can you declare multiple assignments in one statement?

This is one of the most asked interview questions for Python freshers –

There are two ways to do this:

First –

>>> a,b,c=3,4,5 #This assigns 3, 4, and 5 to a, b, and c respectively

Second –

>>> a=b=c=3 #This assigns 3 to a, b, and c

Q.64. If you are ever stuck in an infinite loop, how will you break out of it?

For this, we press Ctrl+C. This interrupts the execution. Let’s create an infinite loop to demonstrate this.

>>> def counterfunc(n):

while(n==7):print(n)

>>> counterfunc(7)

7
7

Traceback (most recent call last):

File “<pyshell#332>”, line 1, in <module>

counterfunc(7)

File “<pyshell#331>”, line 2, in counterfunc

while(n==7):print(n)

KeyboardInterrupt

Technical Python Interview Questions and Answers


Q.65. How do we execute Python?

Python files first compile to bytecode. Then, the host executes them.

Revise the concept of Python Compiler

Q.66. Explain Python’s parameter-passing mechanism.

To pass its parameters to a function, Python uses pass-by-reference. If you change a parameter within a
function, the change reflects in the calling function. This is its default behavior. However, when we pass literal
arguments like strings, numbers, or tuples, they pass by value. This is because they are immutable.

Q.67. What is the with statement in Python?

The with statement in Python ensures that cleanup code is executed when working with unmanaged resources
by encapsulating common preparation and cleanup tasks. It may be used to open a file, do something, and then
automatically close the file at the end. It may be used to open a database connection, do some processing, then
automatically close the connection to ensure resources are closed and available for others. with will cleanup the
resources even if an exception is thrown. This statement is like the using statement in C#.
Consider you put some code in a try block, then in the finally block, you close any resources used. The with
statement is like syntactic sugar for that.

The syntax of this control-flow structure is:


with expression [as variable]:
….with-block

>>> with open('data.txt') as data:

#processing statements

Q.68. How is a .pyc file different from a .py file?

While both files hold bytecode, .pyc is the compiled version of a Python file. It has platform-independent
bytecode. Hence, we can execute it on any platform that supports the .pyc format. Python automatically
generates it to improve performance(in terms of load time, not speed).

Python OOPS Interview Questions and Answers


Q.69. What makes Python object-oriented?

Again the frequently asked Python Interview Question

Python is object-oriented because it follows the Object-Oriented programming paradigm. This is a paradigm
that revolves around classes and their instances (objects). With this kind of programming, we have the
following features:

Encapsulation

Abstraction

Inheritance

Polymorphism

Data hiding

Q.70. How many types of objects does Python support?

Objects in Python are mutable and immutable. Let’s talk about these.

Immutable objects- Those which do not let us modify their contents. Examples of these will be tuples,
booleans, strings, integers, floats, and complexes. Iterations on such objects are faster.

>>> tuple=(1,2,4)

>>> tuple

(1, 2, 4)

>>> 2+4j

(2+4j)
Mutable objects – Those that let you modify their contents. Examples of these are lists, sets, and dicts.
Iterations on such objects are slower.

>>> [2,4,9]

[2, 4, 9]

>>> dict1={1:1,2:2}

>>> dict1

{1: 1, 2: 2}

While two equal immutable objects’ reference variables share the same address, it is possible to create two
mutable objects with the same content.

Open-ended Python Interview Questions


Q.71 Why do you want to work for this company?

Q.72 Where do you see yourself in 10 years?

Q.73 What will you bring to the table if we hire you?

Q.74 Tell me about your best personal project. What challenges did you face, and how did it change the way
you work?

Q.75 Would you have a problem with menial tasks?

Q.76 What makes you like Python over other languages? (The most commonly asked Python interview
questions)

So, these were some of the important Python Interview Questions and Answers. If you practiced all the above
questions then you are ready to move towards the next part of DataFlair’s Python Interview Questions and
Answers Series – Part 2 for Python Interviews. I advise you not to miss a single part of this series. All these
questions are specifically designed by experienced individuals to provide you with complete help for cracking
your next interview.

Start applying for the next Python Interview, you are going to crack it.

Python Programming Interview Questions [2019] – Crack your


Coding Interviews
by DataFlair Team · September 27, 2019

Python Programming Interview Questions and Answers – Prepare with DataFlair’s Python Interview Series

Python Coding Interview Questions for Freshers – Part 1

Python Coding Interview Questions for Experienced – Part 2


This is the second part of our Python Programming Interview Questions and Answers Series, soon we will
publish more. Like our other parts of python programming interview questions, this part is also divided into
further subcategories. The reason we are doing is so you can practice all type of Python Interview Questions
along with their answers. Here is how I divided these Python coding interview questions for experienced –

Python Project Related Interview Questions

Top Python Programming Interview Questions

Technical Python Programming Interview Questions

Python Library Interview Questions

Python Developer Interview Questions

Python Data Science Interview Questions

Advanced Python Programming Interview Questions

Python OOPS Interview Questions

Python Programming Interview Questions for Testers

Python Framework Interview Questions

Frequently asked Python coding Interview Questions

So, without wasting any time let’s start with our first category that is Python project-related Interview Questions
and Answers.

Python Project Related-Interview Questions and Answers


If you are experienced then surely you have worked on projects and this is the only tool which can drive the
interview on your side. Let’s explore the most important python interview questions for experienced.

Q.1. What kinds of classifiers did you use for your projects?

An MLPClassifier is a Multi-Layer Perceptron Classifier. It optimizes the log-loss function using the LBFGS or
stochastic gradient descent. It is a feedforward ANN model. This is different to the SVM or Naive Bayes
algorithms in that it has an internal neural network for classification. Along with the librosa and soundfile
libraries, I used the MLPClassifier from the sklearn library to perform speech emotion recognition with
Python.

I used a softmax classifier to detect the presence of the IDC (Invasive Ductal Carcinoma) breast cancer. This
classifier gets the probabilities for each class label. This is very commonly used as is SVM. The softmax
classifier is a generalization of the binary logistic regression to multiple classes.

The XGBClassifier is the XGBoost model for classification. I used it to classify between healthy patients and
those with Parkinson’s Disease- it uses eXtreme Gradient Boosting and falls under the category of Ensemble
Learning in ML. Training and predicting using many models, it produces one superior output.
To detect fake news with Python, I used the PassiveAggressiveClassifier. Such online learning algorithms that
stays passive for a correct classification outcome, but turns aggressive if there is a miscalculation. This lets it
update and adjust. Such an algorithm does not converge.

Q.2. Why did you choose OpenCV for your project on gender and age detection? What helped you
make the decision?

OpenCV is a very popular library for computer vision, and is powerful too. I chose it for my project on
gender and age detection because it had the tools required for my project. It had the following benefits:

Free

Fast and written in C/C++

Low RAM usage (MATLAB needs a lot of resources)

Portable

When I had to choose between OpenCV and MATLAB, however, the latter had some advantages over the
former:

Since OpenCV has a flann library of its own, it can create conflict when using OpenCV with PCL

There can be memory leaks]

OpenCV does not have an IDE like MATLAB does

In the end, I found OpenCV to be better suited for my project, and chose to go ahead with it.

Q.3. What ratio did you choose to divide your dataset into training and testing sets?

A classifier needs us to divide a dataset into training and testing sets. The ratio is very important because it can
affect the accuracy of the model. If we have few training data, the parameter estimates will have greater
variance. And if we have a few test data, the performance statistic will have greater variance. Depending on the
purpose and the size of the dataset, we usually split the dataset into 60% to 80% for training, and 40% to 20%
for testing.

For most of the projects, I used a ratio of 80:20. However, for the project on breast cancer classification, I used
10% of that 80% of the training set for validation. A model’s performance can depend on the values of its
parameters. I used the training set to train the model and the validation set to evaluate the performance of the
model for different combinations of hyperparameter values so I could figure out which one was best.

Q.4. Why did you use config.py for this project on breast cancer classification?

I created a config file for this project to keep every setting at one place. This made it easy to change the settings
once and for all. This held the path to the input dataset, and the base path. It also held the relative paths to the
training, validation, and testing sets. So if the base path was datasets/idc, the training set path was
datasets/idc/training. It also mentioned the relative sizes of the training and validation sets, which were 0.8 and
0.1 respectively.

Q.5. How was your experience with the XGBClassifier?


My favorite thing about XGBoost is that it is scalable. I used it for the project on detecting Parkinson’s
Disease. It is also easy and efficient, and delivers high performance and accuracy compared to some other
algorithms. In the multiple projects that I’ve tried it for, I found it delivered some of the best results. It is fast
and has a Python API that makes its installation easier. It lets me access sparse datasets and is versatile. One
thing I didn’t like about it is its documentation. It wasn’t very clear with its APIs and examples.

Q.6 What challenges did you face in your best Python projects?

Which algorithm to use was a challenge to figure out. Since the algorithm you use can affect your model’s
accuracy, it is always a task to decide what algorithms and concepts to use together. Also, managing the
changes to the code was tough, and that taught me the importance of version control. It was also important to
make my code readable and easily modifiable.

Top Python Programming Interview Questions


Q.7. What do you know about palindromes? Can you implement one in Python?

A palindrome is a phrase, a word, or a sequence that reads the same forward and backward. One such example
will be pip! An example of such a phrase will be ‘nurses run’. Let’s implement it, shall we?

>>> def isPalindrome(string):

left,right=0,len(string)-1

while right>=left:

if not string[left]==string[right]:

return False

left+=1;right-=1

return True

>>> isPalindrome('redrum murder')

True

>>> isPalindrome('CC.')

False

Well, there are other ways to do this too. Let’s try using an iterator.

>>> def isPalindrome(string):

left,right=iter(string),iter(string[::-1])

i=0

while i<len(string)/2:
if next(left)!=next(right):

return False

i+=1

return True

>>> isPalindrome('redrum murder')

True

>>> isPalindrome('CC.')

False

>>> isPalindrome('CCC.')

False

>>> isPalindrome('CCC')

True

Q.8. What do you mean by *args and **kwargs?

In cases when we don’t know how many arguments will be passed to a function, like when we want to pass a
list or a tuple of values, we use *args.

>>> def func(*args):

for i in args:

print(i)

>>> func(3,2,1,4,7)

3
2
1
4
7

**kwargs takes keyword arguments when we don’t know how many there will be.

>>> def func(**kwargs):

for i in kwargs:

print(i,kwargs[i])
>>> func(a=1,b=2,c=7)

a.1
b.2
c.7
The words args and kwargs are a convention, and we can use anything in their place.

Q.9. What is a closure in Python?

A closure in Python is said to occur when a nested function references a value in its enclosing scope. The
whole point here is that it remembers the value.

>>> def A(x):

def B():

print(x)

return B

>>> A(7)()

Q.10. Are these statements optimal? If not, optimize them.

word=’word’

print(word. len ())

No, these are not optimal. Let’s check the manual for this.

>>> help(str. len )

Help on wrapper_descriptor:

len (self, /)

Return len(self).

len is a wrapper descriptor which in turn makes a call to len(). So why not skip the work and do just that
instead?

The optimal solution:

>>> word='word'

>>> len(word)

4
Q.11. What is the iterator protocol?

The iterator protocol for Python declares that we must make use of two functions to build an iterator- iter() and
next().

iter()- To create an iterator

next()- To iterate to the next element

>>> a=iter([2,4,6,8,10])

>>> next(a)

>>> next(a)

>>> next(a)

>>> next(a)

>>> next(a)

10

>>> next(a)

Traceback (most recent call last):


File “<pyshell#31>”, line 1, in <module>
next(a)
StopIteration

Q.12. What is tuple unpacking?

Suppose we have a tuple nums=(1,2,3). We can unpack its values into the variables a, b, and c. Here’s how:

>>> nums=(1,2,3)

>>> a,b,c=nums

>>> a

>>> b
2

>>> c

Q.13. What will the following code output?

>>> a=1

>>> a,b=a+1,a+1

>>> a,b

The output is (2, 2). This code increments the value of a by 1 and assigns it to both a and b. This is because this
is a simultaneous declaration. The following code gives us the same:

>>> a=1

>>> b,a=a+1,a+1

>>> a,b

(2, 2)

Q.14. What is a frozen set in Python?

First, let’s discuss what a set is. A set is a collection of items, where there cannot be any duplicates. A set is
also unordered.

>>> myset={1,3,2,2}

>>> myset

{1, 2, 3}

This means that we cannot index it.

>>> myset[0]

Traceback (most recent call last):


File “<pyshell#197>”, line 1, in <module>
myset[0]
TypeError: ‘set’ object does not support indexing

However, a set is mutable. A frozen set is immutable. This means we cannot change its values. This also makes
it eligible to be used as a key for a dictionary.

>>> myset=frozenset([1,3,2,2])

>>> myset
frozenset({1, 2, 3})

>>> type(myset)

<class ‘frozenset’>

Before you start exploring the next category, do let me know (through comments) how many questions you
have answered by yourself from the above top python programming interview questions.

Python Technical Interview Questions


Q.15. When you exit Python, is all memory deallocated?

Exiting Python deallocates everything except:

modules with circular references

Objects referenced from global namespaces

Parts of memory reserved by the C library

Q.16. What is the Dogpile effect?

In case the cache expires, what happens when a client hits a website with multiple requests is what we call the
dogpile effect. To avoid this, we can use a semaphore lock. When the value expires, the first process acquires
the lock and then starts to generate the new value.

Q.17. Explain garbage collection with Python.

The following points are worth nothing for the garbage collector with CPython-

Python maintains a count of how many references there are to each object in memory

When a reference count drops to zero, it means the object is dead and Python can free the memory it
allocated to that object

The garbage collector looks for reference cycles and cleans them up

Python uses heuristics to speed up garbage collection

Recently created objects might as well be dead

The garbage collector assigns generations to each object as it is created

It deals with the younger generations first.

Q.18. How will you use Python to read a random line from a file?

We can borrow the choice() method from the random module for this.

>>> import random


>>> lines=open('tabs.txt').read().splitlines()

>>> random.choice(lines)

‘https://data-flair.training/blogs/category/python/’

Let’s restart the IDLE and do this again.

>>> import random

>>> lines=open('tabs.txt').read().splitlines()

>>> random.choice(lines)

‘https://data-flair.training/blogs/’

>>> random.choice(lines)

‘https://data-flair.training/blogs/category/python/’

>>> random.choice(lines)

‘https://data-flair.training/blogs/category/python/’

>>> random.choice(lines)

‘https://data-flair.training/blogs/category/python/’

Q.19. What is JSON? Describe in brief how you’d convert JSON data into Python data?

JSON stands for JavaScript Object Notation. It is a highly popular data format, and it stores data into NoSQL
databases. JSON is generally built on the following two structures:

A collection of <name,value> pairs

An ordered list of values.

Python supports JSON parsers. In fact, JSON-based data is internally represented as a dictionary in Python. To
convert JSON data into Python data, we use the load() function from the JSON module.

Q.20. Differentiate between split(), sub(), and subn() methods of the remodule.

The re module is what we have for processing regular expressions with Python. Let’s talk about the three
methods we mentioned-

split()- This makes use of a regex pattern to split a string into a list

sub()- This looks for all substrings where the regex pattern matches, and replaces them with a different
string

subn()- Like sub(), this returns the new string and the number of replacements made
Q.21. How would you display a file’s contents in reversed order?

Let’s first get to the Desktop. We use the chdir() function/method form the os module for this.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

The file we’ll use for this is Today.txt, and it has the following contents:

OS, DBMS, DS, ADA

HTML, CSS, jQuery, JavaScript

Python, C++, Java

This sem’s subjects

Debugger

itertools

container

Let’s read the contents into a list, and then call reversed() on it:

>>> for line in reversed(list(open('Today.txt'))):

print(line.rstrip())

container
itertools
Debugger

This sem’s subjects

Python, C++, Java


HTML, CSS, jQuery, JavaScript
OS, DBMS, DS, ADA

Without the rstrip(), we would get blank lines between the output.

Q.22. Whenever you exit Python, is all memory de-allocated?

The answer here is no. The modules with circular references to other objects, or to objects referenced from
global namespaces, aren’t always freed on exiting Python.

Plus, it is impossible to de-allocate portions of memory reserved by the C library.

Before you begin to answer more Python Interview Questions, I recommend you to save the information
for Python Career Opportunities
Python Programming Library Interview Questions
Q.23. Can I dynamically load a module in Python?

Dynamic loading is where we do not load a module till we need it. This is slow, but lets us utilize the memory
more efficiently. In Python, you can use the importlib module for this:

import importlib

module = importlib.import_module('my_package.my_module')

Q.24. What is speech_recognition? Does this ship with Python by default?

Speech_recognition is a library for performing the task of recognizing speech with Python. This forms an
integral part of AI. No, this does not ship with Python by default. We must download it from the PyPI and
install it manually using pip.

Score High in Interview – Don’t forget to practice Speech Emotion Recognition Python Project with Source
Code

Q.25. How would you generate a random number in Python?

To generate a random number, we import the function random() from the module random.

>>> from random import random

>>> random()

0.7931961644126482

Let’s call for help on this.

>>> help(random)

Help on built-in function random:

random(…) method of random.Random instance

random() -> x in the interval [0, 1).

This means that it will return a random number equal to or greater than 0, and less than 1.

We can also use the function randint(). It takes two arguments to indicate a range from which to return a
random integer.

>>> from random import randint

>>> randint(2,7)

6
>>> randint(2,7)

>>> randint(2,7)

>>> randint(2,7)

>>> randint(2,7)

Q.26. How will you locally save an image using its URL address?

For this, we use the urllib module.

>>> import urllib.request

>>> urllib.request.urlretrieve('https://yt3.ggpht.com/a-
/ACSszfE2YYTfvXCIVk4NjJdDfFSkSVrLBlalZwYsoA=s900-mo-c-c0xffffffff-rj-k-no','dataflair.jpg')

(‘dataflair.jpg’, <http.client.HTTPMessage object at 0x02E90770>)

You can then get to your Python’s location and confirm this.

Now, moving towards the next category that is Python Programming Interview Questions and Answers for
Developers.

Python Programming Interview Questions for Developer


Q.27. Optionally, what statements can you put under a try-except block?

We have two of those:

else- To run a piece of code when the try-block doesn’t create an exception.

finally- To execute some piece of code regardless of whether there is an exception.

>>> try:

print("Hello")

except:

print("Sorry")

else:
print("Oh then")

finally:

print("Bye")

Hello
Oh then
Bye

Q.28. Can you explain the filter(), map(), and reduce() functions?

Let’s see these Python Functions.

filter()- This function lets us keep the values that satisfy some conditional logic. Let’s take an example.

>>> set(filter(lambda x:x>4, range(7)))

{5, 6}

This filters in the elements from 0 to 6 that are greater than the number 4.

map()- This function applies a function to each element in the iterable.

>>> set(map(lambda x:x**3, range(7)))

{0, 1, 64, 8, 216, 27, 125}

This calculates the cube for each element in the range 0 to 6 and stores them in a set.

reduce()- This function reduces a sequence pair-wise, repeatedly until we arrive at a single value.

>>> reduce(lambda x,y:y-x, [1,2,3,4,5])

Let’s understand this:

2-1=1
3-1=2
4-2=2
5-2=3

Hence, 3.

Q.29. How will you share global variables across modules?

To do this for modules within a single program, we create a special module, then import the config module in
all modules of our application. This lets the module be global to all modules.

Learn Python Modules in detail


Q.30. List some pdb commands.

Some pdb commands include-

<b> — Add breakpoint

<c> — Resume execution

<s> — Debug step by step

<n> — Move to next line

<l> — List source code

<p> — Print an expression

Q.31. What command do we use to debug a Python program?

To start debugging, we first open the command prompt and get to the location the file is at.

Microsoft Windows [Version 10.0.16299.248]

(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\lifei> cd Desktop

C:\Users\lifei\Desktop>

Then, we run the following command (for file try.py):

C:\Users\lifei\Desktop>python -m pdb try.py

> c:\users\lifei\desktop\try.py(1)<module>()

-> for i in range(5):

(Pdb)

Then, we can start debugging.

Q.32. What is Tkinter?

Tkinter is a famous Python library with which you can craft a GUI. It provides support for different GUI tools
and widgets like buttons, labels, text boxes, radio buttons, and more. These tools and widgets have attributes
like dimensions, colors, fonts, colors, and more.

You can also import the tkinter module.

>>> import tkinter

>>> top=tkinter.Tk()
This will create a new window for you:

This creates a window with the title ‘My Game’. You can position your widgets on this.

Python Data Science Interview Questions


Q.33. What is the process to calculate percentiles with NumPy?

Refer to the code below.

>>> import numpy as np

>>> arr=np.array([1,2,3,4,5])

>>> p=np.percentile(arr,50)

>>> p

3.0

Q.34. How would you create an empty NumPy array?

To create an empty array with NumPy, we have two options:

a. Option 1

>>> import numpy

>>> numpy.array([])

array([], dtype=float64)

b. Option 2

>>> numpy.empty(shape=(0,0))

array([], shape=(0, 0), dtype=float64)

Q.35. How is NumPy different from SciPy?

We have so far seen them used together. But they have subtle differences:
SciPy encompasses most new features

NumPy does hold some linear algebra functions

SciPy holds more fully-featured versions of the linear algebra modules and other numerical algorithms

NumPy has compatibility as one of its essential goals; it attempts to retain all features supported by any of
its predecessors

NumPy holds the array data type and some basic operations: indexing, sorting, reshaping, and more

Q.36. Explain different ways to create an empty NumPy array in Python.

We’ll talk about two methods to create NumPy array-

First method-

>>> import numpy

>>> numpy.array([])

array([], dtype=float64)

Second method-

>>> numpy.empty(shape=(0,0))

array([], shape=(0, 0), dtype=float64)

If you want more advanced questions, refer to Python Data Science Interview Questions and Answers

Q.37. What is monkey patching?

Dynamically modifying a class or module at run-time.

>>> class A:

def func(self):

print("Hi")

>>> def monkey(self):

print "Hi, monkey"

>>> m.A.func = monkey

>>> a = m.A()

>>> a.func()
Hi, monkey

Q.38. How will you find, in a string, the first word that rhymes with ‘cake’?

For our purpose, we will use the function search(), and then use group() to get the output.

>>> import re

>>> rhyme=re.search('.ake','I would make a cake, but I hate to bake')

>>> rhyme.group()

‘make’

And as we know, the function search() stops at the first match. Hence, we have our first rhyme to ‘cake’.

Q.39. Write a regular expression that will accept an email id. Use the re module.

>>> import re

>>> e=re.search(r'[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$','abc@gmail.com')

>>> e.group()

‘abc@gmail.com’

Q.40. What is pickling and unpickling?

To create portable serialized representations of Python objects, we have the module ‘pickle’. It accepts a Python
object (remember, everything in Python is an object). It then converts it into a string representation and uses the
dump() function to dump it into a file. We call this pickling. In contrast, retrieving objects from this stored
string representation is termed ‘unpickling’.

Q.41. What is the MRO in Python?

MRO stands for Method Resolution Order. Talking of multiple inheritances, whenever we search for an
attribute in a class, Python first searches in the current class. If found, its search is satiated. If not, it moves to
the parent class. It follows an approach that is left-to-right and depth-first. It goes Child, Mother, Father, Object.

We can call this order a linearization of the class Child; the set of rules applied are the Method Resolution Order
(MRO). We can borrow the mro attribute or the mro() method to get this.

Q.42. How do we make forms in Python?

We use the cgi module for this; we borrow the FieldStorage class from it. It has the following attributes:

form.name: Name of field.

form.filename: Client-side filename for FTP transactions.

form.value: Value of field as string.


form.file: File object from which to read data.

form.type: Content type.

form.type_options: Options of ‘content-type’ line of HTTP request, returned as dictionary.

form.disposition: The field ‘content-disposition’.

form.disposition_options: Options for ‘content-disposition’.

form.headers: All HTTP headers returned as dictionary.

Python OOPS Interview Questions and Answers


Q.43. Is Python call-by-value or call-by-reference?

Python is neither call-by-value, nor call-by-reference. It is call-by-object-reference. Almost everything is an


object in Python. Take this example:

>>> item='milk'

>>> groceries=[]

>>> groceries.append(item)

>>> groceries

[‘milk’]

>>> items=groceries

>>> item='cheese'

>>> items.append(item)

>>> item

‘cheese’

>>> groceries, items

([‘milk’, ‘cheese’], [‘milk’, ‘cheese’])

item is ‘milk’ and groceries is an empty list. We append item to the list of groceries; groceries is now the list
[‘milk’]. Now, we assign groceries to the name items. item is now ‘cheese’; let’s append it to items. So now, the
name item holds the value ‘cheese’, and items is the list [‘milk’, ‘cheese’]. But now, even groceries is the list
[‘milk’, ‘cheese’]. The list groceries got updated too. With Python, you focus on objects, and not on names. It is
one list object we modified for both names ‘groceries’ and ‘items’.

Functions will modify values of mutable objects, but not immutable ones:
>>> a=1

>>> def up(num):

num+=1

>>> up(a)

>>> a

------------------

------------------

>>> a=[1]

>>> def up(list):

list[0]+=1

>>> up(a)

>>> a

[2]

Variables are names bound to an object, not aliases for actual memory locations.

Q.44. Why do we need to overload operators?

To compare two objects, we can overload operators in Python. We understand 3>2. But what is orange>apple?
Let’s compare apples and oranges now.

>>> class fruit:

def init (self,type,size):

self.type='fruit'

self.type=type

self.size=size

def gt (self,other):

if self.size>other.size:

return True

return False

>>> orange=fruit('orange',7)

>>> apple=fruit('apple',8)
>>> apple>orange

True

>>> orange>apple

False

Q.45. Why do we need the init () function in classes? What else helps?

init () is what we need to initialize a class when we initiate it. Let’s take an example.

>>> class orange:

def init (self):

self.color='orange'

self.type='citrus'

def setsize(self,size):

self.size=size

def show(self):

print(f"color: {self.color}, type: {self.type}, size: {self.size}")

>>> o=orange()

>>> o.setsize(7)

>>> o.show()

color: orange, type: citrus, size: 7

In this code, we see that it is necessary to pass the parameter ‘self’ to tell Python it has to work with this object.

Q.46. Does Python support interfaces like Java does?

No. However, Abstract Base Classes (ABCs) are a feature from the abc module that let us declare what methods
subclasses should implement. Python supports this module since version 2.7.

Q.47. What are accessors, mutators, and @property?

What we call getters and setters in languages like Java, we term accessors and mutators in Python. In Java, if we
have a user-defined class with a property ‘x’, we have methods like getX() and setX(). In Python, we have
@property, which is syntactic sugar for property(). This lets us get and set variables without compromising on
the conventions. For a detailed explanation on property, refer to Python property.

Q.48. Consider multiple inheritances here. Suppose class C inherits from classes A and B as class C(A,B).
Classes A and B both have their own versions of method func(). If we call func() from an object of class
C, which version gets invoked?
In our article on Multiple Inheritance in Python, we discussed the Method Resolution Order (MRO). C does
not contain its own version of func(). Since the interpreter searches in a left-to-right fashion, it finds the method
in A, and does not go to look for it in B.

Q.49. What do you mean by overriding methods?

Suppose class B inherits from class A. Both have the method sayhello()- to each, their own version. B overrides
the sayhello() of class A. So, when we create an object of class B, it calls the version that class B has.

>>> class A:

def sayhello(self):

print("Hello, I'm A")

>>> class B(A):

def sayhello(self):

print("Hello, I'm B")

>>> a=A()

>>> b=B()

>>> a.sayhello()

Hello, I’m A

>>> b.sayhello()

Hello, I’m B

Python Interview Questions and Answers for Testers


Q.50. How would you perform unit-testing on your Python code?

For this purpose, we have the module unittest in Python. It has the following members:

FunctionTestCase

SkipTest

TestCase

TestLoader

TestResult

TestSuite

TextTestResult

TextTestRunner
defaultTestLoader

expectedFailure

findTestCases

getTestCaseNames

installHandler

main

makeSuite

registerResult

removeHandler

removeResult

skip

skipIf

skipUnless

Q.51. What is unit testing? How will you do it in Python?

This is the first level of software testing, and it focuses on testing individual units of source code. This is to
make sure everything works as expected. A unit is the smallest testable part of a software and usually comprises
of a few inputs and a single output.

For this, we have the unittest framework. This was inspired by the JUnit framework and offers support for test
automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and
independence of tests from the framework. unittest is about text fixtures, text cases, test suites, and test runners.

Q.52. Can you use Selenium with Python?

Selenium is an open-source, widely-used testing tool that is capable of automating tests on web browsers. Being
open-source means there are no licensing costs. You can write test scripts with Python or other languages like
Java, PHP, C#, Ruby, and Perl.

Practice some Python Programming Interview Questions based on its frameworks.

Python Framework Interview Questions


Q.53. Differentiate between Django, Pyramid, and Flask.
These are three major frameworks in Python. Here are the differences:

We can also use Django for larger applications. It includes an ORM.

Flask is a microframework for a small platform with simpler requirements. It is ready to use and you must
use external libraries.

The pyramid is for larger applications. It is flexible and you can choose the database, the URL structure, the
templating style, and much more. It is also heavily configurable.

Q.54. Explain the Inheritance Styles in Django.

Talking on inheritance styles, we have three possible-

Abstract Base Classes- For the parent class to hold information so we don’t have to type it out for each
child model

Multi-table Inheritance- For subclassing an existing model and letting each model have its own database

Proxy Models- For modifying the model’s Python-level behavior without having to change its fields

Q.55. What is Flask- WTF? Explain its features.

Flask-WTF supports simple integration with WTForms. It has the following features-

Integration with wtforms

Global csrf protection

Recaptcha supporting

Internationalization integration

Secure form with csrf token

File upload compatible with Flask uploads

Q.56. What is Flask?


Python Flask, as we’ve previously discussed, is a web microframework for Python. It is based on the
‘Werkzeug, Jinja 2 and good intentions’ BSD license. Two of its dependencies are Werkzeug and Jinja2. This
means it has around no dependencies on external libraries. Due to this, we can call it a light framework.

A session uses a signed cookie to allow the user to look at and modify session contents. It will remember
information from one request to another.
However, to modify a session, the user must have the secret key Flask.secret_key.

Is there any question in this section which you are not able to answer? If yes, then you need to revise the
concepts of frameworks for cracking the next Python coding interview.

Most Asked Python Coding Interview Questions and Answers


Here comes the most amazing part of Python programming interview questions part 2. Below are frequently
asked Python Coding Interview Questions which you can’t miss. Check it out now –

Q.57. How is multithreading achieved in Python?

A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As you
know, Python is a multithreaded language. It has a multi-threading package.

The GIL (Global Interpreter Lock) ensures that a single thread executes at a time. A thread holds the GIL and
does a little work before passing it on to the next thread. This makes for an illusion of parallel execution. But in
reality, it is just threaded taking turns at the CPU. Of course, all the passing around adds overhead to the
execution.

Q.58. How is memory managed in Python?

Python has a private heap space to hold all objects and data structures. Being programmers, we cannot access it;
it is the interpreter that manages it. But with the core API, we can access some tools. The Python memory
manager controls the allocation.

Additionally, an inbuilt garbage collector recycles all unused memory so it can make it available to the heap
space.

Q.59. What is tuple unpacking?

First, let’s discuss tuple packing. It is a way to pack a set of values into a tuple.

>>> mytuple=3,4,5

>>> mytuple

(3, 4, 5)

This packs 3, 4, and 5 into mytuple.

Now, we will unpack the values from the tuple into variables x, y, and z.

>>> x,y,z=mytuple
>>> x+y+z

12

Q.60. What is a namedtuple?

A namedtuple will let us access a tuple’s elements using a name/label. We use the function namedtuple() for
this, and import it from collections.

>>> from collections import namedtuple

>>> result=namedtuple('result','Physics Chemistry Maths') #format

>>> Ayushi=result(Physics=86,Chemistry=95,Maths=86) #declaring the tuple

>>> Ayushi.Chemistry

95

As you can see, it let us access the marks in Chemistry using the Chemistry attribute of object Ayushi.

Q.61. How do you create your own package in Python?

We know that a package may contain sub-packages and modules. A module is nothing but Python code.

To create a Python package of our own, we create a directory and create a file init .py in it. We leave it
empty. Then, in that package, we create a module(s) with whatever code we want. For a detailed explanation
with pictures, refer to Python Packages.

Q.62. You mentioned PyPI in your previous answer. Can you elaborate?

Sure. PyPI is the Python Package Index. This is a repository of software for Python. It has a large collection of
packages and their binaries for a wide range of uses. Here’s a hint of what it looks like-
Q.63. What will the following code output?

>>> word=’abcdefghij’

>>> word[:3]+word[3:]

The output is ‘abcdefghij’. The first slice gives us ‘abc’, the next gives us ‘defghij’.

Q.64. Have you heard of the yield keyword in Python?

Yes, I have. This keyword bears the ability to turn any function into a generator. Much like the standard return
keyword, but returns a generatorobject. It is also true that one function may observe multiple yields.

>>> def odds(n):

odd=[i for i in range(n+1) if i%2!=0]

for i in odd:

yield i

>>> for i in odds(8):

print(i)

1
3
5
7

Q.65. If a function does not have a return statement, is it valid?

Very conveniently. A function that doesn’t return anything returns a None object. Not necessarily does the
return keyword mark the end of a function; it merely ends it when present in the function. Normally, a block of
code marks a function and where it ends, the function body ends.

So, this was the last category of our Python programming interview questions and answers. Hope this helped
you. If you have more python interview questions for experienced or freshers or any interview experience do
share with us through comments.

Potrebbero piacerti anche