Python中可使用type()函数输出变量类型。示例:对于整数num,type(num)返回<class 'int'>;对于浮点数fnum,type(fnum)返回<class 'float'>;对于字符串string,type(string)返回<class 'str'>;对于布尔值flag,type(flag)返回<class 'bool'>;对于列表my_list,type(my_list)返回<class 'list'>;对于

如何输出 Python 变量类型
在 Python 中,可以使用 type() 函数来输出变量的类型。该函数接受一个变量作为参数,并返回其类型。
以下是一些输出变量类型的示例:
<code class="python"># 整数
num = 10
type(num) # <class 'int'>
# 浮点数
fnum = 3.14
type(fnum) # <class 'float'>
# 字符串
string = "Hello World"
type(string) # <class 'str'>
# 布尔值
flag = True
type(flag) # <class 'bool'>
# 列表
my_list = [1, 2, 3]
type(my_list) # <class 'list'>
# 元组
my_tuple = (1, 2, 3)
type(my_tuple) # <class 'tuple'>
# 字典
my_dict = {"name": "John Doe", "age": 30}
type(my_dict) # <class 'dict'></code>需要注意的是,type() 函数返回的值是一个类型对象,表示变量的类型。要获取类型的名称,可以使用 type() 的 name 属性。
立即学习“Python免费学习笔记(深入)”;
<code class="python"># 获取类型的名称 print(type(num).__name__) # int print(type(fnum).__name__) # float print(type(string).__name__) # str</code>
以上就是python如何输出变量类型的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号