Chapter5 Flashcards
how to Ignore case when checking equality?
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 to check Multiple Condition?
Using and , OR
age_0 >= 21) and (age_1 >= 21
○ Using OR
ge_0 >= 21 or age_1 >= 21
Checking Whether a Value Is in a List
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 to check wheather value is not in a List?
○ 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 else if ?
○ If (cond) :
○ statement
○ Else:
Statement
how the if-elif-else Chain works?
age = 12 if age < 4: u price = 0 elif age < 18: v price = 5 else: w price = 1
Ommitting last else part in if-elif
○ 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.
Testing Multiple Conditions
○ 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.