Getting Started With Python Programming
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:
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
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
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
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
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
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. 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
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
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. Web Frameworks
2. Django
3. Flask
1. Python vs Java
2. Python vs Scala
3. Python vs R
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!
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.
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.
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
ii. Compiler
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:
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).
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 (:).
Output: 21
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
iii. Object-Oriented
The Python programming language supports classes and objects. We discussed these above.
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.
You can use it to develop a GUI (Graphical User Interface). One way to do this is through Tkinter.
6. Python Frameworks
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
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.
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++.
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.
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.
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
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.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
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.
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.
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
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.
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
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.
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.
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.
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.
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.
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!
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.
For Reference-
Django
Flask
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.
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
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.
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.
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!
If you want to explore Python more, you need to check how Netflix is using Python
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.
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.
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 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.
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.
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.
– 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.
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
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.
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
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
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:
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
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.
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?
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
So, this was all about our blog post on Python Career Opportunities
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.
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.
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.
a. What to study?
As a fresher in Python, you should study the following things to build a strong foundation in Python.
DataFlair has published a Python Master Guide for you by which you can learn all these topics.
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.
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.
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.
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
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. >>>
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:
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:
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.
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
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.
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.
Ready to build your own model? Speech Emotion Recognition Python Project with Source Code
Check the complete python project with source code – Detecting Parkinson’s Disease with Python
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
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.
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.
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.
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.
One such use-case is to use clustering in a telephone company to determine tower locations for optimum signal
strength.
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.
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.
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.
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.
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.
So, now you know what data science is all about. But why is Python the best choice for it? Here are a few
reasons-
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.
For carrying out data analysis and other scientific computation, you will need any of the following 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.
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.
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.
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?
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.
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.
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.
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.
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!
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.
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–
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.
This should give you an idea of what the Anaconda prompt looks like. Now, to activate this environment, you
can type-
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
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
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
Optimization
Linear algebra
Integration
Interpolation
Special functions
FFT
Signal and Image processing
ODE solvers
c. 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
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
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
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 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.
In this article on Python 3.6, we will discuss the following new features in Python 3.6. Moreover, we will
discuss
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.
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}")
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.
With PEP 515, we can use underscores in numeric literals- between digits and after base specifiers.
1. >>>
0x_FF_FE_FF_FE
4294901758
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:
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]
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
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.
With Python 3.6, no data loss occurs when we use bytes paths on Windows.
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.
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.
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.
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.
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.
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.
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(τ).
6.283185307179586
Reversible ABC- to represent iterable classes. These also provide the method reversed ().
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.
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.
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:
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.
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.
Installing and running Python on your personal computer is no difficult task. It involves just a few simple steps:
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.
This is the default option. It also includes the IDLE (Integrated Development Environment),
This option allows you to choose the features that you want.
Documentation
pip
tcl/tk and IDLE- installs tkinter and the IDLE
Python test suite- This installs the standard library test suite of 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.
You have now installed Python. Now, you can find a list of programs in the Start Menu.
Install Python 3 on Windows
a. Command Prompt
You can run Python on the command prompt in two ways:
python
Now, you can use it as an interpreter. As an example, we have calculated 2+3.
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.
b. The IDLE
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)
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.
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.
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.
Python Applications
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.
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
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:
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.
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
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.
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.
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
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.
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!
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.
Let us see various basic python syntax that is used while doing programming in Python.
A Python program comprises logical lines. A NEWLINE token follows each of those. The interpreter ignores
blank lines.
1. >>> print("Hi
2. How are you?")
Output:
If you face any doubt anywhere in the Python Syntax Tutorial, Please Comment.
Output:
You can also use it to distribute a statement without a string across lines.
1. >>> a\
2. =\
3. 10
4. >>> print(a)
Output:
10
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:
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 (#).
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.
Output:
Hi
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:
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.
chaperone'); Output:
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.
Output:
If you leave a line with just whitespace, the interpreter will ignore it.
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:
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.
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.
printer)) Output:
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:
You can also use the method to print out identifiers that match certain values.
printer='HP')) Output:
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.
Output:
So, this was all about the Python Syntax tutorial. Hope you like our explanation
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.
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.
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
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'>
7
You can also do this to an if-statement.
“Hello ‘user'”
If you use double quotes outside, use single quotes wherever you need to use a quote inside.
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")
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) (#).
You can also use triple quotes (‘’’ ‘’’ or “”” “””) for this purpose.
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.
Hi
To check a function’s docstring, use its doc attribute.
So, this was all about Python indentation, comment and statement. Hope you like our explanation.
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 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
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.
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)
So, this was all in Python Assert. Hope you like our explanation
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.
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.
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.
3. Python int
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
i. type() function
It takes one argument, and returns which class it belongs to.
1. >>> a=9999999999999999999999999999999999999
2. >>> type(a)
<class ‘int’>
1. >>>
isinstance(a,bool) False
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.
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
1. >>> 2/2
1.0
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)
1. >>> a=2+3i
SyntaxError: invalid syntax
1. >>> a=2+j
a=2+j
1. >>> a=2+1j
2. >>> a
(2+1j)
1. >>> a=2+3j
2. >>> b=2+5j
3. >>> a+b
(4+8j)
1. >>> a*=2
2. >>> a
(4+6j)
More often than not, programmers need to deal with numbers other than decimal. To do this, you can use
appropriate prefixes.
1. >>> print(0b111)
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)
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
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.
1. >>> int(2+3j)
int(2+3j)
TypeError: can’t convert complex to int
1. >>> int(3j)
int(3j)
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
1. >>> float(3j)
float(3j)
1. >>> float(0o10)
8.0
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)
bin(2.3)
1. >>> bin(2+3j)
bin(2+3j)
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)
oct(8.0)
vi. hex()
The hex() function returns the hexadecimal value of a number.
1. >>> hex(255)
‘0xff’
1. >>> hex(0)
‘0x0’
1. >>> hex(0)
‘0x0’
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
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.
0.1000000000000000055511151231257827021181583404541015625
Did you see what happened here? The Decimal() function preserved the significance.This was the Decimal
Function Python number type.
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.
3/2
1. >>> print(Fraction(1,3))
1/3
120
1. >>> math.exp(3)
20.085536923187668
1. >>> math.tan(90)
-1.995200412208242
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.
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.
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.
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
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.
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
(5, 2, 6)
We asked for three random values, this gave us 5, 2, and 6.
Let’s learn Python Debugger
-1
1. >>> randrange(-2,4)
1. >>> randrange(-2,4)
1. >>> randrange(-2,4)
-1
1. >>> randrange(-2,4)
2
1. >>> randrange(-2,4)
-2
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
[7, 2, 9, 7]
1. >>> list
[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]
[6, 7, 4, 2, 4, 2]
1. >>> list
[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]
[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
With the seed() and rand() functions/ methods from NumPy, we can generate random numbers. The
functionality is the same as above.
1. >>> seed(7)
2. >>> rand(3)
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.
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
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’.
1. >>> 9lives=9
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
Python is case-sensitive, and so are Python identifiers. Name and name are two different identifiers.
1. >>> name='Ayushi'
2. >>> name
‘Ayushi’
1. >>> Name
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
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
1. >>> False=choice
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
1. >>> age=fav=7
2. >>> print(age,fav)
77
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
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’
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,
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,
1010
1. >>> print('10'+10)
print(’10’+10)
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
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
d. Python Tuples
A tuple is like a list. You declare it using parentheses instead.
1. >>> subjects=('Physics','Chemistry','Maths')
2. >>> subjects
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’)
1. >>> subjects[2]='Biology'
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
1. >>> type(person)
<class ‘dict’>
1. Accessing a value– To access a value, you mention the key in square brackets.
1. >>> person['city']
‘Ahmedabad’
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}
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)
1. >>> int("a")
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")
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
1. >>> bool([1,2])
True
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. >>> list({1,2,2,3})
[1, 2, 3]
1. >>> list({"a":1,"b":2})
[‘a’, ‘b’]
1. >>> list({a:1,b:2})
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)
1. >>> uvw
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
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.
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.
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
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()
func()
print(a)
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
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.
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.
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’.
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.
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
Lowercase letters (a to z)
Uppercase letters (A to Z)
Digits (0 to 9)
Underscore (_)
myVar
var_3
this_works_too
_9lives
lives9
An invalid name:
9lives
c. We cannot use special symbols in the identifier name. Some of these are:
!
@
#
$
%
.
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.
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
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.
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.
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.
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.
False
True
1. >>> '9lives'.isidentifier()
False
1. >>> '9.5okay'.isidentifier()
False
Let’s discuss Python Flask
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.
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.
<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.
However, you cannot use a single quote to begin a string and a double quote to end it, and vice-versa.
Since we delimit strings using quotes, there are some things you need to take care of when using them inside a
string.
If you need to use double quotes inside a Python string, delimit the string with single quotes.
And if you need to use single quotes inside a string, delimit it with double quotes.
When you want to span a Python string across multiple lines, you can use triple quotes.
Hello
Welcome
It preserves newlines too, unlike using a backward slash for the same.
1. >>> a="Hello\
2. Welcome"
3. >>> print(a)
Hello Welcome
1. >>> a="Dogs"
2. >>> a[0]="H"
a[0]=”H”
1. >>> print(a)
‘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’
1. >>> a[:8]
‘Dogs are’
1. >>> a[8:]
‘ love’
1. >>> a[:]
1. >>> a[:-2]
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]
Concatenation is the operation of joining stuff together. Python Strings can join using the concatenation
operator +.
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
’10’+10
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)
1. >>> name='Ayushi'
2. >>> print(f"It isn't {name}'s birthday")
Notice that because we wanted to use two single quotes in the string, we delimited the entire string with double
quotes instead.
I love dogs
I love cats
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))
%d – for integers
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.
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’]”
1. >>> a='Book'
2. >>> a.lower()
‘book’
1. >>> a.upper()
‘BOOK’
d. strip()
It removes whitespaces from the beginning and end of the string.
‘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.
True
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.
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’
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.
True
False
d. Identity
Python’s identity operators ‘is’ and ‘is not’ can be used on strings.
False
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.
”
2. or- If the value on the left is True, it returns True. Otherwise, the value on the right
is returned.
1. >>> not('1')
False
1. >>> not('')
True
This was all about the tutorial on Python strings. Hope you like the Python strings tutorial.
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.
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-
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:
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 -*-
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.
Interactive editing
History substitution
Code completion on systems with support for readline
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. …
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:
So, we conclude that a code object is an attribute of a function object. The dir() function will tell us more about
the function:
(‘a’, ’x’)
(None, 3)
b. The Bytecode
The following command gives us the bytecode:
‘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.
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.
‘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.
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.
It is recommended to check the Python master guide before we start with the operator in Python.
These Python arithmetic operators include Python operators for basic mathematical operations.
Arithmetic Operators in Python
a. Addition(+)
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(*)
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(**)
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(%)
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.
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
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
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
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
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
Adds the values on either side and assigns it to the expression on the left. a+=10 is the same as a=a+10.
1. >>> a+=2
2. >>> print(a)
Output: 9
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
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
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
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
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
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
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.
a. and
If the conditions on both the sides of the operator are true, then the expression as a whole is true.
Output: False
b. or
The expression is false only if both the statements around the operator are false. Otherwise, it is true.
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
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
Output: True
Output: True
b. not in
Output: True
Don’t you know about the trending Python Project at DataFlair? Here it is – Gender and Age Detection
with Python
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
Output: True
b. is not
Output: True
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
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
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
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.
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.
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’
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).
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.
0&0 0
0&1 0
1&0 0
1&1 1
1. >>> '$'&'%'
‘$’&’%’
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
1. >>> 1&True
1. >>> 1.0&1.0
1.0&1.0
Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.
Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.
0|0 0
0|1 1
1|0 1
1|1 1
1. >>> 6|1
1. >>> 0b110|0b001
1. >>> True|False
True
XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.
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
1. >>> 6^3
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.
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.
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.
1. >>> int(0b11111)
31
1. >>> 31>>3
3
As you can see, it gives us 3, whose binary is 11. Makes sense, doesn’t it?
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.
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.
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
1. >>> 3<3
Output: False
Because 3 is equal to 3, and not less than it, this returns False.
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
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’.
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
1. >>> (1,2)<('One','Two')
(1,2)<(‘One’,’Two’)
However, if you get comparable elements at the same indices, it is possible to compare two tuples.
1. >>> (1,'one')<(2,'two')
Output: True
1. >>> (1,'one')<('two',2)
(1,’one’)<(‘two’,2)
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'}
{1:’one’,2:‘two‘}<{1:‘three‘,2:‘four‘}
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.
1. >>> 0.5>False
Output: True
1. >>> 3,4,5>3,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)
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.
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
Likewise, this operator returns True only if the value on the left is greater than or equal to that on the right.
Output: False
Any doubt in Python Comparison Operators? Please Ask us in the comment.
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
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.
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.
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.
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’
‘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.
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
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-
sub for –
mul for *
truediv for /
floordiv for //
mod for %
pow for **
or for |
isub for -=
imul for *=
idiv for /=
imod for %=
ixor for ^=
ior for |=
pos for +
invert 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. >>> 2+a
3
If the interpreter cannot add left to right, it will call radd () instead. Here, radd is in reverse/reflected add.
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
To really understand something, once is never enough. So, let’s take another example of Operator overloading
in Python.
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.
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:
’11’
[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.
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.
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
1. >>> a,b=2,3
2. >>> if a>b:
3. print("a")
4. else:
5. print("b")
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
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:
‘b’
1. >>> a,b
(0.009415785735741311, 0.9675879478005226)
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]
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.
‘b:0.5955717855531699’
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.
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:
Hello
Hello
True
Hello
True
So, this was all in Python Ternary Operator. Hope you like our explanation.
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.
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
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.
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
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:
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
False
This stops at the first False it gets (the 0 at the third position) and returns False.
True
This stops at the first True it gets (the 1 at the fourth position) and returns True.
1. >>> 7>8>check(4)
False
This stops at 7>8 and returns False.
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
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 (**)
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.
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
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.
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
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.
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.
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’>
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.
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.
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.
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.
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.
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.
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.
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’.
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.
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.
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.
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.
1. a=7
2. if a>4: print("Greater")
Greater
1. >>>
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!
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.
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.
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
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.
0
1
2
If we wanted to print 1 to 3, we could write the following code.
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]
1. >>> list(range(12,2,-2))
[12, 10, 8, 6, 4]
[]
1. >>> list(range(2,12,-2))
[]
1. >>> list(range(12,2,2))
[]
1
2
3
2
3
4
You can also iterate on a string.
w
i
s
d
o
m
1. >>> list=['Romanian','Spanish','Gujarati']
2. >>> for i in range(len(list)):
3. print(list[i])
Romanian
Spanish
Gujarati
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.
0
1
2
3
4
5
6
7
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. >>> i=6
2. >>> while(i>0):
3. j=6
4. while(j>i):
5. print("*",end=' ')
6. j-=1
7. i-=1
8. print()
*
**
***
****
*****
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.
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.
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).
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.
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.
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.
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.
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.
You can access this docstring using the doc attribute of the function.
However, if you apply the attribute to a function without a docstring, this happens.
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. 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.
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.
3.0+2=5.0
1. >>> sum2('Hello',2)
sum2(‘Hello’,2)
print(f”{a}+{b}={a+b}”)
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. >>> c=sum(2,3)
Hello
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.
If you then try to access the variable x outside the function, you cannot.
1. >>> x
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. >>> y
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. >>> func1()
As you can see here, the function func1() doesn’t print 2 the second time.
When deleting a function, you don’t need to put parentheses after its name.
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.
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.
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
120
1. >>> facto(1)
1. >>> facto(2)
1. >>> facto(3)
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.
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.
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.
Python function is a sequence of statements that execute in a certain order, we associate a name with it.
This lets us reuse code.
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.
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)
There are various types of Python arguments functions. Let’s learn them one by one:
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.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)
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.
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.
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
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'”
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’
1. >>> bin(7.0)
bin(7.0)
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]
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’
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]=
a[4]=3
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.
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. >>> delattr(fruit,'size')
2. >>> orange.size
orange.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}
16. dir()
dir() returns an object’s attributes.
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.
(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)')
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')
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))
24. frozenset()
frozenset() returns an immutable frozenset object.
1. >>> frozenset((3,2,4))
frozenset({2, 3, 4})
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()
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
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’
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.
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.
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. >>> issubclass(fruit,citrus)
False
36. iter()
Iter() Python built-in functions, returns a python iterator for an object.
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()
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.
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>
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)
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)
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
1. >>> f=open('topics.txt')
2. >>> f
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
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.
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.
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
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')
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.
Hi
You can also use the syntactic sugar @staticmethod for this.
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.
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)
67. zip()
zip() returns us an iterator of tuples.
1. >>> set(zip([1,2,3],['a','b','c']))
1. >>> set(zip([1,2],[3,4,5]))
1. >>> a=zip([1,2,3],['a','b','c'])
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.
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.
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.
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’.
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.
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:
blackverna.drift()
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.
‘Female’
However, you don’t need to define this function if you don’t need it in your code.
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.
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.
As you can see, the ‘self’ parameter told the method to operate on the current object, that is, orange. Let’s take
another example.
95
1. >>> rollone.printavg()
Average=88.66666666666667
You can also assign values directly to the attributes, instead of relying on arguments.
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.
‘Leo’
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.
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 &
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.
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.
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.
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.
Begin
23
2.0 3
False
23
<class ‘int’>
<class ‘function’>
Also see, ython Lambda Expressions.
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.
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.
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.
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.
Output – 7
Let’s take a look at Python Slice Constructor
We can also do this inside class functions:
Output – 8
Output – 6
Or we can ask the user for input.
What value?8
1. >>> t.val
Output – ‘8’
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.
Output – I detoxify
Now statement by statement, let’s see what’s happening.
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
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.
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.
The range() function is an inbuilt function in Python, and it returns a range object. First, let’s see what it looks
like.
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)
1. >>> list(range(3))
[0, 1, 2]
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))
[]
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))
1. >>> list(range(-7,-3))
1. >>> list(range(-3,-7))
[]
This, again, returns an empty list, for -3 lies to the right of -7.
1. >>> list(range(3,3))
[]
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))
list(range(1,7,1.5))
1. >>> list(range(1,7,2))
[1, 3, 5]
1. >>> list(range(1,7,0))
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.
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.
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.
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.
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, ‘a’)
(2, ‘b’)
(3, ‘c’)
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
1. >>> set(zip())
set()
You can see that this returns an empty iterator.
Single argument
(1,)
(2,)
(3,)
This returns tuples holding single values
1. print(i)
(1, ‘a’)
(2, ‘b’)
(3, ‘c’)
This zips elements together from each list. How about more than two?
When we provide multiple lists of different lengths, it stops at the shortest one.
1. >>> set(zip([1,2],[3,4,5]))
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. >>> z=zip([1,2],[3,4,5])
2. >>> a,b=zip(*z)
3. >>> a,b
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.
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().
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.
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
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.
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.
In the examples so far, we hardcoded the expression. What if we wanted to let the user provide one instead?
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.
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
1. >>> eval(expr)
Did you see how easy it was to extract the password from the code? All it took was one call to a function.
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. >>> safe_dict={}
2. >>> safe_dict['x']=x
3. >>> eval(expr,safe_dict)
40
1. >>> eval(expr,safe_dict)
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.
So before we leave, let’s take a rather practical example of Python eval() Function.
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.
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.
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
1. >>> type(exec)
<class ‘builtin_function_or_method’>
Let’s check the help for this.
1. >>> help(exec)
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
1. >>> exec(code)
0
1
4
9
16
25
36
1. >>> import os
2. >>> code=input('What would you like to do today?')
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. >>> 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.
1. >>> dir()
1. >>> a=7
2. >>> def hello():
print("Hello") 3. >>> dir()
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 )
1. >>> exec('print(tan(90))')
-1.995200412208242
1. >>> exec('print(tan(90))',{})
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.
[‘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())',globals(),locals())
So, let’s start the Python repr Function. Hope you like our explanation.
repr(obj, /)
It is true for many object types and most builtins that eval(repr(obj))=obj
1. >>> s='Hello'
2. >>> eval(repr(s))
‘Hello’
1. >>> s
‘Hello’
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.
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.
“‘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:
‘Hello, world!’
eval(‘Hello, world!’)
Hello, world!
This is because we don’t have a variable with that name (Hello, world!)
“‘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. >>> print(o)
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. >>> print(o)
I am 7 and orange
That was pretty cool! In Java, we would use the toString() method for this.
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 ().
Take a look:
1. >>> str(3)
‘3’
1. >>> repr(3)
‘3’
They appear the same. Okay, now take a look at this one:
‘2018-09-07 17:33:24.261778’
1. >>> repr(t)
1. >>> t
Using this ‘official’ representation, we can reconstruct the object, but not with what str() gives us:
eval(‘2018-09-07 17:33:24.261778’)
2018-09-07 17:33:24.261778
1. >>> print(d)
1. >>> str(d)
1. >>> repr(d)
1. >>> f'{d}'
So, this was all in Python repr tutorial. Hope you like our explanation.
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.
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.
To access an entire list, all you need to do is to type its name in the shell.
1. >>> list1
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]
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.
Suppose we want items second through fourth from list ‘list1’. We write the following code for this.
1. >>> list1[1:4]
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]
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[2]='Java'
2. >>> languages
To delete an entire list, use the del keyword with the name of the list.
1. >>> list1
list1
[‘C++’, ‘Python’]
[‘C++’]
1. >>> list1=[1,2,3,4,5,6,7,8]
1. >>> list1[0]=0
2. >>> list1
[0, 2, 3, 4, 5, 6, 7, 8]
2. Reassigning a slice
1. >>> list1[1:3]=[9,10,11]
2. >>> list1
1. >>> list1=[0,0,0]
2. >>> list1
[0, 0, 0]
To get an even deeper look into lists, read our article on Python Lists.
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.
1. >>> colors=('Red','Green','Blue')
(1, 2, 3)
1. >>> a,b,c=mytuple
2. >>>
print(a,b,c) 1 2 3
1. >>> a=(1)
1. >>> type(a)
<class ‘int’>
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’>
del a[0]
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}
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.
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]
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
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.
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.
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']
1. >>> days=['Monday','Tuesday','Wednesday',4,5,6,7.0]
1. >>> languages=[['English'],['Gujarati'],['Hindi'],'Romanian','Spanish']
2. >>> languages
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'
languages[0][0]=’Albanian’
1. >>> days
1. >>> languages=[['English'],['Gujarati'],['Hindi'],'Romanian','Spanish']
2. >>> print(languages)
To access a single element, use its index in square brackets after the list’s name. Indexing begins at 0.
1. >>> languages[0]
[‘English’]
1. >>> languages[1.0]
languages[1.0]
TypeError: list indices must be integers or slices, not float
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’]
1. >>> indices[:4]
1. >>> indices[4:]
[‘four’, ‘five’]
1. >>> indices[:]
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]
This returns item from the list’s beginning to two items from the end.
1. >>> indices[1:-2]
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.
1. >>> colors=['red','green','blue']
1. >>> colors=['caramel','gold','silver','occur']
2. >>> colors
1. >>> colors[2:]=['bronze','silver']
2. >>> colors
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
1. >>> colors[2:2]=['occur']
2. >>> colors
Now if you want to add another item ‘holographic’ to the list, we cannot do it the conventional way.
1. >>> colors[4]='holographic'
colors[4]=’holographic’
So, you need to reassign the whole list for the same.
1. >>> colors=['caramel','gold','silver','bronze','holographic']
2. >>> colors
You can delete a Python list, some of its elements, or a single element.
colors
1. >>> colors=['caramel','gold','silver','bronze','holographic']
2. >>> del colors[2:4]
3. >>> colors
[‘caramel’, ‘gold’, ‘holographic’]
1. >>> colors[2]
‘holographic’
[‘gold’,
‘holographic’]
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
This is a grocery Python list with lists in it, where the lists are according to a category.
1. >>> a=[[[1,2],[3,4],5],[6,7]]
2. >>> a
To access the element 4 here, we type the following code into the shell.
1. >>> a[0][1][1]
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]
1. >>> a*3.0
a*3.0
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
2 is composite
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.
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.
There are some built-in functions in Python that you can use on python lists.
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
1. >>> max(['1','2','3'])
‘3’
But it fails when some are numeric, and some are strings in python.
1. >>> max([2,'1','2'])
max([2,’1′,’2′])
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)
sum(a)
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'])
f. list()
It converts a different data type into a list.
1. >>> list("abc")
It can’t convert a single int into a list, though, it only converts iterables.
1. >>> list(2)
list(2)
g. any()
It returns True if even one item in the Python list has a True value.
1. >>> any(['','','1'])
True
1. >>> any([])
False
h. all()
It returns True if all items in the list have a True value.
1. >>> all(['','','1'])
False
1. >>> all([])
True
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.
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]
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
[]
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]
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
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.
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.
1. >>> percentages=(99,95,90,89,93,96)
2. >>> a,b,c,d,e,f=percentages
3. >>> c
90
1. >>> a=(1)
2. >>> type(a)
<class ‘int’>
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')
1. >>>
1. >>> print(percentages)
1. >>> percentages[1]
95
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]
1. >>> percentages[4:]
(93, 96)
This prints out items from index 4 to the end of the list.
1. >>> percentages[2:2]
()
b. Negative indexing
Now, let’s look at negative indexing. Unlike positive indexing, it begins traversing from the right.
1. >>> percentages[:-2]
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
Lastly, when you provide no indices, it prints the whole Python tuple.
1. >>> percentages[:]
del percentages[4]
So, deleting a single element didn’t work. Let’s try deleting a slice.
del percentages[2:4]
As you can see, that didn’t work either. Now, let’s try deleting the entire tuple.
percentages
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])
1. >>> my_tuple[3]=6
Traceback (most recent call last):
my_tuple[3]=6
See, that failed. Now how about changing an element from the same list]?
1. >>> my_tuple[3][0]=6
2. >>> my_tuple
This worked without a flaw. So we can see that while tuples are immutable, a mutable item that it holds may be
reassigned.
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.
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)
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)
1. >>> max(('Hi','hi','Hello'))
‘hi’
‘hi’ is the greatest out of these, because h has the highest ASCII value among h and H.
1. >>> max(('Hi',9))
max((‘Hi’,9))
c. min()
Like the max() function, the min() returns the item with the lowest values.
1. >>> min(a)
d. sum()
This function returns the arithmetic sum of all the items in the tuple.
1. >>> sum(a)
21
1. >>> sum(('1','2','3'))
sum((‘1′,’2′,’3’))
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)
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.
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)
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
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)
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.
You can iterate on a Python tuple using a for loop like you would iterate on a list.
1
3
2
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])
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.
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.
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))
1. >>> numbers[1]
1. >>> numbers[:-1]
(4, 5)
numbers
We also learned some functions and methods on tuples and lists. You must read our tutorials on them for more
insight.
Unlike in C++, we don’t have arrays to work with in Python. Here, we have a list instead.
1. >>>
colors=['red','blue','green'] We can
1. >>> colors[-2:]
[‘blue’, ‘green’]
1. >>> colors[0]='pink'
2. >>> colors
‘blue’
colors
Now that we’ve refreshed our memories, we can proceed to differentiate between python tuples vs lists.
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]
1. >>> list1=[7,6,5,4,3,2,1,0]
2. >>> list1
[7, 6, 5, 4, 3, 2, 1, 0]
It worked, great.
[7, 5, 4, 3, 2, 1, 0]
This was easy, but could we delete a slice of the list? Let’s try it.
[7, 5, 4]
1. >>> nums=[1,2,3,4,5]
2. >>> nums[1:3]=[6,7,8]
3. >>> nums
[1, 6, 7, 8, 4, 5]
list1
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
1. >>> mytuple[1]=3
mytuple[1]=3
1. >>> mytuple=2,3,4,5,6
2. >>> mytuple
(2, 3, 4, 5, 6)
1. >>> mytuple[3:]
(5, 6)
del mytuple[3:]
As is visible, we can slice it to access it, but we can’t delete a slice. This is because it is immutable.
del mytuple[3]
mytuple
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
1. >>> mylist=[(1,2,3),(4,5,6)]
2. >>> type(mylist)
<class ‘list’>
1. >>> type(mylist[1])
<class ‘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]
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]
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.
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.
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’.
True
1. >>> type(namedtuple)
<class ‘function’>
To use any of its functionality, we must first import it.
1. >>> import collections
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.
1. >>> c=Counter('Hello')
2. >>> c
1. >>> c=Counter(a=3,b=2,c=1)
2. >>> c
1. >>> c=Counter()
2. >>> c.update('bfg')
3. >>> c
1. >>> c['f']
1. >>> c['h']
The elements() method returns a python iterator for the values in the container.
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
1. >>> c.most_common(2)
1. >>> c1&c2
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.
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.
35
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
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
1. >>> o.move_to_end('c',last=False)
2. >>> o
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
1. >>> red.r
255
1. >>> red.g
1. >>> red.b
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
1. >>> red._asdict()
1. >>> colors._make(['1','2','3'])
1. >>> colors(**{'r':255,'g':0,'b':0})
1. >>> red._fields
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)
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, 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. >>> c['a']
1. >>> issubclass(Counter,dict)
True
Now, let’s take a look at python counter syntax.
To initialize or define a counter in python, we use the counter factory function. Yet, in that, we can do it in three
ways:
1. >>> c=Counter(['a','b','c','a','b','a'])
2. >>> c
1. >>> c=Counter(('a','b','c','a','b','a'))
2. >>> c
1. >>> c=Counter("Hello")
2. >>> c
1. >>> c=Counter({'a','b','c','a','b','a'})
2. >>> c
1. >>> c=Counter({'a':3,'b':2,'c':1})
2. >>> c
1. >>> c=Counter(a=3,b=2,c=1)
2. >>> c
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
1. >>> d.update({'e':2,'o':4})
2. >>> d
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’
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()
1. >>> e.most_common(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()
Of course, Python Counters aren’t immutable. You can reassign a count the following way:
1. >>> e['e']=5
2. >>> e
1. >>> b.clear()
2. >>> b
Counter()
7. Python Counter Arithmetic
1. >>> a=Counter({'a':3,'b':2,'c':1})
2. >>> b=Counter({'c':3,'d':2,'e':1})
1. >>> a+b
1. >>> a-b
1. >>> b-a
1. >>> a.subtract(b)
2. >>> a
1. >>> a&b
Counter({‘c’: 1})
1. >>> a|b
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.
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.
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
olors[1]=10
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. >>> type(namedtuple)
<class ‘function’>
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
1. >>> pets=namedtuple('pets',['name','age'])
2. >>> Fluffy=pets('Fluffy',8)
3. >>> Fluffy
pets(name=’Fluffy’, age=8)
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)
1. >>> type(red.red)
<class ‘int’>
1. >>> red.blue=1
red.blue=1
1. >>> red._asdict()
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.
1. >>> Colors._make(['Purple','Violet','Gold'])
1. >>> Colors(**{'red':255,'green':0,'blue':0})
1. >>> red._fields
1. >>> red._replace(green=1)
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.
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
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}
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
1. >>> otherdict.keys()
1. >>> otherdict['d']
2. Traceback (most recent call last):
3. File "<pyshell#364>", line 1, in <module>
4. otherdict['d']
KeyError: ‘d’
To deal with this situation, we have defaultdict in Python. First, we must import it from Python collections
module.
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.
As we discussed, we first import defaultdict from the ‘collections’ module. Let us see this Python defaultdict
example.
Next, we define a python function to return a default value for the keys we don’t initialize.
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.
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.
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()
<class ‘NoneType’>
a[1]
print(1/0)
Python defaultdict supports all operations that a regular dictionary does. These include keys() and len().
1. >>> dir(a)
a. missing (key)
This Python method is called by the getitem () method of the dict class when the requested key isn’t
found.
It returned 0 here because that’s what we specified as a default value through a lambda expression to the
defaultdict() factory function.
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
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.
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
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.
1. >>> issubclass(OrderedDict,dict)
True
1. >>> d=OrderedDict()
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)])
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.
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)])
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.
1. >>> f.pop('e')
1. >>> f
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
Python Dictionary
Optionally, you can put the dictionary in a variable. If you want to use it later in the program, you must do this.
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’>
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.
1. >>> dict3={1:'carrots','two':[1,2,3]}
2. >>> dict3
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])
dict([1,2],[2,4],[3,6])
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.
1. >>> animals={}
2. >>> type(animals)
<class ‘dict’>
1. >>> animals[1]='dog'
2. >>> animals[2]='cat'
3. >>> animals[3]='ferret'
4. >>> animals
1. >>> dict3
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)
1. >>> mydict[48]
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.
1. >>> dict4={1:1,2:2,3:3}
1. >>> dict4[2]=4
2. >>> dict4
{1: 1, 2: 4, 3: 3}
1. >>> dict4[4]=6
2. >>> dict4
{1: 1, 2: 4, 3: 3, 4: 6}
dict4
1. >>> dict4={1:1,2:2,3:3,4:4}
{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.
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)
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.
1. >>> dict4={3:3,1:1,4:4}
1. >>> sorted(dict4)
[1, 3, 4]
Now, let’s try printing the dictionary dict4 again.
1. >>> dict4
{3: 3, 1: 1, 4: 4}
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’>
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()
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)
e. clear()
The clear function’s purpose is obvious. It empties the Python dictionary.
1. >>> dict4.clear()
2. >>> dict4
{}
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)
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)
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}
1. >>> newdict.popitem()
(9, 9)
Let’s restart the shell and reassign the dictionary again and see which pair is popped.
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}
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}
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.
1. >>> dict4
{1: 1, 3: 3, 4: 4}
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
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}
dict1={{1:2,2:4}:8,8:16}
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.
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.
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)
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
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
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.
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
c. date.resolution
resolution returns the smallest possible difference between non-equal date objects.
1. >>> datetime.date.resolution
datetime.timedelta(1)
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:
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()
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()
1. >>> d=datetime.date(2018,12,30)
2. >>> td=datetime.timedelta(0,99999)
3. >>> d1=d+td
4. >>> d1
1. >>> d1<d
False
Any doubt yet in Date time module in Python or Examples? Please comment.
1. >>> t=datetime.time(11,59,59,99999)
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
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
i. replace()
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 ()
1. >>> t. str ()
’22:59:07.099999′
Now in the Python Datetime Module Tutorial Lets move ahead with datetime Objects
1. >>> d=datetime.datetime(1995,12,31,10,0)
a. today()
This method returns the current local datetime.
1. >>> datetime.datetime.today()
b. now()
now() returns the current local date and time, but is more precise than today().
1. >>> datetime.datetime.now()
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())
e. utctimestamp()
This is like the previous one, but it returns the UTC time.
1. >>> datetime.datetime.utcfromtimestamp(time.time())
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
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
a. replace()
Again, this is like we have been doing so far.
1. >>> d.replace(microsecond=7)
b. date()
date() returns a date object from the datetime object.
1. >>> d.date()
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()
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()
j. utctimetuple()
This returns a time tuple of the UTC time.
1. >>> d.utctimetuple()
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()
1. >>> d=datetime.datetime(2018,12,30)
2. >>> td=datetime.timedelta(0,99999)
3. >>> d1=d+td
4. >>> d1
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)
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
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.
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
C:\Users\lifei>cd Desktop
C:\Users\lifei\Desktop>mkdir calc
C:\Users\lifei\Desktop>cd calc
C:\Users\lifei\Desktop\calc>echo >calc.py
C:\Users\lifei\Desktop\calc>
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).
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.
‘calc’
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’>
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.
2.25
1. >>> fd(9,4)
2
We can also import all from a module:
This will import all names other than those beginning with an underscore(_). However, we disparage this use, as
it makes for poorly readable 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:
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.
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. >>> dir(sys)
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()
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.
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.
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.
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.
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.
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)
€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.
1. >>> pickle.dumps(x)
b’\x80\x03K\x07.’
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’.
1. >>> pickle.load(f)
pickle.load(f)
1. >>> f.seek(0)
1. >>> pickle.load(f)
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())
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.
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.
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.
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’.
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.
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.
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.
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:
(If you’re working the interpreter, you may also do the following:
loadgame=Game.Sound.load
Now, if the Sound subpackage has a function volume_up(), we call it this way:
loadgame.volume_up(7)
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.
one.two.evenodd
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
1. def check():
2. a=int(input('Enter a number'))
3. if a%2==0: print("Even")
4. else: print("Odd")
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.
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.
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)
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
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:
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):
Sample usage:
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")
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)
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:
10. dup(fd)
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:
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 )
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):
Sample usage:
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:
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:
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:
fdopen() returns an open file object that is connected to the file descriptor.
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:
Sample usage:
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:
Sample usage:
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:
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. >>> 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:
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:
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 )
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:
Sample usage:
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:
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:
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:
Sample usage:
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:
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:
Sample usage:
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:
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:
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:
46. remove(path)
remove() removes the specified file path. If that path is a directory, it raises an OSError.
Sample usage:
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())}")
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:
Sample usage:
52. stat_float_times([newvalue])
stat_float_times() Python os Module decides whether stat_result denotes time stamps as float objects.
Sample usage:
53. statvfs(path)
Python os Module statvfs() executes a statvfs system call on the specified path.
The structure has the following members:
Sample usage:
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)
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:
59. tmpnam()
tmpnam() will return a unique path name reasonable enough to create a temporary file.
Sample usage:
61. unlink(path)
This Python os Module will remove specified file path. If it is a directory, it raises an OSError.
Sample usage:
Sample usage:
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!
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.
This constructs a PrettyPrinter instance for us. Let’s talk about its parameters:
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’])]
1. >>> pprint(data)
(2,
{‘e’: ‘E’,
‘f’: ‘F’,
‘g’: ‘G’,
‘h’: ‘H’,
‘i’: ‘I’,
‘j’: ‘J’,
‘k’: ‘K’,
‘l’: ‘L’}),
(4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),
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.
1. >>> formatted=pformat(data)
DEBUG [(1, {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}),
DEBUG (2,
DEBUG (4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),
[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]
1. >>> pprint(colors)
45553504
1. >>> pprint(mylist)
[1, 2, ‘c’, ‘d’, <Recursion on list with id=45553504>] #The recursive reference
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)
(2,
{‘e’: ‘E’,
‘f’: ‘F’,
‘g’: ‘G’,
‘h’: ‘H’,
‘i’: ‘I’,
‘j’: ‘J’,
‘k’: ‘K’,
‘l’: ‘L’}),
(4, [‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’]),
Here, the ellipses denote the levels it excludes from the output.
1. >>> mylist=[1,2,'c','d']
2. >>> pprint(mylist)
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)
‘vcruntime140.dll’]
1. >>> pprintpp.pprint(data)
pp-ez
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.
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.
To do this, type the following in your Command Prompt (for Windows; type ‘cmd’ in search):
python –version
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:
You will see the following directory on your Desktop (or whichever directory you chose to work with):
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
(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.
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.
Collecting numpy
C:\Users\lifei\Desktop\workwithenv>
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:
In fact, to check what version of numpy you are using, you can type the following in the interpreter:
‘1.14.1’
Running the command to install numpy again will just be ignored by the interpreter.
c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages
c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages
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-
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-
c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-
c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-
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
C:\Users\lifei\Desktop\workwithenv>
d. Learning About a Python Package
To find out about a particular package, we use the subcommand ‘show’:
Name: numpy
Version: 1.14.1
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author-email: numpy-discussion@python.org
License: BSD
Location: c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages
Requires:
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)
numpy (1.14.1)
pip (9.0.1)
setuptools (28.8.0)
virtualenv (15.1.0)
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!
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")
Enter a number7
Odd
1. >>>
Another example would be the constants ‘pi’ and ‘e’ from the ‘math’ module.
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. >>> pi
3.141592653589793
1. >>>
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 *:
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.
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
Loading 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’.
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)
import Game.Sound.load
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.
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.
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.
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.
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.
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}
d={[1,2,3],4}
1. >>> d={{1,3,2},4}
d={{1,3,2},4}
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. >>> 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:]
b[1:]
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()
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)
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()
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()
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]
numbers[3]
So, we use two methods for this purpose- add() and update(). We have seen the update() method on tuples, lists,
and strings.
1. add()
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.
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})
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'})
max({1,2,’three’,’Three’})
3. min()
Like the max() function, the min() function returns the item in the Python set with the lowest value.
1. >>> min(days)
‘Friday’
4. sum()
The sum() functionin Python set returns the arithmetic sum of all the items in a set.
1. >>> sum({1,2,3})
1. >>> sum(days)
sum(days)
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, 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.
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.
1. >>> set2.intersection(set1)
{3}
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.
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()
1. >>> set4=set1.copy()
2. >>> set1,set4
9. isdisjoint()
1. >>> {1,3,2}.isdisjoint({4,5,6})
True
1. >>> {1,3,2}.isdisjoint({3,4,5},{6,7,8})
{1,3,2}.isdisjoint({3,4,5},{6,7,8})
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
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.
True
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
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}
{{1,2}:3}
1. >>> {frozenset(1,2):3}
{frozenset(1,2):3}
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’>
1. >>> bool('Wisdom')
True
1. >>> bool([])
False
1. >>> bool(0)
False
1. >>> bool(0.000000000001)
True
A string has a Boolean value of True, but an empty string has False.
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. >>> False-True
-1
Division
1. >>> False/True
0.0
1. >>> True/False
True/False
This was an exception that raised. We will learn more about exception in a later lesson.
The same rules apply for modulus, exponentiation, and floor division as well.
1. >>> False%True
2. >>> True**False
1. >>> False**False
1. >>> 0//1
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
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 &
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 |
1. >>> False|True
True
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
1. >>> ~True
-2
1. >>> ~False
-1
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
True
1. >>> False is 0
False
5. Logical
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.
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.
1. >>> mylist=[]
2. >>> for i in 'anxiety':
3. mylist.append(i)
4. >>> mylist
But with a Python list comprehension, you can do this in one line:
Now that we’ve got your interest, we’ll dig a little deeper.
[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:
Note that not every loop has an equivalent list comprehension in Python.
[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, 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.
[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.
[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.
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.
Let’s take some code to print the tables of numbers 7 and 8. Using regular for-loops, we’d write the following
code:
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:
[[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.
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.
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.
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
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.
If you wanted to import a single method from ‘time’, you would do it this way.
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
1. >>> time.localtime()
This still isn’t completely instantly perceivable. Let’s try something else yet.
1. >>> time.asctime()
1. >>> time.asctime(time.localtime())
1. >>> time.asctime(time.time())
time.asctime(time.time())
1. >>> calendar.month(2017,12)
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
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()
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)
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).
1. >>> time.localtime(777)
c. asctime()
This method gives us the current time in a very readable format. It returns a string of 24 characters.
1. >>> time.asctime()
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())
1. >>> time.clock()
0.0
1. >>> time.clock()
5.456429169367129
1. >>> time.clock()
9.670899252239298
1. >>> time.clock()
16.96956734536135
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()
1. >>> time.gmtime(0)
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
1. >>> time.mktime()
time.mktime()
h. sleep()
When called, sleep() holds the execution for a given amount of seconds. This number is passed to it as an
argument.
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.
Let’s try printing the shortened name of the month in the local time in Python.
1. >>> time.strftime("%b",time.localtime())
‘Dec’
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.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
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()
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
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)
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)
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
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
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-
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
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
(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
[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
[(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.
[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]
[[[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]]]
1. class calendar.TextCalendar(firstweekday=0)
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)
1. >>> print(t.formatmonth(2018,12))
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. >>> 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)
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))
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))
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
1. >>> cd.isleap(2018)
False
1. >>> cd.leapdays(1995,2018)
1. >>> cd.weekday(2018,12,31)
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
1. >>> cd.monthrange(2018,12)
(5, 31)
[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)
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’
1. >>> cd.prcal(2018)
1. >>> cd.calendar(2018)
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.
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.
24
1. >>> factorial(5)
120
Now, let’s implement this with recursion. We mean to make factorial() call factorial().
1. >>> factorial(4)
24
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
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)
factorial(-2)
return n*factorial(n-1)
return n*factorial(n-1)
File “<pyshell#259>”, line 4, in factorial
return n*factorial(n-1)
if n==1:
Add this one to the list of exceptions in our tutorial on Python Errors and Exceptions.Also, read our article on
First, let’s define a function to calculate the sum of the first n natural numbers.
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
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
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?
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.
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.
Where you place lambda expression in Python, it returns the value of the expression.
You can assign it to a variable if you want to be able to call it again later.
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
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.
Hello
Now, let’s do this with a Python lambda expression.
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.
Hello
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.
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(2)
1. >>> o
1. >>> o()
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()
y()
TypeError: <lambda>() missing 2 required positional arguments: ‘a’ and ‘b’
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.
5
Another example would be where the expression is a print statement.
Hi
Hence, we conclude that omitting arguments is acceptable.
1. >>> numbers=[0,1,2,3,4,5,6,7,8,9,10]
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.
[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’.
[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.
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.
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.
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.
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.
Hello
$$$$$$$$$$$$$$$$$$$$$$
Now let’s see each part of the syntax one by one. Also, Read Recursion in Python.
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.
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.
return wrap
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.
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()
newfunc()
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
1. >>> sayhello()
Hello
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.
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
1. >>> divide=decorator(divide)
2. >>> divide(2,3)
0.6666666666666666
1. >>> divide(2,0)
Can’t divide by 0!
Problem Solved.
Can’t divide by 0!
See, it works. Actually, *args is a tuple of arguments, and **kwargs is a dictionary of keyword arguments.
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
$$
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.
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.
1
2
3
4
5
6
7
8
9
10
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.
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
Even
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.
Odd
Even
Odd
Even
Odd
Even
Odd
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)))
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.
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)
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. >>> next(a)
1. >>> next(a)
36
1. >>> next(a)
100
1. >>> next(a)
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.
What is Python Iterator (Syntax & Example) – Create your own Iterator
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.
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
1. >>> iter((1,3,2))
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):
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)
numIter. next ()
StopIteration
We can see this with the dir() function we saw in in-built functions in Python.
1. >>> dir(numIter)
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.
We implement the following class to create an iterator in Python for squares of numbers from 1 to max.
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)
next(i)
raise StopIteration
StopIteration
Alternatively, you can use the iter () and next () methods for this object.
1. >>> j. next ()
2
1. >>> j. next ()
1. >>> j. next ()
1. >>> j. next ()
16
1. >>> j. next ()
j. next ()
raise StopIteration
StopIteration
1. >>> int()
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. >>> 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.
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.
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.
8
6
4
2
To see the generator in detail, refer to our article on Python Generator.
1. >>> iter_obj=iter([3,4,5])
2. >>> next(iter_obj)
1. >>> next(iter_obj)
1. >>> next(iter_obj)
1. >>> next(iter_obj)
next(iter_obj)
StopIteration
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:
32
Here, we got 32. But for a python iterator, we get 16.
16
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’>
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.
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.
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.
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. 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.
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.
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.
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.
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.
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
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.
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.
Hi
Hello
1. >>> orange.shape='Round'
2. >>> orange.shape
‘Round’
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.
I am size 7
A Python class may also have some special attributes, like doc for the docstring.
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.
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
1. >>> orange.salutation
You can store a method object into a variable for later use.
1. >>> sayhi=orange.salutation
2. >>> sayhi()
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’
1. >>> apple=orange
2. >>> apple.color
‘Orange’
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’
I am happy
1. >>> fruit.salutation
orange.shape
AttributeError: ‘fruit’ object has no attribute ‘shape’
orange
1. >>> fruit
2. <class ' main .fruit'>
3. >>> del fruit
4. >>> fruit
fruit
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.
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.
>>> type(7)
<class ‘int’>
>>> a=[1,2,3]
>>> type(a)
<class ‘list’>
print("Hello")
>>> sayhi
>>> type(a)
<class ‘bool’>
>>> type('True')
<class ‘str’>
pass
>>> 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.
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.
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.
def sayhi(self):
print("Hi")
>>> Hannah=Person()
>>> Hannah.sayhi()
Hi
In saying this, we conclude that init () is like a constructor in C++.
>>> 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
>>> hash(Miley)
5988259
>>> hash(Hannah)
5988259
We assigned Hannah to Miley. Now, what if we delete Hannah? How does it affect Miley?
>>> Miley
>>> 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.
>>> orange
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
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
Sub Class
Child Class
Derived Class
pass
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.
>>> x=0
global x
x+=1
print("I'm a fruit")
super(). init ()
global x
x+=2
print("I'm citrus")
>>> x
>>> lime=citrus()
I’m a fruit
I’m citrus
>>> x
pass
pass
pass
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
pass
pass
>>> cobj=C()
>>> cobj.x
>>> class A:
pass
pass
pass
True
>>> class A:
x=1
pass
pass
pass
>>> dobj=D()
>>> dobj.x
def start(self):
print("Starting engine")
def stop(self):
print("Stopping engine")
def say(self):
super().start()
super().stop()
>>> Pulsar=TwoWheeler()
>>> Pulsar.say()
Starting engine
I have two wheels
Stopping engine
>>> class A:
def sayhi(self):
print("I'm in A")
def sayhi(self):
print("I'm in B")
>>> bobj=B()
>>> bobj.sayhi()
I’m in B
return a+b
return a+b+c
>>> add(2,3)
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
‘I speak Python’
You say what if I do this?:
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
Before we proceed to multiple inheritance syntaxes, let’s see the python syntax.
pass
pass
pass
True
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.
(<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
>>> 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.
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.
>>> class A:
id=1
>>> class B:
id=2
>>> class C:
id=3
pass
>>> M.id
>>> M.id
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")
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.
So, let’s start the list of available Python Compilers and Python Interpreters.
A compiler converts the .py source file into a .pyc bytecode for the Python virtual machine.
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-
Written in- C
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-
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:
Written in- C#
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:
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
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.
Here’s a brief:
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.
a. exception zipfile.BadZipFile
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.
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
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
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
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:
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.
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.
d. ZipFile.namelist()
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:
print(myfile.read())
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.
This extracts all members from the archive to the current working directory. The arguments mean the same as
above.
h. ZipFile.printdir()
i. ZipFile.setpassword(pwd)
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.
ZipFile.debug
This denotes the level of debug output to use. 0 means no output (default) and 3 means the most output.
ZipFile.comment
>>> 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
print("Extracting files")
print("Finished extracting")
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():
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
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>>
paths=[]
filepath=os.path.join(root,filename)
paths.append(filepath)
return paths
>>>
>>> directory='./Demo'
>>> paths=get_paths(directory)
>>> print("Zipping these files:")
print(file)
./Demo\1.txt
./Demo\2.txt
./Demo\3.txt
zip.write(file)
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.
>>> file="Demo.zip"
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
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’.
But to work with this, we must store it into a Python variable. Let’s do this.
>>> todo
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.
Mode Description
b Binary mode
>>> todo=open('abc.txt')
todo=open(‘abc.txt’)
>>> 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
print(1/0)
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()
todo.read()
‘Get g’
Now when we call read() without any arguments, it reads the rest of the Python file.
>>> todo.read()
>>> todo.read()
''
>>> todo.close()
>>> 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()
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:
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.readline()
‘Get groceries\n’
>>> todo.readline()
‘Organize room\n’
>>> todo.readline()
‘Print labels\n’
>>> todo.readline()
‘Write article\n’
>>> todo.readline()
>>> 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’]
>>> todo.write("HI")
todo.write(“HI”)
Here, we did not open the Python file in a writable format. Let’s open it properly.
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.read()
todo.read()
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.
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.
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.
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)
>>> 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()
print(1/0)
We can also use the with-statement so that the file automatically closes as the code under it finishes executing.
todo.read()
>>> type(todo)
<class ‘_io.TextIOWrapper’>
>>> todo.read(3)
‘Get’
>>> todo.read()
>>> 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()
>>> 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)
print(line,end='')
Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cook
>>> todo.close()
28
>>> todo.close()
todo.read()
‘Get groceries\nOrganize room\nPrint labels\nWrite article\nStudy for exam\nLearn to cookWrite to the end of
the file’
Let’s now look at some methods to deal with Python Read and Write File.
>>>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()
>>> todo.read()
todo.read()
c. fileno() in Python
fileno() returns a file descriptor of the file. This is an integer number.
>>> 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.read(5)
‘Get g’
g. readable() in Python
This returns True if the object is readable.
>>> todo.readable()
True
>>> todo.close()
False
>>> todo.close()
h. readline(n=-1) in Python
readline() reads the next line.
>>> 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.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.read()
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.
17
>>> todo.close()
p. writelines() in Python
writelines() writes a list of lines to a file.
todo.writelines([‘\nOne’,’\nTwo’,’\nThree’])
print(todo.read())
Get groceries
Organize room
Print labels
Write article
Study for exam
Learn to cookWrite to the
Write assignment
One
Two
Three
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.
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.
Here, are main 4 categories of ways through which Python Copy a file.
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:
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')
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)
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
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.
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
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.
i. call()
The syntax for this command is the following:
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:
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> if status!=0:
if status<0:
else:
else:
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:
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.
>>> os.chdir('C:\\Users\\lifei\\Desktop')
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:
>>> 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
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> shutil.copy('1.txt','2.txt')
'2.txt'
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.
>>> 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()
iv. copyfileobj()
If you have been reading from your source file object, copyfileobj() will start copying from the position you
stopped reading at.
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.
>>> 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.
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.
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)
copy.deepcopy(x)
This returns a deep copy of x.
exception copy.error
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.
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.
>>> list1=[1,3,[7,4],6]
>>> list1
>>> list2
>>> list1
>>> list2
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.
>>> list1=[1,3,[7,4],6]
>>> list1
>>> list2
>>> list1
>>> list2
>>> dict1={'a':1,'b':2,'c':[1,2,3]}
>>> dict2=dict1.copy()
Finally, we’ll append a new element to this.
>>> dict2['c'].append(7)
>>> dict1
>>> dict2
>>> dict1={'a':1,'b':2,'c':[1,2,3]}
>>> dict2=copy.deepcopy(dict1)
>>> dict2['c'].append(7)
>>> dict1
>>> dict2
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.
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’.
>>> os.chdir('C:\\Users\\lifei\\Desktop')
[‘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')
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
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop\\Photos')
>>> i=1
src=file dst="Dog"+str(i)
+".jpg" os.rename(src,dst)
i+=1
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’.
src=file
if src=='Dog7.jpg':
dst="Seventh Dog.jpg"
os.rename(src,dst)
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’.
>>> i=int(input())
10
>>> count=1
>>> while(count<=10):
src=file
if src=='Dog'+str(i)+'.jpg':
dst='Dog'+str(i+1)+'.jpg'
os.rename(src,dst)
i-=1
count+=1
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
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
Let’s first learn a little about the Python Data File formats we will be processing with.
Python Data File Formats – How to Read CSV, JSON, and XLS Files
Python Data File Formats – How to Read CSV, JSON, and XLS Files
{ "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.
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
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
Python Data File Formats – How to Read CSV, JSON, and XLS Files
>>> 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
>>> data=pandas.read_csv('schedule.csv')
>>> print(data.loc[:,['title','rating']])
>>> 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']])
>>> fields=rows=[]
fields=next(reader)
rows.append(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
>>> print(pandas.read_csv('schedule.csv'))
>>> data=pandas.read_csv('schedule.csv')
>>> data.loc[[1,3],['title','rating']]
Python JSON Files – Reading certain rows and columns
>>> data=pandas.read_csv('schedule.csv')
>>> print(data.to_json(orient='records',lines=True))
>>> print(pandas.read_excel('schedule.xlsx'))
>>> data=pandas.read_excel('schedule.xlsx')
>>> print(data.loc[[1,3],['title','rating']])
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']
>>> book=xlrd.open_workbook('schedule.xlsx')
>>> sheet=book.sheet_by_index(0)
'id'
'id'
'title'
'timing'
'genre'
'rating'
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.
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.
In your code, when you mess up the rules of Python Syntax, your code doesn’t run. The following code causes
a syntax error.
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’.
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)
print(a/b)
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.
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.
>>> assert(1==1)
>>> assert(1==2)
assert(1==2)
AssertionError
b. AttributeError in python
This one occurs when an attribute assignment or reference fails. As an example, let’s take class
‘fruit’.
pass
>>> fruit.size
fruit.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
e. GeneratorExit in python
f. ImportError in Python
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]
list[3]
h. KeyError in Python
>>> dict1={1:1,2:2}
>>> dict1[3]
KeyError: 3
i. KeyboardInterrupt in Python
This one occurs when the user hits the interrupt key (Ctrl + C).
print("Hello") Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
KeyboardInterrupt
j. MemoryError in Python
k. ModuleNotFoundError in Python
When you import a module that does not exist, you will get the ModuleNotFoundError.
import maths
l. NameError in Python
>>> eggs
eggs
m. NotImplementedError in Python
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.
n=4
while(n>0):
yield n
n-=1
>>> c=countdown()
>>> next(c)
>>> next(c)
>>> next(c)
>>> next(c)
>>> next(c)
next(c)
StopIteration
s. IndentationError in Python
t. TabError in Python
u. SystemError in Python
v. SystemExit in Python
w. TypeError in Python
When you apply a function or an operation to an object of incorrect type, you get a TypeError.
>>> '10'+10
’10’+10
x. UnboundLocalError in Python
You get an UnboundLocalError when you try to access a local variable without first assigning a value to it.
m+=1
print(m)
>>> sayhi()
sayhi()
y. UnicodeError in Python
When a Unicode-related encoding/decoding error occurs, you get the UnicodeError exception.
z. UnicodeEncodeError in Python
You get a ValueError when you send in an argument of the correct type, but an improper value.
>>> int(input())
3.5
int(input())
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)
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.
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.
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.
try:
for i in range(3):
print(3/i)
except:
= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =
You divided by 0
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,b=1,0
>>> try:
print(a/b)
print('10'+10)
except TypeError:
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:
>>>
>>> try:
print('10'+10)
print(1/0)
except (TypeError,ZeroDivisionError):
print("Invalid input")
Invalid input
>>> try:
print('1'+1)
print(sum)
print(1/0)
except NameError:
except ZeroDivisionError:
except:
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")
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:
finally:
print("Okay")
print("Bye")
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:
finally:
>>> try:
print(1/0)
except ZeroDivisionError:
print(2/0)
finally:
print(1/0)
print(2/0)
So as you can see, the code under the finally block executes no matter what.
Sometimes, you may want to deal with a situation by raising a certain exception. A simple print statement won’t
work here.
raise ZeroDivisionError
ZeroDivisionError
>>> a,b=int(input()),int(input())
0
>>> if b==0:
raise ZeroDivisionError
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:
= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =
You divided by 0
KeyError
>>> try:
print('1'+1)
except:
raise
print(‘1’+1)
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)
>>>
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:
raise
finally:
print("Okay")
print("Bye")
= RESTART: C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\try2.py =
An assert failed.
Okay
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.
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.
print("This is a problem")
This was all about the Python Exception Handling Cheat Sheet.
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.
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
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
>>> 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’
>>> os.chdir("C:\\Users\\lifei")
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-
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.
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.
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.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’]
We made a file named ‘Readme.txt’ inside our folder Christmas 2017. To delete this file, we use the method
remove().
>>> 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')
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.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’]
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’)
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
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.
print(roots,len(dirs),len(files))
C:\Users\lifei\Desktop\Papers 1 29
C:\Users\lifei\Desktop\Papers\Newfolder 0 1
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')
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.
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
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.
4 GET_ITER
8 STORE_NAME 0 (_)
10 JUMP_ABSOLUTE 6
>> 12 POP_BLOCK
16 RETURN_VALUE
Here, GET_ITER is like invoking iter(). Likewise, FOR_ITER is to repeatedly call next() to get each element.
>>> nums=[1,2,3]
>>> number=iter(nums)
>>> next(number)
>>> next(number)
>>> next(number)
>>> next(number)
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.
>>> dir(a)
>>> a. len ()
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.
Most containers are python iterables. Let’s see some relationship using the built-in function issubclass().
>>> 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
>>> issubclass(collections.Set,collections.Iterable)
True
>>> 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
>>> a
>>> next(b)
>>> next(a)
>>> next(b)
3
>>> next(a)
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.
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().
if i>14:
break
print(i)
10
11
12
13
14
print(i)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
if i>10: break
print(i)
4
6
10
>>> c=0
if c>7:
break
print(i)
c+=1
red
blue
red
blue
red
blue
red
blue
>>> c=0
>>> for i in repeat([1,2,3]):
if c>7:
break
print(i)
c+=1
We can also specify the number of times we want it to repeat, as a second argument.
print(i)
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-
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.
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.
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.
print(i)
if i>25: break
10
12
14
16
18
20
22
24
26
Here’s count() with one argument:
print(i)
2
3
4
5
6
7
print(i)
KeyboardInterrupt
print(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
print(i)
red
green
blue
red
green
blue
red
green
blue
red
green
blue
red
green
blue
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.
Red
Red
Red
print(i)
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
Red
print(i)
KeyboardInterrupt
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.
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:
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.
print(i)
print(i)
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.
print(i)
print(i)
c. combinations(iterable,r)
This returns subsequences of length r from the elements of the iterable.
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.
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
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.
a. accumulate(iterable [,func])
This makes an iterator with accumulated sums (or accumulated results of a binary function specified).
print(i)
1
1
2
3
5
8
13
This prints the Fibonacci series. Let’s take another example.
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.
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.
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.
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.
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.
print(i)
7
9
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.
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.
print(i)
1
2
print(i)
3
4
5
print(i)
1
3
5
i. starmap(function,iterable)
This makes an iterator that takes arguments from the iterable, and computes a function.
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().
print(i)
1
2
k. tee(n=2)
This splits an iterator into n independent iterators.
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.
print(i)
(‘A’, ‘1’)
(‘B’, ‘2’)
(‘C’, ‘3’)
(‘*’, ‘4’)
(‘*’, ‘5’)
print(i)
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.
Python Property
self.title=title
def show_title(self):
‘Teddy Bear’
>>> Teddy_Bear.title
‘TEDDY BEAR’
Checking the object’s dictionary, we find this:
Define setter and getter functions. To set and get a value, we define the following functions:
return 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
Make the variable ‘title’ private. To do this, we use a leading underscore. After all these changes, our class
looks like this:
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.
To solve this problem, Python lends us the function property(). This is what this property of Python looks like:
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()
For fget, fset, and fdel, a property object has the following methods: getter(), setter(), and delete(). So, this is
equivalent to this:
>>> title=property(get_title,set_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.title
‘TEDDY BEAR’
>>> Teddy_Bear.show_title()
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.
We have seen the @-syntax in our lesson on Python Property Decorators. Here, we do this:
self.title=title
def show_title(self):
@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.title
‘TEDDY BEAR’
>>> Teddy_Bear.show_title()
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!
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.
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.
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:
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’>
>>> 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]
>>> groceries[0]='cheese'
>>> groceries
>>> groceries[0]='cheese'
>>> groceries
print(“Hi”)
>>> newlist=[sayhi,sayhi]
>>> newlist[0]
>>> 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'
name[0]=’Avery’
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’
>>> a=bytes([1,2,3,4,5])
>>> a
b’\x01\x02\x03\x04\x05′
>>> a[4]=3
a[4]=3
>>> 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′)
>>> 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’>
print(i)
7
6
5
4
3
2
1
We took an entire post on Range in Python.
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
>>> 'Ayu'+'shi'
‘Ayushi’
Integer Multiplication
>>> 'ba'+'na'*2
‘banana’
Membership
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’
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() return the lowest and highest values, respectively, in a Python sequence.
>>> min('cat')
‘a’
>>> max('cat')
‘t’
This comparison is based on ASCII values.
Python index()
>>> 'banana'.index('n')
Python count()
>>> '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.
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}
>>> 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
{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]}
a={[1,2,3]:1,1:[1,2,3]}
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.
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.
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
Downloading
https://files.pythonhosted.org/packages/50/09/3b1755d528ad9156ee7243d52aa5cd2b809ef053a0f31b53d92853
dd653a/nltk-3.3.0.zip (1.4MB)
100% |████████████████████████████████| 1.4MB 669kB/s
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.
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
>>> words=['write','writer','writing','writers']
>>> ps=PorterStemmer()
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’
>>> 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
>>> 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
.: .
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?
>>> ps.stem('indetify')
‘indetifi’
>>> lemmatizer.lemmatize('identify')
‘identify’
>>> 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.
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.
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.
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.
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.
To install this, you can try the following command in the command prompt-
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)
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.
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()
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()
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()
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.
>>> two.aisle.unique()
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.
a. Single Conditions
>>> two[two['aisle']>3]
Filtering Data – Single Conditions
>>> two[two['aisle']==2]
b. Multiple Conditions
>>> two[(two['aisle']==2) & (two['dept_name']=='toys')]
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()
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)
>>> (two.melt(id_vars=['emp_id'],value_name='dept_name')).tail()
>>> 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
Finally, to reduce this result into an analytical base table, we do the following:
>>> base=two[['aisle','dept_name','emp_id']]
>>> feature=[base]+[melt1,melt2]
>>> abt.tail(1)
>>> pandas.concat([one,two])
Concatenating Data
Finally, we can export our clean and filtered data to an Excel sheet.
>>> res=pandas.concat([one,two])
>>> res.to_excel('res.xlsx')
To apply aggregations on our datasets, we can use the Python numpy library along with pandas.
>>> 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]
>>> r=df.rolling(window=3,min_periods=1)
>>> r.aggregate(numpy.sum)
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
>>> r['B'].aggregate(numpy.mean)
>>> 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.
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
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.
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.
Use the following command in the command prompt to install Python numpy on your machine-
C:\Users\lifei>pip install numpy
>>> np.array(['a','b','c','d','e'],ndmin=2)
>>> a=np.array(['a','b',2,'3.0'])
>>> a
>>> type(a)
<class 'numpy.ndarray'>
>>> a.ndim
>>> a.shape
(4,)
>>> a.size
4
>>> a.dtype
dtype('<U3')
>>> 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
>>> b.sum()
21
>>> b-2*b
>>> np.sort(np.array([[3,2,1],[5,2,4]]))
array([[1, 2, 3],
[2, 4, 5]])
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-
>>> frame
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:
>>> 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’>
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-
>>> 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()
>>> frame.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
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])
>>> 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
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
>>> frame.dropna()
This defaults to dropping on axis=0, which excludes an entire row for an NaN value.
Do you know the Python Modules vs Packages
>>> 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.
>>> frame.fillna(7)
>>> frame.fillna(method='pad')
Ways for Python Data Cleansing – Filling Forward or Backward
>>> frame.fillna(method='backfill')
Follow the link to know about Python Property – The Problem and Solution
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.
>>> frame
True
>>> frame.head()
>>> frame.tail(3)
a. Renaming Columns
To rename a column, you can use the rename() method.
>>> frame
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.
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.
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
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()
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()
e. main_thread()
This method returns the main Thread object. Normally, it is that thread which started the interpreter.
>>> threading.main_thread()
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.
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()
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.
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
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.
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.
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):
threading.Thread.start(threading.current_thread())
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.
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())
threading.Thread.join(threading.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'
>>>
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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().
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.
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.
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.
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()
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.
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.
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.
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:
print("Hello")
>>> t=threading.Timer(30.0,hello)
>>> t.start()
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.
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.
>>> b=threading.Barrier(2,timeout=5)
start_server()
b.wait()
while True:
connection = accept_connection()
process_server_connection(connection)
b.wait()
while True:
connection = make_connection()
process_client_connection(connection)
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:
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.
exception threading.BrokenBarrierError
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().
with some_lock:
#do something
>>> 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!
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.
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
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’\’.
So, how does this work? The following rules must be met:
The whole pattern must match, but not necessarily the whole string.
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.
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'))
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.group()
‘ayush’
>>> match.group()
‘weekend’
>>> match.group()
match.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.group()
‘i S’
>>> 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.
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.group()
‘ayushiwasthere@gmail.com’
It worked perfectly!
Here, if you would have typed [\w-.] instead of [\w.-], it would have raised the following error:
p = sre_compile.compile(pattern, flags)
p = sre_parse.parse(p, flags)
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')
print(i)
advise
advice
>>> type(match)
<class ‘list’>
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:
>>> match=re.findall(r'Java[\w]*',f.read())
print(i)
JavaScript
Java
print(i)
(‘Ayushi’, ‘Sharma’)
(‘Fluffy’, ‘Sharma’)
(‘Leo’, ‘Sharma’)
(‘Candy’, ‘Sharma’)
The functions we discussed may take an optional argument as well. These options are:
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)
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)
print(i)
The metacharacters *, +, and ? are greedy. This means that they keep searching. Let’s take an example.
<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)
print(i)
<em>
</em>
<i>
</i>
print(i)
<em>
</em>
<i>
</i>
>>> 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 ??.
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.
‘an apple’
Here, we used ^ so it won’t change apple to anpple. The grammar police approve.
So, we learned so much about Python regular expressions, but where do we use them? They find use in these
places:
Search engines
Lexical analysis
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.
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.
>>> x=8
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
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’.
(Pdb) b 8
Breakpoint 1 at c:\users\lifei\desktop\demopdb.py:8
(Pdb)
(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
(Pdb)
To clear all breaks, we type ‘cl’. And then when it asks for a confirmation, type ‘y’.
(Pdb) cl
(Pdb)
(Pdb) b power_of_itself
Breakpoint 2 at c:\users\lifei\desktop\demopdb.py:3
(Pdb) b
(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):
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
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
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()
(Pdb)
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.
(Pdb) b 7
Breakpoint 1 at c:\users\lifei\desktop\demopdb.py:7
(Pdb) b
(Pdb) b 9
Breakpoint 2 at c:\users\lifei\desktop\demopdb.py:9
(Pdb) b
(Pdb) disable 2
(Pdb) b
(Pdb)
(Pdb) enable 2
(Pdb) b
Num Type Disp Enb Where
(Pdb)
(Pdb) ignore 1 2
(Pdb)
(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()
(Pdb) l
1 import pdb
2 x=8
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()
1 import pdb
2 x=8
3 def power_of_itself(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
(Pdb) l
1 import pdb
2 x=8
3 def power_of_itself(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()
1 import pdb
2 x=8
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()
(Pdb) l
1 import pdb
2 x=8
3 def power_of_itself(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):
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.
823543
> c:\users\lifei\desktop\demopdb.py(8)<module>()
– > three=power_of_itself(3)
(Pdb)
xiv. c(ont(inue))
This continues executing, and only stops when it reaches a breakpoint.
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()
(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.
(Pdb) whatis x
<class ‘int’>
(Pdb)
3 def power_of_itself(a):
4 return a**a
(Pdb)
(Pdb) display x
display x: 8
(Pdb) display
Currently displaying:
x: 8
(Pdb)
xxv. interact
This starts an interactive interpreter holding all current scope names in its global namespace.
(Pdb) interact
*interactive*
>>> 2+3
>>>
(Pdb) alias y p x
(Pdb) y
(Pdb)
(Pdb) unalias y
(Pdb) y
(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)
xxx. q(uit)
This quits the Python debugger.
(Pdb) q
seven=power_of_itself(7)
seven=power_of_itself(7)
return self.dispatch_line(frame)
bdb.BdbQuit
C:\Users\lifei\Desktop>
pdb.set_trace()
print(f"a={a}")
print(f"b={b}")
return a+b
print("Okay, bye")
>>> pdb.run('add')
> <string>(1)<module>()->None
(Pdb)
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.
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.
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.
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.
>>> multiprocessing.cpu_count()
A multi-core processor- a single computing component with more than one independent actual processing
units/ cores.
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
print("Works")
def square(n):
def cube(n):
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:
import multiprocessing
import os
def child1():
print("Child 1",os.getpid())
def child2():
print("Child 2",os.getpid())
print("Parent ID",os.getpid())
p1=Process(target=child1)
p2=Process(target=child2)
p1.start()
p2.start()
p1.join()
print("Is p1 alive?",alive)
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
import multiprocessing
import os
def child1():
print(current_process().name)
def child2():
print(current_process().name)
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")
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
import os
def child1():
print(current_process().name)
def child2():
print(current_process().name)
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")
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.
lock=Lock()
def printer(item):
lock.acquire()
try:
print(item)
finally:
lock.release()
items=['nacho','salsa',7]
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).
def double(n):
return n*2
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
def double(n):
return n*2
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.
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
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.
<?xml version="1.0"?>
<genre catalogue="Pop">
<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>
<artist>Melanie Martinez</artist>
<year>2015</year>
<album>Cry Baby</album>
</song>
</genre>
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.
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.
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> if genre.hasAttribute('catalogue'):
print(f'Root: {genre.getAttribute("catalogue")}')
Root: Pop
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]
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
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.
Now, in Python XML Processing, let’s take a look at few methods first.
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])
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.
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
Handler=SongHandler()
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
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.
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()
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:
Let’s begin with the Python cgi module. First, let’s import it.
>>>
>>> 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:
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:
print("<H1>Error</H1>")
return
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:
>>> if fileitem.file:
linecount = 0
line = fileitem.file.readline()
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
For more control over your CGI programming in Python, you can use the following functions:
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.
While this is deprecated, Python maintains it for backwards-compatibility. But we can use
urllib.parse.parse_qs() 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()
h. cgi.print_form(form)
i. cgi.print_directory()
j. cgi.print_environ_usage()
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
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.
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.
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!
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.
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
Font properties
Axes properties
Line styles
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-
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.
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.
>>> plt.plot([2,3,4,5])
>>> 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])
>>> plt.show()
>>> plt.plot([2,3,4,5],[3,8,10,12],'gs')
>>> plt.axis([0,7,0,21])
[0, 7, 0, 21]
>>> plt.show()
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
>>> 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()
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)
>>> plt.show()
Matplotlib Tutorial -Keyword Strings
>>> values=[1,11,111]
>>> plt.figure(1,figsize=(9,3))
>>> plt.subplot(131)
>>> plt.bar(names,values)
>>> plt.subplot(132)
>>> plt.scatter(names,values)
>>> plt.subplot(133)
>>> plt.suptitle('Varsity')
Text(0.5,0.98,’Varsity’)
>>> plt.show()
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)
>>> plt.show()
>>> plt.plot([1,2,3],[2,4,9],alpha=5.5)
>>> plt.show()
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)
>>> 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')
>>> plt.show()
Matplotlib Tutorial – Color
e. Dashes
>>> plt.plot([1,2,3],[2,4,9],dashes=[1,2,4,4])
>>> 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')
>>> plt.show()
Python Matplotlib Tutorial – Linestyle or ls
>>> plt.plot([1,2,3],[2,4,9],linestyle=':')
>>> plt.show()
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='+')
>>> 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')
>>> plt.show()
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()
>>> plt.plot([1,2,3],[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0)
>>> plt.show()
[2,4,9],marker='.',markerfacecolor='orange',markersize=13.0,markevery=2)
>>> 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)
>>> plt.plot([1,2,6,9],[2,4,9,10],zorder=2,linewidth=4)
>>> plt.show()
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
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)
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.
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
Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Here are some shorts on it:
Optimization
Linear algebra
Integration
Interpolation
Special functions
FFT
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?
In this Python SciPy Tutorial, we will study these following sub-packages of SciPy:
weave- Functionality that lets you write C++/C code in the form of multiline strings.
You will also want to interact with numpy here. Let’s import that too.
For performing operations of linear algebra in SciPy, we will need to import linalg from scipy-
>>> A=numpy.array([[2,3],[3,4]])
>>> B=numpy.array([[7],[10]])
>>> linalg.solve(A,B)
>>> A.dot(linalg.solve(A,B))-B
>>>mat=numpy.array([[8,2],[1,4]])
>>>linalg.det(mat)
30.0
>>> linalg.inv(mat)
>>> 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)
SciPy will let us work with polynomials using the poly1d type from numpy:
>>> p=poly1d([3,2,4])
2
3x+2x+4
>>> p*p
4 3 2
9 x + 12 x + 28 x + 16 x + 16
89
poly1d([6, 2])
>>> 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:
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
>>> numpy.vectorize(addmul)([1,4,3,9],[2,3,7,8])
>>> 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)
[<matplotlib.lines.Line2D at 0x7fbb78d65210>]
>>> plt.grid()
>>> plt.show()
The special module holds various transcendental functions that will help us with multiple tasks in mathematics.
To import it, use the following command-
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
SciPy will also help you with signal processing. Let’s take an example.
SciPy Tutorial – Processing Signals with SciPy
>>> t=numpy.linspace(-10,10,200)
>>> y=numpy.sin(t)
>>> signal.resample(y,100)
>>> t=numpy.linspace(-10,10,200)
>>> y=numpy.sin(t)+t
>>> signal.detrend(y)
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)
The stats subpackage lets us work with statistics. Let’s take a few examples.
from scipy.stats import norm.
>>> norm.ppf(0.5)
0.0
>>> norm.ppf(2.3)
nan
c. RVS
For a random variate sequence, use rvs().
>>> norm.rvs(size=7)
d. Binomial Distribution
For this, we use the cdf() function from uniform.
>>> uniform.cdf([0,1,2,3,4,5,6,7],loc=1,scale=3)
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.
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:
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
In this Python NumPy Tutorial, we are going to study the feature of NumPy:
Multidimensional arrays.
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
array([0, 1, 2, 3, 4, 5, 6])
>>> np.ones((2,3))
>>> np.zeros((1,2))
array([[0., 0.]])
array([[7, 7],
[7, 7],
[7, 7]])
b. Some Parameters
>>> np.array([1,3,4],ndmin=3) #Minimum dimension
array([[[1, 3, 4]]])
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’)
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]])
>>> 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
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([4, 5, 6])
>>> a[1:2,:]
array([[4, 5, 6]])
>>> a[:,1]
array([79, 5, 8])
a. Integer Indexing
It is possible to create an array from another.
>>> a=np.array([[1,2],[3,4],[5,6]])
array([1, 4, 5])
Let’s pick elements-
>>> b
array([0, 2, 0, 1])
>>> a[np.arange(4), b]
array([ 1, 6, 7, 11])
>>> 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
>>> a=np.array([[1,2,3],[4,5,6]])
>>> b=np.array([[7,8,9],[10,11,12]])
>>> a=np.array([[1,2],[3,4]])
>>> np.sum(a)
10
array([4, 6])
array([3, 7])
To transpose this matrix:
>>> a.T
array([[1, 3],
[2, 4]])
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])
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.
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.
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.
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.
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
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.
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
>>>import pandas as pd
>>> furniture=pd.read_csv('furniture.csv')
>>> furniture
>>> furniture.columns
Index([‘Unnamed: 0’, ‘Product’, ‘Brand’, ‘Cost’], dtype=’object’)
We can slice it-
>>> furniture.columns[0:2]
>>> 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)
>> 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()
>>> furniture.index.nunique()
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-
Mutable
Variable columns
Labeled axes
a. Creating a DataFrame
Let’s see how we can create a DataFrame.
>>> df=pd.DataFrame({'company':['Amazon','Apple','Google','Facebook','Microsoft'],
'Founded':[1994,1976,1998,2004,1975]})
>>> df
Python Pandas Tutorial – Creating a DataFrame
>>> df.index=['Third','Second','Fourth','Fifth','First']
>>> df
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]
>>> df.loc[['Second','Fifth']]
>>> df.iloc[3]
Pandas Tutorial
>>> df.iloc[:,1:4]
Python Pandas Tutorial
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
>>> 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
>>> df.set_index('company',inplace=True)
>>> df
>>> df.reset_index(inplace=True)
>>> df
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
Or we can:
Do you know about Python Data Science
>>> furniture.rename(columns={'Product':'Category'},inplace=True)
>>> furniture
Renaming variables
>>> furniture.drop('Cost',axis=1)
>>> furniture['Gross']=furniture.eval('Cost+(Cost*(0.1))')
>>> furniture
Creating New Variables
Here, with the describe() method, we can find out information about a dataset- min, max, mean, count, and
more.
>>> furniture.describe()
>>> furniture.Gross.max()
55000.0
>>> furniture.groupby('Category').Gross.min()
>>> furniture.groupby('Category').Gross.agg(['count','min','max','mean'])
>>> furniture[furniture.index==2]
>>> furniture.loc[furniture.index==2,:]
>>> furniture[furniture.index.isin([1,3])]
Filtering in Groupby
>>> furniture.isnull()
Missing Values in Pandas
>>> furniture.isnull().sum()
To drop a missing value, you can use dropna(), and to fill it, use fillna().
Learn about python Regular expressions
Now, to rank every variable according to its value, we can use rank().
>>> furniture.rank()
So, with the concat() method, we can concatenate two or more DataFrames.
>>> pd.concat([df,furniture])
Python Pandas – Concatenating DataFrames
>>> pd.concat([df,furniture,df])
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
>>> pd.Panel(np.random.rand(2,4,5))
<class 'pandas.core.panel.Panel'>
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.
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
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-
An XML parser.
SVG support.
Data-aware widgets.
Classes to access SQL databases.
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-
>>> app=QApplication(sys.argv)
>>> root=QWidget()
>>> root.resize(320,240)
>>> root.show()
sys.exit(app.exec_())
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()
app=QApplication(sys.argv)
ex=App()
sys.exit(app.exec_())
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()
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
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.move(100,70)
self.show()
app=QApplication(sys.argv)
ex=App()
Adding Buttons
def slot_method(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!")
app=QApplication(sys.argv)
dialog=Dialog()
>>> dialog.exec_()
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)
if buttonReply==QMessageBox.Yes:
print("Yeah")
else: print("Nah")
self.show()
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.
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()
self.textbox.setText("...")
app=QApplication(sys.argv)
ex=App()
Adding a Textbox
Adding a Textbox
You must learn Python OrderDict
super(). init ()
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.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
self.show()
app=QApplication(sys.argv)
ex=App()
Creating a Menu
self.left = 10
self.top = 10
self.width = 440
self.height = 280
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
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()
app=QApplication(sys.argv)
ex=App()
Setting Absolute Position
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)
app=QApplication(sys.argv)
ex=App()
>>> import os
>>> os.chdir('C:\\Users\\Ram\\Desktop')
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()
app=QApplication(sys.argv)
ex=App()
Loading Images
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.move(10,10)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
openColorDialog(self)
color=QColorDialog.getColor()
if color.isValid():
print(color.name())
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
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.move(50,50)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('Click')
openFontDialog(self)
font,ok=QFontDialog.getFont()
if ok:
print(font.toString())
app=QApplication(sys.argv)
ex=App()
Font Dialogs
Font Dialogs
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 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-
Unicode character;
u Py_UNICODE 2
deprecated since Python 3.3
f float float 4
d double float 8
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
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
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
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
>>> arr.extend(array.array('u',['H','e','l','l','o']))
f. array.fromlist(list)
This appends item from a list to the Python arrays.
>>> arr.fromlist([9,0])
>>> arr
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
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
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
l. array.reverse()
This reverses the Python array.
>>> arr.reverse()
>>> arr
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’
Python Arrays are space-efficient collections of numeric values that are uniformly-typed. You can:
Python Arrays Module – More Information
>>> arr[1]
b. Slice an array
>>> arr[1:4]
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.
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.
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.
Platform-independent
Portable
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
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
Now that you’ve installed everything, let’s begin connecting to the database. Let’s create a database first.
>>> cursor=db.cursor()
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
Now let’s take a look at all operations one by one, starting with creating a table.
>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")
>>> cursor=db.cursor()
>>> cursor.execute("DROP TABLE IF EXISTS student") #This drops the table and replaces it
VARCHAR(20),
>>> cursor.execute(query)
>>> db.close()
7. How to Insert a Record in Python Database?
Let’s try inserting a record in ‘student’.
>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")
>>> cursor=db.cursor()
>>> try:
cursor.execute(query)
except:
>>> db.close()
Let’s check if this makes any changes to the database. In the command prompt:
+--------+--------+------+--------------+
+--------+--------+------+--------------+
+--------+--------+------+--------------+
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.
>>> db=pymysql.connect("localhost","ayushi","yourpassword","demo")
caching sha2: succeeded by fast path.
>>> cursor=db.cursor()
>>> try:
cursor.execute(query)
fname=record[0]
lname=record[1]
age=record[2]
enrolment_no=record[3]
except:
>>> db.close()
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.
To update an existing record, we can simply use an SQL query for the same.
>>> cursor=db.cursor()
>>> 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:
+--------+--------+------+--------------+
+--------+--------+------+--------------+
+--------+--------+------+--------------+
>>> db=pymysql.connect("localhost","ayushi","swaysway7!","demo")
>>> cursor=db.cursor()
>>> query="delete from student where age>23"
>>> try:
cursor.execute(query)
db.commit()
except:
db.rollback()
>>> db.close()
+--------+--------+------+--------------+
+--------+--------+------+--------------+
+--------+--------+------+--------------+
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.
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
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
An initialization function.
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);
docstring- Comment
c. C Functions to Call
We can use one of three forms to return a Python object:
PyObject *args,
PyObject *kw);
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-
char * input;
char * result;
PyObject * ret;
{ return NULL;
result=hello(input);
free(result);
return ret;
};
i. Parameters
Now here, we have four parameters-
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.
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}
)
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.
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.
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
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).
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
6. NoSQL vs SQL
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.
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.
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
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.
>>> student=db.student
>>> student_record={
... '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.
>>> student=db.student
>>> db.student.update_one({'Age':'22'},
.. 'Enrolment':'0875CS191003',
>>> 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.
>>> 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.
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.
a. SQLAlchemy
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
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
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
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")
Now to add another row of content into Relational Database with Python, we use the following code:
>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")
Relational Database with Python can delete values raw vise and delete the entire table. So, let’s see how it
works.
>>> data=pandas.read_csv('samplecsv.csv',encoding="ISO-8859-1")
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: []
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.
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.
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.
Now, let’s look at the most useful and famous Python web framework to help you with Web development.
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
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.
TurboGears is a microframework and a full-stack solution. It’s PyPI package is called tg.devtools.
c. 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.
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.
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
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
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
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
c. Python Flask
d. 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
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
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
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.
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.
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\appdata\local\programs\python\python36-32\lib\site-packages (15.1.0)
C:\Users\lifei>cd Desktop
C:\Users\lifei\Desktop>mkdir projectx
C:\Users\lifei\Desktop>cd projectx
C:\Users\lifei\Desktop\projectx>virtualenv venv
C:\Users\lifei\Desktop\projectx>
(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)
Downloading
https://files.pythonhosted.org/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab2211
65194b2d4/itsdangerous-0.24.tar.gz (46kB)
Downloading
https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659
220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Downloading
https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd7
00775a77/click-6.7-py2.py3-none-any.whl (71kB)
Downloading
https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04
fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Downloading
https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4b
fcbdf4172b/MarkupSafe-1.0.tar.gz
Stored in directory:
C:\Users\lifei\AppData\Local\pip\Cache\wheels\2c\4a\61\5599631c1554768c6290b08c02c72d7317910374c
a602ff1e5
Stored in directory:
C:\Users\lifei\AppData\Local\pip\Cache\wheels\33\56\20\ebe49a5c612fffe
1c5a632146b16596f9e64676768661e4e46
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>
>>> @app.route('/')
def sayhello():
return 'Hello'
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
* Environment: production
* Debug mode: on
* Debugger is active!
From here, we take the address 127.0.0.1:5000 and paste it into the address bar of our browser:
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
>>> @app.route('/')
def sayhello():
return 'Hello'
>>> @app.route('/hi')
def sayhi():
return 'Hi'
app.debug=True
app.run()
http://127.0.0.1:5000/hi
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:
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:
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.
>>> @app.route('/')
def sayhello():
return 'Hello'
>>> @app.route('/<one>add<two>')
def add(one,two):
return f"{one}+{two}={int(one)+int(two)}"
app.run()
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
>>> @app.route('/')
def sayhello():
return 'Hello'
>>> @app.route('/hi/')
def sayhi():
return 'Hi'
app.debug=True
app.run()
>>> @app.route('/hi')
>>> @app.route('/')
def sayhello():
return 'Hello'
>>> @app.route('/<name>')
def sayhi(name):
>>> @app.route('/panel/<salutation>')
def saysomething(salutation):
if salutation=='guest':
return redirect(url_for('sayhello'))
else:
return redirect(url_for('sayhi',name=salutation))
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.
a. GET
b. HEAD
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
>>> @app.route('/success/<name>')
def success(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))
app.debug=True
app.run()
<head></head>
<body>
</body>
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.
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.
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.
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.
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
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
When working with Python Django, you can expect the following Django Features-
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.
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>cd project0
C:\Users\lifei\Desktop\project0>
Have a look at Python Function Arguments with Types, Syntax, and Examples
If you see something like this, it means you have successfully installed Django:
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.
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.
b. Project folder
This holds the packages for our project-
Python Django Tutorial – Project folder
wsgi.py- This deploys our project over WSGI and helps the app communicate with the web server. WSGI
stands for Web Server Gateway Interface.
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',
c. db.sqlite3
This is the database file for your project and has the extension .sqlite3.
admin.py- This lets us make the app modifiable in the admin interface.
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'
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
Once you’re done creating a superuser, use this username and password to login to the dashboard.
return HttpResponse("Hello")
>>> os.chdir('C:\\Users\\lifei\\Desktop\\project0')
a. Mapping to a URL
Now in the inner project0 directory, you have a file urls.py. It looks something like this-
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
Class-based views
“””
Follow this link to know about Python Flask: A Web Framework for Python
>>> from django.contrib import admin
>>> 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
path('hello/',hello),
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.
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.
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.
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:
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
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.
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:
salt = uuid.uuid4().hex
else:
We can easily compute the hash value for any given input
It is infeasible to find two different inputs with the same hash value
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
Impressions
Fingerprints
We crack the vector data in such biometric data to collect fool-proof evidence. Let’s take an example.
plaintext=''
p = (ord(each)-k) % 126
if p < 32:
p+=95
plaintext += chr(p)
print(plaintext)
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
>>>
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.
Attaching the dd image of a drive as a secondary drive on a virtual machine, we can recover data.
Step 2
Attach this VM to the default cluster.
Step 3
Boot the VM from the virtual HDD.
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.
username = "Ayushi",
password = "abc123",
ca_file = "ca.crt")
vm_name = "dummy"
memory = vm_memory,
cluster = vm_cluster,
template = vm_template
os = vm_os)
try:
api.vms.add(vm = vm_params)
api.disconnect()
The output:
Virtual machine ‘dummy’ added
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.
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
>>> tm = s.recv(1024)
>>> s.close()
Output:
The client waits for connection
Before moving forward with the Python Forensics tutorial, you should read up on Python Modules.
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:
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.
def locatePackage(pkg):
Output:
D:\Python code>python dshell.py
src INDIA >> dst USA
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.
>>> 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
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/
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.
if (password == null) {
return null;
try {
byte[] saltedPassword = (password + getSalt()).getBytes();
} catch (NoSuchAlgorithmException e) {
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.
Within a process
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
>>>
>>> mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> mysocket
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.
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.
i. s.connect()
This actively initiates TCP server connection.
And
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
>>> myserver=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> host=socket.gethostname()
>>> port=9999
>>> myserver.bind((host,port))
myclient,addr=myserver.accept()
print(f"Connected to {str(addr)}")
myclient.send(msg.encode("ascii"))
myclient.close()
b. Examples-Client
>>> 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"))
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.
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.
a. exception socket.error
b. exception socket.herror
c. exception socket.gaierror
d. exception socket.timeout
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.
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.
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
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:
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:
Also, some methods like imsave() did not show up with scipy in our interpreter. So, we used pip to install
Pillow. Works for us.
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
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')
>>> f=misc.face()
>>> misc.imsave('demo.png',f)
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.
>>> plt.imshow(f)
>>> plt.show()
Read About Python Sets and Booleans with Syntax and Examples
>>> 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
>>> f.tofile('demo.raw')
>>> import np
>>> fromraw=np.fromfile('demo.raw',dtype=np.uint8)
>>> fromraw.shape
(2359296,)
>>> 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
im=np.random.randint(0,256,10000).reshape((100,100))
misc.imsave('random_%02d.png'%i,im)
>>> filelist.sort()
Let’s Learn Python Strings with String Functions and String Operations
4. How to Display an Image in Python?
>>> plt.imshow(f1,cmap=plt.cm.gray)
>>> plt.imshow(f1,cmap=plt.cm.gray,vmin=30,vmax=200)
>>> plt.contour(f1,[50,200])
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')
>>> plt.imshow(f1[320:340,510:530],cmap=plt.cm.gray,interpolation='nearest')
<matplotlib.image.AxesImage object at 0x019954B0>
>>> face=misc.face(gray=True)
>>> face[0,40]
127
>>> face[10:13,20:23]
>>> 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
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)
>>> rotate=ndimage.rotate(face,45)
>>> rotate_noreshape=ndimage.rotate(face,45,reshape=False)
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)
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)
>>> median_denoised=ndimage.median_filter(noisy,3)
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
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)
>>> 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
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.
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.
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
>>> sender='thesender@gmail.com'
>>> receiver='whicheverreceiver@gmail.com'
>>> smtpserver=smtplib.SMTP("smtp.gmail.com",587)
>>> smtpserver.ehlo()
>>> smtpserver.starttls()
>>> smtpserver.ehlo
>>> smtpserver.login(sender,password)
>>> 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
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'
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()
>>> smtpserver.starttls()
>>> 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:
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)
>>> 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:
>>> sender='thesender@gmail.com'
>>> receiver=’whicheverreceiver@gmail.com'
>>> 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
>>> smtpserver.sendmail(sender,receiver,msg)
>>> print('Sent')
>>> smtpserver.close()
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.
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.
So, let’s start Python graphics, let’s create a simple GUI application:
Enter the main event loop to deal with an event when it is triggered.
>>> top=tkinter.Tk()
>>> top.mainloop()
a. Button
You can add a button to your application.
>>> top=Tk()
>>> top.geometry('600x200'
''
>>> B=Button(top,text="Initiate",command=helloCallBack)
>>> B.place(x=300,y=100)
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
>>> 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()
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
>>> 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.
>>> top=Tk()
>>> L1.pack(side=LEFT)
>>> E1=Entry(top,bd=3)
>>> E1.pack(side=RIGHT)
e. Frame
Generally, a frame is a container widget to help hold other widgets.
>>> 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)
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.
>>> 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
>>> 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
i. Menu
This lets us put all kinds of menus in our application.
filewin=Toplevel(top)
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()
k. Radiobutton
When a user has a few choices of radio buttons, they may only choose one at once.
label.config(text=selection)
>>> top=Tk()
>>> var=StringVar()
>>> 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()
l. Scale
Scale widget provides a slider.
selection=f"Volume: {str(var.get())}"
label.config(text=selection)
>>> top=Tk()
>>> var=DoubleVar()
>>> scale=Scale(top,variable=var)
>>> scale.pack(anchor=CENTER)
>>> button.pack(anchor=CENTER)
>>> label=Label(top)
>>> label.pack()
m. Scrollbar
This adds scrolling capability to other widgets like lists.
>>> top=Tk()
>>> scrollbar=Scrollbar(top)
>>> scrollbar.pack(side=RIGHT,fill=X)
>>> scrollbar.pack(side=RIGHT,fill=Y)
>>> list=Listbox(top,yscrollcommand=scrollbar.set)
>>> list.pack(side=LEFT,fill=BOTH)
>>> scrollbar.config(command=list.yview)
n. Text
This displays text in multiple lines.
>>> 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.
>>> main=Tk()
''
>>> new=Toplevel()
''
p. Spinbox
Basically, Spinbox is like an entry field, except it lets us select from a fixed number of values.
>>> s=Spinbox(top,from_=0,to=18)
>>> s.pack()
Spinbox
q. PanedWindow
This is a container widget that can hold any number of panes horizontally or vertically.
>>> w1=PanedWindow()
>>> 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.
>>> top=Tk()
>>> labelframe=LabelFrame(top,text="Lyrics")
>>> labelframe.pack(fill="both",expand="yes")
>>> left.pack()
s. tkMessageBox
This widget displays a message box.
>>> top=Tk()
tkMessageBox in 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.
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.
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
>>> 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.
>>> 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.
>>> 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()
>>> 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
>>> 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)
>>> df=pd.read_csv('furniture.csv',index_col='Serial',parse_dates=True)
>>> df['Gross']=df.Cost+df.Cost*10
>>> df.describe()
>>> df.corr()
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
>>> df1=sn.load_dataset('iris')
>>> sn.pairplot(df,kind='scatter')
>>> plt.show()
Plotting Correlation in Python
>>> d=df1.corr()
>>> d.to_csv('iriscorrelation.csv')
So, this was all in Python Statistics. Hope you like our explanation.
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.
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
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.
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
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
0.3333333333333333
Fraction(97, 84)
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.
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
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.
>>> df=pd.DataFrame(nums)
>>> df.mean()
0 4.9
dtype: float64
Follow this to know more about Python Pandas
>>> df.mode()
07
0 2.726414
dtype: float64
>>> df.skew()
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.
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.
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
>>> 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)
>>> plt.show()
Implement Python Probability Distributions – Normal Distribution in Python
>>> 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()
>>> plt.hist(s,16,normed=True,color='Green')
>>> plt.show()
>>> plt.hist(s,16,normed=True,color='Brown')
>>> plt.show()
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.
To model relationships, we use linear predictor functions with unknown model parameters; we call these
linear models in Python.
>>> sn.set(color_codes=True)
>>> tips=sn.load_dataset('tips')
>>> ax=sn.regplot(x='total_bill',y='tip',data=tips)
>>> plt.show()
>>> 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
>>> x,y=pd.Series(x,name='x'),pd.Series(y,name='y')
>>> ax=sn.regplot(x=x,y=y,marker='*')
>>> ax=sn.regplot(x=x,y=y,ci=68)
>>> plt.show()
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
>>> ax=sn.regplot(x=x,y=y,x_bins=5)
>>> plt.show()
>>> x=np.linspace(0,10,100)
>>> fig,ax=plt.subplots(1,1)
>>> linestyles=['--','-.',':','-']
>>> degrees_of_freedom=[1,3,7,5]
>>> plt.xlim(0,7)
(0, 7)
>>> plt.ylim(0,0.5)
(0, 0.5)
Let’s discuss Python Compilers
>>> plt.show()
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_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
c. Examples of scipy.stats.chisquare
Let’s take a few simple examples.
>>> chisquare([6,8,6,4,2,2])
Power_divergenceResult(statistic=6.285714285714286, pvalue=0.27940194154949133)
>>> 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)
>>> 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)
>>> chisquare([6,8,6,4,2,2],ddof=1)
Power_divergenceResult(statistic=6.285714285714286, pvalue=0.17880285265458937)
>>> 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.
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.
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
>>> df=sn.load_dataset(‘iris’)
>>> sn.distplot(df['sepal_length'])
>>> plt.show()
>>> sn.distplot(df['sepal_length'],bins=25)
>>> 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
>>> sn.distplot(a=df['sepal_length'],hist=True,kde=False,rug=False)
>>> plt.show()
Displaying Only The Histogram
>>> sn.distplot(a=df['sepal_length'],hist=True,kde=True,rug=True)
>>> plt.show()
Displaying Histogram, Rug, and Kernel Density
>>> sn.distplot(a=df['sepal_length'],rug=True,rug_kws={'color':'r','alpha':0.35,'linewidth':5})
>>> plt.show()
>>> sn.distplot(a=df['sepal_length'],kde=True,kde_kws={'color':'r','alpha':0.35,'linewidth':5})
>>> plt.show()
Customizing the density distribution
>>> sn.distplot(df['sepal_length'],color='lightpink',vertical=True)
>>> plt.show()
Vertical Python Histogram
>>> plt.show()
>>> marks=[79,45,22,89,95]
>>> y=np.arange(len(bars))
>>> plt.bar(y,marks,color=’g’)
>>> plt.xticks(y,bars)
>>> plt.show()
>>> plt.bar(y,marks,color=['cyan','skyblue','lightpink','brown','black'])
>>> 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()
>>> plt.bar(y,marks,color=(0.2,0.4,0.2,0.7),edgecolor='deeppink')
>>> plt.xticks(y,bars)
>>> plt.show()
Python Bar Plot – Setting Border Color
>>> plt.barh(y,marks)
>>> plt.yticks(y,bars)
>>> plt.show()
Horizontal Python Bar Plot
>>> plt.bar(y,marks,color=(0.5,0.1,0.5,0.6))
Text(0.5,1,’Sample graph’)
Text(0.5,0,’Roll numbers’)
>>> plt.ylabel('Marks')
Text(0,0.5,’Marks’)
>>> plt.ylim(0,100)
(0, 100)
>>> plt.xticks(y,bars)
>>> plt.show()
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.
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
For this Python Chart tutorial, we will import three libraries- matplotlib, numpy, and pandas.
You can install these Python Libraries using the following commands-
>>> x=np.random.rand(30)
>>> y=np.random.rand(30)
>>> z=np.random.rand(50)
>>> plt.show()
Python Charts – Python Bubble Charts
>>> plt.scatter(x,y,s=z*777,c='Chartreuse')
>>> plt.show()
>>> colors=np.random.rand(30)
>>> plt.show()
>>> plt.scatter(x,y,s=z*3001,marker='D')
>>> plt.show()
Python Charts – Setting bubble shape
>>> plt.scatter(x,y,s=z*3001,marker='<',color='brown')
>>> plt.show()
>>> plt.scatter(x,y,s=z*3001,marker='*',color='pink')
>>> plt.scatter(x,y,s=z*3001,marker='8',color='lavender')
>>> plt.show()
>>> plt.scatter(x,y,s=z*4000,c="beige",alpha=0.4,linewidth=7)
>>> plt.show()
>>> plt.scatter(x,y,s=z*4000,c="beige",linewidth=7,edgecolors='brown')
>>> plt.show()
Setting the edges for your bubbles charts
>>>plt.scatter(x,y,s=z*2000,c=x,cmap="Greys",alpha=0.4,edgecolors="grey",linewidth=2)
>>> plt.show()
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.
>>> 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)
>>> ax.view_init(27,200)
>>> plt.show()
Python 3D Scatterplots
>>> x=np.arange(0,7)
>>> y=sin(x)
>>> plt.plot(x,y)
>>> plt.show()
Properties of Charts in Python
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)
>>> plt.show()
Titling the graph and labeling the axes
Let’s Discuss Python Data File Formats – How to Read CSV, JSON, and XLS Files
>>> plt.plot(x,y,'g')
>>> plt.plot(x,y,'*')
>>> plt.show()
>>> plt.plot(x,y,'*g')
>>> plt.show()
Charts in Python- Setting line color and type
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.
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)
>>> 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()
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)
>>> plt.legend(['Sine','Cosine'],loc=3)
>>> plt.show()
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')
>>> 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.
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
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.
>>> np.random.seed(10)
>>> one=np.random.normal(100,10,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')
Structure-
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.
>>> plt.show()
Creating Python BoxPlot (Using Matplotlib)
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)
>>> plt.show()
>>> y=np.sin(x)
>>> plt.plot(x,y,'o',color='purple')
>>> plt.show()
Creating Scatter Plot in Python using plt.plot
>>> plt.plot(x,y,'-ok')
>>> plt.show()
markersize=15,linewidth=4,
markerfacecolor='white',
markeredgecolor='gray',
markeredgewidth=1)
>>> plt.show()
>>> plt.plot(np.random.rand(20),'o')
>>> 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.
>>> df=pd.DataFrame(np.random.random((7,7)),columns=['a','b','c','d','e','f','g'])
>>> sn.heatmap(df)
Here, we create a DataFrame, and then call the heatmap() method on it borrowing from seaborn.
>>> sn.heatmap(df,annot=True,annot_kws={'size':7})
>>> 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.
>>> sn.heatmap(df,linewidths=2.5,linecolor='pink')
>>> 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)
>>> plt.show()
>>> sn.heatmap(df,cbar=False)
>>> plt.show()
>>> sn.heatmap(df,xticklabels=2)
>>> plt.show()
Keeping few labels inPython heatmap
>>> sn.heatmap(df,cmap='Greens')
>>> plt.show()
>>> sn.heatmap(df,cmap='BuPu')
<matplotlib.axes._subplots.AxesSubplot object at 0x0B0CFAB0>
>>> plt.show()
>>> sn.heatmap(df,cmap='YlGnBu')
>>> plt.show()
>>> plt.show()
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')
>>> plt.show()
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')
>>> plt.show()
Normalizing a column in heatmap Python
>>> 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')
>>> plt.axis('off')
>>> plt.margins(x=0,y=0)
>>> plt.show()
Word Cloud Python Example
>>> wordcloud=WordCloud(width=500,height=500,max_font_size=30,
min_font_size=10,margin=1).generate(text)
>>> plt.imshow(wordcloud,interpolation='bilinear')
>>> plt.axis('off')
>>> plt.margins(x=0,y=0)
>>> plt.show()
Setting the font size in Word Cloud Python
>>> wordcloud=WordCloud(width=500,height=500,max_words=4,margin=1).generate(text)
>>> plt.imshow(wordcloud,interpolation='bilinear')
>>> plt.show()
>>> wordcloud=WordCloud(width=500,height=500,stopwords=['Java','Django']).generate(text)
>>> plt.imshow(wordcloud,interpolation='bilinear')
>>> plt.axis('off')
>>> plt.margins(x=0,y=0)
>>> plt.show()
>>> plt.imshow(wordcloud,interpolation='bilinear')
>>> plt.margins(x=0,y=0)
>>> plt.show()
Change the background in Word cloud Python
>>> wordcloud=WordCloud(height=500,width=500,background_color='white',
colormap='plasma').generate(text)
>>> plt.imshow(wordcloud,interpolation='bilinear')
>>> plt.axis('off')
>>> plt.show()
>>> mask=np.array(Image.open('diamond.png'))
>>> wordcloud=WordCloud(mask=mask).generate(text)
>>> plt.imshow(wordcloud)
>>> plt.axis('off')
>>> plt.show()
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.
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.
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-
Note that you may need to install Microsoft Visual C++ Build Tools 14.0 or higher for this.
You can import it as-
b. Other modules
There are some other modules we will use here-
a. A simple map
Let’s first simply draw a map and fill it in later.
>>> plt.show()
b. Other projections
>>> ax=plt.axes(projection=ccrs.Orthographic())
>>> ax.stock_img() #Add the stock world map image to the plot
>>> 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()
>>> 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
>>> 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',
Text(78.8,10.7,’Indore’)
>>> plt.show()
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()
>>> ax.coastlines()
<cartopy.mpl.feature_artist.FeatureArtist object at 0x06D75C70>
>>> ax.tissot(facecolor='skyblue',alpha=0.6)
>>> plt.show()
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
>>> 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())
>>> ax.coastlines()
>>> plt.show()
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
>>> wordlist="hello how are you can you read this without punctuation do you think this is something what
is the meaning of life".split()
>>> len(wordlist)
>>> 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
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.
>>> series.plot()
>>> pyplot.show()
>>> series.plot(style='k.')
>>> pyplot.show()
Plotting a Line Graph
>>> series.hist()
>>> pyplot.show()
>>> series.plot(kind='kde')
>>> pyplot.show()
>>> autocorrelation_plot(series)
>>> pyplot.show()
Autocorrelation Plot in Time Series Python Analysis
>>> lag_plot(series)
>>> 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 –
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.
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
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.
return x%3
self.assertEqual(modthree(4),1)
unittest.main()
.
———————————————————————-
Ran 1 test in 0.010s
OK
Did you see the output?
Have a look at Python Classes and Objects
self.assertTrue('HELLO'.isupper())
self.assertFalse('HELlO'.isupper())
self.assertEqual('Hello World'.split(),['Hello','World'])
with self.assertRaises(TypeError):
'Hello World'.split(2)
….
———————————————————————-
Ran 4 tests in 0.031s
OK
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()
assertIsNot()- Tests that the arguments do not evaluate to the same object.
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.
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.
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.
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.
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
def test_lstrip(self):
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)
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.
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-
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.
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.
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.
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
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.
What if we wanted to save these messages to a text file instead of throwing them to the Logging console?
>>> logging.basicConfig(filename='demolog.log',level=logging.DEBUG)
>>> logging.warning('This is a warning'); logging.warning('You may run into issues with your code')
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.
>>>
To enable the time of logging in Python, you can use the following piece of Python code-
logging.basicConfig(format=’%(asctime)s %(message)s’)
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.
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.
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.
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]
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
slice(stop)
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.
>>> 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.
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’
>>> s='helloworld'
>>> s[slice(1,6)]
‘ellow’
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:
Positive Indices-
>>> t=(1,2,3,4,5)
>>> t[slice(2,4)]
(3, 4)
Negative Indices-
>>> 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.
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?
(1, 2, 3)
(4, 5)
>>> t[:] #From beginning to end
(1, 2, 3, 4, 5)
(5, 4, 3, 2, 1)
(5, 3, 1)
(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)
(5, 2)
>>> 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.
[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.
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.
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.
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
popen2*
commands*
a. Syntax
It has the following syntax-
b. Examples
Let’s take a few simple examples of Subprocess Call in Python.
Let’s revise Python Operators
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.
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-
b. Examples
Time for some examples of Python Subprocess run().
>>> subprocess.run(['ls','-l'],shell=True)
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)
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-
b. Examples
Now, the example of Python Subprocess check_output
b'”Hello World!”\r\n’
In this example, we print the string Hello World! to the console.
Have a look at Python Exceptional Handling
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
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-
b. Examples
Let’s try the echo command with this Python Subprocess Popen example.
>>> 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.
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?
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.
>>> dir(sys)
Detailed information-
>>> help(sys)
>>> sys.version
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel=’final’, serial=0)
1000
>>> sys.getrecursionlimit()
1500
Let’s revise CGI Programming in Python with functions
import sys
print(sys.argv)
for i in range(len(sys.argv)):
if i==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
>>> x=42
>>> x
42
>>> print(x)
42
print("Output:",x)
>>> sys.displayhook=show
>>> x
Output: 42
>>> print(x)
42
Output: None
23
Type in value: 23
The following piece of code lets us print to the screen:
Way to write12
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.
>>> sys.modules
sys Module in Python
>>> 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
>>> print(sys.copyright)
>>> sys.exit
>>> try:
sys.exit(1)
except SystemExit:
pass
>>> 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’
>>> saveout=sys.stdout
>>> fsock=open('out.log','w')
>>> sys.stdout=fsock
>>> sys.stdout=saveout
>>> fsock.close()
>>> sys.stderr=fsock #Redirecting standard error by assigning file object of file to stderr
>>> 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:
sys.stderr.write('Hello')
Hello5
Hello5
Hello5
Unlike stdout, stderr does not add carriage returns.
A similar function flush() lets us flush write buffers.
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
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.
Below are some of the reasons which will tell you why everyone is using Python for machine learning –
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.
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.
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.
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.
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
Collecting data.
Filtering data.
Analyzing data.
Training algorithms.
Testing algorithms.
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.
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.
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.
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
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
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
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
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
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
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
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.
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.
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
a. 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-
b. SciPy
Python Libraries – SciPy
SciPy is a scientific- and technical- computing Python library that is free and open-source. Install it as-
c. Matplotlib
Matplotlib, a Python library for plotting and has NumPy as its numerical mathematics extension. Install it as-
d. 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-
e. statsmodels
f. 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-
g. scikit-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-
‘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.
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.
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.
>>> array=df.values
>>> x=array[:,0:8]
>>> y=array[:,8]
>>> scaler=MinMaxScaler(feature_range=(0,1))
>>> rescaledX=scaler.fit_transform(x)
>>> 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
>>> 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.
>>> 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
>>> 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.
e. Mean Removal
We can remove the mean from each feature to center it on zero.
>>> 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.])
>>> encoder=OneHotEncoder()
>>> encoder.fit([[0,1,6,2],
[1,5,3,5],
[2,4,2,7],
[1,0,4,2]
])
>>> 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
>>> 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)
>>> label_encoder.inverse_transform(label_encoder.transform(labels))
>>> df.describe()
(1599, 12)
>>> df.head(10)
>>> df.groupby('quality').size()
>>> df.hist()
>>> plt.show()
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
>>> plt.show()
Here, attributes like ‘total sulfur dioxide’, ‘sulphates’, and ‘residual sugar’ appear skewed toward smaller
values.
Do you know about Python Machine Learning Techniques
>>> correlations=df.corr()
>>> fig=plt.figure()
>>> ax=fig.add_subplot(111)
>>> cax=ax.matshow(correlations,vmin=-1,vmax=1)
>>> fig.colorbar(cax)
>>> ticks=numpy.arange(0,9,1)
>>> ax.set_xticks(ticks)
>>> ax.set_yticks(ticks)
>>> plt.show()
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)
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.
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.
We use pandas to import the dataset and sklearn to perform the splitting. You can import these packages as-
Do you Know about Python Data File Formats – How to Read CSV, JSON, XLS
Following are the process of Train and Test set in Python ML. So, let’s take a dataset first.
>>> 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()
>>> x_train.shape
(413, 12)
Do you Know How to Work with Relational Database with Python
>>> x_test.head()
>>> 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
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.
>>> model=lm().fit(x_train,y_train)
>>> predictions=model.predict(x_test)
Text(0.5,0,’True values’)
>>> plt.ylabel('Predictions')
Text(0,0.5,’Predictions’)
Read about Python NumPy – NumPy ndarray & NumPy Array
>>> plt.show()
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.
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.
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.
Rule-based Classification- This classification is based on a set of IF-THEN rules. A rule is denoted as-
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.
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-
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.
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
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.
scikit-learn- Good for data mining, data analysis, and machine learning.
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.
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
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.
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
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.
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.
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.
>>> diabetes=datasets.load_diabetes()
>>> diabetes_X=diabetes.data[:,np.newaxis,2]
>>> diabetes_X_test=diabetes_X[-30:]
>>> diabetes_y_test=diabetes.target[-30:]
>>> regr.coef_
array([941.43097333])
>>> mean_squared_error(diabetes_y_test,diabetes_y_pred)
3035.0601152912695
0.410920728135835
>>> plt.plot(diabetes_X_test,diabetes_y_pred,color='pink',linewidth=3)
>>> plt.xticks(())
>>> plt.yticks(())
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.
>>> 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.fit(x,y)
>>> plt.figure(1,figsize=(3,4))
>>> plt.clf()
>>> plt.scatter(x.ravel(),y,color='lavender',zorder=17)
>>> x_test=np.linspace(-7,7,277)
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)
>>> ols=linear_model.LinearRegression()
>>> ols.fit(x,y)
>>> plt.plot(x_test,ols.coef_*x_test+ols.intercept_,linewidth=1)
>>> plt.axhline(.4,color='.4')
>>> 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.show()
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
balance_data=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-'+
'databases/balance-scale/balance-scale.data',
print(len(balance_data))
print(balance_data.shape)
print(balance_data.head())
return balance_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
clf_gini.fit(x_train,y_train)
return clf_gini
clf_entropy=DecisionTreeClassifier(
clf_entropy.fit(x_train,y_train)
return clf_entropy
y_pred=clf_object.predict(x_test)
return y_pred
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)
>>> y_pred_entropy=prediction(x_test,clf_entropy)
>>> cal_accuracy(y_test,y_pred_entropy)
>>> x,y=make_blobs(n_samples=500,centers=2,
random_state=0,cluster_std=0.40)
>>> plt.scatter(x[:,0],x[:,1],c=y,s=50,cmap='plasma')
>>> plt.show()
>>> xfit=np.linspace(-1,3.5)
>>> 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.xlim(-1,3.5)
(-1, 3.5)
>>> plt.show()
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.
>>> 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
array([[16, 0, 0],
[ 0, 18, 0],
[ 0, 0, 11]], dtype=int64)
>>> cnf_matrix_mnb
array([[16, 0, 0],
[ 0, 0, 18],
[ 0, 0, 11]], dtype=int64)
>>> iris=load_iris()
>>> x=iris.data
>>> y=iris.target
>>> from sklearn.linear_model import LogisticRegression
>>> logreg=LogisticRegression()
>>> logreg.fit(x,y)
>>> 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
>>> metrics.accuracy_score(y,y_pred)
0.96
>>> knn=KNeighborsClassifier(n_neighbors=5)
>>> knn.fit(x,y)
>>> y_pred=knn.predict(x)
>>> metrics.accuracy_score(y,y_pred)
0.9666666666666667
>>> knn=KNeighborsClassifier(n_neighbors=1)
>>> knn.fit(x,y)
>>> y_pred=knn.predict(x)
>>> metrics.accuracy_score(y,y_pred)
1.0
(150, 4)
>>> y.shape
(150,)
>>> x.shape
(150, 4)
>>> y.shape
(150,)
>>> 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)
>>> y_pred=knn.predict(x_test)
>>> metrics.accuracy_score(y_test,y_pred)
0.9666666666666667
>>> k_range=range(1,26)
>>> scores=[]
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
>>> plt.plot(k_range,scores)
[<matplotlib.lines.Line2D object at 0x05FDECD0>]
Text(0,0.5,’Testing Accuracy’)
>>> plt.show()
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.
>>> x=[1,5,1.5,8,1,9]
>>> y=[2,8,1.7,6,0.2,12]
>>> plt.scatter(x,y)
>>> 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)
>>> 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.']
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)
>>> plt.show()
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.
>>> x=np.random.uniform(1,100,1000)
>>> y=np.log(x)+np.random.normal(0,.3,1000)
>>> 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')
>>> pl.show()
>>> 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()
>>> 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)
>>> preds=iris.target_names[clf.predict(test[features])]
>>> pd.crosstab(test['species'],preds,rownames=['actual'],colnames=['preds'])
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
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).
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.
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)
Image recognition.
Bioinformatics.
Mobile advertising.
Image Restoration.
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.
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.
Will deep learning get us from Siri to Samantha in real life? Well, at least Siri disapproves.
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.
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-
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-
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-
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-
For Python Deep Learning Environment Setup, we recommend you to use the Jupyter Notebook. It observes the
following benefits:
Jupyter Notebook
5. Python Hardware
To run your deep learning code in Python, you can use Python Hardware either a CPU or a GPU.
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.
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.
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.
A Deep Learning network can alter the direction in which a person looks in a picture.
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
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.
Using Deep Learning with Python, astronomers can create pictures of volcanoes and galaxies.
Have a look at Deep Learning vs machine Learning
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.
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
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.
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.
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
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
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
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.
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
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.
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.
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-
4. Apache mxnet
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-
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
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-
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-
8. 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
Lasagne is a lightweight Python library that helps us build and train neural networks in Theano. You can install
it using Python pip-
11. 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-
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.
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
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.
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.
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.
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.
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-
a. Overfitting
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-
Use many-core architectures for their large processing capabilities and suitability for matrix and vector
computations.
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.
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-
>>> @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.visualize()
<IPython.core.display.Image object>
>>> computation_graph.visualize(filename='sumlist.svg')
<IPython.core.display.SVG object>
Note that for this code to work, you will need to perform three tasks-
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:
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
Automotive
Healthcare
Video games
Military
Art
Auditing
Advertising
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.
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.
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.
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
>>> text="Today is a great day. It is even better than yesterday. And yesterday was the best day ever."
>>> 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!")
>>> 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.
[‘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
WordNet is an NLP database with synonyms, antonyms, and brief definitions. We downloaded this with the
NLTK downloader.
>>> syn
>>> syn[0].definition()
>>> syn[0].examples()
>>> syn=wordnet.synsets('life')
>>> syn[0].definition()
>>> syn[0].examples()
>>> syn=wordnet.synsets('AI')
>>> syn
>>> syn[1].definition()
‘the branch of computer science that deal with writing computer programs that can solve problems
creatively’
>>> syn[1].examples()
>>> synonyms=[]
synonyms.append(lemma.name())
>>> synonyms
To get the list of antonyms, we first need to check the lemmas- are there antonyms?
>>> antonyms=[]
for l in syn.lemmas():
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
>>> antonyms
[‘elate’]
One more?
for l in syn.lemmas():
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
>>> antonyms
[‘beautiful‘]
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
>>> SnowballStemmer.languages
(‘arabic’, ‘danish’, ‘dutch’, ‘english’, ‘finnish’, ‘french’, ‘german’, ‘hungarian’, ‘italian’, ‘norwegian’,
‘porter’, ‘portuguese’, ‘romanian’, ‘russian’, ‘spanish’, ‘swedish’)
>>> rom_stemmer=SnowballStemmer('romanian')
‘englez’
‘cocoș’
‘frigid’
Let’s discuss Python Deep Learning
‘frigider’
‘goi’
>>> rom_stemmer.stem('goale') #Empty- plural
‘goal’
‘frumoas’
‘frumoas’
‘frumoș’
Psst., please let us know if you catch an incorrect depiction of the language Romanian!
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
>>> 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?
‘crossing’
‘cross’
>>> lemmatizer.lemmatize('crossing',pos='n') #noun
‘crossing’
‘crossing’
Since lemmatizing gives us better results within context, it is often slower than stemming.
Let’s take a tour to Python SciPy
We can filter NLTK stop words from text before processing it.
>>> 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”]
>>> text="Today is a great day. It is even better than yesterday. And yesterday was the best day ever!"
>>> stopwords=set(stopwords.words('english'))
>>> words=word_tokenize(text)
>>> wordsFiltered=[]
if w not in stopwords:
wordsFiltered.append(w)
>>> wordsFiltered
[‘Today’, ‘great’, ‘day’, ‘.’, ‘It’, ‘even’, ‘better’, ‘yesterday’, ‘.’, ‘And’, ‘yesterday’, ‘best’, ‘day’, ‘ever’,
‘!’]
NLTK can classify words as verbs, nouns, adjectives, and more into one of the following classes:
>>> sentences=nltk.sent_tokenize(text)
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.
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
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.
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
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.
AIFF
AIFF-C
FLAC
To test the installation, you can import this in the interpreter and check the version-
‘3.8.1’
http://www.voiptroubleshooter.com/open_speech/american.html
recognize_houndify()- Houndify
recognize_wit()- Wit.ai
Exempting recognize_sphinx(), you need an Internet connection for anything else you’re working with.
>>> demo=sr.AudioFile('demo.wav')
audio=r.record(source)
>>> type(audio)
<class ‘speech_recognition.AudioData’>
>>> 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-
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.
audio=r.record(source,offset=4,duration=3)
>>> r.recognize_google(audio)
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.
audio=r.record(source,offset=3.3,duration=3)
>>> r.recognize_google(audio)
audio=r.record(source,offset=2.5,duration=3)
>>> r.recognize_google(audio)
‘National thanks’
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.
r.adjust_for_ambient_noise(source)
audio=r.record(source,offset=2.5,duration=3)
>>> r.recognize_google(audio)
r.adjust_for_ambient_noise(source,duration=0.51)
audio=r.record(source,offset=2.5,duration=3)
>>> r.recognize_google(audio)
‘National thanks’
r.adjust_for_ambient_noise(source,duration=0.515)
audio=r.record(source,offset=2.5,duration=3)
>>> r.recognize_google(audio)
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.
To be able to work with your own voice with speech recognition, you need the PyAudio package. You can
install it with pip-
Or you can download and install the binaries with pip. Download link-
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio
Then:
For example:
>>> r=sr.Recognizer()
>>> mic=sr.Microphone()
>>> 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)
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.
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.
>>> r.recognize_google(audio)
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.
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.
Automatic Summarization
Translation
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.
Sentence Planning- To choose appropriate words, form meaningful phrases, and set sentence tone.
Together with Machine Learning, we don’t need to hand-code large sets of rules.
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.
MALLET- Java package for latent dirichlet allocation, clustering, topic modeling, information extraction,
document classification, and more.
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
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-
Creating chatbots.
Machine translation.
Fighting spam.
Extracting information.
Summarizing.
Question-answering.
Customer service.
Market analysis.
So, this was all in NLP Tutorial. Hope you like our explanation of Natural Processing Language.
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
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.
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.
Best-First Search
A* Search
Bidirectional Search
Tabu Search
Beam Search
Simulated Annealing
Hill Climbing
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.
One such example of Hill Climbing will be the widely discussed Travelling Salesman Problem- one where we
must minimize the distance he travels.
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.
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.
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:
Even if not better than the current state, continue until the solution reached.
Exit.
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.
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=[]
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
>>> 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.
>>> magic_square([[2,7,6],[9,5,1],[4,3,8]])
True
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.
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.
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.
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.
Loop to step 2.
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 –
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:
Fittest parent propagates genes through generation; parents may produce offsprings better than either parent
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
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
c. Mutation Operator
In mutation, we insert random genes in offsprings to maintain diversity and avoid premature convergence.
>>> geneSet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."
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.
With zip(), we can iterate over two lists at once. Now, let’s perform mutation.
index=random.randrange(0,len(parent))
childGenes=list(parent)
newGene,alternate=random.sample(geneSet,2)
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.
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.
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?
Useful for very large search spaces with many parameters involved
Mutation testing
Code breaking
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
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.
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
Solid-state Physics
Neurobiology
Signal Processing
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).
‘3.4.3’
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-
>>> 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
>>> img=cv2.imread('py.jpg')
>>> cv2.imshow('Python',img)
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.
>>> 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)
>>> 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'))
>>>
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
(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.
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.
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
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.
SymPy- This is a Python library for symbolic mathematics. It is nearly a full-featured Computer Algebra
System.
With logic programming, we can compare expressions and find out unknown values. Consider the following
piece of code:
>>> mul='mul'
>>> fact(commutative,add)
>>> fact(associative,mul)
>>> fact(associative,add)
>>> expr3=(add,(add,(mul,(mul,2,a),b),b),(mul,3,c))
>>> run(0,(a,b,c),eq(expr2,expression))
>>> 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.
If we have a list of numbers, we can find out which ones are prime and also generate such numbers. Let’s see
how.
if isvar(n):
else:
>>> 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)))
>>> 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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
GitHub is an extremely popular platform for version-control repository hosting service. It has over 950 projects
in Python with over 500 stars.
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.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.
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.
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.
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.
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.
>>> 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'
>>> 18//4
-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.
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.
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.
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
>>> nums=[1,2,3,4,5,6,7]
print(nums[i])
1
2
3
4
5
6
7
A Pythonic approach to this would be:
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’
‘A.B.C’
‘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.
>>> 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:
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.
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 – 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
Java
System.out.println("Hello World");
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.
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
int x=7;
System.out.println(x)
System.out.println(x)
1 error
>>> if 2>1:
print("Greater")
Greater
>>> if 2>1:
Now, let’s see if we can skip the curly braces and indentation in Java.
class one
{ if(2>
1)
System.out.println("2");
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
{
if(2<1)
System.out.println("2");
System.out.println("Lesser");
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
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.
Yes it is*/
Here, documentation comments can be used in the beginning of a function’s body to explain what
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
>>> 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
int x=10;
float z;
z=(float)x;
System.out.println(z);
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
int x=10;
x="Hello";
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
int x=10,y=20;
x=x+y;
y=x-y;
x=x-y;
System.out.println(x+" "+y);
20 10
>>> 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.
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.
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
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
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.
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 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
Below we will discuss R vs Python on the basis of definition, responsibilities, career opportunities, advantages,
and disadvantages –
R Vs Python – Definition
R
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
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.
A Python Developer must be able to implement server-side logic to carry out server-based computations.
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.
Another important job role is to carry out efficient unit testing and debugging.
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.
Software Development
Manufacturing
Robotics
Embedded Systems
Ethical Hacking
Some of the companies hiring Python developers are Google, Amazon, Dropbox, Quora, Reddit, Pinterest and
many more.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 has the future in Big data, Artificial Intelligence, networking, etc.
Desktop applications
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.
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.
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.
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 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:
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.
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 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.
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.
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.
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 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.
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.
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.
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()
import cv2
import os
import time
mixer.init()
sound = mixer.Sound('alarm.wav')
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):
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)
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
score=score+1
# if(rpred[0]==1 or lpred[0]==1):
else:
score=score-1
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):
cv2.imwrite(os.path.join(path,'image.jpg'),frame)
try:
sound.play()
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)
break
cap.release()
cv2.destroyAllWindows()
It may take a few seconds to open the webcam and start detection.
Example Screenshot:
Output Screenshot:
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.
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.
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 –
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.
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.
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.
import numpy as np
import pandas as pd
import itertools
2. Now, let’s read the data into a DataFrame, and get the shape of the data and the first 5 records.
df=pd.read_csv('D:\\DataFlair\\news.csv')
df.shape
df.head()
Output Screenshot:
CRACK INTERVIEW IN THE FIRST ATTEMPT!!!
labels labels=df.label
labels.head()
Output 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.
tfidf_vectorizer=TfidfVectorizer(stop_words='english', max_df=0.7)
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.
pac=PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train,y_train)
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.
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.
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.
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.
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:
If you run into issues installing librosa with pip, you can try it with conda.
import librosa
import soundfile
import numpy as np
from sklearn.model_selection import train_test_split
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
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.
X = sound_file.read(dtype="float32")
sample_rate=sound_file.samplerate
if chroma:
stft=np.abs(librosa.stft(X))
result=np.array([])
if mfcc:
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.
emotions={
'01':'neutral',
'02':'calm',
'03':'happy',
'04':'sad',
'05':'angry',
'06':'fearful',
'07':'disgust',
'08':'surprised'
Screenshot:
Facing Failure in Interview?
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=[],[]
file_name=os.path.basename(file)
emotion=emotions[file_name.split("-")[2]]
continue
x.append(feature)
y.append(emotion)
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.
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:
print((x_train.shape[0], x_test.shape[0]))
Screenshot:
{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.
Screenshot:
9. Fit/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).
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.
accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)
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.
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 –
So, let’s start the Python Machine Learning Project with the introduction of terms used –
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.
Prerequisites
You’ll need to install the following libraries with pip:
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.
import numpy as np
import pandas as pd
Screenshot:
2. Now, let’s read the data into a DataFrame and get the first 5 records.
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.
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.
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.
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.
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.
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.
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.
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 –
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.
First introducing you with the terminologies used in this advanced python project of gender and age detection –
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.
It has 2 fully connected layers, each with 512 nodes, and a final output layer of softmax type.
Detect faces
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-
Other packages you’ll be needing are math and argparse, but those come as part of the standard Python library.
opencv_face_detector.pbtxt
opencv_face_detector_uint8.pb
age_deploy.prototxt
age_net.caffemodel
gender_deploy.prototxt
gender_net.caffemodel
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.
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.
12. We’ll add the gender and age texts to the resulting image and display it with imshow().
We’ll get to the command prompt, run our script with the image option and specify an image to classify:
Output:
Python Project Example 2
Output:
Output:
Python Project Example 4
Output:
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 –
Get aware with the terms used in Breast Cancer Classification project in Python
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.
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.
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
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_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.
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:]
if not os.path.exists(basePath):
os.makedirs(basePath)
file=path.split(os.path.sep)[-1]
label=file[-5:-4]
labelPath=os.path.sep.join([basePath,label])
if not os.path.exists(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:
Perform max-pooling
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(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=channelDim))
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")
import numpy as np
import os
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=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)
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.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.
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
Starting with our first part that is Python Interview Questions and Answers for freshers. I have divided this blog
into different categories.
If it makes for an introductory language to programming, Python must mean something. These are its qualities:
Interpreted
Dynamically-typed
Object-oriented
Free
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
mytuple[1]=2
If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.
>>> a,b=2,3
>>> min
Hi
>>> mylist=[0,1,2,3,4,5,6,7,8]
A negative index, unlike a positive one, begins searching from the right.
>>> mylist[-3]
>>> mylist[-6:-1]
[3, 4, 5, 6, 7]
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):
Myname
As you can see, this raised a NameError. This means that Python is indeed case-sensitive.
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:
>>> 'AyuShi'.lower()
‘ayushi’
>>> '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
True
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.
pass
>>>
if i==3: break
print(i)
2
Finally, the continue statement skips to the next iteration.
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
The help() function displays the documentation string and help for its argument.
>>> help(copy.copy)
copy(x)
>>> dir(copy.copy)
>>> mydict={'a':1,'b':2,'c':3,'e':5}
>>> mydict.keys()
Practice with Top Python Projects with source code and become job-ready
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’
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.
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?
‘Ayushi’
>>> type(str.capitalize)
<class ‘method_descriptor’>
>>> '@yushi'.capitalize()
‘@yushi’
>>> 'Ayushi123'.isalnum()
True
>>> 'Ayushi123!'.isalnum()
False
>>> '123.3'.isdigit()
False
>>> '123'.isnumeric()
True
>>> 'ayushi'.islower()
True
>>> 'Ayushi'.isupper()
False
>>> 'Ayushi'.istitle()
True
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:
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’>
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> os.getcwd()
‘C:\\Users\\lifei\\Desktop’
>>> 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]
>>> a.reverse()
>>> a
[4, 3, 2, 1]
>>> 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.
>>>
If you have worked with the IDLE, you will see this prompt.
return a+b
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
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.
>>> 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
>>> max('fly{}iNg')
‘}’
(Bonus: } – 125)
Python is a jack of many trades, check out Applications of Python to find out more.
Desktop GUI
Applications in Education
Applications in Business
Database Access
Network Programming
Games, 3D Graphics
Q.26. Can you name ten built-in functions in Python and explain each in brief?
>>> complex(3.5,4)
(3.5+4j)
>>> eval('print(max(22,22.0)-min(2,3))')
20
filter()- Filters in items for which the condition is true.
[2, 0, False]
>>> hash(3.7)
644245917
>>> hex(14)
‘0xe’
Enter a number7
‘7’
>>> len('Ayushi')
>>> locals()
>>> file=open('tabs.txt')
>>> word[:3]+word[3:]
The output is ‘abcdefghij’. The first slice gives us ‘abc’, the next gives us ‘defghij’.
>>> nums=['one','two','three','four','five','six','seven']
>>> s
>>> list=[1,2,1,3,4,2]
>>> set(list)
{1, 2, 3, 4}
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.
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.
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]
>>> roots
{25: 5, 16: 4, 9: 3, 4: 2, 1: 1}
The // operator performs floor division. It will return the integer part of the result on division.
>>> 7//2
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
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
>>> {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
>>> 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
False
True
>>> not 2==2
False
With the operators ‘in’ and ‘not in’, we can confirm if a value is a member in another.
True
True
The operators ‘is’ and ‘is not’ tell us if two values have the same identity.
>>> 10 is '10'
False
True
>>> 3|2
>>> 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.
>>> 4>>2
>>> a=7.0
>>>
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'
name[0]=’Avery’
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’>
>>> squares
Don’t miss the complete guide for Python Data Types and Variables
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.
"""
The function prints Hi
"""
print("Hi")
>>> sayhi()
Hi
If a string contains only numerical characters, you can convert it into an integer using the int() function.
>>> int('227')
227
>>> type('227')
<class ‘str’>
>>> type(int('227'))
<class ‘int’>
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:
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’
Enter a number7
>>> a*=2
>>> a
14
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.
>>> greater(3,3.5)
3.5
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.
if n==1: return 1
return n*facto(n-1)
>>> facto(4)
24
One of the less common functions with beginners, zip() returns an iterator of tuples.
>>> list(zip(['a','b','c'],[1,2,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)))
This is simple. We call the function len() on the string we want to calculate the length of.
13
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.
[1, 3, 5, 7, 9]
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().
False
>>> 4 in {'a':1,'b':2,'c':3,'d':4}.values()
True
>>> '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.
>>> i=0
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:
print(i,end='')
IlovePython
print(s)
I love Python
I love Python
I love Python
I love Python
I love Python
I love 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’
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.
Q.56. Given the first and last names of all employees in your firm, what data type will you use to store it?
{‘first_name’:’Ayushi’,’second_name’:’Sharma’
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’
list.append(val)
return list
>>> list1,list2,list3
([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))
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]
Python is interpreted
Java is platform-independent
Python vs Java, the most commonly asked python interview question for freshers.
Q.62. What is the best code you can write to swap two numbers?
>>> a,b=b,a
>>> a,b=b,a
>>> a,b
(3, 2)
This is one of the most asked interview questions for Python freshers –
First –
Second –
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.
while(n==7):print(n)
>>> counterfunc(7)
7
7
counterfunc(7)
while(n==7):print(n)
KeyboardInterrupt
Python files first compile to bytecode. Then, the host executes them.
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.
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.
#processing statements
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 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
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.
Q.74 Tell me about your best personal project. What challenges did you face, and how did it change the way
you work?
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 and Answers – Prepare with DataFlair’s Python Interview Series
So, without wasting any time let’s start with our first category that is Python project-related Interview Questions
and Answers.
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
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
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.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.
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?
left,right=0,len(string)-1
while right>=left:
if not string[left]==string[right]:
return False
left+=1;right-=1
return True
True
>>> isPalindrome('CC.')
False
Well, there are other ways to do this too. Let’s try using an iterator.
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
True
>>> isPalindrome('CC.')
False
>>> isPalindrome('CCC.')
False
>>> isPalindrome('CCC')
True
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.
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.
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.
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 B():
print(x)
return B
>>> A(7)()
word=’word’
No, these are not optimal. Let’s check the manual for this.
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?
>>> 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().
>>> a=iter([2,4,6,8,10])
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
10
>>> next(a)
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
>>> 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)
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}
>>> myset[0]
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.
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.
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
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.
>>> random.choice(lines)
‘https://data-flair.training/blogs/category/python/’
>>> 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:
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:
Debugger
itertools
container
Let’s read the contents into a list, and then call reversed() on it:
print(line.rstrip())
container
itertools
Debugger
Without the rstrip(), we would get blank lines between the output.
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.
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')
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
To generate a random number, we import the function random() from the module random.
>>> random()
0.7931961644126482
>>> help(random)
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.
>>> 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?
>>> urllib.request.urlretrieve('https://yt3.ggpht.com/a-
/ACSszfE2YYTfvXCIVk4NjJdDfFSkSVrLBlalZwYsoA=s900-mo-c-c0xffffffff-rj-k-no','dataflair.jpg')
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.
else- To run a piece of code when the try-block doesn’t create 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?
filter()- This function lets us keep the values that satisfy some conditional logic. Let’s take an example.
{5, 6}
This filters in the elements from 0 to 6 that are greater than the number 4.
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.
2-1=1
3-1=2
4-2=2
5-2=3
Hence, 3.
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.
To start debugging, we first open the command prompt and get to the location the file is at.
C:\Users\lifei> cd Desktop
C:\Users\lifei\Desktop>
> c:\users\lifei\desktop\try.py(1)<module>()
(Pdb)
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.
>>> 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.
>>> arr=np.array([1,2,3,4,5])
>>> p=np.percentile(arr,50)
>>> p
3.0
a. Option 1
>>> numpy.array([])
array([], dtype=float64)
b. Option 2
>>> numpy.empty(shape=(0,0))
We have so far seen them used together. But they have subtle differences:
SciPy encompasses most new features
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
First method-
>>> numpy.array([])
array([], dtype=float64)
Second method-
>>> numpy.empty(shape=(0,0))
If you want more advanced questions, refer to Python Data Science Interview Questions and Answers
>>> class A:
def func(self):
print("Hi")
>>> 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.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’
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’.
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.
We use the cgi module for this; we borrow the FieldStorage class from it. It has the following attributes:
>>> item='milk'
>>> groceries=[]
>>> groceries.append(item)
>>> groceries
[‘milk’]
>>> items=groceries
>>> item='cheese'
>>> items.append(item)
>>> item
‘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
num+=1
>>> up(a)
>>> a
------------------
------------------
>>> a=[1]
list[0]+=1
>>> up(a)
>>> a
[2]
Variables are names bound to an object, not aliases for actual memory locations.
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.
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.
self.color='orange'
self.type='citrus'
def setsize(self,size):
self.size=size
def show(self):
>>> o=orange()
>>> o.setsize(7)
>>> o.show()
In this code, we see that it is necessary to pass the parameter ‘self’ to tell Python it has to work with this object.
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.
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.
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):
def sayhello(self):
>>> a=A()
>>> b=B()
>>> a.sayhello()
Hello, I’m A
>>> b.sayhello()
Hello, I’m B
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
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.
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.
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.
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
Flask-WTF supports simple integration with WTForms. It has the following features-
Recaptcha supporting
Internationalization integration
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.
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.
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.
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)
Now, we will unpack the values from the tuple into variables x, y, and z.
>>> x,y,z=mytuple
>>> x+y+z
12
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.
>>> Ayushi.Chemistry
95
As you can see, it let us access the marks in Chemistry using the Chemistry attribute of object Ayushi.
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’.
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.
for i in odd:
yield i
print(i)
1
3
5
7
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.