python打印变量类型
Declare different types of variables; print their types, ids and variables in Python.
声明不同类型的变量; 在Python中打印其类型,id和变量。
There are two inbuilt functions are using in the program:
程序中使用了两个内置函数:
-
type() – its returns the data type of the variable/object.
type() -返回变量/对象的数据类型 。
-
id() – it returns the unique identification number (id) of created object/variable.
id() -返回创建的对象/变量的唯一标识号(id) 。
Program:
程序:
- print("Numbers")
- print("---------------------------------")
- a=10
- print(type(a),id(a),a)
- a=23.7
- print(type(a),id(a),a)
- a=2+6j
- print(type(a),id(a),a)
-
-
- print("\nText")
- print("---------------------------------")
- a='h'
- print(type(a),id(a),a)
- a="h"
- print(type(a),id(a),a)
- a='hello'
- print(type(a),id(a),a)
- a="hello"
- print(type(a),id(a),a)
-
- print("\nBoolean")
- print("---------------------------------")
- a=True
- print(type(a),id(a),a)
-
- print("\nFunction")
- print("---------------------------------")
- def fun1():
- return "I am Function"
- a=fun1
- print(type(a),id(a),a())
-
- print("\nObjects")
- print("---------------------------------")
- class Demo:
- def hi(self):
- return "Hi"
- a=Demo()
- print(type(a),id(a),a.hi())
-
- print("\nCollections")
- print("---------------------------------")
- a=[1,2,3]
- print(type(a),id(a),a)
- a=[]
- print(type(a),id(a),a)
- a=(1,2,3)
- print(type(a),id(a),a)
- a=()
- print(type(a),id(a),a)
- a=1,2,3
- print(type(a),id(a),a)
- a={1,2,3}
- print(type(a),id(a),a)
- a={}
- print(type(a),id(a),a)
- a={"id":1,"name":"pooja"}
- print(type(a),id(a),a)
Output
输出量
- Numbers
- ---------------------------------
- <class 'int'> 10455328 10
- <class 'float'> 139852163465696 23.7
- <class 'complex'> 139852162869456 (2+6j)
-
- Text
- ---------------------------------
- <class 'str'> 139852162670976 h
- <class 'str'> 139852162670976 h
- <class 'str'> 139852162911512 hello
- <class 'str'> 139852162911512 hello
-
- Boolean
- ---------------------------------
- <class 'bool'> 10348608 True
-
- Function
- ---------------------------------
- <class 'function'> 139852163226616 I am Function
-
- Objects
- ---------------------------------
- <class '__main__.Demo'> 139852162234016 Hi
-
- Collections
- ---------------------------------
- <class 'list'> 139852162259208 [1, 2, 3]
- <class 'list'> 139852162261256 []
- <class 'tuple'> 139852162244752 (1, 2, 3)
- <class 'tuple'> 139852182028360 ()
- <class 'tuple'> 139852162244968 (1, 2, 3)
- <class 'set'> 139852163034472 {1, 2, 3}
- <class 'dict'> 139852163024776 {}
- <class 'dict'> 139852163024584 {'id': 1, 'name': 'pooja'}
翻译自: https://www.includehelp.com/python/declare-different-types-of-variables-print-their-values-types-and-ids.aspx
python打印变量类型