Equivalent of a b c in Python

0 votes
What should I do if I want to print out a string in python. I don't want to use the following:

if isfemale_bit:
    print 'F'
else:
    print 'M'
 

The best option is print ['M', 'F'][int(isfemale_bit)]? But is there any better alternative?
Jan 7, 2019 in Python by ana1504.k
• 7,910 points
4,212 views

2 answers to this question.

+1 vote
In Python 2.5, you can use ternary conditionals like this:

a if b else c
answered Jan 7, 2019 by SDeb
• 13,300 points
+2 votes
result = "F" if(isfemale_bit) else "M"

print(result)

Try this out, is the Python's "A if C else B"
answered Dec 15, 2019 by Manel
Hey! I hope you are doing good.

Please register at Edureka Community and upvote the answer if it has helped you. You get credits for every contribution(answering, commenting, asking a question, upvoting/downvoting an answer or question) once you register you can use these credits to become an admin on this community or get discounts of Edureka products.

Related Questions In Python

0 votes
1 answer

The equivalent of a GOTO in python ​​

Gotos are universally reviled in computer science ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 58,020 points
12,766 views
0 votes
0 answers
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
36,075 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,483 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2, 2019 in Python by anonymous
5,890 views
0 votes
1 answer

Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

answered May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points
667 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,468 views
0 votes
1 answer
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