Generate a list and tuple with comma-separated numbers


Generate a list and tuple with comma-separated numbers

Python list:

A list is a container that holds comma-separated values ​​(items or elements) between class brackets, where items or elements are not required to all have the same type. In general, we can define a list as an object that contains many 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.

Python tuple:

A tuple container is a series of values ​​(objects or elements) separated by commas between parentheses (eg, y) co-ordinate. Tuples are like lists, except that they are immutable (ie you cannot change the content once created) and can hold Mix data types.

WEEK-9 (Program)

a) 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’).

Aim: A program which accepts a sequence of comma-separated numbers from console and generates a list and a tuple which contains every number.

Algorithm:

Step1: start

Step2: read the input numbers from console

Step3: Split the input values in to list and assign to l.

Step4: make the list as tuple and assign to t.

Step5: print List

Step6:Print Tuple.

Step7:End

Program:

values=input()

l=values. split(“,”)

t=tuple(l)

print(l)

print(t)

Ouput:

>>>  34,67,55,33,12,98

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

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

>>> 

Recommended Posts:



Generate a list and tuple with comma-separated numbers

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories