Skip to content

Ahad-mirza/Python-Keywords

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Understanding if, elif, and else Statements in Python

This repository contains an explanation of how if, elif, and else statements work in Python. These conditional statements are crucial for decision-making in programming and allow a program to perform different actions based on varying conditions.

Conditional Statements Explained

if Statement

The if statement is used to evaluate a condition. If the condition evaluates to True, the block of code associated with the if statement is executed. It is the starting point of any decision-making process.

elif Statement

elif stands for "else if" and is used to evaluate another condition if the initial if condition is False. It allows for checking multiple possibilities sequentially. You can have multiple elif conditions in your logic.

else Statement

The else statement provides a fallback option. It is executed when none of the preceding if or elif conditions are satisfied. It ensures that the program can handle unexpected inputs or default scenarios.

Scenarios Discussed

1. Username and Password Verification 🔐

This scenario demonstrates how to verify a user's credentials. If the provided username and password match the predefined ones, access is granted. Otherwise, an error message is displayed.

2. Temperature Check 🌡️

Based on the input temperature, different suggestions are provided:

  • High temperature (≥30°C): Advice to stay hydrated.
  • Moderate temperature (between 20°C and 30°C): Suggestion to enjoy a walk.
  • Low temperature (<20°C): Recommendation to wear a jacket.

3. Tax Rate Calculation 💰

The script calculates the tax rate based on annual income:

  • Low-income earners (≤50,000): Tax rate is 10%.
  • Middle-income earners (50,001–100,000): Tax rate is 20%.
  • High-income earners (>100,000): Tax rate is 30%.

4. Day of the Week Check 📅

This scenario identifies whether the entered day is a weekend or a weekday. It categorizes Saturday and Sunday as weekends, while all other days are considered weekdays.

5. Driving License Eligibility 🚗

The script determines eligibility for a driving license based on age:

  • Under 16: Not eligible.
  • Between 16 and 18: Eligible with parental consent.
  • Above 18: Fully eligible.

Key Takeaways

  • Conditional statements are essential for making programs responsive and dynamic.
  • The if statement initiates decision-making.
  • The elif statement allows for multiple conditions to be checked sequentially.
  • The else statement handles all cases not explicitly addressed.

Understanding and implementing these statements effectively can significantly enhance your programming skills.

Happy learning! 😊

About

These are Decision making constructors. If you read this and do practice, you will able to apply checks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors