Python Crash Course - Chapter 5: If Statements

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

How would you check for an inequality?

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 programing languages. dog = 'barron' print(dog != 'barron') >>> False print(dog != 'maggie') >>> True

What is a Boolean expression?

A Boolean expression is just another name for a conditional test. They check the truth of an expression.

What is the difference between a single and double equal sign in an if statement?

A single equal sign is really a statement; you would read as, "Set the value of LEFT equal to RIGHT side of the equal sign. On the other hand, a double equal sign looks for a True or False result: "IS the value of LEFT equal to RIGHT?"

What syntax should you use if you need to test more than two possible situations?

If you need to test more than two possible situations, use Python's if-elif-else syntax. It runs each conditional test in order until one passes. When a test passes, the code following that test is executed and Python skips the rest of the tests. age = 12 if age < 4: <<tab>> price = 0 elif age < 18: <<tab>> price = 5 elif age < 65: <<tab>> price = 10 else: <<tab>> price = 5 print("\nYour admission cost is $" + str(price) + ".")

What is checking for equality?

Most conditional tests compare the current value of a variable to a specific value of interest.

What does an if statement do?

The if statements is used to check a condition and if the conditions is True or False. This is refered to as a conditional test. If the 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. banned_users = ['andrew','carolina','david'] user = 'marie' if user not in banned_users: print(user.title() + ", you can post.")

How does indentation work in if statements?

Indentation play the same role in if statements as it does in for loops. All indented lines after an if statement will be executed if the test passes, and the entire blog of indented lines will be ignored if the test does not pass.

Is the else block required at the end of an if-elif chain?

No. 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. if age < 4: <<tab>> price = 0 elif age < 18: <<tab>> price = 5 elif age < 65: <<tab>> price = 10 elif age >= 65: <<tab>> price = 5 print("\nYour admission cost is $" + str(price) + ".")

What is a limitation of the if-elif-else chain?

The if-elif-else chain is powerful, but it's only appropriate to use when you just need one test to pass. If more than one block of code needs to run, use a series of indpendent if statements.

What does the keyword 'or' do?

The keyword 'or' allows you to check multiple conditions, but it passes when either or both of the individual tests pass. An or expression fails only when both individual tests fail. age_0 = 22 age_1 = 18 print(age_0 >= 21) or (age_1 >=21) >>> True print(age_0 <= 21) or (age_1 >=21) >>> False print(age_0 >= 21) and (age_1 >=21) >>> False

What is the keyword 'in' used for?

To find out whether a particular value is already in a list, use the keyword 'in'. requested_toppings = ['mushrooms', 'onions', 'pineapple'] print('mushrooms' in requested_toppings)

What should you use if you want to take one action when a conditional test passes and a different action in all other cases?

Use Python's if-else syntax. An if-else block is similar to a simple if statement, but the else statement allows you to define an action or set o factions that are executed when the conditional test fails. (<<tab>> is not part of the code; it indicates where a tab is that Quizlets strips out.) age = 17 if age >= 18: <<tab>> print("You are hold enough to vote!") else: <<tab>> print("Sorry, you are too young to vote.")

What is the keyword 'not' used for?

Use the not keyword to check if a value does not appear in a list. requested_toppings = ['mushrooms', 'onions', 'pineapple'] print('olives' not in requested_toppings) >>> True

How would you raise or lower case to ensure an equality or inequality test passes correctly?

User the lower() or title() string methods. dogs = ['barron','maggie','daisy'] print('Maggie'.lower() in dogs) <> OR print(dogs[1].title() == 'Maggie') >>> True

What keyword would you use to combine two conditional tests?

To check whether two conditions are both True simultaneously, use the keyword 'and' to combine two conditionsal tests; if each test passes, the overall expression evaluates to True. Use partheses around the individual tests to improve the readabilty of your code. dogs = ['barron','maggie','daisy'] print('barron' in dogs) and ('daisy' in dogs) >>> True dogs = ['barron','maggie','daisy'] print('george' in dogs) and ('daisy' in dogs) >>> False


Ensembles d'études connexes

Art Appreciation InQuizitive 4.1 - Art and Community

View Set

Unit 5: Topic 2: Economic Development

View Set

Elbow ROM,Wrist ROM,extensor, carpi radialis longus & brevis, carpi ulnaris, digitorum, anconeus, Snuff box(EPL EPB),De Quervain's, lateral epicondylitis

View Set

Chapter 18 : Protein structure and function

View Set

Chapter 15- Mistakes, Fraud, and Voluntary Consent

View Set