【方便自己,方便他人~还在补充中……】
Python语言的基本语法
- 缩进
- 注释
- 常量与变量
- 命名
- 表达式、 输入、输出
- python 操作符-两个除号//、两个乘号**
- 字符串
- 字符串索引说明
- 字符串分割str.split()
- 字符串格式化输出--带单位的信息输出
- Python字符串判断元素组成
- Python检查字符串中是否包含指定中文--Python find()方法
- Python 字符串 strip()方法--移除字符串首尾指定的字符
- 分支语句if else
- 循环语句while
- 函数
- lambda函数
- Python中常用的数值类型
- 异常处理机制
- 列表(list)
- list常用操作汇总
- list.append()在列表末尾添加新的对象
- list.extend()在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
- list.insert()用于将指定对象插入列表的指定位置。
- list.reverse()方法
- list.index()方法
- list.pop()方法
- list.remove()方法
- 关于del:
- 关于sorted、sort()和.reverse()
- filter 函数
- 列表输出样式的变换--把单个字符串用另一个样式连接在一起
- for 循环
- python range() 函数——创建一个整数列表
- 语法详解
- 利用range()函数特点来进行逆序
- 元组
- 列表与元组
- 集合
- 字典
- 字典的遍历
- 字典dict.items()方法
- 循环的终止-break&continue
- 文件的操作
- 文件写操作
- 文件读操作
- 使用with语句操作文件对象
- python 续行符(换行符)
- random() 函数
- numpy.concatenate--连接数组
- python 中 reversed()函数
缩进
注释
多行注释是三双引号开头哦。
常量与变量
一般常量大写,表示不轻易改变。
命名
不能以数字开头!!
表达式、 输入、输出
python 操作符-两个除号//、两个乘号**
1、两个除号是向下取整。
2、两个乘号就是乘方。比如3**4,结果就是3的4次方,结果是81
3、如果是字符串、列表、元组与一个整数N相乘,返回一个其所有元素重复N次的同类型对象,比如"str"*3将返回字符串"strstrstr"
4、如果是函数定义中参数前的 *表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中
例如:
args=(1,2,3)
func=(*args)
等价于函数调用func(1,2,3)
字符串
字符串索引说明
- 正向索引:从0开始标!
反向索引:从-1开始标! - 区间索引
[A:B]
,注意输出不包含位置B!!! - 正向索引区间从0开始,可以省略0。比如:
s[0:3]
等价于s[:3]
- 反向索引:以-1结尾,省略-1表示可以输出到最后一位。
注意这里s[-4:-1]
不等价于s[-4:]
!! - 关于步长,[起始位置:终止位置(不包含):步长]
例1:s[0:3]
默认步长1
例2:s[::2]
步长为2,正向输出
例3:s[::-2]
步长为2,反向输出
例4:s[:2:2]
,终止位置为2即为字母‘t’(但不包含),步长为2,正向输出,所以输出只有‘P’
字符串分割str.split()
https://docs.python.org/3/library/stdtypes.html#str.split
str.split()函数
()里放的是要以什么进行分割
输出的是一个列表list
列表元素的取出与数组一样
字符串格式化输出–带单位的信息输出
用{}的格式指定位置信息:
输出多个,format()里用逗号隔开
例如1:
例如2:
注意:标顺序的标号,不能只写几个,要写就要全写!!!!!
我一开始只写了2和1,就报错了
Python字符串判断元素组成
更多isxxx()方法请参考:
https://docs.python.org/3/library/stdtypes.html#string-methods注意:
直接判断是判断字符串是否只由字母/数字组成:
想要判断字符串是否包含字母/数字,要写循环一个一个字符判断
(print处修改就可以判断了):
Python检查字符串中是否包含指定中文–Python find()方法
描述 find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。
语法 str.find(str, beg=0, end=len(string))
参数
str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。
返回值 如果包含子字符串返回开始的索引值,否则返回-1。
Python 字符串 strip()方法–移除字符串首尾指定的字符
描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
语法 strip()方法语法:str.strip([chars]);
参数 chars – 移除字符串头尾指定的字符序列。
返回值 返回移除字符串头尾指定的字符生成的新字符串。
分支语句if else
先写框架:用pass占位
循环语句while
循环要有条件,不然就是死循环了。
while 后条件没有括号!!!加冒号!!!
函数
lambda函数
add = lambda x, y: x+y
相当于定义了加法函数lambda x, y: x+y
,并将其赋值给变量add,这样变量add就指向了具有加法功能的函数。
这时我们如果执行add(1, 2)
,其输出结果就为 3
Python中常用的数值类型
浮点数*整数=浮点数
整数->字符串:str(3)->‘3’
浮点数->字符串:str(3.14)->‘3,14’
注意:float(1)的结果为 1.0 !!!!!!
关于type()函数:
Python支持的数据类型有:int、integer、float
Python不支持char
Python没有char或byte类型来保存单一字符或8位整型。但是可以使用长度为1的字符串表示字符或8位整型。
异常处理机制
列表(list)
list常用操作汇总
x in list l 检查元素是否在列表中,返回值为True/False
list.append()在列表末尾添加新的对象
语法:list.append(obj)
-
obj
– 添加到列表末尾的对象。 - 该方法无返回值,但是会修改原来的列表。
a = [1, 2, 3, 4, 5]
a.append(6)
print(a)
#输出
#[1, 2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
a.append(b[0:2])
print(a)
#输出
#[1, 2, 3, 4, 5, [6, 7]]
list.extend()在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
语法:list.extend(seq)
-
seq
– 元素列表。 - 该方法没有返回值,但会在已存在的列表中添加新的列表内容。
使用list.extend()
:
a = [1, 2]
b = [3, 4]
a.extend(b)
print(a)
#输出
#[1, 2, 3, 4]
对比用list.append()
:
a = [1, 2]
b = [3, 4]
a.append(b)
print(a)
#输出
#[1, 2, [3, 4]]
list.insert()用于将指定对象插入列表的指定位置。
语法:list.insert(index, obj)
- 它接受两个参数,第一个参数是索引号,第二个参数是待添加的对象
-
index
– 对象 obj 需要插入的索引位置。 -
obj
– 要插入列表中的对象。 - 该方法没有返回值,但会在列表指定位置插入对象。
a = [1, 2, 3, 4, 5]
a.insert(2, 6)
print(a)
#输出
#[1, 2, 6, 3, 4, 5]
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
a.insert(2, b[0:2])
print(a)
#输出
#[1, 2, [6, 7], 3, 4, 5]
list.reverse()方法
reverse() 函数用于反向列表中元素。会对列表的元素进行反向排序
list.index()方法
index() 函数用于从列表中找出某个值第一个匹配项的索引位置。
语法:list.index(x, start, end)
- x-- 查找的对象。
- start-- 可选,查找的起始位置。
- end-- 可选,查找的结束位置。
list.pop()方法
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
- pop()方法语法:
list.pop([index=-1])
- 要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
- 该方法返回从列表中移除的元素对象。
新建列表:l = [] 空就是新建列表
列表区间访问同字符串一样,区间最右侧不包含
列表用方括号[],方括号里用逗号隔开
列表中每个元素类型可以不同,例如:
list.remove()方法
list.remove(x)用于删除列表中第一次出现的元素x
- .remove()方法语法:
list.remove(obj)
- 无返回值
关于del:
del函数为永久性删除,直接改变原列表。
x = [1,2,3,4]
del x[1]
print(x)
结果为:[1,3,4]
关于sorted、sort()和.reverse()
sorted排序后不改变原序列。
sort()和.reverse()函数都是永久性改变,直接更改原列表中元素。reverse()函数将原列表元素倒置。
sort(reverse=True)先对列表中的元素进行排序后倒置。
filter 函数
list_original = [1,2,3,4,5,6,7]
list_filter = filter(lambda x:x>5,list_original)
print(list_filter)
结果为:[6,7]
filter 函数的功能相当于过滤器。
调用一个布尔函数bool_func来迭代遍历每个seq中的元素;
返回一个使bool_seq返回值为true的元素的序列。
列表输出样式的变换–把单个字符串用另一个样式连接在一起
for 循环
while 循环需要计数器;for 循环不需要
python range() 函数——创建一个整数列表
语法详解
python range() 函数可创建一个整数列表,一般用在 for 循环中。
例如可以让for循环实现从0到10计数,或者从10到100每次递增5等等。
- range函数语法
range(start, stop[, step])
- range会返回一个整数序列
start
: 计数从 start 开始。默认是从 0 开始。例如range(5)
等价于range(0,5)
;stop
: 计数到 stop 结束,但不包括 stop。例如:range(0,5)
是[0, 1, 2, 3, 4]
没有5
step
:步长,默认为1。例如:range(0,5)
等价于range(0,5,1)
利用range()函数特点来进行逆序
关于range():
关于list(range(n)):
range()指定从哪个数字开始:
元组
区别于列表,元组用圆括号和逗号隔开。
元组一旦被创建就不能修改。
列表与元组
列表是可以被修改的!!!
集合
python数据类型中无法索引访问的是集合
(元组、字符串、字典可以索引访问)
无序!无重复!
字典
创建字典:
增加一项:
修改某一项:
删除某一项:
是否在字典中:
字典的遍历
字典d.get用法:
注意:创建字典时,列表不能作为key值
字典dict.items()方法
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组的数组。
语法:dict.items()
d=dict()
d['egg']=2.59
d['milk']=3.19
print(d) # {'egg': 2.59, 'milk': 3.19}
print(d.items()) # dict_items([('egg', 2.59), ('milk', 3.19)])
#遍历所有的数据项
for item in d.items():
print(item)
# ('egg', 2.59)
# ('milk', 3.19)
for k,v in d.items():
print('key:',k)
print('value:',v)
# key: egg
# value: 2.59
# key: milk
# value: 3.19
for k in d:
print(k)
# egg
# milk
循环的终止-break&continue
文件的操作
文件写操作
文件操作注意点:
文件名要加引号!文件操作模式也要加引号!
open函数打开文件之后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的。
文件读操作
read():
readline(): 一行一行读(不缺点不知道多少行就没办法遍历完)
readlines(): 返回一个列表
想要读取则:
或者
输出结果为:
使用with语句操作文件对象
推荐!!!
python 续行符(换行符)
在python中,Python 用反斜线 (“\”) 作为续行符(换行符)
random() 函数
random() 方法返回随机生成的一个实数,它在[0,1)范围内。
语法:
import random
random.random()
一般都会有numpy库的哇
那就
import numpy
numpy.random.random()
注意:random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
numpy.concatenate–连接数组
numpy.concatenate 函数用于沿指定轴连接相同形状的两个或多个数组,格式如下:
numpy.concatenate((a1, a2, ...), axis)
参数说明:
a1, a2, …:相同类型的数组
axis:沿着它连接数组的轴,默认为 0
#axis=1表示对应行的数组进行拼接
python 中 reversed()函数
height=[1,8,6,2,5,4,8,3,7]
是一个列表height_reverse=reversed(height)
是一个对象
print(height_reverse)
输出:<list_reverseiterator object at 0x00000132E63D34E0>
显示为一个迭代器对象的内存地址
转为列表:
height_reverse=list(reversed(height))
print(height_reverse)