Conditional Logic and Booleans - Python Programming Exercise

This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are even, or "False" if either one of them is odd. The program prompts the user to input two integer numbers and then checks each number’s parity (whether it is even or odd). By leveraging the conditional operator (also known as the ternary operator), the program evaluates the condition and assigns the appropriate boolean value to bothEven. This approach is a concise and effective way to check multiple conditions in a single line of code. This exercise is useful for understanding how to use the conditional operator in Python, which allows for clean and efficient conditional assignments. It also helps reinforce the concept of parity checks in programming, where the program checks whether numbers are even or odd using the modulo operator. By using this method, the program makes decisions based on the user’s input and provides immediate feedback, demonstrating an essential programming technique for handling conditions.

 Category

Python Data Types

 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

 Copy 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.

  •  Exception Handling V2

    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...

  •  Character

    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...

  •  Triangular Shape

    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...

  •  String Password

    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...

  •  Arithmetic - Conditional

    This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...

  •  Double Value

    This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...