Q:

Python | Example of Ternary Operator

0

Given age of a person and we have to check whether person is eligible for voting or not using Ternary operator.

Syntax:

    [on_true] if [expression] else [on_false]

Here,

  • [on_true] is the statement that will be execute if the given condition [expression] is true.
  • [expression] is the conditional expression to be checked.
  • [on_false] is the statement that will be executed if the given condition [expression] is false.

Example:

    Input:
    Enter Age :21

    Output:
    You are Eligible for Vote.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now