Python with Jupyter Notebook Tutorial - Lists & Tuples (HD) Part 5
This tutorial covers tuples in Python, including their declaration with parentheses, tuple packing, and unpacking techniques, demonstrated within Jupyter Notebook.
đĽ Related Trending Topics
LIVE TRENDSThis video may be related to current global trending topics. Click any trend to explore more videos about what's hot right now!
THIS VIDEO IS TRENDING!
This video is currently trending in Thailand under the topic 'สภาŕ¸ŕ¸ŕ¸˛ŕ¸ŕ¸˛ŕ¸¨'.
About this video
Tuple: A tuple is a collection of values and it is declared using parentheses. However, we can also use a tuple packing to do the same, and unpacking to assign its values to a sequence.
Lists: Unlike in C++, we donât have arrays to work with in Python. Here, we have a list instead.
Mutability
a. A List is Mutable
Letâs first see lists. Letâs take a new list for exemplar purposes.
list1=[0,1,2,3,4,5,6,7]
Now first, weâll try reassigning an element of a list. Letâs reassign the second element to hold the value 3.
list1[1]=3
list1
[0, 3, 2, 3, 4, 5, 6, 7]
Again, letâs see how we can reassign the entire list.
list1=[7,6,5,4,3,2,1,0]
list1
[7, 6, 5, 4, 3, 2, 1, 0]
It worked, great.
Now, we will delete just one element from the list.
del list1[1]
list1
[7, 5, 4, 3, 2, 1, 0]
This was easy, but could we delete a slice of the list? Letâs try it.
del list1[3:]
list1
[7, 5, 4]
We can access a slice the same way. Can we reassign a slice?
nums=[1,2,3,4,5]
nums[1:3]=[6,7,8]
nums
[1, 6, 7, 8, 4, 5]
Indeed, we can. Finally, letâs try deleting the entire list.
del list1
list1
Traceback (most recent call last):
File "pyshell#67", line 1, in module
list1
NameError: name âlist1â is not defined
The list doesnât exist anymore.
b. A Tuple is Immutable
Now, letâs try doing the same things to a tuple. We know that a tuple is immutable, so some of these operations shouldnât work. Weâll take a new tuple for this purpose.
mytuple=0,1,2,3,4,5,6,7
First, letâs try reassigning the second element.
mytuple[1]=3
Traceback (most recent call last):
File "pyshell#70", line 1, in module
mytuple[1]=3
TypeError: âtupleâ object does not support item assignment
As you can see, a tuple doesnât support item assignment.
However, we can reassign an entire tuple.
mytuple=2,3,4,5,6
mytuple
(2, 3, 4, 5, 6)
Next, letâs try slicing a tuple to access or delete it.
mytuple[3:]
(5, 6)
del mytuple[3:]
Traceback (most recent call last):
File "pyshell#74", line 1, in module
del mytuple[3:]
TypeError: âtupleâ object does not support item deletion
As is visible, we can slice it to access it, but we canât delete a slice. This is because it is immutable.
Can we delete a single element?
del mytuple[3]
Traceback (most recent call last):
File "pyshell#75", line 1, in module
del mytuple[3]
TypeError: âtupleâ object doesnât support item deletion
Apparently, the answer is no.
Finally, letâs try deleting the entire tuple.
del mytuple
mytuple
Traceback (most recent call last):
File "pyshell#77", line 1, in module
mytuple
NameError: name âmytupleâ is not defined
So, here, we conclude that you can slice a tuple, reassign it whole, or delete it whole. But you cannot delete or reassign just a few elements or a slice.
Let us proceed with more differences between python tuples vs lists.
Functions
Some python functions apply on both, these are- len(), max(), min(), sum(), any(), all(), sorted(). Weâll take just one example here for both containers.
max((1,3,-1))
3
max([1,3,-1])
3
Methods
Lists and tuples share the index() and count() methods. But other than those, there are a few methods that apply to lists. These are- append(), insert(), remove(), pop(), clear(), sort(), and reverse(). Letâs take an example of one of these.
[1,3,2].index(3)
1
(1,3,2).index(3)
1
To get an insight into all of these methods and functions we mentioned, you should refer to our articles on lists and tuples.
Tuples in a List
We can store tuples in a list when we want to.
mylist=[(1,2,3),(4,5,6)]
type(mylist)
class 'list'
type(mylist[1])
class âtupleâ
But when would we need to do this? Take an example.
list(students)
[(1, âABCâ), (2, âDEFâ), (3, âGHIâ)]
Lists in a Tuple
Likewise, we can also use a tuple to store lists. Letâs see how.
mytuple=([1,2],[3,4],[5,6])
Nested Tuples
A tuple may hold more tuples, and this can go on in more than two dimensions.
mytuple=((1,2),(3,(4,5),(6,(7,(8,9)))))
To access the element with the value 8, we write the following code.
mytuple[1][2][1][1][0]
8
Nested Lists
Similarly, a list may hold more lists, in as many dimensions as you want.
mylist=[[1,2],[3,4]]
myotherlist=[[1,2],[3,[4,5]]]
To access the element with the value 5, we write the following c ode.
myotherlist[1][1][1]
5
Video Information
Views
1.0K
Total views since publication
Likes
7
User likes and reactions
Duration
7:02
Video length
Published
Apr 2, 2019
Release date
Quality
hd
Video definition
About the Channel
Tags and Topics
This video is tagged with the following topics. Click any tag to explore more related content and discover similar videos:
#Python with Jupyter Notebook Tutorial - Getting familiar (HD) #Jupyter Notebook #Jupyter #Programming #Getting Started with python #Beginner tutorial on python #python tutorial #Computer Science #ipynb #python 3 #layman's terms #tutorial #teaching #data science #machine learning #hello world #anaconda navigator #anaconda #coding #installing python #how to #how to use python #Python with Jupyter Notebook Tutorial - String Manipulation (HD) Part 4 #String Manipulation #String Slicing
Tags help categorize content and make it easier to find related videos. Browse our collection to discover more content in these categories.