List In Python Programming

You are an excellent programmer now, and you can declare a variable. Good for you!

You can create a variable and assign a single value to it like the age variable:

age = 15

and the girlfriend variable:

gf = “Rihanna”

But what if you want to compute the ages of all your friends, or a list of all your girlfriends…don’t look at me like that. Some guys have more than two girlfriends when some of us don’t even have one!

Anyway. You can put more than one value in a variable. You can only do that in Python by using what we call List.

When I say list, do you remember what your mom makes you write when you go out to get the groceries?

Ah! Once, I forgot two items on my grocery list and had to lie that the shop did not have them…

In Python, creating a list is as easy as creating a variable. You create a variable and assign a list by using square brackets.

ages = [13, 14, 6, 12, 11, 18]

You must separate the items in your lists with a comma. Not just space. Python does not care about space.

Of all the most commonly used data types in Python, the list may as well be the most popular. Whenever you see square brackets in a script, that is a list. A python list can store data items or multiple values. You can put different data types in the same list.

Lists are popular because they are mutable. That means the items in a list can be changed or replaced.

Let us create a list of different data types:

my_list = ["Hey", 3, 4.5, False, 2.0, 'Me', True]

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials