Python Primer — Module 5: Operations & Expressions
In this installment of our Python Primer series, we delve into the core of how Python processes data: through operations and expressions. Understanding these fundamental concepts is crucial for performing calculations, manipulating data, and making logical decisions in your code. We’ll break down arithmetic, logical, comparison, and bitwise operations, explaining each with practical examples.
Arithmetic Operations
Arithmetic operations are used for performing mathematical calculations. Here’s a look at the most common arithmetic operators in Python:
Basic Arithmetic Operators
# Addition
sum = 10 + 5
print("Sum:", sum) # Outputs: Sum: 15
# Subtraction
difference = 10 - 5
print("Difference:", difference) # Outputs: Difference: 5
# Multiplication
product = 10 * 5
print("Product:", product) # Outputs: Product: 50
# Division
quotient = 10 / 5
print("Quotient:", quotient) # Outputs: Quotient: 2.0- Division in Python always returns a float. If you want integer division (which truncates the decimal), you can use the
//operator.
Modulus and Exponentiation
# Modulus (remainder of division)
remainder = 10 % 3
print("Remainder:", remainder) # Outputs: Remainder: 1
# Exponentiation (power of)
power = 2 ** 3
print("Power:", power) # Outputs: Power: 8Operator Precedence
Operator precedence determines the order in which operations are evaluated. Python follows mathematical precedence rules, which you can manipulate with parentheses:
result = (10 + 5) * 3
print("Result:", result) # Outputs: Result: 45Logical Operations
Logical operators are used for conditional statements, returning either True or False. These are fundamental in control flow and decision-making processes in programming.
Logical Operators
# AND operator
result_and = (10 > 5) and (15 < 20)
print("AND Result:", result_and) # Outputs: AND Result: True
# OR operator
result_or = (10 < 5) or (15 < 20)
print("OR Result:", result_or) # Outputs: OR Result: True
# NOT operator
result_not = not(10 < 5)
print("NOT Result:", result_not) # Outputs: NOT Result: TrueComparison Operations
Comparison operators are used to compare two values. Let’s explore how these work with a few examples.
Comparison Operators
# Equals
is_equal = 10 == 10
print("Is Equal:", is_equal) # Outputs: Is Equal: True
# Not Equals
is_not_equal = 10 != 5
print("Is Not Equal:", is_not_equal) # Outputs: Is Not Equal: True
# Greater than and Less than
is_greater = 10 > 5
is_less = 10 < 15
print("Is Greater:", is_greater) # Outputs: Is Greater: True
print("Is Less:", is_less) # Outputs: Is Less: TrueBitwise Operations
Bitwise operators are used for manipulating individual bits of a data type. These are advanced operations that are essential when you’re dealing with low-level programming.
Bitwise Operators
# AND
bitwise_and = 5 & 3 # 0101 & 0011 = 0001
print("Bitwise AND:", bitwise_and) # Outputs: Bitwise AND: 1
# OR
bitwise_or = 5 | 3 # 0101 | 0011 = 0111
print("Bitwise OR:", bitwise_or) # Outputs: Bitwise OR: 7
# XOR (exclusive OR)
bitwise_xor = 5 ^ 3 # 0101 ^ 0011 = 0110
print("Bitwise XOR:", bitwise_xor) # Outputs: Bitwise XOR: 6
# NOT
bitwise_not = ~5 # ~0101 = -0110 (in two's complement)
print("Bitwise NOT:", bitwise_not) # Outputs: Bitwise NOT: -6
# Left Shift
left_shift = 5 << 1 # 0101 << 1 = 1010
print("Left Shift:", left_shift) # Outputs: Left Shift: 10
# Right Shift
right_shift = 5 >> 1 # 0101 >> 1 = 0010
print("Right Shift:", right_shift) # Outputs: Right Shift: 2Conclusion
Understanding operations and expressions in Python allows you to manipulate data effectively and make logical decisions in your code. This knowledge is the backbone of programming in Python, setting the stage for more complex algorithms and functions.
Get BitWiser’s stories in your inbox
Join Medium for free to get updates from this writer.
Stay tuned for the next module where we delve deeper into control structures to direct the flow of your Python programs.






