博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python学习笔记3---变量与运算符
阅读量:5792 次
发布时间:2019-06-18

本文共 5835 字,大约阅读时间需要 19 分钟。

什么是变量

假设两个list做数学运算
>>> [1,2,3,4,5,6] [1,2,3]Traceback (most recent call last):  File "
", line 1, in
[1,2,3,4,5,6] [1,2,3]TypeError: list indices must be integers or slices, not tuple//A B,先把A乘以3,然后加上B,最后再加上列表A>>> [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]>>> A = [1,2,3,4,5,6]>>> print(A)[1, 2, 3, 4, 5, 6]>>> B = [1,2,3]>>> 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]

变量名的命名规则

变量名命名只能使用字母、数字、下划线
>>> 1a = 2   //变量名的首字母不能是数字SyntaxError: invalid syntax>>> A2 = '1'>>> _2 = '1'>>> A*B='1'SyntaxError: can't assign to operator
系统关键字,不能用在变量名中 保留关键字
>>> and = 1SyntaxError: invalid syntax>>> if = 2SyntaxError: invalid syntax>>> import = 3SyntaxError: invalid syntax>>> type = 3   //type不是系统保留关键字,但是不建议作为变量名,否则极易出错>>> print(type)3>>> type = 1>>> type(1)Traceback (most recent call last):  File "
", line 1, in
type(1)TypeError: 'int' object is not callable>>> 1(1)Traceback (most recent call last): File "
", line 1, in
1(1)TypeError: 'int' object is not callable
python动态语言的特性,声明时不需要指明变量类型
>>> a = '1'>>> a = 1>>> a = (1,2,3)>>> a = {1,2,3}

值类型与引用类型

int、str、tuple是值类型(不可变),list、set、dict是引用类型(可变)

1.int

>>> a = 1>>> b = a>>> a = 3>>> print(b)1

2.list

>>> a = [1,2,3,4,5]>>> b = a>>> a[0] = '1'>>> print(a)['1', 2, 3, 4, 5]>>> print(b)['1', 2, 3, 4, 5]>>> a = [1,2,3]>>> id(a)4405825224>>> hex(id(a))'0x1069b8ec8'>>> a[0]='1'>>> id(a)4405825224>>>

3.str

>>> a = 'hello'>>> a = a + 'python'  //a加上一个新的字符串,不再是原来的字符串了>>> print(a)hellopython>>> b = 'hello'>>> id(b)4405534032>>> b = b + 'python'  //加上新的字符串后,id改变>>> id(b)4355329456>>> 'python'[0]'p'>>> 'python'[0]='o'Traceback (most recent call last):  File "
", line 1, in
'python'[0]='o'TypeError: 'str' object does not support item assignment

4.tuple

>>> a = (1,2,3)>>> a[0] = '1'Traceback (most recent call last):  File "
", line 1, in
a[0] = '1'TypeError: 'tuple' object does not support item assignment>>> b = [1,2,3]>>> b.append(4)>>> print(b)[1, 2, 3, 4]>>> c = (1,2,3)>>> c.append(4)Traceback (most recent call last): File "
", line 1, in
c.append(4)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'>>> a = (1,2,3,[1,2,4])>>> a[2] = '3'Traceback (most recent call last): File "
", line 1, in
a[2] = '3'TypeError: 'tuple' object does not support item assignment>>> a[3][2] = '4'>>> print(a) //元组内的列表可变(1, 2, 3, [1, 2, '4'])

运算符

1.算数运算符:+,-,* ,/,//,%,**

>>> 'hello'+'world''helloworld'>>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3]>>> 3-12>>> 3/21.5>>> 3//2   //整除1>>> 5%2   //求余1>>> 2**2   //求N次方4>>> 2**532

2.赋值运算符:=,+=,-=,*=,/=,%=,**=,//=

>>> c = 1>>> c = c+1>>> print(c)2>>> c+=1>>> print(c)3>>> c-=1>>> print(c)2>>> c++   //python中没有自增和自减运算符SyntaxError: invalid syntax>>> c--SyntaxError: invalid syntax>>> b=2>>> a=3>>> b+=a>>> print(b)5>>> b-=a>>> print(b)2>>> b*=a>>> print(b)6

3.比较(关系)运算符:==,!=,>,<,>=,<=

>>> 1==1True>>> 1>1False>>> 1>=1True>>> a>=bTraceback (most recent call last):  File "
", line 1, in
a>=bNameError: name 'a' is not defined>>> a=1>>> b=2>>> a!=bTrue>>> b=1>>> b+=b>=1 //b=b+True>>> print(b)2>>> print(b>=1) True>>> 1>1False>>> 2>3False>>> 'a'>'b'False>>> ord('a')97>>> ord('b')98>>> 'abc'<'abd' //实际上是a和a比,b和b比,c和d比True>>> ord('abc')Traceback (most recent call last): File "
", line 1, in
ord('abc')TypeError: ord() expected a character, but string of length 3 found>>> ord('c')99>>> ord('d')100>>> [1,2,3]<[2,3,4]True>>> (1,2,3)<(1,3,2)True

4.逻辑运算符:and,or,not

>>> True and TrueTrue>>> True and FalseFalse>>> True or FalseTrue>>> False or FalseFalse>>> not FalseTrue>>> not TrueFalse>>> not not TrueTrue
0 被认为是False,非0 表示True
>>> 1 and 11>>> 'a' and 'b''b'>>> 'a' or 'b''a'>>> not 'a'False>>> a = True>>> b = False>>> a or bTrue>>> b and aFalse
空字符串 False
>>> not 0.1False>>> not ''True>>> not '0'False
空的列表 False
>>> not []True>>> not [1,2]False>>> [1] or [][1]>>> [] or [1][1]>>> 'a' and 'b''b'>>> '' and 'b'''>>> 1 and 00>>> 0 and 10>>> 1 and 22>>> 2 and 11>>> 0 or 11>>> 1 or 01>>> 1 or 21

5.成员运算符:in,not in

>>> a = 1>>> a in [1,2,3,4,5]True>>> b = 6>>> b in [1,2,3,4,5]False>>> b not in [1,2,3,4,5]True>>> b = 'h'>>> b in 'hello'True>>> b not in (1,2,3,4,5)True>>> b not in {1,2,3,4,5}True>>> b = 'a'>>> b in {'c':1}False>>> b = 1>>> b in {'c':1}False>>> b = 'c'>>> b in {'c':1}   //字典里面根据key返回True

6.身份运算符:is,is not

对象的三个特征:id、value、type,判断id用“is”,判断value用“==”,判断type用“isinstance”
>>> a = 1>>> b = 1>>> a is bTrue>>> a='hello'>>> b='world'>>> a is bFalse>>> c='hello'>>> a is cTrue>>> a=1>>> b=2>>> a==bFalse>>> a=1>>> b=1>>> a is bTrue>>> a==bTrue>>> a=1>>> b=1.0>>> a==bTrue>>> a is b   //is不是比较值相等,比较的是两个变量的身份(内存地址)是否相等False>>> id(a)4374928384>>> id(b)4376239272>>> a={1,2,3}>>> b={2,1,3}>>> a==b    //集合是无序的True>>> a is bFalse>>> id(a)4433997384>>> id(b)4433996488>>> c=(1,2,3)>>> d=(2,1,3)>>> c==d    //元组是序列,是有序的False>>> c is dFalse>>> a=1>>> b=2>>> a==bFalse>>> a is bFalse>>> a = 'hello'>>> type(a) == intFalse>>> type(a) == strTrue>>> isinstance(a,str)   //isinstance是判断变量类型的函数True>>> isinstance(a,int)False>>> isinstance(a,(int,str,float))True>>> isinstance(a,(int,float))False

7.位运算符:(==把数字当作二进制数进行运算==)

  • &按位与
  • |按位或
  • ^按位异或
  • ~按位取反
  • <<左移动
  • >>右移动
按位与运算,每一个二进制数位进行对比,两个都为1,则得到1,只要有一个为0,就得到0
>>> a = 2>>> b = 3>>> a & b2
变量 转换为十进制
a 1 0 2
b 1 1 3
按位与 1 0 2
按位或运算,每一个二进制数位进行对比,只要有一个为1,就得到1,两个都为0,则得到0
>>> a = 2>>> b = 3>>> a | b3
变量 转换为十进制
a 1 0 2
b 1 1 3
按位或 1 1 3

转载地址:http://xmwfx.baihongyu.com/

你可能感兴趣的文章
JProfiler学习笔记
查看>>
Loadrunner脚本编程(4)-数据类型操作和字符串操作
查看>>
STL 算法
查看>>
分享:Backbone.js 样例站点与入门指南
查看>>
图的基本算法
查看>>
《架构之美》摘录三
查看>>
HTML基础(一)
查看>>
boost.circular_buffer简介
查看>>
Database Appliance并非Mini版的Exadata-还原真实的Oracle Unbreakable Database Appliance
查看>>
网页图片缩放(js)
查看>>
如何用Fiddler对Android应用进行抓包
查看>>
iOS为所需要的视图添加模糊效果--UIVisualEffectView
查看>>
Kibana登录认证设置
查看>>
volley 应用 GET POST请求 图片异步加载
查看>>
BZOJ-4325: NOIP2015 斗地主 (搜索神题)
查看>>
HDU-1222 Wolf and Rabbit (欧几里得定理)
查看>>
Camera Calibration 相机标定:原理简介(五)
查看>>
ClassCastException:ColorDrawable cannot be cast to RoundRectDrawableWithShadow
查看>>
ehcache实例
查看>>
Linux多线程与同步
查看>>