Python: Example of Ternary Operator

Python Ternary Operator Example: Here, we are actualizing a program that will peruse the age of an individual and check whether an individual is qualified for casting a ballot or not utilizing ternary operator.

Given the age of an individual and we need to check whether an individual is qualified for casting a ballot or not utilizing Ternary operator.

Syntax:

    [on_true] if [expression] else [on_false]

Here,

  • [on_true] is the explanation that will be executed if the given condition [expression] is valid.
  • [expression] is the restrictive articulation to be checked.
  • [on_false] is the explanation that will be executed if the given condition [expression] is false.

Example:

    Input:
    Enter Age :21

    Output:
    You are Eligible for Vote.

Program:

# input age 
age = int(input("Enter Age :"))

# condition
status = "Eligible" if age>=18 else "Not Eligible"

# print message
print("You are",status,"for Vote.")

Output:

Enter Age :21
You are Eligible for Vote.

Leave a Comment

error: Alert: Content is protected!!