CTPS - Module - II Updated
CTPS - Module - II Updated
CTPS - Module - II Updated
Module – II
Data, Expressions, & Statements
10/27/2024 1
Module II
‾ Introduction to Python - Python Interpreter, Modes of Python Interpreter,
‾ Values, and data types; Variables;
‾ Keywords; Identifiers; Statements and Expressions;
‾ Input and Output; Comments; Docstring;
‾ Lines and Indentation; Quotation in Python;
‾ Operators and Types, Operator Precedence.
10/27/2024 2
Why Python
10/27/2024 3
Why Python
10/27/2024 4
• Python is an interpreted programming language which means
that you run each line of code immediately to get the results.
10/27/2024 5
10/27/2024 6
10/27/2024 7
Where Python is used
10/27/2024 8
10/27/2024 9
Features of Python programming
10/27/2024 10
Features of Python programming
1. Simple and Easy to Learn: Python is known for its readability and
simplicity. Its syntax emphasizes code readability and allows developers to
express concepts in fewer lines of code compared to languages like C++ or
Java. This simplicity makes Python an excellent choice for beginners.
2. Freeware and Open Source: Python is free to use and distribute. It has an
open-source community that continually contributes to its development,
making it accessible to anyone who wants to use or modify it. Python source
code is also available under the GNU General Public License (GPL).
10/27/2024 15
Features of Python programming
7. Interpreted: Python is an interpreted language, which means that code is
executed line by line by the Python interpreter. This makes it easier to debug and
test code as we can see results immediately.
10/27/2024 17
Software Requirements for the Course
10/27/2024 18
Software Requirements for the Course
▪ Integrated Development Environments (IDEs): Many integrated
development environments like PyCharm, Visual Studio Code, and IDLE
come with built-in Python interpreters. These IDEs provide a more user-
friendly and feature-rich environment for writing, debugging, and
executing Python code.
10/27/2024 24
Compiler vs Interpreter
10/27/2024 25
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
▪ No, Python does not require a separate compiler behind the interpreter
because Python is an interpreted language, not a compiled one in the
traditional sense.
▪ However, Python's process involves a combination of interpretation
and compilation under the hood.
10/27/2024 26
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
In simple terms:
How Python Works:
1) Source Code to Bytecode Compilation: When you write Python code (in
.py files), the Python interpreter first compiles the code into an
intermediate form called bytecode (files ending with .pyc for "compiled
Python"). This compilation step is automatic and invisible to the user,
and it happens every time you run a Python script.
10/27/2024 27
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
In simple terms:
How Python Works:
2) Execution by the Python Virtual Machine (PVM): The generated
bytecode is then executed by the Python Virtual Machine (PVM).
This is where the interpretation part happens — the PVM reads
the bytecode instructions and executes them one by one.
10/27/2024 28
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
Key Points:
▪ You do not need a separate compiler like C or Java to turn Python source
code into machine code. The Python interpreter handles this behind the
scenes.
▪ Python automatically compiles your source code into bytecode (a low-level,
platform-independent representation).
▪ The Python interpreter (PVM) interprets this bytecode to execute the
program.
10/27/2024 29
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
Why It's Different From Compiled Languages:
▪ In traditional compiled languages like C or C++, you write code, then
explicitly use a compiler to convert that code into machine code (binary
format). After compilation, the machine code can be directly executed by
the hardware.
▪ In Python, however, the conversion to bytecode and execution happen
dynamically within the Python runtime, so you never directly interact with
a separate compilation step.
10/27/2024 30
Compiler vs Interpreter
Important Note:
Do you need a compiler at the back of the interpreter for Python?
Summary:
▪ Python uses bytecode compilation followed by interpretation.
▪ There is no need for a separate compiler outside of the interpreter
because the Python interpreter handles both the bytecode
compilation and execution.
10/27/2024 31
How the Python Interpreter Works
1. CPython:
Description: Reference implementation
written in C; most widely used.
Features:
▪ Compiles Python code to bytecode for
the Python Virtual Machine (PVM).
▪ Extensive library support and automatic
memory management.
Use Cases: General-purpose
programming, web applications,
scientific computing.
10/27/2024 34
Python Interpreters
2. Jython:
▪ Description: Runs on the Java platform, allowing integration with Java
libraries.
▪ Features:
▪ Compiles Python code to Java bytecode.
▪ Supports dynamic typing.
10/27/2024 36
Python REPL
4. Operators. 3. Literals
39
Keywords
Examples:
▪ Valid
identifiers: student_name, _score, total_marks_2023.
10/27/2024 42
Python Naming Conventions
10/28/2024 44
Python Naming Conventions
Definition: Variables are used to store data values. Variable types don’t
need to be declared.
▪ Python figures out the variable types on its own.
Example:
age = 30
name = "Alice"
46
Literals / Constant Values
47
String Literals or Constants
48
Input and Output
Input in Python:
10/27/2024 49
Input and Output
Output in Python:
\n New Line
\r Carriage return
\t Horizontal Tab
51
Escape Sequence Character
52
Comments
Types of Comments:
Single-Line Comments: Begin with # and are used for brief notes.
Example: # This is a comment
Multi-Line Comments: Use triple quotes (''' or """) for longer explanations.
Example:
""" This is a multi-line comment. It can span multiple
lines. """
▪ Enhance code clarity and understanding.
▪ Aid in debugging by allowing code to be temporarily disabled.
▪ Provide context for complex logic.
▪ Be concise and avoid obvious comments.
▪ Explain why something is done rather than what is done.
▪ Keep comments updated with code changes.
10/27/2024 53
Docstrings
10/27/2024 55
Built-in Data Types/Common Data Types
10/27/2024 56
Built-in Data Types/Common Data Types
Types of Statements:
11/1/2024 61
Statements and Expressions
10/28/2024 62
Lines and Indentation
Lines and indentation in Python are important because they are used to
structure code and to define blocks of code.
▪ Lines
Lines in Python code can be either blank or non-blank. Blank
lines are ignored by the Python interpreter. Non-blank lines
contain code that is executed by the Python interpreter.
▪ Indentation
Indentation is used to group statements together. Statements
that are indented at the same level are considered to be part of
the same group. Statements that are indented at a lower level
are considered to be part of a nested group.
Lines and Indentation
Example
▪ The following code shows how to use lines and indentation to create a
simple Python program:
if 5 > 2:
print("Five is greater than two!")
▪ This code is indented at one level, which means that the print()
statement is part of the same block of code as the if statement.
10/28/2024 65
Control Flow
10/27/2024 66
Control Flow
10/27/2024 67
Control Flow
Conditional Statements:
if, if-else, and if-elif-else for decision-making.
Example:
if x > 0:
print("Positive")
Looping Statements:
for Loop: Iterates over a sequence.
while Loop: Repeats as long as a condition is true.
Example:
for i in range(5):
print(i)
10/27/2024 68
Control Flow
10/27/2024 69
Operators in Python
Arithmetic Operators:
Perform mathematical operations.
Examples:
▪ + (Addition: 5 + 2 = 7)
▪ - (Subtraction: 4 - 2 = 2)
▪ * (Multiplication: 2 * 3 = 6)
▪ / (Division)
z = 5 / 2 # Answer 2.5,
integer division
▪ // (Floor Division e.g.: 5//2 = 2)
▪ % (Modulus e.g.: 5 % 2 = 1)
▪ ** (Exponentiationeg.: 4 ** 2 = 16) 70
Operator Name Example
= Assignment Operator a = 7
+= Addition Assignment a += 1 # a = a + 1
Assignment
-= Subtraction Assignment a -= 3 # a = a - 3
Operators with
Multiplication
Examples *= a *= 4 # a = a * 4
Assignment
/= Division Assignment a /= 3 # a = a / 3
%= Remainder Assignment a %= 10 # a = a % 10
Greater Than
>= 3 >= 5 give us False
or Equal To
Less Than or
<= 3 <= 5 gives us True
Equal To 73
Python Logical Operators
OPERATOR EXAMPLE MEANING
Bitwise
^ x ^ y = 14 (0000 1110)
XOR
▪ For example, 2 is 10 in binary
and 7 is 111. Bitwise
>> right x >> 2 = 2 (0000 0010)
shift
▪ In the table: Let x = 10 (0000
Bitwise
1010 in binary) and y = << left x << 2 = 40 (0010 1000)
4 (0000 0100 in binary) shift 75
Python Special operators
▪ Python language offers some
special types of operators like Operator Meaning Example
the
i. identity operator True if the
ii. membership operator. operands are x is
is
▪ Identity operators: In Python, identical (refer to True
is and is not are used to the same object)
check if two values are located True if the
on the same part of the operands are not x is
memory. is not identical (do not not
▪ Two variables that are equal, refer to the same True
do not imply that they are object)
identical. 76
Identity operators
▪Example:
▪Here, we see that x1 and y1 are integers of the same values, so they are
equal as well as identical. Same is the case with x2 and y2 (strings).
▪But x3 and y3 are lists. They are equal but not identical. It is
because the interpreter locates them separately in memory although
they are equal. 77
Membership Operators
Example :
79
Other Operator Types and Precedence
10/27/2024 80
Operator Precedence
Determines the order in which operations are evaluated.
1. Highest Precedence:
▪ Parentheses ()
▪ Exponentiation **
2. Middle Precedence:
▪ Multiplication *, Division /, Modulus %
▪ Addition +, Subtraction -
3. Lowest Precedence:
▪ Comparison ==, !=, >, <
▪ Logical and, or, not
4. Associativity:
▪ Left-to-Right: Most operators (e.g., +, -, *, /)
▪ Right-to-Left: Exponentiation **, assignment =.
10/27/2024 81
Assignment 1
▪ Can we assign multiple names at the same time?
>>> x, y = 2, 3
>>> x # Ouput?
>>> y # Ouput?
10/27/2024 82
Assignment 2
Evaluate the following code.
x = 34 - 23 # A comment.
y = “Hello” # Another one.
z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print x
print y
10/27/2024 83