Wednesday, 7 February 2018

Python Tuples - Day 5(posted next day)


  • Tuples are immutable objects,meaning they cannot be changed/altered ,they can only be assigned
  •   
    # tuplevar1 = ("a","b","c")
    # tuplevar2 = "a","b","c"
    # print(tuplevar1)
    # print(tuplevar2)
    # print(("a","b","c"))
    # print("a","b","c")
    #
    # #tuples are immutable objects,meaning they cannot be changed/altered ,
    #they can only be assigned
    # welcome = "hi","hello",2018
    # print(welcome)
    # print(welcome[0])
    # #the below line of code will give an error ,since we are trying to alter the tuple
    # # welcome[0]="hii"
    # print(welcome)
    #
    # tupvar1 = "hi","ji"
    # print(tupvar1)
    # tupvar2 = welcome[1],tupvar1[0],2019
    # print(tupvar2)
    #
    # #where as the list object can be altered like below
    # listvar1 = ["hi","hello",2020]
    # print(listvar1)
    # listvar1[0]="Hii"
    # print(listvar1)
    
    #right side expression is evaluated first
    # a , b = 1,2
    # print(a,b)
    # c=d=e=f=2
    # print(c,d,e)
    # a,b =b,a
    # print(a,b)
    #
    # albumtuplevar1 = "muthu","rahman",2000
    # print(albumtuplevar1)
    # title,composer,year =albumtuplevar1
    # print(title)
    # print(composer)
    # print(year)
    #the below piece of code will throw an error like ValueError: not enough values 
    #to unpack (expected 4, got 3)
    # var1,var2,var3,var4=albumtuplevar1
    # print(var1)
    # print(var2)
    # print(var3)
    # print(var4)
    #the below piece of code will throw an error likeValueError: 
    #too many values to unpack (expected 2
    # var1,var2=albumtuplevar1
    # print(var1)
    # print(var2)
    #append work in tuple
    # albumtuplevar1.append("Action")
    
    #tuple inside tuple
    # albumtuplevar1 = "muthu","rahman",2000,((1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya"))
    # print(albumtuplevar1)
    # title,composer,year,tracks =albumtuplevar1
    # print(title)
    # print(composer)
    # print(year)
    # print(tracks)
    # albumtuplevar2 = "muthu","rahman",2000,(1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya")
    # print(albumtuplevar2)
    # title,composer,year,track1,track2,track3 =albumtuplevar2
    # print(title)
    # print(composer)
    # print(year)
    # print(track1)
    # print(track2)
    # print(track3)
    #
    # #printing the tracks w/o knowing the count
    # title,composer,year,tracks =albumtuplevar1
    # print(title)
    # print(composer)
    # print(year)
    # for song in tracks:
    #     no,songtitle = song
    #     print("songno-{},songtitle-{}".format(no,songtitle))
        # print(song)
    
    #mutable object inside a tuple can be altered,like the below example the list 
    #inside the tuple can be changed
    albumtuplevar3 = "muthu","rahman",2000,[(1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya")]
    print(albumtuplevar3)
    print(albumtuplevar3[3])
    albumtuplevar3[3].append((4,"thillana thillana"))
    print(albumtuplevar3)
    for song in albumtuplevar3[3]:
        no,songtitle = song
        print("songno-{},songtitle-{}".format(no,songtitle))
    title,composer,year,tracks =albumtuplevar3
    tracks.append((5,"kokku seva kokku"))
    print(albumtuplevar3)
    for song in tracks:
        no,songtitle = song
        print("songno-{},songtitle-{}".format(no,songtitle))
     
    

    No comments:

    Post a Comment