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)
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]
In this example, we have two lists,
numbers2. We perform addition, subtraction, multiplication, and division element-wise using list comprehension and the
zip() function. The resulting lists are stored in
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_listretrieves 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 + list2returns a new list that contains all elements from
list1followed by elements from
- Repetition: You can repeat a list by using the
*operator. For example,
my_list * 3creates a new list that repeats the elements of
- Length: The
len()function returns the number of elements in a list. For example,
len(my_list)gives you the length of
- Modifying Elements: You can modify elements in a list by assigning a new value to a specific index. For example,
my_list = 42assigns 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
- Removing Elements: You can remove elements from a list using various methods like
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,
- Searching: You can search for elements in a list using methods like
inoperator. For example,
my_list.index(5)returns the index of the first occurrence of the element 5 in
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)
Result: -10 Result: 0.5555555555555556 Result: 2.0
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.
- How to do Single Line for Loop in Python
- What is Len() in Python With Example
- What does V mean in Python?
- What is Async and Await in Python With Example
- For i in Range Python Example
- What is a decorator in Python With Example
- Can you do a for loop with a DataFrame in Python?
- What is Break Statement in Python With Example
- What is Name Error Value Error in Python?
- What Is Variable Length Argument In Python With Example?
- What is NumPy in Python with example?
- How to Use def in Python With Example