You are here: Home / Topics / Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers

Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers

Filed under: Python on 2023-09-18 06:54:59

Sample data: 3, 5, 7, 23

Python list:

A list is a container which holds comma separated values (items or elements) between square brackets where items or elements need not all have the same type. In general, we can define a list as an object that contains multiple data items (elements). The contents of a list can be changed during program execution. The size of a list can also change during execution, as elements are added or removed from it.

Python tuple:

A tuple is container which holds a series of comma separated values (items or elements) between parentheses such as an (x, y) co-ordinate. Tuples are like lists, except they are immutable (i.e. you cannot change its content once created) and can hold mix data types.

Code:


values = input("Input some comma seprated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)     

Output:

Input some comma seprated numbers : 3,5,7,23                       
List :  ['3', '5', '7', '23']                                      
Tuple :  ('3', '5', '7', '23')   

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.