Confusion in "nested if" in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confusion in "nested if" in Python

how does actually nested if work in Python. In this following code , 1st and 3rd if statements are true. so I get two output as expected. https://code.sololearn.com/cT9SwUJ1rV3O/?ref=app but if I put a TAB before 3rd "if" , it becomes part of the second"if". In C or Java we use braces to make nested "if". so I'm finding Python "nested if" a bit confusing. Thanks in advance.

30th Jul 2017, 3:00 PM
Rupak Chowdhury
Rupak Chowdhury - avatar
3 Answers
+ 1
It works based on indentation. It is 4 spaces or a tab.
30th Jul 2017, 3:08 PM
Biser Z Ivanov
Biser Z Ivanov - avatar
+ 1
Unlike most other programming languages, white space in python matters, and essentially replaces curly brackets. While in other languages you could write the something like: static void myFunction() { // Good looking code; } as: static void myFunc( ){ // Don't write code like this! This is confusing to look at; } In python, this would not work. Instead of curly brackets, python uses indention.
30th Jul 2017, 3:30 PM
Christian Barraza
Christian Barraza - avatar
0
got it :) thanks to both of you
30th Jul 2017, 3:42 PM
Rupak Chowdhury
Rupak Chowdhury - avatar