Tuesday, March 10, 2015

Python List Basics - Python Basics - Python Tutorial

Python listPython List


In this Python tutorial, we are about to take a brief tour of another data type in Python called a list. A Python list is a positionally ordered collection of data. A List has no maximum size limitations so your list can be as large as you want or need it can even be empty which is referred to an empty string. We are able to modify the list after they are created by assignment or specific list methods. A Python list is very useful when programming we could use them for so many reasons like a sports roster, list of business and many more.


Python List Syntax


A list is declared by a square bracket( [ ] ) and the content inside a list referred to as the list data and each piece of data is separated by a comma.


List Syntax


[987, “Python”, “Dog”, 1, -5, “Cat”]

In the example above we have created a list that contains numbers and strings. Each piece contained in the list is separated by a comma.


Python List Sequence


Like strings a list is in a sequence but unlike strings the index on counts pieces of data(between the commas) in the list. We will use the above example in list syntax to see how the sequence work in list.
















List987“Python”“Dog”1-5“Cat”
Index012345

 


Python List Examples


Fire up your Python interpreter and follow long.  We will create some Python list and assign them to a variable. This will be a good chance for you to practice creating and assigning list.


#Create a Python list
>>> a = ["Tom", "Mike", "Jaime", "Jake"]

#Call the Python List
>>> a
['Tom', 'Mike', 'Jaime', 'Jake']

#Create a Python List
>>> b = [987, 897, 34, 21, 67]

#Call the Python List
>>> b
[987, 897, 34, 21, 67]

#Try slicing the list we have not tried this before
>>> b[2]
34
>>> b[-1]
67
>>> b[-0]
987
>>> b[-2]
21

#Create a list
>>> list = ["eggs", "milk", "apples", "tea"]

#call the list
>>> list
['eggs', 'milk', 'apples', 'tea']

Now we have complete our brief overview of Python list we can now set up and call list. We will cover list in full in a couple chapters.  If you have any questions leave us a comment below or Q & A section.



No comments:

Post a Comment