Ternary Operator, Positive & Lesser - Python Programming Exercise

This Python program prompts the user to input two numbers and then uses the conditional operator (?) to perform a series of checks on the input. The program first checks if the first number is positive, then checks if the second number is positive. It also verifies if both numbers are positive at the same time. Finally, the program determines which number is smaller, comparing them using conditional logic. The conditional operator is used to simplify these checks and return appropriate results, all in a concise manner. This exercise is an excellent opportunity to practice using the conditional operator for multiple conditions in a single expression. It also provides a practical example of how to compare two numbers and output results based on conditions like positivity and relative size. Using the conditional operator allows for compact and efficient decision-making, making the program easier to read and understand. This program showcases how to handle multiple conditions and comparisons with Python in an intuitive way.

 Category

Mastering Flow Control

 Exercise

Ternary Operator, Positive & Lesser

 Objective

Develop a Python program that prompts the user for two numbers and uses the conditional operator (?) to determine the following:

If the first number is positive
If the second number is positive
If both are positive
Which one is smaller

 Example Python Exercise

 Copy Python Code
# Ask the user for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Check if the first number is positive
result_num1 = "The first number is positive." if num1 > 0 else "The first number is not positive."

# Check if the second number is positive
result_num2 = "The second number is positive." if num2 > 0 else "The second number is not positive."

# Check if both numbers are positive
result_both = "Both numbers are positive." if num1 > 0 and num2 > 0 else "Both numbers are not positive."

# Determine which number is smaller
smaller = num1 if num1 < num2 else num2

# Print the results
print(result_num1)
print(result_num2)
print(result_both)
print(f"The smaller number is: {smaller}")

 Output

Case 1:
Enter the first number: 5
Enter the second number: -3
The first number is positive.
The second number is not positive.
Both numbers are not positive.
The smaller number is: -3.0

Case 2:
Enter the first number: -2
Enter the second number: 8
The first number is not positive.
The second number is positive.
Both numbers are not positive.
The smaller number is: -2.0

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Prime Digit

    This Python program prompts the user to input an integer and determines whether it is a prime number or not. A prime number is defined as a number great...

  •  Dispense Change

    This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price o...

  •  Error Handling

    This Python program prompts the user for two numbers and performs the division operation. It uses a try..except block to catch potential errors such as ...

  •  Positive and Negative Numbers

    In this exercise, you will develop a Python program that prompts the user to input a number and then determines if the number is positive or n...

  •  Multiply Unless Zero

    In this exercise, you will develop a Python program that prompts the user for a number. If the number is not zero, the program will ask for a second ...

  •  Division When Non-Zero

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...