Home >>Python Tutorial >Learn Python Operators

Learn Python Operators

The Python Operators

Operators are basically a type of function in this programming language that is used to perform a different number of operations on the different kinds of values and variables that are used in this programming language. In this tutorial, we will also be looking at the Python conditional operator. The operators that are used in this programming language are divided or categorized into a number of different groups. Those groups are mentioned below.
  • Bitwise operators
  • Arithmetic operators
  • Membership operators
  • Assignment operators
  • Identity operators
  • Comparison operators
  • Logical operators
These are all the seven different types of operators that are used in this programming language.

The Python Arithmetic Operators

The Python arithmetic operators are used to perform a number of different common mathematical operations. These Python operators are used with different numeric values. And some of the Python operators examples are mentioned below.
Operators Name Example
+ Addition x + y
- Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor Division x // y

The Python Assignment Operators

The assignment operators in this programming language are often used to assign different values to a number of different variables. And some of the Python operators examples are mentioned below.
Operators Example Same As
= x = 5 x = 5
+= x += 2 x = x + 2
-= x -= 2 x = x – 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
%= x %= 2 x = x % 2
//= x //= 2 x = x // 2
**= x **= 2 x = x ** 2
&= x &= 2 x = x & 2
I = x I = 2 x = x I 2
^ = x ^= 2 x = x ^ 2
>> = x >> = 2 x = x >> 2
<< = x<< = 2 x = x << 2

The Python Comparison Operators

Comparison operators can basically be defined as the type of operators that are used in this programming language to compare any two particular values. And some of the Python operators examples are mentioned below.
Operator Name Example
== Equal x == y
!= Not Equal x != y
> Greater Than x > y
< Less Than x < y
>= Greater Than or Equal To x >= y
<= Less Than or Equal To x <= y

The Python Logical Operators

As a developer, while using this programming language if you wish to combine any type of conditional statements then to do that you must use the function of logical operators. And some of the Python operators examples are mentioned below.
Operator Description Example Try It
and Returns true if both statements are true x < 3 and x < 10
or Returns true if one of the statements is true x < 5 or x < 3
not Reverse the result, return false if the result is true not( x < 3 and x < 10)

The Python Identity Operators

As a developer in this programming language, if you wish to compare any particular objects to check if they are the same objects which further have the same memory location and not to see if they are equal then you can use the function of the Identity Operators. And some of the Python operators examples are mentioned below.  
Operator Description Example
is Returns true if both the variables are the same object x is y
is not Returns true if both the variables are not the same object x is not y
 

The Python Membership Operators

If you wish to test whether or not the sequence is presented in an object then you should use the Membership Operators. And some of the Python operators examples are mentioned below.  
Operator Description Example
in Returns true if the sequence with the specified value is present in the object x in y
not in Returns true if the sequence with the specified value is not present in the object x not in y

The Python Bitwise Operators

If you wish to combine any kind of conditional statements while using this programming language then you should use logical operators.
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
I OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of the two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bit fall off
  With this, we finish the Python operator module in this Python tutorial. You can learn Python numbers visit other pages


No Sidebar ads