EXERCICES ON Python PROGRAMMING LANGUAGE
Exercises on Python Programming
1. Basics of Python
Write a program that prints "Hello, Python!" to the console.
Declare variables of different types (e.g., int, float, str, bool) and demonstrate
operations on them (e.g., addition for numbers, concatenation for strings).
Write a program that asks the user to input two numbers and performs addition,
subtraction, multiplication, and division on them. Display the results.
2. Conditional Statements
Write a program to check whether a number entered by the user is positive, negative,
or zero using an if-elif-else statement.
Create a program that assigns a grade based on the user’s score using nested if
statements. (e.g., A for scores above 90, B for 80-89, etc.)
Write a program to determine whether a given year is a leap year.
3. Loops
Write a program to print the multiplication table of a number entered by the user, up to
12 rows.
Create a program to calculate the factorial of a number using a for loop.
Write a program to sum all numbers in a list using a while loop.
4. Functions
Write a function is_prime(n) that checks whether a number is prime. Test it with
several inputs.
Create a function reverse_string(s) that takes a string as input and returns it
reversed.
Write a recursive function to calculate the Fibonacci sequence up to a given number.
5. Lists and Tuples
Write a program to find the largest and smallest numbers in a list of integers.
Create a program that demonstrates slicing operations on a list. (e.g., reverse the list,
extract even-indexed elements)
Write a program to merge two lists and remove duplicates.
6. Dictionaries and Sets
Write a program to count the frequency of characters in a given string using a
dictionary.
Create a program that uses a set to find the common elements between two lists.
Write a program to create a phonebook using a dictionary, allowing users to add,
delete, and search for entries.
7. File Handling
Write a program to create a text file, write a series of numbers to it, then read the
numbers and display them.
Create a program that counts the number of lines, words, and characters in a text file.
Write a program to copy the contents of one file to another.
8. Object-Oriented Programming
Define a class called Rectangle with attributes length and width. Add methods to
calculate and print the area and perimeter.
Create a class BankAccount with methods for deposit, withdrawal, and checking
balance. Use it to simulate basic banking operations.
Write a program that demonstrates inheritance by creating a Base class and a Derived
class with overridden methods.
9. Exception Handling
Write a program that handles a ZeroDivisionError gracefully using a try-except
block.
Create a program to handle multiple exceptions (e.g., invalid input, file not found).
Write a custom exception class for invalid age input and use it in a program that
checks for a valid age.
10. Advanced Topics
Use list comprehensions to generate a list of squares of numbers from 1 to 10.
Write a program that uses lambda functions to filter out even numbers from a list.
Create a program that fetches JSON data from a public API using the requests
library and parses the data.
Additional Challenges
1. Write a program that simulates a basic "To-Do List" application where users can add,
mark as complete, and delete tasks.
2. Create a text-based Tic-Tac-Toe game where two players can play against each other.
3. Build a small inventory management system using classes and dictionaries to track
items, their quantities, and prices.
4. Write a program that implements a simple web scraper using BeautifulSoup to
extract data from a website.
Bonus Challenge
Build a Python program that creates a graphical user interface (GUI) for a basic
calculator using the tkinter library.