There is one more topic to cover from the Introduction to Python's module three. The final topic in this module is conditionals. Today's post will explain why conditionals are important in programming and how to create different kinds of conditionals in Python. By the end of this post, you'll learn how to make each type of conditional statement and use some of the best practice guidelines to help you along the way.
What is a conditional statement?
When developers discuss conditionals, they are referring to making decisions. Computers must make numerous decisions, and they must do so quickly. Conditional statements help the computer accomplish this.
The way people make decisions is very similar to how computers make decisions. For example, people look at the weather to decide what they need before stepping outside. The forecast helps people make multiple decisions quickly.
If the forecast predicts rain, a person might want to take an umbrella. However, a forecast that predicts snow will encourage a person to bundle up completely. A forecast that predicts warm, sunny weather will encourage someone to pack sunglasses.
These kinds of decisions are encountered by computers every day in large quantities. To make lots of decisions quickly, they need to look at the conditions for each decision. The condition determines if the computer runs the actions or ignore them.
If a condition is true, the computer will run the actions that need to be performed. However, a false condition tells the computer to ignore the actions. It will move on to the next decision that needs to be made.
Conditionals are an example of selection. Think of them as the digital crossroads computers encounter as they go through their to-do lists. Conditionals depend on three statement types. They are:
- if statement
- if-else statements
- if-elif-else statement
If Statements
The first statement you need to learn is an if statement. The if-statement is level 1 of the conditional statements because it makes only one decision. The computer checks if the condition is true and will run the code in a sequence.
If the condition is false, the computer will ignore the code and just move to the rest of the program. To create an if statement, you begin with an if keyword. The if keyword lets the computer know you are about to create a conditional statement.
Next, put your condition. Once you are have finished your condition, put a colon at the end. Press Enter so you are on the next line.
Underneath your conditional statement, put any code that you would like to run if this condition is true. Make sure you indent this code before you start typing. Your code might look like this code sample below.
temp = 40
if temp == 40
print("Time for fall! Is Linus still waiting on the Great Pumpkin?")
This code sample has an example of an if statement. There is a variable with a string assigned to it. On the second line of the code is an if statement that checks if the temp is 40. If it is true, the code on the third line will run. If the temp is false, the code in this if statement won't run.
If-Else statement
The if-else statement is the level 2 of the conditional statement. You still have an if statement. The big difference is the else condition.
When the first condition is false in an if-else statement, the else statement is a second condition developers can use to add another condition to their code. If the first condition is false, the computer will automatically run the else code before continuing with the rest of the program. Sometimes you won't need an else statement so you can use an if statement or simplify your code to one line.
To create an if-else statement, create the if statement just like the example in the if-statement section. After you finish writing the code that will run if the first condition is true, press Enter so you are staring a brand new line.
Before you start typing, make sure you backspace so there is no indent. Next, put the else keyword. This tells the computer you are creating another condition. After the else keyword, put a colon.
Press Enter again so you are on a brand new line. Indent your code and type out the code you want the computer to run. Your code will look like this code sample underneath.
temp = 25
if temp == 40:
print("Time for fall! Is Linus still waiting on the Great Pumpkin?")
else:
print("Let it is snow and drink some hot cocoa!")
I added an else statement from the code I had earlier and changed the value assigned to the temp variable. When the computer runs this code, it will check the first condition to see if the temp variable is equal to 40. The variable's value is 25, making the first condition false.
The computer will ignore the first condition's code and move to the else statement. It will run the string inside the else condition in the console.
If-Elif-Else
Do you have multiple conditions for one decision? Level 3 of the conditional statements is the if-elif-else statement. This conditional statement allows developers to create multiple conditions when the first condition is false.
To create an if-elif-else statement, start by creating an if-else statement just like the example above. After you write the code, the first condition needs to run. Press the Enter key.
On this new life, backspace so you aren't indenting. Put the elif keyword. Elif is a shorter way of saying else if. It tells the computer we are going to check another condition.
After the elif keyword, put the condition you want the computer to check. Once you have finished writing your condition, end with a colon. Then press Enter to go to the next line.
On this new line, indent your code. Then add the code you want the computer to run if this condition is true. Press Enter when you are done.
If you have multiple conditions you need to check, create more elif statements in the same format. Just make sure you use elif when you start each statement. Here's how an elif statement is added to the code sample you have seen in the previous two levels.
temp = 80
if temp == 40:
print("Time for fall! Is Linus still waiting on the Great Pumpkin?")
elif temp == 80:
print("It is summertime! Pull out those sunglasses."
else:
print("Let it is snow and drink some hot cocoa!")
This code sample keeps the original if and else statements from the previous levels. The new addition is the elif statement in between them. The elif statement has the condition after the elif keyword with the colon.
The line after the elif statement is indented, letting the computer know this code needs to run if the condition is true. This code will print a string if the condition is true.
Indent carefully!
Indentation isn't just important for functions and loops. Indentation impacts conditionals, too. If a developer indents incorrectly, the code won't work.
As you create conditional statements, make sure the code inside the if-statement, if-else statements, and if-elif-else statements is indented. When you indent your code, make sure you press the tab key and not the space bar. When you finish writing your conditional statements, always double-check your indentation to see if you missed anything.
Indentation is very important as you start creating nested conditionals. Nested conditionals are conditional statements inside another conditional statement. This means you might be able to have an if-else statement inside another if-else statement.
x = 20
if x < 50:
if x < 40:
print("Hello World")
else:
print("We're less than 50")
else:
print("The number is greater than 50")
This code sample is an example of a nested conditional. Look closely at the if statement. Inside, you'll see another if-else statement.
There is a lot of indentation in this code to make sure the right code will run for the right conditions. If the code wasn't indented correctly, the computer would assume the code goes in another condition or was outside of certain statements. That would mess up any decisions the computer would be making when you run the program.
Pay attention to your sequence!
Sequence is just as important as good indentation. The way you order your code is important in creating good conditionals. This means you structure your code correctly when creating if, if-else, and if-elif-else statements.
When you create a conditional statement, you always start with one if statement. This is the first condition. If you want multiple conditions, you need to use elif followed by the condition you are checking for.
Your last condition should use the else keyword. The else keyword doesn't need a condition, so you just need to put your colon. You don't want to start a conditional with an else statement.
Here's another code sample of how good a sequence looks in a conditional statement. There is an if statement at the start of the code, and there are elif statements when the computer needs to check multiple conditions. At the end is an else statement.
x = 12
if x == 25:
print("The number is 25")
elif x < 25:
print("The number is less than 25")
elif x > 25:
print("The number is greater than 25")
else:
print("Is this a number?"
Your program might not need multiple conditions you don't need elif or else statements if you don't need them. The key thing to remember is how you organize your conditional statement the way the computer will understand and run your code.
Take advantage of the comparison and logical operators.
Developers often use comparison and logical operators when they are creating conditional statements. As programs try to solve more real-life problems, there is a lot of logic that goes into making these programs possible. To make these programs, comparison and logical operators can be used to create the conditions that computers are deciding on.
x = 10
if x > 10 and x != 10:
print("Try again")
elif x < 10 or x != 10:
print("Keep trying!")
elif x == 10:
print("Correct")
else:
print("Game not working!")
This code sample uses comparison and logical operators as part of the code. Look closely at the if statement and the first elif statement. The if statement will only be true if both conditions are true, while the first elif statement only needs one of the conditions to be true to run the code.
Conclusion
That's a wrap on module three. Conditionals and loops are two important programming concepts that will help you as you continue to code in Python. Now you know how both concepts work in programming and how to create both in Python.
Module four is coming up next. The theme of module four is working with lists. This is the module where you'll learn how to create lists, indexing, and slicing.
Top comments (0)