Python Programming 1
Python Programming 1
PROGRAMMING
UNIT 1
By Ms. K. S. Jeswani
INTRODUCTION
• Python is a general-purpose, dynamically
typed, high-level, compiled and interpreted,
garbage-collected, and purely object-oriented
programming language that supports
procedural, object-oriented, and functional
programming.
• Python is a widely used high-level,
interpreted programming language.
• It was designed with an emphasis on code
readability, and its syntax allows
programmers to express their concepts in
fewer lines of code.
• Python is a programming language that lets
you work quickly and integrate systems more
HISTORY
• Python laid its foundation in the late 1980s.
• The implementation of Python was started in
December 1989 by Guido Van Rossum at CWI in
Netherland.
• In February 1991, Guido Van Rossum published the code
(labeled version 0.9.0) to alt.sources.
• In 1994, Python 1.0 was released with new features like
lambda, map, filter, and reduce.
• Python 2.0 added new features such as list
comprehensions, garbage collection systems.
• On December 3, 2008, Python 3.0 (also called "Py3K") was
released. It was designed to rectify the fundamental flow
of the language.
• ABC programming language is said to be the predecessor
of Python language, which was capable of Exception
Handling and interfacing with the Amoeba Operating
System.
• The following programming languages influence Python -
• ABC language
Easy to
Use &
Read
Dynami
Open
cally
Source
Typed
Rich Featur
Standar High
d es of Level
Library Python
Cross
Compile
Platfor
d&
m
Interpre
Compat Garbag ted
ibility e
Collecte
d
FEATURES
• Easy to use and Read - Python's syntax is clear
and easy to read, making it an ideal language for
both beginners and experienced programmers. This
simplicity can lead to faster development and
reduce the chances of errors.
• Dynamically Typed - The data types of variables
are determined during run-time. We do not need to
specify the data type of a variable during writing
codes.
• High-level - High-level language means human
readable code.
• Compiled and Interpreted - Python code first gets
compiled into bytecode, and then interpreted line by
line. When we download the Python in our system
form org we download the default implement of
Python known as CPython. CPython is considered to
be Complied and Interpreted both.
FEATURES
• Garbage Collected - Memory allocation and de-
allocation are automatically managed.
Programmers do not specifically need to manage
the memory.
• Cross-platform Compatibility - Python can be
easily installed on Windows, macOS, and various
Linux distributions, allowing developers to create
software that runs across different operating
systems.
• Rich Standard Library - Python comes with
several standard libraries that provide ready-to-use
modules and functions for various tasks, ranging
from web development and data manipulation to
machine learning and networking.
• Open Source - Python is an open-source, cost-
free programming language. It is utilized in several
APPLICATION
• Data Science AREAs of Python
• Desktop • Computer Vision
Applications or Image
• Console-based Processing
Applications Applications
• Mobile • Audio and Music
Applications • Writing scripts
• Software • Speech
Development Recognition
• Artificial • Scientific
Intelligence computing
• Web Applications • Education
• Enterprise • Testing
Applications • Gaming
• 3D CAD • IoT
Applications • Networking
• APPLICATIONS
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.
• Artificial Intelligence: AI is an emerging Technology, and Python is a perfect
language for artificial intelligence and machine learning because of the
availability of powerful libraries such as TensorFlow, Keras, and PyTorch.
• Web Applications: Python is commonly used in web development on the
backend with frameworks like Django and Flask and on the front end with
tools like JavaScript HTML and CSS.
• Enterprise Applications: Python can be used to develop large-scale
enterprise applications with features such as distributed computing,
networking, and parallel processing.
APPLICATIONS
• Machine Learning: Python is widely used for machine learning due to its simplicity, ease
of use, and availability of powerful machine learning libraries.
• 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.
• 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
STRUCTURE OF PYTHON
PROGRAM
• As a dynamically typed, interpreted language, Python
has a simpler basic program structure than compiled
languages like C and C++.
• Python scripts execute top-to-bottom, so order is very
important.
• The use of proper whitespace for indentation also
factors prominently since it defines scope and code
blocks in Python.
• There is no need to explicitly declare variable types
either.
• Python Program Consists of –
The several components that a Python program
holds are -
• Expressions - An expression represents something,
like a number, a string, or an element. Any value is an
expression.
• Statements - Anything that does something is a
statement. Any assignment to a variable or function
call is a statement. Any value contained in that
statement is an expression.
• Comments - Comments are the additional information
provided against a statement or a chunk of code for
the better clarity of the code. Interpreter ignores the
comments and does not count them in commands.
• Functions - Function is a set of instructions defined
under a particular name, which once written can be
called whenever and wherever required.
• Block(s) - A block refers to a group of statements
which are part of another statement or function. All
statements inside a block are indented at the same
level
PYTHON BASICS
• Indentation
• Variables
• Identifiers
• Keywords
• Comments
(spaces or tabs) at the beginning of code line.
• It is used to define the code blocks.
• Indentation is crucial in Python because, unlike
many other programming languages that use
braces "{}" to define blocks, Python uses
indentation.
• It improves the readability of Python code, but on
other hand it became difficult to rectify indentation
errors.
• Even one extra or less space can lead to
indentation error.
• Python Variables -
• A variable is like a container that stores values you
can access or change.
• The purpose of using variables is to allow the
stored values to be used later on.
• Variables in Python are essentially named
references pointing to objects in memory. (Unlike
some other languages, you don't need to declare a
variable's type explicitly in Python)
• Python Identifiers -
• In Python, identifiers are unique names that
are assigned to variables, functions, classes,
and other entities.
• They are used to uniquely identify the entity
within the program.
• They should start with a letter (a-z, A-Z) or
an underscore "_" and can be followed by
letters, numbers, or underscores.
• Python keywords -
• Keywords in Python are reserved words that
have special meanings and serve specific
purposes in the language syntax.
• They cannot be used as identifiers (names
for variables, functions, classes, etc.).
• Below is the list of Keywords -
• Comments in Python -
• Comments in Python are statements written
within the code.
• They are meant to explain, clarify, or give
context about specific parts of the code.
• Python Single Line Comment -
• Single line comments are preceded by the "#"
symbol. Everything after this symbol on the
same line is considered a comment.
• Python Multi-line Comment
• Python doesn't have a specific syntax for multi-
line comments.
• However, programmers often use multiple
single-line comments, one after the other, or
sometimes triple quotes (either ''' or """), even
though they're technically string literals.
NUMERIC DATA TYPES
• Represents the data that has a numeric value.
• A numeric value can be an integer, a floating
number, or a complex number.
• These values are defined as Python int , Python
float , and Python complex classes in Python .
• Integers – Value is represented by int class. It
contains positive or negative whole numbers (without
fractions or decimals). There is no limit to how long an
integer value can be.
• Float – Value is represented by the float class. It is a
real number with a floating-point representation. It is
specified by a decimal point. (Optionally, the character
e or E followed by a positive or negative integer may
be appended to specify scientific notation.)
• Complex Numbers – A complex number is
represented by a complex class. It is specified as (real
part) + (imaginary part)j . For example – 2+3j
• Integer Value -
• a=5
• print("The type of a", type(a))
• Floating Point Value -
• b = 40.5
• print("The type of b", type(b))
• Complex Value -
• c = 1+3j
• print("The type of c", type(c))
• print(" c is a complex number",
isinstance(1+3j,complex))
• Output:
• The type of a <class 'int'>
• The type of b <class 'float'>
• The type of c <class 'complex'>
• c is complex number: True
BOOLEAN DATA TYPES
• Python Data type with one of the two
built-in values, True or False.
• Boolean objects that are equal to True
are truthy (true), and those equal to
False are falsy (false).
• Non-Boolean objects can be evaluated
in a Boolean context as well and
determined to be true or false.
• It is denoted by the class bool.
• False can be represented by the 0 or the
letter "F," while true can be represented
EXAMPLE -
# Python program to check the boolean
type
• print(type(True))
• print(type(False))
• print(false)
Output –
• <class 'bool'>
• <class 'bool'>
• NameError: name 'false' is not defined
SEQUENCE DATA TYPES
• The sequence Data Type in Python is the
ordered collection of similar or
different Python data types.
• Sequences allow storing of multiple
values in an organized and efficient
fashion.
• There are several sequence data types of
Python -
• Python String
• Python List
• Python Tuple
OPERATORS
• Operators are used to perform operations on
variables and values.
• OPERATORS: These are the special symbols. Eg- + ,
* , /, etc.
• OPERAND: It is the value on which the operator is
applied.
• Python divides the operators in the following groups -
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Arithmetic Operators Assignment Operators
Operat Name Exampl Operato Example Same As
or e r
+ Addition x+y = x=5 x=5
+= x += 3 x=x+3
- Subtraction x-y
-= x -= 3 x=x-3
* Multiplicatio x*y
*= x *= 3 x=x*3
n
/= x /= 3 x=x/3
/ Division x/y
%= x %= 3 x=x%3
% Modulus x%y //= x //= 3 x = x // 3
** Exponentiati x ** y **= x **= 3 x = x ** 3
on &= x &= 3 x=x&3
// Floor x // y |= x |= 3 x=x|3
division ^= x ^= 3 x=x^3
>>= x >>= 3 x = x >>
3
<<= x <<= 3 x = x <<
3
:= print(x := 3) x=3
print(x)
Comparison Operators Identity Operators
Operato Name Example Operat
Description Example
r or
== Equal x == y Returns True if
is both variables are x is y
!= Not equal x != y the same object
> Greater x>y Returns True if
than both variables are
is not x is not y
< Less than x<y not the same
object
>= Greater x >= y
than or
equal to
<= Logical
Less than Operators
x <= y
or equal to
Operato
Description Example
r
Returns True if both
and x < 5 and x < 10
statements are true
Returns True if one of the
or x < 5 or x < 4
statements is true
Reverse the result, returns not(x < 5 and x <
not
Membership Operators Bitwise Operators
Operat Operat Examp
Description Example Name Description
or or le
Sets each bit to 1
Returns True & AND x&y
if both bits are 1
if a sequence
Sets each bit to 1
with the | OR if one of two bits x|y
in specified x in y is 1
value is
Sets each bit to 1
present in the ^ XOR if only one of two x^y
object bits is 1
Returns True ~ NOT Inverts all the bits ~x
if a sequence Shift left by
with the pushing zeros in
x not in Zero fill
not in specified <<
left shift
from the right and x << 2
y let the leftmost
value is not
Ternary
present Operators
in the bits fall off
object Shift right by
pushing copies of
Signed
the leftmost bit in
>> right x >> 2
from the left, and
shift
let the rightmost
bits fall off
OPERATORS PRECEDENCE