Programming Exercises - 001: Input-Output, Variables, Expressions
Next Topic(s):
Created:
14th of August 2024
02:46:13 PM
Modified:
24th of September 2024
03:52:31 PM
Programming Exercises - 001: Input-Output, Variables, Expressions
Below are 20 exercise programs designed to help you practice basic Python concepts, such as variables, data types, operators, expressions, and basic input/output. Each exercise includes a question, hints, and the name of the file you should use to complete the task. These exercises do not use loops or functions, making them perfect for beginners.
1. Simple Greeting
Question: Write a Python script named simple_greeting.py
that asks the user for their name and then prints a greeting message.
Hint: Use the input()
function to get the user's name and the print()
function to display the message.
2. Addition of Two Numbers
Question: Create a Python script named add_two_numbers.py
that asks the user to enter two numbers and then prints their sum.
Hint: Use the input()
function to read the numbers as strings and int()
to convert them to integers before adding.
3. Calculate the Square of a Number
Question: Write a Python script named calculate_square.py
that reads a number from the user and prints its square.
Hint: To find the square, multiply the number by itself.
4. Check for Even or Odd Number
Question: Create a Python script named even_or_odd.py
that checks whether a given number is even or odd.
Hint: Use the modulus operator %
to determine if the number is divisible by 2.
5. Convert Celsius to Fahrenheit
Question: Write a Python script named celsius_to_fahrenheit.py
that converts a temperature given in Celsius to Fahrenheit.
Hint: Use the formula F = (C * 9/5) + 32
to convert Celsius to Fahrenheit.
6. Calculate the Area of a Rectangle
Question: Create a Python script named area_rectangle.py
that calculates the area of a rectangle given its length and width.
Hint: The area of a rectangle is calculated using the formula Area = length * width
.
7. Calculate Simple Interest
Question: Write a Python script named simple_interest.py
that calculates simple interest using the formula SI = (P * R * T) / 100
, where P
is the principal amount, R
is the rate of interest, and T
is the time period.
Hint: Read the values of P
, R
, and T
from the user.
8. Find the Largest of Three Numbers
Question: Create a Python script named largest_of_three.py
that reads three numbers from the user and prints the largest one.
Hint: Use the if-elif-else
statements to compare the three numbers.
9. Check if a Number is Positive, Negative, or Zero
Question: Write a Python script named check_number.py
that reads a number from the user and checks whether it is positive, negative, or zero.
Hint: Use if
, elif
, and else
to handle the different cases.
10. Calculate the Perimeter of a Rectangle
Question: Create a Python script named perimeter_rectangle.py
that calculates the perimeter of a rectangle given its length and width.
Hint: The perimeter of a rectangle is calculated using the formula Perimeter = 2 * (length + width)
.
11. Convert Kilometers to Miles
Question: Write a Python script named km_to_miles.py
that converts a distance given in kilometers to miles.
Hint: Use the conversion factor 1 kilometer = 0.621371 miles
.
12. Swap Two Variables
Question: Create a Python script named swap_variables.py
that swaps the values of two variables.
Hint: Use a temporary variable to hold one of the values during the swap.
13. Calculate the Area of a Circle
Question: Write a Python script named area_circle.py
that calculates the area of a circle given its radius.
Hint: Use the formula Area = 3.14159 * radius * radius
for the calculation.
14. Calculate the Average of Three Numbers
Question: Create a Python script named average_three_numbers.py
that reads three numbers from the user and prints their average.
Hint: Calculate the average using the formula Average = (num1 + num2 + num3) / 3
.
15. Check if a Year is a Leap Year
Question: Write a Python script named leap_year.py
that checks if a given year is a leap year.
Hint: A year is a leap year if it is divisible by 4, but not by 100 unless it is also divisible by 400.
16. Convert Hours and Minutes to Minutes
Question: Create a Python script named time_to_minutes.py
that converts a given time in hours and minutes to total minutes.
Hint: Multiply the hours by 60 and add the minutes to get the total minutes.
17. Reverse a String
Question: Write a Python script named reverse_string.py
that reads a string from the user and prints it in reverse.
Hint: You can reverse a string using slicing like this: reversed_string = string[::-1]
.
18. Calculate the Volume of a Cube
Question: Create a Python script named volume_cube.py
that calculates the volume of a cube given its side length.
Hint: Use the formula Volume = side ** 3
for the calculation.
19. Check if a Number is Divisible by Another
Question: Write a Python script named divisibility_check.py
that checks if one number is divisible by another.
Hint: Use the modulus operator %
to check divisibility.
20. Convert Days to Hours, Minutes, and Seconds
Question: Create a Python script named days_to_hms.py
that converts a given number of days into hours, minutes, and seconds.
Hint: Use the conversion factors: 1 day = 24 hours
, 1 hour = 60 minutes
, and 1 minute = 60 seconds
.