Python Example For Arithmetic Operations On Lists

Here’s an example of performing arithmetic operations on lists using Python:

# Example arithmetic operations on lists
numbers1 = [1, 2, 3, 4, 5]
numbers2 = [6, 7, 8, 9, 10]

# Addition
addition_result = [a + b for a, b in zip(numbers1, numbers2)]
print("Addition result:", addition_result)

# Subtraction
subtraction_result = [a - b for a, b in zip(numbers1, numbers2)]
print("Subtraction result:", subtraction_result)

# Multiplication
multiplication_result = [a * b for a, b in zip(numbers1, numbers2)]
print("Multiplication result:", multiplication_result)

# Division
division_result = [a / b for a, b in zip(numbers1, numbers2)]
print("Division result:", division_result)
Code language: Python (python)

Output:

Addition result: [7, 9, 11, 13, 15]
Subtraction result: [-5, -5, -5, -5, -5]
Multiplication result: [6, 14, 24, 36, 50]
Division result: [0.16666666666666666, 0.2857142857142857, 0.375, 0.4444444444444444, 0.5]Code language: CSS (css)

In this example, we have two lists, numbers1 and numbers2. We perform addition, subtraction, multiplication, and division element-wise using list comprehension and the zip() function. The resulting lists are stored in addition_result, subtraction_result, multiplication_result, and division_result, respectively. Finally, we print the results.

What are the operations on list in Python program?

In Python, you can perform various operations on lists. Here are some commonly used operations:

  • Accessing Elements: You can access individual elements in a list using indexing. For example, my_list[0] retrieves the first element of the list.
  • Slicing: Slicing allows you to extract a portion of a list. For example, my_list[1:4] returns a new list containing elements from index 1 to 3.
  • Concatenation: Lists can be concatenated using the + operator. For example, list1 + list2 returns a new list that contains all elements from list1 followed by elements from list2.
  • Repetition: You can repeat a list by using the * operator. For example, my_list * 3 creates a new list that repeats the elements of my_list three times.
  • Length: The len() function returns the number of elements in a list. For example, len(my_list) gives you the length of my_list.
  • Modifying Elements: You can modify elements in a list by assigning a new value to a specific index. For example, my_list[2] = 42 assigns the value 42 to the element at index 2.
  • Adding Elements: You can add elements to the end of a list using the append() method. For example, my_list.append(10) adds the element 10 to the end of my_list.
  • Removing Elements: You can remove elements from a list using various methods like pop(), remove(), or del. For example, my_list.pop(2) removes and returns the element at index 2.
  • Sorting: You can sort a list using the sort() method, which rearranges the elements in ascending order. For example, my_list.sort() sorts my_list in place.
  • Searching: You can search for elements in a list using methods like index() or the in operator. For example, my_list.index(5) returns the index of the first occurrence of the element 5 in my_list.

These are just some of the operations you can perform on lists in Python. The list data type is quite versatile and provides many more useful methods and functionalities.

How to do multiple arithmetic operations in Python?

To perform multiple arithmetic operations in Python, you can use the operators and apply them sequentially or within an expression. Here’s an example:

# Multiple arithmetic operations
a = 5
b = 3

# Addition, subtraction, multiplication
result = a + b - (a * b)
print("Result:", result)

# Division, exponentiation
result = a / b ** 2
print("Result:", result)

# Multiple operations within an expression
result = (a + b) * (a - b) / a
print("Result:", result)Code language: Python (python)

Output:

Result: -10
Result: 0.5555555555555556
Result: 2.0Code language: Python (python)

In this example, we perform multiple arithmetic operations using addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**). The results are stored in the result variable and printed out. Note that parentheses can be used to control the order of operations and to group operations together.

Read More;

  • I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment