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

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level ๐Ÿ”ฐ What Is a Module? A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs….

Read more

GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level ๐Ÿ”ฐ What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

Domingo

๐ŸŸข Lesson 4 ๐Ÿ”ฐ What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: ๐Ÿงฑ Example: Why Use…

Read more

SHARK_PRO

๐Ÿ”ฐ What Is File Handling? ๐ŸŸข Lesson 3 File handling lets your Python programs interact with files on your computer โ€” read data from them or save data to them….

Read more

ZinaTv

๐ŸŸข Lesson 2 โ€“ Intermediate Level ๐Ÿ”น Python Dictionaries: Storing and Accessing Key-Value Data ๐Ÿ”ฐ What Is a Dictionary? A dictionary in Python is a data structure that stores data…

Read more