Difference between If and Elif

Asked

Viewed 25,024 times

1

What is the difference between the two code snippets

if variavel < 10:
   variavel = 1
if variavel >= 10 and <15:
   variavel = 2
.
.
.
else 
   variavel = 0

if variavel < 10:
   variavel = 1
elif variavel >= 10 and <15:
   variavel = 2
.
.
.
else 
   variavel = 0

4 answers

9


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.

2

If is for checking a condition and Elif is for checking another condition if the If condition is false. In the code there is not much difference, Elif will ensure that that condition is checked if If is false, different from the two If that are independent 'streams'.

0

Basically, a if is the first if, the else is when you want to make him complete some action if the if is false, and elif is when you have a if within a else

Example: this code snippet:

if(x==1):
    # para if verdadeiro

else:

    if(x==21):
        # para o if dentro do else verdadeiro

is just like that:

if(x==1):
    # primeiro if verdadeiro
elif(x==21):
    #segundo if verdadeiro

which is also equal to this (although it is a "wrong" way of writing, by "breaking" an associated if/Else into several other if’s):

if(x==1):
    # primeiro if verdadeiro
if(x==21):
    #segundo if verdadeiro

-2

And not to mention that you can use more than one Elif ( not always useful, but here is the illustration.

 n = 3
if n == 1:
    print('n == 1')
elif n == 2:
    print('n == 2')
elif n == 3:
    print('n == 3')
elif n == 4:
    print('n == 4')

Browser other questions tagged

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