Generate a List and Tuple with Comma-Separated Numbers

Generate a list and tuple with comma-separated numbers

List in python Example:

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.

Learn more about List:

Tuple in Python Example:

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.

Learn more about Tuple:

List and Tuple in Python

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 that accepts a sequence of comma-separated numbers from the console and generates a list and a tuple which contains every number.

Algorithm:

  • Step1: start
  • Step2: read the input numbers from the console
  • Step3: Split the input values into a list and assign them to a variable1.
  • Step4: make the list as a tuple and assign it to variable2.
  • Step5: print List
  • Step6:Print Tuple.
  • Step7:End

List in Python Program:

values=input()

variable = values. split(",")

print( variable1 )
Output:
>>>  34,67,55,33,12,98
['34', '67', '55', '33', '12', '98']
>>>

Tuple in Python Program:

values=input()

variable2 = tuple( values )

print( variable2 )
Output:
1,2,3,4,5,6,7,8,9                                                                                                                                                            
('1', ',', '2', ',', '3', ',', '4', ',', '5', ',', '6', ',', '7', ',', '8', ',', '9')
list and tuple in python
list and tuple in python

Recommended Posts:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment