A boolean object has one of two values: True
or False
Comparison operators the result in boolean objects :
x == y # x is equal to y (remember, " = " is an assignment operator)
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= Y # x is less than or equal to y
Some scratch work:
import random
print(4 < 5)
print(4 > 5)
print(4 == 5)
result = 4 == 5
print(result)
# Example of binary selection
for i in range(5):
x = random.randrange(10)
y = random.randrange(10)
print("Is", x, "greater than", y, "?")
if x > y:
print('True')
else:
print('False')
There are three logical operators and
, or
, and not
.
Suppose e1 and e2 are two expressions that have a boolean value. For example, suppose e1 = “It is raining,” and e2 = “I am hungry.” Then,
The espression e1 and e2
is True
if and only if e1 is True
and e2 is True.
The expression e1 or e2
is True
if either of e1 and e2 is True.
That is, e1 or e2
is False
if and only if e1 is False
and e2 is False
.
The not
operator causes negation. for example not a > b
is True
when a > b
is False
.
Arithmetic operators have highest precedence, then relational operations, and then logical operators.
2*x + 1 >= 7 or y - 3 < 5
order … suppose \(x\) = 3 and \(y\) = 8
Example
import random
def is_goldie_locks(x_in):
if x_in > 4 and x_in <= 6:
print(x_in, ' is a goldie locks number')
else:
print('Sorry, ', x_in, ' is not a goldie locks number')
def main():
for trial in range(5):
is_goldie_locks(random.randrange(10))
main()
Executing statements based on the value of a boolean expression or value
if BOOLEAN EXPRESSION:
STATEMENTS_1 # execute all of these statements if the boolean expression is True
else:
STATEMENTS_2 # execute all of these statements if the boolean expression is False
Activity 1: Write a program that includes a non-fruitful function entitled find_interval(x)
that determines if the input integer \(x\), randomonly selected from the interval [1,100] is greater than or equal to fifty. The function should include a binary selection compound statement. If \(x \ge 50\), the function prints “Greater than or equal to 50.” If \(x < 50\), the function prints “Less than 50.” Use a program structure similar to that of the Goldie Locks example above. That is, your program should have all import
statements first, then code defining the program functions including the main()
function last. Remember, the last line of your code should be the statement main()
that invokes the main()
function.
In this case, there is no else
clause. A block of statements are executed if a boolean expression is True
.
if BOOLEAN EXPRESSION:
STATEMENTS_1 # execute all of these statements if the boolean expression is True
STATEMENTS_2 # all following, un-indented statements are excuted regardless
Activity 2: Write a program that includes a function called sum_of_odds(n_trials)
that returns the sum all of the odd numbers randomly selected from the interval [1, 10]. The parameter n_trials
is the number of times the random.randrange(1,11)
function is invoked.
Nesting if
- else
statements to determine conditions.
if BOOLEAN EXPRESSION_1:
STATEMENTS_1 # execute all of these statements if the boolean expression 1 is True
else:
if BOOLEAN EXPRESSION_2:
STATEMENTS_2 # execute all of these statements if the boolean expression 2 is True
else:
STATEMENTS_3 # execute all of these statements if the boolean expression 2 is False
Activity 3: Modify the find_interval(x)
function to determine if a randomonly selected integer from the interval [1,100] is greater than or equal to 66, greater than or equal to 33 and less than 66, or less than 33. Use a nested if
- else
structure in the function. Have the program print the result of the selection for each of 10 trials.
The second if
- else
conditional in the nested structure shown above can be accomplished using the elif
selector.
if BOOLEAN EXPRESSION_1:
STATEMENTS_1 # execute all of these statements if the boolean expression 1 is True
elif BOOLEAN EXPRESSION_2:
STATEMENTS_2 # execute all of these statements if the boolean expression 2 is True
else:
STATEMENTS_3 # execute all of these statements if the boolean expression 2 is False
Any numbeer of elif
statements may be used, but only one else
statement may be used. It is optional, but if used, it must be at the end - the last branch of the statement.
Activity 4: Modify the find_interval(x)
function to determine if a randomonly selected integer from the interval [1,100] is greater than or equal to 80, greater than or equal to 60 and less than 80, greater than or equal to 40 and less than 60, greater than or equal to 20 and less than 40, or less than 20. Use a proper if
- elif
- else
chained conditionals compound statement. Have the program print the result of the selection for each of 10 trials.
A Boolean function returns either True
or False
They can be used to “hide” or shorten some of the coding in the selection process.
Activity 5: Modify the find_interval(x)
so that it is now a Boolean function that returns True
if a the number x is a Goldie Locks number. That is, if \(45 \le x \le 55\). Pattern your code after the isDivisible
example in section 7.8 of the text.
Activity 6: Modify the find_interval(x)
function so that it returns the month name and day number corresponding the the Julian day number. Julian day numbers run from 1 (January 1) to 365 ( December 31) for non-leap years and 1 - 366 for leap years. Name the function julian_2_mn_day(julian)
where the parameter julian
is an integer in the interval 1 to 365. That, write a function for non-leap years. The function returns the month name and day number of the month. For example, if julian_2_mn_day(61)
returns March 2
, where March
is a str
object and 2
is an int
object.
Activity 7: Write a function named find_extremes(n_list, length)
that takes a list of integers, and the length of the list, as parameters and returns the maximum value and minimum value in the list including the value,s location in the list. For example, if n_list = [3, -2, 7, 10, -3, 5] is input with its length = 6, the function returns 10, 3, -3, 4. Once you have your function working, use unit testing to test that your find_extremes()
function is correct for various inputs.
Assignment 11
Unittesting. Please, no input statements.
Use if - else
(binary selection) rather than unary selection twice.
No quotes around True
or False
Just the function. No need for a main()
function, etc.
Assignment 13
def isLeap(year):
return year % 4 == 0 and year % 100 != 0 or year % 100 == 0 and year % 400 == 0
def isLeap(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0