Python – Arithmetic and Logical Operations #
Python provides a range of operators that allow you to perform arithmetic and logical operations. These operators help in manipulating values, performing calculations, and making decisions.
1. Arithmetic Operations in Python #
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and more.
1.1 List of Arithmetic Operators #
Operator | Operation | Description |
---|---|---|
+ | Addition | Adds two numbers. |
- | Subtraction | Subtracts the second number from the first. |
* | Multiplication | Multiplies two numbers. |
/ | Division | Divides the first number by the second. |
% | Modulus | Returns the remainder of the division. |
** | Exponentiation | Raises the first number to the power of the second. |
// | Floor Division | Returns the integer result of division (ignores the remainder). |
1.2. Basic Arithmetic Examples #
# Arithmetic Operations
a = 10
b = 3
print("Addition:", a + b) # Output: 13
print("Subtraction:", a - b) # Output: 7
print("Multiplication:", a * b) # Output: 30
print("Division:", a / b) # Output: 3.333...
print("Modulus:", a % b) # Output: 1
print("Exponentiation:", a ** b) # Output: 1000
print("Floor Division:", a // b) # Output: 3
1.3. Division and Floor Division #
- Normal Division (
/
) gives the result in floating-point form, even if both numbers are integers. - Floor Division (
//
) returns the largest integer smaller than or equal to the division result.
Example: #
# Division and Floor Division
x = 17
y = 4
print("Division:", x / y) # Output: 4.25
print("Floor Division:", x // y) # Output: 4
2. Logical Operations in Python #
Logical operators are used to combine conditional statements and check conditions based on Boolean values (True
or False
).
2.1 List of Logical Operators #
Operator | Operation | Description |
---|---|---|
and | Logical AND | Returns True if both operands are true. |
or | Logical OR | Returns True if at least one operand is true. |
not | Logical NOT | Returns True if the operand is false, and False if true. |
2.2. Examples of Logical Operations #
# Logical Operations
x = True
y = False
print("x and y:", x and y) # Output: False (because one of them is False)
print("x or y:", x or y) # Output: True (because one of them is True)
print("not x:", not x) # Output: False (because x is True)
3. Comparison (Relational) Operators #
Comparison operators are used to compare two values. These operations return either True
or False
.
3.1 List of Comparison Operators #
Operator | Operation | Description |
---|---|---|
== | Equal to | Returns True if both values are equal. |
!= | Not equal to | Returns True if the values are not equal. |
> | Greater than | Returns True if the left value is greater. |
< | Less than | Returns True if the left value is smaller. |
>= | Greater than or equal | Returns True if the left value is greater or equal. |
<= | Less than or equal | Returns True if the left value is smaller or equal. |
3.2. Examples of Comparison Operators #
# Comparison Operations
x = 10
y = 20
print("x == y:", x == y) # Output: False
print("x != y:", x != y) # Output: True
print("x > y:", x > y) # Output: False
print("x < y:", x < y) # Output: True
print("x >= 10:", x >= 10) # Output: True
print("y <= 30:", y <= 30) # Output: True
4. Combining Logical and Comparison Operators #
You can combine logical and comparison operators to form more complex expressions.
Example: #
# Combining Logical and Comparison Operators
age = 25
has_license = True
# Check if the person can legally drive
can_drive = age >= 18 and has_license
print("Can the person drive?", can_drive) # Output: True
In this example, both conditions age >= 18
and has_license
must be True
for the person to be able to drive.
5. Arithmetic with Assignment Operators #
Assignment operators are used to assign values to variables. In Python, you can perform arithmetic operations and assign the result to a variable in a single step.
5.1 List of Arithmetic Assignment Operators #
Operator | Operation | Equivalent to |
---|---|---|
+= | Addition assignment | x = x + y |
-= | Subtraction assignment | x = x - y |
*= | Multiplication assignment | x = x * y |
/= | Division assignment | x = x / y |
%= | Modulus assignment | x = x % y |
**= | Exponentiation assignment | x = x ** y |
//= | Floor division assignment | x = x // y |
5.2. Examples of Assignment Operators #
# Arithmetic with Assignment Operators
x = 10
x += 5 # Equivalent to x = x + 5
print("x after += 5:", x) # Output: 15
x -= 3 # Equivalent to x = x - 3
print("x after -= 3:", x) # Output: 12
x *= 2 # Equivalent to x = x * 2
print("x after *= 2:", x) # Output: 24
x //= 4 # Equivalent to x = x // 4
print("x after //= 4:", x) # Output: 6
6. Bitwise Operators in Python #
Bitwise operators perform operations on the binary representations of numbers. These operators treat numbers as a sequence of bits and perform operations like AND, OR, and NOT at the bit level.
6.1 List of Bitwise Operators #
Operator | Operation | Description |
---|---|---|
& | Bitwise AND | Performs AND operation on each bit. |
` | ` | Bitwise OR |
^ | Bitwise XOR | Performs XOR operation on each bit. |
~ | Bitwise NOT | Flips all the bits (1 becomes 0, 0 becomes 1). |
<< | Left shift | Shifts bits to the left by a given number. |
>> | Right shift | Shifts bits to the right by a given number. |
6.2. Examples of Bitwise Operations #
# Bitwise Operations
x = 6 # In binary: 110
y = 3 # In binary: 011
print("x & y:", x & y) # Output: 2 (In binary: 010)
print("x | y:", x | y) # Output: 7 (In binary: 111)
print("x ^ y:", x ^ y) # Output: 5 (In binary: 101)
print("~x:", ~x) # Output: -7 (Inverts the bits)
print("x << 1:", x << 1) # Output: 12 (Shifts left: 110 -> 1100)
print("x >> 1:", x >> 1) # Output: 3 (Shifts right: 110 -> 11)
7. Conclusion #
Python provides a rich set of operators for performing arithmetic, logical, comparison, and bitwise operations. By mastering these operations, you can handle various mathematical calculations, logical decisions, and data manipulation tasks efficiently in your programs.