+3 votes
in Programming Languages by (8.6k points)
I can use if else to do my task, but I want to know if there is a ternary conditional operator in Python?

1 Answer

+1 vote
by (16.9k points)

Yes, python does have a ternary operator. It allows to quickly test a condition instead of a multiline if statement. You can specify ternary operator like this: condition_is_true if condition else condition_is_false

Have a look at the following example.

>>> for idx in range(1,119,10):
...     e=idx+9 if idx+9<=119 else 119
...     print(e)
...
10
20
30
40
50
60
70
80
90
100
110
119

...