Operators

Learn about operators in Python, including arithmetic, comparison, logical, bitwise, assignment, membership, and identity operators.

Last updated: 2024-12-13

Introduction 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.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
//Floor Divisiona // b
%Modulus (Remainder)a % b
``Exponentiationa b

Comparison Operators:

Used to compare values, resulting in a boolean value.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Logical Operators:

Used to perform logical operations.

OperatorDescriptionExample
andLogical ANDa > 0 and b < 5
orLogical ORa > 0 or b < 5
notLogical NOTnot (a > 0)

Bitwise Operators:

Operate on binary representations of numbers.

OperatorDescriptionExample
&ANDa & b
``OR
^XORa ^ b
~NOT (Complement)~a
<<Left Shifta << 2
>>Right Shifta >> 2

Assignment Operators:

Used to assign values to variables.

OperatorDescriptionExample
=Assigna = 5
+=Add and Assigna += 5
-=Subtract and Assigna -= 5
*=Multiply and Assigna *= 5
/=Divide and Assigna /= 5
//=Floor Divide and Assigna //= 5
%=Modulus and Assigna %= 5
=Exponentiate and Assigna = 5

Membership Operators:

Check membership in sequences like lists, tuples, or strings.

OperatorDescriptionExample
inPresent in sequence'a' in 'apple'
not inNot present in sequence'x' not in 'apple'

Identity Operators:

Check if two objects share the same memory location.

OperatorDescriptionExample
isSame identitya is b
is notDifferent identitya 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:

OperatorDescription
``Exponentiation
+, - (unary)Positive, Negative
*, /, //, %Multiplication, Division
+, -Addition, Subtraction
==, !=, >, <, >=, <=Comparison
notLogical NOT
andLogical AND
orLogical 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.

Additional Resources

  1. Python Operators