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, 2019 in Python by Nisha
938 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, 2019 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, 2019 in Python by Junaid
2,216 views
0 votes
1 answer

Is there a list of timezones in python?

import pytz list(pytz.common_timezones) READ MORE

answered Aug 2, 2019 in Python by Mohammad
• 3,230 points
1,223 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, 2019 in Python by Arvind
• 3,050 points
716 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 30, 2019 in Python by Greg
7,397 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,423 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,043 views
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,300 points
943 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,560 points
2,048 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP