数据类型
- 严格意义上讲,python只有一个类型
- 标准数据类型六种
- 数字 Number
- 字符串类型 str
- 列表 list
- 元组 tuple
- 字典 dict
- 集合 set
常见数字类型
整数
- 没有小数部分
- 包含正数,负数和0
- 二进制
- 只有0,1
- 以0b开头的01串
- 例如:
- 0b111
- 0b1110
- 八进制
- 以0o开头的,0到7之间的数字串
- 例如:
- 0o76
- 十六进制
- 以0x开头的,0到9,a到f之间的数字串
- 例如
- 0xa
#二进制定义
a = 0b111
print(a)
#八进制的定义
b = 0o76
print(b)
#十六进制的定义
c = 0xa
print(c)
7
62
10
浮点数
- 就是通俗意义上的小数
- 常见的案例格式
- 3.14159
-
- 代表是3.0这个数
- 0.4
- .4
- 代表是0.4这个数
#浮点数的定义
a = 3.
print(a)
b= .4
print(b)
3.0
0.4
科学计数法
- 定义跟数学定义一致
- 写法就是e后面跟整数用来表示10的整数
#科学技术
height = 172
print(height)
height = 1.72e2
print(height)
172
172.0
复数
- 与数学定义一致
- 例如:Z=A+BJ(A,B均为实数)
- A称为实部,B称为虚部,J称为虚数单位。
- 当Z的虚部为0时,称Z为实数
- 当Z的虚部不为0时,实部为0时,Z称为纯虚数
- 例如:Z=A+BJ(A,B均为实数)
- 复数的虚部用j/J表示
#复数
a = (4j)
print(a)
4j
布尔值
- 就是用来表示真假的值
- 只有两个值 True 和 False
- 在python中,布尔值可以当作数字使用
- True=1.False=0
#布尔值当数字使用
age = 18 + True
print(age)
age = 18 + False
print(age)
#if语句
a = 55
if a>0:
print("a是True")
else :
print("a是False")
19
18
a是True
字符串类型
- 表达文字信息的内容
- 形式上是引号引起来的一段内容
- 引号包括
- 单引号
- 双引号
- 三引号
- 单、双引号含义一致
- 单、双引号只能引用一行
- 三引号可以表示多行
#字符串案例
#单引号
love = '爱生活,爱Python!'
print(love)
#双引号
love = "爱生活,爱Python!"
print(love)
#三引号
love = """
爱
生活
爱
Python
"""
print(love)
爱生活,爱Python!
爱生活,爱Python!
爱
生活
爱
Python
- 使用方法修改字符串的大小写
- title()以首字母大写的方式显示每个单词
- upper()将字符串改为全部大写
- lower()将字符串改为全部小写
#首字母大写
animal = "dog momkey tiger"
print(animal.title())
#全部大写
animal = "dog monkey tiger"
print(animal.upper())
#全部小写
animal = "Dog Monkey TIGER"
print(animal.lower())
Dog Momkey Tiger
DOG MONKEY TIGER
dog monkey tiger
- 合并(拼接)字符串
- Python使用(+)来合并字符串
#合并字符串
a = "apple"
b = "banana"
c = a + "+" + b + " are fruit"
print(c.title())
Apple+Banana Are Fruit
- 添加空白
- 使用制表符来添加空白
- 可使用字符组合:\t
- 使用空白符来添加空白
- 可使用字符组合:\n
- 使用制表符来添加空白
print("Python")
#制表符
print("\tPython")
#空白符
print("\n\tPython\n\tLove")
Python
Python
Python
Love
- 删除空白
- 使用rstrip()删除末尾空白
- 临时删除
- 永久删除
- 永久删除必须将删除操作的结果返回到变量中
- 使用lstrip()删除开头空白
- 使用strip()同时删除字符串两端的空白
- 使用rstrip()删除末尾空白
#临时删除末尾空白
a = 'apple '
print(a.rstrip())
#永久性删除末尾空白
a = 'apple '
a = a.rstrip()
print(a)
#删除开头空白
a = ' apple'
print(a.lstrip())
#同时删除两端空白
a = ' apple '
print(a.strip())
apple
apple
apple
apple
None类型
- 表示没有,通常用来占位
- 比如返回,用来表示返回一个空
列表
- 列表是由一系列按特定排列顺序的元素组成的
- 在Python中,用方括号([ ])来表示列表,并用逗号来分隔其中的元素
列表的基本操作
- 访问列表的元素
- 索引从0开始而不是1开始
- 索引指定为-1时,可让Python返回到最后一个元素
- 索引-2返回倒数第二个列表元素
#访问列表中的第二个元素
apple = ["blue","red","yellow"]
print(apple[1])
#索引为-1时
apple = ['blue','red','yellow']
print(apple[-1])
red
yellow
My apple is red.
- 使用列表中的各个值
n = ['1','2','3']
message1 = "The number is " + n[1] + "."
message2 = "The number is " + n[0] + "."
print(message1)
print(message2)
The number is 2.
The number is 1.
- 修改列表中的元素
#修改blue为green
colour = ['red','blue','yellow']
colour[1] = 'green'
print(colour)
[‘red’, ‘green’, ‘yellow’]
- 添加列表中的元素
- 使用append()在列表末尾添加元素
- 使用insert()在列表中插入元素
#末尾添加元素
color = ['red','blue']
color.append('green')
color.append('yellow')
print(color)
#插入元素
color = ['red','blue']
color.insert(0,'yellow')
color.insert(2,'green')
print(color)
[‘red’, ‘blue’, ‘green’, ‘yellow’]
[‘yellow’, ‘red’, ‘green’, ‘blue’]
- 删除列表中的元素
- 使用del语句删除元素
- 如果你知道删除的元素在列表中的位置,可使用del语句
- 使用方法pop()删除元素
- 可删除列表末尾的元素,并让你能够接着使用它
- 弹出列表中任何位置处的元素
- 每当使用pop()时,被弹出的元素就不再在列表中了
- 使用remove()根据值删除元素
- 从列表中删除元素时,也可接着使用他的值
- 使用del语句删除元素
#del删除元素
color = ['red','green','yellow','blue']
del color[0]
print(color)
#pop()删除元素
color = ['red','green','blue']
color.pop()
print(color)
#弹出pop()删除元素
color = ['red','green','blue']
popped_color = color.pop()
print(popped_color)
#弹出列表中任何位置处的元素
color = ['red','yellow','blue','green']
love_color = color.pop(1)
print("My favourite color is " + love_color + ".")
print(color)
#使用remove()删除元素
color = ['red','blue','yellow','green']
bad_color = 'red'
color.remove(bad_color)
print(color)
print("The bad color is " + bad_color + ".")
[‘green’, ‘yellow’, ‘blue’]
[‘red’, ‘green’]
blue
My favourite color is yellow.
[‘red’, ‘blue’, ‘green’]
[‘blue’, ‘yellow’, ‘green’]
The bad color is red
组织列表
- 使用方法sort()对列表进行永久性排序
- 正向排序 sort()
- 反向排序 sort(reverse = True)
#正向排序
a = [ 5 , 6 ,2 , 4]
a.sort()
print(a)
#反向排序
a = [ 5 , 6 ,2 , 4]
a.sort(reverse = True)
print(a)
[2, 4, 5, 6]
[6, 5, 4, 2]
- 使用函数sorted()对列表进行临时排序
- 正向临时排序sorted()
- 反向临时排序sorted(reverse = True)
#正向临时排序
a = [ 5 , 6 ,2 , 4]
print(sorted(a))
print(a)
#反向临时排序
a = [ 5 , 6 ,2 , 4]
b = sorted(a,reverse= True)
print(b)
print(a)
[2, 4, 5, 6]
[5, 6, 2, 4]
[6, 5, 4, 2]
[5, 6, 2, 4]
- 倒着打印列表
- 不是顺序相反的顺序排列列表的元素,而只是反转列表元素的排列顺序
- 永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,再次调用reverse()即可
#倒着输出列表信息
a = [ 5 , 6 ,2 , 4]
a.reverse()
print(a)
[4, 2, 6, 5]
- 确定列表的长度
- 使用len()可快速获悉列表的长度
#列表的长度
a = [ 5 , 6 ,2 , 4]
len(a)
4
本文深入讲解Python中的数据类型,包括数字(Number)、字符串(str)、列表(list)、元组(tuple)、字典(dict)和集合(set),并详细介绍每种类型的特点和使用方法,如整数、浮点数、复数、布尔值等的定义与操作。
2530

被折叠的 条评论
为什么被折叠?



