Unit I Python Introduction
Unit I Python Introduction
(23DCE2101)
(As per NEP 2023 pattern)
Unit I
Introduction to Python
Programming
Features of Python
• Easy to use and Learn: Python has a simple and easy-to-understand syntax, unlike
traditional languages like C, C++, Java, etc., making it easy for beginners to learn.
• Expressive Language: It allows programmers to express complex concepts in just
a few lines of code or reduces Developer's Time.
• Interpreted Language: Python does not require compilation, allowing rapid
development and testing. It uses Interpreter instead of Compiler.
• Object-Oriented Language: It supports object-oriented programming, making
writing reusable and modular code easy.
• Open Source Language: Python is open source and free to use, distribute and
modify.
• Extensible: Python can be extended with modules written in C, C++, or other
languages.
• Learn Standard Library: Python's standard library contains many modules and
functions that can be used for various tasks, such as string manipulation, web
programming, and more.
Continued…
• IoT: Python is used in IoT for developing scripts and applications for devices like
Raspberry Pi, Arduino, and others.
• Networking: Python is used in networking for developing scripts and applications
for network automation, monitoring, and management.
• DevOps: Python is widely used in DevOps for automation and scripting of
infrastructure management, configuration management, and deployment
processes.
• Finance: Python has libraries like Pandas, Scikit-learn, and Statsmodels for
financial modeling and analysis.
• Audio and Music: Python has libraries like Pyaudio, which is used for audio
processing, synthesis, and analysis, and Music21, which is used for music analysis
and generation.
• Writing scripts: Python is used for writing utility scripts to automate tasks like file
operations, web scraping, and data processing.
Python Popular Frameworks and Libraries
Python has wide range of libraries and frameworks widely used in various
fields such as machine learning, artificial intelligence, web applications, etc.
We define some popular frameworks and libraries of Python as follows.
• Frameworks:
• Web development (Server-side) - Django Flask, Pyramid, CherryPy
• GUIs based applications - Tk, PyGTK, PyQt, PyJs, etc.
• Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
• Kivy: a framework for building multi-touch applications
• Pytest: a testing framework for Python
• Django REST framework: a toolkit for building RESTful APIs
• FastAPI: a modern, fast web framework for building APIs
Python Virtual Machine (PVM)
• Frozen binaries
• Run a python program in a interactive mode
• Run a python program in Script or file mode
• Python interpreter
• Python run time engine
Run Time Engine
• Software that must be running in the computer for applications to execute.
For example, all programming language interpreters are runtime engines.
They convert the program, which is in its original source code or which has
been converted into an interim, intermediate language, into machine
language. In addition, runtime engines provide common routines and
functions that the applications require.
If you want to host and run Python in the cloud, these implementations may be right for you:
• PythonAnywhere (freemium hosted Python installation which lets you run Python in the
browser, e.g. for tutorials, showcases, etc.)
Internal working of Python
• Python is an object-oriented programming language like Java. Python is
called an interpreted language. Python uses code modules that are
interchangeable instead of a single long list of instructions that was
standard for functional programming languages. The standard
implementation of Python is called “cpython”. It is the default and widely
used implementation of Python.
Byte Code
Program
Outputs
Basic Building Blocks of Python
• Identifiers
• Keywords
• Variables
• Indentation
• Comments
Identifiers
• A Python identifier is a name given to a function, class, variables,
module or other objects that is used in Python.
• Rules to form an identifier in Python:
1. Combination of uppercase (A-Z), lowercase letters (a-z), underscores(_), and
digits (0-9) only.
2. Identifier cannot start with a digit
3. Cannot use keywords as identifiers
4. Python is case sensitive language.
• Valid identifiers: sum, product1, Final_Sum
• Invalid identifiers: 2sum, loan%_sanctioned, data-type
Keywords
• Python keywords are reserved words that have specific meaning and
function.
• E.g. lambda, for, if, class, etc.
• Keywords are used to define the syntax and structure of the
programming language.
Variables
• A variables is a container that stores values that we can access or change.
• A variables is a name given to memory location where value can be stored that
can be used in the program.
• The size of the memory reserved by the variable depends on the type of data it is
going to hold.
• A variable, as the name indicates is something whose value is changeable over
time.
• Rules to form a variable name are same that of the identifier.
• E.g. a=10
A=‘c’
sum=11.8
first_name=“Sohan”
Indentation
• Indentation refers to the spaces at the beginning of a code line.
• Python uses indentation to indicate a block of code.
• In addition, indentation helps to increase the readability of the code.
• Generally, four white spaces are used for indentation and is preferred
over tabs.
for i in range (1,11):
print(i)
Comments
• Single line comment (#)
• Multiple line Comments:
‘’’ ……………………………….
……………………………….. ‘’’
or “”” ……………..
……………… “””
Continued…
# Output:
True
• Although the values of num1 and num2 are the same, Python
optimizes small integer objects to share the same memory location,
leading to a True output.
id() function
• Using id() Function with Variables:
python code
x=5
y=x
print(id(x)) # Output: 140732337151648
print(id(y)) # Output: 140732337151648
In this example, both x and y reference the same memory location, as
indicated by their identical id() value
Membership Operators
• These are used to find the existence of a particular element in the sequence and used only with
sequences like string, tuple, list, dictionary, etc.
a = [1, 2, 3]
b = [1, 2, 3]
result1 = a == b # Output: True (values are the same)
result2 = a is b # Output: False (memory locations are different)
• In this example, result1 is True because the values of a and b are the same. However,
result2 is False because the memory locations of a and b are different. This distinction is
vital when comparing objects in Python.
Python Operator Precedence and Associativity
Precedence Operators Description Associativity
Comparisons,
in, not in, is, is
12 membership tests, Left to Right
not, <, <=, >, >=, !=, ==
identity tests
Assignment expression
18 := Right to left
(walrus operator)
Small Python Programs
1. Program to find the square root of a number
2. Program to find the area of Rectangle
3. Program to calculate area and perimeter of the square
4. Program to calculate the surface volume and area of a cylinder
5. Program to swap the value of two variables
End of Unit I – Introduction to Python Programming