Sunday, September 16, 2018

Python Ninja Bootcamp 9-Tuple

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)
>> ( 1,1.5,'string' )

# len() Calculates the length of tuple
print(len(t))
>>3



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
>>1

print( my_tuple[2] ) # garbs the element at index 2
>> 'three'

print( my_tuple[-1] ) # garbs the last element
>> 'four'


Tuple Slicing


We can access a range of items in a tuple by using the colon.

We have my_tuple = ( 1 , 2 , 'three' , 'four' ) 
print( my_tuple[1:] ) # garbs everything from the first index 
>> ( 2 , 'three' , 'four' ) 


print( my_tuple[2:] ) # garbs everything from the second index 
>> ( 'three' , 'four' )


print( my_tuple[:3] ) # garbs everything from first index 0 to third index(excluded)
>> ( 1 , 2 , 'three' )


print( my_tuple[:-1] ) # garbs everything from 0 index to the last index(excluded)
>> ( 1 , 2 , 'three' )


print( my_tuple[1:3:2] ) # garbs every 2nd element from the 1st index to 3th index(excluded) 
>> (2,)

*Important Syntax
print( my_tuple[::-1] ) #  Reverse the tuple
>> ( 'four' , 'three' , 2 , 1 )


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  
>> TypeError: 'tuple' object does not support item assignment

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 )
>> ( 1 , 2, 3, 4, 5, 6, 4, 7, 8, 9 )


We can also use the * operator to repeats a list for the given number of times.
t1= ( 1, 2, 3 )
print( t1 * 3 )
>> ( 1,2,3,1,2,3,1,2,3 )


Built-in Function



Methods that are available with tuple object in Python.


t = (1,1,2,2,2,3,4,5)
print(t)
>> (1,1,2,2,2,3,4,5)

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 
>> 3

We have tuple t = (1,2,3,4,5,6)
print(t.index(6)) # Return the index of the item 
>>5



EDITS ARE WELCOMED!!

In the next Blog, we will discuss File. 

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-_1R1xnw


View the Jupyter Notebook for this lecture

Download 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