# Boolean Expressions
# Relational or Comparison ops.
(Assume variable a = 10, b = 20 )
Operator | Description | Example |
---|---|---|
and Logical AND | If both the operands are true then condition becomes true. | ( a and b ) is true. |
or Logical OR | If any of the two operands are non-zero then condition becomes true. | ( a or b ) is true. |
not Logical NOT | Used to reverse the logical state of its operand. | Not(a and b) is false. |
# Conditional or Logical ops.
(Assume variable a = 10, b = 20 )
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | ( a == b ) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | ( a != b ) is true. |
<> | If values of two operands are not equal, then condition becomes true. | ( a <> b ) is true. This is similar to != operator. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | ( a > b ) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | ( a < b ) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | ( a >= b ) is not true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | ( a <= b ) is true. |
*Know short – circuit evaluations! And vs. Or
- Short circuit evaluation: deciding the value of a compound Boolean expression after evaluating only one sub expression
# Reviews
What is the result of the following Boolean expression, if x equals 5, y equals 3, and z equals 8?
not (x < y or z > x) and y < z
What are the values that the variable num contains through the iterations of the following for loop?
for num in range(2, 9, 2)
The first input operation is called the , and its purpose is to get the first input value that will be tested by the validation loop.
if
x = 1
,y = 2
what would print given the following statement, given C is a true value? result is 1print(x if C else y)
To quit the Python shell, you can either select the window’s close box or press the key combination.
In Python, you can write a print statement that includes two or more expressions separated by .
The result of evaluating 45 % 0 is .
A(n) function is a function with the same name as the data type to which it converts.
Mix and Match
Loop Types:
- nested loops
You can use one or more loop inside any another while, for or do..while loop.
- for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
- while loop
Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
Mix and Match
Statement choices:
- Pass statement
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
- Break statement
Terminates the loop statement and transfers execution to the statement immediately following the loop.
- Continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Name 4 built-in Python conversion type functions
str() float() bool() int()
Name at least 5 built-in functions
input() print() round() ord() chr()
Name at least 6 Python keywords
if else elif while except finally
What is the purpose of a sentinal value
special value that marks the end of a sequence of items (terminates loop!)