Skip to content

Instantly share code, notes, and snippets.

@LucienZhang
Last active June 24, 2021 08:17
Show Gist options
  • Select an option

  • Save LucienZhang/bb5a97fad1715cc065b745c6ee75d0dc to your computer and use it in GitHub Desktop.

Select an option

Save LucienZhang/bb5a97fad1715cc065b745c6ee75d0dc to your computer and use it in GitHub Desktop.
How to learn Python 3

现在要聊的是:Python位运算符,地址:https://www.runoob.com/python3/python3-basic-operators.html#ysf5

& | ^ ~ 都不用管,从来都用不到

左移和右移也很少用,但解释一下,以左移为例:

左移运算符:<<,理解这个运算符要知道数字的二进制存储方式,比如数字 5, 以二进制方式表示为 101 (为什么呢?以下这个例子展示为什么5等于101):

十进制数字 二进制表示
0 0
1 1
2 10
3 11
4 100
5 101

左移,就是把二进制的所有数字往左移动一位,最右边补0,所以二进制101左移一位就是1010,101等于5,1010等于10,所以5左移一位是10.

右移动同理,就是往右边移

左移和右移很少用,用的效果“大概”是:左移一位,这个数乘以二,右移一位,这个数除以二。

把5左移两位,用这个运算符写出来就是 5<<2

把8右移三位,就是8>>3

逻辑运算符要会用: and or not,注意运算顺序的问题,比如:

  1. not a and b 和 not (a and b) 是不一样的
  2. a and b or c 和 a and (b or c) 是不一样的

这里很容易出错

教程:https://www.runoob.com/python3/python3-tutorial.html 注意!不要全看只看从Python3注释Python3集合的一部分 如果喜欢,你可以在自己的电脑上安装Python3,但你也可以 使用repl来在线写程序并与我共享

查看教程并理解以下概念:

  1. 注释
  2. 数据类型 (教程里都看一遍,但不用记住)
    1. 不可变数据
      1. number
      2. string
      3. tuple
    2. 可变数据
      1. list
      2. dictionary
      3. set
  3. 运算符
  4. 要记住的东西:
    1. 数字
      1. 操作符:+ - * / ** //
      2. 函数 int() float()
    2. 字符串
      1. 生成,删除
      2. 操作符 + * in not in
      3. f-string
      4. 索引 [] [:]
    3. list
      1. 生成,删除
      2. len() + * in not in
      3. append() pop()
      4. 索引 [] [:]
    4. tuple
      1. 生成
      2. len() + * in not in
    5. dictionary
      1. 生成,删除
      2. len() in not in
      3. items() keys() values()
    6. set
      1. 生成,删除
      2. len() in not in
      3. - | & ^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment