+ 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

11th Mar 2020, 2:13 AM
Jenda Mustafa
Jenda Mustafa - avatar
3 Respuestas
+ 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
11th Mar 2020, 2:15 AM
Jax
Jax - avatar
+ 1
The num==7 case is never reached.
11th Mar 2020, 5:34 AM
Sonic
Sonic - avatar
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")
11th Mar 2020, 1:12 PM
SHIVANT KUMAR PANDEY
SHIVANT KUMAR PANDEY - avatar