Item 1
List
A list stores many values in one variable, inside square brackets. Lists are ordered and mutable (can be changed). Indexing starts at 0. Use append() to add and len() to count items.
Try it — edit the code and press Run:
Lesson 4
Item 1
A list stores many values in one variable, inside square brackets. Lists are ordered and mutable (can be changed). Indexing starts at 0. Use append() to add and len() to count items.
Try it — edit the code and press Run:
Item 2
A tuple is like a list but immutable — it cannot be changed after creation. Tuples use round brackets ( ).
Try it — edit the code and press Run:
Item 3
A dictionary stores data as key: value pairs inside curly braces { }. You access a value using its key.
Try it — edit the code and press Run:
Item 4
A function is a reusable block of code defined with def. It can take parameters and return a value. Functions make code organized and avoid repetition.
Try it — edit the code and press Run:
1. What is the index of the first item in a list?
2. Which is a tuple?
3. Which method adds an item to the end of a list?
4. How do you get "Ali" from s = {"name": "Ali"}?
5. Which keyword defines a function?
Auto-checked exercises at three levels. Open hints one by one before the solution.
colors = ["red", "green", "blue"]. Print the last item.
Write a function square(n) that returns n * n, then print square(6).
Write total(nums) that returns the sum of a list WITHOUT using sum(). Print total([4, 8, 15, 16]).
Goal: Store many students and reuse logic with functions.
Requirement: Store students in a list of dictionaries. Write a function get_grade(avg) and print each student's name with their grade.