Home / Python / Python Basic Operators And How To Use Them

Python Basic Operators And How To Use Them

In this lesson of Python Programming Language, we will learn Basic Operators in Python Programming Language their types and how we can use them in our code.

Now we will discuss each of the above topics in detail to learn to understand them easily. I am sure after going through this lesson you will be able to implement these python operators in your code easily.

Python Basic Operators

Python operators are special symbols dedicated to performing specific tasks in a programming language. These operators consist of two parts one is the operator symbol itself and the other is operand and the final result is called output.  See the illustration below.

In the above illustration, both side 20 and 20 are called the operands and the symbol of multiplication( * ) is called an operator and the final result which is 400 is called output.

Now we will discuss each of the above operators in detail.

Python Arithmetic Operators

These operators are used to perform the operation like addition, subtraction, multiplication, and division. See the table below to understand more easily.

Symbol of Arithmetic Operators Name and Its FunctionsDemo Illustration
+ Addition:
It uses to add two values.
10 + 10 = 20
-Subtraction:
It uses to subtract one values from another value.
10 - 5 = 5
*Multiplication:
It uses to multiply values with each other.
5 * 5 = 25
/Division:
It always perform division from left operand to right one.
10 / 5 = 2
%Modulus:
It divides the left operand with right operand and return remainder in the output.
12 / 5 = 2.4
** Exponent:
It multiply the right operand to the power of left operand.
4**3 = 32

Example Of Python Arithmetic Operators

We the above program code executes the following results shows

Python Comparison Operators

These operators are used to compare the values and it gives us result back to us in the form of True or False. These operators are also called “Relational Operators“. See the table below to understand more easily.

Symbol of Comparison OperatorsName and Its FunctionsDemo Illustration
!ERROR! unexpected operator '='Equal To: Return True if values on both sides are equal otherwise return False.a == b
!=Not equal to: Return True if values on both sides are not equal otherwise return Falsea !=b
<>Not equal to: Work same like the above not equal operator(a <> b ) work same like a != b
>Greater than: If the value on the left side is bigger than the value on the right side then it return True Otherwise return False.a > b
<Less Than: If the value on the left side is smaller than the value on the right side it returns True otherwise False. a < b
>=Greater Than Or Equal To: If the value on the left side is greater or equal to the value on the right side it returns True otherwise returns False. a >= b
<= Smaller Than Or Equal To: If the value on the left side is smaller or equal to the value on the right side it returns True otherwise returns False. a <= b
Recommended:  Python Loops: How To Use Them

Example Of Python Comparison Operators

After execution of the above code the following results shows.

Python Assignment Operators

When you need to assign the value of one operand to other operand or variable you need to use assignment operators.  There are different types of assignment operators. See the table below to understand more easily.

Symbol of Assignment OperatorsName and Its FunctionsDemo Illustration
=Assignment: It assigns the value of right operand to the left operand.z = x + y
It assigns the value of ( x + y ) to z
+=Add AND: It first add value of right operand to the left operand and then assign added value to the operand left side of the assignment operator.z+= x is equal to
z = z + x
-= Subtract AND: It first subtract the value of right operand from left operand and then assign subtracted value to the operand left side of the assignment operator.z-= x is equal to
z = z - x
*= Multiply AND: It first multiply right operand with left operand and assigns the multiplied value to the operand left side of the assignment operator. z*= x is equal to
z = z * x
/=Divide AND: It divides the left operand with the right operand and assigns the divided value to the operand left side of the assignment operator. z/= x is equal to
z = z / x
%= Modulus AND: It returns the remainder after division of two operands. z %= x is equal to
z = z % x
**= Exponent AND: It performs exponential power calculation and store the result to operand left side of the assignment operator. z**= x is equal to
z = z** x
//=Floor Division: This operators do floor division on the operands and assigns value to the left operand.z //= x is equivalent to z = z // x
Recommended:  IF,ELIF,ELSE Statement in Python Programming Language

Example Of Python Assignment Operators

After execution of the above code, the following results will be shown.

Python Logical Operators

Python Programming Language supports some of the logical operators which are as follows:

Logical OperatorsFunctionsDemo Illustration
and Logical ANDThis operator returns True if only both the operands are true. ( x and y ) is True
or Logical ORThis operator returns True if any one of the both operands are non-zero then it becomes true.( x or y ) is True
not Logical NOTThis operator is use to reverse the current state of the operands. Not(a and b) is false

Example Of Python Logical Operators

After execution of the above code, the following results will be shown.

Python Bitwise Operators

Another type of operators that work in Python Programming Language is called Bitwise Operators. They work on bits and perform their actions in a bit by bit sequence. We will take some values for better understanding.

Let’s assume x = 10 which is  0000 1010 in binary and y = 4 which is 0000 0100

Now we do calculation using Python Bitwise Operators.

x = 0000 1010

y = 0000 0100

——————-

Symbol of Bitwise OperatorsName and Its FunctionsDemo Illustration
&Binary AND: It gets the bit to the results if it exists in both operands.x& y = 0 (0000 0000)
|Binary OR: It gets the bit in the result
if it exists in either operands
x | y = 14 (0000 1110)
~Binary NOT: It's function is to reverse the value.
It is also called Binary one's complement
~x = -11 (1111 0101)
^Binary XOR: It gets the bit if it is available in one operand but not both.x ^ y = 14 (0000 1110)
>>Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand.x>> 2 = 2 (0000 0010)
<<Binary Left Shift: It's main function is that The left operands value is moved left by the number of bits specified by the right operand.x<< 2 = 40 (0010 1000)
Recommended:  Python Nested if Statements With Example

Python Membership Operators

In Python Programming Language membership operators are used to checking whether a value or variable present in a  sequence in lists, strings or in tuples. There are two membership operators which are as follows:

Symbol of Membership OperatorsFunctionsDemo Illustration
inIt evaluates and If a value of variable found in a sequence it is True otherwise False. 10 in x
not inIt evaluates and check If a value of variable not found in a sequence it is
True otherwise False.
10 not in x

Example of Python Membership Operators

After execution following result will be shown.

Python Identity Operators

The Python Identity Operators are used in the case when we want to find out if two values or variable placed on the same memory address or location. Remember if two values of the variable are equal it does not mean they are identical.

SymbolFunctionDemo Illustration
isIt returns True if the operands are identical. x is y here it will true is x is identical to y
is notIt returns True if the operands are not identical x is not y here it will true is x is not identical to y

Example of Python Identity Operators

After execution following result will be shown.
 

About Jack Sparrow

Avatar
Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Check Also

Nested-if-statement

Python Nested if Statements With Example

Python Nested if Statements With Example In this lesson, I will teach you about Python …

2 comments

  1. Avatar

    What IDE should I use to type python?

Leave a Reply

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