Unit I Python Introduction
Unit I Python Introduction
Python
(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…
• GUI Programming Support: Python provides several GUI frameworks, such as Tkinter and
PyQt, allowing developers to create desktop applications easily.
• Integrated: Python can easily integrate with other languages and technologies, such as C/C+
+, Java, and . NET.
• Embeddable: Python code can be embedded into other applications as a scripting language.
• Dynamic Memory Allocation: Python automatically manages memory allocation, making it
easier for developers to write complex programs without worrying about memory
management.
• Wide Range of Libraries and Frameworks: Python has a vast collection of libraries and
frameworks, such as NumPy, Pandas, Django, and Flask, that can be used to solve a wide
range of problems.
• Versatility: Python is a universal language in various domains such as web development,
machine learning, data analysis, scientific computing, and more.
Continued…
• Large Community: Python has a vast and active community of developers contributing to
its development and offering support. This makes it easy for beginners to get help and
learn from experienced developers.
• Career Opportunities: Python is a highly popular language in the job market. Learning
Python can open up several career opportunities in data science, artificial intelligence,
web development, and more.
• High Demand: With the growing demand for automation and digital transformation, the
need for Python developers is rising. Many industries seek skilled Python developers to
help build their digital infrastructure.
• Increased Productivity: Python has a simple syntax and powerful libraries that can help
developers write code faster and more efficiently. This can increase productivity and save
time for developers and organizations.
• Big Data and Machine Learning: Python has become the go-to language for big data and
machine learning. Python has become popular among data scientists and machine
learning engineers with libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and more.
Where is Python used?
• Data Science: Data Science is a vast field, and Python is an important language for this
field because of its simplicity, ease of use, and availability of powerful data analysis and
visualization libraries like NumPy, Pandas, and Matplotlib.
• Desktop Applications: PyQt and Tkinter are useful libraries that can be used in GUI -
Graphical User Interface-based Desktop Applications. There are better languages for this
field, but it can be used with other languages for making Applications.
• Console-based Applications: Python is also commonly used to create command-line or
console-based applications because of its ease of use and support for advanced features
such as input/output redirection and piping.
• Mobile Applications: While Python is not commonly used for creating mobile applications,
it can still be combined with frameworks like Kivy or BeeWare to create cross-platform
mobile applications.
• Software Development: Python is considered one of the best software-making languages.
Python is easily compatible with both from Small Scale to Large Scale software.
Continued…
• Computer Vision or Image Processing Applications: Python can be used for computer
vision and image processing applications through powerful libraries such as OpenCV and
Scikit-image.
• Speech Recognition: Python can be used for speech recognition applications through
libraries such as SpeechRecognition and PyAudio.
• Scientific computing: Libraries like NumPy, SciPy, and Pandas provide advanced numerical
computing capabilities for tasks like data analysis, machine learning, and more.
• Education: Python's easy-to-learn syntax and availability of many resources make it an
ideal language for teaching programming to beginners.
• Testing: Python is used for writing automated tests, providing frameworks like unit tests
and pytest that help write test cases and generate reports.
• Gaming: Python has libraries like Pygame, which provide a platform for developing games
using Python.
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…
1 & This operator performs AND operation between a & b = 1010 & 0100 = 0
(Bitwise AND) operands. Operator copies a bit, to the result, if it >>> a & b
exists in both operands 0
>>> bin (a & b)
‘0b0’
2 | It copies a bit, to the result, if it exists in either (or a | b = 1010 | 0100 = 14
(Bitwise OR) both) operands (1110)
3 ^ It copies a bit, to the result, if it exists in only on a ^ b = 1010 ^ 0100 = 14
(Bitwise XOR) operand but not both (1110)
4 ~ It is unary operator and it has the effect of flipping ~ a = ~ 1010 = 0101
(Bitwise one’s the bit i.e. opposite the bit of the operand
complement)
5 << The left operands value is moved left by the number a << 2 = 1010 << 2
(Bitwise left shift) of bits specified by the right operand = 101000 = 40
6 >> The left operands value is moved right by the a >> 2 = 1010 >> 2
(Bitwise right shift) number of bits specified by the right operand = 0010 = 2
Bitwise operators
A B A&B A|B A^B ~A
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
• Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left (fills 1 in the case of
a negative number) as a result. Similar effect as of dividing the number with some power of two.
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5
• Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids right as a result. Similar
effect as of multiplying the number with some power of two.
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
Identity Operators
• Sometimes, need to compare the memory address of two objects. Identity
operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location
• Used to check whether both operands are same or not. They are used to check if
the two values are located on the same part of the memory. Two variables that
are equal does not imply that they are identical.
is Return True, if variables on either side of the operator >>> a=3
point to the same object and False otherwise >>> b=3
>>> print (a is b)
True
is not Return False, if variables on either side of the operator >>> a=3
point to the same object and True otherwise >>> b=3
>>> print (a is not b)
False
Continued…
num1 = 10
num2 = 10
result = num1 is num2
print(result)
# 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
1 () Parentheses Left to right
Comparisons,
in, not in, is, is
12 membership tests, Left to Right
not, <, <=, >, >=, !=, ==
identity tests
Assignment expression
18 := (walrus operator) Right to left
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