Python --007--Original

Original (tuple)

· Bracket operator: '()'

Function:
1. Control the operation priority (the highest priority)

1+2*3    (1+2)*3
1+2**3   (1+2)**3
1    (1)表示1  一个数字加圆括号值与类型都不变

2. As a component of the ancestor (not required)

· Origin

List:

元素:可以任意修改mutable,任意插入或者删除一个元素

Origin:

元素:不可以修改 immutable,元祖本身不可以改变

·创建一个元祖

1. Basically create an ancestor

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

2. Access a tuple

t[0]#访问下标0的元素

3. The sharding of Yuanzu

同列表一样
t[0:3] #取前3个值
t[:3]#取前三个元素
t2=t[:] #拷贝元祖,并且让t2指向新的元祖

4. Yuanzu cannot be modified

l=[1,2,3,4,5]
t=(1,2,3,4,5)
print t[0]# 报错not support assignment
print l[0]#正常不会报错

5. When there is only one element in the ancestor

t=(10,)  不加逗号就是整数

6. Create a tuple without parentheses

t=10,
与t=(10,)效果一样
t=10,20,30,40
也是一个元祖

The print function adds parentheses to the tuple

print 1,2,3,4#输出四个值
print (1,2,3,4)#输出元祖
t=(1,2,3,4)
print t

7, sky ancestor

t=()
print t

t=(,)#语法错误

If there are elements in the tuple, use a comma to distinguish whether it is a tuple, if there is no element, use parentheses to distinguish whether it is a tuple or not

8. Multiplication operation

t=(1,2,3)
t2=t*3
print t2  #(1,2,3,1,2,3,1,2,3)

l=[1,2,3]
l*3
print l 

*List and tuple multiplication are converted into addition of multiple lists and tuple

· Original update

(1,2,3)
在1后面插入4,可以这样操作
t=(1,2,3)  #创建一个元祖,
t1=t[0:1]#通过分片截取拷贝之前的内容
t2=t[1:]#通过分片截取后面的内容
t3=t1+(4,)+t2  #(1,2,3,4),将两个截取的数据进行相加拼接
t=t3 #拼接数据重新赋值t3指向的元祖用t也指向
分片:把原元祖通过下标截取一段数据,并且拷贝出来
以前的旧元祖从来没有发生改变

·Deletion of Yuanzu

del直接删除
t=(1,2,3)
del t

Deleting the entire tuple, to be precise, deleting the tag (variable name) of the tuple and the tuple data itself is not
deleted. When there are multiple tags pointing to a tuple at the same time, the variable name will be deleted one by one, and the tuple itself is still there.
Data that is not pointed to will be collected as garbage.

· Yuanzu related operators

1. The operator of the ancestor splicing:

t1=(1,2,3)
t2=(4,5,6)
t=t1+t2

2. Repeat operator: '*'

t=(1,2,3)
t1=t*3#(1,2,3,1,2,3,1,2,3)

3. Relational operators:

< >  <= >=

t1=(1,2,3)

t2=(4,5,6)
print t1<t2
True
first compare and then compare

4. Logical operators:

    and or not

5. Member operator:

in /not in 
t1=(1,2,3)
print 1 in t1

True

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324517967&siteId=291194637