Learn how to use the
ifstatement 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:
if--elsestatements in pythn
Step 1: Create Project
Create python project or file named
Step 2: Write Code
Add the following code:
age = int(input('Input your age: ')) if age >= 18: print('adult') elif age >= 6: print('teenager') else: print('kid')
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:
I then enter
You will get the following:
Input your age: 87 adult