List in Python
Lists are ordered, mutable collections that can contain duplicate values and different data types.
What is List?
Lists are ordered, mutable collections that can contain duplicate values and different data types.
Create, update, filter, and iterate over a Python list.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
even_numbers = [number for number in numbers if number % 2 == 0]
print(even_numbers)
Expected output
[2, 4, 6]
How it works
The example appends a value and then uses a list comprehension to select only even numbers.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.