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:
- How to Reverse a List in Python
- Python List Remove()
- List Extend Python
- Python List Sort by a key
- Python List Count Method
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:
- About Tuple in Python with examples
- Tuple without parentheses
- Tuple unpacking
- list inside tuple Python
- While Loop and tuple in Python
- Find a type of element and tuple with one element
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')
Recommended Posts:
- How to Create Forgot System Password with PHP & MySQL
- Datatables Editable Add Delete with Ajax, PHP & MySQL
- Download Login and Registration form in PHP & MySQL
- Export Data to Excel in Php Code
- Hospital Database Management System with PHP MySQL
Get Salesforce Answers