In this video, I have discussed about tuples and how to create a tuple, accessing elements, concatenation, deleting elements in a tuple, copying, etc.
A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list.
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas. The parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).
Access Tuple Elements:
There are various ways in which we can access the elements of a tuple.
1. Indexing
We can use the index operator [] to access an item in a tuple, where the index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5. Trying to access an index outside of the tuple index range(6,7,... in this example) will raise an IndexError.
The index must be an integer, so we cannot use float or other types. This will result in TypeError.
2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
3. Slicing
We can access a range of items in a tuple by using the slicing operator colon :.
Changing a Tuple
Unlike lists, tuples are immutable.
This means that elements of a tuple cannot be changed once they have been assigned. But, if the element is itself a mutable data type like a list, its nested items can be changed.
Deleting a Tuple
We cannot change the elements in a tuple. It means that we cannot delete or remove items from a tuple.
Deleting a tuple entirely, however, is possible using the keyword del.
Advantages of Tuple over List
Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain advantages of implementing a tuple over a list. Below listed are some of the main advantages:
We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data types.
Since tuples are immutable, iterating through a tuple is faster than with list. So there is a slight performance boost.
Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible.
If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
#tuple #python #parnikatutorials
00:00 Introduction
00:36 What is a Tuple
01:12 Tuple creation
03:00 Accessing elements
07:31 How to modify elements in a tuple
10:41 Concatenation
11:47 Count & Index
14:18 Implementation in Jupyter Notebook
Website: www.parnikatutorials.in
Email id: parnikatutorials@gmail.com
To get the regular updates:
Telegram link: https://t.me/Parnikatutorials
Facebook: https://m.facebook.com/profile.php?id...
Linkedin: / parnika-tutorials-a8a9831b2
Pinterest: / parnikatutorials0892
Instagram: / parnikatutorials
Playlists:
Virtual Coffee with Jagadeesh: • VIRTUAL COFFEE WITH JAGADEESH
Digital Logic: • ABOUT PARNIKA TUTORIALS
Computer Organization and Architecture: • ABOUT PARNIKA TUTORIALS
C Programming: • L 1: WHAT IS AN ALGORITHM AND CHARACT...
Data Structures: • L 1: Uncover the Benefits of Linked L...
Theory of Computation: • ABOUT PARNIKA TUTORIALS
Compiler Design: • ABOUT PARNIKA TUTORIALS
Operating Systems: • PROCESS STATE DIAGRAM | LONG TERM, SH...
Databases: • ABOUT PARNIKA TUTORIALS
Computer Networks: • ABOUT PARNIKA TUTORIALS
For GATE PYQs and much more explore: / parnikatutorials
Sur cette page du site, vous pouvez voir la vidéo en ligne L 26: TUPLE IN PYTHON | OPERATIONS ON TUPLE IN PYTHON | PYTHON TUTORIAL FOR BEGINNERS durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Parnika Tutorials 01 janvier 1970, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 469 fois et il a aimé 16 téléspectateurs. Bon visionnage!