Learn Python like a Professional! Take You from 0 to Hero
In this Python lecture, we will learn about the Tuple.
"Tuple is an ordered collection of immutable elements".
Let's look at Tuple example-
Create Tuple
Tuple is defined by placing the all the elements under parentheses ( ) and separated by a comma.
t = (1,2,3,4,5)
print(t)
>> (1,2,3,4,5)
We have an option in python that tuple can hold different object type (integer, float, string etc.).
t = (1,1.5,'string')
print(t)
# len() Calculates the length of tuple
print(len(t))
Tuple Indexing
We can use the index operator [] to access an item in a tuple. Index starts from 0.
my_tuple=(1,2,'three','four')
# Since, we have 4 elements in the tuple. So, the index starts from 0 to 3
print( my_tuple[0] ) # garbs the element at index 0
print( my_tuple[2] ) # garbs the element at index 2
print( my_tuple[-1] ) # garbs the last element
Tuple Slicing
We can access a range of items in a tuple by using the colon.
We have my_tuple = ( 1 , 2 , 'three' , 'four' )
We have my_tuple = ( 1 , 2 , 'three' , 'four' )
print( my_tuple[1:] ) # garbs everything from the first index
print( my_tuple[2:] ) # garbs everything from the second index
print( my_tuple[:3] ) # garbs everything from first index 0 to third index(excluded)
print( my_tuple[:-1] ) # garbs everything from 0 index to the last index(excluded)
print( my_tuple[1:3:2] ) # garbs every 2nd element from the 1st index to 3th index(excluded)
*Important Syntax
print( my_tuple[::-1] ) # Reverse the tuple
Tuple Properties
Mutability
Tuple is immutable, the elements within it can be changed or replaced, unlike string or tuple.
We have my_list = ( 1 , 2 , 'three' , 'four' )
my_tuple[0] = 5
We can also use the + operator to combine/concatenate two tuples.
t1 = ( 1, 2, 3, 4, 5)
t2 = ( 6, 4, 7, 8, 9)
t3 = t1 + t2
print( t3 )
We can also use the * operator to repeats a list for the given number of times.
t1= ( 1, 2, 3 )
print( t1 * 3 )
Built-in Function
Methods that are available with tuple object in Python.
t = (1,1,2,2,2,3,4,5)
print(t)
Note: If press TAB after t. in the Juypter notebook. It will show the list of functions available in the tuple.
We have tuple t = (1,1,2,2,2,3,4,5)
print(t.count(2)) # Return the occurrence of the item
We have tuple t = (1,2,3,4,5,6)
print(t.index(6)) # Return the index of the item
In the next Blog, we will discuss File.
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-10-file.html
......................................................................................................................................
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
Join the FB group for Q&A
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-10-file.html
......................................................................................................................................
Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnwView the Jupyter Notebook for this lecture
Join the FB group for Q&A
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com
No comments:
Post a Comment