Learn how to use the if statement in python via Examples.

Example 1: Categorize People App

A simple app to categorize a person based on his/her age.

This example will teach you the following:

  1. if--else statements in pythn

Step 1: Create Project

Create python project or file named main.py.

Step 2: Write Code

Add the following code:

age = int(input('Input your age: '))

if age >= 18:
elif  age  >=  6:

input() will return a string with the entered age.
We pass that string to th int() function to convert it to a number, otherwise the numerical comparison wouldn’t be possible.

Step 3: Run

Run the code by navigating to the project directory:

cd your_project_folder

Then execute:

python main.py

I then enter 87.

You will get the following:

Input your age: 87

Categorized in: