Essential Python Programming Examples and Exercises
Posted on Mar 16, 2026 in Computers
String Manipulation
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
if str2 in str1:
print("Second string is present in first string")
else:
print("Second string is NOT present in first string")
Removing Substrings
onestring = input("Enter main string: ")
removestring = input("Enter string to remove: ")
if removestring in onestring:
finalstring = onestring.replace(removestring, "")
print("Final string is:", finalstring)
else:
print("String not found")
Number to Word Conversion
num = int(input("Enter number between 0 and 19: "))
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
if 0 <= num <= 19:
print("In words:", words[num])
else:
print("Number out of range")
Student Grading System
m1 = int(input("Enter marks of subject 1: "))
m2 = int(input("Enter marks of subject 2: "))
m3 = int(input("Enter marks of subject 3: "))
total = m1 + m2 + m3
average = total / 3
print("Total =", total)
print("Average =", average)
# Pass / Fail
if m1 <= 39 or m2 <= 39 or m3 <= 39:
print("Result: FAIL")
else:
print("Result: PASS")
# Grade for each subject
marks = [m1, m2, m3]
for m in marks:
if m <= 39: grade = "F"
elif m <= 44: grade = "P"
elif m <= 49: grade = "C"
elif m <= 54: grade = "B"
elif m <= 59: grade = "B+"
elif m <= 69: grade = "A"
elif m <= 79: grade = "A+"
else: grade = "O"
print("Marks:", m, "Grade:", grade)
Variable Swapping
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
x, y = y, x
print("After swapping:")
print("x =", x)
print("y =", y)
Mathematical Calculations
Sine Series Approximation
deg = float(input("Enter angle in degrees: "))
x = deg * 3.14159 / 180
sinx = 0
n_terms = 10
sign = 1
for i in range(n_terms):
fact = 1
for j in range(1, 2*i+2):
fact *= j
term = sign * (x**(2*i + 1)) / fact
sinx += term
sign *= -1
print("sin({}) = {}".format(deg, sinx))
List and Stack Operations
List Filtering
list1 = [1, 2, 3, 4, 5, 6]
list2 = [2, 4, 6, 8]
list3 = [x for x in list1 if x not in list2]
print("Third list (elements in list1 not in list2):", list3)
Stack Implementation
stack = []
while True:
print("\nStack Operations Menu\n1. Push\n2. Pop\n3. Display\n4. Exit")
choice = int(input("Enter choice: "))
if choice == 1:
item = int(input("Enter element to push: "))
stack.append(item)
elif choice == 2:
print(stack.pop() if stack else "Stack is empty")
elif choice == 3:
print("Stack:", stack)
elif choice == 4: break
Dictionary Processing
d1 = {'P101': 5000, 'P102': 3000, 'P103': 4000}
d2 = {'P102': 2000, 'P104': 6000, 'P105': 1000}
for key, value in d2.items():
d1[key] = d1.get(key, 0) + value
print("Merged Dictionary:", d1)
# Sorting and Analysis
sorted_dict = dict(sorted(d1.items(), key=lambda item: item[1], reverse=True))
print("Sorted Dictionary:", sorted_dict)
Text Analysis and Dates
Word Frequency
s = input("Enter a sentence: ")
words = s.replace(".", "").split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
print("Word Frequency:", freq)
Date Difference
from datetime import date
d1 = date(2026, 3, 5)
d2 = date(2026, 3, 10)
print("Number of days between dates:", abs((d2 - d1).days))
Sorting Tuples
foods = [("Pizza", 250), ("Burger", 120), ("Pasta", 200), ("Sandwich", 100)]
foods_sorted = sorted(foods, key=lambda x: x[1], reverse=True)
print("Food items sorted by price:", foods_sorted)
Fibonacci Sequence
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b