Lecture
Conditional Statements (if, elif, else)
Sometimes your program needs to make decisions, just like you do in real life:
- If your score is above 90, you get an A.
- If it's above 80, you get a B.
- Otherwise, you try harder next time.
This is the essence of conditional logic.
How Python Makes Decisions
Python uses the keywords if, elif, and else to control which parts of your code run.
Here's a simple example:
Conditional Statements
score = 85 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") else: print("Grade: C or below")
What's Happening Here?
- Python checks the first condition:
score >= 90. It isfalse, so that block is skipped. - Then it checks the next condition:
score >= 80. It istrue, so it printsGrade: B. - Once a condition is met, Python does not check the others.
- If none are true, the
elseblock runs.
Key Ideas to Remember
- Conditions are checked from top to bottom.
- Indentation shows which lines belong to each block.
- You can use multiple
elifstatements, but only oneifand oneelse.
Lessons in this chapter Β· Python Programming Review
- 1. Variables and Data Types
- 2. Expressions and Arithmetic
- 3. String Operations
- 4. Lists in Python
- 5. List Methods and Slicing
- 6. Tuples and Sets
- 7. Dictionaries - Keys and Values
- 8. Dictionary Methods and Nesting
- 9. Conditional Statements (if, elif, else)
- 10. Loops - for and while
- 11. Control Flow & Data Structures Recap
- 12. Multiple-choice quiz
- 13. Loop Controls (break, continue, pass)
- 14. Functions - Defining and Calling
- 15. Function Arguments and Return Values
- 16. Variable Scope and Nested Functions
- 17. Exception Handling with try/except
- 18. File Reading and Writing
- 19. CSV File Parsing
- 20. Using Built-in Modules (math, random, datetime)
- 21. Importing External Modules
- 22. Fill-in-the-blank quiz
Quiz
0 / 1
In Python, which keyword is used to check another condition after an initial if?
if
else
elif
for
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help