Lesson 3: Basic Operations and Expressions

Jun 18, 2023 | Uncategorized | 0 comments

Home 9 Uncategorized 9 Lesson 3: Basic Operations and Expressions

Python provides various operators for performing arithmetic, comparison, and logical operations on data. Understanding these operators is crucial for manipulating and processing data effectively.

Let’s explore some basic operations and expressions in Python:

# Arithmetic operations
x = 10
y = 3

print("Sum:", x + y)       # Addition
print("Difference:", x - y)  # Subtraction
print("Product:", x * y)    # Multiplication
print("Quotient:", x / y)   # Division
print("Remainder:", x % y)  # Modulo (Remainder)
print("Exponent:", x ** y)  # Exponentiation

# Comparison operations
a = 5
b = 7

print("Equal?", a == b)     # Equality
print("Not equal?", a != b)  # Inequality
print("Greater?", a > b)    # Greater than
print("Lesser?", a < b)     # Lesser than
print("Greater or equal?", a >= b)  # Greater than or equal to
print("Lesser or equal?", a <= b)   # Lesser than or equal to

# Logical operations
p = True
q = False

print("Logical AND:", p and q)  # Logical AND
print("Logical OR:", p or q)    # Logical OR
print("Logical NOT:", not p)    # Logical NOT

Explanation:

  • We perform arithmetic operations (addition, subtraction, multiplication, division, modulo, and exponentiation) on variables x and y.
  • We use comparison operations (equality, inequality, greater than, lesser than, greater than or equal to, and lesser than or equal to) on variables a and b.
  • We demonstrate logical operations (AND, OR, and NOT) on variables p and q.

Now it’s time for a practical task:

Task 3:

Write a Python program that calculates and prints the area of a rectangle. The length and width of the rectangle should be stored in variables. Use appropriate variable names and print a descriptive message with the calculated area.

Solution:

Spoiler warning

    length = 10
    width = 5
    area = length * width
    print("The area of the rectangle is", area)

Once you’ve completed the task, you can proceed to the next lesson.

Join The FFF Newsletter

Future Flow Forecast

Future Flow Forecast

Joinย the number one AI newsletter and learn how to be at the forefront of the latest discoveries. This is your "dotcom bubble" chance!

You have Successfully Subscribed!