Welcome to The Coding College, your trusted resource for learning Python programming. In this tutorial, we’ll explore Python operators, which are essential for performing operations on variables and values. Whether you’re a beginner or revisiting the basics, this guide will give you a thorough understanding of Python operators and their use cases.
Let’s dive in!
What Are Python Operators?
Operators in Python are special symbols or keywords that perform operations on one or more operands. They allow you to perform calculations, comparisons, and logical operations, manipulate data, and much more.
Types of Operators in Python
Python provides several types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
Arithmetic operators are used for basic mathematical operations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division (float) | 5 / 3 | 1.6667 |
// | Floor Division | 5 // 3 | 1 |
% | Modulus (remainder) | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
Example
a = 10
b = 3
print(a + b) # Output: 13
print(a % b) # Output: 1
2. Comparison Operators
Comparison operators compare two values and return a Boolean (True
or False
).
Operator | Description | Example | Output |
---|---|---|---|
== | 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 |
Example
x = 7
y = 10
print(x > y) # Output: False
print(x != y) # Output: True
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example | Output |
---|---|---|---|
and | Returns True if both are True | True and False | False |
or | Returns True if at least one is True | True or False | True |
not | Reverses the Boolean value | not True | False |
Example
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
4. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
Operator | Description | Example | Output |
---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
` | ` | Bitwise OR | `5 |
^ | Bitwise XOR | 5 ^ 3 | 6 |
~ | Bitwise NOT | ~5 | -6 |
<< | Left Shift | 5 << 1 | 10 |
>> | Right Shift | 5 >> 1 | 2 |
Example
x = 5 # Binary: 101
y = 3 # Binary: 011
print(x & y) # Output: 1 (Binary: 001)
5. Assignment Operators
Assignment operators assign values to variables.
Operator | Description | Example | Equivalent |
---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 3 | x = x * 3 |
/= | Divide and assign | x /= 3 | x = x / 3 |
//= | Floor divide and assign | x //= 3 | x = x // 3 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
Example
x = 10
x += 5
print(x) # Output: 15
6. Membership Operators
Membership operators test if a value is in a sequence.
Operator | Description | Example | Output |
---|---|---|---|
in | True if value is in sequence | "a" in "apple" | True |
not in | True if value is not in sequence | "z" in "apple" | False |
7. Identity Operators
Identity operators check if two objects are the same in memory.
Operator | Description | Example | Output |
---|---|---|---|
is | True if same object | x is y | True |
is not | True if not same object | x is not y | False |
Example
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True
print(x is z) # Output: False
Exercises
1. Arithmetic Practice
Write a program to compute the following:
- Sum of two numbers
- Remainder when one number is divided by another
2. Comparison Challenge
Check if two numbers are equal or not using comparison operators.
3. Logical Fun
Use logical operators to check if a number is both greater than 5 and less than 20.
Learn Python at The Coding College
At The Coding College, we aim to make coding accessible to everyone. Python operators are the foundation of many programming tasks, and mastering them will set you up for success.
Conclusion
Python operators are versatile tools that allow you to manipulate data, compare values, and execute logical conditions. Practice these concepts to enhance your programming skills, and don’t forget to explore other Python tutorials on The Coding College.