Write a Python Program to Add an Item in a Tuple

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Add An Item In A Tuple

 

Title : 

Write A Python Program To Add An Item In A Tuple



Write a Python Program to Add an Item in a Tuple


 

Description : 

Hi in this article you will learn a python program On how to add an item into a tuple. As we know tuple is an immutable data structure that means the items present in the tuple cannot be modified or you cannot add an item to the tuple. In order to perform the modification or addition to tuple items you should create a new tuple. Let's write a Python program to add item to a tuple Take a length variable and ask the user to enter the count of the number of items that need to be added into a tuple. Also take an empty tuple variable and initialize it.


    
length = int(input("Enter Number of Items Need to be added to Tuple : "))
items = tuple()

    


Once we get a tuple variable use the for loop to iterate from 0 to the tuple length and ask the user to enter an integer and append it to the tuple. While you are appending an item to tuple, use the print function to print the items present in tuple.


    

for _ in range(length):
   temp = int(input("Enter an Integer : "))
   print("Adding Integer to Tuple : ", items)
   items += (temp,)
   print("The new tuple is : ", items)

    


Use print function call to print the final tuple.


    
print("The Final Tuple is : ", items)


    




Complete Python Code : 


    
length = int(input("Enter Number of Items Need to be added to Tuple : "))
items = tuple()

for _ in range(length):
   temp = int(input("Enter an Integer : "))
   print("Adding Integer to Tuple : ", items)
   items += (temp,)
   print("The new tuple is : ", items)

print("The Final Tuple is : ", items)

    

 

 

Output : 


    
Enter Number of Items Need to be added to Tuple : 5
Enter an Integer : 1
Adding Integer to Tuple :  ()
The new tuple is :  (1,)
Enter an Integer : 2
Adding Integer to Tuple :  (1,)
The new tuple is :  (1, 2)
Enter an Integer : 3
Adding Integer to Tuple :  (1, 2)
The new tuple is :  (1, 2, 3)
Enter an Integer : 4
Adding Integer to Tuple :  (1, 2, 3)
The new tuple is :  (1, 2, 3, 4)
Enter an Integer : 5
Adding Integer to Tuple :  (1, 2, 3, 4)
The new tuple is :  (1, 2, 3, 4, 5)

The Final Tuple is :  (1, 2, 3, 4, 5)

    

 

Output : 


    
Enter Number of Items Need to be added to Tuple : 3
Enter an Integer : 100
Adding Integer to Tuple :  ()
The new tuple is :  (100,)
Enter an Integer : 200
Adding Integer to Tuple :  (100,)
The new tuple is :  (100, 200)
Enter an Integer : 300
Adding Integer to Tuple :  (100, 200)
The new tuple is :  (100, 200, 300)

The Final Tuple is :  (100, 200, 300)


    



Conclusion :

The above Python program asks the user to enter two numbers either integer or floating point values. once the user enters the two numbers so the arithmetic operations performed are addition, subtraction, multiplication, division and exponential Upon performing the arithmetic operations the results are printed onto a standard output. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments