Python Lists
Hey friends 👋 welcome back, In the last lesson, we learned about functions, Today, we are going to learn about lists, which are used to store multiple values in a single variable, Lists are very useful when you want to organize and work with collections of data.
Creating a List
You can create a list using square brackets [], Example:
-
Lists can store different types of values together
-
Items in a list are ordered, which means they have a specific position called an index
Accessing List Items
You can access items using their index, starting from 0, Example:
Output:
Modifying List Items
You can change the value of any item by its index, Example:
Output:
Adding Items to a List
You can add items using append() or insert(), Example:
Output:
Removing Items from a List
You can remove items using remove() or pop(), Example:
Output:
Practice Time
Create a Python file called lists.py and try these exercises, 1, Create a list of five fruits and print the first and last items, 2, Change one fruit in the list and print the updated list, 3, Add two new items to your list and print it, 4, Remove one item by value and one by index, then print the final list
Great job, You just learned how to store, access, and modify multiple values in Python using lists, In the next lesson, we will explore Python Tuples and Sets, where we will learn about other types of collections, We will soon be launching our YouTube channel, where we are going to post step-by-step video tutorials on Python and other tech skills, These videos will make learning easier and more visual, helping beginners follow along confidently.
0 Comments