+ 3
if statements Python
num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") why the answer is not 3 and 7
3 Answers
+ 8
It all depends on the way theyâre nested. The âif num == 7â is nested inside of âif num < 5â meaning âif num < 5â has to be true or else âif num == 7â will never execute
+ 1
The num==7 case is never reached.
0
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
Example
One line if else statement:
a = 2
b = 330
print("A") if a > b else print("B")