Operators in Python

Python tutorial · PySpark.in

Operators in Python 

Operators in Python are symbols or keywords that help you perform operations on variables and values.They are the backbone of expressions and logic in every Python program.

In this tutorial, we will learn different types of operators with clear examples.

Types of Operators in Python

Python supports the following categories of operators:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Membership Operators
  6. Identity Operators

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator

Description

Example

Result

+

Addition

5 + 3

8

-

Subtraction

10 - 2

8

*

Multiplication

6 * 7

42

/

Division

9 / 2

4.5

%

Modulus (Remainder)

9 % 2

1

**

Exponentiation

3 ** 2

9

//

Floor Division

9 // 2

4

Example   

```

a = 34

b = 2

print("a + b =", a + b)

print("a - b =", a - b)

print("a * b =", a * b)

print("a / b =", a / b)

print("a % b =", a % b)

print("a // b =", a // b)

print("a ** b =", a ** b)

```

Comparison (Relational) Operators

These operators compare two values and return either True or False.

Operator

Meaning

Example

Result

==

Equal to

5 == 5

True

!=

Not equal to

5 != 3

True

>

Greater than

5 > 3

True

<

Less than

5 < 3

False

>=

Greater or equal

5 >= 5

True

<=

Less or equal

3 <= 4

True

Example

```

a = 34

print(a > 4) # True

print(a < 4) # False

print(a >= 4) # True

print(a <= 4) # False

print(a == 34) # True

print(a != 34) # False

````

Logical Operators

Logical operators are used to combine conditional expressions.

Operator

Description

Example

Result

and

True if both conditions are True

True and False

False

or

True if at least one condition is True

True or False

True

not

Reverses the result

not True

False

Example

```

print(True and True) # True

print(True and False) # False

print(False or True) # True

print(False or False) # False

print(not True) # False

print(not False) # True

```

and → all conditions must be true
or → at least one condition must be true
not → flips True ↔ False

Assignment Operators

Assignment operators are used to assign or update the value of variables.

Operator

Example

Meaning

=

a = 5

Assign value

+=

a += 3

a = a + 3

-=

a -= 3

a = a - 3

*=

a *= 3

a = a * 3

/=

a /= 3

a = a / 3

%=

a %= 3

a = a % 3

**=

a **= 3

a = a ** 3

//=

a //= 3

a = a // 3

Example

```

a = 32

a += 3

print(a) # 35

a -= 3

print(a) # 32

a *= 3

print(a) # 96

```

Membership Operators

Used to test whether a value exists in a sequence (like strings or lists).

Operator

Example

Meaning

in

'a' in 'apple'

True if value is present

not in

'b' not in 'apple'

True if value is not present

Example

```

print('a' in 'apple') # True

print('b' not in 'apple') # True

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

print("apple" in fruits) # True

print("grape" not in fruits) # True

```

Identity Operators

These operators compare memory locations of objects.

Operator

Example

Meaning

is

a is b

True if both refer to the same object

is not

a is not b

True if both are different objects

Example

```

a = [1, 2, 3]

b = a

c = [1, 2, 3]

print(a is b) # True (same memory)

print(a is c) # False (different memory)

print(a is not c) # True

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges