
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can we use Python Ternary Operator Without else?
If you want to convert a statement like −
if <condition>: <some-code>
to a single line, You can use the single line if syntax to do so −
if <condition>: <some-code>
Another way to do this is leveraging the short-circuiting and operator like −
<condition> and <some-code>
If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and <some-code> will be evaluated.
- Related Articles
- Ternary operator ?: vs if…else in C/C++
- How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?
- If elseif else or ternary operator to compare numbers PHP?
- How to overload python ternary operator?
- Ternary Operator in Python?
- How can we use MySQL UNION operator on datasets?
- How can we use MySQL SELECT without FROM clause?
- How can we speed up Python "in" operator?
- Does Python have a ternary conditional operator?
- Changing ternary operator into non-ternary - JavaScript?
- How we can import Python modules without installing?
- How Ternary operator in PowerShell Works?
- How do we use equivalence (“equality”) operator in Python classes?
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- Ternary Operator in Java

Advertisements