Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

001 Exercises in python fundamentals

The document outlines a series of Python exercises designed to teach fundamental programming concepts, including data types, control structures, functions, lists, dictionaries, file handling, libraries, and classes. Each section contains multiple exercises that require writing Python programs to perform specific tasks, such as temperature conversion, string manipulation, and basic calculator functions. The exercises aim to enhance the user's understanding of Python programming through practical application.

Uploaded by

muherezachris3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

001 Exercises in python fundamentals

The document outlines a series of Python exercises designed to teach fundamental programming concepts, including data types, control structures, functions, lists, dictionaries, file handling, libraries, and classes. Each section contains multiple exercises that require writing Python programs to perform specific tasks, such as temperature conversion, string manipulation, and basic calculator functions. The exercises aim to enhance the user's understanding of Python programming through practical application.

Uploaded by

muherezachris3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXERCISES IN PYTHON FUNDAMENTALS

1. WORKING WITH DATA TYPES


EXERCISE 1: TEMPERATURE CONVERTER
Write a Python program that converts a temperature in Fahrenheit to Celsius. The formula for conversion is:
Celsius = (Fahrenheit - 32) * 5/9.
EXERCISE 2: STRING MANIPULATION
Write a program that takes a user-input string and performs the following tasks:
a) Print the string in uppercase.
b) Print the string in lowercase.
c) Count and print the number of characters in the string (excluding spaces).
EXERCISE 3: LIST OPERATIONS
Create a list of numbers and perform the following tasks:
a) Calculate and print the sum of the numbers in the list.
b) Find and print the maximum and minimum values from the list.
c) Remove any duplicates from the list and print the updated list.
EXERCISE 4: SIMPLE CALCULATOR
Write a basic calculator program that takes two numbers and an operator (+, -, *, /) as input from the user.
Perform the corresponding arithmetic operation and display the result.
EXERCISE 5: DICTIONARY MANIPULATION
Create a dictionary containing names of students as keys and their corresponding ages as values. Perform the
following tasks:
a) Add a new student to the dictionary.
b) Remove a student from the dictionary.
c) Print all the student names.
d) Print the average age of the students.

2. CONTROL STRUCTURES (IF STATEMENTS AND LOOPS)


EXERCISE 1: ODD OR EVEN CHECKER
Write a program that takes an integer input from the user. Determine if the number is odd or even and print an
appropriate message.
EXERCISE 2: FACTORIAL CALCULATOR
Write a program that calculates and prints the factorial of a positive integer input by the user. Use a loop to
perform the calculation.
EXERCISE 3: GUESS THE NUMBER GAME
Create a simple number guessing game where the computer generates a random number between 1 and 100. The
user has to guess the number, and the program gives feedback whether the guess is too high, too low, or correct.
The game should continue until the user guesses correctly.
EXERCISE 4: FIBONACCI SEQUENCE GENERATOR
Write a program that generates and prints the first n terms of the Fibonacci sequence. The value of n is provided
by the user. The Fibonacci sequence starts with 0 and 1, and each subsequent term is the sum of the previous two
terms.
EXERCISE 5: PRIME NUMBER CHECKER
Write a program that takes an integer input from the user and checks whether it's a prime number or not. A prime
number is a positive integer greater than 1 that is only divisible by 1 and itself.

1|E x e r c i s e s i n P y t h o n F u n d a m e n t a l s
3. FUNCTIONS AND MODULAR PROGRAMMING
EXERCISE 1: AREA CALCULATOR
Write a function that calculates and returns the area of a circle based on its radius. Prompt the user to input the
radius and then use the function to calculate and display the area.
EXERCISE 2: GRADE CALCULATOR
Write a function that takes a student's score as input and returns their corresponding letter grade based on the
following scale:
• 90-100: A
• 80-89: B
• 70-79: C
• 60-69: D
• Below 60: F

EXERCISE 3: PASSWORD GENERATOR


Create a function that generates a random password of a specified length. The password should include a mix of
lowercase letters, uppercase letters, digits, and special characters. Allow the user to input the desired password
length.
EXERCISE 4: PALINDROME CHECKER
Write a function that checks whether a given string is a palindrome (reads the same forwards and backwards).
Ignore spaces and punctuation during the check.
EXERCISE 5: SHOPPING CART TOTAL
Create a program that simulates a shopping cart. Define a function for adding items to the cart along with their
prices. Calculate and return the total price of all items in the cart. Allow the user to add items to the cart and then
display the total.

4. LISTS AND DICTIONARIES


EXERCISE 1: LIST MANIPULATION
Write a program that does the following:
a) Creates a list of integers.
b) Adds a new integer to the end of the list.
c) Removes an integer from the list.
d) Prints the sum of all the integers in the list.
EXERCISE 2: DICTIONARY OPERATIONS
Create a dictionary that stores names of countries as keys and their corresponding populations as values. Perform
the following tasks:
a) Add a new country and population to the dictionary.
b) Remove a country from the dictionary.
c) Print all countries in the dictionary.
d) Calculate and print the average population of all countries.
EXERCISE 3: LIST COMPREHENSION
Write a program that generates a list of squares of numbers from 1 to 10 using list comprehension. Then, use a
loop to print each square on a separate line.
EXERCISE 4: DICTIONARY OF LISTS
Create a dictionary that stores the names of different courses as keys and lists of student names as values. Perform
the following tasks:
a) Add new courses and student names to the dictionary.
b) Remove a student from a specific course.
c) Print the list of students in a particular course.

2|E x e r c i s e s i n P y t h o n F u n d a m e n t a l s
EXERCISE 5: SHOPPING LIST
Design a program that simulates a shopping list. Use a dictionary to store items as keys and their corresponding
quantities as values. Implement the following:
a) Add items and quantities to the shopping list.
b) Remove items from the list if they are purchased.
c) Print the final shopping list.

5. FILE HANDLING
EXERCISE 1: FILE READING AND PRINTING
Write a program that reads a text file named "data.txt" and prints its contents to the console.
EXERCISE 2: LINE COUNTER
Create a program that reads a text file (use some kind of placeholder text) and counts the number of lines in it.
Display the total number of lines at the end.
EXERCISE 3: FILE COPY
Write a program that reads the contents of one text file (use some kind of placeholder text) named "source.txt"
and writes them to another text file named "destination.txt".
EXERCISE 4: WORD FREQUENCY COUNTER
Design a program that reads a text file (use any paragraph from any media source/ news article) and counts the
frequency of each word. Store the word frequencies in a dictionary and print the results.
EXERCISE 5: CSV DATA ANALYSIS
Assume you have a CSV file "data.csv" with rows of student names and their corresponding scores. Write a
program that reads the CSV file, calculates the average score, and prints the names of students who scored above
the average.

6. LIBRARIES AND MODULES


EXERCISE 1: MATH OPERATIONS
Write a program that prompts the user to enter two numbers and then calculates and prints the square root of the
sum of their squares. Use the `math` module for the square root calculation.
EXERCISE 2: RANDOM NUMBER GUESSING GAME
Create a game where the computer generates a random number between 1 and 100, and the user has to guess the
number. Provide feedback on whether the guess is too high or too low. Use the `random` module to generate the
random number.
EXERCISE 3: DATE DIFFERENCE CALCULATOR
Write a program that calculates and prints the number of days between two dates entered by the user. Use the
`datetime` module to handle date and time-related operations.
EXERCISE 4: TEMPERATURE CONVERSION WITH UNITS
Develop a program that converts temperatures between Celsius and Fahrenheit. Allow the user to input the
temperature and the desired conversion direction. Use the `math` module for the conversion formula.
EXERCISE 5: RANDOM PASSWORD GENERATOR
Create a function that generates a random password of a specified length. The password should include a mix of
uppercase letters, lowercase letters, digits, and special characters. Use the `random` module for generating
random elements.

3|E x e r c i s e s i n P y t h o n F u n d a m e n t a l s
7. INTRODUCTION TO CLASSES AND OBJECTS
EXERCISE 1: SIMPLE CLASS CREATION
Create a class called `Person` with attributes `name` and `age`. Write a constructor to initialize these attributes,
and a method to print the person's details.
EXERCISE 2: BANK ACCOUNT
Design a class `BankAccount` that represents a basic bank account. Include attributes for the account holder's
name, account number, and balance. Implement methods to deposit and withdraw funds, and display the current
balance.
EXERCISE 3: CIRCLE AREA CALCULATOR
Define a class `Circle` with a constructor that takes the radius as a parameter. Include a method to calculate and
return the area of the circle. Create instances of the class to calculate areas of circles with different radii.
EXERCISE 4: BOOK INFORMATION
Create a class `Book` that represents a book. Include attributes for the book's title, author, and publication year.
Implement methods to display the book's details.

EXERCISE 5: STUDENT RECORD KEEPING


Develop a class `Student` with attributes for the student's name, roll number, and grades in different subjects.
Write methods to calculate the average grade and display the student's information.

4|E x e r c i s e s i n P y t h o n F u n d a m e n t a l s

You might also like