Python入门学习笔记4:变量与运算符

  1 #变量与运算符
  2 print([1,2,3,4,5,6]*3+[1,2,3]+[1,2,3,4,5,6]) #[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]
  3 #变量 定义变量  变量命名规则:
  4 #1.首字符不能为数字,变量由字母、数字、下划线组成
  5 #2.系统关键字不能用在变量名中,保留关键字
  6 #3.变量名区分大小写
  7 #4.变量类型不固定,是python为动态语言的体现
  8 A = [1,2,3,4,5,6]
  9 B = [1,2,3]
 10 print(A*3 + B + A) #[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]
 11 skill = ['新月打击','苍白之瀑']
 12 print(skill[0:2])#['新月打击', '苍白之瀑']
 13 
 14 # int str tuple (不可改变)值类型
 15 # list set dict (可变) 引用类型
 16 a = 1
 17 b = a
 18 a = 3
 19 print(b) #1
 20 a = [1,2,3,4,5]
 21 b = a
 22 a[0] = '1' # 说明:a[0]并未重新赋值,只是将原有列表进行更改
 23 print(a) #['1', 2, 3, 4, 5]
 24 print(b) #['1', 2, 3, 4, 5]
 25 a = 'hello'
 26 a = a + ' python'
 27 print(a) #hello python
 28 b = 'hello'
 29 print(id(b)) #id()内容地址  4458517168
 30 b = b + ' python'
 31 print(id(b)) # 4368705008
 32 #tuple(元组)  list(列表)
 33 a = [1,2,3]
 34 print("内存地址:",id(a),",16进制:",hex(id(a)),",a = ",a)   #内存地址: 4328269184 ,16进制: 0x101fc2580 ,a =  [1, 2, 3]
 35 a[0] = '1'
 36 print(("内存地址:",id(a),",16进制:",hex(id(a))),",a = ",a) #('内存地址:', 4328269184, ',16进制:', '0x101fc2580') ,a =  ['1', 2, 3]
 37 #列表中添加元素  多维数组
 38 b = [1,2,3]
 39 b.append(4)
 40 print(b) #[1, 2, 3, 4]
 41 a = (1,2,3,[1,2,4])
 42 print(a[3][2]) #二维数组取值 4
 43 a[3][2] = '4'
 44 print(a) #(1, 2, 3, [1, 2, '4'])
 45 a = (1,2,3,[1,2,['a','b','c']])
 46 print(a[3][2][1]) #三维数组取值 b
 47 #运算符:
 48 # 算术运算符(+/-/*/ '/' / '//' /%/**)
 49 # 赋值运算符(=/+=/*= / '/=' /%=/**=/ //=)
 50 # 比较(关系)运算符(==/!=/>/</>=/<=)
 51 # 逻辑运算符(and(且)/or/not)
 52 # 成员运算符(in/not in)
 53 # 身份运算符(is/is not)
 54 # 位运算符(&/|/^/~/<</>>)
 55 print('hello' + " world") #hello world
 56 print([1,2,3]*3)          #[1, 2, 3, 1, 2, 3, 1, 2, 3]
 57 print(3-1)                #2
 58 print(3/2)                #1.5
 59 print(3//2)               #整除 1
 60 print(5%2)                #取余数 1
 61 print("3的三次方:",3**3,",3的四次方:",3**4)  #指数: 3的三次方: 27 ,3的四次方: 81
 62 c = 1
 63 c += 1 # c += 1 等于 c=c+1
 64 print(c) #2
 65 b = 2
 66 a = 3
 67 b += a # b = b + a
 68 print(b) #5
 69 b -= a # b = b-a
 70 print(b) #2
 71 b *= a # b =b * a
 72 print(b) #6
 73 
 74 print(1==1,1>1,1>=1,1<=1,1!=2)#True False True True True
 75 b = 1
 76 b +=b >= 1
 77 print(b,b>=1,int(True)) #2 True 1
 78 print(ord('a'),ord('b'),'a' > 'b') #字符串之间比较的是ascll码值 97 98 False
 79 print('abc'<'abd',[1,2,3] < [2,3,4],(1,2,3) < (1,3,2))#比较列表的每个元素开始比较 True True True
 80 
 81 # 逻辑运算符(and(且)/or/not)
 82 print(True and True, True and False)#True False
 83 print(True or False, False or False)#True False
 84 print(not True,not False,not not True)#False True True
 85 #int float 0 被认为是False, 非0 表示True
 86 print(bool(0),bool(0.1),not 0,not 0.1) #False True True False
 87 #字符串 空字符串False,非空字符串为True
 88 print(bool(''),bool('a'),not '',not 'a')#False True True False
 89 #列表:空False,非空True
 90 print(bool([]),bool([1,2,3]),not [],not [1,2,3])#False True True False
 91 #and 运算结果为True则返回为True的值,    若运算为True,两个值均为True则返回最后一个
 92 #根据每个逻辑运算符,若仅凭第一个可以判断出运算结果后,则直接返回第一个值即可
 93 print([1] or [],[] or [1])#[1] [1]
 94 print(1 and 0,0 and 1)#0 0
 95 print(1 and 2,2 and 1 ,2 and 1 and 3)#2 1 3
 96 print(1 or 0,0 or 1,1 or 2)#1 1 1
 97 
 98 # 成员运算符(in/not in)
 99 a = 1
100 print(a in [1,2,3,4,5])     #True
101 print(a not in [1,2,3,4,5]) #False
102 b=6
103 print(b in [1,2,3,4,5])     #False
104 print(b not in [1,2,3,4,5]) #True
105 print('h' in 'hello','h' not in (1,2,3,4,5),'h' not in {1,2,3,4,5}) #True True True
106 #字典成员运算符 判断是否在字典中存在是判断字典中的key是否存在
107 print('a' in {'c':1},1 in {'c':1},'c' in {'c':1})#False False True
108 
109 # 身份运算符(is/is not)
110 a = 'hello'
111 b = 'world'
112 c = 'world'
113 print(a is b,b is c) #False True
114 # == 比较值是否相等,is 比较身份(内存地址)是否相等  type 比较类型
115 a = 1
116 b = 1.0
117 print(a == b, a is b,id(a),id(b))  #True False 4470852256 4472861808
118 a = {1,2,3}
119 b = {2,1,3}
120 print(a == b,a is b,id(a),id(b))    #True False 4325657952 4325657280
121 c = (1,2,3)
122 d = (2,1,3)
123 print(c == d,c is d)                #元组是序列 False False
124 #对象的三个特征 id(is)、value(==)、type(isinstance)
125 #type不能判断变量的子类是哪种类型,isinstance可以
126 a = 'hello'
127 print(type(a) == int,type(a) == str,isinstance(a,int),isinstance(a,str),isinstance(a,(int,str,float)))#False True False True True
128 
129 # 位运算符-把数字当做二进制数进行运算(&(按位与)/|(按位或)/^(按位异或)/~(按位取反)/<<(左移动)/>>(右移动)) 精确比较当数字转为二进制数后每一位数进行比较
130 a = 2
131 b = 3
132 print(a&b,a|b)#2 3
原文地址:https://www.cnblogs.com/liuxiaoming123/p/13019665.html