Python Ternary Operator

Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the code more concise.

In this tutorial, let us look at what is the ternary operator in Python and how we can use it in our code with some examples.

Syntax of Ternary Operator

The ternary operator is available from Python version 2.5 onwards, and the syntax is:

[value_if_true] if [expression] else [value_if_false] 

The simpler way is to represent ternary operator Syntax is as follows:

<expression 1> if <condition> else <expression 2> 

Note: The conditionals are an expression, not a statement. It means that you can’t use assignment statements or pass other statements within a conditional expression.

Introduction to Python Ternary Operators

Let us take a simple example to check if the student’s result is pass or fail based on the marks obtained. 

Using a traditional approach

We are familiar with “if-else” conditions lets first write a program that prompts to enter student marks and returns either pass or fail based on the condition specified.

marks = input('Enter the marks: ')

if int(marks) >= 35:
    print("The result is Pass")
    print("The result is Fail")


Enter the marks: 55
The result is Pass

Now instead of using a typical if-else condition, let us try using ternary operators. 

Example of Ternary Operator in Python

The ternary operator evaluates the condition first. If the result is true, it returns the value_if_true. Otherwise, it returns value_if_false

The ternary operator is equivalent to the if-else condition. 

If you are from a programming background like C#, Java, etc., the ternary syntax looks like below.

if condition:
condition ? value_if_true : value_if_false

However, In Python, the syntax of the ternary operator is slightly different. The following example demonstrates how we can use the ternary operator in Python.

marks = input('Enter the marks: ')
print("The result is Pass" if int(marks)>=35 else "The result is Fail")


Enter the marks: 34
The result is Fail
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python List Pop()

Python List pop()

Table of Contents Hide Syntax of List pop() pop() ParametersReturn Value from List pop()Example 1: Pop item at the given index from the listExample 2: pop() without an index, and for…
View Post
Python Write Text File

Python Write Text File

Table of Contents Hide Steps on How to write to a File in Python Python open() function Methods for Writing to a text file in PythonPython close() functionExamples for Writing to…
View Post
Python Jsonpath

Python JSONPath

Table of Contents Hide JSONPath Library in PythonInstalling jsonpath-ng ModuleJsonpath operators:Parsing a Simple JSON Data using JSONPathParsing a Json Array using JSONPath Expression JSONPath is an expression language that is…
View Post