Python operators are symbols that perform operations on variables and values. They are used in various programming tasks, including arithmetic calculations, comparisons, logical operations, and more. Here's a detailed look at the different types of Python operators:
- Arithmetic Operators Arithmetic operators are used to perform mathematical operations.
-
+
(Addition): Adds two operands.
x = 5 + 3 x will be 8
-
-
(Subtraction): Subtracts the second operand from the first.
x = 5 - 3 x will be 2
-
(Multiplication): Multiplies two operands.
python x = 5 3 x will be 15
`` -
/
(Division): Divides the first operand by the second.`python x = 5 / 2 x will be 2.5 `
-
%
(Modulus): Returns the remainder of the division.`python x = 5 % 2 x will be 1 `
- `` (Exponentiation): Raises the first operand to the power of the second.
x = 2 3 x will be 8
-
//
(Floor Division): Divides the first operand by the second and returns the largest integer less than or equal to the result.
x = 5 // 2 x will be 2
- Comparison Operators
Comparison operators compare two values and return a boolean value (
True
orFalse
).
-
==
(Equal): ReturnsTrue
if both operands are equal.
x = (5 == 3) x will be False
-
!=
(Not Equal): ReturnsTrue
if operands are not equal.
x = (5 != 3) x will be True
-
>
(Greater Than): ReturnsTrue
if the left operand is greater than the right.
x = (5 > 3) x will be True
-
<
(Less Than): ReturnsTrue
if the left operand is less than the right.
x = (5 < 3) x will be False
-
>=
(Greater Than or Equal To): ReturnsTrue
if the left operand is greater than or equal to the right.
x = (5 >= 3) x will be True
-
<=
(Less Than or Equal To): ReturnsTrue
if the left operand is less than or equal to the right.
x = (5 <= 3) x will be False
- Logical Operators Logical operators are used to combine conditional statements.
-
and
: ReturnsTrue
if both statements are true.
x = (5 > 3 and 5 < 10) x will be True
-
or
: ReturnsTrue
if one of the statements is true.
x = (5 > 3 or 5 < 3) x will be True
-
not
: Reverses the result, returnsFalse
if the result is true.
x = not(5 > 3) x will be False
- Assignment Operators Assignment operators are used to assign values to variables.
-
=
: Assigns a value to a variable.
x = 5
-
+=
: Adds and assigns the result.
x += 3 x will be 8 if x was 5
-
-=
: Subtracts and assigns the result.
x -= 3 x will be 2 if x was 5
-
=
: Multiplies and assigns the result.
x = 3 x will be 15 if x was 5
-
/=
: Divides and assigns the result.
x /= 3 x will be 1.6667 if x was 5
-
%=
: Takes modulus and assigns the result.
python
x %= 3 x will be 2 if
https://www.youtube.com/watch?v=Zs8fxcqKro4
Top comments (0)