Worked Examples · Example 9.1
Q.Example 9.1: Create lists of different kinds and print each — list1, a list of six even numbers; list2, a list of vowels; list3, a list of mixed data types; and list4, a list of lists (nested list).
West Bengal WbchseTextbookSubjective· 2mImportance★★★★★est
4% · 1/26 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A Python list is an ordered sequence written in square brackets; its elements may be all of one type, of mixed types, or even other lists — this example creates and prints one of each kind.
The idea (list data type). A list is Python's most flexible sequence: [value1, value2, …]. Unlike arrays in many languages, a single list can freely mix an int, a float and a str, and an element can itself be another list — which is how nested (two-dimensional) data is represented.
Program
# list1: a list of six even numbers
list1 = [2, 4, 6, 8, 10, 12]
print(list1)
# list2: a list of vowels
list2 = ['a', 'e', 'i', 'o', 'u']
print(list2)
# list3: a list of mixed data types (int, float, string)
list3 = [100, 23.5, 'Hello']
print(list3)
# list4: a list of lists (nested list)
list4 = [[2, 3, 4], [6, 8, 10]]
print(list4)
Expected output
[2, 4, 6, 8, 10, 12]
['a', 'e', 'i', 'o', 'u']
[100, 23.5, 'Hello']
[[2, 3, 4], [6, 8, 10]]
What each shows
list1— homogeneous list of integers.list2— list of single-character strings; note Python prints strings inside a list with quotes. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.