python基本语法:多维度索引与位运算符

学习视频:《Python3入门与进阶》
第五章 变量与运算符

b = [1,2,3]
b.append(4)      
print(b)     #[1,2,3,4]
#如果是元组就会报错
c = (1,2,3)
c.append(4)
print(c)     #AttributeError: 'tuple' object has no attribute 'append'

多维度索引

#二维的
a = (1,2,3,[1,2,4])
a[2]       #3
a[3]      #[1,2,4]
a[3][2]   #4
#三维的
a = (1,2,3,[1,2,['a','b','c']])
a[3][2][1]     #'b'
#关系运算符
1==1    #True
1>1      #False
#算数运算符
2**4    #16    乘方运算
#身份运算符
a=1  
b=1.0
a ==b  #True   ==是比较两个值是否相等
a is b   #False     is比较的是两个变量的**内存地址**是否相等

a={1,2,3}
b={2,1,3}
print(a==b)       #True  集合是无序的
print(a is b)      #False  比较两者内存地址 

c=(1,2,3)
d=(2,1,3)
print(c==d)      #False  序列是有序的
print(c is d)      #False

#类型判断
a='hello'
type(a)==int     #False
type(a)==str     #True
isinstance(a,str)    #True
isinstance(a,int)    #False
isinstance(a,(int,str,float))   #True    **(int,str,float)是tuple类型**

对象的三个特征 id(内存地址)、value(值)、type(类型)
它们的判断方式分别是 is, ==, isinstance

位运算符
把数字当做二进制数进行运算

a=2
b=3
print(a&b)     #2
print(a|b)      #3

猜你喜欢

转载自blog.csdn.net/weixin_42610407/article/details/86666423