Python Module 1 Notes
Python Module 1 Notes
Module-1
Expressions and Statements: Values and data types, Variables, Variable names
and keywords, Statements, Evaluating expressions, Operators and operands,
Type converter functions, Order of operations, Operations on strings, Input,
Composition, The modulus operator.
Iteration: Assignment, Updating variables, the for loop, the while statement, The
Collatz 3n + 1 sequence, tables, two-dimensional tables, break statement,
continue statement, paired data, Nested Loops for Nested Data.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 1
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Features:
What is a Program?
What is Debugging?
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 2
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Types of Errors:
Experimental Debugging
Variables: Definition
Example:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 3
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Examples of Variables
a) Integer Variable
a = 100
print(a)
Output:
100
b) Float Variable
pi = 3.1415
print(pi)
Output:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 4
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
3.1415
c) String Variable
message = "Welcome to PESITM Shivamogga"
print(message)
Output:
Output:
True
e) Multiple Assignments
x, y, z = 10, 20, 30
print(x, y, z)
Output:
10 20 30
f) Same Value to Multiple Variables
a = b = c = 50
print(a, b, c)
Output:
50 50 50
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 5
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Data types: Python data types help define the nature of the data stored in variables.
They are automatically determined at runtime and categorized as: Numeric, Boolean,
String, Sequence, Set, Mapping, Binary, and None
Main Categories of Data Types
Numeric Types
Used to store numbers.
Boolean Type
Used for logical values.
String Type
Used to store a sequence of characters.
Sequence Types
Used to store collections of ordered data.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 6
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Example:
a = 10
b = 3.14
c = 2 + 3j
d = True
e = "Python"
f = [1, 2, 3]
g = (4, 5, 6)
h = {"name": "Rahul", "age": 20}
i = {1, 2, 3}
j = None
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 7
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Arithmetic Operators
# Arithmetic Operators Example
a = 10
b = 3
print("a + b =", a + b) # Addition
print("a - b =", a - b) # Subtraction
print("a * b =", a * b) # Multiplication
print("a / b =", a / b) # Division
print("a // b =", a // b) # Floor Division
print("a % b =", a % b) # Modulus
print("a ** b =", a ** b) # Exponentiation
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 8
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Sample Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333333333333335
a // b = 3
a % b = 1
a ** b = 1000
Comparison Operators
print("a == b:", a == b)
print("a != b:", a != b)
print("a > b:", a > b)
print("a < b:", a < b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)
Sample Output:
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False
Logical Operators
# Logical Operators Example
x = True
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 9
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
y = False
Sample Output:
x and y: False
x or y: True
not x: False
Assignment Operators
# Assignment Operators Example
c = 5
print("c =", c)
c += 3
print("c += 3 ->", c)
c -= 2
print("c -= 2 ->", c)
c *= 4
print("c *= 4 ->", c)
c /= 3
print("c /= 3 ->", c)
c %= 2
print("c %= 2 ->", c)
c **= 3
print("c **= 3 ->", c)
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 10
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
c //= 2
print("c //= 2 ->", c)
Sample Output:
c = 5
c += 3 -> 8
c -= 2 -> 6
c *= 4 -> 24
c /= 3 -> 8.0
c %= 2 -> 0.0
c **= 3 -> 0.0
c //= 2 -> 0.0
Bitwise Operators
# Bitwise Operators Example
p = 5 # 0101 in binary
q = 3 # 0011 in binary
Sample Output:
p & q = 1
p | q = 7
p ^ q = 6
~p = -6
p << 1 = 10
p >> 1 = 2
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 11
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Membership Operators
# Membership Operators Example
list1 = [1, 2, 3, 4, 5]
Sample Output:
3 in list1: True
6 not in list1: True
Identity Operators
# Identity Operators Example
a1 = 10
b1 = 10
Sample Output:
a1 is b1: True
a1 is not b1: False
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 12
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Operator Precedence:
Question 1
result = -2 ** 3 ** 2
Answer: -512
Explanation: Exponentiation first (3 ** 2 = 9), then 2 ** 9 = 512, unary minus
applied → -512
Question 2
x + y * z ** 2 // y - z
Answer: 14
Explanation: z ** 2 = 16, y * 16 = 48, 48 // y = 16, x + 16 = 18, 18 - z = 14
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 13
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Question 3
a & b | c ^ a
Answer: 7
Explanation: a & b = 1, c ^ a = 7, 1 | 7 = 7
Question 4
x or y and not x or y
Answer: True
Explanation: not x = False, y and False = False, x or False = True, True or y =
True
Question 5
a << b >> c + 1
Answer: 64
Explanation: c + 1 = 3, a << b = 512, 512 >> 3 = 64
Question 6
not x < y and x + y > 12
Answer: False
Explanation: x < y = True, not True = False, x + y > 12 = True, False and True =
False
Question 7
x ** y % z * 2 + 1
Answer: 3
Explanation: x ** y = 81, 81 % z = 1, 1 * 2 = 2, 2 + 1 = 3
Question 8
a & ~b | b ^ a
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 14
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Answer: 5
Explanation: ~b = -3, a & -3 = 5, b ^ a = 5, 5 | 5 = 5
Question 9
x + y << z ** 2
Answer: 327680
Explanation: z ** 2 = 16, x + y = 5, 5 << 16 = 327680
Question 10
x and y or not z and x or y
Answer: 2
Explanation: not z = False, x and y = 2, False and x = False, 2 or False = 2, 2 or y
=2
Question 11
x in lst and lst[0] is not x or lst[-1] == x
Answer: True
Explanation: x in lst = True, lst[0] is not x = True, True and True = True, lst[-1]
== x = True, True or True
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 15
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Definition
The input() function in Python is used to take input from the user during
program execution. It allows the user to enter data through the keyboard.
Syntax
variable = input('Message for the user: ')
The message inside quotes is displayed as a prompt. The function always returns the
input as a string.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 16
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
to float
str() Converts input str(input()) String
to string
1. Write a Python program to input the marks of three subjects and print the
average.
sub1 = float(input("Enter marks of Subject 1: "))
sub2 = float(input("Enter marks of Subject 2: "))
sub3 = float(input("Enter marks of Subject 3: "))
2. Write a program to read name, age, and branch of a student and display them
neatly.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 17
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Output Statement:
Definition
The print() function in Python is used to display information on the screen. It
can display text, numbers, variables, or expressions.
Syntax
print(value)
Examples
Example 1: Printing text
print("Welcome to Python")
print(10)
name = "Ananya"
age = 19
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 18
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
print(name)
print(age)
name = "Ananya"
age = 19
print("Name:", name, "Age:", age)
a = 10
b = 20
print("Sum =", a + b)
Type conversion (also called type casting) means converting a value from one data
type to another.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 19
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Example:
x = 5 # int
y = 3.2 # float
z = x + y # int + float → float
print(z) # Output: 8.2
print(type(z)) # Output: <class 'float'>
Explanation:
# Integer conversion
a = int(4.9)
print(a) # 4
# Float conversion
b = float("3.14")
print(b) # 3.14
# String conversion
c = str(100)
print(c) # '100'
# Boolean conversion
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 20
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
The for loop is a control flow statement that allows you to iterate over a sequence
(like a list, tuple, string, or range) and execute a block of code for each item.
It is also called a definite loop because the number of iterations is usually known in
advance.
Syntax:
Output:
1
2
3
4
5
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 21
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
1. Take the first element from the sequence and assign it to the loop variable.
2. Execute the loop body using this variable.
3. Move to the next element in the sequence.
4. Repeat steps 2–3 for all elements in the sequence.
5. When all elements are processed, the loop terminates and the program continues
with the next instructions.
The while loop is a control flow statement that allows code to be executed
repeatedly as long as a specified condition is True.
Syntax:
while condition:
# code block
Example: Print numbers 1 to 5
i = 1
while i <= 5:
print(i)
i += 1
Execution Flow of a while Loop
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 22
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
i = 1
while i <= 50:
print(i, end=" ")
i += 1
Output:
1 2 3 ... 50
N = int(input("Enter N: "))
sum = 0
i = 1
while i <= N:
sum += i
i += 1
print("Sum of first", N, "numbers:", sum)
Example Input/Output:
Enter N: 5
Sum of first 5 numbers: 15
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 23
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Example:
Enter a number: 5
Factorial of 5 is 120
Input/Output:
Example:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 24
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
N = int(input("Enter N: "))
for i in range(1, N+1):
print(i, "squared is", i**2)
Input/Output:
Enter N: 5
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Input/Output:
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 25
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Definition:
The break statement in Python is used to terminate the loop immediately, regardless
of the loop’s condition.
When the program encounters break, it exits the nearest enclosing loop (either for
or while).
Syntax:
or
while condition:
if condition2:
break
# statements
Output:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 26
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
1
2
3
4
5
Loop ended.
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
print("Exited from loop.")
Output:
1
2
3
4
Exited from loop.
The continue statement is used to skip the current iteration of the loop and
move directly to the next iteration.
It does not terminate the loop like break; it only skips certain parts of the loop
body based on a condition.
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 27
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Syntax:
or
while condition:
if condition2:
continue
# rest of code
Explanation:
Output:
1
2
4
5
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 28
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Explanation:
i = 0
while i < 5:
i += 1
if i == 2:
continue
print(i)
Output:
1
3
4
5
Nested Loops:
# 2D list (table)
table = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 29
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Output:
1 2 3
4 5 6
7 8 9
Output:
1 2 3
2 4 6
3 6 9
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 30
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
def function_name(parameters):
# statements
return value
def greet():
print("Hello, welcome to Python!")
Output:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 31
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Output:
Sum = 15
Write a function to calculate the addition of two no.
i)without parameters.
ii) with parameters.
iii) with return statements.
The function does not take any input, and the numbers are defined inside
the function.
def add_numbers():
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum:", sum)
Example Input/Output:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 32
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Output:
Sum: 12
iii) Function With Return Statement
The function returns the sum, which can be stored in a variable or printed.
Output:
Sum: 12
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 33
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Local Variables:
A local variable is defined inside a function and can be used only within that
function.
It is created when the function starts and destroyed when the function ends.
Local variables cannot be accessed outside their function.
Example:
def test():
x = 10 # Local variable
print("Inside function:", x)
test()
print("Outside function:")
# print(x) # Error: x is not defined outside the
function
Explanation:
Global Variables:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 34
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Example:
y = 20 # Global variable
def show():
x=10 # Local variable
print("Inside function:", x,y)
show()
print("Outside function:", x)
Output:
Inside function: 10 20
Outside function: 10
To change the value of a global variable inside a function, use the global keyword.
Example:
x = 5
def change():
global x
x = x + 10
print("Inside function:", x)
change()
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 35
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
print("Outside function:", x)
Output:
Inside function: 15
Outside function: 15
def test():
x = 50 # Local variable
print("Inside function:", x)
test()
print("Outside function:", x)
Output:
Inside function: 50
Outside function: 100
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 36
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Syntax:
Program:
Output:
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 37
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Program:
# Lambda function to subtract two numbers
sub = lambda a, b: a - b
Program:
# Lambda function to find the square of a number
square = lambda x: x * x
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 38
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
2.a) Define variables in Python. Explain the rules for naming variables with
examples. (5 Marks)
b) Write a Python program to demonstrate multiple assignment and type
checking using type() function. (5 Marks)
4.a) What are operators in Python? Explain various types of operators with
suitable examples. (5 Marks)
b) Write a Python program to demonstrate arithmetic, comparison, logical,
and bitwise operators. (5 Marks)
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 39
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
7.a) Explain the use of input() and print() functions in Python with
syntax and examples. (5 Marks)
b) Write a Python program to read the marks of three subjects, calculate
the average, and display the result. (5 Marks)
8.a) Explain the working of the for loop in Python. Give syntax and an
example. (5 Marks)
b) Write a Python program to print the multiplication table of a given
number using a for loop. (5 Marks)
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 40
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
9.a) Explain the working of the while loop in Python. Compare it with the
for loop. (5 Marks)
b) Write a Python program to display the Collatz 3n + 1 sequence for a given
number. (5 Marks)
10.a) Explain the use of break and continue statements in loops with
examples. (5 Marks)
b) Write a Python program using a for loop that prints numbers from 1 to
10, but skips 5 and stops when the number is 8. (5 Marks)
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 41
Prerana Educational and Social Trust®
PES Institute of Technology and
Management
NH-206,Sagar Road,Shivamogga-577204
Department Computer Science and Engineering (Data Science)
Dr. Sunitha Pramod , Prof and HOD CSE(DS), PESTIM, Shivamogga Page 42