w3resource

Python: Generate a list and tuple with comma-separated numbers

Python Basic: Exercise-6 with Solution

Write a Python program that accepts a sequence of comma-separated numbers from the user and generates a list and a tuple of those numbers.

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.

Sample data : 3, 5, 7, 23
Output :
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')

Sample Solution:

Python Code:

# Prompt the user to input a sequence of comma-separated numbers and store it in the 'values' variable
values = input("Input some comma-separated numbers: ")

# Split the 'values' string into a list using commas as separators and store it in the 'list' variable
list = values.split(",")

# Convert the 'list' into a tuple and store it in the 'tuple' variable
tuple = tuple(list)

# Print the list
print('List : ', list)

# Print the tuple
print('Tuple : ', tuple)

Sample Output:

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

Explanation:

The said code prompts the user to input a list of numbers separated by commas. Then it converts the string of numbers into a list and a tuple, and finally it prints both the list and the tuple.

  1. The first line values = input("Input some comma seprated numbers : ") gets a string of numbers separated by commas from the user using the input() function and assigns it to the variable 'values'.
  2. The second line splits the string of numbers into a list of individual numbers using the split() method and assigns it to the variable 'list'. This method takes a separator as input, in this case ',' (comma) and returns a list of substrings that were separated by the separator.
  3. The third line converts the list into a tuple using the tuple() function and assigns it to the variable 'tuple'.
  4. The fourth line prints the list to the console.
  5. The fifth line prints the tuple to the console.

Flowchart:

Flowchart: Generate a list and tuple with comma-separated numbers.

Python Code Editor:

 

Previous: Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them.
Next: Write a Python program to accept a filename from the user and print the extension of that.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.