Chapter5 Flashcards

1
Q

how to Ignore case when checking equality?

A

car = ‘Audi’
car.lower() == ‘audi’

• For example, a site might use a conditional test like this to ensure that every user has a truly unique username, not just a variation on the capitalization of another person’s username. When some- one submits a new username, that new username is converted to lowercase and compared to the lowercase versions of all existing usernames. During this check, a username like 'John' will be rejected if any variation of 'john' is already in use.  Ignoring case when checking equality
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to check Multiple Condition?

A

Using and , OR

age_0 >= 21) and (age_1 >= 21
○ Using OR
ge_0 >= 21 or age_1 >= 21

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

Checking Whether a Value Is in a List

A

For example, you might want to check whether a new username already exists in a list of current usernames before complet- ing someone’s registration on a website

○ We can use the keyword in to find out if a value is in a list
○ requested_toppings = [‘mushrooms’, ‘onions’, ‘pineapple’]
‘mushrooms’ in requested_toppings

§This returns true

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

how to check wheather value is not in a List?

A

○ The word not is used to check item is not in a list
○ if user not in banned_users:
print(user.title() + “, you can post a response if you wish.”)

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

How else if ?

A

○ If (cond) :
○ statement
○ Else:
Statement

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

how the if-elif-else Chain works?

A
age = 12 
		if age < 4: 
			u price = 0 
		elif age < 18: 
			v price = 5 else: 
                       w price = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ommitting last else part in if-elif

A

○ Python does not require an else block at the end of an if-elif chain. Some- times an else block is useful; sometimes it is clearer to use an additional elif statement that catches the speci c condition of interest:
○ If you have a speci c nal condition you are testing for, consider using a nal elif block and omit the else block. As a result, you’ll gain extra con dence 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
8
Q

Testing Multiple Conditions

A

○ If elseif is use to select if only one condition is true
○ You can use concatenated if –if to tast multiple conditions

In summary, if you want only one block of code to run, use an if-elif- else chain. If more than one block of code needs to run, use a series of independent if statements.

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