Python Programming
An Introduction
Learning objective
• What is Python Programming?
• Interpreter vs Compiler
• Functions of Python Interpreter
• History of Python
• Features of Python
• Why should we learn Python?
• Application of Python Programming
What is Python Programming
• Python is a high level, interpreted, intelligent, and object-oriented
scripting language.
• Python is handled at runtime by the interpreter. You don't need to
compile your code before executing it.
• Learning Python Programming is as easier as C language.
• Python's simple, easy to learn syntax emphasizes readability and
therefore reduces the cost of program maintenance.
What is Python Programming
• Python supports modules and packages, which encourages
program modularity and code reuse.
• The Python interpreter and the extensive standard library are
available in source or binary form without charge for all major
platforms and can be freely distributed.
Interpreter vs Compiler
• An Interpreter is a program that executes Python code.
• An Interpreter translates code written in high-level programming
language into machine code line-by-line as the code runs.
• Compiler converts code into machine code before execution.
• A compiler translates code from a high-level programming
language into machine code as a whole before the program runs.
Functions of Python Interpreter
• Read (Lexical Analysis):
• The interpreter reads the source code (Python script) and breaks it into tokens
(smallest elements like keywords, identifiers, etc.).
• Parse (Syntax Analysis):
• Checking if the code contains any syntax errors.
• Translate (Intermediate Code Generation):
• The interpreter converts the source code into bytecode, which is an intermediate,
platform-independent representation of the code.
• Bytecode files have a .pyc extension and are stored in the __pycache__ folder.
• Bytecode improves performance by skipping the need to re-interpret unchanged
files.
Functions of Python Interpreter
• Execute (Runtime):
• The bytecode is executed by the Python Virtual Machine (PVM), which is part
of the interpreter.
• The PVM converts bytecode into machine-specific instructions and interacts
with the operating system to execute them.
• Handle Errors:
• If the interpreter encounters errors during execution, it halts the process and
provides a detailed error message.
__pycache__ folder
• Inside __pycache__ folder, the code is not stored in plain Python
syntax. Though, we can view the files in the __pycache__ folder.
• The files in the __pycache__ folder are compiled bytecode files
with names like your_script.cpython-<version>.pyc.
• These are not directly in the human-readable format.
Use dis module to see the bytecode
• Create a script test.py
print("Welcome to Python")
• Run the script to generate a bytecode file in __pycache__:
myProject2\Test> python.exe .\hello.py
• Use the dis module to examine the bytecode:
import dis
dis.dis(compile(open('hello.py').read(), 'hello.py', 'exec'))
Changing location of __pycache__ folder
• Modify the location of the __pycache__ folder by setting the
PYTHONPYCACHEPREFIX environment variable.
• Open Command Prompt.
C:\>set PYTHONPYCACHEPREFIX=C:\CustomCacheLocation
• Run your script again, and the compiled files will now be stored in
the specified folder.
History of Python Programming
• Python was created by Guido van Rossum.
• The design began in the late 1980.
• Soon after that, Guido Van Rossum began doing its application-
based work in December of 1989 by at Centrum Wiskunde &
Informatica (CWI) which is situated in Netherlands.
• The first release was in February 1991.
History of Python Programming
• “Python is an experiment in how much freedom programmers
need. Too much freedom and nobody can read another's code.
Too little and expressiveness is endangered.”
• Guido van Rossum
Why was it named Python?
• It wasn't named after a dangerous snake.
• Rossum was fan of a comedy series in late seventies.
• The name "Python" was adopted from the same series "Monty
Python's Flying Circus".
Features of Python
• Python is Interpreted: Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is
similar to PERL and PHP.
• Python is Interactive: You can actually sit at a Python prompt and
interact with the interpreter directly to write your programs.
• Python is Object-Oriented: Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
• Python is a Beginner's Language: Python is a great language for the
beginner-level programmers and supports the development of a wide
range of applications from simple text processing to WWW browsers to
games.
Features of Python
• A simple language which is easier to learn
• Free and open-source
• Portability
• Extensible and Embeddable
• A high-level, interpreted language
• Large standard libraries to solve common tasks
Why should we learn Python?
• In an independent 3rd party survey, it has been found that the
Python programming language is currently the most popular
language for Data Scientists worldwide.
• This claim is substantiated by IEEE, which tracks programming
languages by popularity.
• According to them, Python tops the list of the most popular
programming languages in 2018
Why should we learn Python?
• According to Indeed, the average Python developer salary in the
US in 2020 is $120K per year (or $56.75 per hour), which grew by
14% for the last 4 years.
• The entry-level Python developer salary in the USA is $74,435.
• Middle developers earn $100,826.
• Experienced Python developers are paid on average $127,217 per
year
Why should we learn Python?
• Python is used in Machine Learning and Artificial Intelligence
• Python is used in Web development
• Python has a large community
• Python is used in Scripting and Automation
• Python has many libraries and frameworks
Application of Python Programming
• Python supports cross-platform operating systems which makes
building applications with it more convenient.
• Some of the globally known applications such as YouTube,
BitTorrent, DropBox, etc. use Python to achieve their functionality.
Application of Python Programming
• Web Development
• Game Development
• Scientific and Numeric Applications
• Artificial Intelligence and Machine Learning
• Software Development
• Enterprise-level/Business Applications
• Education programs and training courses
• Language Development
• Desktop GUI
• Business Applications
You have learnt:
• What is Python Programming?
• Interpreter vs Compiler
• Functions of Python Interpreter
• History of Python
• Features of Python
• Why should we learn Python?
• Application of Python Programming