Getting Started with Python Operators

TechwithJulles
4 min readFeb 21, 2023

--

In programming, operators are symbols or keywords used to perform operations on data. In Python, operators are used to manipulate values and variables. In this article, we will explore the most commonly used comparison and logical operators in Python.

Comparison Operators

Python has six comparison operators that allow us to compare two values. The comparison operators are used to evaluate conditions and return either True or False. The six comparison operators in Python are:

  • Less than operator (<)
  • Greater than operator (>)
  • Equal to operator (==)
  • Not equal to operator (!=)
  • Less than or equal to operator (<=)
  • Greater than or equal to operator (>=)

In this article, we will focus on the first four comparison operators.

Less Than Operator (<)

The less than operator is used to compare two values to see if the left operand is less than the right operand. Here’s an example:

x = 5
y = 10
if x < y:
print("x is less than y")

Greater Than Operator (>)

The greater than operator is used to compare two values to see if the left operand is greater than the right operand. Here’s an example:

x = 5
y = 10
if y > x:
print("y is greater than x")

Equal To Operator (==)

The equal to operator is used to compare two values to see if they are equal. Here’s an example:

x = 5
y = 5
if x == y:
print("x and y are equal")

Not Equal To Operator (!=)

The not equal to operator is used to compare two values to see if they are not equal. Here’s an example:

x = 5
y = 10
if x != y:
print("x and y are not equal")

Logical Operators

Python has two logical operators that allow us to combine two or more conditions. The logical operators are used to evaluate multiple conditions and return either True or False. The two logical operators in Python are:

  • And operator
  • Or operator

In this article, we will focus on these two logical operators.

And Operator

The and operator is used to combine two or more conditions. The and operator returns True if all the conditions are True. Here’s an example:

x = 5
y = 10
z = 15
if x < y and y < z:
print("x is less than y and y is less than z")

Or Operator

The or operator is used to combine two or more conditions. The or operator returns True if at least one of the conditions is True. Here’s an example:

x = 5
y = 10
z = 15
if x > y or y < z:
print("x is greater than y or y is less than z")

Conclusion

In conclusion, operators are an essential part of programming in Python. Understanding and utilizing operators allows us to create complex programs that perform operations on data. The comparison and logical operators covered in this article are the most commonly used operators in Python, but there are many more operators that you can explore to expand your knowledge and coding skills.

In addition to the comparison and logical operators covered in this article, Python has a few other types of operators:

  • Arithmetic Operators: used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and more.
  • Bitwise Operators: used to perform operations on binary numbers.
  • Assignment Operators: used to assign values to variables.
  • Membership Operators: used to test if a value is a member of a sequence or a mapping.
  • Identity Operators: used to compare the memory locations of two objects.

To learn more about these types of operators, I recommend exploring the Python documentation or taking a Python course or tutorial.

Here are a few examples of arithmetic and assignment operators in Python:

Arithmetic Operators

x = 10
y = 5

Addition

z = x + y
print(z)

Subtraction

z = x - y
print(z)

Multiplication

z = x * y
print(z)

Division

z = x / y
print(z)

Modulo

z = x % y
print(z)

Exponentiation

z = x ** y
print(z)

Assignment Operators

x = 10

Add and assign

x += 5
print(x)

Subtract and assign

x -= 3
print(x)

Multiply and assign

x *= 2
print(x)

Divide and assign

x /= 3
print(x)

Modulo and assign

x %= 2
print(x)

Exponentiation and assign

x = 2
x **= 3
print(x)

In summary, Python operators are essential tools for programming in Python. They allow us to manipulate data, evaluate conditions, and perform calculations. Understanding the different types of operators and how to use them is crucial for writing effective Python code.

Part 3: Getting Started with Python: Datatypes | by TechwithJulles | Feb, 2023 | Medium

Part 5: Getting Started with Python — if, else, and elif Statements | by TechwithJulles | Feb, 2023 | Medium

Article Notebook — Google Drive

If you enjoyed this article and would like to show your support, feel free to buy me a coffee! Your support is greatly appreciated and it helps me continue creating content that you love. You can make a contribution by following this link: Buy Me a Coffee. Thank you for your generosity and for being a part of this journey!

--

--

TechwithJulles

I'm a software developer who enjoys teaching people about programming and the tech world. #TechWithJulles