Geek Slack

Start creating your course and become a part of GeekSlack.

Introduction to Python
About Lesson



Python Operators


Python Operators

Operators in Python are special symbols or keywords used to perform operations on variables and values.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, etc.

Example:

a = 10
b = 5

addition = a + b        # 15
subtraction = a - b     # 5
multiplication = a * b  # 50
division = a / b        # 2.0
modulus = a % b         # 0
exponentiation = a ** b # 100000

2. Comparison Operators

Comparison operators are used to compare two values. They return either True or False.

Example:

a = 10
b = 5

equal = a == b         # False
not_equal = a != b     # True
greater = a > b        # True
less = a < b           # False
greater_equal = a >= b # True
less_equal = a <= b    # False

3. Logical Operators

Logical operators are used to combine conditional statements. They return either True or False.

Example:

a = True
b = False

and_operator = a and b    # False
or_operator = a or b      # True
not_operator = not a      # False

4. Assignment Operators

Assignment operators are used to assign values to variables.

Example:

a = 10
b = 5

a += b  # Equivalent to a = a + b

5. Membership Operators

Membership operators are used to test if a value is present in a sequence.

Example:

sequence = [1, 2, 3, 4, 5]

is_member = 3 in sequence     # True
not_member = 6 not in sequence # True

Conclusion

Python provides various types of operators to perform different types of operations. Understanding how to use these operators is essential for writing efficient and readable Python code.

Join the conversation