Boolean Expressions in Python PDF

Summary

This document provides a tutorial on boolean expressions in Python. It explains what boolean expressions are, how to assign them to variables, and use comparisons. It also includes examples to illustrate the concepts.

Full Transcript

# Boolean Expressions in Python ## Boolean Expressions In programming, it's very common to want to know if something is true or false. For example, in a game we want to know if the game is over, or if it is still going, or if a user got a question right or wrong. ## Boolean Variables Python auto...

# Boolean Expressions in Python ## Boolean Expressions In programming, it's very common to want to know if something is true or false. For example, in a game we want to know if the game is over, or if it is still going, or if a user got a question right or wrong. ## Boolean Variables Python automatically detects a Boolean-type variable when the value is set to "True" or "False," (a Boolean expression). When you're setting a variable to "True" or "False," make sure you capitalize the T in True and the F in False. For example: my_value = True. Boolean variables are like light switches. There are only two options: on or off (True or False). ## Assigning Boolean Expressions When you assign a Boolean expression to a variable, Python will set the value to "True" or "False" depending on whether the Boolean expression is true or false. For example: - height = 58 - meet_limit = height > 50 - print(meet_limit) This will print: True The expression is True because the given height is 58, and 58 > 50, which means that meet_limit is true. This example shows assigning the variable of test1 to the Boolean expression 2 is equal to 4, which is false. - test1 = 2 == 4 - print(test1) This will print: False **Number variables store the answer to a calculation and not the mathematical expression. Boolean variables are similar because they store "True" or "False" as the answer to a comparison expression instead of the comparison itself** ## Comparison Operators Comparison operators evaluate information to be true or false. They compare two values to each other. | SYMBOL | MEANING | SYMBOL | MEANING | | :------ | :---------------- | :------ | :--------------------------------- | | == | is equal to | > | is greater than | | != | is not equal to | <= | is less than or equal to | | < | is less than | >= | is greater than or equal to | ### Examples of expressions that evaluate to True | EXPRESSION | MEANING | VALUE | | :--------- | :----------- | :---- | | 2 == 2 | 2 is equal to 2 | True | | 2 != 3 | 2 is not equal to 3 | True | | 2 < 3 | 2 is less than 3 | True | | 4 > 3 | 4 is greater than 3 | True | | 2 <= 2 | 2 is less than or equal to 2 | True | | 5 >= 3 | 5 is greater than or equal to 3 | True | ### Examples of expressions that evaluate to False | EXPRESSION | MEANING | VALUE | | :--------- | :--------------------------------- | :---- | | 2 == 5 | 2 is equal to 5 | False | | 2 != 2 | 2 is not equal to 2 | False | | 3 < 3 | 3 is less than 3 | False | | 2 > 3 | 2 is greater than 3 | False | | 5 <= 3 | 5 is less than or equal to 3 | False | | 2 >= 3 | 2 is greater than or equal to 3 | False | ## Conditional Statements Conditional statements in Python always start with the key word "if," followed by the Boolean expression and a colon. The code that runs if the condition is true is added below the first line and indented one tab (or four spaces). **Boolean expressions use ==, !=, <, >, <=, and >=.** ### For Example You can write a program that displays a message if a game player has reached the expert level. The code that prints "Skill Level: Expert" will run only if the xp (experience points) is greater than or equal to 90. If the xp is less than 90, nothing happens. - xp = 120 **Start with "if". Add the conditional statement and a colon after it** - if xp >= 90: - print("Skill Level: Expert") **Indent here to show this print() function is the code to run if xp >= 90.** Skill Level: Expert will print because 120 > 90. ## Else Statements An ELSE statement is a statement that runs when the Boolean value is False. To use the else statement, type "else" on a new line. Then, on a new line, indent 1 tab (4 spaces) and add code that will run if the Boolean expression is false. For example, you could say: If xp is greater than or equal to 90, then display "Skill Level: Expert", but if the user's xp is less than 90, display "Skill Level: Novice." - xp = 50 - if xp >= 90: - print("Skill Level: Expert") - else: - print("Skill Level: Novice") **tells what code should run when the Boolean expression is False** The example will display "Skill Level: Novice" because xp is 50, which is not greater than or equal to 90. ## Elif ELIF is used to combine an else statement with another conditional statement to check for additional information. Elif is used only after an "if" or another "elif" statement. Next to elif, add a Boolean expression and then a colon. **THE CELEBRITY COUPLE NAME FOR IF AND ELSE WOULD BE ELIFI** For example, you can add another experience level to the game by adding an elif statement to the code. - xp = 50 - if xp >= 90: - print("Skill Level: Expert") - elif xp >= 50: - print("Skill Level: Intermediate") - else: - print("Skill Level: Novice") **A flowchart of the program shows the different branches for each condition:** **Start** - **xp >= 90?** - True: **Skill Level: Expert** - False: - **xp >= 50?** - True: **Skill Level: Intermediate** - False: **Skill Level: Novice** **WHAT? YOU'RE STILL A NOVICE??** ## Compound Conditional Statements A COMPOUND CONDITIONAL STATEMENT is a conditional statement that contains two Boolean expressions. For example, you could make a game where the player gets bonus points if their score is greater than 50 AND their difficulty setting is set to "hard." ## Logical Operators LOGICAL OPERATORS combine multiple Boolean expressions or values that evaluate to one Boolean value. Python uses three logical operators: - **AND:** If both expressions are True, then the whole condition is True. If one or both of the expressions are False, then the whole condition is False. - **OR:** If one or both expressions 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). **When we talk or write about AND, OR, and NOT, we use all caps; however, within Python programs, the words are written in all lowercase.** ### For Example We could display a special message if a player wins on the most difficult setting and their xp is greater than 90. The compound conditional statement starts with "if", and then we can add a Boolean expression followed by the logical operator "AND" and the other Boolean expression: - xp = 150 - difficulty = "Hard" **First Boolean expression** - if xp > 90 and difficulty == "Hard": - print("You surpass all expectations!") **You surpass all expectations!** **Second Boolean expression** ### Examples - 3 < 4 and 6 == 6 - True AND True evaluates to True - 5 == 5 or 6 < 3 - True OR False evaluates to True - 4 != 4 and 6 > 2 - False AND True evaluates to False - 4 > 12 or 7 != 7 - False OR False evaluates to False - not(6 < 13) - True evaluates to False ## Nested Conditional Statements NESTED CONDITIONAL STATEMENTS can be nested within loops. **Loop** **NESTED CONDITIONALS**

Use Quizgecko on...
Browser
Browser