Python3学习笔记5-常用的内置函数,ASCII码

124 篇文章 0 订阅

1 昨天看到了Python Built-in functions。
https://docs.python.org/3/library/functions.html页面有每个内置函数的简单介绍。
常用的内置函数有input(),abs(),max(),min(),int(),float(),str(),len(),dir(),help(),sum(),range(),print(),open()等等。

1.1 input()

>>> age = input("Enter your age:")
Enter your age:22
>>> age
'22'
>>> msg = input("-->")
-->it's a nice day!
>>> 
>>> msg
"it's a nice day!"

1.2 abs(),int(),float(),str()

>>> a = -230
>>> b = abs(a)
>>> b
230
>>> c = float(b)
>>> c
230.0
>>> d = str(c)
>>> d
'230.0'
>>> e = float(d)
>>> e
230.0
>>> f = int(e)
>>> f
230

1.3 dir(),help()

dir([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
help([object])
Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.
来自:
https://docs.python.org/3/library/functions.html#dir
https://docs.python.org/3/library/functions.html#help

>>> a = 230
>>> dir(a)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

>>> help(a.__abs__)
Help on method-wrapper object:

__abs__ = class method-wrapper(object)
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __reduce__(...)
 |      helper for pickle
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __objclass__
 |  
 |  __self__
 |  
 |  __text_signature__

1.4 max(),min,range(),bool()

>>> list1 = ['a','b','c',1,2,3,'A',"B"]
>>> min(list1)
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    min(list1)
TypeError: '<' not supported between instances of 'int' and 'str'
# 数字和字符串不能比较大小。前面有str(),int(),float()函数。

>>> list2 = ['a','b','c','1','2','3','A',"B"]
>>> max(list2)
'c'
>>> min(list2)
'1'
>>> bool('A' < 'a')
True

>>> for i in list2:
    print(i)


a
b
c
1
2
3
A
B
>>> 

2 ASCII码对照表。

>>> bool('A' < 'a')
True

这里比较的是A和a在ASCII码表中的大小。
http://www.ascii.net.cn/页面有ASCII码的详细介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值