Python Operators

Mastering Python Operators: Arithmetic, Logical, Comparison & More

Python Operators Explained: Complete Guide

Operators in Python are special symbols or keywords used to perform operations on variables and values. They play a fundamental role in defining logic and expressions within a program. In this guide from Devyra, you’ll learn the full range of Python operators, including how they are categorized and used in different scenarios.

What Are Python Operators?

Operators allow developers to perform operations like addition, comparison, logical evaluation, and more. For instance, the + operator is used to add two numbers:

print(10 + 5)

Python operators are classified into the following categories:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

Python Arithmetic Operators

Used for performing common mathematical calculations:

Operator Name Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor Division x // y

Python Assignment Operators

Used to assign values to variables:

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x – 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
:= x := 3 x = 3 (Walrus Operator)

Python Comparison Operators

Used to compare values and return Boolean outcomes:

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Python Logical Operators

Used to combine conditional statements:

Operator Description Example
and True if both are true x < 5 and x < 10
or True if at least one is true x < 5 or x < 4
not Reverses the result not(x < 5 and x < 10)

Python

More From Author

Python Boolean Values

Understanding Python Boolean Values: True or False in Depth

Python Lists

Comprehensive Python Lists Tutorial with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *