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

in CampusConnect2 years ago (edited)

python class banner day 2 week 2.PNG


Good day everyone!
Hope you're doing great!! Today is the second 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 TUPLES COLLECTION

More like Lists collection, Tuples Collection is also a type of Python collections and also a type of Sequence data types. As discussed in our last class, tuple is ordered, unchangeable, and allows duplicate members. Tuples collection can collect values of the same or different data types. It can collect string data type values, it can also collect number data type values, and it can also collect both string and number data type values in one tuple collection.

Tuple collection is written in parenthesis "()" like this; names = ("Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris"). The instance given is an example of tuple that contains name values of string data type. From the example given, we can see that the name values in the collection is enclosed in parenthesis.

We will detail tuple collection in this class by discussing the following:

  • How to access tuple items.
  • How to change values in a tuple.
  • How to loop through a tuple.
  • Checking if an item exists in a tuple.
  • Checking the length of a tuple.
  • Adding items to the tuple.
  • Removing items from the tuple.
  • Using the tuple() constructor to create a tuple.

EXAMPLE OF TUPLE COLLECTION;

names = ("Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
print(names)

Output
('Anyiglobal', 'Reminiscence', 'Nelson', 'Matto445', 'Starrchris')

python tuple collection.PNG
Tuple collection of name values

If we look at the code above, we can see that the name values in the tuple collection was enclosed in parenthesis "()". Please note the difference between Tuple and List. List is enclosed in square brackets [], while Tuple is enclosed in parenthesis ().


ACCESSING TUPLE ITEMS
We can access any item stored in a tuple collection by just referencing to the item using index numbers in a square bracket. As discussed in our previous class, indexing is used to access any specific item in a collection of values. Read this material to know more about array indexing!

For Example;
Let's access the item at index 0. The item at index 0 is "Steemcurator01".

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
print(names[0])

Output
Steemcurator01

python tuple accessing.PNG
Accessing item in a tuple

From the code above, we can see that we were able able to extract the name Steemcurator01 from the tuple using an index number 0.

NB: Copy the code and try it out in your text editor. Please make sure you practice along with the teacher as that's the only way to learn programming very fast.


CHANGING TUPLE VALUE
We cannot change the value of a tuple collection. Once a tuple collection is created, the values stored in it cannot be changed, this is because tuples are immutable or unchangeable.

For Example;
Let's try to change the value at index number 5 i.e. Starrchris to Arinzegold12. We will see that we will get an error in our terminal during program execution.

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
names[5] = "Arinzegold12"
print(names)

Output
TypeError: 'tuple' object does not support item assignment

python change tuple value.PNG
Changing tuple value

Looking at the code above, from our terminal, we can see that a runtime TypeError occurred as a result of the error we made in our program by trying to change a tuple value which is impossible in Python programming language.

NB: Please copy the code in the example above and try it out in your text editor.


LOOPING THROUGH A TUPLE
By using a for loop, you can loop through a tuple to get all the items stored in a tuple one by one. How does a loop work? Loop works by creating a temporary variable "x" or any other variable name you wish that will hold our tuple values during runtime, and lastly printing all the values of the tuple in the temporary variable to the screen. This can be seen in the example below:

For Example;

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
for x in names:
  print(x)

Output
Steemcurator01
Anyiglobal
Reminiscence
Nelson
Matto445
Starrchris

python tuple loop.PNG
Looping through a tuple

From the code above, we can see that we created a temporary variable "x" during the runtime to hold the values in the tuple before printing them one by one to the screen.

NB: Copy the code above and try it out in your text editor. Please make sure you practice along with the teacher so that you won't be left behind in the course.


CHECKING IF AN ITEM EXISTS IN A TUPLE
To check if an item exists in a tuple, we use the if statement and the "in" keyword. The "in" keyword is a membership operator used to check if a specified value is present in a sequence of values as we discussed in our previous class about Python operators.

For Example;
Let's check if the value "Nelson" is present in the tuple collection, and if it is present, it returned True, otherwise it returned False.

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
if "Nelson" in names:
  print("Yes, Nelson is present in the tuple")
else:
  print("Sorry, Nelson is not present in the tuple")

Output
Yes, Nelson is present in the tuple

python tuple item exists.PNG
Checking if a value exists in a tuple

From the screenshot above, we can see that the code returned True i.e. "Yes, Nelson is present in the tuple" because the name Nelson is present in our tuple collection above. However, if Nelson wasn't present, it would've returned False i.e. "Sorry, Nelson is not present in the tuple".

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


CHECKING THE LENGTH OF A TUPLE
To check the length of a tuple, we use the len() method. When we use the method on our tuple, the program will return the number values contained in the tuple.

For Example;

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
print(len(names))

Output
6

python length check.PNG
Checking the length of a tuple

From the code above, we can see that the program returned 6 as the total number of values contained in the name tuple.

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


ADDING ITEMS TO THE TUPLE
We cannot add items to a tuple because tuples are unchangeable once created. However, if we try to add new item to a tuple, the Python interpreter will raise an error, specifically, TypeError.

For Example;
Let's try to add a new name at index number 6. This will throw an TypeError to us.

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
names[6] = "Victoh98"
print(names)

Output
TypeError

python tuple add items.PNG
Adding items in a tuple

From the code above, we can see that we got an error in our terminal as we try to add new item to the tuple. This is because Tuples are unchangeable once created.

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


REMOVING ITEM FROM THE TUPLE
You cannot also remove any item from the tuple because of the unchangeable nature of Tuples once created.

For Example;
Let's try to delete "Starrchris" from the tuple using the "del" keyword. Note that if we try to do so, we will encounter TypeError during runtime in our terminal.

names = ("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris")
del names[5]
print(names)

Output
TypeError: 'tuple' object doesn't support item deletion

python tuple deletion.PNG
Deleting an item in a tuple

Looking at the screenshot above, we can see in our code that we got a runtime TypeError because we tried deleting an item from the tuple, which is totally impossible in Python programming language.

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


PYTHON TUPLE() CONSTRUCTOR
We can as well use tuple() constructor to make a tuple collection.

For Example;

names = tuple(("Steemcurator01", "Anyiglobal", "Reminiscence", "Nelson", "Matto445", "Starrchris"))
print(names)
print(type(names))

Output
('Steemcurator01', 'Anyiglobal', 'Reminiscence', 'Nelson', 'Matto445', 'Starrchris')
<class 'tuple'>

python tuple constructor.PNG
Python tuple constructor

From the screenshot above, we can see that the tuple() was used to change the collection to a tuple. We also printed the class type of the collection for transparency sake, and we saw in our terminal that the collection belongs to a class of tuple.

NB: Copy the code above and try it out in your text editor. Please make sure you practice along with the teacher so that you won't be lost anywhere in the course!



Conclusion

From this class, we can see that it is very important as a programmer to know the kind of collection you want to create and make use of in your program before creating it because some collections like tuples are immutable i.e. unchangeable once we create the collection. We have covered some of the ways we can manipulate tuples collection in this class; we discussed how we can access an item in a tuple, how to change values in a tuple, how to loop through a tuple, how to check if an item exists in a tuple, how to check the length of a tuple, how to add item to the tuple, how to remove item from the tuple, and how to use the tuple() constructor to create a tuple collection. Thanks for being part of the class, I hope you learnt something new today!



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

Sort:  

Thank you for contributing to #LearnWithSteem theme (#learnwithsteem , #tutorial, and #lesson). This post has been upvoted by @tucsond 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

 2 years ago 

Please with all due respect @tucsond which kind of low percentage vote that you gave me here? This vote is very very discouraging! or does it mean you're stingy of votes? Oh now I see...

Sevengers is a reputable team that work earnestly and equally, but seems you wanna tarnish their reputation. No problem! You just degraded the quality of this post by your upvote. But anyway, all is well!! I have never receive such poor percentage upvote from team sevengers before. Improve yourself next time! Thank you very much!!!

Regards,
Anyiglobal

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

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 71342.73
ETH 3809.46
USDT 1.00
SBD 3.49