PPS Python Program Template Guide With Examples
1. Input + Output Basic
------------------------
Task: Double a number
Code:
num = int(input("Enter a number: "))
result = num * 2
print("Double:", result)
2. If-Else Conditions
----------------------
Task: Check even or odd
Code:
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
3. Elif Ladder
----------------
Task: Grade system
Code:
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Grade C")
4. For Loop
------------
Task: Print 1 to 5
Code:
for i in range(1, 6):
print(i)
5. While Loop
--------------
Task: Print 1 to 5 using while
Code:
i = 1
while i <= 5:
print(i)
i += 1
6. Star Pattern
----------------
Task: Triangle of stars
Code:
for i in range(1, 6):
print("* " * i)
7. List Operations
-------------------
Task: Append to list
Code:
numbers = [1, 2, 3]
numbers.append(4)
print("List:", numbers)
8. Dictionary Example
----------------------
Task: Create and access dictionary
Code:
student = {"name": "Alice", "age": 20}
print("Name:", student["name"])
9. Functions
-------------
Task: Add two numbers
Code:
def add(x, y):
return x + y
print(add(3, 4))
10. File Handling
------------------
Task: Write and read file
Code:
with open("data.txt", "w") as f:
f.write("Hello")
with open("data.txt", "r") as f:
print(f.read())
11. Class and Object
---------------------
Task: Student class
Code:
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Name:", self.name)
s = Student("Alice")
s.display()
12. Prime Number Check
------------------------
Task: Check if number is prime
Code:
num = int(input("Enter number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
13. Factorial
--------------
Task: Calculate factorial
Code:
n = int(input("Enter number: "))
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial:", fact)