当前位置:网站首页>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]:身高。
边栏推荐
- deepstream学习笔记(三):deepstream-imagedata-multistream解析与接入适配yolov5模型测试
- [SUCTF 2019]CheckIn (.htaccess和.user.ini)
- YOLOV5学习笔记(七)——训练自己数据集
- 【集训DAY3】挖金矿【二分答案】
- Kubernetes 开发环境比对
- ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
- JVM Memory and Garbage Collection - 10. Direct Memory
- 信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念
- In-depth understanding of multithreading (Part 1)
- 漫谈缺陷管理的自动化实践方案
猜你喜欢
随机推荐
断开和服务器共享连接的方法「建议收藏」
In-depth understanding of multithreading (Part 1)
考柏的感慨
Leecode-205. 同构字符串
AUTOCAD——形位公差如何标注、CAD打断于点的操作
JVM内存和垃圾回收-10.直接内存
The older tester has just passed the "hurdle" of being 35 years old, and I want to tell you something from my heart
Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)
CAD 绘制圆角处理
Redis-基本介绍/linux下环境配置/配置文件
[SUCTF 2019]CheckIn (.htaccess和.user.ini)
数字孪生电力系统,可视化应用实现科学调度的电子设备
KingbaseGIS Jin Cang database using manual (6.3. Geometric object creation function)
AirFlow介绍
第十二,十三章 mysql数据类型,视图的课后练习
【数据存储】signed,unsigned到底怎么区分?如何计算?
漫谈缺陷管理的自动化实践方案
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
const修饰指针的三种情况
数据库的备份与恢复「建议收藏」