Basic Python Operations and Expressions

In this article, we’ll dive into the fundamental operations and expressions in Python. Understanding these basics will enable you to perform a wide range of tasks and build more complex programs.

Arithmetic Operations

Arithmetic operations are used to perform mathematical calculations.

  1. Addition (+): Adds two numbers.
  2. Subtraction (-): Subtracts one number from another.
  3. Multiplication (*): Multiplies two numbers.
  4. Division (/): Divides one number by another, returns a float.
  5. Floor Division (//): Divides one number by another, returns an integer.
  6. Modulus (%): Returns the remainder of a division.
  7. Exponentiation (**): Raises one number to the power of another.

Examples:

# Arithmetic operations
a = 10
b = 3

print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000

Comparison Operators

Comparison operators compare two values and return a boolean result (True or False).

  1. Equal to (==): Checks if two values are equal.
  2. Not equal to (!=): Checks if two values are not equal.
  3. Greater than (>): Checks if one value is greater than another.
  4. Less than (<): Checks if one value is less than another.
  5. Greater than or equal to (>=): Checks if one value is greater than or equal to another.
  6. Less than or equal to (<=): Checks if one value is less than or equal to another.

Examples:

# Comparison operators
a = 10
b = 3

print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False

Logical Operators

Logical operators combine multiple boolean expressions and return True or False.

  1. and: Returns True if both expressions are True.
  2. or: Returns True if at least one expression is True.
  3. not: Returns the opposite of the expression.

Examples:

# Logical operators
a = True
b = False

print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False

Assignment Operators

Assignment operators are used to assign values to variables.

  1. =: Assigns a value to a variable.
  2. +=: Adds and assigns a value.
  3. -=: Subtracts and assigns a value.
  4. *=: Multiplies and assigns a value.
  5. /=: Divides and assigns a value.
  6. //=: Floor divides and assigns a value.
  7. %=: Takes modulus and assigns a value.
  8. **=: Exponentiates and assigns a value.

Examples:

# Assignment operators
a = 5

a += 2 # Equivalent to a = a + 2
print(a) # Output: 7

a -= 2 # Equivalent to a = a - 2
print(a) # Output: 5

a *= 3 # Equivalent to a = a * 3
print(a) # Output: 15

a /= 3 # Equivalent to a = a / 3
print(a) # Output: 5.0

a //= 2 # Equivalent to a = a // 2
print(a) # Output: 2.0

a %= 2 # Equivalent to a = a % 2
print(a) # Output: 0.0

a **= 3 # Equivalent to a = a ** 3
print(a) # Output: 0.0

Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

  1. AND (&): Sets each bit to 1 if both bits are 1.
  2. OR (|): Sets each bit to 1 if one of the bits is 1.
  3. XOR (^): Sets each bit to 1 if only one of the bits is 1.
  4. NOT (~): Inverts all the bits.
  5. Left Shift (<<): Shifts bits to the left.
  6. Right Shift (>>): Shifts bits to the right.

Examples:

# Bitwise operators
a = 10 # In binary: 1010
b = 4 # In binary: 0100

print(a & b) # Output: 0 (binary: 0000)
print(a | b) # Output: 14 (binary: 1110)
print(a ^ b) # Output: 14 (binary: 1110)
print(~a) # Output: -11 (binary: ...1111110101) (two's complement)
print(a << 1) # Output: 20 (binary: 10100)
print(a >> 1) # Output: 5 (binary: 0101)

Membership Operators

Membership operators test whether a value is part of a sequence such as a list, tuple, or string.

  1. in: Returns True if the value is in the sequence.
  2. not in: Returns True if the value is not in the sequence.

Examples:

# Membership operators
fruits = ["apple", "banana", "cherry"]

print("banana" in fruits) # Output: True
print("grape" not in fruits) # Output: True

Identity Operators

Identity operators compare the memory locations of two objects.

  1. is: Returns True if both variables point to the same object.
  2. is not: Returns True if both variables point to different objects.

Examples:

# Identity operators
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b) # Output: True (b points to the same object as a)
print(a is c) # Output: False (c is a different object with the same content)
print(a is not c) # Output: True

Real-Time Example: Basic Calculator

Let’s create a simple calculator that can perform basic arithmetic operations.

# Basic calculator

# Getting user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Performing operations
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
print("Floor Division:", num1 // num2)
print("Modulus:", num1 % num2)
print("Exponentiation:", num1 ** num2)

Exercises

  1. Write a program that takes three numbers as input and prints the largest one.
  2. Create a program that checks if a given string is present in a list of strings.
  3. Write a script that takes a number as input and prints its binary, octal, and hexadecimal representations.

Conclusion

In this article, we’ve explored basic Python operations and expressions, including arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators. Mastering these fundamentals will help you write more efficient and effective Python code. In the next article, we’ll cover control flow, including if statements and loops. Happy coding!

Data AI Admin

Senior AI Lead having overall Experience of 10+ years in IT, Data Science, Machine Learning, AI and related fields.

Related Posts

Exploring Different Pandas File Formats

Data handling and manipulation are fundamental in data analysis. Pandas, a powerful Python library, supports various file formats for reading, writing, and converting data. Understanding these formats and their benefits…

Read more

Making Beautiful Plots with Seaborn in Python

Welcome to the sixth tutorial in our series on data analysis with Python! In this article, we’ll introduce you to Seaborn, a powerful Python visualization library built on top of…

Read more

Leave a Reply

You Missed

Exploring Different Pandas File Formats

  • June 28, 2024
Exploring Different Pandas File Formats

Making Beautiful Plots with Seaborn in Python

  • June 28, 2024
Making Beautiful Plots with Seaborn in Python

Mastering Data Visualization with Matplotlib

  • June 28, 2024
Mastering Data Visualization with Matplotlib

Data Cleaning and Preprocessing with Pandas

  • June 27, 2024
Data Cleaning and Preprocessing with Pandas

Exploring Data with Pandas: Series and DataFrames

  • June 27, 2024
Exploring Data with Pandas: Series and DataFrames

NumPy : Basic Operations and Arrays

  • June 27, 2024
NumPy : Basic Operations and Arrays