0% found this document useful (0 votes)
12 views5 pages

Python Operators: Types and Examples

The document provides an overview of various operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, identity, and the ternary operator. Each operator is defined with its symbol, examples, and results. This serves as a comprehensive guide for understanding how to perform different operations in Python programming.

Uploaded by

achantaaditya300
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)
12 views5 pages

Python Operators: Types and Examples

The document provides an overview of various operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, identity, and the ternary operator. Each operator is defined with its symbol, examples, and results. This serves as a comprehensive guide for understanding how to perform different operations in Python programming.

Uploaded by

achantaaditya300
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

1.

Arithmetic Operators
Definition:

Arithmetic operators in Python are used to perform mathematical calculations such as addition (+),
subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//)
between numeric operands.

Operator Symbol Definition / Meaning Example Result |

Addition ` +` Adds two numbers `4 + 2` `6`


Subtraction `-` Subtracts right operand from left `4 - 2` `2`
Multiplication `*` Multiplies two numbers `4 * 2` `8`
Division `/` Divides left operand by right (float result) `4 / 2` ` 2.0` Floor
Division `//` Divides and returns whole number part `5 // 2` `2` Modulus
`%` Returns remainder of division `5 % 2` `1` Exponentiation `**`
Rises number to the power of another `2 ** 3` `8`

2. Comparison Operators
Definition:

Comparison operators in Python are used to compare the values of two operands. The result of a
comparison is either True or False.

Operator Name Description Example Output

`==` Equal True if values are equal `5 == 5` `True`

`!=` Not Equal True if values are not equal `5 != 3` `True`

`>` Greater than True if left > right `5 > 3` `True`

`<` Less than True if left < right `5 < 3` `False`

`>=` Greater or Equal True if left >= right `5 >= 5` `True`

`<=` Less or Equal True if left <= right `3 <= 5` `True`

3. Assignment Operators
Definition:

Arithmetic operators in Python are used to perform mathematical calculations such as addition (+),
subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//)
between numeric operands.
Operator Example Same As

`=` `a = 5` Assign 5 to a

`+=` `a += 3` `a = a + 3`

`-=` `a -= 2` `a = a - 2`

`*=` `a *= 4` `a = a * 4`

`/=` `a /= 2` `a = a / 2`

`//=` `a //= 2` `a = a // 2`

`%=` ` a %= 3` `a = a % 3`

`**=` `a **= 2` `a = a ** 2`

4. Logical Operators
Definition:

Logical operators in Python are used to perform logical operations on Boolean values (True or False).
These operators are mainly used in conditional statements.

Syntax :

AND Operator:

python

condition1 and condition2

OR Operator:

python

condition1 or condition2

NOT Operator:

python

not condition

Operator Description Example Output

`and` True if both are true ` True and False` `False`

`or` True if at least one is ` True or False` ` True`


`not` Reverses the result ` not True` `False`

5. Bitwise Operators
Definition:

Bitwise operators in Python are used to perform operations on individual bits of integer values. These
operations are fast and work directly on the binary representation of numbers.

Syntax:

python

result = operand1 <operator> operand2

Operator Name Example Result (Binary) Output

`&` AND `5 & 3` `0101 & 0011` `1`

'I' OR `5 | 3` `0101 | 0011` `7`

`^` XOR `5 ^ 3` `0101 ^ 0011` `6`

`~` NOT `~5` `~0101` `-6`

`<<` Left Shift `5 << 1` `01010` `10`

`>>` Right Shift `5 >> 1` `0010` `2`

6. Membership Operators

Definition:

Membership operators in Python test for presence or absence of a value in a sequence (such as list,
string, tuple, dictionary, or set). They return a Boolean value (True or False).

Syntax (Single Line)

in

python

value in sequence

not in

python

value not in sequence


Operator Description Example Output

`in` True if value is present `3 in [1,2,3]` `True`

`not in` True if value is not present `5 not in [1,2,3]` `True`

7. Identity Operators
Definition: Definition:

Identity operators in Python are used to compare the memory location of two objects — whether they
are actually the same object, not just equal in value.

These operators are:

[Link]

[Link] not

Syntax:

python

a is b # True if a and b refer to the same object

a is not b # True if a and b do NOT refer to the same object

Operator Description Example Output

`is` True if both refer to same object `x is y` `True/False`

`is not` True if they do not refer same object `x is not z` `True/False`

[Link] OPERATOR

Definition:

A ternary operator in Python is a way to perform conditional decision-making in a single line. It’s used to
evaluate something based on a condition and return one of two values depending on whether the
condition is True or False.

Syntax:

python

<value_if_true> if <condition> else <value_if_false>


Component Description Example

`condition` The test expression to evaluate `a > b`

`value_if_true` The result if the condition is True `a`

`value_if_false` The result if the condition is False `b`

Full expression Combines all parts into a one-line conditional check `a if a > b elseb`

You might also like