Learning Python: Part 2: Lists

Learning Python

Python is a general-purpose interpreted programming language. It emphasizes readability through the use of whitespace and supports multiple programming paradigms such as functional and object-oriented models.


Lists

Many programming languages have a concept called arrays. In Python, this same functionality and structure is known as lists.

A list is an ordered, sequential set of data. Different entries of the set can be accessed through their index, the position within the data.

Like other types of variables in Python, they are created and used through the name of a variable.

A list is created through opening and closing square brackets. Whatever is inside the brackets is “in” the set of data.

Accessing Entries

A list starts with the 0th position. This is the first entry in the list. Each new entry after that increases the total by one each time. A list with two entries, then, has the positions 0 and 1 for its data.

Assigning a value to a position in a list changes that value if that position already exists. If it does not, an error will happen.

Adding and Removing

New values can be added to a list through two functions: append() and remove().

By supplying a new item to the function append(), it will be added to the list. Similarly, giving a value to remove() will search for and remove the item from the list.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.