Learn Python with practical coding examples
Last update: 06-26-2024
Welcome to this blog post dedicated to helping you reinforce your understanding of Python basics through hands-on programming exercises. Whether you are a beginner looking to solidify your grasp on fundamental concepts or a teacher looking for inspiration for their next class, these exercises are designed to ensure you don't miss any crucial elements required to fully master Python and programming.
Exercises
1. Variables
Create variables to store your name, age, and whether you are a student (True/False).
name = "John Doe"
age = 25
is_student = True
2. String, Int, Float, Boolean
Define a string, an integer, a float, and a boolean variable, then print them.
my_string = "Hello, world!"
my_int = 100
my_float = 3.14
my_boolean = False
print(my_string, my_int, my_float, my_boolean)
3. Displaying Variables as Strings
Print the above variables as strings.
print(str(my_string))
print(str(my_int))
print(str(my_float))
print(str(my_boolean))
4. Converting Strings to Numbers
Convert a string "123" to an integer and "456.78" to a float and print them.
int_number = int("123")
float_number = float("456.78")
print(int_number, float_number)
5. Conditional statements
Write a script to check if a number is positive, negative, or zero.
number = 10 # Try different numbers here
if number > 0:
print("Positive")
elif number == 0:
print("Zero")
else:
print("Negative")
6. Using elif in Conditions
Expand the previous exercise by adding more ranges and corresponding messages using elif
statements (below -10, -9 to -1, 0, 1, to 9, above 10).
7. Else Statements
Modify the range-checking program to include an else
statement that catches any unexpected inputs.
8. The Math Module
Exercise: Use the math module to find the square root of 16 and the sine of 45.
import math
sqrt_val = math.sqrt(16)
sine_val = math.sin(45)
print("Square root:", sqrt_val)
print("Sine:", sine_val)
9. Modulo
Exercise: Write a program that defines a number and checks if it's divisable by 3.
number = 10
remainder = number % 3
print("Divisable by 3:", remainder == 0)
10. Composite Conditions (1)
Write a program that asks the user for a number and prints whether the number is positive, negative, or zero, and whether it is even or odd.
11. Composite Conditions (2)
Write a program that prompts the user to input the current temperature. If the temperature is above 30 degrees, print "Hot Day". If it's 20 to 30 degrees, check if it's sunny or cloudy by asking the user and then print "Warm and Sunny" or "Warm and Cloudy" accordingly. If the temperature is below 20 degrees, print "Cold Day".
12. While loops (1)
Write a Python program that uses a while
loop to print the numbers from 1 to 10.
13. While loops (2)
Create a script that continuously asks the user to enter a number and prints the sum of all entered numbers. The loop should stop when the user enters the number 0.
14. While loops (3)
Write a program that uses a while
loop to reverse the digits of a given integer. For example, if the user enters 1234, the program should print 4321.
15. Lists (1)
Write a Python program that creates an list of five numbers and prints all of them
16. Lists (2)
Write a program that defines a list containing the names of five different fruits. Print the first and last fruit in the array.
17. For loops (1)
Write a Python program that uses a for
loop to iterate over a list of five numbers and prints each number.
18. For loops (2)
Write a Python program that uses a for
loop to iterate over a string and count the number of vowels (a, e, i, o, u) in the string. Print the total count of vowels.
19. For loops (3)
Write a Python program that initializes a list and finds the largest and smallest values in it.
20. Range (1)
Write a program that uses 'range
' to print the integer numbers from 0 to 9
21. Range (1)
Write a program that uses 'range
' to:
- Print the even integer numbers from 10 to 30
- Print the integer numbers from 10 to 2 (decreasing order)
22. Break, Continue (1)
Write a Python program that iterates through numbers 1 to 10 using a for
loop. Use the break
statement to exit the loop when the number 5 is reached, and print the numbers before the break occurs.
23. Functions (1)
Write a Python function called greet
that takes a name as a parameter and prints "Hello, [name]!" Call this function with three different names.
24. Functions (2)
Write a Python function called is_even
that takes a single integer as a parameter and returns True
if the number is even and False
if the number is odd. Use this function to check if the numbers 4, 7, and 10 are even or odd, and print the results.
25. Functions (3)
Create a function named convert_to_celsius
that takes a temperature in Fahrenheit as a parameter and returns the temperature in Celsius. Use a for
loop to go over a list of temperatures in Farenheit and call the function for each of them.
To convert from Farenheit to Celsius, subtract 32 and divide the result by 1.8.