阿里云大学人工智能学前小测验-Python测试
4.代码 print(type([1,2])) 输出结果为
A. <class 'list'>
B. <class 'tuple'>
C. <class 'int'>
D. <class 'set'>
如何知道一个变量的数据类型
基本上入门级最开始使用的就是type 函数,如:
1 num1 = 123
2 str1 = 'hello'
3 noneobj = None
4 print(type(num1))
5 print(type(str1))
6 print(type(noneobj))
7
8 <class 'int'>
9 <class 'str'>
10 <class 'NoneType'>
直接对数据对象本身进行type ,如type(123),type('hello'),type(None),结果是一样的。
对于一些内置变量,比如abs 函数,max 函数, len 函数,也都可以使用type 去获取他们的对象类型。
1 print(type(abs))
2 print(type(max))
3 print(type(len))
4
5 <class 'builtin_function_or_method'> # 这一类说明是Python 内置的函数
6 <class 'builtin_function_or_method'>
7 <class 'builtin_function_or_method'>
补充:python数据类型
字符串 str string “”或 ‘’
列表 list []
元祖 tuple () 类似于列表,但不能修改元素
字典 dict dictionary {} 键值对
集合 set {} 非键值对
集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。