python变量不用声明类型
python变量有:整型、字符串、布尔、浮点、列表、元祖、字典、集合
直接赋值,整型常用的就是转换string类型函数str()
num = 2
print(num)
print(type(num))
str = str(num)
print(str)
print(type(str))
str = ' this is a string '
print(str)
print(type(str))
字符串常用函数:
strip()
去掉字符串最前和最后的空格;
str = ' this is a string '
new_str = str.strip()
print(new_str)
splite()
字符串分割成列表,括号内默认为以空格为分割,以其他字符分割,可在括号内加上,如:splite('-')
str = ' this is a string '
new_str = str.split()
print(new_str)
str1 = 'that-is-a-string'
new_str1 = str1.split('-')
print(new_str1)
str = 'this is a string'
up_str = str.upper()
print(up_str)
lw_str = up_str.lower()
print(lw_str)
str = 'this is a string'
new_str = str.capitalize()
print(new_str)
str = 'this is a string'
new_str = str.title()
print(new_str)
str = 'this is a string'
new_str = str.replace('this','That')
print(new_str)
布尔类型相对比较简单,只有两种值TRUE
和FALSE
num=3.1415926
print(type(num))
lis = [1,2,3,4,'libai',[2,3]]
print(lis)
print(type(lis))
lis = [1,2,3,4,'libai',[2,3]]
new_lis = lis.copy()
print(new_lis)
lis = [1,2,3,4,'libai',[2,3]]
lis.append('abc')
print(lis)
lis = [1,2,3,4,'libai',[2,3]]
lis.pop(1)
print(lis)
lis.pop()
print(lis)
lis = [1,2,3,4,'libai',[2,3]]
lis.remove(2)
print(lis)
sort()
和sort(reverse=True)
分别是按正序和倒序给列表排序
lis = [3,1,4,1,5,9]
lis.sort()
print(lis)
reverse()
是反向输出,按照列表的顺序原封不动的反过来输出
lis = [3,1,4,1,5,9]
lis.reverse()
print(lis)
lis = [3,1,4,1,5,9]
lis.clear()
print(lis)
lis = [3,1,4,1,5,9]
lis.insert(1,'m')
print(lis)
lis = [3,1,4,1,5,9]
lis[2]='m'
print(lis)
lis = [3,1,4,1,5,9]
print(lis[1:3])
tup1 = (1,2,3)
print(type(tup1))
tup1 = (1,2,3)
print(len(tup1))
tup1 = (3,1,4,1,5,9,2,6,5,3,5,7)
print(tup1.count(3))
tup1 = (3,1,4,1,5,9,2,6,5,3,5,7)
print(tup1.index(3,5,10))
tup1 = (3,1,4,1,5,9,2,6)
tup2 = (5,3,5,7)
print(tup1+tup2)
整个字典被大括号包括,里面以键值的形式展示
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
print(type(dic))
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
dic['name'] = "lisi"
print(dic)
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
print(dic['name'])
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
dic['weight'] = '50'
print(dic)
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
dic.pop('name')
print(dic)
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
new_dic = dic.copy()
print(new_dic)
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
print(dic.keys())
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
print(dic.items())
dic = {"name":"zhangsan","age":20,"id":"41150388974"}
print(dic.values())
se = {1,3,4,2}
print(type(se))
se = {1,1,3,3,6,8}
print(se)
se = {1,1,3,5,7,9}
sr = {2,3,4,6,7,9}
print(se&sr)
se = {1,1,3,5,7,9}
sr = {2,3,4,6,7,9}
print(se|sr)
支持索引取值
支持重复
支持切片
支持相加
支持增删改
支持索引取值
支持重复
支持切片
支持相加
不支持增删改
支持键值对索引取值
key不能重复,重复会被重写
不支持相加
不支持切片
支持增删改
不支持索引取值
无序性和唯一性
不支持相加
不支持切片