The python tuple index, sliced, add, delete, modify,

# When tuple? When operating a database to store --- and does not support any modifications (additions and deletions)
Note 1 : sub-elements # tuple list, you can modify the list of sub-elements
m = (1, 0.02,'hello',[1,2,3],True)
m[3][2] = 'nihao'
print(m)  # (1, 0.02, 'hello', [1, 2, 'nihao'], True)
# Tuple format: tuples tuple symbol ()
# 1 empty tuple
c= ()
# 2. tuple can contain any type of data 
# 3. tuple of elements divided according to a comma
a = (1, 0.02,'hello',[1,2,3],True)
# 4. tuple element, there is an index value, starting from 0 
# 5. Obtain a single value tuple: tuple [index]
print(a[-1])  # True
print(a[2])  # hello
Slicing string with # 6-membered group, the group name list of operand [Index head: tail Index: step]
print(a[::2])  # [1, 'hello', True]
print(a[0:5:2])   # [1, 'hello', True]
Note # 2: tuples there is only one element, we need to add a comma
x = ('hello')
print(type(x))  # <class 'str'>
y = (1)
print(type(y))  # <class 'int'>
z =(1,)
print(type(z))  # <class 'tuple'>

 

 



Guess you like

Origin www.cnblogs.com/kite123/p/11639845.html