Tuples and lists are similar, except that tuples are two times the list is processed, the list is [], the tuple is (), and the tuple cannot be modified, added, deleted, and it is recommended to add a comma (,) after the tuple is written.
Tuples represent:
Tu = (the "DDS",)
1. Index value or slice value
# !/usr/bin/env python # index value tu = (tu[0, "DDS",) n= "print"(n) # Slice Fetch value n1=tu[0:3]print(N1)
Results:
1(1, 2, 3)
2. For loop value
# !/usr/bin/env python # for loop value Tu = (-----,"DDS",) for -in tu: Print(a)
Results:
123DDS
3. Convert strings and lists into tuples
# !/usr/bin/env python # string conversion to tuple s="ssshdj"= tuple (s)print(S1) # List converted to tuple li=["1df", 12,11= tuple (li)print (S2)
Results:
('s' s ' h ' s ' 'd'J') (' 1df', 12, 11)
4, the first level element of the tuple cannot be modified, but the element that gets to the list can be modified
# !/usr/bin/env python # the first-level element of a tuple cannot be modified tu= (1,"ww","rtr", [(45,87)],2,) S1=tu[3]print(S1) S2=tu[3][0]print(S2) S3 =tu[3][0]=100Print(S3)print(TU)
Results:
[(a)] (the"ww""rtr ") , [100], 2)
Python Learning meta-group