学习python的第二天之数据类型
数据类型
-
Numbers(数字)
num_int = 45 #int 整数类型 num_float = 3.1415926 #float 浮点数类型 # 复数类型: # 通过 complex() 函数,可以传入两个参数(实部和虚部): z = complex(3, 4) print(z) # 输出: (3+4j) # 复数对象有两个属性:.real 和 .imag,分别表示复数的实部和虚部: z = 3 + 4j print(z.real) # 输出: 3.0 print(z.imag) # 输出: 4.0 # 共轭复数:使用 .conjugate() 方法得到复数的共轭。 z = 3 + 4j print(z.conjugate()) # 输出: (3-4j) # 获取复数的模(即复数的绝对值):使用 abs() 函数。 z = 3 + 4j print(abs(z)) # 输出: 5.0 复数相位:可以使用 cmath 模块中的 phase() 函数获取。 import cmath z = 3 + 4j print(cmath.phase(z)) # 输出相位,以弧度为单位
-
String(字符串)
字符串的内容需要用‘ ‘或“ ”或’‘’ ‘’‘或”“” “”“引起来。
string_1 = 'hello' #str 字符串类型 string_2 = "hello" string_3 = '''hello'''#三引号是允许换行的 string_4 = ''' hello world ! ''' #三引号是允许换行的 string_5 = """hello"""#三引号是允许换行的 string_6 = '45' #str 字符串类型
-
bool(布尔类型)
0, ‘’, None 均也可以表示False
1和非空的值均也可以表示True
另外True可以当作1(Flase当作0),参与到算数运算中bool_true_1 = True #True 布尔类型 bool_true_2 = 2 > 1 #True 布尔类型 bool_False_1 = False #False 布尔类型 bool_False_2 = 2 < 1 #False 布尔类型 a1 = 0 a2 = '' a3 = [] a4 = None a5 = 1 a6 = '0' a7 = True + 3 a8 = Flase * 5 print(not a1) #--> True print(not a2) #--> True print(not a3) #--> True print(not a4) #--> True print(not a5) #--> False print(not a6) #--> False print(a7) #--> 4 print(a8) #--> 0
-
byte(字节类型)
byte_1 =b'Hello'#list 列表类型
-
List(列表)
允许重复元素
list_1 = ['apple', 'pear', 'orange', 'banana'] #list 列表类型
-
Dictionary(字典)
不允许重复元素
dictionary = {'name': 'Tom', 'age': 18, 'addr': 'china'} #dict 字典类型
-
Tuple(元组)
允许重复元素
tuple_1 = (0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9) #tuple 元组类型
-
Set(集合)
不允许重复元素
set_1 = {1, 'hello', 2, '呵呵', 3, True}
Python3 已经废弃long类型
查看数据类型
使用内置type()类
a = 12
b = 3.14
c = 'hello'
d = True
e = b'hello'
f = ['apple', 'pear', 'orange', 'banana']
g = {'name': 'Tom', 'age': 18, 'addr': 'china'}
h = (0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
i = {1, 'hello', 2, '呵呵', 3, True}
print(type(a)) #--> <class 'int'>
print(type(b)) #--> <class 'float'>
print(type(c)) #--> <class 'str'>
print(type(d)) #--> <class 'bool'>
print(type(e)) #--> <class 'bytes'>
print(type(f)) #--> <class 'list'>
print(type(g)) #--> <class 'dict'>
print(type(h)) #--> <class 'tuple'>
print(type(i)) #--> <class 'set'>
#四种类型是否允许重复元素情况
j = [1, 2, 3, 1, 2, 3]
k = {'one':1, 'two':2, 'three':3, 'one':1, 'two':2, 'three':3}
l = (1, 2, 3, 1, 2, 3)
m = {1, 2, 3, 1 , 2, 3}
print(j) #--> [1, 2, 3, 1, 2, 3], 允许
print(k) #--> {'one': 1, 'two': 2, 'three': 3}, 不允许
print(l) #--> (1, 2, 3, 1, 2, 3), 允许
print(m) #--> {1, 2, 3}, 不允许
在Python里,变量是没有数据类型的,我们所说变量的数据类型,其实是变量对应的值的数据类型。也就是说,对于同一个变量,其查询的类型会随赋予变量的内容的改变而改变。
占位字符
用于在字符串中占位的
%s:(str)给字符串占位的,如果值是一个整形,默认会将整型或者浮点型转换成字符串;
%d:(digit)专门给整型占位的,即使给的不是整型,浮点型会取整型部分,而字符串就会报错;
%f:(float)给浮点型占位的(默认小数点后6位,超过的四舍五入),并且可以设置小数点后面的位数:%.nf(n表示的是位数)并且超过的四舍五入;
a1 = '小星星'
a2 = 1313
a3 = 3.1415926
print('我们一起唱%s' %a1) # 我们一起唱小星星
print('我们一起唱%s' %a2) # 我们一起唱1313
print('我们一起唱%s' %a3) # 我们一起唱3.1415926
print('我们一起唱%d' %a1) # 报错 TypeError: %d format: a number is required, not str
print('我们一起唱%d' %a2) # 我们一起唱1313
print('我们一起唱%d' %a3) # 我们一起唱3
print('我们一起唱%f' %a1) # 报错 TypeError: must be real number, not str
print('我们一起唱%f' %a2) # 我们一起唱1313.000000
print('我们一起唱%f' %a3) # 我们一起唱3.141593
print('我们一起唱%.4f' %a3) # 我们一起唱3.1416
print('我们一起唱%.7f' %a3) # 我们一起唱3.1415926
检查一个对象是否是一个已知的类型
isinstance() 是 Python 中的一个内置函数,用于检查一个对象是否是一个已知的类型,或者是否是一个已知类型的子类。其常用形式为 isinstance(object, classinfo),其中 object 是要进行检查的对象,classinfo 是一个类、类型或者由类和类型组成的元组。
参数
object:要检查的对象。
classinfo:一个类、类型,或者包含类和类型的元组。
返回值
如果 object 是 classinfo 类型的实例对象或其子类的实例对象,则返回 True。
如果 object 不是 classinfo 类型的实例对象或其子类的实例对象,则返回 False。
检查整数,字符串,浮点数,列表,字典,元组,集合
a = 5
b = 'Hello'
c = 3.14
d = [1, 2]
e = {'1':'one', '2':'two'}
f = (1, 2)
g = {1, 2}
print(isinstance(a, int)) # True,因为 a 是一个整数
print(isinstance(b, str)) # True,因为 b 是一个字符串
print(isinstance(c, float)) # True,因为 c 是一个浮点数
print(isinstance(d, list)) # True,因为 d 是一个列表
print(isinstance(e, dict)) # True,因为 e 是一个字典
print(isinstance(f, tuple)) # True,因为 f 是一个元组
print(isinstance(g, set)) # True,因为 g 是一个数组
检查元组中的类型
x = "Hello"
print(isinstance(x, (str, int, float))) # True,因为 x 是一个字符串,而字符串是元组中的一个类型
检查自定义类:
class MyClass:
pass
class AnotherClass(MyClass):
pass
obj = MyClass()
another_obj = AnotherClass()
print(isinstance(obj, MyClass)) # True,因为 obj 是 MyClass 的一个实例
print(isinstance(another_obj, MyClass)) # True,因为 another_obj 是 MyClass 的子类的一个实例
print(isinstance(another_obj, AnotherClass)) # True,因为 another_obj 是 AnotherClass 的一个实例