Menu

Conditional Statements

Conditional Statements

Conditional statements allow Python programs to make decisions based on data. They are useful for categorizing values, applying business rules, and assigning labels to observations.

Using if, elif, and else

sales = 85000

if sales >= 100000:

category = "High"

elif sales >= 50000:

category = "Medium"

else:

category = "Low"

print("Sales category:", category)

Python checks the conditions from top to bottom and executes the first matching branch. Indentation is essential because it defines which statements belong to each block.

Classifying Multiple Values

You can use a loop with conditional statements to classify several observations.

sales_values = [25000, 65000, 120000, 42000]

for sales in sales_values:

if sales >= 100000:

print(sales, "High")

elif sales >= 50000:

print(sales, "Medium")

else:

print(sales, "Low")

This is a simple example of rule-based classification. Later, libraries such as Pandas will make it easier to apply conditions to entire columns of a dataset.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details