Python Exercise - Operators and Data Structures
Introduction
This unit covers Python’s basic operators, focusing on arithmetic, comparison, and logical operators. You will practice using these operators through multiple tasks to solidify your understanding.
Task 1: Arithmetic and Comparison Operators
Description:
Practice using arithmetic and comparison operators in Python.
Instructions:
- Define two variables:
a = 10
andb = 5
. - Perform the following arithmetic operations and print the results:
- Modulus: Calculate the remainder when
a
is divided byb
. - Exponentiation: Raise
a
to the power ofb
.
- Modulus: Calculate the remainder when
- Perform the following comparison operations and print whether each statement is true or false:
- Check if
a
is equal tob
. - Check if
a
is not equal tob
. - Check if
a
is greater thanb
. - Check if
a
is less than or equal tob
.
- Check if
Task 2: Logical and Combined Operations
Description:
Use comparison operators in combination with logical operators (and
, or
, not
) in Python.
Instructions:
- Define the same two variables:
a = 10
andb = 5
. - Perform the following combined operations:
- Check if
a
is greater than 8 andb
is less than 10. - Check if
a
is equal to 10 orb
is equal to 10. - Use the
not
operator to invert a comparison result.
- Check if
- Print the result of each logical operation.