0% found this document useful (0 votes)
31 views13 pages

Python Conditional and Loop Exercises

The document contains a series of Python programming exercises focused on using conditional statements (if, else, elif), loops (for, while), and nested conditions. Each exercise includes a question followed by a sample answer demonstrating the implementation of the specified concept. The exercises are designed for practice and understanding of basic programming logic in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

Python Conditional and Loop Exercises

The document contains a series of Python programming exercises focused on using conditional statements (if, else, elif), loops (for, while), and nested conditions. Each exercise includes a question followed by a sample answer demonstrating the implementation of the specified concept. The exercises are designed for practice and understanding of basic programming logic in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Use of IF statement:

Question 1:

Write a Python program that takes a user's age as input. If the age is greater than or equal to 18,
print "You are an adult."

Answer 1

age = int(input("Enter your age: "))

if age >= 18:

print("You are an adult.")

Question 2:

Write a program that checks if a given number is positive. If it's positive, print "Positive number."

Answer 2:

num = int(input("Enter a number: "))

if num > 0:

print("Positive number.")

Question 3:

Write a Python program that determines whether a user's entered character is a vowel.

Answer 3:

char = input("Enter a character: ").lower()

if char in 'aeiou':

print("Vowel")

Question 4:

Write a program to check if a user's entered number is divisible by 7.

Answer 4:
python

Copy code

num = int(input("Enter a number: "))

if num % 7 == 0:

print("Divisible by 7.")

Question 5:

Write a Python program to check if a user's entered username is valid. A valid username should be at
least 6 characters long.

Answer 5:

python

Copy code

username = input("Enter your username: ")

if len(username) >= 6:

print("Valid username.")

2. Use of IF, ELSE:

Question 6:

Write a program that checks whether a given number is even or odd. Print "Even" if it's even, and
"Odd" if it's odd.

Answer 6:

python

Copy code

num = int(input("Enter a number: "))

if num % 2 == 0:

print("Even")
else:

print("Odd")

Question 7:

Write a Python program that checks if a user's entered number is positive or negative. If positive,
print "Positive," otherwise print "Negative."

Answer 7:

python

Copy code

num = int(input("Enter a number: "))

if num >= 0:

print("Positive")

else:

print("Negative")

Question 8:

Write a program to determine whether a user is eligible to vote based on their age. If eligible, print
"You can vote," otherwise, print "You cannot vote."

Answer 8:

python

Copy code

age = int(input("Enter your age: "))

if age >= 18:

print("You can vote.")

else:

print("You cannot vote.")

Question 9:

Write a Python program that checks if a user's entered year is a leap year. If it's a leap year, print
"Leap year," otherwise, print "Not a leap year."
Answer 9:

python

Copy code

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print("Leap year")

else:

print("Not a leap year")

3. If elif Else:

Question 10:

Write a program that checks if a user's entered number is greater than, less than, or equal to 100.

Answer 10:

python

Copy code

num = int(input("Enter a number: "))

if num > 100:

print("Greater than 100")

elif num < 100:

print("Less than 100")

else:

print("Equal to 100")

Question 11:

Write a Python program that classifies a given exam score into grades: A (90-100), B (80-89), C (70-
79), D (60-69), F (0-59).

Answer 11:
python

Copy code

score = int(input("Enter your exam score: "))

if score >= 90:

print("Grade: A")

elif score >= 80:

print("Grade: B")

elif score >= 70:

print("Grade: C")

elif score >= 60:

print("Grade: D")

else:

print("Grade: F")

Question 12:

Write a Python program that checks if a given temperature is hot, warm, or cold. Use the following
criteria: Hot (above 30°C), Warm (20-30°C), Cold (below 20°C).

Answer 12:

python

Copy code

temperature = float(input("Enter the temperature in Celsius: "))

if temperature > 30:

print("Hot")

elif 20 <= temperature <= 30:

print("Warm")

else:

print("Cold")

Question 13:

Write a program that takes a user's entered number and determines whether it is positive, negative,
or zero.
Answer 13:

python

Copy code

num = float(input("Enter a number: "))

if num > 0:

print("Positive number.")

elif num < 0:

print("Negative number.")

else:

print("Zero.")

Question 14:

Write a Python program that checks if a user's entered character is a vowel or a consonant.

Answer 14:

python

Copy code

char = input("Enter a character: ").lower()

if [Link]():

if char in 'aeiou':

print("Vowel")

else:

print("Consonant")

else:

print("Not a valid character.")

Question 15:

Write a program that takes three numbers as input and prints the largest one.

Answer 15:
python

Copy code

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

if num1 >= num2 and num1 >= num3:

print("Largest number:", num1)

elif num2 >= num1 and num2 >= num3:

print("Largest number:", num2)

else:

print("Largest number:", num3)

4. If nested in IF:

Question 16:

Write a program that checks if a number is positive, negative, or zero. If positive, check if it's even or
odd.

Answer 16:

python

Copy code

num = int(input("Enter a number: "))

if num > 0:

print("Positive number.")

if num % 2 == 0:

print("Even")

else:

print("Odd")

elif num == 0:

print("Zero")

else:
print("Negative number.")

Question 17:

Write a program that checks whether a given number is a multiple of 3. If it is, check whether it's
also a multiple of 5.

Answer 17:

python

Copy code

num = int(input("Enter a number: "))

if num % 3 == 0:

print("Multiple of 3.")

if num % 5 == 0:

print("Also a multiple of 5.")

else:

print("Not a multiple of 5.")

else:

print("Not a multiple of 3.")

Question 18:

Write a Python program that checks if a user's entered year is a leap year. If it's a leap year, check if
it's a century year.

Answer 18:

python

Copy code

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print("Leap year.")

if year % 100 == 0:

print("Century year.")

else:
print("Not a leap year.")

Question 19:

Write a program that checks if a user's entered number is positive, negative, or zero. If positive,
check if it's a multiple of 7.

Answer 19:

python

Copy code

num = int(input("Enter a number: "))

if num > 0:

print("Positive number.")

if num % 7 == 0:

print("Multiple of 7.")

else:

print("Not a multiple of 7.")

elif num == 0:

print("Zero")

else:

print("Negative number.")

Question 20:

Write a Python program that checks if a user's entered character is an uppercase letter, lowercase
letter, or a digit.

Answer 20:

python

Copy code

char = input("Enter a character: ")

if [Link]():

if [Link]():

print("Uppercase letter.")
else:

print("Lowercase letter.")

elif [Link]():

print("Digit.")

else:

print("Not a letter or digit.")

Feel free to use these exercises for practice and further understanding of the specified Python
concepts! If you have any more questions or if there's a specific area you'd like more exercises on,
let me know!

For Loop Problems:


[Link] Numbers:

Use a for loop to print all the numbers from 1 to 10.

for i in range(1, 11):

print(i)

[Link] Numbers:

Print all even numbers between 1 and 20 using a for loop.

for i in range(2, 21, 2):

print(i)

[Link]:

Calculate the factorial of a given number using a for loop. The factorial of a number is the product of
all positive integers up to that number.

num = 5

factorial = 1

for i in range(1, num + 1):

factorial *= i

print(f"The factorial of {num} is {factorial}")

[Link] a Triangle:
Use nested for loops to print a right-angled triangle pattern.

for i in range(1, 6):

for j in range(i):

print("*", end="")

print()

5. Sum of Squares:

Calculate the sum of the squares of numbers from 1 to 5 using a for loop.

sum_of_squares = 0

for i in range(1, 6):

sum_of_squares += i**2

print(f"The sum of squares is {sum_of_squares}")

While Loop Problems:


[Link]:

Use a while loop to implement a countdown from 5 to 1.

count = 5

while count > 0:

print(count)

count -= 1

[Link] of Digits:

Write a program that calculates the sum of the digits of a given number using a while loop.

num = 123

sum_of_digits = 0

while num > 0:

digit = num % 10

sum_of_digits += digit

num //= 10

print(f"The sum of digits is {sum_of_digits}")

[Link] the Number:


Implement a simple number guessing game using a while loop. Generate a random number and let
the user guess it. Provide feedback on whether the guess is too high, too low, or correct.

import random

secret_number = [Link](1, 10)

guess = 0

while guess != secret_number:

guess = int(input("Guess the number: "))

if guess < secret_number:

print("Too low. Try again.")

elif guess > secret_number:

print("Too high. Try again.")

else:

print("Congratulations! You guessed the correct number.")

[Link] of 3:

Print the multiplication table of 3 using a while loop.

i=1

while i <= 10:

result = 3 * i

print(f"3 x {i} = {result}")

i += 1

5. Reverse a Number:

Write a program to reverse a given number using a while loop.

num = 12345

reversed_num = 0

while num > 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

print(f"The reversed number is {reversed_num}")


 While True loop in Python that keeps taking the user’s input printing the
cumulative sum:

You might also like