Programming Exercises - 002: Loops and Fundamental Concepts

Python Programming Exercises 002

Below are 20 Python programming exercises to help you practice and enhance your skills. Each exercise includes a hint to guide you. Try to solve them on your own before looking at the solutions. File names for saving your work are specified in each exercise.

Exercise 1: Print Odd Numbers

Filename: print_odd_numbers.py

Write a program to print all odd numbers from 1 to 50.

Hint: Use a for loop with a step of 2 starting from 1.

Exercise 2: Sum of Even Numbers

Filename: sum_even_numbers.py

Write a program to calculate the sum of all even numbers from 1 to 100.

Hint: Use a for loop and check if each number is even before adding it to the sum.

Exercise 3: Factorial Calculation

Filename: factorial.py

Write a program to compute the factorial of a given number.

Hint: Use a while loop or recursion.

Exercise 4: Multiplication Table

Filename: multiplication_table.py

Write a program to print the multiplication table of a number entered by the user.

Hint: Use a for loop from 1 to 10.

Exercise 5: Fibonacci Sequence

Filename: fibonacci_sequence.py

Write a program to generate the first 20 numbers of the Fibonacci sequence.

Hint: The next number is the sum of the previous two numbers.

Exercise 6: Reverse a String

Filename: reverse_string.py

Write a program to reverse a string entered by the user.

Hint: Use slicing or a for loop.

Exercise 7: Count Vowels

Filename: count_vowels.py

Write a program to count the number of vowels in a string entered by the user.

Hint: Iterate through the string and check if each character is a vowel.

Exercise 8: Palindrome Check

Filename: palindrome_check.py

Write a program to check if a string entered by the user is a palindrome.

Hint: A palindrome reads the same backward as forward.

Exercise 9: Find the Largest Number

Filename: find_largest.py

Write a program to find the largest number in a list of numbers entered by the user.

Hint: Use a for loop to compare each number.

Exercise 10: Prime Number Check

Filename: prime_check.py

Write a program to check if a number entered by the user is a prime number.

Hint: A prime number is only divisible by 1 and itself.

Exercise 11: Generate a List of Prime Numbers

Filename: list_primes.py

Write a program to generate a list of all prime numbers up to 100.

Hint: Use nested loops to check each number.

Exercise 12: Convert Celsius to Fahrenheit

Filename: celsius_to_fahrenheit.py

Write a program to convert a temperature from Celsius to Fahrenheit.

Hint: Use the formula F = C * 9/5 + 32.

Exercise 13: Calculate Distance Traveled

Filename: distance_traveled.py

Write a program to calculate the distance traveled by a projectile given initial velocity and time.

Hint: Use the formula d = vt.

Exercise 14: Quadratic Equation Roots

Filename: quadratic_roots.py

Write a program to find the roots of a quadratic equation ax2 + bx + c = 0.

Hint: Use the quadratic formula x = (-b ± sqrt(b2 - 4ac)) / 2a.

Exercise 15: Calculate Compound Interest

Filename: compound_interest.py

Write a program to calculate compound interest given principal, rate, and time.

Hint: Use the formula A = P(1 + r/n)(nt).

Exercise 16: Generate a Sine Wave Table

Filename: sine_wave.py

Write a program to generate a table of sine values for angles from 0 to 360 degrees.

Hint: Use the math.sin() function and convert degrees to radians using math.radians().

Exercise 17: Calculate Area of Circle

Filename: area_circle.py

Write a program to calculate the area of a circle given its radius.

Hint: Use the formula A = πr2.

Exercise 18: Calculate Projectile Motion

Filename: projectile_motion.py

Write a program to calculate the maximum height and range of a projectile given initial velocity and angle of launch.

Hint: Use the formulas H = (v2 * sin2θ) / (2g) and R = (v2 * sin(2θ)) / g.

Exercise 19: Calculate Sine and Cosine Values

Filename: sine_cosine.py

Write a program to calculate and print the sine and cosine values for angles from 0 to 360 degrees.

Hint: Use the math.sin() and math.cos() functions.

Exercise 20: Implement a Basic Calculator

Filename: basic_calculator.py

Write a program to implement a basic calculator that can perform addition, subtraction, multiplication, and division.

Hint: Use functions for each operation and a loop to repeatedly ask for user input until they choose to exit.

Key Takeaways

These exercises will help you practice basic and fundamental programming concepts in Python. Start with simpler tasks like counting and summing numbers, and gradually move to more complex problems involving mathematical formulas and scientific concepts. Remember to save your files with the specified names and test your code thoroughly to ensure it works as expected.