Operators in Python are symbols that instruct the interpreter to perform specific operations. They are fundamental tools in any programming language, allowing you to manipulate data, evaluate conditions, and perform various tasks within your code. Python categorizes operators into several groups, including Arithmetic, Relational, Logical, and Bitwise operators. Let's dive deeper into these operator groups and their functionality.
- The
=
sign is the assignment operator, used to assign a value to a variable. - You can assign multiple values in one line, such as
x, y = 5, 10
.
Arithmetic operators are used to perform basic mathematical operations:
- Addition (
+
): Adds two values. - Subtraction (
-
): Subtracts one value from another. - Multiplication (
*
): Multiplies two values. - Division (
/
): Divides one value by another. - Power (
**
): Raises a number to the power of another, e.g.,2 ** 3
equals 8. - Floor Division (
//
): Divides two numbers and rounds down to the nearest whole number. - Modulo (
%
): Returns the remainder of a division operation.
-
An expression is a combination of values, variables, operators, and calls to functions that the interpreter evaluates and computes to produce another value. For example,
3 + 5
orx * y
. -
Unary vs. Binary Operators:
- Unary Operators: Operate on a single operand (e.g.,
-x
). - Binary Operators: Operate on two operands (e.g.,
x + y
).
- Unary Operators: Operate on a single operand (e.g.,
Relational operators compare two values and return a Boolean result (True
or False
).
- Equal to (
==
): Checks if two values are equal. - Not equal to (
!=
): Checks if two values are not equal. - Greater than (
>
): Checks if the left value is greater than the right. - Less than (
<
): Checks if the left value is less than the right. - Greater than or equal to (
>=
): Checks if the left value is greater than or equal to the right. - Less than or equal to (
<=
): Checks if the left value is less than or equal to the right.
Important Note: The assignment operator =
is different from the equality operator ==
. The former assigns a value to a variable, while the latter checks if two values are equal.
Logical operators are used to combine conditional statements:
- And (
and
): ReturnsTrue
if both statements are true. - Or (
or
): ReturnsTrue
if at least one statement is true. - Not (
not
): Reverses the Boolean value of the operand.
Combination operators, also known as compound assignment operators, combine two operations into one. For example:
x += 1
is equivalent tox = x + 1
.- Other examples include
-=
,*=
,/=
,%=
,**=
, and//=
.
String operators manipulate text:
- Concatenation (
+
): Joins two strings together into one (e.g.,'Hello' + 'World'
results in'HelloWorld'
). - Repetition (
*
): Repeats a string a specified number of times (e.g.,'A' * 3
results in'AAA'
). - Slicing (
[]
): Extracts parts of a string.- Basic Slice (
[x]
): Accesses the character at positionx
. - Range Slice (
[x:y]
): Extracts a substring from positionx
toy-1
.
- Basic Slice (
Bitwise operators perform operations on binary numbers at the bit level:
- Bitwise AND (
&
): Compares each bit of two numbers and returns1
if both bits are1
. - Bitwise OR (
|
): Compares each bit of two numbers and returns1
if at least one of the bits is1
. - Bitwise NOT (
~
): Inverts all the bits of the number. - Bitwise XOR (
^
): Compares each bit of two numbers and returns1
if the bits are different. - Shift Right (
>>
): Shifts the bits of the number to the right. - Shift Left (
<<
): Shifts the bits of the number to the left.
Understanding these operators is crucial for performing various tasks in Python, from simple arithmetic to more complex data manipulations and logic operations.