Course Content
Introduction to Python (copy)
Introduction to Python (copy)
Recap
Congratulations on completing this exciting section of our Python course!
You've developed a strong understanding of controlling the logic flow within your programs using various Python constructs.
Let's review the key concepts you've mastered:
Boolean Data Types and Comparisons
You've learned to use comparison operators to evaluate conditions in Python. These operators help you check relationships between values and include the following:
- Equal to:
==
; - Not equal to:
!=
; - Greater than:
>
; - Less than:
<
; - Greater than or equal to:
>=
; - Less than or equal to:
<=
.
item_price = 20 discount_price = 15 print(item_price > discount_price) # `True` print(item_price == discount_price) # `False`
Combining Conditions
You've mastered the art of combining multiple conditions using logical operators to make more complex decisions:
and
: Evaluates toTrue
if both conditions areTrue
;or
: Evaluates toTrue
if at least one condition isTrue
;not
: Reverses the logical state of its operand.
stock_level = 50 on_sale = True print(stock_level > 30 and on_sale) # `True`
Membership Operators and Type Comparisons
We've explored how to use membership operators to check for the presence or absence of an element within a sequence and how to use the type()
function to confirm the data type of a variable:
- Membership operators like
in
andnot in
. - Comparing types by using
type()
.
products = "milk, eggs, cheese" print('milk' in products) # True item_type = 20.0 print(type(item_type) == float) # True
Conditional Expressions
You've learned how to use if
, else
, and elif
statements to execute different code blocks based on various conditions. This foundational skill is crucial for writing dynamic and responsive Python programs:
temperature = 75 if temperature > 80: print("It's too hot!") elif temperature < 60: print("It's too cold!") else: print("It's just right!")
1. Which operator is used to check if two values are NOT equal in Python?
2. What will the following print statement return?
3. How do you check if the substring "apple"
is in the string assigned to fruits
?
4. Which line of code correctly checks the data type of item_price
to see if it is a float
?
5. What output will the following Python code produce?
Thanks for your feedback!