PYTHON LIST TUTORIAL WITH EXAMPLES

in WORLD OF XPILAR3 years ago (edited)


list in python.png

Hello everyone. After my recent post on installing python in your windows OS, I am here again with our first part tutorial on python programming. This post will cover a basic of list, methods available for list and various types of operations you can do with the list. So let's get started with what lists are.

List is one of the important data structure in python that can be used to store a series of data. It can be used to store items that maybe of string, character, integer, float or a combination of any of these. In order to create a list, we use square brackets [] and write values inside it separated by a comma. For example a simple list of countries would look like this: countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]. Lets open a Sublime Text and run our python code. Sublime Text is lightweight and can be used to debug your python program. After opening Sublime Text, go to Tools->Build System->Python and then you are good to execute python program from sublime text.

image.png

If you print the above list of countries, Sublime Text will show you the following output:

image.png

If you want to retrieve the particular value, you can do that using index number. The first value in the list holds index number 0, second value holds index number 1 and so on. For example, if you want to get the value of an item in the seventh position of the list then you have to use index number 6. Like for the same code, we can do the following to get value in seventh position.

image.png

The syntax is very easy to use. You have to write the name of the list and then square bracket. Inside square bracket, write the index number for the value you want to retrieve.

You can also sort the value in the list using sort() function whether your value be of integer or string or character. We can try sorting out the above list. Lets see the code:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("Before Sorting", countries)

countries.sort()
print()
print("List in sorted order is:", countries)

print() statement will add one extra blank line. Upon execution, the following output will be shown:

image.png

You can see that it has been alphabetically sorted. We can also reverse the list with the help of reverse() function. We can do the same for above list of countries.

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("Before Reversing", countries)

countries.reverse()
print()
print("List in reverse order is:", countries)

Upon the execution of this code, you can see the following output:

image.png

We have achieved our desired output. I will stop this post for now and in my another post we will see how to modify a list i.e. updating value inside list, adding a new value to the list and removing the items from the list. Let me know in the comment section if you have any confusion or doubt or encounter error during coding. I would be happy to help. Cheers!

Hey @steemcurator01, @steemcurator02, @steem, @steemitblog, @steemit, I have been constantly receiving downvote from whales here. They aren't letting me thrive here. I am feeling like all the efforts and time I put in making my posts are going in vain. Please help. Or else, I don't wanna be a part of this community.

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 67491.07
ETH 3345.66
USDT 1.00
SBD 2.72