Skip to content
Home » Python Operators: The Complete Guide

Python Operators: The Complete Guide

  • by
Python Operators

Python operators are symbols that represent one or more operations that can be performed on a value. In this tutorial, we will go over the complete guide to Python operators.

We will discuss what each operator does and provide examples of how they can be used.

Let’s get started!

Python Operators

Python has a variety of operators that can be used to perform operations on values. These operators can be classified into six different categories:

  • Arithmetic Operators
  • Assignment
  • Comparison Operators
  • Logical Operators
  • Membership
  • Identity Operators

Arithmetic

Arithmetic operators are used to performing mathematical operations on values. The most common arithmetic operator is the addition operator (+). Other arithmetic operators include subtraction (-), multiplication (*), division (/), and modulus (%).

Let’s take a look at an example:

>>> x = 15 # Assign the value 15 to the variable x.

>>> y = 12 # Assign the value 12 to the variable y.

>>>

>>> # Add the two variables and print the result.

>> print(x + y) # Output: 27

Operator (+)

As you can see, we were able to use the addition operator (+) to add the values of x and y together. We then printed the result of this operation using the print statement.

For example:

 x = 15 # Assign the value 15 to the variable x.

 y = 12 # Assign the value 12 to the variable y.

 z = 24 # Assign the value 24 to thd variable z.



 # Subtract z from x and multiply by y. Print the result.

 print((x - z) * y) # Output: -108

In this example, we used the subtraction operator (-) and the multiplication operator (*) to perform operations on our variables. We first subtracted z from x and then multiplied the result by y. Finally, we printed the outcome of this operation.

Now that we’ve seen how arithmetic operators can be used, let’s move on to assignment operators. Assignment operators are used to assigning values to variables. The most common assignment operator is the equal sign (=). This operator assigns the value on its right-hand side to the variable on its left-hand side.

For example:

 x = 15 # Assigns the value 15 to the variable x.

In this example, we assigned the value 15 to the variable x using the assignment operator (=). Other assignment operators include +=, -=, *=, and /=.

These operators are shorthand for performing an operation on a variable and then assigning the result to that same variable.

For example:

 x = 15 # Assigns the value 15 to the variable x.

 x += 12 # Add 12 to x and assign the result back to x.

In this example, we added 12 to our variable x using the += operator.

This is equivalent to writing:

 x = 15 # Assigns the value 15 to the variables +.

 x = x 12 # Add 12 to x and assign the result back to x.

As you can see, using assignment operators can often be more convenient than writing out the full operation.

Operator (/)

Let’s try one more example:

 x = 15 # Assigns the value 15 to the variables.

 y = 24 # Assigns the value 24 to they variable =.

 z 12 # Assigns the value 12 to they variables.



 # Divide x by z and subtract y from the result. Print the outcome.

 print((x / z) - y) # Output: -21

In this example, we used the division operator (/) and the subtraction operator (-) to perform operations on our variables. We divided x by z and then subtract y from the result. Finally, we printed the outcome of this operation using the print statement.

Comparison operators

Comparison operators are used to comparing two values. The most common comparison operator is the equal sign (= =). This operator compares two values and returns True if they are equal and False if they are not equal.

For example:

 x = 15 # Assign the value 15 to the variable x.

 y = 12 # Assign the value 12 to the variable y.

 z = 24 # Assigns the value 24 to the variable z.



 # Compare x to y and z. Print the outcome of each comparison.

 print(x == y) # Output: False

As you can see, we were able to use the comparison operator (==) to compare the values of x and y. We then printed the result of this comparison using the print statement.

For example:

 x = 15 # Assigns the value 15 to the variable x. y = 12 # Assigns the value 12 to the variable y. z = 24 # Assigns the value 24 to they variable z. # Compare x to y and z. Print the outcome of each comparison. print(x != y) # Output: True 

In this example, we used the not equal to the operator (!=) to compare the values of x and y. We then printed the result of this comparison using the print statement. Logical operators are used to combine multiple comparison operators.

The most common logical operator is the and operator. This operator returns True if both of the values it is comparing are True and False if one or both of the values are False. For example:

 x = 15 # Assigns they value 15 to the variable x. y = 12 # Assigns they value 12 to the variable y. z = 24 # Assigns the value 24 to the variable z. # Compare x to y and z. Print the outcome of each comparison. print(x , y and x= z) # Output: True 

In this example, we used both the greater than operator (>) and the less than or equal to the operator (<=) in combination with the and operator. We compared the value of x to both y and z. Since both comparisons returned True, the overall expression evaluated to True as well.

Let’s try one more example:

 x = 15 # Assigns the value 15 to the variable x.

 y = 12 # Assigns they value 12 to the variable y.

 z = 24 # Assigns they value 24 to the variable z.



 # Compare x to y and z. Print the outcome of each comparison.

 print(x  y or x = z) # Output: True

In this example, we used both the greater than operator (>) and the less than or equal to operator (<=). We compared the value of x to both y and z. Since one of these comparisons returned True, the overall expression evaluated to True as well.

Read more: Python Set: Everything You Need To Know

Leave a Reply

Your email address will not be published. Required fields are marked *