Worked Examples On Python By Akachukwu
Here are 30 Python challenge exercises covering the topics we have done so far:
Operators and Expressions
- Write a program to calculate the area of a triangle given its base and height using multiplication and division operators.
- Write a program that swaps the values of two variables without using a temporary variable.
- Evaluate the expression
((10 + 3) * 2 - 4 / 2)
and explain the operator precedence used.
Data Types, String Concatenation, and Replication
- Create a program that asks for the user’s first and last name and prints them together using string concatenation.
- Write a program that replicates a string
n
times, where n
is a user-provided integer. - Convert a floating-point number to an integer and a string. Print the results.
Input and len()
Function
- Write a program that takes a user’s full name as input and prints the number of characters in their name.
- Create a program that asks for the user’s age and calculates the year they were born.
- Use the
len()
function to check the length of a password and display whether it is “Weak”, “Moderate”, or “Strong”.
Comparison Operators
- Write a program to check if a given number is greater than, less than, or equal to 50.
- Take two inputs from the user and compare if the first input is lexicographically smaller than the second.
- Demonstrate the difference between
==
and =
by writing a small program with both.
Boolean Operators
- Write a program to check if a number is divisible by both 3 and 5.
- Take three user inputs and check if all of them are non-negative numbers using boolean operators.
- Combine comparison and boolean operators to check if a number is between 10 and 20 (inclusive).
Flow Control and Conditional Statements
- Write a program to display all even numbers between 1 and 50 using a
for
loop. - Create a
while
loop that counts down from 10 to 0 and prints “Liftoff!” at the end. - Write a program that prints all numbers from 1 to 20 but skips multiples of 3 using the
continue
statement. - Use a
for
loop and break
to stop iterating through a list of numbers when you find a negative number. - Write a program to calculate the factorial of a user-provided number using a
while
loop.
Combining Concepts
- Ask the user for their age, and based on the input, print whether they are a “Child”, “Teenager”, “Adult”, or “Senior”.
- Write a program that takes a string input and prints “Palindrome” if the string reads the same backward.
- Use a
for
loop to generate a multiplication table for a number input by the user. - Create a program that calculates the sum of all integers between two numbers provided by the user.
- Write a program to find the largest and smallest numbers in a list using a
for
loop.
Bonus Challenges
- Create a program that asks for a user’s full name and reverses the order of their name (e.g., “John Doe” becomes “Doe John”).
- Write a guessing game where the user has to guess a random number between 1 and 100. Use
while
loops to keep asking until they guess correctly. - Create a program to check if a user-input string contains both uppercase and lowercase letters.
- Write a program to count the vowels in a user-provided string.
- Write a program that takes a list of numbers and prints only the prime numbers from the list.
Recent Posts
In the ever-evolving landscape of modern agriculture, artificial intelligence (AI) is emerging as a game-changing…
The Internet of Things (IoT) is reshaping the way we live, work, and produce goods.…
Introduction Have you ever wondered how some manufacturing industries consistently deliver high-quality products while minimizing…
In the ever-evolving landscape of modern agriculture, the integration of Internet of Things (IoT) technology…
Introduction Have you ever imagined diagnosing equipment issues without even being on-site? Welcome to the…
In the ever-evolving world of manufacturing, staying competitive means adopting innovative solutions to optimize every…
This website uses cookies.