Check in- Create a drawing to represent your holiday
Conditional statements are used to execute code based on a condition, such as a variable's value. Python can use a variety of conditional statements, such as if/else, elif, and nested ifs. Conditional statements can also be used with compound logical operators, such as 'and' and 'or'.
Python Conditional Statements
if-else statement: An if-else statement is a programming construct that tests a condition and then runs one block of code if the condition is true and runs another block of code if the condition is false. Nested if-else statement: A nested if-else statement is a programming construct that allows for multiple levels of branching. This means that you can use an if statement within an if statement, which is useful for testing multiple conditions. Logical Operators: Logical operators are used to execute boolean logic, which is the comparison of two values to determine if they are equal, not equal, greater than, less than, and so on.
Display a message if a game player has accumulated a set amount of experience points (variable name of xp). This program will only run if the experience points are greater than or equal to 90. This action will then print out the text “Skill Level: Expert”. If the number of experience points is less than 90, nothing will happen.
This action is possible with an else statement. (An else statement runs when the Boolean value is False.) To use the else statement, type “else” on a new line. Then, on the next line, indent 1 tab and add code that will run if the Boolean expression is false.
What if you want to run code when a statement is false?
One last conditional statement is an elif statement. Elif combines an else statement with another conditional statement to check for additional information. Elif can only be used after an “if” or another “elif” statement. Next to elif, add a Boolean expression and then a colon.
Brain break: Draw a monster made of spaghetti with meatball eyes and noodle arms and legs.
What are the three types of conditional statements in Python?
- if, else, elif
- for, while, do-while
- try, except, finally
What is the symbol used for 'equal to' in Python?
Which of these operators is used for 'not equal to' in Python?
Compound Conditional Statements
A compound conditional statement contains two Boolean expressions. Code runs that must meet the conditions as part of the conditional statement. In order to combine these Boolean expressions, we will use logical operators .
We use them to check if certain conditions are met before deciding the execution path your programs will follow.
Python has three logical operators. Can you name all 3?
AND : If both expressions are true, then the whole condition is true. If one or both of the conditions are false, then the whole condition is false. OR : If one or both conditions are true, then the condition is true. It doesn’t matter whether the other condition is true or false. NOT : Switches the expression to its opposite (from true to false and false to true).
Python Logical Operators
Let’s say we want to display a special message when a player gets their experience points above 120 and is playing the game on hard mode. The compound conditional statement starts with an “if”. Then, we add a Boolean expression followed by the logical operator “AND” and the other Boolean expression.