Introduction
In this article, I will explain the basic operators in Python.
Definition
An operator is a symbol which represents some operations that can be performed on data. There are different types of operators in Python. There are:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations.
Syntax
- Variable 1 arithmetic operator variable 2
The arithmetic operators and their meaning are given in the below table:
operators
|
operations
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo division
|
Example
- x = 10
- y = 2
-
- print(x + y)
- print(x - y)
- print(x * y)
- print(x / y)
- print(x % y)
Output
Relational operators
Relational operators are used to compare two or more operans. It evaluates whether the given expression is true or false.
operators
|
operations
|
<
|
is less than
|
<=
|
is less than or equal
|
>
|
is greater t
|
>=
|
is greater than or equal
|
==
|
is equal to
|
!=
|
is not equal to
|
Syntax
- Variable 1 relational operator variable 2
Example
- x = 20
- y = 10
-
- print(x == y)
- print(x != y)
- print(x > y)
- print(x < y)
- print(x >= y)
- print(x <= y)
Output
Logical operators
They are used to combine the result of two or more conditions.
operators
|
operations
|
&&
|
And
|
||
|
Or
|
!
|
Not
|
Syntax
- Variable 1 logical operator variable 2
Example
- x = 8
-
- print(x > 3 and x < 10)
- print(x > 3 or x < 1)
- print(not(x > 3 and x < 11))
Output
Assignment operators
Assignment operators have to assign a value or an expression or a value of a variable to another variable. Assignment operators use arithmetic operators (+, -, *, /) along with assignment operator (=).
operators
|
Example
|
Meaning
|
+=
|
X+=Y
|
X=X+Y
|
-=
|
X-=Y
|
X=X-Y
|
*=
|
X*=Y
|
X=X*Y
|
/=
|
X/=Y
|
X=X/Y
|
%=
|
X%=Y
|
X=X%Y
|
Syntax
- Variable 1 assignment operator variable 2
Example
- a = 10
- b = 10
- c = 10
- d = 10
- e = 10
-
- a += 20
- b -= 20
- c *= 20
- d /= 20
- e %= 20
-
- print(a)
- print(b)
- print(c)
- print(d)
- print(e)
Output
Conclusion
In this article, we have seen basic operators in Python. I hope this article was useful to you. Thanks for reading!