0

Today I downloaded a python to do some simple test, what to me is strange is that python's if-else statements don't have brackets.

Then how to prove one statement belong to which branch? During my test, when I changed the indentation, result is different. (b = b*2) Does it mean python determine the statements by user's coding style? So how if a user doesn't follow the indentation rule?

a = 10
b = 20

i = 0

while i<5 :
   if (a < 9) :  
       if (a < 5) :
           b = b - 1
       else :
           b = b + 1
   b = b*2
   a = a - 1
   i = i + 1
   print (a, b, i)

a = 10
b = 20

i = 0

while i<5 :
   if (a < 9) :  
       if (a < 5) :
           b = b - 1
       else :
           b = b + 1
           b = b*2
   a = a - 1
   i = i + 1
   print (a, b, i)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
sean
  • 51
  • 6
  • 7
    Python uses indentation to delimit blocks. Indentation is not just style, it is a requirement. – Martijn Pieters Aug 30 '13 at 08:02
  • 1
    You can drop the parentheses around `if` test expressions; they are not needed and they detract from readability. – user4815162342 Aug 30 '13 at 08:32
  • Don't try to learn a language by trial and error and asking questions here. Read a good book on the subject. This is just about the first subject covered by every Python book. – David Heffernan Aug 30 '13 at 13:56
  • I agree with you that to learn a new language we need read some guide systematically to understand it better. But in real case, we are not at school, sometimes you are only given half day to do a task and you want to try some new method you are not familiar with, trail and ask is not a bad thing, right? – sean Sep 02 '13 at 01:12

3 Answers3

4

So how if a user doesn't follow the indentation rule?

The coder must follow proper indentation. The only other choice is for the code to not work properly (or to work by accident, but that's not really a choice per se).

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Indentation is part of Python's syntax.

Discarding the indentation is no different to discarding the { } in C (i.e you end up with broken or incorrect code)

Why? This question is answered in the Design and History FAQ,

Guido van Rossum believes that using indentation for grouping is extremely elegant and contributes a lot to the clarity of the average Python program. Most people learn to love this feature after a while.

Since there are no begin/end brackets there cannot be a disagreement between grouping perceived by the parser and the human reader. Occasionally C programmers will encounter a fragment of code like this:

if (x <= y)
        x++;
        y--;
z++;

Only the x++ statement is executed if the condition is true, but the indentation leads you to believe otherwise. Even experienced C programmers will sometimes stare at it a long time wondering why y is being decremented even for x > y.

Because there are no begin/end brackets, Python is much less prone to coding-style conflicts. In C there are many different ways to place the braces. If you’re used to reading and writing code that uses one style, you will feel at least slightly uneasy when reading (or being required to write) another style.

Many coding styles place begin/end brackets on a line by themselves. This makes programs considerably longer and wastes valuable screen space, making it harder to get a good overview of a program. Ideally, a function should fit on one screen (say, 20-30 lines). 20 lines of Python can do a lot more work than 20 lines of C. This is not solely due to the lack of begin/end brackets – the lack of declarations and the high-level data types are also responsible – but the indentation-based syntax certainly helps

Also the History of Python has an additional story about the origins of the indentation syntax

dbr
  • 165,801
  • 69
  • 278
  • 343
  • in verilog/systemverilog, we use "begin..end" and in C or perl we use "{}", now Python is using indentation. Sounds good, I just need to get used to it :) – sean Sep 02 '13 at 01:26
  • @sean: Once you do, you won't look back. – JS. Sep 06 '13 at 22:02
0

That's why after an if statement you put a colon. After that, you do what i known as 'indentation' where basically what you put a space (or how ever many spaces you want) after the on the next line or so and then type the code you want to run during the if statement. If you don't insert the spaces, then python will come up with an error, because you added an if statements so it expects you to have some sort of indentation. For example:

if 4<7:
 print("Hello")

Also, there is no indentation rule. It can be as many lines as you want, but generally since you want your code to be readable by humans, the max spaces I would I indent any code is 4.

  • thanks,now my understanding is that the compiler is designed to expect indentation as a rule other than a style. It is python, we need follow some basic rules if we want to use it properly :-) – sean Sep 02 '13 at 01:18