Conditional Statements
Welcome to the lesson on conditional statements. Logic is the main part of programming. The if statement is your first step into the realm of decision-making within your code.
` ˙⋆˖⁺‧₊☽◯☾₊‧⁺˖⋆˙ `
Logical Conditions and Boolean Type
Logical conditions are expressions that evaluate to either True or False. These values are known as boolean values and are represented by the bool type in Python.
For example:
#Example: Boolean values
print(True) # Output: True
print(False) # Output: False
In Python, we often use comparison operators to create logical conditions. These operators include:
- == : equal to
- != : not equal to
- > : greater than
- < : less than
- >= : greater than or equal to
- <= : less than or equal to
When we use these operators, the expression evaluates to either True or False:
#Examples of logical conditions
print(5 == 5) # Output: True
print(5 != 3) # Output: True
print(10 > 7) # Output: True
print(4 < 6) # Output: True
print(7 >= 7) # Output: True
print(3 <= 5) # Output: True
These boolean values are crucial for controlling the flow of your program using conditional statements.
If Statements
An if statement is used to test a condition and execute a block of code if the condition is true. If the condition is false, the code block is skipped.
The syntax for an if statement (note on tabulate before the code):
- if condition:
tab code to execute if the condition is true
#Example: If statement
age = 18
if age >= 18:
print("You are an adult.")
In this example, the condition age >= 18 is checked. If it evaluates to True, the message "You are an adult." is printed.
Practical Application
In the Hall of Decisions, the power to make choices based on conditions is crucial. The if spell lets you cast different outcomes depending on circumstances.
⋆˖⁺‧₊ Create a spell that checks if your power level is sufficient to enter the Inner Sanctum (power level must be greater than 50). ₊‧⁺˖⋆
Sample Output:
Your level is 75.
You may enter the Inner Sanctum.