【数据类型】揭秘,深度解析不同数据类型的奥秘与应用
数据类型是编程语言中用来定义变量存储的数据种类和结构。不同的编程语言支持不同的数据类型,但以下是一些常见的数据类型:
1. "基本数据类型":
- "整数(Integer)":表示没有小数部分的数值,如 `int`、`long`、`short`。
- "浮点数(Float)":表示有小数部分的数值,如 `float`、`double`。
- "布尔(Boolean)":表示逻辑值,只有两个值:`true` 或 `false`。
- "字符(Character)":表示单个字符,如 `char`。
2. "复合数据类型":
- "数组(Array)":一系列相同类型的数据的集合。
- "字符串(String)":字符序列,用于存储文本。
- "集合(Collection)":如列表(List)、集合(Set)、字典(Dictionary)等,用于存储多个不同类型的数据。
3. "枚举(Enum)":一组命名的整数值,用于限制变量的取值范围。
4. "引用数据类型":
- "对象(Object)":由属性和方法组成的数据结构,用于表示现实世界中的实体。
5. "特殊数据类型":
- "日期和时间(Date and Time)":用于表示日期和时间。
- "文件(File)":用于表示文件系统
相关内容:
- 1 数值型1.1 数值类型的创建1.2 数值类型转换1.3 python内置的数学相关函数
- 2 布尔型
- 3 字符型string
- 4 列表(list)
- 5 元组(tuple)
- 6 字典(dict)
- 7 集合(set)
- 作业
计算机是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值。但是,计算机能处理的远不止数值,还可以处理文本、图形、音频、视频、网页等各种各样的数据,不同的数据,需要定义不同的数据类型。在Python中,能够直接处理的数据类型有以下几种
1 数值型
数值型分为以下两种类型:
- 整型整型长整型(2**62)
- 浮点型
- 复数
2 是一个整数的例子。 长整数 不过是大一些的整数。 3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 ** 10-4。 (-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。 |
3.x已不区分整型与长整型,统统称为整型
1.1 数值类型的创建
In : a = 10In : b = aIn : b = 666In : print(a)10In : print(b)666

1.2 数值类型转换
In : a = '3.14'In : b = '100'
In : type(a)
Out: strIn : type(b)
Out: str
In : c = float(a)In : d = int(b)
In : type(c)
Out: floatIn : type(d)
Out: int
1.3 python内置的数学相关函数
函数名 | 描述 |
abs(x) | 返回数字的绝对值,如abs(-10)结果返回10 |
ceil(x) | 返回数字的上入整数,如math.ceil(4.1)结果返回5 |
fabs(x) | 返回数字的绝对值,如math.fabs(-10)结果返回10.0 |
floor(x) | 返回数字的下舍整数,如math.floor(4.9)结果返回4 |
max(x1,x2,…) | 返回给定参数的最大值,参数可为序列类型 |
min(x1,x2,…) | 返回给定参数的最小值,参数可为序列类型 |
pow(x,y) | 计算x的y次幂,结果为x ** y运算后的值 |
round(x ) | 返回浮点数x的四舍五入值,若给定n值,则代表保留n位小数 |
2 布尔型
布尔型只有两个值:
- True
- False
也可用1表示True,用0表示False,请注意首字母大写
In : TrueOut: True
In : 4 > 2Out: True
In : bool()Out: True
In : bool()Out: False
In : True + 1Out: 2
与或非操作:
In : bool(1 and 0)
Out: False
In : bool(1 and 1)
Out: True
In : bool(1 or 0)
Out: True
In : bool(not 0)
Out: True
布尔型经常用在条件判断中:
age=18if age>18: # bool(age>18)
print('old')else:
print('young')
3 字符型string
字符串是以单引号'或双引号"或三引号'''括起来的任意文本,比如'abc',"123",'''456'''等等。
单引号与双引号的区别?
请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此,字符串'abc'只有a,b,c这3个字符。如果'本身也是一个字符,那就可以用""括起来,比如"I'm OK"包含的字符是I,',m,空格,O,K这6个字符
创建字符串
a = 'Hello world!'
b = "Hello tom"
c = '''hehe'''
字符串相关操作:
# 1 * 重复输出字符串In : 'hello' * 2
Out: 'hellohello'
# 2 , 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表In : 'helloworld'
Out: 'lloworld'
# 3 in 成员运算符 - 如果字符串中包含给定的字符返回 TrueIn : 'el' in 'hello'
Out: True
# 4 % 格式字符串In : name = 'wangqing'
In : print('wangqing is a good teacher')
wangqing is a good teacher
In : print('%s is a good teacher' % name)
wangqing is a good teacher
万恶的字符串拼接:
python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
# 5 + 字符串拼接In : a='123'In : b='abc'In : c='789'
In : d1=a+b+cIn : print(d1)123abc789
# + 效率低,该用joinIn : d2=''.join()
In : print(d2)123abc789
python内置的字符串相关函数方法:
统计次数
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
string.count(str, beg=0, end=len(string))
In : a = 'hello world'
In : a.count('l')
Out: 3
In : a.count('l',3)
Out: 2
首字母大写
In : a = 'hello world'
In : a.capitalize()
Out: 'Hello world'
内容居中
string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
In : a = 'hello world'
In : a.center(50)
Out: ' hello world '
In : a.center(50,'-')
Out: '-------------------hello world--------------------'
内容左对齐
string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
In : a = 'hello world'
In : a.ljust(50)
Out: 'hello world '
In : a.ljust(50,'*')
Out: 'hello world***************************************'
内容右对齐
string.rjust(width) 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
a = 'hello world'
a.rjust(50) # ' hello world'
a.rjust(50,'*') # '***************************************hello world'
以指定内容结束
string.endswith(obj, beg=0, end=len(string)) 检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.
a = 'hello world'
a.endswith('d') # True
a.endswith('o',0,5) # True
以指定内容开头
string.startswith(obj, beg=0,end=len(string)) 检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查
a = 'hello world'
a.startswith('h') # True
a.startswith('w',5) # False
通过制表符( )设置字符间的间隔
string.expandtabs(tabsize=8) 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8
a = 'hello world'
a.expandtabs(tabsize=8)
a.expandtabs(tabsize=10)
查找到第一个指定元素的位置,返回其索引值
string.find(str, beg=0, end=len(string)) 检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是,返回开始的索引值,否则返回-1
a = 'hello world'
a.find('w') # 6
a.find('p') # -1
从右往左查找到第一个指定元素的位置,返回其索引值
从右往左找第一个指定元素的位置,返回其索引值,该索引值的位置是从左到右数的
string.rfind(str, beg=0,end=len(string)) 类似于 find()函数,不过是从右边开始查找
a = 'hello world'
a.rfind('w') # 6 虽然是从右往左找,但是顺序是从左数到右的
a.rfind('p') # -1
b = 'hello worldworld'
b.rfind('w') # 11 虽然是从右往左找,但是顺序是从左数到右的
查找到第一个指定元素的位置,返回其索引值
string.index(str, beg=0, end=len(string)) 跟find()方法一样,只不过如果str不在 string中会报一个异常
a = 'hello world'
a.index('w') # 6
a.index('p') # ValueError: substring not found
从右往左查找到第一个指定元素的位置,返回其索引值
string.rindex(str,beg=0,end=len(string)) 类似于 index(),不过是从右边开始
a = 'hello world'
a.rindex('w') # 6
a.rindex('p') # ValueError: substring not found
b = 'hello worldworld'
b.rindex('w') # 11
format格式化字符串
将format方法中指定的变量的值替换到字符串中
a = 'My name is {name},I am {age} years old.'
a.format(name='tom',age=20) # 'My name is tom,I am 20 years old.'
format_map格式化字符串
将format_map方法中指定的变量的值替换到字符串中
a = 'My name is {name},I am {age} years old.'
a.format_map({'name': 'tom','age': 20}) # 'My name is tom,I am 20 years old.'
判断字符串中是否全为数字
string.isnumeric() 如果 string 中只包含数字字符,则返回 True,否则返回 False
a = 'abc12cde'
a.isnumeric() # False
b = '123'
b.isnumeric() # True
判断字符串中是否全为字母
string.isalpha() 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
a = 'abc12cde'
a.isalpha() # False
b = 'abcdef'
b.isalpha() # True
判断是否只包含字母或数字
a = 'abc12cde'
a.isalnum() # True
b = '123'
b.isalnum() # True
c = 'abc123!@#'
c.isalnum() # False
判断是否为十进制
string.isdecimal() 如果 string 只包含十进制数字则返回 True 否则返回 False
a = '1234'
a.isdecimal() # True
判断是否为整数
string.isdigit() 如果 string 只包含数字则返回 True 否则返回 False
a = '1234'
a.isdigit() # True
b = '10.4'
b.isdigit() # False
判断是否为小写字母
string.islower() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
a = 'abcDEF'
a.islower() # False
b = 'abc'
b.islower() # True
判断是否为大写字母
string.isupper() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
a = 'abcDEF'
a.isupper() # False
b = 'ABC'
b.isupper() # True
判断是否为空格
string.isspace() 如果 string 中只包含空格,则返回 True,否则返回 False
a = ' '
a.isspace() # True
b = ' abc'
b.isspace() # False
判断是否为标题
每个单词的首字母大写视为标题
string.istitle() 如果 string 是标题化的(见 title())则返回 True,否则返回 False
a = 'Hello World'
a.istitle() # True
b = 'Hello world'
b.istitle() # False
将大写字母变成小写
string.lower() 转换 string 中所有大写字符为小写
a = 'ABCdef'
b = a.lower()print(b) # abcdef
将小写字母变成大写
string.upper() 转换 string 中的小写字母为大写
a = 'ABCdef'
b = a.upper()print(b) # ABCDEF
将小写字母变成大写同时将大写字母变成小写
string.swapcase() 翻转 string 中的大小写
a = 'ABCdef'
b = a.swapcase()print(b) # abcDEF
把字符串左边、右边的空格或换行符去掉
a = ' haha xixi '
a.strip() # haha xixi
把字符串左边的空格或换行符去掉
a = ' haha xixi '
a.lstrip() # 'haha xixi '
把字符串右边的空格或换行符去掉
a = ' haha xixi '
a.rstrip() # ' haha xixi'
替换内容
string.replace(str1, str2, num=string.count(str1)) 把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次
a = 'hello world world'
b = a.replace('l','p')print(b) # 'heppo worpd worpd'
c = a.replace('l','p',2) # 'heppo world world'
以指定字符作为分隔符将字符串中的元素分别取出来存入一个列表中
string.split(str="", num=string.count(str)) 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
a = 'a:b:c:d:e'
b = a.split(':')print(b) #
c = a.split(':',2)print(c) #
以行为分隔符将字符串中的元素分别取出来存入一个列表中
string.splitlines(num=string.count('
')) 按照行分隔,返回一个包含各行作为元素的列表,如果 num 指定则仅切片 num 个行
a = 'hello world hello haha xixi hehe'
b = a.splitlines()print(b) #
c = a.splitlines(2)print(c) #
把字符串变成标题格式
string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写
a = 'hello world'
b = a.title()print(b) # 'Hello World'
返回字符串 str 中最大的字母
a = 'abcGhijZ'
max(a) # j
返回字符串 str 中最小的字母
a = 'abcGhijZ'
min(a) # G
从指定位置将字符串分豁成三部分
string.partition(str)
有点像 find()和 split()的结合体
从 str 出现的第一个位置起,把字符串string分成一个3元素的元组(string_pre_str,str,string_post_str)
如果 string 中不包含str 则 string_pre_str == string
a = 'abcGhijZ'
b = a.partition('G')print(b) # ('abc', 'G', 'hijZ')
rpartition
string.rpartition(str) 类似于 partition()函数,不过是从右边开始查找
a = 'abcGhiGjZ'
b = a.rpartition('G')print(b) # ('abcGhi', 'G', 'jZ')
4 列表(list)
需求:把班上所有人的名字存起来
有同学说,不是学变量存储了吗,我就用变量存储呗,呵呵,不嫌累吗,同学,如果班里有一百个人,你就得创建一百个变量啊,消耗大,效率低。
又有同学说,我用个大字符串不可以吗,没问题,你的确存起来了,但是,你对这个数据的操作(增删改查)将变得非常艰难,不是吗,我想知道张三的位置,你怎么办?
在这种需求下,Python有了一个重要的数据类型----列表(list)
什么是列表:
列表(list)是Python以及其他语言中最常用到的数据结构之一。Python使用使用中括号 来解析列表。列表是可变的(mutable)——可以改变列表的内容。
列表是一种容器类型:
- 可以包含任意对象的有序集合,通过索引进行访问其中的元素,是一种可变对象,其长度可变
- 支持异构和任意嵌套
- 支持在原处修改
对应操作:
1.定义列表( )
names =
2.查(