资讯专栏INFORMATION COLUMN

python学习笔记3---变量与运算符

LMou / 1116人阅读

摘要:什么是变量假设两个做数学运算先把乘以,然后加上,最后再加上列表变量名的命名规则变量名命名只能使用字母数字下划线变量名的首字母不能是数字系统关键字,不能用在变量名中保留关键字不是系统保留关键字,但是不建议作为变量名,否则极易出错动态语言的特性

什么是变量
假设两个list做数学运算

</>复制代码

  1. >>> [1,2,3,4,5,6] [1,2,3]
  2. Traceback (most recent call last):
  3. File "", line 1, in
  4. [1,2,3,4,5,6] [1,2,3]
  5. TypeError: list indices must be integers or slices, not tuple
  6. //A B,先把A乘以3,然后加上B,最后再加上列表A
  7. >>> [1,2,3,4,5,6]*3+[1,2,3]+[1,2,3,4,5,6]
  8. [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]
  9. >>> A = [1,2,3,4,5,6]
  10. >>> print(A)
  11. [1, 2, 3, 4, 5, 6]
  12. >>> B = [1,2,3]
  13. >>> A*3 + B + A
  14. [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]
变量名的命名规则
变量名命名只能使用字母、数字、下划线

</>复制代码

  1. >>> 1a = 2 //变量名的首字母不能是数字
  2. SyntaxError: invalid syntax
  3. >>> A2 = "1"
  4. >>> _2 = "1"
  5. >>> A*B="1"
  6. SyntaxError: can"t assign to operator
系统关键字,不能用在变量名中 保留关键字

</>复制代码

  1. >>> and = 1
  2. SyntaxError: invalid syntax
  3. >>> if = 2
  4. SyntaxError: invalid syntax
  5. >>> import = 3
  6. SyntaxError: invalid syntax
  7. >>> type = 3 //type不是系统保留关键字,但是不建议作为变量名,否则极易出错
  8. >>> print(type)
  9. 3
  10. >>> type = 1
  11. >>> type(1)
  12. Traceback (most recent call last):
  13. File "", line 1, in
  14. type(1)
  15. TypeError: "int" object is not callable
  16. >>> 1(1)
  17. Traceback (most recent call last):
  18. File "", line 1, in
  19. 1(1)
  20. TypeError: "int" object is not callable
python动态语言的特性,声明时不需要指明变量类型

</>复制代码

  1. >>> a = "1"
  2. >>> a = 1
  3. >>> a = (1,2,3)
  4. >>> a = {1,2,3}
值类型与引用类型
int、str、tuple是值类型(不可变),list、set、dict是引用类型(可变)

1.int

</>复制代码

  1. >>> a = 1
  2. >>> b = a
  3. >>> a = 3
  4. >>> print(b)
  5. 1

2.list

</>复制代码

  1. >>> a = [1,2,3,4,5]
  2. >>> b = a
  3. >>> a[0] = "1"
  4. >>> print(a)
  5. ["1", 2, 3, 4, 5]
  6. >>> print(b)
  7. ["1", 2, 3, 4, 5]
  8. >>> a = [1,2,3]
  9. >>> id(a)
  10. 4405825224
  11. >>> hex(id(a))
  12. "0x1069b8ec8"
  13. >>> a[0]="1"
  14. >>> id(a)
  15. 4405825224
  16. >>>

3.str

</>复制代码

  1. >>> a = "hello"
  2. >>> a = a + "python" //a加上一个新的字符串,不再是原来的字符串了
  3. >>> print(a)
  4. hellopython
  5. >>> b = "hello"
  6. >>> id(b)
  7. 4405534032
  8. >>> b = b + "python" //加上新的字符串后,id改变
  9. >>> id(b)
  10. 4355329456
  11. >>> "python"[0]
  12. "p"
  13. >>> "python"[0]="o"
  14. Traceback (most recent call last):
  15. File "", line 1, in
  16. "python"[0]="o"
  17. TypeError: "str" object does not support item assignment

4.tuple

</>复制代码

  1. >>> a = (1,2,3)
  2. >>> a[0] = "1"
  3. Traceback (most recent call last):
  4. File "", line 1, in
  5. a[0] = "1"
  6. TypeError: "tuple" object does not support item assignment
  7. >>> b = [1,2,3]
  8. >>> b.append(4)
  9. >>> print(b)
  10. [1, 2, 3, 4]
  11. >>> c = (1,2,3)
  12. >>> c.append(4)
  13. Traceback (most recent call last):
  14. File "", line 1, in
  15. c.append(4)
  16. AttributeError: "tuple" object has no attribute "append"
  17. >>> a = (1,2,3,[1,2,4])
  18. >>> a[2]
  19. 3
  20. >>> a[3]
  21. [1, 2, 4]
  22. >>> a[3][2]
  23. 4
  24. >>> a = (1,2,3,[1,2,["a","b","c"]])
  25. >>> a[3][2][1]
  26. "b"
  27. >>> a = (1,2,3,[1,2,4])
  28. >>> a[2] = "3"
  29. Traceback (most recent call last):
  30. File "", line 1, in
  31. a[2] = "3"
  32. TypeError: "tuple" object does not support item assignment
  33. >>> a[3][2] = "4"
  34. >>> print(a) //元组内的列表可变
  35. (1, 2, 3, [1, 2, "4"])
运算符

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

</>复制代码

  1. >>> "hello"+"world"
  2. "helloworld"
  3. >>> [1,2,3]*3
  4. [1, 2, 3, 1, 2, 3, 1, 2, 3]
  5. >>> 3-1
  6. 2
  7. >>> 3/2
  8. 1.5
  9. >>> 3//2 //整除
  10. 1
  11. >>> 5%2 //求余
  12. 1
  13. >>> 2**2 //求N次方
  14. 4
  15. >>> 2**5
  16. 32

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

</>复制代码

  1. >>> c = 1
  2. >>> c = c+1
  3. >>> print(c)
  4. 2
  5. >>> c+=1
  6. >>> print(c)
  7. 3
  8. >>> c-=1
  9. >>> print(c)
  10. 2
  11. >>> c++ //python中没有自增和自减运算符
  12. SyntaxError: invalid syntax
  13. >>> c--
  14. SyntaxError: invalid syntax
  15. >>> b=2
  16. >>> a=3
  17. >>> b+=a
  18. >>> print(b)
  19. 5
  20. >>> b-=a
  21. >>> print(b)
  22. 2
  23. >>> b*=a
  24. >>> print(b)
  25. 6

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

</>复制代码

  1. >>> 1==1
  2. True
  3. >>> 1>1
  4. False
  5. >>> 1>=1
  6. True
  7. >>> a>=b
  8. Traceback (most recent call last):
  9. File "", line 1, in
  10. a>=b
  11. NameError: name "a" is not defined
  12. >>> a=1
  13. >>> b=2
  14. >>> a!=b
  15. True
  16. >>> b=1
  17. >>> b+=b>=1 //b=b+True
  18. >>> print(b)
  19. 2
  20. >>> print(b>=1)
  21. True
  22. >>> 1>1
  23. False
  24. >>> 2>3
  25. False
  26. >>> "a">"b"
  27. False
  28. >>> ord("a")
  29. 97
  30. >>> ord("b")
  31. 98
  32. >>> "abc"<"abd" //实际上是a和a比,b和b比,c和d比
  33. True
  34. >>> ord("abc")
  35. Traceback (most recent call last):
  36. File "", line 1, in
  37. ord("abc")
  38. TypeError: ord() expected a character, but string of length 3 found
  39. >>> ord("c")
  40. 99
  41. >>> ord("d")
  42. 100
  43. >>> [1,2,3]<[2,3,4]
  44. True
  45. >>> (1,2,3)<(1,3,2)
  46. True

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

</>复制代码

  1. >>> True and True
  2. True
  3. >>> True and False
  4. False
  5. >>> True or False
  6. True
  7. >>> False or False
  8. False
  9. >>> not False
  10. True
  11. >>> not True
  12. False
  13. >>> not not True
  14. True
0 被认为是False,非0 表示True

</>复制代码

  1. >>> 1 and 1
  2. 1
  3. >>> "a" and "b"
  4. "b"
  5. >>> "a" or "b"
  6. "a"
  7. >>> not "a"
  8. False
  9. >>> a = True
  10. >>> b = False
  11. >>> a or b
  12. True
  13. >>> b and a
  14. False
空字符串 False

</>复制代码

  1. >>> not 0.1
  2. False
  3. >>> not ""
  4. True
  5. >>> not "0"
  6. False
空的列表 False

</>复制代码

  1. >>> not []
  2. True
  3. >>> not [1,2]
  4. False
  5. >>> [1] or []
  6. [1]
  7. >>> [] or [1]
  8. [1]
  9. >>> "a" and "b"
  10. "b"
  11. >>> "" and "b"
  12. ""
  13. >>> 1 and 0
  14. 0
  15. >>> 0 and 1
  16. 0
  17. >>> 1 and 2
  18. 2
  19. >>> 2 and 1
  20. 1
  21. >>> 0 or 1
  22. 1
  23. >>> 1 or 0
  24. 1
  25. >>> 1 or 2
  26. 1

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

</>复制代码

  1. >>> a = 1
  2. >>> a in [1,2,3,4,5]
  3. True
  4. >>> b = 6
  5. >>> b in [1,2,3,4,5]
  6. False
  7. >>> b not in [1,2,3,4,5]
  8. True
  9. >>> b = "h"
  10. >>> b in "hello"
  11. True
  12. >>> b not in (1,2,3,4,5)
  13. True
  14. >>> b not in {1,2,3,4,5}
  15. True
  16. >>> b = "a"
  17. >>> b in {"c":1}
  18. False
  19. >>> b = 1
  20. >>> b in {"c":1}
  21. False
  22. >>> b = "c"
  23. >>> b in {"c":1} //字典里面根据key返回
  24. True

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

对象的三个特征:id、value、type,判断id用“is”,判断value用“==”,判断type用“isinstance”

</>复制代码

  1. >>> a = 1
  2. >>> b = 1
  3. >>> a is b
  4. True
  5. >>> a="hello"
  6. >>> b="world"
  7. >>> a is b
  8. False
  9. >>> c="hello"
  10. >>> a is c
  11. True
  12. >>> a=1
  13. >>> b=2
  14. >>> a==b
  15. False
  16. >>> a=1
  17. >>> b=1
  18. >>> a is b
  19. True
  20. >>> a==b
  21. True
  22. >>> a=1
  23. >>> b=1.0
  24. >>> a==b
  25. True
  26. >>> a is b //is不是比较值相等,比较的是两个变量的身份(内存地址)是否相等
  27. False
  28. >>> id(a)
  29. 4374928384
  30. >>> id(b)
  31. 4376239272
  32. >>> a={1,2,3}
  33. >>> b={2,1,3}
  34. >>> a==b //集合是无序的
  35. True
  36. >>> a is b
  37. False
  38. >>> id(a)
  39. 4433997384
  40. >>> id(b)
  41. 4433996488
  42. >>> c=(1,2,3)
  43. >>> d=(2,1,3)
  44. >>> c==d //元组是序列,是有序的
  45. False
  46. >>> c is d
  47. False
  48. >>> a=1
  49. >>> b=2
  50. >>> a==b
  51. False
  52. >>> a is b
  53. False
  54. >>> a = "hello"
  55. >>> type(a) == int
  56. False
  57. >>> type(a) == str
  58. True
  59. >>> isinstance(a,str) //isinstance是判断变量类型的函数
  60. True
  61. >>> isinstance(a,int)
  62. False
  63. >>> isinstance(a,(int,str,float))
  64. True
  65. >>> isinstance(a,(int,float))
  66. False

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

&按位与

|按位或

^按位异或

~按位取反

<<左移动

>>右移动

按位与运算,每一个二进制数位进行对比,两个都为1,则得到1,只要有一个为0,就得到0

</>复制代码

  1. >>> a = 2
  2. >>> b = 3
  3. >>> a & b
  4. 2
变量 转换为十进制
a 1 0 2
b 1 1 3
按位与 1 0 2
按位或运算,每一个二进制数位进行对比,只要有一个为1,就得到1,两个都为0,则得到0

</>复制代码

  1. >>> a = 2
  2. >>> b = 3
  3. >>> a | b
  4. 3
变量 转换为十进制
a 1 0 2
b 1 1 3
按位或 1 1 3

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44884.html

相关文章

  • Python学习笔记2(解释器+算符

    摘要:解释器的系统上,一般默认的版本为,我们可以将安装在目录中。中的按位运算法则如下下表中变量为,为,二进制格式如下逻辑运算符图片逻辑运算符测试实例中包含了一系列的成员,包括字符串,列表或元组。 3.Python3解释器 Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /usr/local/python3 目录中。 安装完成后,...

    happyhuangjinjin 评论0 收藏0
  • python3学习笔记(2)----python的数据类型

    摘要:的基本数据类型中的变量不需要声明。在里,只有一种整数类型,表示为长整型,没有中的。字符串的截取的语法格式如下变量头下标尾下标索引值以为开始值,为从末尾的开始位置。列表列表是中使用最频繁的数据类型。注意构造包含或个元素的元组的特殊语法规则。 1、python3的基本数据类型 Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在 Python 中,...

    陆斌 评论0 收藏0
  • python3学习笔记(1)----基本语法

    摘要:一的基本语法缩进统一个或者个空格。中的数据类型中有个标准类型数字字符串列表元组集合字典数字复数在中,只有一种整数类型,表示长整型。如则会显示,并不是换行。空行与代码缩进不同,空行并不是语法的一部分。我们将首行及后面的代码组称为一个子句。 一、python3的基本语法 1、缩进统一(1个tab或者4个空格)。 for i in range(10): print (i) ...

    yanwei 评论0 收藏0
  • python学习笔记4---分支、循环、条件枚举

    摘要:表达式表达式是运算符和操作数所构成的序列运算符优先级同级的运算符的优先级还是有区别的比如逻辑运算符里的的优先级大于两个括号同级,左结合出现赋值符号时,右结合优先级在文本文件中编写代码脚本是后缀名为的文件,通过命令行执行推荐的,大型工程适合用 表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 >>> 1 + 1 2 >>> a ...

    livem 评论0 收藏0
  • Python 3 学习笔记之——数据类型

    摘要:常量的值近似为。在后传入一个整数可以保证该域至少有这么多的宽度表示浮点数保留位小数常量的值近似为。 1. 数字 类型 int, float, bool, complex type() 查看变量类型 isinstance(a, int) 查看变量类型 showImg(https://segmentfault.com/img/remote/1460000016789047); 运算符 ...

    Riddler 评论0 收藏0

发表评论

0条评论

LMou

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<