In this section if variable is less than 10, first will set the value 1 and then the else
of the second if will set 0
, this because they are two separate ifs:
if variavel < 10:
variavel = 1
if variavel >= 10 and <15:
variavel = 2
.
.
.
else:
variavel = 0
Here the elif
is part of the "logic" of if
, or if variable is less than 10 it will only set the variavel = 1
:
if variavel < 10:
variavel = 1
elif variavel >= 10 and <15:
variavel = 2
.
.
.
else:
variavel = 0
In short, the elif
is a condition of if
along with else
, would be pretty much the same as doing this:
if variavel < 10:
variavel = 1
else:
if variavel >= 10 and <15:
variavel = 2
.
.
.
else:
variavel = 0
Unlike other languages Python does not use {...}
, That’s because he works with indentation PEP 8 (although the translation of the word is something like "indentation", we usually adapt it from English to "indentation", not that it is the correct way, but perhaps you will hear a lot like this)
IMPORTANT NOTE: I believe this if is wrong elif variavel >= 10 and <15:
, the correct would be elif variavel >= 10 and variavel < 15:
That is to define a if
spaces will be required (note that python3 no longer accepts mixing tabs) on the following lines:
if ... :
exec1
exec2
exec3
If you do this the exec3
will be out of condition:
if ... :
exec1
exec2
exec3
About using the Portuguese "indentation" - it’s effectively how the Brazilian Python community uses it. For several years, Luciano Ramalho (founder of Assoc. Python Brasil and author of Fluent Python) and I tried to use other terms - he suggested that the correct could be "endentation" (and later discovering that not). The use of "recoil" simply does not refer to enough code, despite being an existing Portuguese. So, even better to continue using "identation" and let the grammars update themselves with the real use of the language, day by day, in some years, as with all terms.
– jsbueno