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.
- Addition (
+
): Adds two numbers. - Subtraction (
-
): Subtracts one number from another. - Multiplication (
*
): Multiplies two numbers. - Division (
/
): Divides one number by another, returns a float. - Floor Division (
//
): Divides one number by another, returns an integer. - Modulus (
%
): Returns the remainder of a division. - 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
).
- Equal to (
==
): Checks if two values are equal. - Not equal to (
!=
): Checks if two values are not equal. - Greater than (
>
): Checks if one value is greater than another. - Less than (
<
): Checks if one value is less than another. - Greater than or equal to (
>=
): Checks if one value is greater than or equal to another. - 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
.
- and: Returns
True
if both expressions areTrue
. - or: Returns
True
if at least one expression isTrue
. - 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.
=
: Assigns a value to a variable.+=
: Adds and assigns a value.-=
: Subtracts and assigns a value.*=
: Multiplies and assigns a value./=
: Divides and assigns a value.//=
: Floor divides and assigns a value.%=
: Takes modulus and assigns a value.**=
: 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.
- AND (
&
): Sets each bit to 1 if both bits are 1. - OR (
|
): Sets each bit to 1 if one of the bits is 1. - XOR (
^
): Sets each bit to 1 if only one of the bits is 1. - NOT (
~
): Inverts all the bits. - Left Shift (
<<
): Shifts bits to the left. - 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.
- in: Returns
True
if the value is in the sequence. - 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.
- is: Returns
True
if both variables point to the same object. - 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
- Write a program that takes three numbers as input and prints the largest one.
- Create a program that checks if a given string is present in a list of strings.
- 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!