Category: python
Python Practice Programs
Published on 24 Jul 2026
Explanation
The best way to master Python is through regular practice. This lesson covers simple programming exercises that reinforce concepts such
Code:
# Program 1: Check Even or Odd
number = int(input('Enter a number: '))
if number % 2 == 0:
print('Even Number')
else:
print('Odd Number')
# Program 2: Find Largest of Three Numbers
num1 = int(input('Enter First Number: '))
num2 = int(input('Enter Second Number: '))
num3 = int(input('Enter Third Number: '))
largest = max(num1, num2, num3)
print('Largest Number:', largest)
Explanation
Coding challenges help improve programming speed, logic, and confidence. This lesson focuses on solving problems using functions, loops, strings, lists,
Code:
# Count vowels in a string
def count_vowels(text):
vowels = 'aeiouAEIOU'
count = 0
for ch in text:
if ch in vowels:
count += 1
return count
sentence = input('Enter a sentence: ')
print('Number of Vowels:', count_vowels(sentence))
Explanation
Professional applications frequently combine file handling with exception handling to ensure data is stored and retrieved safely. This lesson demonstrates
Code:
try:
with open('students.txt', 'w') as file:
file.write('101,John\n')
file.write('102,Alice\n')
with open('students.txt', 'r') as file:
print('Student Records')
for line in file:
print(line.strip())
except Exception as error:
print('Error:', error)
finally:
print('File operation completed.')
Explanation
This mini project combines multiple Python concepts learned throughout the course, including classes, objects, functions, loops, lists, and user input.
Code:
class Student:
def __init__(self, student_id, name, marks):
self.student_id = student_id
self.name = name
self.marks = marks
def display(self):
print(self.student_id, self.name, self.marks)
students = []
for i in range(2):
student_id = input('Student ID: ')
name = input('Student Name: ')
marks = int(input('Marks: '))
students.append(Student(student_id, name, marks))
print('\nStudent Details')
print('----------------')
for student in students:
student.display()
Explanation
The final coding challenge encourages learners to apply all major Python concepts in a single application. The project uses object-oriented
Code:
class Library:
def __init__(self):
self.books = []
def add_book(self, title):
self.books.append(title)
def display_books(self):
print('\nAvailable Books')
print('---------------')
if not self.books:
print('No books available.')
else:
for book in self.books:
print(book)
library = Library()
library.add_book('Python Programming')
library.add_book('Java Fundamentals')
library.add_book('Data Structures')
library.display_books()