Ugenz Academy/Tutorial

Operators

7 min read1,385 words

Reading progress

Keep learning

Operators in Python

An operator is a symbol or keyword used to perform an operation on values or variables.

Example:

a = 10 b = 5 print(a + b)

Here, + is an operator.

Python operators are mainly divided into 7 types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
%Modulus10 % 31
**Exponentiation2 ** 38
//Floor Division10 // 33

Example

a = 10 b = 3 print(a + b) print(a - b) print(a * b) print(a / b) print(a % b) print(a ** b) print(a // b)

Output:

13 7 30 3.3333333333333335 1 1000 3

Modulus %

Returns the remainder:

print(10 % 3)

Output:

1

Floor Division //

Returns the quotient without the decimal part:

print(10 // 3)

Output:

3

2. Assignment Operators

Assignment operators are used to assign or update values in variables.

OperatorExampleMeaning
=x = 10Assign
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5Bitwise AND
=`x= 5`
^=x ^= 5Bitwise XOR
>>=x >>= 5Right shift
<<=x <<= 5Left shift

Example

x = 10 x += 5 print(x)

Output:

15

This:

x += 5

is equivalent to:

x = x + 5

3. Comparison Operators

Comparison operators are used to compare two values. The result is always:

True

or:

False
OperatorNameExample
==Equal to10 == 10
!=Not equal to10 != 5
>Greater than10 > 5
<Less than10 < 5
>=Greater than or equal to10 >= 10
<=Less than or equal to5 <= 10

Example

a = 10 b = 5 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)

Output:

False True True False True False

Important: = vs ==

= means assignment:

x = 10

== means comparison:

x == 10

4. Logical Operators

Logical operators are used to combine or modify conditions.

Python has three logical operators:

OperatorMeaning
andTrue if both conditions are true
orTrue if at least one condition is true
notReverses the result

and

Both conditions must be true.

age = 20 print(age >= 18 and age <= 60)

Output:

True

or

At least one condition must be true.

age = 15 print(age < 18 or age > 60)

Output:

True

not

Reverses the result.

is_student = True print(not is_student)

Output:

False

5. Identity Operators

Identity operators check whether two variables refer to the same object in memory.

There are two identity operators:

OperatorMeaning
isSame object
is notNot the same object

Example:

a = [1, 2, 3] b = a print(a is b)

Output:

True

Because b refers to the same list object as a.

is not

a = [1, 2, 3] b = [1, 2, 3] print(a is not b)

Output:

True

The lists contain the same values, but they are separate objects.

== vs is

This is important:

a == b

checks whether the values are equal.

a is b

checks whether they are the same object.

6. Membership Operators

Membership operators check whether a value exists inside a sequence or collection.

There are two:

OperatorMeaning
inValue exists
not inValue does not exist

in

fruits = ["apple", "banana", "mango"] print("apple" in fruits)

Output:

True

not in

fruits = ["apple", "banana", "mango"] print("orange" not in fruits)

Output:

True

Membership operators can be used with strings too:

text = "Python" print("Py" in text)

Output:

True

7. Bitwise Operators

Bitwise operators work with numbers at the binary/bit level.

OperatorName
&AND
``OR
^XOR
~NOT
<<Left Shift
>>Right Shift

Bitwise AND &

a = 5 b = 3 print(a & b)

Binary:

5 = 101 3 = 011 --------- 001

Result:

1

Bitwise OR |

print(5 | 3)

Result:

7

Bitwise XOR ^

print(5 ^ 3)

Result:

6

Bitwise NOT ~

print(~5)

Result:

-6

Left Shift <<

print(5 << 1)

Result:

10

Right Shift >>

print(5 >> 1)

Result:

2

Bitwise operators are especially useful in areas such as low-level programming, networking, embedded systems, and cybersecurity.

Operator Precedence

When an expression contains multiple operators, Python follows a specific order.

For example:

result = 10 + 5 * 2

Python performs multiplication first:

5 * 2 = 10 10 + 10 = 20

So:

print(result)

Output:

20

Using parentheses changes the order:

result = (10 + 5) * 2 print(result)

Output:

30

Basic precedence order

From higher to lower:

() ** +x, -x, ~x *, /, //, % +, - <<, >> & ^ | <, <=, >, >=, ==, != not and or

Complete Summary

TypeOperators
Arithmetic+ - * / % ** //
Assignment`= += -= *= /= %= //= **= &== ^= >>= <<=`
Comparison== != > < >= <=
Logicaland or not
Identityis, is not
Membershipin, not in
Bitwise& | ^ ~ << >>

Simple way to remember

Arithmetic → Calculate Assignment → Assign/Update Comparison → Compare Logical → Combine conditions Identity → Check same object Membership → Check presence Bitwise → Work with binary bits

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy