Python Operators – Free Python Tutorials

Today in this tutorial we will learn about Python operators. Like all the other programming languages, Python too has certain python operators to carry out various tasks or operations. These python operators work in a similar way to the general conventions that some of us might be aware of. These operators are:

  1. Arithmetic Operators
  2. Relational Operators or Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Special Operators

  1. Arithmetic Operator:

These are the python operators that help in operations involving arithmetic calculations. Some of the arithmetic python operators are: 

  • + -> addition
  • – -> subtraction
  • * -> multiplication
  • / -> division
  • % -> modulo

Example:

>>>a= 20 #declaring a 
>>>b= 10 #declaring b
>>>print(a+b)
>>>print(a-b)
>>>print(a/b)
>>>print(a*b)
>>>print(a%b)

Output:

30 #output for addition
10 #output for subtraction
2.0 #output for division
200 #output for multiplication 
0 #output for modulo

Note: The division operator always yields an output in the floating data type.


Floor Division Operator:

It is denoted by two forward slashes i.e. //. The output can be an int value or a float value. When both the divisor and the dividend are int then we get the output as int, whereas when both the divisor and the dividend are float then we get output as float.

We can demonstrate the difference between a normal division operator and a floor division operator with the help of an example.

Example:>>>10/2  #using the normal division operator
Output:5.0 #output is in float

Example: >>>10.0//2 #using the floor division operator
Output:5 #output is in int

Through the above lines of code, we perform the arithmetic operation of division. First, we divide 10 by 2 and obtain the output as 5.0. Then in the next example, we divide We 10.0 by 2 and get the output as 5 without a floating point.


Exponent Operator or Power Operator:

This is denoted by 2 Asterix. It is simply used when we need to obtain the number raised by a certain value i.e. its power. 

Example:>>>2**2
Output:4

In the above example, we raise 2 to the power of two using the power operator.

-It can also be used for negative powers. 

Example:>>>10** -1
Output:0.1

Here in the above example, we raise 10 to one negative power using the power operator.


Addition operator:

This operator can be used for arithmetic operations like addition as we have seen in a previous example.

Note: String concatenation operator– It can also be used for concatenation of two words. However, taking a string and an integer will lead to an error in Python as both arguments must be string literals.

Example:>>>’cats’+’purr’
Output:catspurr

Here we concatenate the two strings ‘cats’ and ‘purr’ using the ‘+’ operator.

Example:>>>’cats’+10
Output:Error

In the above example, we try to concatenate a string ‘cats’ and int 10 using the ‘+’ operator. This gives an error as both must be string literals.


Multiplication operator:

This operator is used for multiplying two operands. 

Note: String multiplication operator– using the multiplication operator we can also obtain a string multiple times in an output.

Example: >>>’meow’*3
Output:‘meowmeowmeow’

In the program above we see the string has been printed three times.

Example: >>>’meow’*3.0
Output:Error

The program above gives an error as the first argument is a string whereas the second argument is a float but since only an integer value is expected there thus we get an error.

Leave a Comment

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