Essential Python Programming Examples for Beginners
Posted on May 1, 2026 in Computers
Unit Conversions
print("Select Conversion:")
print("1. Rupees to Dollar")
print("2. Celsius to Fahrenheit")
print("3. Inches to Feet")
choice = int(input("Enter your choice: "))
if choice == 1:
rupees = float(input("Enter amount in Rupees: "))
dollars = rupees / 83 # approx rate
print("Amount in Dollars =", dollars)
elif choice == 2:
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Temperature in Fahrenheit =", f)
elif choice == 3:
inches = float(input("Enter inches: "))
feet = inches / 12
print("Value in Feet =", feet)
else:
print("Invalid Choice")
Student Record Management
students = {}
while True:
print("\n--- Student Record Menu ---")
print("1. Add Student")
print("2. Update Student")
print("3. Display Students")
print("4. Delete Student")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
attendance = int(input("Enter attendance: "))
students[name] = {"Marks": marks, "Attendance": attendance}
print("Student added successfully!")
elif choice == 2:
name = input("Enter student name to update: ")
if name in students:
marks = int(input("Enter new marks: "))
attendance = int(input("Enter new attendance: "))
students[name]["Marks"] = marks
students[name]["Attendance"] = attendance
print("Record updated!")
else:
print("Student not found!")
elif choice == 3:
print("\nStudent Records:")
for name, data in students.items():
print(name, "-> Marks:", data["Marks"], ", Attendance:", data["Attendance"])
elif choice == 4:
name = input("Enter student name to delete: ")
if name in students:
del students[name]
print("Student deleted!")
else:
print("Student not found!")
elif choice == 5:
print("Exiting program...")
break
else:
print("Invalid choice!")
Loops and Character Analysis
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
for j in range(i):
print("*", end=" ")
print()
ch = input("Enter a character: ")
if ch.islower():
print("Lowercase Character")
elif ch.isupper():
print("Uppercase Character")
elif ch.isdigit():
print("Digit")
else:
print("Special Character")
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
File Processing
file = open("sample.txt", "r")
data = file.read()
file.close()
words = data.split()
length = int(input("Enter word length to find: "))
print("Words with length", length, ":")
for word in words:
word = word.strip(",.!?;:")
if len(word) == length:
print(word)
Error Handling
try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
result = a / b
print("Result =", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed!")
except ValueError:
print("Error: Please enter valid numbers!")
finally:
print("Program executed successfully.")
Object-Oriented Programming
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print("Name:", self.name)
print("Marks:", self.marks)
# Creating object
s1 = Student("Rahul", 85)
s2 = Student("Priya", 92)
# Calling method
s1.display()
print()
s2.display()
Regex Validation
import re
phone = input("Enter phone number: ")
email = input("Enter email: ")
phone_pattern = "^[6-9][0-9]{9}$"
email_pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if re.match(phone_pattern, phone):
print("Valid phone number")
else:
print("Invalid phone number")
if re.match(email_pattern, email):
print("Valid email")
else:
print("Invalid email")
password = input("Enter password: ")
if (len(password) >= 8 and
re.search("[A-Z]", password) and
re.search("[a-z]", password) and
re.search("[0-9]", password) and
re.search("[@#$%^&*]", password)):
print("Strong password")
else:
print("Weak password")
Data Analysis with NumPy
import numpy as np
data = np.array(list(map(float, input("Enter numbers: ").split())))
print("Mean:", np.mean(data))
print("Median:", np.median(data))
print("Standard Deviation:", np.std(data))
print("Variance:", np.var(data))
data2 = np.array(list(map(float, input("Enter second array: ").split())))
print("Correlation Coefficient:", np.corrcoef(data, data2)[0][1])
Geometry Calculations
import math
print("Select Shape:")
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
choice = int(input("Enter your choice: "))
if choice == 1:
r = float(input("Enter radius: "))
area = math.pi * r * r
print("Area of Circle =", area)
elif choice == 2:
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
area = l * b
print("Area of Rectangle =", area)
elif choice == 3:
b = float(input("Enter base: "))
h = float(input("Enter height: "))
area = 0.5 * b * h
print("Area of Triangle =", area)
else:
print("Invalid Choice")