Table of contents

Operations: A Comprehensive Guide

Understanding basic operations in Python is crucial for any programmer. These operations form the foundation of more complex logic and algorithms. In this guide, we’ll explore various basic operations in Python, from arithmetic and assignment to comparison and logical operations.

Arithmetic operations are used to perform mathematical calculations. Python supports several arithmetic operators:

Operator Description Example  Output
+ Addition  5 + 2
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus 5 % 2 1
** Exponentiation 5 ** 2 25

Example:

a = 5 b = 2 print(a + b) # Addition, Output: 7 print(a - b) # Subtraction, Output: 3 print(a * b) # Multiplication, Output: 10 print(a / b) # Division, Output: 2.5 print(a // b) # Floor Division, Output: 2 print(a % b) # Modulus, Output: 1 print(a ** b) # Exponentiation, Output: 25

Assignment operators are used to assign values to variables. Python supports various assignment operators:

Operator  Example Equivalent to
= x = 5 Assign 5 to x
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
//= x //= 3 x = x // 3
%= x %= 3  x = x % 3
**= x **= 3 x = x ** 3

Example:

x = 5 x += 3 # Equivalent to x = x + 3 print(x) # Output: 8 x -= 2 # Equivalent to x = x - 2 print(x) # Output: 6 x *= 4 # Equivalent to x = x * 4 print(x) # Output: 24 x /= 2 # Equivalent to x = x / 2 print(x) # Output: 12.0

Comparison operators are used to compare two values. They return a boolean result (True or False).

Operator Description Example Output
== Equal to 5 == 2  False
!= Not equal to 5 != 2 True
> Greater than 5 > 2 True
< Less than 5 < 2 False
>= Greater than or equal to 5 >= 2 True
<= Less than or equal to 5 <= 2 False

Example:

a = 5 b = 3 print(a == b) # Equal to, Output: False print(a != b) # Not equal to, Output: True print(a > b) # Greater than, Output: True print(a < b) # Less than, Output: False print(a >= b) # Greater than or equal to, Output: True print(a <= b) # Less than or equal to, Output: False

Logical operators are used to combine conditional statements. Python supports and, or, and not operators.

Operator Description Example Output
and Logical AND True and False False
or Logical OR True or False True
not Logical NOT not True False

Example:

a = True b = False print(a and b) # Logical AND, Output: False print(a or b) # Logical OR, Output: True print(not a) # Logical NOT, Output: False

Bitwise operators perform operations on the binary representations of integers. Python supports & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).

Operator Description Example Output
& Bitwise AND 5 & 3 1
| Bitwise OR 5 | 3 7
^ Bitwise XOR 5 ^ 3 6
~ Bitwise NOT ~5 -6
<< Left shift 5 << 1 10
>> Right shift 5 >> 1 2

Example:

a = 5 # Binary: 0101 b = 3 # Binary: 0011 print(a & b) # Bitwise AND, Output: 1 (Binary: 0001) print(a | b) # Bitwise OR, Output: 7 (Binary: 0111) print(a ^ b) # Bitwise XOR, Output: 6 (Binary: 0110) print(~a) # Bitwise NOT, Output: -6 (Binary: 1010) print(a << 1) # Left shift, Output: 10 (Binary: 1010) print(a >> 1) # Right shift, Output: 2 (Binary: 0010)

Create a simple calculator that performs basic arithmetic operations.

# Basic Calculator def calculator(a, b, operation): if operation == "add": return a + b elif operation == "subtract": return a - b elif operation == "multiply": return a * b elif operation == "divide": return a / b else: return "Invalid operation" # User input num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) op = input("Enter operation (add, subtract, multiply, divide): ") # Calculate and print result result = calculator(num1, num2, op) print("Result:", result)

Use logical operators to check if a number is within a specified range.

# Check if a number is within range def is_within_range(num, lower, upper): return lower <= num <= upper # User input number = int(input("Enter a number: ")) low = int(input("Enter the lower bound: ")) up = int(input("Enter the upper bound: ")) # Check and print result if is_within_range(number, low, up): print(f"{number} is within the range {low} to {up}.") else: print(f"{number} is not within the range {low} to {up}.")

Write a program to check if a number is even or odd.

# Check if a number is even or odd num = int(input("Enter a number: ")) if num % 2 == 0: print(f"{num} is even.") else: print(f"{num} is odd.")

Write a program to check if a year is a leap year.

# Check if a year is a leap year year = int(input("Enter a year: ")) if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.")

In this comprehensive guide, we've covered the basics of Python operations, including arithmetic, assignment, comparison, logical, and bitwise operations. These operations are fundamental to any programming task and are essential for writing more complex code. Make sure to practice these concepts with the provided examples and exercises to strengthen your understanding. Happy coding!