python
解释器:高级语言
语言:
编译型:预编译 → 编译 → 汇编 → 链接
安装
常见的组合:
python + vsc
python + sublime
anaconda + pycharm
安装时记得勾选环境变量
pip 安装 python库
基础
print("Hello, World!")
print(*value, sep="间隔", end="结束符")
print(*values, sep=' ', end='\n', file=sys.stdout, flush=False)
注释
# 单行注释
'''
多行注释 """
'''
保留字
import keyword
print(keyword.kwlist)
赋值
javaScript:
let name = "myqf";
C:
int age = 18
变量
name = 'myqf'
变量名 = '变量值'
age = 18
length = 18.8
a = b = c = 1
a, b, c = 1, 2, 3
a, b = b, a
print(a, b, c)
标准数据类型
python3中 六个 标准数据类型:
- Number(数字)
- String(字符串)
- List(列表)
- Tuple(元组)
- Set(集合)
- Dictionary(字典)
数字(number)
python3中 支持,int、float、bool、complex(复数)
myqf= 123
# 查看变量类型
type(myqf)
# 删除变量
del myqf
# print(myqf)
# 前置类型转换
int → float + float
数字运算符
加减乘除
//:除法,得到整数
%:取余
**:乘方
函数
int | math.floor(11.9)
转换成小的整形
参考:https://blog.csdn.net/qq_40074819/article/details/105179478
字符串(string)
word = '字符串'
sentence = "这是应该句子"
paragraph = r"""这是一个段落,
可以由\n多行组成
"""
print(paragraph)
在字符前加一个r,防转义
切片(slice、对list、string、tuple,的高级索引方法)
变量[头下标:尾下标:步长]
text = '123456789'
print(text) # 输出字符串
print(text[0]) # 输出字符串的第一个字符
print(text[0:-1]) # 输出第一个到倒数第二个的所有字符
print(text[2:5]) # 输出从第三个开始到第五个字符
print(text[2:]) # 输出从第三个开始后的所有字符
print(text[1:5:2]) # 输出从第二个开始到第五个,且步长为2
print(text * 2) # 输出字符串两次
print(text + '你好') # 链接字符串
msg = 'aa'
msg2 = 'bb'
print(msg + msg2)
函数
.format():格式化字符串
text = '123{}45{}6789'
print(text.format('xxx', 'aaa'))
.center(数量,字符):格式化输出,居中显示
text = '123{}45{}6789'
print(text.format('xxx', 'aaa').center(50, '*'))
.find(str, [beg=, end=]):查找字符串的下标
text = '123456789'
print(text.find('1'))
len():长度,列表同样适用
print(len('123456'))
.replace(old str,new str):字符串替换
text = 'abcd'
print(text.replace('b', '123'))
.format():格式化字符串
print(f “{}”)
a = 'myqf'
print(f'{a}123')
.center(数量,字符):格式化输出,居中显示
.find(str, [beg=, end=]):查找字符串的下标
len()
replace()
列表(list)
List写在方括号之间,元素用逗号隔开,支持相加
a = [1, 2, 3]
a[2] = 'x'
a[1:3] = 'a', 'b'
a[2] = []
print(a)
函数
max()
a = [1, 2, 3]
b = ['a', 'b', 'c', 'A']
min()
.append():末尾添加新对象
a = [1, 2, 3]
a.append(['c', 456])
.count():统计出现次数
a = [1, 2, 3, 1, 1]
print(a.count(2))
.index():找索引
a = [1, 2, 3, 1, 1, 'aa']
print(a.index('aa'))
.pop([index=-1]):移除元素,并且返回该元素
默认为-1 删除最后一个,并返回值
a = [1, 2, 3, 1, 1, 'aa']
deltxt = a.pop(2)
.remove():指定删除
a = [1, 2, 3, 1, 1, 'aa']
a.remove(1)
.append():末尾添加新对象
.count():统计出现次数
.index():找索引
.pop([index=-1]):移除元素,并且返回该元素![在这里插入图片描述](https://img-blog.csdnimg.cn/e4633046059a489c8e84aa780f09dccd.png)
.remove():指定删除
无返回值