🤔 Conditions - If Statements
Learn how to make Python make decisions!
📖 The "If" Statement
An "if" statement tells Python to do something ONLY if a condition is true.
⚠️ Indentation is Key!
Available space at the beginning of a code line is called indentation. Python relies on indentation (whitespace at the beginning of a line) to define scope. Other programming languages use curly-brackets for this purpose.
🎯 Quick Quiz
Question: What happens if you forget the indentation?
💻 Try It Yourself!
Write an if statement to check if 10 is greater than 5.
↔️ Elif - Multiple Choices
What if the first condition isn't true?
📖 The "Elif" Keyword
The elif keyword is Python's way of saying "if the previous conditions were not
true, then try this condition".
🎯 Quick Quiz
Question: Can you have multiple ELIFs?
💻 Try It Yourself!
Create two variables equal to each other and use elif to check equality.
🛑 Else - The Fallback
Catch anything that isn't caught by the preceding conditions.
📖 The "Else" Keyword
The else keyword catches anything which isn't caught by the preceding conditions.
🎯 Quick Quiz
Question: Does else need a condition (like else x > y)?
💻 Try It Yourself!
Write an if/elif/else chain where the ELSE block runs.
⚡ Short Hand - Quick Logic
If you have only one statement to execute, you can put it on the same line.
📖 Short Hand If
📖 Short Hand If...Else
🎯 Quick Quiz
Question: Is Short Hand always easier to read?
💻 Try It Yourself!
Write a one-line if statement.
🧠 Logical Operators - And, Or, Not
Combine conditional statements!
📖 And
The and keyword is a logical operator, and is used to combine conditional
statements:
📖 Or
The or keyword is a logical operator, and is used to combine conditional statements:
📖 Not
The not keyword is a logical operator, and is used to reverse the result of the
conditional statement:
🎯 Quick Quiz
Question: True or False? True OR False = ?
💻 Try It Yourself!
Write a condition using 'and' to check two things at once.
📦 Nested If - Layers of Logic
You can have if statements inside if statements, this is called nested if statements.
📖 Nested If
🎯 Quick Quiz
Question: How many levels deep can you nest if statements?
💻 Try It Yourself!
Write an if statement inside another if statement.
👻 Pass Statement - Do Nothing
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
📖 The "Pass" Statement
🎯 Quick Quiz
Question: Does 'pass' print anything?
💻 Try It Yourself!
Write an if statement that does nothing using 'pass'.
🚀 Real World Challenge - Space Mission
Apply everything you've learned to save the mission!
🧑🚀 Mission Briefing
You are the lead programmer for a Space Rocket Launch System. You need to write a Python script to check if the rocket is safe to launch.
Requirements:
- Create a variable
fuel_level(values: 0-100) - Create a variable
weather(text: "Sunny", "Rainy", or "Stormy") - Logic:
- If fuel is less than 50 or weather is "Stormy", print "ABORT LAUNCH!"
- Elif fuel is between 50 and 75 and weather is "Rainy", print "DELAY LAUNCH"
- Else, print "LAUNCH SUCCESSFUL!"
- You MUST use:
if,elif,else, and variables. - Also print the type of your variables using
type()at the end.
💻 Mission Control Console
Write your mission code below. No hints this time!