PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 1 - PYTHON ARRAYS | WEEK 5

in Steem4Nigeria2 years ago (edited)

python class banner day 1 week5.PNG


Good day everyone!
Hope you're doing great!! Welcome back to my Python programming class. Today is the first day of the Python Programming class week 5. If you have followed my classes in the previous weeks 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 previous weeks classes, please visit my blog now @anyiglobal to see all the 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 ARRAYS

Arrays as we all know is a user-defined data structure that collects and stores element of the same or similar data type. It is a contigous block of memory location. Elements stored in an array can be accessed using array index, e.g array[0], array[1], array[2], array[3], and so on... The number of index is dependent on the number of elements stored in the array. Arrays can be denoted as "array[0:n-1]" where "n" is the number of elements, and this also means that array index that starts from 0 ends with n-1 during referencing, however, array index that starts with 1 ends with n. What the aforementioned means is that for example;

let's assume we have an array of 5 elements, indexing the elements starting from 0 will simply be something like this: array = array[0], array[1], array[2], array[3], array[4]. Conversely, indexing the elements starting from 1 will simply be something like this: array = array[1], array[2], array[3], array[4], array[5] and it can be denoted as "array[1:n]". If you observe the above example very closely, you will be able to spot the difference between the two. Simply, the difference is that;

  • in the first indexing starting from 0, we stopped at n-1 i.e 5-1 = 4.
  • in the second indexing starting from 1, we stopped at n i.e 5.

In Python Programming Language, we don't have any built-in support for Arrays, but in this case, Python Lists can be used instead - according to W3Schools.com. In other articles like Geeks for Geeks, Python can be created by importing a module named array. But for the sake of this class, we will be discussing how to create array with a list. As discussed earlier in our previous classes, we all know what Python Lists is all about. Let's have a quick recap about Python Lists; it is a type of Sequence collections which is mutable, ordered and indexed. We can store the same or different data type in a List, and here draws us to the big difference between Python Lists and Arrays.

The big difference between Python Lists and Arrays is:

  • Python Lists store elements of the same or different data types, while Python Arrays store elements of the same or similar data types only.

So, having known the difference between Lists and Arrays, let's now dive fully into what we have in today's class i.e. Python Arrays.

WHAT IS AN ARRAY?
An Array is simply a variable which stores multiple values of the same data types at a time. In lists, we can only store few values and it maybe hard to loop through a list when searching for a specific item. But, in Arrays, we can loop through multiple values, say 400, held under a single name or variable. And each and every one of the items can be accessed by referring to it's index number as we have discussed above. We can


USING A LIST TO CREATE AN ARRAY
Let's use list of fruits to create an array in this class. You can conveniently use any other type values in your own practice, but for the sake of this class, we will be using List of Fruits.

For Example:

array = ["mango", "orange", "apple", "cherry", "pawpaw"]
print(array)

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

python arrays.PNG


From the screenshot illustrated above, we can see how we created an array using List collection. You can also create array of numbers. However, we can access the elements stored in an array one after the other and print them to the screen as we will see in our next example.

ACCESSING ARRAY ELEMENTS
We can swiftly and seamlessly access the elements stored in an array by referring to it's index number. retrospecting to our explanations given above on array, you will be able to recall how we disscussed elements of an array can be accessed. From what you see above, we can access elements stored in an array in the following ways: **"array[0], array[1], array[2], array[3], array[4], and so on... depending on the number of values it holds.

For Example:
Let's access the third element in the array of fruits below;

array = ["mango", "orange", "apple", "cherry", "pawpaw"]
x = array[2]
print(x)

Output
apple

python array index.PNG


From the screenshot illustrated above, we saw how we were able to get the element at index 2 from the array. This will work as well with other index numbers as far as the index number is within the range of the items stored. For example, we will get an error if we try to access an index number that is not within the range of 5 elements in the array which can be accessed with 5-1 = 4 index. For example, we can't access the array elements with the following index numbers; array[5], array[6], array[7], array[8], and so on...

NB: Copy the code in the screenshot above and run it in your text editor, also try to run however other index numbers by accessing them one after the other.


Without wasting much of our time, let me quickly tell that you how we can manipulate array elements as that of Lists collection. What I mean is that you can do the following on arrays using the following methods as that of lists:

  • Modify or change a value: You can use the method array[3] = "Banana" to change the third element in the array, and so on... Please refer back to our previous class on "Python Lists collection" to learn more about this method.

  • Length of an Array: You can use the len() method as used in lists collection to return the length of an array(i.e. the number of elements stored in the array). Please refer back to our previous class on "Python Lists collection" to learn more about this method.

  • Looping through an Array: You can use the for in keyword to loop through an array of elements to get all the elements stored in the array one after the other. Please refer back to our previous class on "Python Lists collection" to learn more about this method.

  • Adding elements to an Array: You can use the append() and insert() methods to add new elements to the array. Please refer back to our previous class on "Python Lists collection" to learn more about this method.

  • Removing elements from an Array: You can use the pop() and remove() methods to remove elements from an array. Please refer back to our previous class on "Python Lists collection" to learn more about this method.

  • Clearing an Array: You can use the clear() method to remove all the elements from the array.

  • Copying an Array: You can also use the copy() method to copy an array as done in Lists. Please refer back to our previous class on "Python Lists collection" to learn more about this method.

NB: Please make sure you practice along with the teacher because that is the only way to learn programming faster. Also don't forget to copy the code in the examples above and try it out in your own text editor. Please also read on other articles on Arrays to have an indepth knowledge about the subject matter.

There is another way of creating array using array module. Use this material provided below to read about that:
Creating Array using array module



Conclusion

Python Arrays is one of the most efficient ways to make our algorithms and programs run faster. Time and Space complexity are the factors to consider when giving input to a program. Our goal is to maintain efficiency of our programs and that is why we use user-defined data structure like Array as input to our programs. This is because it holds multiple values and processes them at the same time by referring to the elements respective index numbers. We can conveniently use the methods used to manipulate Lists collections to manipulate Array elements too. The methods are: len(), append(), insert(), pop(), remove(), clear(), copy(), and so on... Please as a student, do well to practice along with the teacher, and also make sure you read other materials from other websites to have an in-depth understanding of what I teach you here. In our next class, we will look at "Python Classes/Objects". Thanks for being part of this class, I appreciate your coming!!



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!



THANKS TO THE COMMUNITY ADMINS FOR THE SMOOTH RUNNING OF THIS COMMUNITY
Cc: @steem4nigeria @reminiscence01 @ngoenyi

Sort:  

Never underestimate the importance of having fun! We’ve re-shared this post! 💃🏽😎💚

I can never underestimate the power of having fun, smiles... Thanks for resteeming my post!

 2 years ago 

Screenshot (694).png

You are not eligible for #club5050. Please power up.

I have powered up and I'm now back in club5050 please check again and consider my voting

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.032
BTC 66856.70
ETH 3114.05
USDT 1.00
SBD 3.75