Python for Absolute Beginners

Lesson 2 of 6

Lists, Dicts, and Loops

Two of Python's most-used data structures are lists (ordered collections) and dictionaries (key-value pairs).

Pythonshares state with other Python blocks

Lists

languages is a list, an ordered, mutable (changeable) collection. Access an item by its position, starting from 0, so languages[0] is "python". Lists can hold any mix of types and grow or shrink with methods like .append(...).

Dictionaries

scores is a dict, it maps keys (like "python") to values (like 95), instead of positions. Look up a value with scores["python"], trying to access a key that doesn't exist raises an error.

Loops

for lang in languages: visits every item in the list, one at a time, lang is a new variable holding the current item on each pass.

List comprehensions

[len(lang) for lang in languages] builds a brand-new list in a single line. Read it right to left: "for each lang in languages, compute len(lang), and collect the results into a list." It's exactly equivalent to:

Pythonshares state with other Python blocks

just shorter, once the pattern clicks, you'll use it constantly.

📝 Collections Quiz

Passing score: 70%
  1. 1.What does len(["a", "b", "c"]) return?

  2. 2.Python dictionaries map keys to values.