Python Operators - A Quick Reference

Explore Python's operators – arithmetic, comparison, logical, and more. Our guide covers operator types, examples, and practical usage in Python programming
E
Edtoks3:21 min read

Some common operators in Python, including the in operator, the as keyword (used in with statements), and a few others:

1. in Operator

The in operator is used to test if a value is found in a sequence (such as a string, list, tuple, or dictionary) or a membership of an element in an iterable.

Example with Strings

text = "Hello, World!"

# Check if a substring is present in a string
if "Hello" in text:
    print("Found 'Hello' in the text.")

Example with Lists

fruits = ["apple", "banana", "cherry"]

# Check if an item is present in a list
if "banana" in fruits:
    print("Found 'banana' in the list.")

Example with Dictionaries

person = {"name": "Alice", "age": 30}

# Check if a key is present in a dictionary
if "age" in person:
    print("Found 'age' in the dictionary.")

2. as Keyword (Used in with Statements)

The as keyword is used in with statements to assign a name to the context manager, which can be useful for working with resources that need to be managed (e.g., files).

Example with File Handling

# Using 'as' with 'with' to open a file
with open("example.txt", "r") as file:
    content = file.read()

# 'file' is automatically closed when the 'with' block exits

In this example, the as keyword is used to assign the name file to the file object returned by open(). It ensures that the file is properly closed when the with block exits, even if an exception occurs.

3. Comparison Operators:

Python has a variety of comparison operators used to compare values:

  • == (Equal): Tests if two values are equal.
  • != (Not Equal): Tests if two values are not equal.
  • > (Greater Than): Tests if one value is greater than another.
  • < (Less Than): Tests if one value is less than another.
  • >= (Greater Than or Equal To): Tests if one value is greater than or equal to another.
  • <= (Less Than or Equal To): Tests if one value is less than or equal to another.

Example

x = 10
y = 5

if x > y:
    print("x is greater than y.")

4. Logical Operators

Logical operators are used to combine conditional statements:

  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Returns the opposite of the condition.

Example

age = 25
income = 50000

if age < 30 and income > 40000:
    print("Eligible for a special offer.")

5. Membership Operators

Python provides two membership operators to test for membership in a sequence:

  • in: Returns True if a value is found in the sequence.
  • not in: Returns True if a value is not found in the sequence.

Example

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

if 6 not in numbers:
    print("6 is not in the list.")

These are some of the common operators and keywords used in Python. Understanding how to use them is essential for writing effective and expressive Python code.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.