In this Python tutorial, we will explore how to perform basic arithmetic operations (such as addition, subtraction, multiplication, and division) on lists. Lists in Python can contain numeric values, and we can easily apply arithmetic operations to each element of the list. We will write a program that performs these operations and outputs the results. This exercise will help in understanding how to manipulate lists with arithmetic functions.
# List of numbers
numbers = [10, 20, 30, 40, 50]
# Performing arithmetic operations
addition_result = sum(numbers) # Sum of all elements in the list
subtraction_result = [num - 5 for num in numbers] # Subtract 5 from each number
multiplication_result = [num * 2 for num in numbers] # Multiply each number by 2
division_result = [num / 10 for num in numbers] # Divide each number by 10
# Output the results
print("Sum of numbers:", addition_result)
print("Subtraction (each number - 5):", subtraction_result)
print("Multiplication (each number * 2):", multiplication_result)
print("Division (each number / 10):", division_result)
Sum of numbers: 150
Subtraction (each number - 5): [5, 15, 25, 35, 45]
Multiplication (each number * 2): [20, 40, 60, 80, 100]
Division (each number / 10): [1.0, 2.0, 3.0, 4.0, 5.0]
[10, 20, 30, 40, 50]
.sum()
function to find the sum of all the numbers in the list.This code demonstrates how to perform basic arithmetic operations on each element of a list in Python using built-in functions and list comprehensions.