当前位置:网站首页>7. type( )函数——查询数据类型
7. type( )函数——查询数据类型
2022-08-09 23:32:00 【安迪Python】
7. type( )函数——查询数据类型
1. print( )函数知识回顾
print( )函数:Python中的输出函数,作用是将print( )函数括号里的内容输出到屏幕上。
print( )函数的语法
print( )函数由4部分组成:
1.函数名:print
2.英文小括号:( )
3.英文引号(有4种使用情形)
4.要输出的内容

2. 什么是type( )函数
type[taɪp]:类型。
type的中文意思是类型。
type( )函数在Python中的作用是查询括号中的内容的数据类型。
print( )函数:输出函数。
type( )函数:查询数据类型函数。
【功能】查询type( )函数括号中的内容的数据类型。
3. type( )函数的语法
type( )函数的语法与print( )函数的语法类似。
type( )函数由3部分组成:
1.函数名:type
2.英文小括号:( )
3.要查询的内容。(内容是变量或具体的数据内容)
函数括号里的值我们通常称为参数。
【type函数的参数为变量】

【type函数的参数为具体的数据本身】

4. type( )函数的返回值
1.str:表示数据类型为字符串
2.int:表示数据类型为整数
3.float:表示数据类型为浮点数
5. type( )函数示例
【示例1】
name = "任嘉伦"
type(name)
在.py文件运行上述代码,我们看不到任何的输出,代码也不会报错。
因为type函数只能完成查询数据类型的指令,不能实现输出。
如果我们想知道变量name的数据类型,我们就需要使用print函数进行输出。
所以,type函数通常是和print函数搭配使用的。
# 新建一个name变量
name = "任嘉伦"
# 用type函数查询该变量的数据类型并输出
print(type(name))
【终端输出】
<class 'str'>
type函数括号里为变量名,即查询变量的数据类型。
运行代码后,输出<class 'str'>。
表示变量name的数据类型为字符串。
class [klɑːs]:种类。
【示例2】
# 新建一个age变量
age = 19
# 用type函数查询该变量的数据类型并输出
print(type(age))
【终端输出】
<class 'int'>
运行代码后,输出<class 'int'>。
表示变量age的数据类型为整数。
【示例3】
# 新建一个π变量
π = 3.14
# 用type函数查询该变量的数据类型并输出
print(type(π))
【终端输出】
<class 'float'>
运行代码后,输出<class 'float'>。
表示变量π的数据类型为浮点数。
【示例4】
括号里也可以直接写入查询的内容。
print(type("任嘉伦"))
print(type(18))
print(type(3.14))
【终端输出】
<class 'str'>
<class 'int'>
<class 'float'>
type函数括号里也可以直接写入查询的内容。
6. 总结
type( )函数:查询数据类型函数。
type( )函数的参数可以是变量,也可以是数据本身。
重点注意参数为变量时,变量不加引号。
7. 课后练习
1.请回答后面代码分别代表什么类型: <class 'str'> <class 'int'> <class 'float'>
2.定义3个变量,分别存储张三,65,180.5,并输出三个变量的数据类型。
【参考答案】
# 定义三个变量
name = '张三'
weight = 65
height = 180.5
# print( )函数查看数据类型
print(type(name))
print(type(weight))
print(type(height))
【终端输出】
<class 'str'>
<class 'int'>
<class 'float'>
weight[weɪt]:体重。
height [haɪt]:身高。
边栏推荐
猜你喜欢

防火墙之系统防护

Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)

Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom

YOLOV5 study notes (7) - training your own data set

Why don't suggest you run in Docker Mysql?

从TRPO到PPO(理论分析与数学证明)

经济衰退即将来临前CIO控制成本的七种方法

labelme标注的json标签转txt格式

程序员从佩洛西窜访事件中可以学到什么?

【数据存储】signed,unsigned到底怎么区分?如何计算?
随机推荐
AirFlow介绍
WPF DataGrid 使用数据模板
【渗透工具】浏览器数据导出工具
Golden Warehouse Database KingbaseGIS User Manual (6.5. Geometry Object Editing Function)
ES6 从入门到精通 # 13:数组的扩展方法二
数字孪生智慧制造生产线项目实施方案,平台认知与概念
【SSL集训DAY2】Sort【树状数组】
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
Creo5.0 introductory tutorial free material
拒绝“重复造轮子”,百度EasyDL让你玩转AI定制开发
NotWritableError: The current user does not have write permissions when conda creates a new environment
天猫全网商品详情封装接口
MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
基于ABP的AppUser对象扩展
解锁时间生成与比较
十位时间戳转化成时间
LeetCode常见题型——链表
【SSL集训DAY2】有趣的数【数位DP】
Wireshark经典实践和面试13点总结