PYTHON | Part - 5 | Tuples in Python
Автор: High on Code
Загружено: 2020-06-25
Просмотров: 61
Описание:
Hello Everyone,
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.)
A tuple can also be created without using parentheses. This is known as tuple packing.
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 :
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 list, its nested items can be changed.
[11:27 AM, 6/26/2020] Dhulo: for list
[11:28 AM, 6/26/2020] Shubh Jani: ??
[11:29 AM, 6/26/2020] Dhulo: Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data types used in Python.
In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas.
It can have any number of items and they may be of different types (integer, float, string, etc.).
A list can also have another list as an item. This is called a nested list.
How to access elements from a list?
There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5 elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an IndexError. The index must be an integer. We can't use float or other types, this will result in TypeError.
Nested lists are accessed using nested indexing.
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.
We can access a range of items in a list by using the slicing operator :(colon).
So, Stay Tuned for more.
Check us out for more updates:
Instagram - https://bit.ly/2WJioSw
Facebook - https://bit.ly/2VckgCq
Join our Whatsapp Group for regular updates and doubt solving.
Whatsapp Group - https://bit.ly/2xDhcpP
If you are having any suggestions for us to make your learning a better experience, write to us in the COMMENT section.
Don't forget to LIKE, SHARE & SUBSCRIBE
Thank You for watching!!
#PYTHON #webdevelopment #TuplesinPython #Tuples
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: