Collections · Lesson 13 of 25
Lists
A list of values: add, remove and loop.
8 minutes3 games
A list lives in [ ]. append adds to the end, remove deletes a value and len counts items. A powerful shortcut is the list comprehension: [n * 2 for n in nums].
Python
langs = ["Python", "JavaScript"]
langs.append("C++")
print(langs, len(langs))
print(langs[0], langs[-1])
nums = [3, 8, 1, 12]
print(sorted(nums))
print([n * 10 for n in nums])
print([n for n in nums if n > 5])nums =
3[0]
8[1]
1[2]
12[3]
What's the output?
a = [1, 2, 3] a.append(4) print(len(a))
What will this code print?
Complete the code
Add "Go" to the end of the list.
langs = ["Python"] langs.("Go")
Exercise
Print a list with every number squared.
Expected output: [1, 4, 9, 16]
Python
nums = [1, 2, 3, 4]
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.