Introduction to Python Programming
Contents to be covered
Introduction of Python
Features of Python
Applications of Python
Flavors of Python
Byte Code, Memory Management & Garbage Collection
Python Virtual Machine (PVM)
Installation of Python
Execution of program
Managing Input and Output with examples
Introduction
Python is a popular programming language.
It combines the features of C and Java.
It was created by Guido van Rossum, and released in 1991.
The name "Python" was adopted from the Rossum’s favourite comedy series "Monty Python's Flying Circus".
Python is mainly interpreted language. It is an open source software.
It is used for web development (server-side), software development, mathematics, system scripting etc.
In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce.
Python 2.0 added new features such as list comprehensions, garbage collection systems.
On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify the fundamental flaw of the lan
Features of Python
The following are some of the important features of
Simple: It is a simple programming language. When we read a Python program, we feel like reading English sentences.
Easy to learn:■It uses very few keywords. Its programs use very simple■structure. So, developing programs in Python becom
Open Source:■There is no need to pay for Python software. It can be freely downloaded from [Link]. website.
Dynamically Typed:■In python, we need not declare anything. An■assignment statement binds a name to an object, and the
Platform independent:■ Python program are not dependent on any specific operating system, we can use Python almost all o
Portable: When a program yields the same result on any
computer in the world, then it is called a portable program.
Pyt
Features of Python [Contd..]
Application of Python
General purpose language: Used to create Machine learning, Web applications/development, GUI, Software development.
Used alongside software to create workflows.
Connect to database systems. It can also read and modify files.
Can be used to handle big data and perform complex mathematics.
Can be used for rapid prototyping, or for production-ready software development.
Top companies using Python: Google, Dropbox, Youtube, Quora, Yahoo, NASA, Reddit
Flavors of Python
Difference Between C and Python
Byte Code
Python is usually called an interpreted language, however, it combines compiling and interpreting.
When we execute a source code (a file with a .py extension).
Python first compiles it into a byte code.
The byte code is a low-level platform-independent representation of your source code, however, it is not the binary machine c
In fact, it is a set of instructions for a virtual machine which is called the Python Virtual Machine (PVM).
Byte Code
After compilation, the byte code is sent for execution to the PVM.
The PVM is an interpreter that runs the byte code and is part of the Python system.
The byte code is platform-independent, but PVM is specific to the target machine.
The default implementation of the Python programming language is CPython which is written in the C programming language.
CPython compiles the python source code into the byte code, and this bytecode is then executed by the CPython virtual mach
Memory Management in Python
Memory allocation and de-allocation are done during run time automatically.
The programmer need not allocate memory while creating objects or de-allocate memory when deleting the objects.
Python PVM will take care of such issues.
Everything is considered as an object in Python. For every object memory should be allocated.
Memory manager inside the PVM allocates memory required for objects created in a python program.
All these objects are stored on a separate memory called heap.
Heap is the memory which allocated during run time.
The size of the heap memory depends on the RAM of our computer and it can increase or decrease.
Garbage Collection in Python
A module represents Python code that performs a specific task. Garbage collector is a module in Python that is useful to delet
The module that represents the garbage collector is named as gc.
Garbage collector in the simplest way to maintain a count for each object regarding how many times that object is referenced
When an object is referenced twice, its reference count will be 2. when an object has some count, it is being used in the progr
Garbage Collection in Python
When an object is found with reference count 0.
Garbage collector will understand that the object is not used by the program and hence it can be deleted from memory.
Garbage collector can detect reference cycles.
A reference cycle is a cycle of references pointing to the first object from last object.
Internal Working of Python
Python uses code modules that are interchangeable instead of a single long list of instructions that was standard for functiona
The standard implementation of python is called “cpython”. It is the default and widely used implementation of the Python.
Python doesn’t convert its code into machine code, something that hardware can understand.
It is into byte code and this byte code can’t be understood by CPU. So we need actually an interpreter called the python virtua
The python virtual machine executes the byte codes.
Internal Working of Python
PVM is nothing but a software/interpreter that converts the byte code to machine code for given operating system.
PVM is also called Python Interpreter and this is the reason Python is called an Interpreted language.
Python file first get compiled to give us byte code and that byte code is interpreted into machine language.
This is performed by PVM.
Execution:
Installation of Python
Go to: [Link]/downloads
Click the “download python” button, save it and run.
Follow the step by step process of installation wizard.
An Open File - Security Warning pop-up window will appear.
Click Run. A Python 3.8.5 Setup pop-up window will appear.
Ensure that the Install launcher for all users
(recommended) and the Add Python 3.8 to PATH
checkboxes at the bottom are
Installation of Python [Contd..]
A new Python 3.8.5 Setup pop-up window will appear with a
Setup Progress message and a progress bar. Pop-up
window will
Installation of Python [Contd..]
How to use Python
There are three ways of executing a python program.
Using Python’s Command line window
Using Python IDLE graphics window
Directly from System prompt
The first two are called interactive modes where we can type the program one line at a time and the PVM executes it immedia
The last one is called non-interactive mode where the PVM executes program after typing the entire program.
Using Python’s Command line window
Open the Python command line window
>>> (symbol) which is called Python prompt
Type program at the >>> prompt
After typing the last line and pressing enter button, it display the result. ■ After that, type exit() or quit() to close Python co
Using Python IDLE graphics window
Click the Python’s IDLE (Integrated Development Environment) window
Type program :
To terminate the IDLE window, type exit() or quit(). It will display a message as to kill the■ program or not.
Directly from System prompt
Open a text editor and type the program
Save the program by clicking File ■ Save As, type the program name with extension .py
Open the command prompt
Go to that directory where the program is saved
Execute the program by calling the python command
c:\xyz>python pro_name.py
Python First program
Open Python IDLE (white window)
Click on File ■New or Press Ctrl+N
Type:■■
Save it as [Link] in the computer.
For Execution
Click on Run ■ Run Module or Press F5
Now the program will be executed.
Input Function
To accept input from keyboard, Python provides the input() function. This function takes a value from the keyboard and return
Example:
■str=input('enter you city: ')
■print(str)
■str=input('enter a number: ')
■x=int(str)
■print(x)
■x=int(input('enter any no.: '))
■print(x)
■
■x=float(input('enter any no.: '))
■print(x)
Output:
enter you city: gorakhpur
gorakhpur
enter a number: 4
4
enter any no.: 54
54
enter any no.: 37.5
37.5
Python Programming
Comments – Everything after # on a line is ignored.
Blocks and Indentation – It follows the block structure and nested block structure with indentation.
ABC Block-1
ABC Block-2
ABC Block-3
Lines – Statement separator is a semi colon, but needed only when there are more than one statement on a line.
File Extension- Program have the File Extension “.py”.
Python Programming
Creating Variables
Variables are containers for storing data values.
Python has no command for declaring a variable.
A variable is created at the time, first value assign to it.
x= 5 # Numeric Variable
y= “Ajay“ # Character Variable
print(x)
print(y)
Python Programming
Rules for Variable Naming
A variable can have a short name (like x and y) or a more descriptive name (age, EmpName, total_volume).
Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
Python Programming
Reserve Words – It can not be used as constant or any other identifier name.
and ■exec ■not ■assert ■finally ■or ■break
for■pass ■class ■from ■■ ■■continue
global ■raise ■def ■if ■■return ■del ■import
try ■elif ■in ■while ■■else ■■is ■with
except ■yield ■lambda
List of the Python keywords
>>>help("keywords")
False class from or None continue global pass
True def if raise and del import return
as elif in try assert else is while
async except lambda with await finally nonlocal yield
break for not
Another way of access to the Python keywords:
>>>import keyword
>>> [Link]
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
>>> print(len([Link])) #total keywords
35
Python Programming
Basic Data Types
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her ad
Python has five standard data types −
Numbers
String
List
Tuple
Dictionary
Data types: Hierarchical View
Python Programming
Python Identifiers
It is a name used to identify a variable, function, class, module or other objects in Python program.
An identifier starts with a letter (A to Z) or (a to z) or an underscore (_) followed by zero or more letters, underscores and digits
Python does not allow the characters - @, $, and % .
Valid Identifier- myvar, my_var , _my_var, myVar, MYVAR, myvar2