Conditions: if, then, else - Learn Python 3 - Snakify

Lesson 3. Conditions: if, then, else




2/15. If and else: what has just happened?

Let's discuss what's happening in details.

This program uses the conditional statement if. After the if we put a condition x > 0 following by colon. After that we put a block of instructions which will be executed only if the condition is true (i.e. evaluates to True). This block may be followed by the word else, colon and another block of instructions which will be executed only if the condition is false (i.e. evaluates to False). In the case above, the condition if false, so the if-block is ignored and the else-block is executed.
x = int(input())
if x > 0:
    print(x)
else:
    print(-x)