🎓 Python Programming – Day 2
This document contains practical Python coding exercises designed to help you understand
basic concepts such as variables, data types, operators, and control structures. Each section
includes commented examples and explanations.
1️⃣ Program to Add Two Numbers
This program asks the user to input two numbers and then displays their sum.
• num1 = int(input("Enter Number 1 :"))
num2 = int(input("Enter Number 2 :"))
value = num1 + num2
print("The sum is", value)
2️⃣ Convert String to Integer and Multiply
Here we convert a string '100' into an integer and multiply it by 2 using different methods.
• # 1st Method
value = int("100")
print(value * 2)
# 2nd Method
test = "100"
test2 = int(test)
test3 = test2 * 2
print(test3)
# 3rd Method
print(int("100") * 2)
3️⃣ Module 3: Operators and Expressions
Operators are used to perform operations on variables and values. Here are examples of
arithmetic operations:
• a=5
b=3
print(a + b)
print(a - b)
print(a * b)
print(a ** 2)
print(a / b) # Normal Division
print(a // b) # Floor Division
# Division Examples
print(10 / 5)
print(10 // 5)
print(12 / 3)
# Modulus (Remainder)
print(10 % 6)
print(50 % 8)
print(100 % 23)
print(250 % 100)
print(251 % 100)
# Operator Precedence Examples
print(10 - 2 * 3)
print(100 + 50 * 2)
print(20 / 10 - 5 + 3 * 2)
print(2 + 10 - 5 * 10)
print(10 + 2 * 5) # 20
print(50 - 10 / 2) # 45.0
print(8 + 6 - 3 * 2) # 8
print(20 / 5 + 3 * 4) # 16.0
print(2 + 3 ** 2) # 11
print(15 // 4 + 2) # 5
print(18 % 5 + 10) # 13
print(10 + 5 * 2 - 3) # 17
print((10 + 2) * 3) # 36
print(100 - (20 + 5 * 3)) # 65
4️⃣ Module 4: Control Structures
Control structures determine the flow of a program. Here we use conditional statements (if,
elif, else) to check conditions.
• # Check if a number is positive, negative, or zero
number = int(input("Enter Number: "))
if number > 0:
print("Positive Number")
elif number < 0:
print("Negative Number")
else:
print("Zero")
•
# Password Check Example
password = "abc123"
mypassword = input("Enter your password: ")
if password == mypassword:
print("Access Granted")
else:
print("Access Denied")
End of Practical Session
Prepared by: Lahiru Neththasinghe