Intro to Programming: Conditionals

Basics of Python Syntax / Intro to Programming

Yeldos Balgabekov
3 min readJan 15, 2023

This lesson is written in the co-authorship with artificial intelligence and lots of manual edits of a human being. The rest of the course can be found here: “Intro to Programming”.

Conditional expressions in real life

A conditional statement is like a set of instructions that tells a computer to do something only if a certain condition is met. It’s like giving instructions to your little brother or sister, like “if you finish your homework, then you can play video games.” The computer will only follow the instructions if the condition (finishing homework) is true.

A similar example of a conditional statement in programming could something like this:

if it's hot outside:
drink water

So if it’s hot outside the computer will follow the instruction and drink water, if it’s not hot, the computer will not follow the instruction.

Another example is:

if it's cold outside:
wear a jacket
else:
wear a T-shirt

So if it’s cold outside the computer will wear a jacket, if it’s not cold, the computer will wear a T-shirt.

Conditional expressions in Python

A conditional expression in Python is a way for a program to make a decision based on a certain condition. It is written using the if keyword followed by the condition, a colon, and a block of code that will be executed if the condition is true. The basic syntax for a conditional expression in Python is:

if condition:
# code to be executed if the condition is true

For example,

if age < 7:
print("You are a kid")

In this example, the condition is age < 7, so the program would check the value of age and if it is less than 7, it would execute the code inside the block, which is print("You are a kid").

You can also use elif statement ( "else if") which allows you to check for multiple conditions one after the other. The syntax for an elif statement is:

elif condition:
# code to be executed if the condition is true

You can use as many elif statements as you need.

An else statement is used to execute a block of code if none of the conditions in the if and elif statements are true. The syntax for an else statement is:

else:
# code to be executed if none of the conditions are true

So, in the example I provided before, the complete code will look like:

if age < 7:
print("You are a kid")
elif age < 18:
print("You are a teenager")
elif age < 60:
print("You are an adult")
else:
print("You are a senior")

This program will check the value of age and will print "You are a kid" if age is less than 7, "You are a teenager" if age is greater or equal than 7 but less than 18, "You are an adult" if age is greater or equal than 18 but less than 60 and "You are a senior" if age is greater or equal than 60.

The indentation is very important in Python, it indicates the block of code that should be executed when the condition is true, if you don’t indent the code in the right way, the code will not work properly.

If want to learn more about how `elif` statements makes the life of programmers better, see this lesson: Intro to Programming: Why ‘elif’ matters?

--

--