If/else statements

An "if(x):" statement is a programming conditional statement that, if "x" is True or not empty, performs some code. An "else:" is a programming conditional statement that if previous conditions are not true, it will perform an alternate code part. An "elif(x):" (short of "else if") is a conditional statement performed after an if statement that, if true, performs a function.

Now, it is important to mention that Python does not use "{}" to bracket lines of code. Instead, Python is a language that totally relies on indentation. Indentation refers to the whitespaces at the beginning of each code line, and Python uses indentation to indicate a block of code. You will need to indent code after each line that has a ":" at the end. Don't worry, you will get used to it.

"x" will be interpreted as True when:

A = 3
B = True
C = []      # Empty list
D = [0]     # Not empty list

if A == 3:
    print('1, This line will run')

if 0 < A and B:   
    print('2, This line will run')
else:       # It won't reach this part because the previous is already True
    print('2, This line will not run')

if C:       # C is empty, so it is interpreted as False
    print('3, This line will not run')
elif D:     # D is not emptu, so it is interpreted as True
    print('3, This line will run')
else:       # It won't reach this part because the previous is already True
    print('3, This line will not run')

if 0:       # 0, as an integer, is interpreted as False
    print('4, This line will not run')
elif -1:    # Any integer diffetent than 0 is interpreted as True
    print('4, This line will run')
elif 2:     # It won't reach this part because the previous is already True
    print('4, This line will not run')
else:       # It won't reach this part because a previous one is already True
    print('4, This line will not run')

Nested if/else

Sometimes you will need to do multiple checks, in those cases you will need nested if/else statements. It is totally valid and really helpful.

# This code will tell if a number is multiple of 3, 5 or 15
A = int(input('Give me an integer number: '))

if A%3==0:
    if A%5==0:
        print(A,'is multiple of 15')
    else:
        print(A,'is multiple of 3')
elif A%5==0:
    print(A,'is multiple of 5')
else:
    print(A,'is not multiple of 3 or 5')

Ternary operator (if/else in 1 line)

Even though some books will not recommend it for "good practices", it exists and thanks universe for it. It is really helpful to avoid meaningless lines.

# This code will tell if a number is multiple of 3, 5 or 15
A = int(input('Give me an integer number: '))

if A%3==0:
    # Printing with ternary operator
    print(A,'is multiple of 15') if A%5==0 else print(A,'is multiple of 3')    
elif A%5==0:
    print(A,'is multiple of 5')
else:
    print(A,'is not multiple of 3 or 5')

# Assingning a value with ternary operator
conditional = True
val = "True case" if conditional else "Otherwhise"

Pass (do nothing)

"if" statements cannot be empty, but if you have an if statement with no content (for any reason), put in the "pass" statement to avoid getting an error. You can use it when you want to run your code and you are not ready to write the full code of that case.

# This code will tell if a number is multiple of 3, 5 or 15
A = int(input('Give me an integer number: '))

if A%3==0:
    print(A,'is multiple of 15') if A%5==0 else print(A,'is multiple of 3')    
elif A%5==0:
    print(A,'is multiple of 5')
else:   # It won't print anything
    pass