Exercise
Conditional Logic And Booleans
Objective
Develop a Python program that uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if two numbers entered by the user are even, or "False" if any of them is odd.
Example Python Exercise
Show Python Code
# Prompt the user for two numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Use the conditional operator to check if both numbers are even
bothEven = True if num1 % 2 == 0 and num2 % 2 == 0 else False
# Display the result
print(f"Both numbers are even: {bothEven}")
Output
Enter the first number: 4
Enter the second number: 6
Both numbers are even: True
Enter the first number: 4
Enter the second number: 7
Both numbers are even: False
Enter the first number: 3
Enter the second number: 5
Both numbers are even: False
Share this Python Exercise
More Python Programming Exercises of Python Data Types
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...
This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...
This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part...
This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are en...
This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...
This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...