3pythonoperators

Operators in Python

Discover how operators work in Python, allowing you to perform calculations, comparisons, and logical operations with ease.

Types of Operators

Operators in Python are special symbols used to perform operations on variables and values. They help you carry out mathematical calculations, compare values, and make decisions in your code.

1. Arithmetic Operators

Arithmetic operators are used for basic mathematical operations:

  • + (Addition): Adds two values.
  • - (Subtraction): Subtracts the second value from the first.
  • * (Multiplication): Multiplies two values.
  • / (Division): Divides the first value by the second.
  • // (Floor Division): Divides and returns the whole number part only.
  • % (Modulus): Returns the remainder of a division.
  • ** (Exponentiation): Raises the first value to the power of the second.

# Examples of arithmetic operators
x = 10
y = 3

print(x + y)   # Output: 13 (Addition)
print(x - y)   # Output: 7 (Subtraction)
print(x * y)   # Output: 30 (Multiplication)
print(x / y)   # Output: 3.333... (Division)
print(x // y)  # Output: 3 (Floor Division)
print(x % y)   # Output: 1 (Modulus)
print(x ** y)  # Output: 1000 (Exponentiation)
        

2. Comparison Operators

Comparison operators compare two values and return a boolean result (True or False):

  • ==: Checks if values are equal.
  • !=: Checks if values are not equal.
  • >: Checks if the first value is greater than the second.
  • <: Checks if the first value is less than the second.
  • >=: Checks if the first value is greater than or equal to the second.
  • <=: Checks if the first value is less than or equal to the second.

# Examples of comparison operators
a = 5
b = 10

print(a == b)   # Output: False (Equal)
print(a != b)   # Output: True (Not equal)
print(a > b)    # Output: False (Greater than)
print(a < b)    # Output: True (Less than)
print(a >= b)   # Output: False (Greater than or equal to)
print(a <= b)   # Output: True (Less than or equal to)
        

3. Logical Operators

Logical operators combine multiple conditions and return True or False:

  • and: Returns True if both conditions are true.
  • or: Returns True if at least one condition is true.
  • not: Returns the opposite of the condition's truth value.

# Examples of logical operators
x = 5
y = 3

print(x > 2 and y < 5)  # Output: True (Both conditions are true)
print(x > 10 or y < 5)  # Output: True (One condition is true)
print(not (x > 10))     # Output: True (Reverses the false condition)
        

4. Assignment Operators

Assignment operators are used to assign values to variables. They can also perform arithmetic operations and update the variable's value:

  • =: Assigns a value to a variable.
  • +=: Adds and assigns (x += 2 is the same as x = x + 2).
  • -=: Subtracts and assigns.
  • *=: Multiplies and assigns.
  • /=: Divides and assigns.
  • %=: Modulus and assigns.
  • **=: Exponentiates and assigns.
  • //=: Floor divides and assigns.

# Examples of assignment operators
x = 5
x += 3  # Same as x = x + 3
print(x)  # Output: 8

x *= 2
print(x)  # Output: 16

x -= 5
print(x)  # Output: 11
        
0 Interaction
2K Views
Views
43 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home