Popular
Data Science
Technology
Finance
Management
Future Tech
Python operators are the symbols that represent the arithmetic or logical operations, and the value on which the operator is operating is called an operand. There are seven categories of operations in Python: arithmetic, assignment, comparison, logic, identity, membership, and boolean operators.
The operators in Python language are special symbols that enable the manipulation of variables and values through various operations. It can be categorised into several types, including arithmetic operators for basic mathematical calculations (such as addition and multiplication), comparison operators for evaluating relationships between values (like equal to or greater than), logical operators for combining conditional statements (such as AND and OR), and assignment.
There are seven categories of Python operators:
In Python language, the assignment operator is used to perform some basic operations on values and variables. These are the special symbols that carry out arithmetic, logical and bitwise computation in Python. The value the operator operates on is known as the Operand.
Operator | Description | Syntax |
= | They assign a value to a variable | x = 10 |
+= | This adds and assigns | x += (equiv to x =x +5) |
-= | It subtracts and assigns | x -= 3 (equiv to x= x-3) |
*= | It multiples and assigns | x *= 2 (equiv to x = x-3) |
/= | It divides and assigns | x/=4 (equiv. To x =x /4) |
//= | Floor division and assigns | x // =3 (equiv to x = x//3) |
%= | Its modules and assigns | x %=2 (equiv to x=x %2) |
**= | It exponentiation and assigns | x ** =2 (eqiv to x = x **2) |
&= | It bitwise and and assigns | x &= 1 (equiv to x = x & 1) |
^= | It bitwise XOR and assigns | x ^=4 (equiv to x = x^4) |
>>= | Right shift and assign | x >> 1 (eqiv. to x= x>>1) |
<<= | Left Shift and assigns | x << 1 (equiv to x = x<< 1) |
Program
Output
Python language arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division.
In Python, 3.x, the result of division is a floating-point, while in Python 2.x, the division of 2 integers was an integer. To obtain an integer result in Python, we can use 3.x floored (// integer).
Operator | Description | Syntax |
+ | This adds two operands | x + y |
– | It subtracts two operands | x-y |
* | It multiplies two operands | x * y |
/ | It divides the first operand by the second | x / y |
// | It divides the first operand by the second operator | x // y |
% | It returns the remainder when the first operand is divided by the second operator | x % y |
** | It returns first raised to power second | x ** y |
These operators compare the value of the left operand and the right operand and return either true or false. Let’s consider a equals 10 and b equals 20.
Operator | Description | Example |
== | These operators compare the values of two operands. If the two operands are equal, then it will return true or false. | (x == y) is not true |
!= | If the values of two operands are not equal, then the condition becomes true. | (a!=b) is true |
<> | If the values of two operands are not equal, then the condition becomes true. | (a<>b) is true. This is similar to the!=operator |
> | If the value of the left operand is greater than that of the right, the condition is true. | (a>b) is not true. |
>= | If the value of the left operand is less than or equal to that of the right, the condition is true. | (a <=b) is true. |
<= | If the value of the left operand is less than or equal to that of the right, the condition is true. | (a<=b) is true |
Program
Output
There are three operators in Python: Logical AND, Logical OR, and Logical Not. It is used to combine conditional statements.
Operator | Description | Syntax |
and | It is true if both the operands are true | x and y |
or | It is true if either of the operands is true | x or y |
not | It is true if the operand is false | not x |
In the C language, there are two identity operators: is and is not. Both are used to check if two values are located in the same part of the memory.
Operator | Example | Description |
is | x is y | It returns true if both variables are the same object |
is not | x is not y | It returns true if both variables are not the same object. |
This operator searches for the value in the specified sequence. If the value is found, it returns true; otherwise, it returns false. The not-in operator returns true if the value specified is not found in the given sequence.
Operator | Example | Description |
in | 5 in {1,2,3,4,5} | It returns true if it finds the corresponding value in a specified sequence. Otherwise, it returns false. |
not in | P5 not in {5,3,4,5} | It returns true if it does not find the corresponding value in a given sequence and true otherwise. |
These operators perform operations on binary numbers. If the number given is not binary, it is converted to binary internally, and an operation is performed. These operations are generally performed bit by bit.
Operator | Name | Description |
& | AND | It sets each bit to 1 if both bits are 1 |
| | OR | It sets each bit to 1 if one of the two bits is 1 |
^ | XOR | It sets each bit to 1 if one of the two bits is 1 |
~ | NOT | This covers all the bits. |
<< | Zero-fill left shift | This shift left by pushing zeroes in from the right and letting the leftmost bits fall off |
>> | Signed right shift | It shifts right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off. |
In this article, we learned about the Python Operators. They are among the fundamental tools used for performing all ranges of operations-from the very basic, like arithmetic and comparisons, to complex logical evaluations and memory management. There are several types of categorised and membership operators, each of which has different purposes in programming. The operator allows for writing more efficient and readable source codes, which makes implementation of decision-making processes easier or just manipulation of the data structure easier.
The DevOps Playbook
Simplify deployment with Docker containers.
Streamline development with modern practices.
Enhance efficiency with automated workflows.
Popular
Data Science
Technology
Finance
Management
Future Tech
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
© 2024 Hero Vired. All rights reserved