大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
>>> abs(9.8)
9.8
>>> abs(-9.8)
9.8
>>> dict({"key":"value"})
{'key': 'value'}
>>> help(map)
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
-- More --
print(min([3, 4, 2]))
print(min("wqeqwe"))
print(min((3, 6, 4)))
print(max([3, 4, 2]))
print(max("wqeqwe"))
print(max((3, 6, 4)))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
2
e
3
4
w
6
Process finished with exit code 0
print(all([1, 2, 0])) # 列表中的0是False,所以返回False
print(all([1, 2, 5])) # 列表中的所有值都是True,所以返回True
print(all([])) # 空的列表,all()返回true
print(help(all))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
False
True
True
Help on built-in function all in module builtins:
all(iterable, /)
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
None
Process finished with exit code 0
any()列表中的任意一个为True,就返回True
print(any([1, 2, 0]))
print(any([1, 2, 5])) # 列表中的任意一个是True,就返回True
print(any([])) # 空的列表,any()返回false
print(help(any))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
True
True
False
Help on built-in function any in module builtins:
any(iterable, /)
Return True if bool(x) is True for any x in the iterable.
If the iterable is empty, return False.
None
Process finished with exit code 0
print(dir()) # 打印当前程序的所有变量
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
Process finished with exit code 0
hex()转换为16进制
print(hex(16))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0x10
Process finished with exit code 0
>>> l = [2,3,4,5,6,7]
>>> s = slice(1,5,2)
>>> l(s)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'list' object is not callable
>>> l[s]
[3, 5]
>>> divmod(10,3)
(3, 1)
>>> divmod(10,2)
(5, 0)
>>>
>>> sorted([1,9,4])
[1, 4, 9]
d = {1: 0, 10: 4, 9: 2, 15: 3}
print(d.items())
print(sorted(d.items(), key=lambda x: x[1], reverse=True))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
dict_items([(1, 0), (10, 4), (9, 2), (15, 3)])
[(10, 4), (15, 3), (9, 2), (1, 0)]
Process finished with exit code 0
>>> ascii("qwqw我")
"'qwqw\\u6211'"
>>> print(oct(8))
0o10
>>> print(bin(10))
0b1010
print(eval("{1:2}"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{1: 2}
Process finished with exit code 0
print(eval("1+2*3"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
7
Process finished with exit code 0
eval('print("hello")')
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
hello
Process finished with exit code 0
eval()只能解析单行代码,不能解析多行的代码
code = '''
if 3 > 2:
print("3>2")
'''
eval(code)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
File "E:/PythonProject/python-test/BasicGrammer/test.py", line 9, in
eval(code)
File "", line 2
if 3 > 2:
^
SyntaxError: invalid syntax
Process finished with exit code 1
code = '''
if 3 > 2:
print("3>2")
'''
exec(code)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
3>2
Process finished with exit code 0
code = '''
def foo():
if 3 > 2:
print("3>2")
return 3
foo()
'''
re_exec = exec(code)
print(re_exec)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
3>2
None
Process finished with exit code 0
print(eval("1+2+3"))
print(exec("1+2+3"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
6
None
Process finished with exit code 0
ord() 获取对应的ascii码表中的值
chr() 获取ascii表中值对应的字符
>>> ord("a")
97
>>> chr(97)
'a'
>>> sum((1,2,3))
6
>>> sum([1,2,3])
6
>>> sum({1:2,3:4})
4
>>> sum({1,2,3,4})
10
>>> sum("123")
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
>>> s = "woai中国"
>>> s[0] = "W"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
>>> s = bytearray(s)
Traceback (most recent call last):
File "", line 1, in
TypeError: string argument without an encoding
>>> s = s.encode('utf-8')
>>> s
b'woai\xe4\xb8\xad\xe5\x9b\xbd'
>>> s = bytearray(s)
>>> s
bytearray(b'woai\xe4\xb8\xad\xe5\x9b\xbd')
>>> s[0]='W'
Traceback (most recent call last):
File "", line 1, in
TypeError: an integer is required
>>> s[0]=97
>>> s
bytearray(b'aoai\xe4\xb8\xad\xe5\x9b\xbd')
>>> s.decode('utf-8')
'aoai中国'
>>> id(s[0]) # s[0]的内存地址会变
1487327920
>>> s[0]=66
>>> id(s[0])
1487326928
>>> id(s)
2879739806752 # s的内存地址是不变的
>>> s[0]=67
>>> id(s)
2879739806752
map()
>>> list(map(lambda x:x*x,[1,2,3]))
[1, 4, 9]
filter()
>>> list(filter(lambda x:x>3,[1,2,3,4,5]))
[4, 5]
>>> import functools
>>> functools.reduce(lambda x,y:x+y,[1,2,3,4])
10
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4])
24
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4],2)
48
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4],3)
72
>>> functools.reduce(lambda x,y:x+y,[1,2,3,4],3)
13
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
msg = "msg"
# 文件的模式必须是可写入模式(w,r+),不能是只读模式
f = open(file="写文件.txt", mode="w", encoding="utf-8")
print(msg, "my input", sep="|", end=":::",file=f)
运行程序
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Process finished with exit code 0
查看"写文件.txt"
msg|my input:::
>>> a = [1,2,3]
>>> tuple(a)
(1, 2, 3)
>>> tuple("1,2,3")
('1', ',', '2', ',', '3')
>>> tuple({1:2})
(1,)
>>> tuple(2)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not iterable
callable()判断是否可调用,即通过abc()方式调用
函数是可调用的,可用于判断是否是函数
>>> callable(abs)
True
>>> callable(list)
True
>>> callable([1,2,3])
False
>>> s = frozenset({1,2,3})
>>> s.discard(2)
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'frozenset' object has no attribute 'discard'
>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1
, 2, 3]}
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
'a', 'd', 'l', 's']
>>>
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1, 2,
3]}
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1, 2, 3]}
>>>
>>> repr(abs(23))
'23'
>>> repr(frozenset({12,4}))
'frozenset({12, 4})'
>>> repr({1,2,3})
'{1, 2, 3}'
>>>
>>> a = [1,2,3,4,5]
>>> b = ["a","b","c"]
>>> zip(a)
>>> zip(a,b)
>>> list(zip(a,b))
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> dict(zip(a,b))
{1: 'a', 2: 'b', 3: 'c'}
>>> str(zip(a,b))
''
>>> tuple(zip(a,b))
((1, 'a'), (2, 'b'), (3, 'c'))
>>>
>>> complex(3,5)
(3+5j)
>>> complex(3)
(3+0j)
>>> round(3.12123333333334444445555555555555555,18)
3.1212333333333446
>>> round(3.12123333333334444445555555555555555,2)
3.12
不可变数据类型才是可hash的,包含整数,字符串,元组,都是不可变的,是可hash的
>>> hash("12")
8731980002792086209
>>> hash("123")
-1620719444414375290
>>> hash([1,2])
Traceback (most recent call last):
File "", line 1, in
TypeError: unhashable type: 'list'
>>> hash(1)
1
>>> hash(123)
123
>>> hash((1,2))
3713081631934410656
>>> hash((1,2,3))
2528502973977326415
>>> hash({1,2,3})
Traceback (most recent call last):
File "", line 1, in
TypeError: unhashable type: 'set'
>>>
>>> set([1,2,3])
{1, 2, 3}
>>> set((1,2,3))
{1, 2, 3}
>>> set("21")
{'1', '2'}
>>> set(2)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not iterable
>>> set({1:2,3:4})
{1, 3}
>>>