Python Programming · Week 4 notes
Conditionals
Introduction to Conditionals
Definitions and More
Here, you can find the definitions and the topics that we had covered in class.
What are Conditionals?
Conditionals are simply statements that say that if something happens, the computer must do this, otherwise, the computer should do something else.
Think of it as your mom telling you to finish your chores. If you finish your chores, you will get your allowance. But if you don't, your mom will get mad at you.
Types of Conditional Statements
- If statement: The if statement simply starts off the conditional
- Elif statement: This is the middle, it is like, if the one before didn't happen, but this event occurs, then do the following function
- Else statement: This is the final statement, this ends it saying that, if nothing above happened, then do the following
Conditions
Conditions are the part of the conditional which state what is supposed to happen.
For math:
- Equal to:
== - Not equal to:
!= - Less than:
< - Greater than:
> - Less than or Equal to:
<= - Greater than or equal to:
>=
For strings, you can use equal (==) to and not equal to (!=) for determining whether the string is the same or not the same as the other string that it is compared to.
How to Write Conditionals:
- For conditionals, you have the if or the elif, and then have a space and write the condition statement followed by a colon.
- The next line, which tells the computer what to do, must have a tab or 4 spaces before you can start writing.
- After the writing of what to do under a condition, write the elif and the statement (back to the same level/remove the four spaces/tab), or the else, depending on what you are trying to do.
- Tabs/4 spaces are basically indicators that something is part of a loop, function, or conditional statement.
If Statements
If Statements in Python
The if statement is the most basic form of conditional in Python. It allows you to execute a block of code only if a certain condition is true.
Basic Syntax
if condition:
# code to execute if condition is true
Example
age = 18
if age >= 18:
print("You are an adult")
In this example, the message "You are an adult" will only be printed if the age is 18 or greater.
Multiple Conditions
You can use logical operators to check multiple conditions:
and: Both conditions must be trueor: At least one condition must be truenot: Inverts the condition
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive a car")
Remember, indentation is crucial in Python. The indented block under the if statement will only execute if the condition is true.
Elif and Else Statements
Elif and Else Statements
The elif (else if) and else statements allow you to handle multiple conditions and provide alternative code blocks to execute.
Elif Statement
The elif statement allows you to check multiple conditions one after another.
score = 85
if score >= 90:
print("A grade")
elif score >= 80:
print("B grade")
elif score >= 70:
print("C grade")
elif score >= 60:
print("D grade")
Else Statement
The else statement provides a default block of code to execute when none of the previous conditions are true.
score = 55
if score >= 90:
print("A grade")
elif score >= 80:
print("B grade")
elif score >= 70:
print("C grade")
elif score >= 60:
print("D grade")
else:
print("F grade")
In this example, if the score doesn't meet any of the previous conditions, it will print "F grade".
Combining If, Elif, and Else
You can use these statements together to create complex decision-making structures:
age = 25
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 65:
print("Adult")
else:
print("Senior")
Remember, Python will execute the first condition that evaluates to True and skip the rest of the conditions.
Nested Conditionals
Nested Conditionals
Nested conditionals are conditional statements inside other conditional statements. They allow for more complex decision-making processes.
Basic Structure
if outer_condition:
# outer code
if inner_condition:
# inner code
else:
# inner else code
else:
# outer else code
Example
age = 25
has_license = True
if age >= 18:
print("You are an adult")
if has_license:
print("You can drive a car")
else:
print("You need a license to drive")
else:
print("You are not an adult yet")
In this example, we first check if the person is an adult. If they are, we then check if they have a license to determine if they can drive.
Tips for Using Nested Conditionals
- Use nested conditionals sparingly, as they can make your code harder to read and maintain.
- Consider using logical operators (and, or) instead of nesting when possible.
- Pay close attention to indentation, as it determines the structure of your nested conditionals.
Nested conditionals are powerful, but remember that simpler code is often easier to understand and maintain.