Python Coding Exercise #13: Simple if Statements
Part of the series Python Practice Exercise — part 13
Before you start: Know input() and int() · Know what an if statement looks like
This post groups two short exercises that both ask the program to make a decision with an if statement. If if and else are new to you, Exercise #12 walks through how they work step by step.
Quick reminder: comparison operators
An if statement runs its indented lines only when a condition is true. Conditions are usually built with comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | is equal to | 1984 == 1984 | True |
!= | is not equal to | 1984 != 2020 | True |
< | is less than | -7 < 0 | True |
> | is greater than | 5 > 9 | False |
<= | is less than or equal to | 3 <= 3 | True |
>= | is greater than or equal to | 2 >= 5 | False |
Watch out for one classic mix-up: a single = assigns a value (number = 5), while a double == compares two values. Inside an if, you almost always want ==.
Exercise 1: Orwell
The question
Please write a program which asks the user for an integer number. The program should print out “Orwell” if the number is exactly 1984, and otherwise do nothing.
Please type in a number: 2020
Another example:
Please type in a number: 1984
Orwell
My Solution
number = int(input("Please type in a number: "))
if number == 1984:
print("Orwell")
How it works
int(input(...))asks for a number and converts it from text to an integer.number == 1984is the condition. It’sTrueonly when the number is exactly 1984.- If the condition is true, the indented
print("Orwell")runs. If it’s false, Python skips the indented line, and the program simply ends.
Watch out for an unnecessary else
It’s natural to want to add an else branch to cover “everything else”:
number = int(input("Please type in a number: "))
if number == 1984:
print("Orwell")
else:
print()
But the exercise says to do nothing otherwise, and print() is not nothing. An empty print() still outputs a blank line. For the input 2020, this version prints an invisible empty line, while the correct program prints nothing at all. Since the course checks the output exactly, that difference matters. (We saw the same kind of invisible-output problem in Exercise #05.)
An if doesn’t need an else. When there is nothing to do in the other case, leave it out.
Exercise 2: Absolute value
The question
Please write a program which asks the user for an integer number. If the number is less than zero, the program should print out the number multiplied by -1. Otherwise the program prints out the number as is. Please have a look at the examples of expected behaviour below.
Please type in a number: -7
The absolute value of this number is 7
Please type in a number: 1
The absolute value of this number is 1
Please type in a number: -99
The absolute value of this number is 99
My Solution
number = int(input("Please type in a number: "))
if number < 0:
print(f"The absolute value of this number is {number * -1}")
else:
print(f"The absolute value of this number is {number}")
How it works
The absolute value of a number is its distance from zero, so it is never negative: the absolute value of both 7 and -7 is 7.
The program checks number < 0:
- If the number is negative, multiplying it by
-1flips its sign:-7 * -1is7. - Otherwise, the number is zero or positive, so it is already its own absolute value and is printed as is. That includes
0.
University Solution
number = int(input("Please type in a number: "))
absolute_value = number
if number < 0:
absolute_value = number * -1
print("The absolute value of this number is", absolute_value)
The University Solution stores the answer in a variable called absolute_value. It starts out equal to the number, and only the if changes it when the number is negative. The print() then appears just once, after the if, so the sentence isn’t written twice. If you ever wanted to change the wording, you’d only have to change it in one place.
Python can do this for you
Python has a built-in function, abs() (see Built-in Functions in Python), that returns the absolute value directly:
number = int(input("Please type in a number: "))
print("The absolute value of this number is", abs(number))
It gives the same output for every example above. The if version is still worth writing, because the exercise is practice in making decisions. In everyday code, though, abs() is the right tool.
Key takeaways
- An
ifstatement runs its indented lines only when the condition is true. ==compares two values,!=checks they differ, and<,>,<=and>=compare sizes. A single=is for assigning, not comparing.elseis optional. If nothing should happen in the other case, leave it out. An emptyprint()still prints a blank line.- Storing a result in a variable and printing it once after the
ifavoids repeating the sameprint()line. - The absolute value of a number is its distance from zero. Python has
abs()for it.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: These exercises are taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.