Conditional Statements

If statements are Conditional Statements

In Python, if statements are used for decision-making. They allow your program to execute certain code only if a specified condition is met

For Example if we wanted to make a program that a user is a teengaer or not, we could use if statements for this

age = int(input("Enter your age: "))
if age < 13:
print("You are NOT a teenager!")
elif age > 13:
print("You are a teenager!")
elif age == 13:
print("You are a teenger!")
else:
print("Invalid Input")

Key Points

Every statement must end with a colon (:)

The code below your statement MUST start with an indent (the example code wouldnt allow indents! )

After your first if statement and want to add another if statement it must be a "elif" statement!

If you want a better understanding of Conditional Statements in Python Click the link below to watch a video about Conditional Statements

https://youtu.be/-QI9kx0jZ7E?si=c0eAqno287lx321t Next

Challenge (optional)

Make a program that asks the user to input a number and if the number is more than 50! print("Thats a high number!") and if the number is below 50 then print("Thank You!") else print("Invalid Input")

r


⏳ Loading...