Control the flow of your program by making decisions based on conditions.
Imagine you are designing a smart home: how does the heater 'know' to turn on only when the temperature drops, rather than running all day and wasting energy?
In programming, we don't always want every line of code to run. Sometimes, we only want an action to happen if a specific condition is met. This is called conditional logic. At the heart of this is the Boolean value, which can only be or . When you write an `if` statement, Python checks the condition. If the condition evaluates to , the code inside the block runs. If it is , Python simply skips over it and moves to the next part of the program. It is like a gatekeeper that only opens the door when the password is correct.
Quick Check
What are the two possible values a condition can evaluate to in an if-statement?
Answer
True or False (Boolean values).
To write an `if` statement, you must follow Python's strict syntax rules. First, you start with the keyword `if`, followed by your condition (like ). Second, you must place a colon `:` at the end of that line. This colon acts like a signal saying, 'The instructions are coming up next!' Finally, the code that belongs to the `if` statement must be indented (usually 4 spaces). This indentation is how Python knows which lines are part of the decision and which lines are just the rest of the program.
Let's check if a student passed a test.
1. Define the variable: `score = 85` 2. Create the condition: `if score >= 50:` 3. Indent the action: ` print("You passed!")`
Since is , the message will appear on the screen.
Quick Check
What happens if you forget to indent the code underneath an if-statement?
Answer
Python will throw an IndentationError and the program will not run.
When reading code, you must act like the computer. Look at the variable values first, then solve the math in the condition. If the condition is , ignore everything indented. For example, if we have `age = 10` and the code says `if age >= 18:`, the computer sees as . It will skip the indented block entirely. This 'skipping' is what allows programs to behave differently in different situations, making your software feel 'smart' and responsive to user input.
We can use the modulo operator `%` to check for even numbers. The expression checks if the remainder of divided by is zero.
1. `number = 7` 2. `if number % 2 == 0:` 3. ` print("This is even!")` 4. `print("Check complete.")`
In this case, equals . Since is , the 'even' message is skipped, and only 'Check complete' is printed.
Which of the following is a correctly formatted if-statement header?
If `temp = 32`, what is the output of: `if temp > 30:` ` print('Hot')` `print('Done')`?
In Python, you can use any number of spaces for indentation as long as it is consistent, but 4 spaces is the standard.
Review Tomorrow
Tomorrow, try to explain to a friend why the colon and indentation are like a 'container' for the code that only runs sometimes.
Practice Activity
Create a 'Password Checker' script: define a variable `password`, and if its length is less than 8, print a warning message.