chapter_five Flashcards

1
Q

How do I execute code based on certain conditions

A

“if” statements.

Python’s if statement allow you to examine the current state of a program and respond appropriately to that state.

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

What is at the heart of every if statement?

A

At the heart of every if statement is an expression that can be evaluated as “True” or “False” and is called a conditionale test.
Python uses the values “True” and “False” to decide whether the code in an if statement should be executed.
If a conditional test evaluates to “True”, Python executes the code following the if statement.
If the test evaluates to “False”, Python ignores the code following the if statement.

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

How do I check for Equality in an if statement?

A

Checking for Equality
Most conditional tests compare the current value of a variable to a specific value of interest.
The simplest conditional test checks whether the value of a variable is equal to the value of interest

This equality operator returns “True” if the value on the left and right side of the operator match, and “False” if they don’t match.

number = 'one'
number == 'one'
>>> True
number == 'two'
>>> False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What could be a problem with the equality operator while using strings?
How do we negate that problem?

A

Testing for equality is case sensitive in Python.

If case matters, this behavior is advantageous. But if case doesn’t matter and instead you just want to test the value of a variable, you can convert the value’s value to lowercase before doing the comperison.

number = ‘One’
number.lower( ) == ‘one’

The lower function doesn’t change the value that was originally stored in the list, so you can do comparison without affecting the original variable.

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

What do you do if you want to check for two values that are not equal in an if statement?

A

When you want to determine whether two values are not equal, you can combine an exclamation point and an equal sign ( != ),
The exclamation point represents “not”, as it does in many programming languages.

If these two values do not match, Python returns “True” and executes the code following the if statement.
If the two values match, Python returns “False” and does not run the code following the if statement.

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

What are all the mathematical comparisons for numbers that can be used in an if statement?

A
==
!=
<
<=
>
>=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you check if multiple conditions are True at the same time?
(if statement)

A

and

To check whether two conditions are both “True” simultaneously, use the keyword “and” to combine the two conditional tests;
if each test passes, the overall expression evalueated to “True”.
If either test fails or if both tests fail, the expression evaluates to “False”.

To improve readability, you can use parentheses around individual tests, but they are not required.

(age_0 >= 21) and (age_1 >= 21)

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

How do you check if atleast one or all conditions are True?

if statement

A

or

The keyword “or” allows you to check multiple conditions as well, but it passes when either or both of the individual tests pass.
An “or” expression fails only when both individual tests fail.

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

How do you check whether a value is not in a list?

A

You can use the keyword “not” in this situation.

prime_numbers = [ 'two', 'three', 'five', 'seven' ]
number = 'six'

if number not in prime_numbers:
print( f”{ number.title( ) } is not a prime number.” )

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

Which value type are the following values?

True, False

A

A Boolean expression is just another name for a conditional test.
A Boolean value is either “True” or “False”, just like the value of a certain conditional expression after it has been evaluated.

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

For what do you use Boolean expressions?

A

Boolean values are often used to keep track of certain conditions, such as whether a game is running or whether a user can edit certain content on a website.

game_active = False
can_edit = True

Boolean values provide an efficient way to track the state of a program or a particular condition that is important in your program.

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

How do you check whether a value is in a list?

A

To find whether a paricular value is already in a list, use the keyword “in”.

prime_numbers = [ ‘two’, ‘three’, ‘five’, ‘seven’ ]
if ‘three’ in prime_numbers:
print( “is a prime number” )

This technique is quite powerful because you can create a list of essential values, and then easily check whether the value you’re testing matches one of the values in the list.

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

What code do you use, when you want to do one action if the if statement passes and another one in all other cases?

A

An if-else block is similar to a simple if statement, but the else statement allows you to define an action or set of actions that are executed when the conditional test fails.

The if-else structure works well in situations in which you want Python to always execute one of two possible actions.

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

What code do you need to use to if you want to test for more that two possible situations in an if statement?

A

Python executes only one block in an if-elif-else chain. It runs each conditional test in order until one passes. When a test passes, the code following that test is executed an Python skips the rest of the test.

As soon as Python finds one test that passes, it skips the rest of the tests. This behaviour is beneficial, because it’s efficient and allows you to test for one specific condition.

You can use as many elif blocks in your code as you like.

Python does not require an else block at the end of an if-elif chain.
Sometimes an else block is useful; sometimes it is clearer to use an additional elif statement that catches the specific condition of interest.

The else block is a catchall statement. It matches any condition that wasn’t matched by a specific if or elif test, and that can sometimes include invalid or even malicious data.
If you have a specific final condition you are testing for, consider using a final elif block and omit the else block.
As a result, you’ll gain extra confidence that your code will run only under the correct conditions.

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

What code do you use if you want to have more than one output if the if statement is true?

A

Sometimes it’s important to check all of the conditions of interest. In this case, you should use a series of simple if statements with no elif or else blocks.

This technique makes sense when more than one condition could be true, and you want to act on every condition that is true.

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

list with for loop that contains an if-else statement that gives out a text if a specific item is contained in a list.

A
17
Q

checking if a list is empty

A

When the name of a list is used in an if statement, Python returns True if the list contains at least one item; an empty list evaluates to False.

18
Q

using two lists. one to list existing items, the other list comes from a customer.
If the item doesn’t exist in the existing items list, give out a comment about it.

A