Learning Python - Part 4 - Introducing Lists

in steemiteducation •  7 years ago 

Untitled-1-Recovered.jpg
Learning Python - Part 3 - Comments
https://steemit.com/steemiteducation/@pentest1002/learning-python-part-3-comments
Learning Python - Part 2 - Declaring Variables and understanding strings
https://steemit.com/programming/@pentest1002/learning-python-part-2-declaring-variables-and-understanding-strings
Learning Python - Part 1 - Familiarise yourself with the Python Console and write your first Python program!
https://steemit.com/programming/@pentest1002/learning-python-part-1-familiarise-yourself-with-the-python-console-and-write-your-first-python-program

Introducing Lists


A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family. You can put anything you want into a list, and the items in your list don’t have to be related in any particular way.
In Python, square brackets ([]) indicate a list, and individual elements
in the list are separated by commas.

For example:

names = ['james', 'connor', 'jacob', 'bella']
print(names)

The output will be:

['james', 'connor', 'jacob', 'bella']

Accessing Elements in a List


Lists are ordered collections, so you can access any element in a list by
telling Python the position, or index, of the item desired.

 names = ['james', 'connor', 'jacob', 'bella']
print(name[0])

The output will be:
james
Lists start from 0, so james is 0, connor is 1, jacob is 2, bella is 3.

You can also use strings methods on lists, for example:

names = ['james', 'connor', 'jacob', 'bella']
print(names[0].title())

The output will be:
James

Python has a special syntax for accessing the last item.

names = ['james', 'connor', 'jacob', 'bella']
print(names[-1])

The result will be the following:
bella


If this post helped you, don't forget to upvote and comment if you have a question!

See you in the next Post!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!