Chapter 4-Selections(if statements) Flashcards

1
Q

Remember that a program can decide which statements to execute based on a condition

A

potatoes and molasses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Boolean expressions?

A

Boolean expressions are selection statements which use conditions.

It is an expression that evaluates to a Boolean value “True” or “False”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are comparison operators(relational operators)?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A variable that holds a boolean value is known as a…

What number represents True?
What number represents False?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the bool function do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the outputs of these two functions?

print(bool(0))

print(bool(4)

print(bool(24))

A

bool(0)-prints false

bool(4)-prints true

bool(24)-prints true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the randint (a, b) function.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the randrange(a, b) funciton

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Are randint(0,10)
and randrange(0,11) the same?
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the random() function do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A one way if-statement executes the statement if the condition is true.

So what are the types of selection statements available?

A
One way if statements,
 two way if-else statements, 
nested if statements, 
multi-way if-elif-else statements 
and conditional expressions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a one way if statement?

And what does it look like?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a flowchart?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Two-way if-else statements. What is it?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to form a nest if statement?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the multi-way if statement?

A

if-elif-else.

elif is short for else if