0% found this document useful (0 votes)
0 views55 pages

Lecture1 Introduction to Python 3 June 2024

The document is an introductory lecture on Python programming by Dr. Sunita Varma, covering the basics of programming, the history and features of Python, and its various implementations. It explains concepts such as classes and objects, the execution process of Python programs, and the Python Virtual Machine. Additionally, it discusses the extensive libraries available in Python and the different flavors of Python compilers for various applications.

Uploaded by

kumud yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views55 pages

Lecture1 Introduction to Python 3 June 2024

The document is an introductory lecture on Python programming by Dr. Sunita Varma, covering the basics of programming, the history and features of Python, and its various implementations. It explains concepts such as classes and objects, the execution process of Python programs, and the Python Virtual Machine. Additionally, it discusses the extensive libraries available in Python and the different flavors of Python compilers for various applications.

Uploaded by

kumud yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Lecture 1

Introduction to Python
By
Dr. Sunita Varma
What is Programming
• Word Program can be traced back to the Greek words Pro (before)
and Graphein (write)
• In other words it was the act of writing something in advance or
ahead-of-time
• Process of human writing computer code is analogous to one human
giving instructions to follow
What is Programming
• Classic example of this comes to play in food-preparation recipes
• Recipe for meal can be boiled-down to a sequence of instructions
that human must follow very carefully in the correct order to arrive at
desired goal: delicious meal
• As rudimentary example let’s use the recipe for making peanut butter
and jelly sandwich
What is Programming
• If you had to write down steps you’d probably tell him something
such as
• Get out two slices of bread jar of peanut-butter and jar of jelly
• Place each piece of bread next to each-other on plate
• Get knife and scoop out few ounces of peanut butter
• Spread peanut butter evenly on one of open bread faces
• Get separate knife and scoop out few ounces of jelly
• On other slice of bread spread evenly
• Fold two places of bread together with peanut butter and jelly sides facing
each other
• enjoy
What is Programming
• Writing computer code or programming computer can be thought of
in similar way to give instructions to another human being
• There are some differences because computer is a very different type
of entity
• Computers are very good at following instructions
• They do exactly what user tell them to do
• Computer are not creature and don’t have intuitions
• Users have to be very specific when telling computer what to do
What is Programming
• If we were to program the computer to make Pb&J’s
• We need to have instructions with much greater detail
• For example:
• Begin at the north-west corner of the kitchen
• Move 1.5 meters south
• Turn 90 degrees left
• Use arm to open the cabinet door in front of you
• Find jar with words peanut butter on it
• Pull said jar out of cabinet and place on counter top immediately below
• Close cabinet door
• Turn 180 degrees right
• Move 1.2 meters forward
• Use arm to open up left refrigerator door in front of you
What is Programming
• Computer programming is language which tell computer what to do
• As with human language there are many different languages use to
communicate instructions
• Plethora of spoken languages are available that human can understand
• In a similar sense there is an abundance of computer languages varieties
with their pros/cons and differences/similarities
• Some of the most commonly used computer languages in the world are:
• Java
• Python
• C
• Java script
Python: History
• Python is programming language that combines the features of C and
Java
• Python was developed by Guido Van Rossum in the year 1991 at the
centre for Mathematics Computer Science managed by the Dutch
Government
• Van Rossum picked the name Python for the new language from TV
show Monty Python’s Flying Circus
• Python’s first working version was ready by early 1990 and Van
Rossum released it for the public on February 20,1991
Python: History
• The logo of python shows two interwind snakes
Python: History
• Python is open source software
• It can be freely downloaded by anybody from www.python.org and
use it to develop program
• Its source code can be accessed and modified as required in the
projects
Features of Python
• Simple
• Easy to Learn
• Open Source
• High level Language
• Dynamically Typed
• Platform Independent
• Portable
• Procedure and Object Oriented
Class and Object
• Class:
• Does not exist physically
• It is only abstract idea which represents common behaviour at several objects
• For example dog is a class
• Object:
• It is anything that exists physically in the real world
• For example dog with the name snoopy
• Snoopy is an object it physically exist
• Objects will have behaviour represented by their attributes and actions
• For example snoopy has attributes like height weight age and colour
• Attributes are represented by variables in programming
• Snoopy can perform actions like barking biting eating running etc.
• Actions are represented by methods in programming
• Objects contains variables and method
Class and Object
• When class is created no memory is allocated because class does not
exist physically
• Object exists physically hence memory is allocated when object is
created
• In Python language everything like variables lists functions arrays etc.
are treated as objects
Interpreted
• Python program code is called source code
• Python compiler use to compile python source code
• Compiler translates python program into intermediate code called
byte code
• Byte code is executed by Python Virtual Machine
• PVM contains interpreter which converts byte code instructions into
machine code
• Machine code will understand by processor
• Processor run machine code to produce results
Extensible
• Programs or pieces of code written in C or C++ can be integrated into
python and executed using PVM
• This is standard python downloaded from www.python.org
• There are other flavours of python where programs from other
languages can be integrated into python
• For example Jython is useful to integrate Java code into python
program and run JVM
• Iron python is useful to integrate .NET programs and libraries into
python programs and run on Common Language Runtime
Embeddable
• Python programs can be inserted into C or C++ program
• Several applications are already developed in python which can be
integrated into other programming languages like:
• C
• C++
• Delphi
• PHP
• Java and
• .NET
Huge Library
• Python has big library which can be used on any operating system
like:
• UNIX
• Windows
• Macintosh
• Programs can very easily be developed using modules available in
python library
Scripting Language
• It is programming language that does not use compiler for executing
source code
• It uses interpreter to translate source code into machine code on fly
• Scripting languages generally perform supporting tasks for bigger
application or software
• For example PHP is scripting language that performs supporting task
of taking input from HTML page and send it to Web Server software
• Python is considered as scripting language as it is interpreted and it is
used on Internet to support other software
Database Connectivity
• Database represents software that stores and manipulates data
• Oracle is popular database
• Python provides interfaces to connect its program to all major
databases like:
• Oracle
• Sybase
• MySql
Scalable
• Program would be scalable if it could be moved to another operating
system or hardware and take full advantage of new environment in
terms of performance
• Python programs are scalable since they can run on any platform and
use features of new platforms effectively
Batteries Included
• Huge library of python contains several small applications which are
already developed and immediately available to programmers
• Small packages of python can be used and maintained easily
• Libraries are called batteries included
• some batteries or packages are
• argparsc is package that represents command line parsing library
• botois Amazon web services library
• cherrPy is object-oriented HTTP framework
• cryptography offers cryptographic techniques for programmers
• fino reads and writes big data files
• Jellyfish is library for doing approximate and phonetic matching strings
Batteries Included
• MySql-connector-python is is driver written in python to connect to
MySql database
• numpy is package for processing arrays of single or multidimensional
type
• pandas is package for powerful data structures for data analysis time
series statistics
• matplotlib is package for drawing electronic circuits and 2D graphs
• pillow is python imaging library
• Pyquerry represents jquery like library for python
Batteries Included
• Scipy is scientific library to do scientific and engineering calculations
• Sphnix is python documentation generator
• Sympy is package for computer algebra system in python
• w3lib is library for web related functions
• whoosh contains fast and pure python full text indexing search and
spell checking library
• To know the entire list of packages included in python visit:
https://www.pythonanywhere.com/battries-included
Execution of Python Program
• Let’s assume the name of the python program is x.py
• Where x is program name and .py is extension name
• Every python program is typed with extension name .py
• Python program with .py extension is compile by using python compiler
• Compiler converts python program into bytecode
• Byte code represents fixed set of instructions which represents all
operations like:
• arithmetic operations
• comparison operations
• Memory related operations etc.
Execution of Python Program
• Bytecode run on any operating system and hardware
• Size of each byte code instructions is 1 byte
• Byte code instructions are contained in file x.pyc
• For running python program byte code is converted into machine
code
• Python Virtual Machine uses interpreter which understands byte
code and convert it into machine code
• PVM understands processor and operating system in computer
• PVM converts byte code into machine code understandable to
processor and into format that understandable to operating system
• Machine code instructions are executed by processor and results are
displayed
Execution of Python Program
• To increase speed of execution of program compiler is added to PVM
in some flavours of python
• This compiler is called Just in Time Compiler
• Standard python software is called cypthon which is created using C
language does not include JIT compiler
• Python software PyPy is created in python language uses JIT compiler
in addition to interpreter in PVM
Execution of Python program

Python Python
101001
program compiled computer
Compile 011101
x.py file x.pyc
using
python
Machine
compiler
code
Execution of Python program
• Steps

Source code Byte code Machine code output


Execution of Python program
• C:\>python x.py
• Compile and run python program

• C:\python –m py_compile x.py


• Create .pyc file from source code

• -m represents module and module name is py_compile


• This module generates .pyc file for specified .py file
Execution of Python program
• Compiler creates separate directory in current directory by name
__pycache__ where it stores .pyc file
• The .pyc file name may be x.cypthon-34.pyc
• Cpython indicates python compiler used was created using c
• .pyc file is interpreted by using PVM
• This can be done by calling python compiler as
c:/python x.cpython-34.pyc
• .pyc file is supplied to python compiler
Execution of Python program
• Python compiler skip first step of converting source code into byte
code as byte code is already available inside the .pyc file
• .pyc file is directly executed by PVM to produce the output
• Program takes less time to run and performance will be improved
• This is reason that after compilation of project .pyc files are
distributed to user who can directly run theses files using PVM and
view the output
Viewing Byte code
• Python program

#python program to add two numbers


a=b=10 #take two variables and store 10 in to them
Print(“Sum =“,(a+b)) #display their sum
Write in note pad first.py
C:\>python first.py
Sum =20
C:\>python –m dis first.py
dis module:-> known as disassembler that displays byte code in human understandable
format
Flavors of Python
• It refers to different types of python compilers
• These flavors are useful to integrate various programming languages into python

• Cpython:
• This is standard python compiler is implemented in C language
• This this python software is being downloaded from https://www.python.org/downloads
• In this compiler any python program is internally converted into byte code using c
language functions
• This byte code is run on interpreter available in Python Virtual Machine created in
C language
• Advantage is that it is possible to execute C and C++ functions and programs in
Cpython
Flavors of Python
• Jython:
• This is earlier known as Jpython
• This is implementation of python programming language which is designed to
run on Java platform
• Jython compiler first compiles python program into Java byte code
• This byte code is executed by Java Virtual Machine to produce output
• Jython contains libraries which are useful for both python and Java
programmers
• This can be downloaded from
http://www.jython.org
Flavors of Python
• Ironpython
• This is implementation of python language .NET framework
• This is written in C# language
• Python program when compiled gives intermediate language which runs on
Common Language Runtime to produce the output
• This flavors of python gives flexibility of using both .NET and python libraries
• IronPython can be downloaded from
• http://ironpython.net
Flavors of Python
• PyPy:
• This is python implementation using python language
• Pypy is written in language called Rpython which was created in python
language
• Rpython is suitable for creating language interpreters
• PyPy programs run very fast since there is Just in Time Compiler added tp
PVM
• PyPy can be downloaded by visiting page
• http://pypy.org/download.thml
• PyPy programs run faster than those of python
• PyPy can be downloaded from
• http://PyPy.org/download.html
Flavors of Python
• RubyPython
• This is bridge between Ruby and Python interpreters
• It encloses python interpreter inside Ruby applications
• This flavor of python can be downloaded from
https://rubygems.org/gems/rubypython/versions/0.6.3
Flavors of Python
• Stackless Python
• Small tasks which should run individually are called tasklets
• Tasklets run independently on CPU and can communicate with others via
channels
• Channel is manager that takes care of scheduling tasklets controlling them
and suspending them
• Thread is process which runs hundreds of such tasklets
• Threads and tasklets can be created in stackless python which is
reimplementation of original python language
Flavors of Python
• Pythonxy
• This is pronounced as python xy and written as python(X,Y)
• This is python implementation in which scientific and engineering related
packages are added
• It can be downloaded from
• http://python-xy.github.io/downloads.html
Flavors of Python
• Anaconda Python
• When python is redeveloped for handling large scale
• data processing
• Predictive analytics
• Scientific computing
It is called Anaconda python
• This implementation mainly focuses on large scale of data
• This can be downloaded from
https://www.continuum.io/downloads
Python Virtual Machine
• Any program source code is converted into machine code because it is
understand by computer
• Python compiler converts program source code into another code called
byte code
• Each python program statements is converted into group of byte code
instructions
• Byte code represents fixed set of instructions created by python developers
representing all types of operations
• The size of each byte code instruction is 1 byte
• Bytecode instructions are available in .pyc file
• Role of Python Virtual Machine is to convert byte code instructions into
machine code so that computer can execute those machine code
instructions and display final output
Python Virtual Machine
• To carry out this conversion PVM is equipped with interpreter
• Interpreter converts byte code into machine code to computer
processor for execution
• PVM is also called interpreter
Frozen Binaries
• There are two ways to provide software developed in python to end
user
• Provide .pyc files to user
• User will install PVM in computer and run byte code instructions of .pyc file
• Provide .pyc files PVM files along with necessary python library
• All .pyc files related python library and PVM will be converted into single executable file
with extension .exe so that user can directly execute that file by double clicking on it
• Converting python program into true executables is called frozen binaries
• Frozen binaries will have more size than that of simple .pyc files since this contain PVM
and library files also
• Other party software are needed for creating frozen binary
• For example py2exe is software that produces frozen binaries for windows and
pyinstaller for UNIX and LINUX
• Freeze is another program from python organisation to generate frozen binaries for UNIX
Memory Management in Python
• Python memory allocation and deallocation are done during runtime
automatically
• Programmer need not allocate memory while creating objects or
deallocate memory when deleting objects
• Python’s PVM will take care of such issues
• Everything is considered as object in python
• Strings lists functions modules etc. are objects
• For every object memory should be allocated
• Memory manager inside PVM allocates memory required for objects
created in python program
Memory Management in Python
• All objects are stored on separate memory called heap
• Heap is memory which is allocated during runtime
• Size of heap memory depends on RAM of computer
• Heap can increase or decrease its size depending on requirement of
program
Memory Management in Python

Object specific Object specific


allocations allocation

Python’s raw memory allocation

Operating system memory allocator

RAM
Memory Management in Python
• Actual memory for any program is allocated by underlying operating
system
• On top of operating system raw memory allocator oversees whether
enough memory is available to it for storing objects
• On top of raw memory allocator there are several object specific
allocators on same heap
• Memory allocators will implement different types of memory
management policies types of memory management policies
depending on type of objects
• Integer number strings tuples dictionary should be stored in different
ways
Garbage Collection in Python
• Module represents python code that perform specific task
• Garbage collector is module in python that is useful to delete objects
from memory which are not used in program
• Garbage collector module name is gc
• Garbage collector is simplest way to maintain count for each object
regarding how many times that object is referenced
• When object is referenced twice its reference count will be 2
• When object has some count it is being used in program and garbage
collector will not remove it from memory
Garbage Collection in Python
• If object is found with reference count 0 garbage collector will
understand that object is not used by program and it can be deleted
from memory
• Memory allocated for that object is deallocated or freed
• Garbage collector can detect reference cycles
• Reference cycle is cycle of references pointing to first object from last
object
• For example take three objects A B and C
• Object A refers object B where as object B holds reference to object C
• If object C refers to first object A it will form reference cycle
Garbage Collection in Python

A B C
Garbage Collection in Python
• Objects B and C are no longer used in python program still these
objects contain 1 reference to each one
• Reference count for each object is 1 garbage collector will not remove
these objects from memory
• These objects stay in memory even after program execution
completes
• Garbage collector uses algorithm for detecting reference cycles and
removing objects in cycle
Garbage Collection in Python
• Garbage collector classifies objects into three generations
• Newly created objects are considered as generation 0 objects
• Generation 1 is first time examine by garbage collector in memory
and if object is used by program it does not remove object from
memory
• Garbage collector intends to delete objects for second time and
objects also survives for second time then it is placed into generation
2
• Garbage collector tries to delete younger objects which are not
referenced in program rather than old objects
Garbage Collection in Python
• Garbage collector runs automatically
• Python schedules garbage collector depending upon number called
threshold
• This number represents frequency of how many times garbage
collector removed objects
• Number of allocations minus number of deallocation is greater than
threshold number garbage collector will run automatically
• Threshold number can be known by using method get threshold of gc
module
• More and more objects are created and if system runs out of memory
then automatic garbage collector will not run
• Instead python program will throw exception
Garbage Collection in Python
• When programmer is sure that his program does not contain any reference
cycles then automatic garbage collector is best suitable
• In some cases where reverence cycles are found in program it is better to
run collector manually
• Manual garbage collection ca be done in two ways:
• Time-based
• Event-based
• If garbage collector is called in certain intervals of time it is called time-
based garbage collection
• If garbage collector is called on basis of event for example when user
disconnected from an application it is called event-based garbage
collection
• Running garbage collector too frequently will slow down program
execution
Thank you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy