Python Tutorial- Ternary Operator

Python Exercise Lists

Assuming there are two numbers now, and we want to get the larger one, we can use an if else statement, for example:

if a>b:
    max = a;
    max = b;

But Python provides a more concise way of writing, as follows:

max = a if a > b else b

This is similar to the ternary operator. In other programming languages, Python is a minimalist programming language. This new ternary operator uses the existing if else keyword to achieve the same functionality. The format of the ternary operator (conditional operator) using if else is as follows:

exp1 if condition else exp2

The condition is a judgment condition, and exp1 and exp2 are two expressions. If condition is true (the result is true), then exp1 is executed and the result of exp1 is used as the result of the entire expression; if condition is not (the result is false), then exp2 is executed and the result of exp2 is used as the result of the entire expression.

The previous statement max = a if a> b else b means:

  • If a> b is true, “a” is used as the value of the entire expression and assign it to the variable max;
  • If a> b is false, “b” is used as the value of the entire expression and assigned to the variable max.

Python Nesting of ternary operators

Python ternary operators support nesting, which can make complex expressions. Pay attention to the pairing of “if and else” when nesting, for example:

a if a>b else c if c>d else d

Can be explained as:

a if a>b else ( c if c>d else d )

For example:

The program is a nested ternary operator. The program first evaluates a> b. If the expression is True, the program returns the first expression and print ("a bigger than b"), otherwise it will continue to execute the content after the else, that is:

(print ("a less than b") if a < b else print ("a equal to b"))

After entering the expression, first determine whether a < b is true. If the result of a < b is True, print ("a less than b") will be executed, otherwise print ("a equal to b") will be executed.


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises