- Get link
- X
- Other Apps
Recent - Posts
- Get link
- X
- Other Apps
Welcome back. Remember arrays from the QBasic series, DIM marks(10), fixed size,
decided in advance? Python's version is called a list, and it's a lot more relaxed about things. No DIM, no
deciding the size up front, it grows and shrinks as your program runs.
What's in this post
- What is a list?
- Indexing, and why it starts at 0
- Negative indexing
- Slicing a list
- Build, slice and modify a list (try it!)
- Useful list methods
- Looping through a list
- Example 1 (Easy): A list of fruits
- Example 2 (Medium): Average of a list
- Example 3 (Harder): Finding the maximum, manually
- Example 4 (Hero level): Names, marks, and grades together
- Common mistakes
- Practice tasks
1. What is a list?
Definition: a list is an ordered collection of values stored under a
single variable name. Unlike QBasic arrays, a Python list doesn't need a fixed size, and it can hold different types
mixed together if you really want.
create_list.py
fruits = ["apple", "banana", "mango"]
print(fruits)
2. Indexing, and why it starts at 0
The biggest difference from QBasic: Python lists always start counting at
0, not 1, and there's no OPTION BASE to change that.
fruits[0] is "apple", not "banana". This
trips up almost everyone coming from a 1-based language, so slow down here.indexing.py
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # apple
print(fruits[2]) # mango
3. Negative indexing
Analogy: think of a queue of people waiting for a bus. Counting from the front, the first person is position
1. But you can also describe someone by counting from the back, "the last person in line." Python lets you do
exactly that with negative numbers,
-1 always means "the last item," no matter how long the list is.
negative_index.py
fruits = ["apple", "banana", "mango"]
print(fruits[-1]) # mango, the last item
print(fruits[-2]) # banana, second from the end
4. Slicing a list
Slicing grabs a chunk of a list at once, list[start:end], and just like
range() from the last post, the end position is not included.
slicing.py
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
5. Build, slice and modify a list
Try it: your own list
Add items, remove the last one, and drag the slice numbers to see exactly what gets selected.
my_list[
:
]
6. Useful list methods
| Method | What it does |
|---|---|
len(my_list) |
Number of items in the list |
my_list.append(x) |
Adds x to the end |
my_list.insert(i, x) |
Inserts x at position i |
my_list.remove(x) |
Removes the first item equal to x |
my_list.pop() |
Removes and returns the last item |
my_list.sort() |
Sorts the list in place |
my_list.reverse() |
Reverses the list in place |
7. Looping through a list
loop_list.py
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
Notice you loop directly over the items themselves here, no index needed unless you specifically
want one. If you do need the position too, use for i in range(len(fruits)), which we'll use in Example
4.
8. Example 1 (Easy): A list of fruits
fruits.py
fruits = ["apple", "banana", "mango", "guava", "orange"]
for fruit in fruits:
print("I like", fruit)
9. Example 2 (Medium): Average of a list
average.py
marks = [78, 55, 92, 38, 67]
total = 0
for m in marks:
total = total + m
average = total / len(marks)
print("Average marks:", average)
10. Example 3 (Harder): Finding the maximum, manually
Python actually has a built-in max() function, but let's write it ourselves once,
the same "assume, then check everyone else" pattern from the QBasic arrays post.
find_max.py
marks = [78, 55, 92, 38, 67]
highest = marks[0]
for m in marks:
if m > highest:
highest = m
print("Highest mark:", highest)
11. Example 4 (Hero level): Names, marks, and grades together
grade_report.py
names = ["Sunita", "Ramesh", "Anita", "Suresh"]
marks = [78, 55, 92, 38]
for i in range(len(names)):
if marks[i] >= 90:
grade = "A+"
elif marks[i] >= 75:
grade = "A"
elif marks[i] >= 60:
grade = "B"
elif marks[i] >= 40:
grade = "C"
else:
grade = "Fail"
print(names[i], "scored", marks[i], "-> Grade", grade)
Two parallel lists, lined up by position, names[i] and marks[i] always
refer to the same student. This is lists, loops, and if/elif from the last three posts, all working together at
once.
12. Common mistakes
- Forgetting lists start at 0. The classic QBasic-to-Python mistake.
- IndexError from going out of range. A list with 5 items only has valid indexes 0 to 4.
- Forgetting slicing's end is exclusive, same rule as range().
- Letting two parallel lists get out of sync, if you add a name but forget to add the matching mark, Example 4's logic breaks immediately.
13. Practice tasks
- Create a list of 5 numbers and print only the even ones.
- Write a program that finds the smallest value in a list, mirroring Example 3.
- Ask the user to type 5 numbers, add each to a list using
append(), then print the list sorted. - Challenge: using slicing, print a list reversed without using
.reverse()(hint:my_list[::-1]is worth looking up).
Lists are the single most-used tool in everyday Python, almost every real program ends up storing a collection of something. Next in the rotation, we're back to DBMS for Part 3, where we finally start changing data with INSERT, UPDATE, and DELETE. After that, Python Part 5, strings in proper depth.
— Diwakar
Coding Tutorials
Computer Science Notes
For Students
Lists
Programming for Beginners
Python
SEE Computer
- Get link
- X
- Other Apps
Comments