Note that the single is used for assignment, while the double is used to = == compare two expressions to see if they are equal. Other comparison operators that can be used to create a Boolean expression include (for and (for ≥) ≠). >= != You can make compound conditionals by connecting Boolean expressions with and In the following examples, remember that is still 3. and, or, not. a In []: a+1 >= 2 and a**2 != 5 # compound conditional Out[]: True The only two Boolean values are and True False. Predict, try, discuss, and explain the output: In []: # 3a. Prediction: In []: a**2 >= 9 and not a>3 Out[]: In []: # If prediction was wrong, discuss and explain In []: # 3b. Prediction: In []: a+2 == 5 or a-1 != 3 Out[]: In []: # If prediction was wrong, discuss and explain 3a is True; 3b is True One situation a programmer would use a compound conditional is when deciding whether a mouse click is within a certain region on the screen. For example, you might want your program to respond if a user clicks a rectangular shape, like a button or tab, as shown in the figure on the left. Creating a program that uses mouse input is part of Lesson 1.5. For now, we’ll just assign x and y arbitrary values—let’s say using the assignment (65,40), operator These coordinates are shown by the red dot in the figure above on =. the right. Unlike most languages, allows in a single line of Python multiple assignment code, as shown here: In []: x, y = (65, 40)
Note that the single = is used for assignment, while the double == is used to compare two expressions to see if they are equal. Other comparison operators that can be used to create a Boolean expression include >= (for ≥) and != (for ≠). You can make compound conditionals by connecting Boolean expressions with and, or, and not. In the following examples, remember that a is still 3. In []: a+1 >= 2 and a**2 != 5 # compound conditional Out[]: True The only two Boolean values are True and False. Predict, try, discuss, and explain the output: In []: # 3a. Prediction: In []: a**2 >= 9 and not a>3 Out[]: In []: # If prediction was wrong, discuss and explain In []: # 3b. Prediction: In []: a+2 == 5 or a-1 != 3 Out[]: In []: # If prediction was wrong, discuss and explain 3a is True; 3b is True One situation a programmer would use a compound conditional is when deciding whether a mouse click is within a certain region on the screen. For example, you might want your program to respond if a user clicks a rectangular shape, like a button or tab, as shown in the figure on the left. Creating a program that uses mouse input is part of Lesson 1.5. For now, we’ll just assign x and y arbitrary values—let’s say (65,40), using the assignment operator =. These coordinates are shown by the red dot in the figure above on the right. Unlike most languages, Python allows multiple assignment in a single line of code, as shown here: In []: x, y = (65, 40)