Chapter 4-Selections(if statements) Flashcards
Remember that a program can decide which statements to execute based on a condition
potatoes and molasses
What are Boolean expressions?
Boolean expressions are selection statements which use conditions.
It is an expression that evaluates to a Boolean value “True” or “False”
What are comparison operators(relational operators)?
They are used to compare two values. So a relational/comparison operators are the less thans, greater thans, not equal tos, greater than or equal tos…
< > != >= <= == (this means equal to since a single = is an assignment operator)
A variable that holds a boolean value is known as a…
What number represents True?
What number represents False?
Boolean variable. A boolean variable can hold 1 of 2 values which are True or False
It might also be helpful to know that True and False are literals like a number such as 10.
Internally, Python uses 1 for True and 0 for False.
What does the bool function do?
It converts a numerical value into True or False. If the number is 0, it is False and if it is any other number, it will be True.
What are the outputs of these two functions?
print(bool(0))
print(bool(4)
print(bool(24))
bool(0)-prints false
bool(4)-prints true
bool(24)-prints true
Explain the randint (a, b) function.
the randint function can be used to generate a random integer between “a” and “b” -inclusively-
The radint function is within the random module, where random integer i, will be between a and b inclusively.
Explain the randrange(a, b) funciton
It combines the random function with the range function.
Recall that range(a,b) goees from a up to but not including b (so b-1)
Are randint(0,10) and randrange(0,11) the same?
It is the same
first=random.randint(0,1)
second=random.randrange(0,2)
remember when you want to use the random module, you have to import random first.
and when utilizing the randint or randrange(remember that these are functions that act upon objects), you need the function to act upon objects which are the random variable.
The random variable is a fixed term used in python(supposedly)
Although in the textbook on page 94, it says that randint is more intuitive
What does the random() function do?
random.random() will generate a random float, r such that
0=< r <1.0 in other words, random.random() returns a number between 0.0 and 1.0
A one way if-statement executes the statement if the condition is true.
So what are the types of selection statements available?
One way if statements, two way if-else statements, nested if statements, multi-way if-elif-else statements and conditional expressions
What is a one way if statement?
And what does it look like?
A one way executes an action if, and only if the condition is true.
The syntax of a one way if statement is :
if boolean expression:
statement
What is a flowchart?
A diagram that describes an algorithm, showing the steps as boxes of various kinds, and their order by connecting these boxes with arrows.
A diamond box indicates a boolean expression while a rectangle box illustrates statements.
Two-way if-else statements. What is it?
The two-way if-else statement decides which statements to execute based on whether the condition is true or false.
Recall that a one way if statement only goes into action if the statement is true. If the statement is false, nothing happens. But in the two-way- if-else statement, if the statement is false, then there may be an alternate route.
This is what a two way if else statement generally looks like:
True False
-Statements for true case State. for false
-—————–> O <———————-/
|
|/
How to form a nest if statement?
You put an if statement inside another if statement, this is a nested if statement. However, nested if statements can only be formed in this way.
The inner if statement is said to be nested inside the outer if-statement. There is no limit to nesting