Programming Class - How to create List in Python, Access items in the list, change item value in the list, Loop through the list, Add item in the list and remove item from a list

in Steem Skillshare3 years ago

Hello Steem Skillshare and SIZ,

Welcome to my blog. In this content, I will teach you to how to create list of items in python and how to perform some actions on the list. Please @steem.skillshare, @milakz and @daytona475, I want to use this opportunity to plead for a badge in this community as a Python teacher. I will be glad if my request is granted!

So if you have interest in learning Python programming basics, just follow my blog always and you will learn it as wished!
So before I proceed with this class, I will first of all define what a List is.



What is a List?

A list is a collection of ordered and changeable elements of the same or different data types. In real life, this list can be applicable when you are working with a list of students in your office. You can successfully use python programming to manipulate the list of students stored in the list variable i.e. you can access any student stored in the list, change the name of the student stored on the list, loop through the list to get all the students stored in the list, add a new student on the list, delete a student from the list and many more.



Now that you have known what a list is, let's go down to what we have in this class. Before you follow me in this tutorial make sure you have Vs Code installed in your system. Visit here to download it. after successful download and installation, click here to install the python extension in the vscode.

If you have done this successfully, launch the Vs Code let's start work immediately!



First of all create a folder in your local drive C: and name it Python List. Then open the folder by the clicking the 'open folder' link as shown in the screenshot below:

python list.PNG



After successfully opening the folder in Vs Code, create a new file with an extension name '.py' in the folder that you just opened and name it "list.py", then click 'Enter' as shown below:

list file.PNG



Now we are set to start our coding!

Creating List

Let's create our list, print it and then run it. Do same as shown below:

list1.PNG

Note that if you want to run your python program, you have to move your mouse to the "Terminal tab" at the top of the Vs Code and click it, when the dropdown opens, you will see "New Terminal", click on it and a new terminal will open at the bottom of the Vs Code. Also Note that we use the "print()" method to print our output to the screen. This terminal is where we execute our python codes as the one shown above. If you want to run the code in the Terminal, just type "py list.py" or "python list.py" depending on the command the version of your Vs Code accepts.



Accessing items in the list

Let's access the second item in the list and see the output.

accesslist.PNG

In the code above, we use "myList[1]" to access the second element in the list! Do same as I did. Make sure you follow my steps!! If you have any more questions you can ask me in the comment section of this post.



Change Item Value in the List

Let's change the second item in the list to "Mango".

changeitem.PNG

In the code above, line 8, we use the code myList[1] = "mango" to change the second item ("apple") in the list to "mango" and we have the following output as shown above.



Loop Through the List

Let's loop through the list to print all the elements or items stored in our list variable.

loopitem.PNG

If you want to know more about Python "for loop" read this article.
Code in line 12 loops through each of the items stored in the list and prints them one after the other until a condition is met as shown in the screenshot above. Our output is shown in the Terminal of the Vs Code.



Add New Item in the List

Let's add a new item in the list, "cherry".

additem.PNG

We use the "append()" method to add a new item to the list! from the screenshot above, line 16, We use the code myList.append("cherry") to add cherry to our existing list to form a new list with four items. And our Output is displayed in the terminal when we run the code.



Removing Item from a List

Let's remove the second item from the list.

removeitem.PNG

We use "remove()" method to remove an item from the list. In the screenshot above, in line 20, we used the remove method to remove the second item ("banana") from the list. Our Output is shown in the terminal.



The Whole Code

The whole code of the tutorial is shown below.

wholelistcode.PNG



Conclusion

Python Programming Language is a proficient language which has diverse applications. It is a general-purpose programming language that can be applied in many areas ranging from Web development to Artificial Intelligence. It has powerful libraries used for achieving many things in different areas. You will have a great job opportunities if you learn this language very well. That's why it's very important that you follow my Blog to learn the basics of Python first, before going to complex.



Thank you very much for learning with me till the end. I believe you have something new today. If you have any question regarding to what I have just taught you, you can drop it in the comment section of this post!



Cc: @steem.skillshare
@siz-official
@milakz
@cryptokraze
@daytona475
@etainclub

Sort:  
 3 years ago 

Thanks for your post. Python teacher tag attached

 3 years ago 

Thanks so much! Am delighted to be your python teacher

 3 years ago 

@daytona475 please may I request for your WhatsApp number... This is mine +2349038503700 Thank you!

Sorry, I do not provide such information. If you have any requests or suggestions, you can use this channel.

#affable

 3 years ago 

I mean no harm... Am for good

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 68148.22
ETH 3249.65
USDT 1.00
SBD 2.67