;

Python Program to Perform Arithmetic Operations on Lists


Tutorialsrack 30/04/2020 Python

In this Python program, we will learn how to perform arithmetic operations on lists such as addition, subtraction, multiplication, division on lists. Here are some examples to perform these operations on the list.

Here is the source code of the program to perform the arithmetic operation on the list.

Program 1: Python Program to Perform Arithmetic Operations on Lists For using Loop with range() function

In this program, we will use the for loop to iterate each element of the lists.

Python Program to Perform Arithmetic Operations on Lists For using Loop with range() function
# Python Program to Perform Arithmetic Operations on Lists For using Loop with range() function

# Number List
NumList1 = [10, 20, 30]
NumList2 = [5, 2, 3]

print("List 1: ",NumList1)
print("List 2: ",NumList2)

add = []
sub = []
multi = []
div = []
mod = []
expo = []
 
for j in range(3):
    add.append(NumList1[j] + NumList2[j])
    sub.append(NumList1[j] - NumList2[j])
    multi.append(NumList1[j] * NumList2[j])
    div.append(NumList1[j] / NumList2[j])
    mod.append(NumList1[j] % NumList2[j])
    expo.append(NumList1[j] ** NumList2[j])
 
print("\nThe List Items after Addition =  ", add)
print("The List Items after Subtraction =  ", sub)
print("The List Items after Multiplication =  ", multi)
print("The List Items after Division =  ", div)
print("The List Items after Modulus =  ", mod)
print("The List Items after Exponent =  ", expo)
Output

List 1:  [10, 20, 30]

List 2:  [5, 2, 3]

 

The List Items after Addition =   [15, 22, 33]

The List Items after Subtraction =   [5, 18, 27]

The List Items after Multiplication =   [50, 40, 90]

The List Items after Division =   [2.0, 10.0, 10.0]

The List Items after Modulus =   [0, 0, 0]

The List Items after Exponent =   [100000, 400, 27000]

Program 2: Python Program to Perform Arithmetic Operations on Lists For Using Loop

In this program, we will use the for loop to iterate each element of the list and take the input by the user.

Python Program to Perform Arithmetic Operations on Lists For Using Loop
# Python Program to Perform Arithmetic Operations on Lists For Using Loop

NumList1 = []
NumList2 = []
add = [] 
sub = [] 
multi = []
div = []
mod = []
expo = []

# Take the input From the User
Number = int(input("Enter the Total Number of List Elements: "))
print("Enter the Items of a First and Second List: ")
for i in range(1, Number + 1):
    List1value = int(input("Enter the %d Element of List1: " %i))
    NumList1.append(List1value)

    List2value = int(input("Enter the %d Element of List2: " %i))
    NumList2.append(List2value)
 
# Print the List Entered By the User 
print("\nList 1: ",NumList1)
print("List 2: ",NumList2)     
    
for j in range(Number):
    add.append(NumList1[j] + NumList2[j])
    sub.append(NumList1[j] - NumList2[j])
    multi.append(NumList1[j] * NumList2[j])
    div.append(NumList1[j] / NumList2[j])
    mod.append(NumList1[j] % NumList2[j])
    expo.append(NumList1[j] ** NumList2[j])
     
print("\nThe List Items after Addition =  ", add)
print("The List Items after Subtraction =  ", sub)
print("The List Items after Multiplication =  ", multi)
print("The List Items after Division =  ", div)
print("The List Items after Modulus =  ", mod)
print("The List Items after Exponent =  ", expo)
Output

Enter the Total Number of List Elements: 3

Enter the Items of a First and Second List: 

Enter the 1 Element of List1: 5

Enter the 1 Element of List2: 3

Enter the 2 Element of List1: 1

Enter the 2 Element of List2: 4

Enter the 3 Element of List1: 5

Enter the 3 Element of List2: 3

 

List 1:  [5, 1, 5]

List 2:  [3, 4, 3]

 

The List Items after Addition =   [8, 5, 8]

The List Items after Subtraction =   [2, -3, 2]

The List Items after Multiplication =   [15, 4, 15]

The List Items after Division =   [1.6666666666666667, 0.25, 1.6666666666666667]

The List Items after Modulus =   [2, 1, 2]

The List Items after Exponent =   [125, 1, 125]

Program 3: Python Program to Perform Arithmetic Operations on Lists For While Loop

Python Program to Perform Arithmetic Operations on Lists For While Loop

# Python Program to Perform Arithmetic Operations on Lists For While Loop

NumList1 = []; NumList2 = []
add = [] ; sub = [] ; multi = []
div = []; mod = [] ; expo = []
i = 0
j = 0

# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
print("Enter the Items of a First and Second List: ")
while(i < Number):
    List1value = int(input("Enter the %d Element of List1: " %i))
    NumList1.append(List1value)

    List2value = int(input("Enter the %d Element of List2: " %i))
    NumList2.append(List2value)
    i = i + 1
    
# Print the List Entered by the User
print("\nList 1: ", NumList1)
print("List 2: ", NumList2)

while(j < Number):
    add.append(NumList1[j] + NumList2[j])
    sub.append(NumList1[j] - NumList2[j])
    multi.append(NumList1[j] * NumList2[j])
    div.append(NumList1[j] / NumList2[j])
    mod.append(NumList1[j] % NumList2[j])
    expo.append(NumList1[j] ** NumList2[j])
    j = j + 1
 
print("\nThe List Items after Addition =  ", add)
print("The List Items after Subtraction =  ", sub)
print("The List Items after Multiplication =  ", multi)
print("The List Items after Division =  ", div)
print("The List Items after Modulus =  ", mod)
print("The List Items after Exponent =  ", expo)
Output

Enter the Total Number of List Elements: 3

Enter the Items of a First and Second List: 

Enter the 0 Element of List1: 5

Enter the 0 Element of List2: 4

Enter the 1 Element of List1: 1

Enter the 1 Element of List2: 6

Enter the 2 Element of List1: 3

Enter the 2 Element of List2: 2

 

List 1:  [5, 1, 3]

List 2:  [4, 6, 2]

 

The List Items after Addition =   [9, 7, 5]

The List Items after Subtraction =   [1, -5, 1]

The List Items after Multiplication =   [20, 6, 6]

The List Items after Division =   [1.25, 0.16666666666666666, 1.5]

The List Items after Modulus =   [1, 1, 1]

The List Items after Exponent =   [625, 1, 9]


Related Posts



Comments

Recent Posts
Tags