当前位置:   article > 正文

Python-Class___函数与常用内置函数_python class函数

python class函数

一、函数

1.1、定义函数

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号“ : ”,然后,在缩进块中编写函数体,函数的返回值用return语句返回。

  1. >>def func():
  2. print("---hello word---")
  3. return 0
  4. >>func()
  5. ---hello word---

函数执行完毕也没有return语句时,自动return None。 

  1. >>def func():
  2. print("---hello word---")
  3. return 0
  4. >>print(func())
  5. ---hello word---
  6. 0
  7. ------------------------------------
  8. >>def func():
  9. print("---hello word---")
  10. >>print(func())
  11. ---hello word---
  12. None

1.2、空函数

  1. def func():
  2. pass

1.3、函数作用域

country为全局变量,可以被函数体内调用;

函数体内“func_name”和“csdn_name”为局部变量,无法被其他函数调用。

  1. >>country = "China"
  2. >>def func():
  3. func_name = "FUNC"
  4. print(country + "_"+func_name)
  5. >>def csdn():
  6. csdn_name = "CSDN"
  7. print(country + "_"+csdn_name)
  8. >>func()
  9. >>csdn()
  10. China_FUNC
  11. China_CSDN

二、内置函数

2.1、abs()

返回一个数字的绝对值

  1. >>a = -1
  2. >>b = 2
  3. >>c = 0
  4. print(abs(a))
  5. print(abs(b))
  6. print(abs(c))

2.2、all()

在一个可迭代元素里,如果有一个元素为假及返回False(如果为空返回True)。

  1. >>print(all([0, -1, 2]))
  2. False
  3. ---------------------------
  4. >>print(all([-1, 2]))
  5. True
  6. ---------------------------
  7. >>print(all([]))
  8. True

 2.3、any()

在一个可迭代元素里,如果有一个元素为真及返回True(如果为空返回False)。

  1. >>print(any([0, -1, 2]))
  2. True
  3. ---------------------------
  4. >>print(any([-1, 2]))
  5. True
  6. ---------------------------
  7. >>print(any([]))
  8. False

2.4、ascii()

返回包含对象的可打印表示的字符串,如果对象是非ascii使用\x、\u或\u转义

  1. >>print(ascii('中国'))
  2. '\u4e2d\u56fd'

2.5、bin()

十进制转二进制

  1. >>print(bin(255))
  2. 0b11111111

2.6、 bool()

布尔运算,判断真假

  1. >>print(bool([]))
  2. >>print(bool(()))
  3. >>print(bool(0))
  4. --------------------------------
  5. False
  6. False
  7. False

2.7、callable()

判断是否能被调用

  1. >>def func():
  2. pass
  3. print(callable(func))

2.8、chr()

返回ascii码对应表中数字对应的字符

  1. >>print(chr(97))
  2. a

2.9、dict()

生成一个空字典

  1. >>a = dict()
  2. >>print(a)
  3. {}

2.10、dir()

返回对象可调用的方法

  1. >>a = dir(dict())
  2. >>for i in a:
  3. print(i)
  4. __class__
  5. __contains__
  6. __delattr__
  7. __delitem__
  8. __dir__
  9. __doc__
  10. __eq__
  11. __format__
  12. __ge__
  13. __getattribute__
  14. __getitem__
  15. __gt__
  16. __hash__
  17. __init__
  18. __init_subclass__
  19. __iter__
  20. __le__
  21. __len__
  22. __lt__
  23. __ne__
  24. __new__
  25. __reduce__
  26. __reduce_ex__
  27. __repr__
  28. __setattr__
  29. __setitem__
  30. __sizeof__
  31. __str__
  32. __subclasshook__
  33. clear
  34. copy
  35. fromkeys
  36. get
  37. items
  38. keys
  39. pop
  40. popitem
  41. setdefault
  42. update
  43. values

2.11、 divmod()

除法运算,返回商和余数

  1. >>print(divmod(5, 2))
  2. (2, 1)

2.12、 enumerate()

返回一个包含计数(从开始时默认为0)和迭代获得的值的元组。

  1. >>a = list(enumerate(["张三", "李四", "王五"]))
  2. >>print(a[0])
  3. >>print(a[0][1])
  4. (0, '张三')
  5. 张三

2.13、 eval()

字符串变为字典

  1. >>x = 1
  2. >>print(eval("x+1"))
  3. 2

2.14、filter()

从一个可迭代元素中,根据一定的规则进行筛选,从而生成一个新的迭代器

  1. >>res = filter(lambda n: n > 5, [0, 2, 3, 6, 8, 10, 23])
  2. >>for i in res:
  3. print(i)
  4. 6
  5. 8
  6. 10
  7. 23

2.15、float()

  1. >>> float('+1.23')
  2. 1.23
  3. >>> float(' -12345\n')
  4. -12345.0
  5. >>> float('1e-003')
  6. 0.001
  7. >>> float('+1E6')
  8. 1000000.0
  9. >>> float('-Infinity')
  10. -inf

2.16、format()

格式化字符串

  1. #通过关键字
  2. >>print('{名字今天{动作}'.format(名字='陈某某',动作='拍视频'))
  3. 陈某某今天拍视频
  4. >>grade = {'name' : '陈某某', 'fenshu': '59'}
  5. >>print('{name}电工考了{fenshu}'.format(**grade))
  6. 陈某某电工考了59
  7. -----------------------------------------------------------
  8. #通过位置
  9. >>print('{1}今天{0}'.format('拍视频', '陈某某'))
  10. >>print('{0}今天{1}'.format('陈某某', '拍视频'))
  11. 陈某某今天拍视频
  12. 陈某某今天拍视频
  13. -----------------------------------------------------------
  14. #通过精度f
  15. >>print('{:.1f}'.format(4.234324525254))
  16. >>print('{:.4f}'.format(4.1))
  17. 4.2
  18. 4.1000
  19. -----------------------------------------------------------
  20. #填充和对齐^<>分别表示居中、左对齐、右对齐,后面带宽度
  21. >>print('{:^20}'.format('陈某某'))
  22. >>print('{:>4}'.format('陈某某'))
  23. >>print('{:<10}'.format('陈某某'))
  24. >>print('{:*<14}'.format('陈某某'))
  25. >>print('{:&>14}'.format('陈某某'))
  26. 陈某某
  27. 陈某某
  28. 陈某某
  29. 陈某某***********
  30. &&&&&&&&&&&陈某某
  31. -----------------------------------------------------------
  32. #进制转化,b o d x 分别表示二、八、十、十六进制
  33. >>print('{:b}'.format(250))
  34. >>print('{:o}'.format(250))
  35. >>print('{:d}'.format(250))
  36. >>print('{:x}'.format(250))
  37. 11111010
  38. 372
  39. 250
  40. fa
  41. -----------------------------------------------------------
  42. #千分位分隔符,这种情况只针对与数字
  43. >>print('{:,}'.format(100000000))
  44. >>print('{:,}'.format(235445.234235))
  45. 100,000,000
  46. 235,445.234235

2.17、frozenset()

创建一个不可编辑的集合

2.18、 gloables()

返回表示当前全局符号表的字典,对比参照locals()

2.19、hash()

返回对象的散列值(如果有)。散列值是整数。它们用于在查找字典时快速比较字典键。比较相等的数值具有相同的散列值(即使它们属于不同的类型,如1和1.0)。

  1. >>> a = hash("zzl")
  2. >>> print(a)
  3. -1832558360181557176

2.20、 help()

帮助文档

2.21、 hex()

将一个整数转换为小写的十六进制字符串,前缀为“0x”。如果x不是Python int对象,则必须定义一个__index__()方法来返回一个整数。

  1. >>> print(hex(255))
  2. '0xff'
  3. >>> print(hex(-42))
  4. '-0x2a'

如果你想把一个整数转换成大写或小写的十六进制字符串加上前缀或不带前缀,你可以使用以下两种方法: 

  1. >>> print("%#x" % 255, "%x" % 255, "%X" % 255)
  2. 0xff ff FF
  3. >>> print(format(255, "#x"), format(255, "x"), format(255, "X"))
  4. 0xff ff FF
  5. >>> print(f"{255:#x}", f"{255:x}", f"{255:X}")
  6. 0xff ff FF

2.22、id()

返回内存地址

2.23、input()

输入函数 

2.24、int()

转化成整数

2.25、isinstance()

 如果对象参数是classinfo参数的实例,或者它的(直接、间接或虚拟)子类的实例,则返回true。

  1. >>> a = (1, 2, 3)
  2. >>> print(isinstance(a, list))
  3. False

2.26、iter()

返回一个迭代器,参数必须是一个可序列化的

2.27、lens()

返回一个对象的长度,参数可以是序列(如字符串、字节、元组、列表或范围)或集合(如字典、集合或冻结集)。 

2.28、locals()

更新和返回表示当前本地符号表的字典

  1. >>> def func():
  2. local_var = 333
  3. print(locals())
  4. print(locals().get("local_var"))
  5. >>> func()
  6. >>> print(globals().get("local_var"))
  7. >>> print(locals().get("local_var"))
  8. {'local_var': 333}
  9. 333
  10. None
  11. None

2.29、map() 

从一个可迭代元素中,根据一定的规则进行处理,从而生成一个新的迭代器

  1. >>> res = map(lambda n: n * n, [0, 2, 3, 6, 8, 10, 23])
  2. >>> for i in res:
  3. print(i)
  4. 0
  5. 4
  6. 9
  7. 36
  8. 64
  9. 100
  10. 529
  11. ----------------------------------------------------------
  12. >>> res = map(lambda n: n > 5, [0, 2, 3, 6, 8, 10, 23])
  13. >>> for i in res:
  14. print(i)
  15. False
  16. False
  17. False
  18. True
  19. True
  20. True
  21. True

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/490121
推荐阅读
相关标签
  

闽ICP备14008679号