Search This Blog

Basic Operators in Python

 

Basic Operators in Python

Operators in Python are special symbols or keywords that allow you to perform operations on variables and values. Python supports a variety of operators, which can be grouped into several categories: arithmetic, assignment, comparison, logical, bitwise, and membership operators. Here’s an overview of each.


1. Arithmetic Operators

Arithmetic operators are used for mathematical operations on numeric values.

Operator Description Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus (remainder) x % y
** Exponentiation x ** y
// Floor Division x // y

Example:

x = 10
y = 3

print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.3333...
print(x % y)  # 1 (remainder of 10 divided by 3)
print(x ** y) # 1000 (10 to the power of 3)
print(x // y) # 3 (floor division)

2. Assignment Operators

Assignment operators are used to assign values to variables. The most basic is the = operator, but Python also supports shorthand operators for updating the value of a variable.

Operator Description Example
= Assign x = 5
+= Add and assign x += 3
-= Subtract and assign x -= 3
*= Multiply and assign x *= 3
/= Divide and assign x /= 3
%= Modulus and assign x %= 3
**= Exponent and assign x **= 3
//= Floor division and assign x //= 3

Example:

x = 10
x += 5  # x is now 15
x *= 2  # x is now 30
x -= 10 # x is now 20
print(x)

3. Comparison Operators

Comparison operators are used to compare two values, and they return True or False based on the comparison result.

Operator Description 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

Example:

x = 10
y = 5

print(x == y)  # False
print(x != y)  # True
print(x > y)   # True
print(x <= y)  # False

4. Logical Operators

Logical operators are used to combine multiple conditions. The result is True or False depending on the logical outcome of the combined conditions.

Operator Description Example
and True if both conditions are true (x > 5 and y < 10)
or True if at least one condition is true (x > 5 or y > 10)
not Inverts the condition not(x > 5)

Example:

x = 10
y = 5

print(x > 5 and y < 10)  # True
print(x > 15 or y < 10)  # True
print(not(x > 5))        # False

5. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

Operator Description Example
& AND x & y
` ` OR
^ XOR x ^ y
~ NOT (inverts all bits) ~x
<< Left Shift x << 2
>> Right Shift x >> 2

Example:

x = 5   # Binary: 0101
y = 3   # Binary: 0011

print(x & y)  # 1  (Binary: 0001)
print(x | y)  # 7  (Binary: 0111)
print(x ^ y)  # 6  (Binary: 0110)
print(~x)     # -6 (Inverts all bits)
print(x << 1) # 10 (Binary: 1010, left shift)
print(x >> 1) # 2  (Binary: 0010, right shift)

6. Membership Operators

Membership operators check if a value is present in a sequence (like a list, tuple, or string).

Operator Description Example
in Returns True if value is in sequence "a" in "apple"
not in Returns True if value is not in sequence "x" not in "apple"

Example:

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)   # True
print("grape" not in fruits) # True

7. Identity Operators

Identity operators check if two variables point to the same object in memory.

Operator Description Example
is Returns True if variables are the same object x is y
is not Returns True if variables are not the same object x is not y

Example:

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)      # True, because y references the same object as x
print(x is not z)  # True, because x and z are different objects
print(x == z)      # True, because x and z have the same contents

Summary

Python’s operators make it easy to work with variables and perform complex calculations, comparisons, and conditions in your code. Understanding these operators will help you to write efficient and concise Python code.

Popular Posts