Skip to main content

10.Python Tuples

PYTHON TUPLES 
    A tuple is a collection of objects which is ordered and unchangeable. Tuples written with round brackets. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists. In other words, we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list. Also tuples use parentheses, whereas lists use square brackets. The iteration is faster in tuple when compared to lists.Therefore you can use tuple to enhance the speed of execution.

    A Tuple is a collection of Python objects separated by commas. In some ways a tuple is similar to a list in terms of indexing, nested objects and repetition. The tuple items can be accessed by referring to the index number, inside square brackets. Once a tuple is created. It is not possible to add items to it or remove items from it. However, it is possible to delete the entire tuple. This is due to the fact that the Tuples are unchangeable. + operator can be used to join two or more tuples. 

Program
Wizard = ("Harry", " Ron", "Hermoine") #tuple creation 
print(Wizard) 
Wizard2 = tuple(("Malfoy", "Hagrid", "Dumbledore"))#with tuple() 
print(Wizard2) 
Wizard3 = Wizard + Wizard2 #union of 2 tuples 
print(Wizard3) 
print(Wizard3[3]) #print the 3rd item 
print(Wizard3[-1]) # to print the last item (reversing) 
print(Wizard3[1:5]) # print the item from 1 to 4 
print(Wizard3[-5:-1]) # in reverse order 
print(len(Wizard3)) #count of items in this tuple 
for x in Wizard3: 
    print(x) 
Wizard[3] = "Luna" # you cannot add /remove items from tuple 
print(Wizard) 
del wizard3 #this tuple is deleted 
Output 
('Harry', ' Ron', 'Hermoine') #wizard
('Malfoy', 'Hagrid', 'Dumbledore') #wizard2
('Harry', ' Ron', 'Hermoine', 'Malfoy', 'Hagrid', 'Dumbledore') #wizard3
Malfoy # 3rd item of wizard3
Dumbledore #last item of wizard3
(' Ron', 'Hermoine', 'Malfoy', 'Hagrid') #items 1 to 4 of wizard3
(' Ron', 'Hermoine', 'Malfoy', 'Hagrid') #in reverse order
#count of items in wizard3
Harry #prints the elements in wizard3
Ron 
Hermoine 
Malfoy 
Hagrid 
Dumbledore 
Traceback (most recent call last): 
File "main.py", line 17, in <module> 
Wizard[3] = "Luna" # you cannot add /remove items from tuple 
TypeError: 'tuple' object does not support item assignment
 Watch the video below to know more about tuples!
Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl