1.保留字
保留字即关键字,我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字
2.注释
多行注释可以用多个 # 号,还有 ‘’’ 和 “”"
3.缩进与行
【缩紧】python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} 。缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。
【行与换行】Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠()来实现多行语句。在 [], {}, 或 () 中的多行语句,不需要使用反斜杠()
4.数字类型 Number
数字的四种类型:整型 布尔 浮点数 复数
· int 证书类型. 1
·bool 布尔类型 True. True False
·float 浮点数. 1.23 3E-2
·complex 复数. 1+2j、 1.1+2.2j
4.字符串 String
·python中单引号和双引号一样 word = ‘字符串’ word = “字符串”
·三引号(‘’’ 或者"“”) 指定一个多行字符串。注意开闭统一
·转译符号 ‘’
·反斜杠用来转移 r防止反斜杠转移 如:r’zhao Phor \n’ 的\n会显示
·按字面意义级联字符串,如"this " "is " "string"会被自动转换为this is string
·字符串拼接 + *运算符重复. 如:print(str * 2) # 输出字符串两次
·两种索引方式:0 左起➡️右 -1右起 ⬅️
·Python中的字符串不能改变。 解释如下 4.a
·Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。
·字符串的截取的语法格式如下:变量[头下标:尾下标:步长]
4.a Python中的字符串不能改变
字符串测试:
5.空行
函数之间或类的方法之间用空行分隔,表示一段新的代码的开始。
类和函数入口之间也用一行空行分隔,以突出函数入口的开始。
空行与代码缩进不同,空行并不是Python语法的一部分。
书写时不插入空行,Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码,便于日后代码的维护或重构。
**记住:**空行也是程序代码的一部分。
6.同一行显示多条语句
Python可以在同一行中使用多条语句,语句之间使用分号(;)分割
7.多个语句构成代码组
缩进相同的一组语句构成一个代码块,我们称之代码组。像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组。
我们将首行及后面的代码组称为一个子句(clause)。
8.Print 输出
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end=“”:
9.import 与 from...import
在 python 用 import 或者 from…import 来导入相应的模块。
将整个模块(somemodule)导入,格式为: import somemodule
从某个模块中导入某个函数,格式为: from somemodule import somefunction
从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc
将某个模块中的全部函数导入,格式为: from somemodule import *
10.命令行参数
很多程序可以执行一些操作来查看一些基本信息,Python可以使用-h参数查看各参数帮助信息: