Simple Conditions

Simple conditions are often referred to as if statements and allow decisions to be made within our code.

If a user is over a certain age, then they can drive a car legally.

If a user has a certain name, then they can view their details.

Key Points

Headings range from Heading 1 to Heading 6.

Heading 1 is the largest and should be used for main headings.

Heading 2 is smaller than heading one and should be used for subheadings.

Headings keep getting smaller all-the-way to Heading 6.

Operators

To make our conditions work properly you must have an understanding of the following operators.

Each operator changes how the conditional statement works.

Example CodeExplanation
if age < 18:Checks if age is less than 18
if age > 18:Checks if age is greater than 18
if age <= 18:Checks if age is less than or equal to 18
if age >= 18:Checks if age is greater than or equal to 18
if age == 18:Checks if age is exactly equal to 18
if age != 18:Checks if age is not equal to 18

Simple Condition Examples

In Python, all of the code that runs when your condition is true, must be indented under the if statement.

In the following code, if the user is 18 or older, they are old enough to watch scary movies.

if age >= 18:
    print("You are older enough to watch scary movies!")

In the following code, if the price is less than or equal to £20.00, then the user has enough to buy the item.

if item_cost <= 20.00:
    print("You can afford this item.")

In the following code, if the user’s username is “StudyParty” then give them access.

if username == "StudyParty":
    print("Access Granted!")

Conditions using Variables

The next step in writing conditions is understanding how variables fit into the conditions.

In the following code snippet, both variables have been declared and assigned, before also being used in the conditional statement.

number1 = 10
number2 = 5

if number1 > number2:
    print("Number 1 is bigger than Number 2!")

National 5 Requirements

You should be able to identify and explain each of the operators.

You should be able to implement simple conditions into your code.