2022年 11月 7日

python打印变量类型_Python | 声明不同类型的变量,打印其值,类型和ID

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:

程序中使用了两个内置函数:

  1. type() – its returns the data type of the variable/object.

    type() -返回变量/对象的数据类型

  2. id() – it returns the unique identification number (id) of created object/variable.

    id() -返回创建的对象/变量的唯一标识号(id)

Program:

程序:

  1. print("Numbers")
  2. print("---------------------------------")
  3. a=10
  4. print(type(a),id(a),a)
  5. a=23.7
  6. print(type(a),id(a),a)
  7. a=2+6j
  8. print(type(a),id(a),a)
  9. print("\nText")
  10. print("---------------------------------")
  11. a='h'
  12. print(type(a),id(a),a)
  13. a="h"
  14. print(type(a),id(a),a)
  15. a='hello'
  16. print(type(a),id(a),a)
  17. a="hello"
  18. print(type(a),id(a),a)
  19. print("\nBoolean")
  20. print("---------------------------------")
  21. a=True
  22. print(type(a),id(a),a)
  23. print("\nFunction")
  24. print("---------------------------------")
  25. def fun1():
  26. return "I am Function"
  27. a=fun1
  28. print(type(a),id(a),a())
  29. print("\nObjects")
  30. print("---------------------------------")
  31. class Demo:
  32. def hi(self):
  33. return "Hi"
  34. a=Demo()
  35. print(type(a),id(a),a.hi())
  36. print("\nCollections")
  37. print("---------------------------------")
  38. a=[1,2,3]
  39. print(type(a),id(a),a)
  40. a=[]
  41. print(type(a),id(a),a)
  42. a=(1,2,3)
  43. print(type(a),id(a),a)
  44. a=()
  45. print(type(a),id(a),a)
  46. a=1,2,3
  47. print(type(a),id(a),a)
  48. a={1,2,3}
  49. print(type(a),id(a),a)
  50. a={}
  51. print(type(a),id(a),a)
  52. a={"id":1,"name":"pooja"}
  53. print(type(a),id(a),a)

Output

输出量

  1. Numbers
  2. ---------------------------------
  3. <class 'int'> 10455328 10
  4. <class 'float'> 139852163465696 23.7
  5. <class 'complex'> 139852162869456 (2+6j)
  6. Text
  7. ---------------------------------
  8. <class 'str'> 139852162670976 h
  9. <class 'str'> 139852162670976 h
  10. <class 'str'> 139852162911512 hello
  11. <class 'str'> 139852162911512 hello
  12. Boolean
  13. ---------------------------------
  14. <class 'bool'> 10348608 True
  15. Function
  16. ---------------------------------
  17. <class 'function'> 139852163226616 I am Function
  18. Objects
  19. ---------------------------------
  20. <class '__main__.Demo'> 139852162234016 Hi
  21. Collections
  22. ---------------------------------
  23. <class 'list'> 139852162259208 [1, 2, 3]
  24. <class 'list'> 139852162261256 []
  25. <class 'tuple'> 139852162244752 (1, 2, 3)
  26. <class 'tuple'> 139852182028360 ()
  27. <class 'tuple'> 139852162244968 (1, 2, 3)
  28. <class 'set'> 139852163034472 {1, 2, 3}
  29. <class 'dict'> 139852163024776 {}
  30. <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打印变量类型