0% found this document useful (0 votes)
2 views

Operators_in_Python

The document provides an overview of various operators in Python, categorized into eight types: Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity, and Ternary Operators. Each category includes examples demonstrating the functionality of the operators. This serves as a concise reference for understanding how to perform different operations in Python programming.

Uploaded by

Vedansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Operators_in_Python

The document provides an overview of various operators in Python, categorized into eight types: Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity, and Ternary Operators. Each category includes examples demonstrating the functionality of the operators. This serves as a concise reference for understanding how to perform different operations in Python programming.

Uploaded by

Vedansh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Operators in Python with Examples

1. Arithmetic Operators:

These perform basic mathematical operations.

Examples:

Addition: 5 + 3 -> 8

Subtraction: 5 - 3 -> 2

Multiplication: 5 * 3 -> 15

Division: 5 / 3 -> 1.666...

Floor Division: 5 // 3 -> 1

Modulus: 5 % 3 -> 2

Exponentiation: 5 ** 3 -> 125

2. Comparison Operators:

These compare two values and return a Boolean result.

Examples:

Equal to: 5 == 3 -> False

Not equal to: 5 != 3 -> True

Greater than: 5 > 3 -> True

Less than: 5 < 3 -> False

Greater than or equal to: 5 >= 3 -> True

Less than or equal to: 5 <= 3 -> False

3. Logical Operators:

These combine conditional statements.

Examples:

AND: (5 > 3) and (5 < 10) -> True

OR: (5 < 3) or (5 < 10) -> True


NOT: not(5 > 3) -> False

4. Assignment Operators:

These assign values to variables.

Examples:

x = 5 -> Assigns 5 to x

x += 3 -> x = x + 3 -> 8

x -= 3 -> x = x - 3 -> 5

x *= 3 -> x = x * 3 -> 15

x /= 3 -> x = x / 3 -> 5.0

x //= 3 -> x = x // 3 -> 1

x %= 3 -> x = x % 3 -> 2

x **= 3 -> x = x ** 3 -> 125

5. Bitwise Operators:

These operate on binary representations of integers.

Examples:

AND: 5 & 3 -> 1 (0101 & 0011 -> 0001)

OR: 5 | 3 -> 7 (0101 | 0011 -> 0111)

XOR: 5 ^ 3 -> 6 (0101 ^ 0011 -> 0110)

NOT: ~5 -> -6 (Inverts bits)

Left Shift: 5 << 1 -> 10

Right Shift: 5 >> 1 -> 2

6. Membership Operators:

These check for membership in sequences.

Examples:

'a' in 'apple' -> True


'b' not in 'apple' -> True

7. Identity Operators:

These compare the memory location of objects.

Examples:

x = [1, 2, 3]

y=x

x is y -> True

x is not y -> False

8. Ternary Operator:

Short-hand for conditional expressions.

Example:

x=5

y=3

z = 'Greater' if x > y else 'Smaller'

Output: Greater

You might also like