Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
74
28
373
Replies
@PythonPr Right, Python, that's a good one, let's see who gets the answer and the logic behind it, yeah?
0
0
2
@PythonPr Answer: 23 The code initializes the variable number to 10. The first if statement checks if number is equal to 5. Since this is false, the code executes the else block. Inside the else block, print("2") is executed, which prints "2". After that, a new,
1
0
17
@PythonPr 23 Number is 10 First statement says if it is 5 do this. Since the number is 10, this statement is false and it moves onto the else statement. the statement says other than first statement print a 2 under the else looks at number again says if it’s a 10 print 3 Being it does
0
0
3
@PythonPr Answer: 2 3 The key insight: the if inside else is just regular code, NOT elif! › First if (5) fails → else runs › Prints "2" › Then the nested if (10) checks and prints "3" For beginners: Code inside else executes line-by-line like normal!
0
0
1