EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Python

Operators in Python

In Python, operators are symbols that perform specific computations or operations on variables and values, acting as essential tools in programming.

  • Operator: Special symbol used to perform operations, like +, -, *, /.
  • Operand: Value on which the operator acts.

Types of Operators in Python

  • Arithmetic
  • Operators
  • Comparison
  • Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Identity and Membership Operators

Arithmetic Operators in Python

These operators help perform basic arithmetic tasks such as addition, subtraction, multiplication, and division.

In Python 3.x, the division operator / returns a floating-point result, while // returns a floored integer result.

OperatorDescriptionSyntax
+Adds two valuesx + y
Subtracts second value from the firstx - y
*Multiplies two valuesx * y
/Divides first value by the second (float)x / y
//Divides first value by second (floor)x // y
%Returns remainderx % y
**Raises first value to power of secondx ** y

Example: Performing Arithmetic Operations

a = 12
b = 5

sum_val = a + b
diff_val = a - b
product_val = a * b
quotient_val = a / b
floor_div = a // b
modulus_val = a % b
power_val = a ** b

print("Sum:", sum_val)
print("Difference:", diff_val)
print("Product:", product_val)
print("Quotient (float):", quotient_val)
print("Quotient (floor):", floor_div)
print("Modulus:", modulus_val)
print("Power:", power_val)

Output:

Sum: 17
Difference: 7
Product: 60
Quotient (float): 2.4
Quotient (floor): 2
Modulus: 2
Power: 248832

Comparison Operators

Comparison operators compare values and return True or False based on the condition.

OperatorDescriptionSyntax
>True if left value is greaterx > y
<True if left value is smallerx < y
==True if values are equalx == y
!=True if values are not equalx != y
>=True if left value is greater or equalx >= y
<=True if left value is smaller or equalx <= y

Example: Using Comparison Operators

x = 15
y = 20

print("Is x greater than y?", x > y)
print("Is x less than y?", x < y)
print("Is x equal to y?", x == y)
print("Is x not equal to y?", x != y)
print("Is x greater than or equal to y?", x >= y)
print("Is x less than or equal to y?", x <= y)

Output:

Is x greater than y? False
Is x less than y? True
Is x equal to y? False
Is x not equal to y? True
Is x greater than or equal to y? False
Is x less than or equal to y? True

Logical Operators

Logical operators combine conditional expressions.

OperatorDescriptionSyntax
andTrue if both conditions are truex and y
orTrue if either condition is truex or y
notTrue if condition is falsenot x

Example: Logical Operations with Conditions

a = True
b = False

print("a and b:", a and b)
print("a or b:", a or b)
print("not a:", not a)

Output:

a and b: False
a or b: True
not a: False

Bitwise Operators

These operators perform bit-by-bit operations.

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise OR`x
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx >> y
<<Bitwise left shiftx << y

Example: Applying Bitwise Operations

a = 5
b = 3

print("a & b:", a & b)
print("a | b:", a | b)
print("~a:", ~a)
print("a ^ b:", a ^ b)
print("a >> 1:", a >> 1)
print("a << 1:", a << 1)

Output:

a & b: 1
a | b: 7
~a: -6
a ^ b: 6
a >> 1: 2
a << 1: 10

Assignment Operators

These operators assign values to variables, often combining an arithmetic operation.

OperatorDescriptionSyntax
=Simple assignmentx = y
+=Add and assignx += y
-=Subtract and assignx -= y
*=Multiply and assignx *= y
/=Divide and assign (float)x /= y
//=Divide and assign (floor)x //= y
%=Modulus and assignx %= y
**=Exponent and assignx **= y
&=Bitwise AND and assignx &= y
|=Bitwise OR and assign`x
^=Bitwise XOR and assignx ^= y
>>=Right shift and assignx >>= y
<<=Left shift and assignx <<= y

Example: Using Assignment Operators

x = 10
y = 3

x += y
print("After += :", x)

x -= y
print("After -= :", x)

x *= y
print("After *= :", x)

x //= y
print("After //= :", x)

Output:

After += : 13
After -= : 10
After *= : 30
After //= : 10

Identity Operators

Identity operators check if two variables point to the same object in memory.

OperatorDescription
isTrue if both are identical
is notTrue if they are not

Example: Checking Object Identity

a = 100
b = 100
c = a

print("a is b:", a is b)
print("a is c:", a is c)

Output:

a is b: True
a is c: True

Membership Operators

Membership operators check if a value is in a sequence, like a list or a string.

OperatorDescription
inTrue if value is in sequence
not inTrue if value is not in sequence

Example: Checking Membership in a List

fruits = ["apple", "banana", "cherry"]
fruit = "banana"

print("Is 'banana' in fruits?", fruit in fruits)
print("Is 'orange' not in fruits?", "orange" not in fruits)

Output:

Is 'banana' in fruits? True
Is 'orange' not in fruits? True
End of lesson.