How does Python identify which commands are inside if?

Asked

Viewed 112 times

3

In C we have how to inform the size, or how far the function is present as for example:

if (n > 2){ ... }

but in Python is

if n > 2: ...

I would like to understand how far these functions end because in C we have keys to view but not in Python, when I need to create a if, or is not sure how far the if is acting to create another function.

  • 2

    In a row: IDENTATION

  • Block "open and close" is done through indentation

1 answer

3


In C we have how to inform the size, or how far the function is present

This statement is wrong. This is nothing to do with function this is a block of commands. A function always has a block of commands. Some commands may or may not have a command block. So this is valid:

if (n > 2) ...

or

if (n > 2)
    ....

As long as it’s just one statement. If there is more than one there keys should be used to group commands as a single block, in C. Some people always prefer to use the block even when there is only one statement by standardization. I tested both in each language, I get a wall top in some cases.

It doesn’t matter if you have one line or more, the number of lines doesn’t define anything in C, but if you have several there will be confusion and it may seem something wrong. Even indentation doesn’t matter to that language, so it doesn’t do what you think it does (only the first line is conditional):

if (n > 2)
    printf("%d", n);
    n++

Python uses a technique called whitespace significancy, so when you do a spacing called indentation it’s like you’re starting a block of commands, it’s like you’re opening the key (indent) and when it comes back some spaces in a row to the same column that the header of that block is like closing the key (dedent).

In Python lines are confused with statements in many cases.

As there is some ambiguity for the author it is common to need to put : at the end of the command that may have a block. The compiler doesn’t need this and I think it was an exaggeration to demand this (the language has the philosophy of being explicit primarily, but does not always follow, after all the use of the keys is the most explicit and unambiguous way of doing this, it is even the strongest criticism of not using keys in Python or C).

Then it is valid (a line):

if n > 2: print(n)
print()

Or so:

if n > 2:
    print(n)
n += 1 #não é condicional
print()

Who do the same thing. Now:

if n > 2:
    print()
    n += 1 #condicional
print()

I put in the Github for future reference.

Already there the two indented lines are part of the same block and are conditional.

The first one doesn’t have a clear block, so it closes there on the same line, and we know it doesn’t have a block because it didn’t have indentation.

The second is the indentation that determined the beginning and end of the command block, in case having only one command.

And the third indentation determined a block of two statements.

Python preferred to officially adopt what is only conventionally used in C to better organize. I like it, a lot of people don’t like it. It has advantages and disadvantages. In C it works with all wrong indentation, not in Python (to some extent). Python tends to make the code cleaner, but those who are disorganized make bad code the same way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.