Python Mathematical Operators
Python provides a range of mathematical operators for performing arithmetic operations, comparisons, and more. These operators can be used with various data types, including integers, floating-point numbers, and complex numbers.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
1.1 Addition (+
)
Adds two numbers.
Example:
Output:
1.2 Subtraction (-
)
Subtracts one number from another.
Example:
Output:
1.3 Multiplication (*
)
Multiplies two numbers.
Example:
Output:
1.4 Division (/
)
Divides one number by another, resulting in a floating-point number.
Example:
Output:
1.5 Floor Division (//
)
Divides one number by another and returns the largest integer less than or equal to the result.
Example:
Output:
1.6 Modulus (%
)
Returns the remainder of the division.
Example:
Output:
1.7 Exponentiation (**
)
Raises one number to the power of another.
Example:
Output:
2. Comparison Operators
Comparison operators are used to compare values and return boolean results.
2.1 Equal to (==
)
Checks if two values are equal.
Example:
Output:
2.2 Not equal to (!=
)
Checks if two values are not equal.
Example:
Output:
2.3 Greater than (>
)
Checks if one value is greater than another.
Example:
Output:
2.4 Less than (<
)
Checks if one value is less than another.
Example:
Output:
2.5 Greater than or equal to (>=
)
Checks if one value is greater than or equal to another.
Example:
Output:
2.6 Less than or equal to (<=
)
Checks if one value is less than or equal to another.
Example:
Output:
3. Assignment Operators
Assignment operators are used to assign values to variables with operations.
3.1 Addition Assignment (+=
)
Adds and assigns the result to the variable.
Example:
Output:
3.2 Subtraction Assignment (-=
)
Subtracts and assigns the result to the variable.
Example:
Output:
3.3 Multiplication Assignment (*=
)
Multiplies and assigns the result to the variable.
Example:
Output:
3.4 Division Assignment (/=
)
Divides and assigns the result to the variable.
Example:
Output:
3.5 Floor Division Assignment (//=
)
Floor divides and assigns the result to the variable.
Example:
Output:
3.6 Modulus Assignment (%=
)
Applies modulus and assigns the result to the variable.
Example:
Output:
3.7 Exponentiation Assignment (**=
)
Raises to the power and assigns the result to the variable.
Example:
Output:
4. Bitwise Operators
Bitwise operators perform operations on the binary representations of integers.
4.1 AND (&
)
Performs a bitwise AND operation.
Example:
Output:
4.2 OR (|
)
Performs a bitwise OR operation.
Example:
Output:
4.3 XOR (^
)
Performs a bitwise XOR operation.
Example:
Output:
4.4 NOT (~
)
Performs a bitwise NOT operation (one's complement).
Example:
Output:
4.5 Left Shift (<<
)
Shifts the bits of a number to the left.
Example:
Output:
4.6 Right Shift (>>
)
Shifts the bits of a number to the right.
Example:
Output:
Conclusion
Python provides a rich set of mathematical operators for performing arithmetic, comparison, assignment, and bitwise operations. Understanding these operators and their usage can greatly enhance your ability to manipulate and analyze data in Python.
By practicing the examples provided, you can gain a deeper understanding of how to use these operators effectively in your programming tasks.