Is there a Python version of the following if–else statement in C++ or similar statement like this:
int t = 0;
int m = t==0?100:5;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both of the above lines will result in the same thing.
The first line makes use of Python’s version of a “ternary operator” available since version 2.5, though the Python documentation refers to it as
Conditional Expressions.The second line is a little hack to provide inline functionality in many (all of the important) ways equivalent to
?:found in many other languages (such as C and C++).Documentation of Python – 5.11. Conditional Expressions