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码的详细介绍。