Monday, August 25, 2025

๐ŸBASIC OPERATORS

Basic Operators in Python

Learn how to perform calculations and comparisons using Python operators.

Introduction

Operators are special symbols in Python that perform operations on variables and values. Just like a calculator, Python can add, subtract, multiply, and divide numbers. It can also compare values and combine conditions.

1. Arithmetic Operators

These are used to perform basic mathematical operations:


a = 10
b = 3

print(a + b)   # Addition → 13
print(a - b)   # Subtraction → 7
print(a * b)   # Multiplication → 30
print(a / b)   # Division → 3.333...
print(a // b)  # Floor Division → 3
print(a % b)   # Modulus (Remainder) → 1
print(a ** b)  # Exponentiation → 1000

2. Comparison Operators

These are used to compare two values. They return either True or False.


x = 5
y = 10

print(x == y)  # Equal → False
print(x != y)  # Not equal → True
print(x > y)   # Greater than → False
print(x < y)   # Less than → True
print(x >= 5)  # Greater than or equal → True
print(y <= 10) # Less than or equal → True

3. Logical Operators

Used to combine conditional statements:


x = 7

print(x > 5 and x < 10)  # True (both conditions are True)
print(x > 5 or x < 5)    # True (at least one condition is True)
print(not(x > 5))        # False (because x > 5 is True, and not True → False)

4. Assignment Operators

These are used to assign values to variables in different ways:


a = 10
a += 5   # Same as a = a + 5 → 15
a -= 3   # Same as a = a - 3 → 12
a *= 2   # Same as a = a * 2 → 24
a /= 4   # Same as a = a / 4 → 6.0

๐Ÿ’ก Try It Yourself

  1. Take two numbers from the user and print their sum, difference, product, and quotient.
  2. Ask the user for their age. If they are 18 or older, print "You can vote", else print "You cannot vote".
  3. Check if a number is divisible by both 2 and 3 using logical operators.

What’s Next?

Now that you know operators, the next step is learning about Conditional Statements — the building blocks of decision-making in programs.

➡️ Next: Conditional Statements in Python

⬅️ Back to Main Page

๐Ÿ–ฅ️ Practice in Browser

No comments:

Post a Comment

๐ŸWhat is scikitlearn??