Delux lite

๐Ÿ”ฐ Introduction :

The current image has no alternative text. The file name is: dik.jpg

So far, your programs have performed the same steps regardless of what the user enters.

But what if you want your program’s behavior to change based on a specific condition?

That’s where conditional statements come in!

๐Ÿง  What is an if statement ?

A conditional statement is used when you want code to execute only if a specific condition is met.

โœ… General Format :

Python

if Condition:

# Code to execute if the condition is true

๐Ÿงช Example :

Python

age = int(input(“How old are you?”))

if age >= 18:

print(“You are an adult and can register.”)

๐Ÿ“Œ If the age is 18 or older,

the message will be executed.

If it is younger, nothing will be printed.

๐Ÿ” Using else :

Python

if age >= 18:

print(“You are an adult.”)

else:

print(“You are a minor.”)

๐Ÿ“Œ else is used to execute alternative code if the condition is not true.

๐Ÿ” Using elif (else if) :

Python

grade = int(input(“How much did you score on the exam?”))

if grade >= 90:

print(“Excellent!”)

elif grade >= 75:

print(“Very good.”)

elif grade >= 50:

print(“Pass.”)

else:

print(“Failed, try again.”)

๐ŸŸข The program here makes multiple decisions based on values.

๐Ÿ”Ž Comparison Operators :

Symbol Meaning

== Equal

!= Not Equal

> Greater Than

< Less Than

>= Greater or Equal

<= Less or Equal

๐Ÿ” Real-World Example :

Python

password = input(“Enter password: “)

if password == “12345”:

print(“Login Successful!”)

else:

print(“Incorrect password.”)

๐Ÿงช Exercise 1 :

Write a program that asks the user for their age, then:

If under 13 โ†’ “You are a child.”

If between 13 and 17 โ†’ “You are a teenager.”

If 18 or older โ†’ “You are an adult.”

๐Ÿงช Exercise 2:

Write a program that asks for the username and compares whether it is “admin” or not:

python

username = input(“Enter username: “)

if username == “admin”:

print(“Hello, Administrator!”)

else:

print(“Hello, ” + username)

โš ๏ธ Beginner’s Tip:

Make sure you spell the condition correctly.

Indentations are very important in Python:

โŒ Incorrect:

python

if x > 0:

print(“Positive”)

โœ… Correct:

python

if x > 0:

print(“Positive”)

๐Ÿ“ Conclusion:

You learned how to use if, else, and elif to implement conditions.

You learned how to use comparison operators.

You wrote programs that react to user input and make intelligent decisions.

๐Ÿ“Œ What will we learn in Lesson 4 ?

In the next lesson, we’ll learn about for and while loops, which help you automatically repeat commands without rewriting your code

โฌ‡๏ธ Download Delux ุชุญู…ูŠู„

Related Posts

Rapid_tv

๐Ÿ”น Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. ๐Ÿ‘‰ Rules: ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned…

Read more

VOD-ZalHD

๐Ÿ”น Introduction The Number Guessing Game is a classic beginner Python project. ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned ๐Ÿ”น Summary โฌ‡๏ธ Download VOD-ZalHD ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ…

Read more

Delux_pro

๐Ÿ”น Introduction The To-Do List App is one of the most popular beginner projects. It teaches you how to: ๐Ÿ”น Code Example ๐Ÿ”น Features of This App ๐Ÿ”น Example Run…

Read more

Fam4k

๐Ÿ”ฐ Project Overview A calculator is one of the simplest yet most effective projects to start with in Python.You will learn: โœ๏ธ Step 1: Plan the Calculator We need: ๐Ÿ–ฅ๏ธ…

Read more

Speed_HD+

1. Introduction Data visualization helps us understand patterns and insights in data by converting raw numbers into charts and graphs.In Python, the most popular libraries are: 2. Installing Required Libraries…

Read more

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more