if statements Python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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