PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 1 - PYTHON LISTS COLLECTION | WEEK 2

in hive-120962 •  2 years ago  (edited)

python class banner day 1 week 2.PNG


Good day everyone!
Hope you're doing great!! Today is the first day of the Python Programming class week 2. If you have followed my week 1 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1 classes, please visit by blog now @anyiglobal to see all my week 1 classes.

I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.

Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON LISTS COLLECTION

Python programming language has different types of collections that allows a programmer to store more than one value in a variable. A collection can collect values of the same data type or values of different data types. Python collection can be ordered, unordered, changeable, unchangeable, indexed, unindexed, allows duplicate, and does not allow duplicate. It all depends on the type of collection you're making use of in your program.

In this class, we will discuss about "Python Lists Collection". We have different types of collections in python, they include;

  • List: It is ordered, changeable, and allows duplicate members.
  • Tuple: It is ordered, unchangeable, and allows duplicate members.
  • Set: It is unordered, unindexed, and does not allow duplicate members.
  • Dictionary: It is unordered, indexed, changeable, and does not allow duplicate members.

So for the sake of this class, we are going to detail List collection in the following ways:

  • How to access items in a list.
  • How to change a value in a list of items
  • How to loop through a list.
  • How to check if an item exists in a list.
  • How to check the length of a list.
  • How to add items to a list.
  • How to remove an item from a list.
  • How to copy a list.
  • How to use list() constructor to make a new list.

WHAT IS A LIST?
A list is a collection of items that is ordered, changeable, and allows duplication of it's members. Duplication of it's members means that you can duplicate an item in the list twice without having any error during runtime. E.g. If our list of fruits contain; ["orange", "apple", "mango", "pawpaw", "banana"], then, duplicating a member means we can duplicate any of the fruits contained in the list twice like this; ["orange", "apple", "mango", "orange", "pawpaw", "banana"].

If you can observe, from our list example above, we wrote "orange" twice in the list. Hence, this will work correctly without any error because list collection allows duplicate members. However, this can't work in Set or Dictionary because it does not allow duplicate members. Don't worry, we will see some of these examples as soon as we start writing our codes.

NB: Python list is written in square brackets [].


Example of List
Suppose we have a list of fruits in a our program, we can write and print the list items in the following ways in our programs:

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
print(fruits)

Output
['orange', 'apple', 'mango', 'pawpaw', 'banana']

Python list
python lists.PNG
List of fruits in our program

From our code above, you can see that we listed the fruit items we want to make use of in our program and printed it to the screen. Note that we can manipulate these items in a list by performing some actions on them which we will see below.


ACCESSING AN ITEM IN A LIST
In a program, we can access a specific item in a list after we have listed them and assign them to a variable. We make use of index numbers to refer to the item we want to access in a list.

Note: We use index numbers to access items stored in a list. Read this material to know more about array indexing!

For Example;
Lets access the item at index number 2 i.e. "mango" and print it to the screen.

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
print(fruits[2])

Output
mango

python accessing items in a list.PNG
We accessed an item at index 2

From our program above, we see that we were able to extract the item "mango" at index 2.

NB: Copy the code and try it out in your text editor. Please try and access other items in the list using the appropriate index. Note that if you try to access an item that is not in the list, Python will throw you an error. Read this material to know more about array indexing!


CHANGING AN ITEM VALUE IN A LIST
We can also manipulate the list collection by changing the items in the list. To change the value of an item in a list, just refer to the index number of the specific value you want to change.

For Example;
Lets change the item at index 0 i.e. "orange" to "watermelon"

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits[0] = "watermelon"
print(fruits)

Output
['watermelon', 'apple', 'mango', 'pawpaw', 'banana']

python change list item value.PNG
Changing a list item value

As we can see from our code above, we changed the item value stored at index 0 i.e. "mango" to "watermelon". So our new list becomes ['watermelon', 'apple', 'mango', 'pawpaw', 'banana'].

NB: Copy the code and try it out in your text editor. Practice along please!!


LOOPING THROUGH A LIST
In Python programming, we can loop through a list to access all the items in the list using "for" loop and print them to the screen in just one block of code. We will learn about "for Loop" as we progress in this course.

For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
for x in fruits:
  print(x)

Output
orange
apple
mango
pawpaw
banana

python list looping.PNG
Looping through a list

From the code above, we loop through the list to access all the items in the list and we got our output in the terminal.

NB: Copy the code above and try it out in your text editor.


CHECK IF ITEM EXISTS IN A LIST
Apparently, we can check if a particular value exists in a list. To do this, we use "in" keyword which is a membership operator to check if an item exists in a list.

For Example,

Lets check if apple exists in the list of fruits.

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
if "apple" in fruits:
  print("Yes, Apple exists in fruits list")

Output
Yes, Apple exists in fruits list

python check item in a list.PNG
Check item in a list

From the code above, we checked if "apple" exists in the fruits list, and because it exists, our output is printed to the screen.


CHECKING LIST LENGTH
We can as well check to know the length of a list i.e. the number of items contained in a list. To do that, we use the len() method.

For Example,

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
print(len(fruits))

Output
5

python list length.PNG
Checking the length of a list

From the code above, we checked our fruits list to determine the total number of item values it holds. And our result is 5.

NB: Copy the code above and try it out in your text editor.


ADDING ITEMS TO THE LIST
If you have a list of items, you can add new items to the list using the "append()" method.

For Example,
Lets append new item named "watermelon" to the given list below.

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits.append("watermelon")
print(fruits)

Output
['orange', 'apple', 'mango', 'pawpaw', 'banana', 'watermelon']

python list item adding.PNG
Adding new item to a list

From the code above, we can see that our newly added item was appended to the end of the list.

But we can also choose which position we want our new item to be appended to by using the "insert()" method. For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits.insert(2, "watermelon")
print(fruits)

Output
['orange', 'apple', 'watermelon', 'mango', 'pawpaw', 'banana']

python insert method.PNG
Using insert() method to add to a specified index in a list

As we can see from the list in the program above, we added a new item "watermelon" at index number 2. The item will shift the existing item at index 2 to the right and take up the current position in the list.

NB: Copy the codes in the example above and try it out in your text editor. Please practice along while learning.


REMOVING ITEMS FROM A LIST
To remove item from a list, use the "remove()". Aside the "remove()", we have other methods like "pop()", "del", and "clear()" which we can also use to remove an item or items from a list.

For Example,
Lets use the "remove()" method to remove the fruit "pawpaw" from the list.

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits.remove("pawpaw")
print(fruits)

Output
['orange', 'apple', 'mango', 'banana']

python remove item from a list.PNG
Removing item from a list using the remove() method

From the code above, we removed "pawpaw" from the list of fruits using the "remove()" method.


Using the "pop()" method to remove item from the list:
Using the "pop()" method, Python removes an item at the specified index, butif the index is not specified, it removes the last item in the list. For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits.pop(1)
print(fruits)

Output
['orange', 'mango', 'pawpaw', 'banana']

python pop remove item.PNG
Removing a list item using the "pop()" method


Using the "del" method to remove item from the list:
The del keyword has the capability of removing an item at a specified index or deleting the list completely. For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
del fruits
print(fruits)

Output
NameError

python deleting list.PNG
Deleting a list completely using the "del" keyword

From the code above, we can see that we got an error in our terminal because we're trying to print a variable that does not exist i.e. has been deleted. Please try the other "del" keyword with a specified index to see the result. Use "del fruits[2]" to delete an item at the specified index.


Using the clear() method to clear the list entirely:
This method clears the items in a list entirely and returns an empty list. For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
fruits.clear()
print(fruits)

Output
[]

python clear method.PNG
Using clear() to clear the items in a list

From the code above, we can see that the list returned an empty list after clearing the items in the list using the clear() method.

NB: Copy the codes above and try it out in your text editor. Use this material or this material to learn more about Python list collection.


COPY A LIST
We can copy an existing list entirely into a new variable by using the "copy()" method. Note that you cannot copy a list by simply doing fruits2 = fruits because if you do so, what you are simply doing is that you're referencing to the existing list, and any changes made to the first list will affect the second list. However, this is not what we want. We want to copy the fruits list into a new variable such that any changes made to the first list will not affect our new list. Therefore, to achieve that we use the Python built-in List method "copy()".

For Example;

fruits = ["orange", "apple", "mango", "pawpaw", "banana"]
new_fruits_list = fruits.copy()
print(new_fruits_list)

python copy method.PNG
Copying a list into a new variable

Done! We successfully copied our fruits list into a new fruits list variable as we can see from the code above.

NB: Copy the code above and try it out in your text editor. Please also try referencing a new fruits list variable to the existing fruits list variable and see the result. Make a change in the original fruits list and see that the changes will be made to the new fruits list just referenced. Use this material or this material to learn more about Python list collection.


PYTHON LIST() CONSTRUCTOR
This list() constructor is simply another way to make a list of string items or numbers in Python programming language.

For Example;
Lets make a list of fruits using the "list()" constructor.

fruits = list(("orange", "apple", "mango", "pawpaw", "banana"))
print(fruits)

Output
['orange', 'apple', 'mango', 'pawpaw', 'banana']

python list constructor.PNG
Python list() constructor

As we can see from the codes above, we create a new fruits list using the Python built-in "list()" constructor.

NB: Copy the code and try it out in your text editor. Use this material or this material to learn more about Python list collection.



Conclusion

Python list collection is also one of the types of data types we have in Python programming language. The list falls under "Sequence data type" as we listed in our previous class when we discussed about data types in Python. Having knowledge of list collection and other types of collections as a programmer is very important because it will help you know which type of collection to make when creating a collection, this is because some collections are immutable after creation, i.e. you cannot change the values in it, Hence, It's importance. Thank you very much and God bless you!



This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.

Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill.


Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



Cc:
@campusconnectng, @whitestallion, @kouba01, @pelon53, @reminiscence01, @steemchiller, @justyy

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!
Sort Order:  

@anyiglobal, Thanks for sharing with us on @campusconnect , Continue sharing your quality contents with us here we love and appreciate your effort ,Thanks

REMARKS;

Plagiarism-freeYES
#steemexclusiveYES
#club5050YES
DelegationYES
Bot-freeYES
BENEFICIARYNO

Thanks!

Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @daytona475 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.

Club Status: #Club5050

Sevengers Comment GIF.gif

Regards,
Team #Sevengers

The #learnwithsteem tag focuses on teaching through tutorials and lessons some knowledge, skill or profession that you have. Please avoid using it if it's not about that. Thank you!

Thanks!

Wow I really appreciate your good work thanks for sharing with us this wonderful contents to us in this campusconnectng community

You're welcome bro. Please try and participate in the classes. Also try and resteem it so that it will reach larger number of people. I appreciate your acknowledgement!

Wow that is a very detailed tutorial on Python. Thanks for sharing

You're welcome dear! Resteem it so that 01 can see it

Great class... learnt alot
i started python programming some weeks back but i must say your notes are more explicit.
Please i hav a question


When you were using methods such as pop(), append(), remove(), insert() etc on the fruits list, you applied it as follows;

fruits.append('item')
fruits.insert('item')
etc

but when using the del method, you applied it as follows;
del fruits.


so why didn't you use the del method as the other methods forexample

fruits.del()

That's just the language syntax. You just have to master all the syntax to become a good programmer.

But you know sometimes we learn by trial and error, include the paranthesis "()" in the del method to see the error it will throw to you! Understand?

okay sir...
understood

on the other hand, i am setting up a curation team, i'd like you to be part of it, thats if you dont mind

Ok I wouldn't mind, am interested

Your post is manually rewarded by the @nftmc Community Curation Trail.


Join the NFTMC community to get rewarded.

USE TAG - #nftmc

Curation Trail- @nftmc
Discord- https://discord.gg/5P57gwYYcT
Twitter- https://mobile.twitter.com/NFTMC3