python基础变量之—列表
python常用API了解
1.大小写转化
text="hello,world,Zhl,python"
text2=text.lower()#转化为小写
print(text2)
text3=text.upper()#转化为大写
print(text3)
text4=text.capitalize()#首字母大写
print(text4)
hello,world,zhl,python
HELLO,WORLD,ZHL,PYTHON
Hello,world,zhl,python
2.去除首尾空格
text=" hello world Zhl python "
text6=text.strip()#去除首尾空格
print(text6)
hello world Zhl python
3.空格分隔字符串
text="hello world Zhl python"
text7=text.split()#空格分隔字符串
print(text7)
hello world Zhl python
4.逗号分隔字符串
text="hello world Zhl python"
text8=text.split(",")#逗号分隔字符串
print(text8)
hello,world,Zhl,python
5.将列表中的字符串连接
word=['hello','world','Zhl','python']
text9=("*".join(word))#将列表中的字符串连接
print(text9)
hello*world*Zhl*python
6.替换字符串
text="hello,world,Zhl,python"
print(text.replace("Zhl","Wh"))#替换字符串
hello,world,Wh,python
7.查找字符串位置
text="hello,world,Zhl,python"
print(text.find("Zhl"))#查找字符串位置
12
8.判断字符串是否以指定字符串开头
text="hello,world,Zhl,python"
print(text.startswith("hello"))#判断字符串是否以指定字符串开头
True
9.判断字符串是否以指定字符串结尾
text="hello,world,Zhl,python"
print(text.endswith("python"))#判断字符串是否以指定字符串结尾
True
10.判断字符串是否为纯字母
text="hello,world,Zhl,python"
print(text.isalpha())
text="123aa"
print(text.isalpha())
text="aaaa"
print(text.isalpha())
False
False
True
11.首字母大写
text="hello,world,Zhl,python"
print(text.title())#首字母大写
Hello,World,Zhl,Python
12.统计字符串出现的次数
text="hello,world,Zhl,python,Zhl"
print(text.count("Zhl"))#统计字符串出现的次数
2
13.格式化字符串
name="zhl"
age=21
print("我的名字是{},今年{}岁".format(name,age))
我的名字是zhl,今年21岁
python数字类型了解
1.1整数
整数(int)
#十进制
10
#二进制 (二进制的写法 0b 开头 后跟 0~1)
0b110
#八进制 (八进制的写法 0o开头 后跟 0~7)
0o10
#十六进制 (十六进制的写法 0x 开头 后跟 0~9, a-f)
0xA1
1.2浮点数(float)
#小数 1.235
#科学计数 5e-5
1.3布尔类型(boo)
Ture
False
注:0、0.0、-0.0、空字符串、空列表、空字典、空集合、空元组、None等都是False;
1.4
复数(complex):
x=5+7j
1.5数字类型转换
- bin():转换为二进制
- oct():转换为八进制
- int():转换为十进制整数
- hex():转换为十六进制
- float():转换为浮点数
- complex(x):将x转换到一个复数,实数部分为 x,虚数部分为 0。
- complex(x, y):将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。
- bool(x):将 x 转化为布尔值型转换
实例
如何把10进制的数转化为16进制数,举例
print(hex(10))
0xa
#表示取完0-9之后取a,所以为0xa
print(hex(255))
0xff
#给255除以16,得到15余15,余数表示16进制f,结果再次取余,商0余15,故16进制为0xff
如何把16进制的数转化为8进制数,举例
#hex oct
print(oct(int(0xff)))
0o377
#先用int转为十进制,再用oct转为八进制,转为八进制还是用取余,十进制除以8取余,倒序依次写
一、列表
1.列表介绍
列表是一种动态数组,可以存储不同类型的数据(如整数、字符串、对象等),并且支持动态扩展和收缩。
例如
my_list = [1, 2, "hello", 3.14]
2.列表运算
加法
x1=[1,2,3,4]
x2=[5,6,7,8]
print(x1+x2)
[1, 2, 3, 4, 5, 6, 7, 8]
乘法
x1=[1,2,3,4]
print(x1*3)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
追加
x1=[1,2,3,4]
x1+=[6,8]
print(x1)
[1, 2, 3, 4, 6, 8]
比较
x1=[1,2,3,4]
x2=[5,6,7,8]
x1==x2
x1!=x2
False
True
判断
x1=[1,2,3,4]
x3=[2,3,4]
x3 in x1
x1=[1,[2,3,4]]
x3=[2,3,4]
x3 in x1
False
True
3.列表访问
和字符串切片相差不大,以下示例
x1 = [1, 2, 3, 4, 5, 6]
print(x[0]) # 1
print(x[-1]) # 6
4.列表切片
和字符串切片相差不大,以下示例
列表[(开始索引b):(终止索引e)(: (步长s))]
#列表切片
x=[1,2,3,4,5,6,7,8,9]
print(x[0:3])#从0开始到3结束
print(x[3:])#从3开始到结束
print(x[:3])#从0开始到3结束
print(x[::2])#从0开始到结束,步长为2
print(x[::-1])#倒序
print(x[1:8:2])#从1开始到8结束,步长为2
print(x[::])#从0开始到结束
5.列表操作
增
x=[10,20,30]
re=x.append(40)#追加元素
print(x)
x.insert(1,50)#在指定位置插入元素
print(x)
x=[10,20,30]
x.extend([60,70])#在指定位置插入列表,相当于x+
print(x)
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 20, 30, 60, 70]
删
x=[10,20,30]
x.remove(20)#删除指定元素
print(x)
x=[10,20,30]
x.pop(1)#删除指定索引位置的元素
print(x)
x.clear()#清空列表
print(x)
x=[10,20,30]
del x[1]
print(x)
[10, 30]
[10, 30]
[]
[10, 30]
改
# 修改列表的
# 修改列表的
x = [1, 2, 3, 4]
x[1] = 5
print(x)
# x[4] = 6 下标不能越界
# print(x)
# print(x[4])
print(x[2:100]) # 切片可以越界
# x[1:3] = 666 # 切片赋值必须赋可迭代对象
# print(x)
x[1:3] = [666, 777]#切片赋值必须赋可迭代对象
[1, 5, 3, 4]
[3, 4]
6.列表常用API
#列表常用的API
x=[1,2,3,4,5,6,7,8,9]
print(len(x))
x=[1,2,3,4,5,6,7,8,9]
print(max(x))
x=[1,2,3,4,5,6,7,8,9]
print(min(x))
x=[1,2,3,4,5,6,7,8,9]
print(x.index(3))
9
9
1
2