‘’’
python 3
1. Basic part:
1. Identifier-variable name
1.1 python保留字--变量名中不能用的名
2. Annotation
2.1 多行注释 '''''';""""""
2.2 单行注释 #
3. Lines and indentation
3.1 缩进代表了python执行的层级关系。
3.2 多行语句 +\
total = item_one + \
item_two + \
item_three
在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(\)。
4. Escape
4.1 \+ 特殊字符,如 \n
4.2 在字符串前面+r,表示原字符,不会转译。
5. Scattered Grammar
5.1 print
5.2 input (接收到的格式为字符串格式)
input("请输入*****")
5.3 end=""
实现在print中不换行,则在末尾加上end=""
5.4 导入模块
import
from...import
5.4.1 导入somemodule
import somemodule
5.4.2 导入模块中的某个函数
from somemodule import somefunciton
5.4.3 导入模块中的多个函数
from somemodule import firstfun1,secondfun2...
5.4.4 导入模块中的全部函数
from somemodule import *
5.5 in
a in list 判断a是不是在list中
2. Basic grammar:
1. Basic data types
1)python中不用声明变量,但是使用前都必须赋值。赋值后的变量并没有数据类型,我们所说的类型只是指内存中对象的类型。
2)用 = 号赋值。
单个
name = 'Aing'
多个
a= b= c = 1
a,b,c = 'Aing','HERB',5
3)查看数据类型
type(a) ;不会认为子类是一种父类类型
isinstance(a,int) 返回bool ; 会认为子类是一种父类类型
4)del 删除对变量的引用,并不删除数据。
1.1 Standard data types (basic)
1.1.1 Number
1)int bool float complex--整数,布尔,浮点数,复数。
2)+ - * / // 取整 % 取余 ** 乘方
3)混合计算时,会直接把整形转换为浮点数。
【进阶】
随机函数;三角函数;数学常量。
https://www.runoob.com/python3/python3-number.html
1.1.2 String
1)单双引号完全相同。
2)+ 号字符串的拼接;* 号字符串的重复。
3)正向索引是从0开始的;反向是从-1开始。
可通过索引获取,如str1[0]
4)字符串不能改变。
如word[0]="m",虽然在其他语法里称为,对word字符串的[0]位置进行修改,但是
在python中不行,因为python中是对变量的引用。
5)字符串截取:变量[起始:终止:步长]
6)可以理解为特殊的元祖。
【进阶】字符串格式化和内建函数
a)%s,%d 字符串/数字
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
b)f方式
name = 'Runoob'
f'Hello {name}' # 替换变量
输出为 'Hello Runoob'
f'{1+2}' # 使用表达式
输出为 '3'
w = {'name': 'Runoob', 'url': 'www.runoob.com'}
f'{w["name"]}: {w["url"]}'
输出为 'Runoob: www.runoob.com'
c)格式化函数
format
内建函数
https://www.runoob.com/python3/python3-string.html
1.1.3 List (Ordered)
1)list=[]
2)类似于字符串,可理解为字符串的嵌套。
3) + * 同样适用,索引也相同。
列表的更新也同字符串 list[2]="aing"
4)列表中的元素可以改变。
5)一些内置方法
append()、pop()
6)复数,逆向读取 list(1::-1) 反向从最后一个向前读取。
【进阶】
删除列表元素 del list[2]
列表的长度 len(list)
内置函数
https://www.runoob.com/python3/python3-list.html
1.1.4 Tuple
1)tuple=()
2)类似于list,但是内容不能更改。
3)同 + * 操作以及切片&索引。
4)字符串其实是特殊的元祖。
5)一个元素也需要在后面加逗号tup1 = (20,)
【进阶】
删除元组 del tuple
元组的长度 len(tuple)
内置函数
https://www.runoob.com/python3/python3-tuple.html
1.1.5 Dictionary (unordered)
1)dict={}
2)字典中的元素,是通过键值来创建和提取的。
2.1)dict1={}
dict1["name"]="AING"
dict1["age"]=24
2.2)dict2={"name":"HERB","age":25}
3)只输出键,只输出值
dict1.keys() dict1.values()
4)访问数据
print('dict["name"]:'dict['name'])
5)增加数据
dict1["name2"]="Pilriy"
dict1["age"]=22
6)清空字典
dict1.clear()
7)删除字典
del dict1
ps:同一个键不能出现两次;键必须不可变。
8)内置函数
https://www.runoob.com/python3/python3-dictionary.html
1.1.6 Set
(第一次)不是很能理解他存在的意义。
https://www.runoob.com/python3/python3-set.html
最后,数据类型的转换。
1.2 Operator
1.2.1 算术运算符
+ - * / , // 取整 % 取余 ** 乘方
1.2.2 比较运算符
== != > < >= <=
1.2.3 逻辑运算符
and or not 与 或 非
1.2.4 成员运算符
in not in
1.2.5 身份运算符
is is not
ps: is 判断的是两个变量引用的对象是否是同一个
== 判断两个变量引用的对象的值是否相同
1.2.6 赋值运算符
只举一例 += c+=a ===> c=c+a
1.2.7 位运算符
暂时没懂(第一次)
最后,运算符的优先级。
Finished from the rookie tutorial
https://www.runoob.com/python3/python3-tutorial.html
‘’’
String example
str1 = 'AingPilriy'
print(str1)
print(str1[0:-1])
print(str1[0])
print(str1[2:5])
print(str1[1:8:2])
print(str1[0:3]+"你好"+str1[4:-1])
print(str1*2)
# 字典相关。
dict1={
}
dict1["name"]="AING"
dict1["age"]=24
print(dict1)
dict2={
"name":"HERB","age":25}
print(dict2)
print(dict1.keys())
print(dict1.values())
20210320 18:59