python_tutorial
python_tutorial
What is Python?
Python is a very popular general-purpose interpreted, interactive, object-oriented, and
high-level programming language. Python is dynamically-typed and garbage-collected
programming language. It was created by Guido van Rossum during 1985- 1990. Like
Perl, Python source code is also available under the GNU General Public License (GPL).
Python Jobs
Today, Python is very high in demand, and all the major companies are looking for great
Python programmers to develop websites, software components, and applications or to
work with data science, AI, and ML technologies. When we were developing this tutorial in
2022, there was a high shortage of Python programmers, where the market demanded a
greater number of Python programmers due to its applications in machine learning,
artificial intelligence, etc.
Today, a Python programmer with 3-5 years of experience is asking for around $150,000
in an annual package, and this is the most demanding programming language in America.
Though it can vary depending on the location of the job. It's impossible to list all of the
companies using Python, to name a few big companies are:
Google
Intel
NASA
PayPal
Facebook
IBM
Amazon
Netflix
Pinterest
Uber
Many more...
i
Python Tutorial
So, you could be the next potential employee for any of these major companies. We have
developed great learning material for you to learn Python programming, which will help
you prepare for the technical interviews and certification exams based on Python. So, start
learning Python using this simple and effective tutorial from anywhere and anytime,
absolutely at your pace.
Try to click the icon to run the following Python code to print conventional "Hello,
World!".
ii
Python Tutorial
Below code box allows you to change the value of the code. Try to change the
value inside print() and run it again to verify the result.
Characteristics of Python
Following are important characteristics of Python Programming −
It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled for building large
applications.
It provides very high-level dynamic data types and supports dynamic type
checking.
It supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Applications of Python
Python is a general purpose programming language known for its readability. It is widely
applied in various fields.
In Data Science, Python libraries like Numpy, Pandas, and Matplotlib are used for
data analysis and visualization.
Python frameworks like Django, and Pyramid, make the development and
deployment of Web Applications easy.
This programming language also extends its applications to computer vision and
image processing.
It is also favored in many tasks like Automation, Job Scheduling, GUI development,
etc.
iii
Python Tutorial
Features of Python
The latest release of Python is 3.x. As mentioned before, Python is one of the most widely
used languages on the web. I'm going to list a few of them here:
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
A broad standard library − Python has a bulk of portable and cross-platform
libraries and they are compatible with UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode that allows
interactive testing and debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
Databases − Python provides interfaces to all major commercial databases.
GUI Programming − Python supports GUI applications that can be created and
ported to many system calls, libraries, and operating systems, such as Windows,
MFC, Macintosh, and the X Window system of Unix.
Scalable − Python provides a better structure and support for large programs than
shell scripting.
Python Reference
The complete function and method references −
Python Complete Reference
Python Built-in Functions Reference
Python Modules Reference
Python Keywords Reference
Python Cheatsheet
Python Practice
Practice Python from the below-given links:
Python Quick Guide
Python Online Quiz
Python Interview Questions & Answers
Download Python
You can download Python from its official website: https://www.python.org/downloads/
Target Audience
This tutorial has been prepared for the beginners to help them understand the basics to
advanced concepts of Python programming language. After completing this tutorial, you
will find yourself at a great level of expertise in Python programming, from where you can
take yourself to the next levels.
iv
Python Tutorial
Prerequisites
Although it is a beginner’s tutorial, we assume that the readers have a reasonable
exposure to any programming environment and knowledge of basic concepts such as
variables, commands, syntax, etc.
v
Python Tutorial
Table of Contents
About the Tutorial ............................................................................................................................................ i
Prerequisites ................................................................................................................................................... v
PYTHON BASICS......................................................................................................... 1
3. Python - Features............................................................................................................................................ 7
4. Python vs C++................................................................................................................................................ 11
vi
Python Tutorial
vii
Python Tutorial
viii
Python Tutorial
ix
Python Tutorial
x
Python Tutorial
xi
Python Tutorial
xii
Python Tutorial
xiii
Python Tutorial
xiv
Python Tutorial
xv
Python Tutorial
Python Basics
1
1. Python – Overview Python Tutorial
>>>import this
2
Python Tutorial
3
2. Python - History and VersionsPython Tutorial
History of Python
Python was developed by Guido van Rossum (a Dutch programmer) in the late 1980s and
early nineties at the National Research Institute for Mathematics and Computer Science in
the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-
68, SmallTalk, and Unix shell and other scripting languages. Guido van Rossum wanted
Python to be a high-level language that was powerful yet readable and easy to use.
Python is copyrighted. Like Perl, Python source code is now available under the GNU
General Public License (GPL).
Being the principal architect of Python, the developer community conferred upon him the
title of Benevolent Dictator for Life (BDFL). However, in 2018, Rossum relinquished the
title. Thereafter, the development and distribution of the reference implementation of
Python is handled by a nonprofit organization Python Software Foundation.
Python 0.9.0
Python's first published version is 0.9. It was released in February 1991. It consisted of
features such as classes with inheritance, exception handling, and core data types like lists
and dictionaries.
Python 1.0
In January 1994, version 1.0 was released, armed with functional programming tools,
features like support for complex numbers etc. and module system which allows a better
code organization and reuse.
Python 2.0
Next major version − Python 2.0 was launched in October 2000. Many new features such
as list comprehension, garbage collection and Unicode support were included with it.
Throughout the 2000s, Python 2.x became the dominant version, gaining traction in
industries ranging from web development to scientific research. Various useful libraries
like NumPy, SciPy, and Django were also developed.
4
Python Tutorial
Python 3.0
Python 3.0, a completely revamped version of Python was released in December 2008.
The primary objective of this revamp was to remove a lot of discrepancies that had crept
in Python 2.x versions. Python 3 was backported to Python 2.6. It also included a utility
named as python2to3 to facilitate automatic translation of Python 2 code to Python 3.
Python 3 provided new syntax, Unicode support and improved integer division.
5
Python Tutorial
6
3. Python - Features Python Tutorial
Features of Python
Python's most important features are as follows:
Easy to Learn
Dynamically Typed
Interpreter Based
Interactive
Multi-paradigm
Standard Library
Open Source and Cross Platform
GUI Applications
Database Connectivity
Extensible
Active Developer Community
Easy to Learn
This is one of the most important reasons for the popularity of Python. Python has a limited
set of keywords. Its features such as simple syntax, usage of indentation to avoid clutter
of curly brackets, and dynamic typing that doesn't necessitate prior declaration of variable
help a beginner to learn Python quickly and easily.
7
Python Tutorial
Dynamically Typed
Python is a dynamically typed programming language. In Python, you don't need to specify
the variable at the time of the declaration. The types are specified at the runtime based
on the assigned value due to its dynamically typed feature.
Interpreter Based
Instructions in any programming languages must be translated into machine code for the
processor to execute them. Programming languages are either compiler based or
interpreter based.
In case of a compiler, a machine language version of the entire source program is
generated. The conversion fails even if there is a single erroneous statement. Hence, the
development process is tedious for the beginners. The C family languages (including C,
C++, Java, C# etc.) are compiler based.
Python is an interpreter based language. The interpreter takes one instruction from the
source code at a time, translates it into machine code and executes it. Instructions before
the first occurrence of error are executed. With this feature, it is easier to debug the
program and thus proves useful for the beginner level programmer to gain confidence
gradually. Python therefore is a beginner-friendly language.
Interactive
Standard Python distribution comes with an interactive shell that works on the principle of
REPL (Read – Evaluate – Print – Loop). The shell presents a Python prompt >>>. You can
type any valid Python expression and press Enter. Python interpreter immediately returns
the response and the prompt comes back to read the next expression.
>>> 2*3+1
7
>>> print ("Hello World")
Hello World
The interactive mode is especially useful to get familiar with a library and test out its
functionality. You can try out small code snippets in interactive mode before writing a
program.
Multi-paradigm
Python is a completely object-oriented language. Everything in a Python program is an
object. However, Python conveniently encapsulates its object orientation to be used as an
imperative or procedural language – such as C. Python also provides certain functionalities
that resemble functional programming. Moreover, certain third-party tools have been
developed to support other programming paradigms such as aspect-oriented and logic
programming.
Standard Library
Even though it has a very few keywords (only Thirty-Five), Python software is distributed
with a standard library made of large number of modules and packages. Thus Python has
out of box support for programming needs such as serialization, data compression,
8
Python Tutorial
internet data handling, and many more. Python is known for its batteries included
approach.
Some of the Python's popular modules are:
NumPy
Pandas
Matplotlib
Tkinter
Math
GUI Applications
Python's standard distribution has an excellent graphics library called TKinter. It is a
Python port for the vastly popular GUI toolkit called TCL/Tk. You can build attractive user-
friendly GUI applications in Python. GUI toolkits are generally written in C/C++. Many of
them have been ported to Python. Examples are PyQt, WxWidgets, PySimpleGUI etc.
Database Connectivity
Almost any type of database can be used as a backend with the Python application. DB-
API is a set of specifications for database driver software to let Python communicate with
a relational database. With many third party libraries, Python can also work with NoSQL
databases such as MongoDB.
Extensible
The term extensibility implies the ability to add new features or modify existing features.
As stated earlier, CPython (which is Python's reference implementation) is written in C.
Hence one can easily write modules/libraries in C and incorporate them in the standard
library. There are other implementations of Python such as Jython (written in Java) and
IPython (written in C#). Hence, it is possible to write and merge new functionality in these
implementations with Java and C# respectively.
9
Python Tutorial
10
4. Python vs C++ Python Tutorial
What is Python?
Python is currently one of the most widely used programming languages. It is an
interpreted programming language that operates at a high level. When compared to other
languages, the learning curve for Python is much lower, and it is also quite straightforward
to use.
Python is the programming language of choice for professionals working in fields such as
Artificial Intelligence, Machine Learning (ML), Data Science, the Internet of Things (IoT),
etc., because it excels at both scripting applications and as standalone programmers.
In addition to this, Python is the language of choice because it is easy to learn. Because
of its excellent syntax and readability, the amount of money spent on maintenance is
decreased. The modularity of the program and the reusability of the code both contribute
to its support for a variety of packages and modules.
Using Python, we can perform −
Web development
Data analysis and machine learning
Automation and scripting
Software testing and many more
Features
Here is a list of some of the important features of Python −
Easy to learn − Python has a simple structure, few keywords, and a clear syntax.
This makes it easy for the student to learn quickly. Code written in Python is easier
to read and understand.
Easy to maintain − The source code for Python is pretty easy to keep up with.
A large standard library − Most of Python's libraries are easy to move around
and work on UNIX, Windows, Mac.
Portable − Python can run on a wide range of hardware platforms, and all of them
have the same interface.
Python Example
11
Python Tutorial
In our example, we have taken two variables "a" and "b" and assigning some value to
those variables. Note that in Python, we don’t need to declare datatype for variables
explicitly, as the PVM will assign datatype as per the user’s input.
The input() function is used to take input from the user through keyboard.
In Python, the return type of input() is string only, so we have to convert it
explicitly to the type of data which we require. In our example, we have converted
to int type explicitly through int( ) function.
print() is used to display the output.
Output
On execution, this Python code will produce the following output −
What is C++?
C++ is a statically typed, compiled, multi-paradigm, general-purpose programming
language with a steep learning curve. Video games, desktop apps, and embedded systems
use it extensively. C++ is so compatible with C that it can build practically all C source
code without any changes. Object-oriented programming makes C++ a better-structured
and safer language than C.
Features
Let’s see some features of C++ and the reason of its popularity.
Middle-level language − It's a middle-level language since it can be used for
both system development and large-scale consumer applications like Media
Players, Photoshop, Game Engines, etc.
Execution Speed − C++ code runs quickly because it's compiled and uses
procedures extensively. Garbage collection, dynamic typing, and other modern
features impede program execution.
12
Python Tutorial
C++ Example
Let’s understand the syntax of C++ through an example written below.
#include
using namespace std;
int main() {
int a, b;
cout << "Enter The value for variable a \n";
cin >> a;
cout << "Enter The value for variable b";
cin >> b;
cout << "The value of a is "<< a << "and" << b;
return 0;
}
In our example, we are taking input for two variables "a" and "b" from the user through
the keyboard and displaying the data on the console.
Output
On execution, it will produce the following output −
Compiled vs Interpreted
Like C, C++ is also a compiler-based language. A compiler translates the entire code in a
machine language code specific to the operating system in use and processor architecture.
Python is interpreter-based language. The interpreter executes the source code line by
line.
Cross platform
When a C++ source code such as hello.cpp is compiled on Linux, it can be only run on any
other computer with Linux operating system. If required to run on other OS, it needs to
be recompiled.
13
Python Tutorial
Python interpreter doesn't produce compiled code. Source code is converted to byte code
every time it is run on any operating system without any changes or additional steps.
Portability
Python code is easily portable from one OS to other. C++ code is not portable as it must
be recompiled if the OS changes.
Speed of Development
C++ program is compiled to the machine code. Hence, its execution is faster than
interpreter based language.
Python interpreter doesn't generate the machine code. Conversion of intermediate byte
code to machine language is done on each execution of program.
If a program is to be used frequently, C++ is more efficient than Python.
Easy to Learn
Compared to C++, Python has a simpler syntax. Its code is more readable. Writing C++
code seems daunting in the beginning because of complicated syntax rules such as use of
curly braces and semicolon for sentence termination.
Python doesn't use curly brackets for marking a block of statements. Instead, it uses
indents. Statements of similar indent level mark a block. This makes a Python program
more readable.
OOP Concepts
Both C++ and Python implement object oriented programming concepts. C++ is closer to
the theory of OOP than Python. C++ supports the concept of data encapsulation as the
visibility of the variables can be defined as public, private and protected.
Python doesn't have the provision of defining the visibility. Unlike C++, Python doesn't
support method overloading. Because it is dynamically typed, all the methods are
polymorphic in nature by default.
C++ is in fact an extension of C. One can say that additional keywords are added in C so
that it supports OOP. Hence, we can write a C type procedure oriented program in C++.
Python is completely object oriented language. Python's data model is such that, even if
you can adapt a procedure oriented approach, Python internally uses object-oriented
methodology.
Garbage Collection
14
Python Tutorial
C++ uses the concept of pointers. Unused memory in a C++ program is not cleared
automatically. In C++, the process of garbage collection is manual. Hence, a C++ program
is likely to face memory related exceptional behavior.
Python has a mechanism of automatic garbage collection. Hence, Python program is more
robust and less prone to memory related issues.
Application Areas
Because C++ program compiles directly to machine code, it is more suitable for system
programming, writing device drivers, embedded systems and operating system utilities.
Python program is suitable for application programming. Its main area of application today
is data science, machine learning, API development etc.
Python is a dynamic-typed
Typing C++ is a static-typed language.
language.
15
Python Tutorial
==========
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com
16