Python for Beginners begunpro

Python Learning Day 14

Python

Welcome to day 14 of Python exploration! Today, we’ll focus on enhancing your understanding of conditionals (if, else, elif) and loops (for, while) through a series of exercises. Let’s dive in:

Exercise 1: Odd or Even?

Write a program that takes a user input (an integer) and prints whether it’s odd or even.

Exercise 2: Multiplication Table

Create a program that generates the multiplication table (up to 10) for a number provided by the user.

Exercise 3: Sum of Squares

Write a program that calculates and prints the sum of squares for a given range of numbers (e.g., 1 to 5: 1^2 + 2^2 + 3^2 + 4^2 + 5^2).

Exercise 4: Guess the Number

Generate a random number between 1 and 10. Ask the user to guess the number. Provide feedback on whether the guess is correct, too high, or too low. Repeat until the user guesses correctly.

Exercise 5: Factorial Calculator

Write a program that calculates the factorial of a number provided by the user.

Exercise 6: Password Checker

Create a simple password checker. Ask the user to enter a password, and check if it meets the following criteria:

  • At least 8 characters long
  • Contains both uppercase and lowercase letters
  • Includes at least one digit

Exercise 7: Fibonacci Series

Generate and print the Fibonacci series up to a specified number of terms. The Fibonacci series starts with 0 and 1, and each subsequent term is the sum of the two preceding ones.

Exercise 8: Pattern Printing

Write a program that prints the following pattern using loops:

				
					*
**
***
****
*****
				
			

Exercise 9: Palindrome Checker

Ask the user to enter a word and check if it’s a palindrome (reads the same backward as forward).

Exercise 10: Prime Numbers

Write a program that prints all prime numbers between 1 and 50.

Challenge: Rock, Paper, Scissors

Implement a simple Rock, Paper, Scissors game where the user can play against the computer.

				
					# Exercise 1: Odd or Even?
user_input = int(input("Enter a number: "))

if user_input % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")
				
			
				
					# Exercise 2: Multiplication Table
number = int(input("Enter a number: "))

for i in range(1, 11):
    result = number * i
    print(f"{number} * {i} = {result}")
				
			
				
					# Exercise 3: Sum of Squares
start_range = int(input("Enter the starting range: "))
end_range = int(input("Enter the ending range: "))

sum_of_squares = sum(i**2 for i in range(start_range, end_range + 1))
print(f"The sum of squares from {start_range} to {end_range} is: {sum_of_squares}")
				
			
				
					# Exercise 4: Guess the Number
import random

random_number = random.randint(1, 10)

while True:
    user_guess = int(input("Guess the number (between 1 and 10): "))
    
    if user_guess == random_number:
        print("Congratulations! You guessed the correct number.")
        break
    elif user_guess < random_number:
        print("Too low. Try again!")
    else:
        print("Too high. Try again!")
				
			
				
					# Exercise 5: Factorial Calculator
number = int(input("Enter a number: "))

factorial = 1
for i in range(1, number + 1):
    factorial *= i

print(f"The factorial of {number} is: {factorial}")
				
			

Conclusion: Building Confidence in Python Basics

Congratulations on completing today’s exercises! By working through these examples, you’ve strengthened your understanding of conditionals and loops in Python. These fundamental concepts are the building blocks for more advanced programming skills.

Remember, practice is key to mastering any programming language. Don’t hesitate to modify the code, try different inputs, and experiment with variations. As you become more comfortable with these basics, you’ll find yourself better equipped to tackle more complex challenges in Python.

Facebook
Twitter
LinkedIn

1 thought on “Python Learning Day 14

Comments are closed.