Operators

Keywords to carry out special actions.

Description of the GIF

Python has a variety of operators that perform operations on variables and values. These operators are categorized based on their functionality:

Arithmetic Operators

Used to perform mathematical operations:

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
**Exponentiationa ** b (a^b)
//Floor Divisiona // b
a, b = 10, 3
print(a + b)  # 13
print(a ** b)  # 1000

Comparison (Relational) Operators

Used to compare two values and return a boolean result:

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
a, b = 10, 3
print(a > b)  # True

Logical Operators

Used to combine conditional statements:

OperatorDescriptionExample
andLogical ANDa > 5 and b < 10
orLogical ORa > 5 or b < 10
notLogical NOTnot(a > 5)
a, b = 10, 3
print(a > 5 and b < 5)  # True

Assignment Operators

Used to assign values to variables:

OperatorDescriptionExample
=Assigna = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
%=Modulus and assigna %= 5

Membership Operators

Used to check if a value is part of a sequence:

OperatorDescriptionExample
inReturns True if a value exists'a' in 'apple'
not inReturns True if it does not'x' not in 'apple'
fruits = ["apple", "banana"]
print("apple" in fruits)  # True

Identity Operators

Used to compare the memory locations of two objects:

OperatorDescriptionExample
isReturns True if objects are identical (same memory location)a is b
is notReturns True if objects are not identicala is not b
No questions available.