Loading...
Loading...
3pythonoperators

Operators in Python

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

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.

Quick Quiz: Operators Basics

What do operators do in Python?

  • Only perform mathematical calculations
  • Perform operations on variables and values
  • Just make the code look more complex

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)
        

Arithmetic Operators Quiz

What is the output of 11 % 4?

  • 3
  • 2
  • 2.75

Which operator gives the whole number part of division?

  • /
  • //
  • %

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)
        

Comparison Operators Quiz

What does 7 != 7 evaluate to?

  • False
  • True

Which operator checks "greater 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)
        

Logical Operators Quiz

What is the output of not (True and False)?

  • True
  • False

Which operator returns True if either condition is true?

  • and
  • or
  • not

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
        

Assignment Operators Quiz

What is the value of x after x = 10; x /= 2?

  • 5.0
  • 5
  • 8

Which operator combines addition and assignment?

  • =+
  • +=
  • =
0 Interaction
2K Views
Views
43 Likes
×
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

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

top-home