Operators
Learn about operators in Python, including arithmetic, comparison, logical, bitwise, assignment, membership, and identity operators.
Last updated: 2024-12-13Introduction to Operators
Python is a popular programming language because of its natural-language-like syntax and ease of reading. It is the perfect first programming language because of its simplicity. Understanding operators, which are tools that let you to execute operations on variables and values, is a crucial part of learning Python. Even anyone with little to no programming expertise will benefit from this guide's straightforward explanations and illustrations of Python's fundamental operators.
What are Operators?
An operator in Python generates a new value from one or more variables or values. As a result, operators are employed to process data and provide outcomes. For example, the greater-than operator (>)
compares two integers, whereas the addition operator (+)
adds two numbers together.
Operators can be grouped according to how many operands they accept:
One operand can be operated on using unary operators. For instance, the logical not (not)
inverts the operand's truth value, and the negation operator (-)
reverses a number's sign.
Two operands can be operated on using binary operators. The majority of operators, including equality (==)
, multiplication (*)
, and addition (+)
, fall within this category.
Types of Operators
Arithmetic Operators:
Used for basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
// | Floor Division | a // b |
% | Modulus (Remainder) | a % b |
`` | Exponentiation | a b |
Comparison Operators:
Used to compare values, resulting in a boolean value.
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Logical Operators:
Used to perform logical operations.
Operator | Description | Example |
---|---|---|
and | Logical AND | a > 0 and b < 5 |
or | Logical OR | a > 0 or b < 5 |
not | Logical NOT | not (a > 0) |
Bitwise Operators:
Operate on binary representations of numbers.
Operator | Description | Example |
---|---|---|
& | AND | a & b |
` | ` | OR |
^ | XOR | a ^ b |
~ | NOT (Complement) | ~a |
<< | Left Shift | a << 2 |
>> | Right Shift | a >> 2 |
Assignment Operators:
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = 5 |
+= | Add and Assign | a += 5 |
-= | Subtract and Assign | a -= 5 |
*= | Multiply and Assign | a *= 5 |
/= | Divide and Assign | a /= 5 |
//= | Floor Divide and Assign | a //= 5 |
%= | Modulus and Assign | a %= 5 |
= | Exponentiate and Assign | a = 5 |
Membership Operators:
Check membership in sequences like lists, tuples, or strings.
Operator | Description | Example |
---|---|---|
in | Present in sequence | 'a' in 'apple' |
not in | Not present in sequence | 'x' not in 'apple' |
Identity Operators:
Check if two objects share the same memory location.
Operator | Description | Example |
---|---|---|
is | Same identity | a is b |
is not | Different identity | a is not b |
Operator Precedence and Associativity
Operator precedence determines the order of execution in expressions with multiple operators. Associativity defines how operators with the same precedence are processed.
Precedence Table:
Operator | Description |
---|---|
`` | Exponentiation |
+ , - (unary) | Positive, Negative |
* , / , // , % | Multiplication, Division |
+ , - | Addition, Subtraction |
== , != , > , < , >= , <= | Comparison |
not | Logical NOT |
and | Logical AND |
or | Logical OR |
Examples of Arithmetic Operators
Arithmetic operators are fundamental in any programming task that involves numerical calculations. Here’s how you can use these operators in Python:
# Example of Addition
a = 10
b = 5
print("Addition:", a + b) # Output: 15
# Example of Subtraction
print("Subtraction:", a - b) # Output: 5
# Example of Multiplication
print("Multiplication:", a * b) # Output: 50
# Example of Division
print("Division:", a / b) # Output: 2.0
# Example of Floor Division
print("Floor Division:", a // b) # Output: 2
# Example of Modulus
print("Modulus:", a % b) # Output: 0
# Example of Exponentiation
print("Exponentiation:", a ** 2) # Output: 100
Examples of Comparison Operators
Comparison operators are used to compare two values. Here are some examples of comparison operators in Python:
# Example of Equality
a = 10
b =10
print("Equality:", a == b) # Output: True
# Example of Inequality
print("Inequality:", a != b) # Output: False
# Example of Greater Than
print("Greater Than:", a > b) # Output: False
# Example of Less Than
print("Less Than:", a < b) # Output: False
# Example of Greater Than or Equal To
print("Greater Than or Equal To:", a >= b) # Output: True
# Example of Less Than or Equal To
print("Less Than or Equal To:", a <= b) # Output: True
These examples demonstrate how to utilise Python operators in a program to handle real-world input, carry out computations, and reach logical conclusions. You can gain a better grasp of how these operators affect the behaviour of your code by experimenting with them by changing variables and conditions. You'll discover that these operators serve as the foundation for increasingly intricate logic and features in Python applications as you get more familiar with them.
Conclusion
Understanding Python operators is crucial for building efficient and readable code. Practice using these operators in different contexts to strengthen your programming skills.