Question 4:

Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.

Suppose the following input is supplied to the program:

34,67,55,33,12,98

Then, the output should be:

['34', '67', '55', '33', '12', '98']

('34', '67', '55', '33', '12', '98')

Hints:

1. In case of input data being supplied to the question, it should be assumed to be a console input.

2. tuple() method can convert list to tuple

3. Use string split function

Solution:

values=input()
l=values.split(",")
t=tuple(l)
print(l)
print(t)

 

Discussion:

1. Get input from the user using the input() function.

2. Call the string split() function to split the string to list using the comma(,) as delimiter.

3. Pass the list to tuple() function to convert the list to tuple.

4. Print the list and tuple.