Python ProgrammingPython Programming

How to create and print list in python?

A list is created by putting element values inside a square bracket. To reuse our list, we can give it a name and store it like this.

The following program shows how to create lists of different data-types:

intList = [10, 20, 30, 40]
strList = ["Canada", "Japan", "London"]
mixList = ["Canada", 20, True, 500.50]
generate = [x + 6 for x in [4, 5, 6]]  # Generate from loop
listfromtuple = list((5, 13, 15, 17, 29))  # List from a tuple
python = list('python')  # List from a string
 
print(intList)
print(strList)
print(mixList)
print(generate)
print(listfromtuple)
print(python)
Output
[10, 20, 30, 40]
['Canada', 'Japan', 'London']
['Canada', 20, True, 500.5]
[10, 11, 12]
[5, 13, 15, 17, 29]
['p', 'y', 't', 'h', 'o', 'n']