if statements Python | SoloLearn: Learn to code for FREE!


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/11/2020 2:13:18 AM

Jenda Mustafa

3 Answers

New Answer


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


The num==7 case is never reached.


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")