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

FlyFeditv

๐Ÿ“˜ Introduction In this project, youโ€™ll create a command-line Expense Tracker that allows users to: โœ… Add expenses with category, amount, and descriptionโœ… View total spending and category breakdownโœ… Automatically…

Read more

NetCinFly

๐Ÿ“˜ Introduction In this project, youโ€™ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

๐ŸŒ Introduction In this project, youโ€™ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.Youโ€™ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

๐ŸŒฆ๏ธ Introduction In this project, youโ€™ll build a Weather App in Python that retrieves live weather information from an online API.Youโ€™ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: ๐ŸŽฏ Objective: Create a simple, text-based contact book application that allows users to: ๐Ÿ’ก Code (contact_book.py): ๐Ÿง  Example Usage Console Output Example: ๐Ÿ’พ Notes โฌ‡๏ธ Download Show7-Pro ุชุญู…ูŠู„…

Read more

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