Python Programming
• Introduction to Python,
• Features of Python,
• Python Versions,
• Writing and Execution of a Python Program,
• Memory Management
Introduction
► A Dutch programmer, created Python programming
language in the late 80’s.
► Why Name Python??
► Taken from popular comedy series "Monty Python's
Flying Circus" on BBC.
Introduction to
Python
• Python is a general-purpose, high-level programming language with
dynamic semantics.
• It emphasizes code readability and reduces the cost of program
maintenance.
• Python supports multiple programming paradigms, including
object-oriented, functional, and procedural.
• Known for its 'batteries-included' philosophy, Python offers
extensive standard libraries.
• Cross-platform support: Python works seamlessly on Windows,
Linux, and macOS.
+ O
+ SUM
Guido Van Rossum
Features of Python
• Easy-to-learn: Python has relatively few keywords, simple
structure, and a clearly defined syntax.
• Easy-to-read: Python code is much more clearly defined and visible
to the eyes.
• Easy-to-maintain: Python's success is that its source code is fairly
easy-to-maintain.
• A broad standard library: One of Python's greatest strengths is the
bulk of the library is very portable and cross-platform compatible
on UNIX, Windows, and Macintosh.
• Interactive Mode: Support for an interactive mode in which you can
enter results from a terminal right to the language, allowing
interactive testing and debugging of snippets of code.
Features of Python (cont’d)
► 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 windows
systems, such as Windows MFC, Macintosh, and the X Window system
of Unix.
History of Python
• Python was developed by Guido van Rossum in the late 1980s 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.
• First Release: Python 1.0 was introduced in 1991.
• Python is now maintained by a core development team at the
institute, although Guido van Rossum still holds a vital role in
directing it's progress.
• Major versions include Python 2.0 (2000) and Python 3.0 (2008).
• The name Python is inspired by 'Monty Python's Flying Circus', not
the snake.
Programming Paradigms Supported by
Python
1. Imperative: Executes instructions step by step.
2. Object-Oriented: Encapsulation, inheritance, and polymorphism.
3. Functional: Functions as first-class objects.
4. Logical: Rules and facts to infer conclusions.
Philosophy of Python
The Zen of Python highlights its core principles:
1. Beautiful is better than ugly.
2. Simple is better than complex.
3. Readability counts.
4. There should be one -- and preferably only one -- obvious way to
do it.
Applications of Python
• Web Development: Frameworks like Django, Flask.
• Data Science: Libraries like Pandas, NumPy, and Matplotlib.
• Machine Learning: TensorFlow, PyTorch.
• Scripting: Automate repetitive tasks.
Comparison with Other Languages
• Python vs Java: Python is more concise, Java is faster for compiled
applications.
• Python vs C++: Python is easier to learn; C++ provides lower-level
control.
• Python vs R: Python is more versatile; R excels in statistics.
Overview of Python Versions
• Python 1.x: Introduced core features like functions and exception
handling.
• Python 2.x: Added Unicode support and garbage collection (2000).
• Python 3.x: Modern version, with better features and not
backward-compatible (2008).
• Python continues to evolve with active community support.
Key Features of Python 3.x
• Print Function: print('Hello') instead of print 'Hello'.
• Integer Division: 5/2 gives 2.5 instead of 2.
• Unicode by Default: Strings are Unicode by default.
• Asynchronous Programming: Asyncio for non-blocking tasks.
Python
• In Feb 1991, Guido Van Rossun published the code (version 0.9.0)
• Python 1.0 was Versions
released with new features like lambda, nap,
filter, and reduce.
❑ Python 1.5 - December, 1997
❑ Python 1.6 - September, 2000
• Python 2.0 - October, 2000 added new features such as list,
comprehensions, garbage collection systems.
❑ Python 2. 1 - April, 200 I
❑ Python 2.2 - December, 2001
❑ Python 2.3 - July, 2003
❑ Python 2.4 - November, 2004
❑ Python 2.5 - September, 2006
❑ Python 2.6 - October, 2008
❑ Python 2.7 - July, 2010 (Python 2.7 is latest version of python
• Python 3.0 - On December, 2008, Python 3.0 was released.
❑ Python 3.1 - June, 2009
❑ Python 3.2 - February, 2011
❑ Python 3.3 - September, 2012
❑ Python 3.4 - March, 2014
❑ Python 3.5 - September, 2015
❑ Python 3.6 - December, 2016
❑ Python 3.7.0 - June, 2018
▪ Python 3.7.1 - October, 2018
▪ Python 3.7.2 - December, 2015
▪ Python 3.7.3 – March, 2019
▪ Python 3.7.4 - July, 2019
Development Tools for Python
1. PyCharm: Feature-rich IDE for debugging and development.
2. Jupyter Notebook: Interactive coding environment for data
analysis.
3. VS Code: Lightweight editor with Python support.
Careers in Python
► Game Developer
► Web Designer
► Python Developer
► Full Stack Developer
► Machine Learning Engineer
► Data Scientist
► Data Analyst
► And many more
C Vs Python
C Python
Variables are declared Variables are not declared
Does not have concept of OOP Implements OOP
Pointers are available No pointer functionality
Limited built in functions Large library of built in functions
C++ Vs Python
C++ Python
Long line of code Fewer line of code
It is compiled It is interpreted
Data type Mandatory Data Type not Mandatory
Installing Python
IDLE
IDLE is a Python editor with a graphical user interface. IDLE
stands for Integrated Development Environment.
It has two modes:
► Interactive Mode: we can write a single lien python
command and execute them.
► Script Mode: we can write multiple lines of code and
then run it.
Compiling and interpreting
► Many languages require you to compile (translate) your program
into a form that the machine understands.
compile execute
source code byte code output
[Link] [Link]
► Python is instead directly
interpretinterpreted into machine instructions.
source code output
[Link]
Writing Python Programs
1. Python programs are written in .py files.
2. Use any text editor or IDE e.g., PyCharm, Jupyter Notebook).
for writing Python code.
3. Example:
def greet():
print('Hello, Python!')
Executing Python Programs
1. Interactive Mode Programming : Run code directly in the
Python shell.
Example:
>>> print "Hello, Python!";
Hello, Python!
>>> 3+4*5;
23
Executing Python Programs
2. Script Mode Programming: Execute saved Python scripts (.py) via
the command line.
Example:
python [Link]
• Invoking the interpreter with a script parameter begins execution
of the script and continues until the script is finished. When the
script is finished, the interpreter is no longer active.
• For example, put the following in one [Link], and run,
print "Hello, Python!";
print “Welcome to python class!";
The output will be:
Hello, Python!
Welcome to python class!
Sample Python Program
Example Program: Calculating Factorial
def factorial(n):
if n == 0: return 1
return n * factorial(n-1)
print(factorial(5)) # Output: 120
Memory Management in
Python
1. Python uses dynamic memory allocation for variables.
2. Garbage collection automatically handles unused memory.
3. Memory is managed efficiently using stacks and heaps.
4. Module 'gc' provides tools for manual memory management.
Garbage Collection
1. Tracks objects no longer in use.
2. Reference counting is the primary mechanism.
3. Circular references are handled by the garbage collector.
4. Use the 'gc' module to manage garbage collection manually.
Memory Optimization Techniques
1. Use built-in data types and avoid redundant objects.
2. Use generators instead of lists for large datasets.
3. Profile memory usage with tools like memory_profiler.
Stack and Heap Memory
1. Stack Memory: Used for function calls and local variables.
2. Heap Memory: Used for dynamic memory allocation.
3. Python optimizes memory usage via stack and heap segregation.
Common Memory Issues
1. Memory Leaks: Occurs when memory is not released after use.
2. Circular References: Two or more objects reference each other.
3. Solutions: Use weak references and the gc module.