Python Operators
Python Operators Tutorial
Welcome to this comprehensive tutorial on Python Operators, brought to you by codeswithpankaj.com. In this tutorial, we will delve into various types of operators in Python, covering their syntax, usage, and practical examples. By the end of this tutorial, you will have a thorough understanding of how to use operators effectively in your Python programs.
Table of Contents
Introduction to Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Identity Operators
Membership Operators
Operator Precedence
Practical Examples
1. Introduction to Operators
Operators are special symbols or keywords in Python that perform operations on variables and values. Python provides a variety of operators, each serving a specific purpose. These operators can be categorized into several types based on their functionality.
Types of Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Identity Operators
Membership Operators
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and more.
List of Arithmetic Operators
Operator | Description | Example |
---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Modulus |
|
| Exponentiation |
|
| Floor Division |
|
Examples
3. Comparison Operators
Comparison operators are used to compare two values. They return either True
or False
based on the comparison result.
List of Comparison Operators
Operator | Description | Example |
---|---|---|
| Equal to |
|
| Not equal to |
|
| Greater than |
|
| Less than |
|
| Greater than or equal to |
|
| Less than or equal to |
|
Examples
4. Logical Operators
Logical operators are used to combine conditional statements. They include and
, or
, and not
.
List of Logical Operators
Operator | Description | Example |
---|---|---|
| Returns True if both statements are true |
|
| Returns True if one of the statements is true |
|
| Reverses the result, returns False if the result is true |
|
Examples
5. Bitwise Operators
Bitwise operators are used to perform bit-level operations on integers. They include &
, |
, ^
, ~
, <<
, and >>
.
List of Bitwise Operators
Operator | Description | Example |
---|---|---|
| Bitwise AND |
|
` | ` | Bitwise OR |
| Bitwise XOR |
|
| Bitwise NOT |
|
| Bitwise left shift |
|
| Bitwise right shift |
|
Examples
6. Assignment Operators
Assignment operators are used to assign values to variables. They include =
, +=
, -=
, *=
, /=
, %=
, **=
, and //=
.
List of Assignment Operators
Operator | Description | Example |
---|---|---|
| Assign |
|
| Add and assign |
|
| Subtract and assign |
|
| Multiply and assign |
|
| Divide and assign |
|
| Modulus and assign |
|
| Exponentiation and assign |
|
| Floor division and assign |
|
Examples
7. Identity Operators
Identity operators are used to compare the memory locations of two objects. They include is
and is not
.
List of Identity Operators
Operator | Description | Example |
---|---|---|
| Returns True if both variables are the same object |
|
| Returns True if both variables are not the same object |
|
Examples
8. Membership Operators
Membership operators are used to test if a sequence contains a specified value. They include in
and not in
.
List of Membership Operators
Operator | Description | Example |
---|---|---|
| Returns True if a sequence contains a |
specified value | a in b
| | not in
| Returns True if a sequence does not contain a specified value | a not in b
|
Examples
9. Operator Precedence
Operator precedence determines the order in which operations are performed in an expression. Operators with higher precedence are evaluated before those with lower precedence.
List of Operator Precedence
The following table lists the operators in descending order of precedence:
Precedence | Operator | Description |
---|---|---|
1 |
| Parentheses |
2 |
| Exponentiation |
3 |
| Unary plus and minus |
4 |
| Multiplication, Division, Modulus, Floor Division |
5 |
| Addition, Subtraction |
6 |
| Bitwise shift operators |
7 |
| Bitwise AND |
8 |
| Bitwise XOR |
9 | ` | ` |
10 |
| Comparison operators |
11 |
| Logical NOT |
12 |
| Logical AND |
13 |
| Logical OR |
Examples
10. Practical Examples
Example 1: Arithmetic Operations
Example 2: Comparison and Logical Operations
Example 3: Bitwise Operations
Example 4: Assignment Operations
Example 5: Identity and Membership Operations
This concludes our detailed tutorial on Python Operators. We hope you found this tutorial helpful and informative. For more tutorials and resources, visit codeswithpankaj.com. Happy coding!
Last updated