1.“>>>”是python的提示符

2.控制台程序“.py”,窗口程序“.pyw”。

3.检查语法错误:check module,run module。

4.运行程序:打开文件所在位置,shift+右键,输入python 文件名,即可运行。

5.常用快捷键:

Alt+p:浏览历史命令(上一条)

Alt+n:浏览历史命令(下一条)

Ctrl+F6:重启shell,之前定义的对象和导入的模块全部失效

F1:打开Python帮助文档

Alt+/:自动补全前面曾经出现过的单词,如果之前有多个单词具有相同前缀,则在多个单词中循环选择

Ctrl+】:缩进代码块

Ctrl+【:取消缩进代码块

Alt+3:注释代码块

Alt+4:取消注释代码块

Tab:补全单词

6.pip命令,没听太懂(基础视频2–5分钟左右)

7.Python的对象模型

python 重复上一行命令 python怎么重复上一段命令_bc


8.惰性求值的特点:释放一次就没了

>>> r=zip('abcdefg','12345')
>>> r
<zip object at 0x000001437386BB08>
>>> ('a','1')in r
True
>>> ('a','1')in r
False

具有该特点的有:range对象,zip对象,enumerate对象,map对象,filter对象等等。
9.Python中,不需要事先声明变量名及其类型,直接赋值即可创建各种类型的变量。Python属于强类型编程语言和动态类型语言,变量的类型也是可以随时变化的。

>>> x=3
>>> print(type(x))
<class 'int'>
>>> x='Hello world.'
>>> print(type(x))
<class 'str'>
>>> x=[1,2,3]
>>> print(type(x))
<class 'list'>
>>> isinstance(3,int)
True
>>> isinstance('hello world',str)
True

/isinstance:测试对象是否是某个类型的实例/

>>> x=[1,2,3]
>>> print(x)
[1, 2, 3]
>>> x[1]=5
>>> print(x)
[1, 5, 3]
>>> print(x[2])
3

字符串和元组属于不可变序列,不能通过下标的方式来修改其中的元素值,试图修改元组中元素的值时会抛出异常

>>> x=(1,2,3)
>>> print(x)
(1, 2, 3)
>>> x[1]=5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> print(x)
(1, 2, 3)

Python允许多个变量指向同一个值

>>> x=3
>>> id(x)
140708234096960
>>> y=x
>>> id(y)
140708234096960

但是当改变变量的内存地址后,并不影响另一个变量

>>> x+=6
>>> id(x)
140708234097152
>>> y
3
>>> id(y)
140708234096960
>>> id(y+6)
140708234097152

10.Python采用的是基于值的内存管理方式,也就是说只针对数值进行存储,如果给不同变量赋予相同的值,这个值在内存中只有一份,且多个变量指向同一块内存地址。大大提高了内存的利用率和使用效率。

>>> x=9
>>> id(x)
140708234097152
>>> y=9
>>> id(y)
140708234097152
>>> x=[1,1,1,1]
>>> id(x[0])==id(x[1])
True
>>> id(x[0])==id(x[3])
True

11.Python具有自动内存管理功能,对于没有任何变量指向的值,Python自动将其删除。并且会跟踪所有的值,随时删除不再有变量指向的值。但是似乎用del命令删除不需要的值,仍然是个好习惯。
12.在定义变量的时候需要注意:

  • 变量名必须以字母或下划线开头,以下划线开头的变量在Python中有特殊含义。
  • 变量名中不能有空格以及标点符号(括号,引号,逗号,斜线,反斜线,冒号,问号,句号等等)。
  • 不能使用关键字作为变量名,可以导入keyword模块后使用print(keyword.kwlist)查看所有Python关键字
  • 不建议使用系统内置的模块名,类型名或函数名以及已导入的模块名及其成员名作变量名,这将会改变其类型和含义。
  • 变量名对英文字母的大小写敏感,例如student和Student是不同的变量。

13.Python内置支持复数类型

>>> a=3+4j
>>> b=5+6j
>>> c=a+b
>>> c
(8+10j)
>>> c.real
8.0
>>> c.imag
10.0
>>> c.conjugate()
(8-10j)
>>> a*b
(-9+38j)
>>> a/b
(0.6393442622950819+0.03278688524590165j)

14.字符串合并

>>> a='abc'+'123'
>>> print(a)
abc123

字符串格式化

>>> a=3.548678
>>> '%6.2f'%a
'  3.55'
>>> "%d:%c"%(65,65)
'65:A'

转义字符:

>>> "My name is %s,and my age is %d"%('He zhi bin',22)
'My name is He zhi bin,and my age is 22'
>>> print('我是\u67d0\u67d0\u67d0\u000d\u000a')
我是某某某

可以用Unicode编码转换在线转换需要的内容。
也可以:

>>> ord('何')
20309
>>> hex(20309)
'0x4f55'
>>> print('\u4f55')
何

调用不同函数,输出有可能不同

>>> x='''Tom said,"let's go"'''
>>> x
'Tom said,"let\'s go"'
>>> print(x)
Tom said,"let's go"

15.Python中运算符和表达式的规则:

>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> (1,2,3)+(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "int") to tuple
>>> (1,2,3)+(4,)
(1, 2, 3, 4)
>>> 'abc'+'1234'
'abc1234'
>>> 'A'+1+
  File "<stdin>", line 1
    'A'+1+
         ^
SyntaxError: invalid syntax
>>> true+1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> True+1
2
>>> False+1
1

*运算符的作用:相乘和重复

>>> 2.0*3
6.0
>>> (3+4j)*2
(6+8j)
>>> (3+4j)*(3-4j)
(25+0j)
>>> "a"*10
'aaaaaaaaaa'
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)

Python中的除法:‘/’和‘//’

>>> 3/5
0.6
>>> 3//5
0
>>> 3.0/5
0.6
>>> 3.0//5
0.0
>>> 13//10
1
>>> 13/10
1.3
>>> -13//10
-2

range()函数是用来生成指定范围数字的内置函数。eg:range(1,10,1)意思是在[1,10)中以步长为1进行查询,有True,无False。

>>> 10 in range(1,10,1)
False

in还可以成员遍历

IndentationError: expected an indented block
>>> for i in (3,5,7):
... print(i,end='\t')
  File "<stdin>", line 2
    print(i,end='\t')
        ^
IndentationError: expected an indented block
>>> for i in (3,5,7):
...     print(i,end'\t')
  File "<stdin>", line 2
    print(i,end'\t')
                  ^
SyntaxError: invalid syntax
>>> for i in (3,5,7):
...       print(i,end='\t')
...
3       5       7       >>>

注意:学Python中遇到的第一个错误
IndentationError: expected an indented block

把这段英文报错翻译过来就是: 缩进错误: 期望一个缩进的块。
首先我们要了解python的语法结构和特点,在python中,所有的逻辑代码块也就是一个方法中的代码,都必须使用相同的缩进来标识区分是同一个方法,否则编译会报错。
16.is的用法

>>> x=[300,300,300]
>>> x[0] is x[1]
True

桥豆fucking麻袋

>>> x=[1,2,3]
>>> y=[1,2,3]
>>> x is y
False

为什么呢,因为基于值的内存管理,同一个值在内存中只有一份,但是x,y不是同一个列表对象。
17.集合的交集,并集,对称差集,差集等等…
对称差集:

>>> {1,2,3}^{3,4,5}
{1, 2, 4, 5}

差集:

>>> {1,2,3}-{3,4,5}
{1, 2}

18.逻辑运算符and和or具有惰性求值的特点。

>>> 3>5
False
>>> 3>5 and a>3
False
>>> 3<5 and a>3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 3<5 or a>3
True
>>> 3 and 5
5
>>> 3 and 5>2
True
>>> 3 not in [1,2,3]
False
>>> 3 is not 5
True

19.显示内容:

>>> c=[1,2,3,4,5,6]
>>> d=list(map(str,c))
>>> d
['1', '2', '3', '4', '5', '6']

求sin函数:

>>> import math
>>> list(map(math.sin,c))
[0.8414709848078965, 0.9092974268256817, 0.1411200080598672, -0.7568024953079282, -0.9589242746631385, -0.27941549819892586]

rstrip函数的作用,删除字符串右端的字符:

>>> ('welcome,'*3).rstrip(',')+'!'
'welcome,welcome,welcome!'
>>> ('welcome, '*3).rstrip(',')+'!'
'welcome, welcome, welcome, !'