Exercise
Python Operators
These exercises are designed to help you practice working with various operators in Python. Follow each step carefully and try to predict the output before running the code.
File Organization
We'll add a new directory called operators to your existing file structure. The updated structure will look like this:
csf101-python_exercises/
│
├── basics/
│ ├── numbers.py
│ ├── strings.py
│ └── booleans.py
│
├── data_structures/
│ ├── lists.py
│ └── dictionaries.py
│
└── operators/
├── arithmetic.py
├── assignment.py
├── comparison.py
└── logical.py
Create a new directory called operators inside your csf101-python_exercises directory.
Exercise 1: Arithmetic Operators
File: operators/arithmetic.py
Create a new file called arithmetic.py in the operators directory and complete the following exercises in this file.
-
Create two variables
aandbwith values 15 and 4 respectively.a, b = 15, 4 print(f"a = {a}, b = {b}")Expected output:
a = 15, b = 4 -
Perform addition, subtraction, multiplication, and division with these variables.
print(f"Addition: {a + b}") print(f"Subtraction: {a - b}") print(f"Multiplication: {a * b}") print(f"Division: {a / b}")Expected output:
Addition: 19 Subtraction: 11 Multiplication: 60 Division: 3.75 -
Use the modulus operator to find the remainder when
ais divided byb.print(f"Modulus: {a % b}")Expected output:
Modulus: 3 -
Use the exponentiation operator to calculate
ato the power ofb.print(f"Exponentiation: {a ** b}")Expected output:
Exponentiation: 50625 -
Use floor division to divide
abyb.print(f"Floor Division: {a // b}")Expected output:
Floor Division: 3
Exercise 2: Assignment Operators
File: operators/assignment.py
Create a new file called assignment.py in the operators directory and complete the following exercises in this file.
-
Create a variable
xwith an initial value of 10.x = 10 print(f"Initial x: {x}")Expected output:
Initial x: 10 -
Use the
+=operator to add 5 tox.x += 5 print(f"After x += 5: {x}")Expected output:
After x += 5: 15 -
Use the
-=operator to subtract 3 fromx.x -= 3 print(f"After x -= 3: {x}")Expected output:
After x -= 3: 12 -
Use the
*=operator to multiplyxby 2.x *= 2 print(f"After x *= 2: {x}")Expected output:
After x *= 2: 24 -
Use the
/=operator to dividexby 4.x /= 4 print(f"After x /= 4: {x}")Expected output:
After x /= 4: 6.0
Exercise 3: Comparison Operators
File: operators/comparison.py
Create a new file called comparison.py in the operators directory and complete the following exercises in this file.
-
Create two variables
aandbwith values 10 and 5 respectively.a, b = 10, 5 print(f"a = {a}, b = {b}")Expected output:
a = 10, b = 5 -
Use comparison operators to compare
aandb.print(f"a == b: {a == b}") print(f"a != b: {a != b}") print(f"a > b: {a > b}") print(f"a < b: {a < b}") print(f"a >= b: {a >= b}") print(f"a <= b: {a <= b}")Expected output:
a == b: False a != b: True a > b: True a < b: False a >= b: True a <= b: False -
Create a variable
cwith value 10 and compare it witha.c = 10 print(f"a == c: {a == c}")Expected output:
a == c: True
Exercise 4: Logical Operators
File: operators/logical.py
Create a new file called logical.py in the operators directory and complete the following exercises in this file.
-
Create two boolean variables
xandy.x = True y = False print(f"x = {x}, y = {y}")Expected output:
x = True, y = False -
Use the
andoperator withxandy.print(f"x and y: {x and y}")Expected output:
x and y: False -
Use the
oroperator withxandy.print(f"x or y: {x or y}")Expected output:
x or y: True -
Use the
notoperator withxandy.print(f"not x: {not x}") print(f"not y: {not y}")Expected output:
not x: False not y: True
Congratulations!
Remember to run each file separately to see the output of your exercises. You can do this by navigating to the appropriate directory in your terminal and running python filename.py (e.g., python arithmetic.py).