Is there an equivalent of C’s (?:) ternary operator in python?

0 votes
There is ternary operator in C (?:). Is there an equivalent operator in Python?
Jul 25 in Python by Nisha
31 views

1 answer to this question.

0 votes

Yes, there is. The syntax is as follows:

[true] if [expression] else [false]

x, y = 2, 6
small = x if x < y else y

Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators:

[expression] and [true] or [false]
answered Jul 25 by Alia

Related Questions In Python

0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14 in Python by Junaid
38 views
0 votes
1 answer

Is there a list of timezones in python?

import pytz list(pytz.common_timezones) READ MORE

answered Aug 2 in Python by Mohammad
• 3,060 points
18 views
0 votes
1 answer

What is the use of '==' operator in dictionary in Python?

Yes you can use '==' operator to ...READ MORE

answered Jul 24 in Python by Arvind
• 2,960 points
19 views
0 votes
1 answer

Is there a scanf() or sscanf() equivalent in python?

There is nothing as such for python. For ...READ MORE

answered Jul 29 in Python by Greg
139 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 289 views
+4 votes
6 answers
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,160 points
67 views
0 votes
1 answer

Is there an expression for an infinite generator in Python?

you can iterate over a callable returning ...READ MORE

answered Nov 20, 2018 in Python by Nymeria
• 3,520 points
108 views