五. 基本数据类型
Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。
在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。
Python3 中有六个标准的数据类型:
- Number(数字)
- String(字符串)
- List(列表)
- Tuple(元组)
- Set(集合)
- Dictionary(字典)
#!usr/bin/pathon3
a = 100
b = "1001.1"
c = "haha"
print(a)
print(b)
print(c)
结果:
100
1001.1
haha
查询变量类型:
#!usr/bin/python3
a = 10
b = 1.2
c = "haha10"
print(type(a))
print(type(b))
print(type(c))
结果:
<class 'int'>
<class 'float'>
<class 'str'>
用 isinstance 来判断类型
a = 111
b = isinstance(a, int)
print("b = ", b)
结果:
b = True
5.1 字符串
5.1.1 基本操作
#!/usr/bin/python3
str = '123456789'
print(str) # 输出字符串
print(str[0:-1]) # 输出第一个到倒数第二个的所有字符
print(str[0]) # 输出字符串第一个字符
print(str[2:5]) # 输出从第三个开始到第五个的字符
print(str[2:]) # 输出从第三个开始后的所有字符
print(str[1:5:2]) # 输出从第二个开始到第五个且每隔一个的字符(步长为2)
print(str * 2) # 输出字符串两次
print(str + '你好') # 连接字符串
print('------------------------------')
print('hello\nworld') # 使用反斜杠(\)+n转义特殊字符
print(r'hello\nworld') # 在字符串前面添加一个 r,表示原始字符串,不会发生转义
结果:
123456789
12345678
1
345
3456789
24
123456789123456789
123456789你好
------------------------------
hello
world
hello\nworld
5.1.2 字符串反转操作
def reverseWords(input):
inputWords = input.split(" ")
inputWords = inputWords[-1::-1]
output = ' '.join(inputWords)
return output
if __name__ == "__main__":
input = 'haha hehe xixi'
rw = reverseWords(input)
print(rw)
结果:
xixi hehe haha
5.2.列表
#!/usr/bin/python3
list = [ 'abcd', 123 , 2.2, 'haha', 70.2 ]
tinylist = [3, 'hhhh']
print (list) # 输出完整列表
print (list[0]) # 输出列表第一个元素
print (list[1:3]) # 从第二个开始输出到第三个元素
print (list[2:]) # 输出从第三个元素开始的所有元素
print (tinylist * 2) # 输出两次列表
print (list + tinylist) # 连接列表
结果:
['abcd', 123, 2.2, 'haha', 70.2]
abcd
[123, 2.2]
[2.2, 'haha', 70.2]
[3, 'hhhh', 3, 'hhhh']
['abcd', 123, 2.2, 'haha', 70.2, 3, 'hhhh']
5.3 元组(Tuple)
元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号 () 里,元素之间用逗号隔开。
元组中的元素类型也可以不相同:
#!/usr/bin/python3
tuple = ( 'abcd', 786 , 2.23, 'haha', 70.2 )
tinytuple = (123, 'heihei')
print (tuple) # 输出完整元组
print (tuple[0]) # 输出元组的第一个元素
print (tuple[1:3]) # 输出从第二个元素开始到第三个元素
print (tuple[2:]) # 输出从第三个元素开始的所有元素
print (tinytuple * 2) # 输出两次元组
print (tuple + tinytuple) # 连接元组
结果:
('abcd', 786, 2.23, 'haha', 70.2)
abcd
(786, 2.23)
(2.23, 'haha', 70.2)
(123, 'heihei', 123, 'heihei')
('abcd', 786, 2.23, 'haha', 70.2, 123, 'heihei')
5.3 set
基本操作:
#!/usr/bin/python3
sites = {'Google', 'Taobao', 'Facebook', 'Zhihu', 'Baidu'}
print(sites) # 输出集合,重复的元素被自动去掉
# 成员测试
if 'Baidu' in sites :
print('Baidu 在集合中')
else :
print('Baidu 不在集合中')
结果:
{'Facebook', 'Google', 'Baidu', 'Zhihu', 'Taobao'}
Baidu 在集合中
5.4 Dictionary(字典)
#!/usr/bin/python3
dict = {}
dict['haha'] = "hehe"
dict[2] = 3343
print (dict['haha'])
print (dict[2])
print (dict)
print (dict.keys())
print (dict.values())
结果:
hehe
3343
{'haha': 'hehe', 2: 3343}
dict_keys(['haha', 2])
dict_values(['hehe', 3343])
六. 数据类型转换
6.1 隐式类型转换
在隐式类型转换中,Python 会自动将一种数据类型转换为另一种数据类型,不需要干预。
a = 123
b = 1.23
c = a + b
print("datatype of a:", type(a))
print("datatype of b:", type(b))
print("Value of c:", c)
print("datatype of c:", type(c))
结果:
datatype of a: <class 'int'>
datatype of b: <class 'float'>
Value of c: 124.23
datatype of c: <class 'float'>
6.2 显式类型转换
a = int(11)
b = int(2.3)
c = int("34")
print("a=",a)
print("b=",b)
print("c=",c)
结果:
a= 11
b= 2
c= 34
x = float(122)
y = float(2.2)
z = float("9")
w = float("4.8")
print(x)
print(y)
print(z)
print(w)
结果:
122.0
2.2
9.0
4.8
a = str("sss1")
b = str(32)
c = str(23.3)
print("a = ", a)
print("b = ", b)
print("c = ", c)
结果:
a = sss1
b = 32
c = 23.3