0x1100
现在正式开始学习python
1.python的关键字:
关键字 | 含义 |
---|---|
and | 逻辑运算符。 |
as | 创建别名。 |
assert | 用于调试。 |
break | 跳出循环。 |
class | 定义类。 |
continue | 继续循环的下一个迭代。 |
def | 定义函数。 |
del | 删除对象。 |
elif | 在条件语句中使用,等同于 else if。 |
else | 用于条件语句。 |
except | 处理异常,发生异常时如何执行。 |
False | 布尔值,比较运算的结果。 |
finally | 处理异常,无论是否存在异常,都将执行一段代码。 |
for | 创建 for 循环。 |
from | 导入模块的特定部分。 |
global | 声明全局变量。 |
if | 写一个条件语句。 |
import | 导入模块。 |
in | 检查列表、元组等集合中是否存在某个值。 |
is | 测试两个变量是否相等。 |
lambda | 创建匿名函数。 |
None | 表示 null 值。 |
nonlocal | 声明非局部变量。 |
not | 逻辑运算符。 |
or | 逻辑运算符。 |
pass | null 语句,一条什么都不做的语句。 |
raise | 产生异常。 |
return | 退出函数并返回值。 |
True | 布尔值,比较运算的结果。 |
try | 编写 try…except 语句。 |
while | 创建 while 循环。 |
with | 用于简化异常处理。 |
注:变量取名不能为关键字。
2. python的内置函数:
- abs()
- delattr()
- hash()
- memoryview()
- set()
- all()
- dict()
- help()
- min()
- setattr()
- any()
- dir()
- hex()
- next()
- slice()
- ascii()
- divmod()
- id()
- object()
- sorted()
- bin()
- enumerate()
- input()
- oct()
- staticmethod()
- bool()
- eval()
- int()
- open()
- str()
- breakpoint()
- exec()
- isinstance()
- ord()
- sum()
- bytearray()
- filter()
- issubclass()
- pow()
- super()
- bytes()
- float()
- iter()
- print()
- tuple()
- callable()
- format()
- len()
- property()
- type()
- chr()
- frozenset()
- list()
- range()
- vars()
- classmethod()
- getattr()
- locals()
- repr()
- zip()
- compile()
- globals()
- map()
- reversed()
- __import__()
- complex()
- hasattr()
- max()
- round()
注:内置函数名可以做变量名,但内置函数会被覆盖。(极不推荐)
1.abs(x)
代表绝对值
abs(1)
Output[1]:1
abs(-5)
Output[2]:5
delattr(object, name)
删除一个对象的属性,等价于del object.name
class Dog:
a1 = 12
a2 = 13
a3 = 14
a = Dog()
print(a.a1)
print(a.a2)
print(a.a3)
+---------------------------------------------------------+
Output[1]:|12 |
|13 |
|14 |
____________________________________________________________________
delattr(a,a1)
print(a.a1)
print(a.a2)
print(a.a3)
----------------------------------------------------------
Output[2]:|12 |
|13 |
|Traceback (most recent call last): |
| File "<pyshell#23>", line 1, in <module> |
| a.a3 |
|AttributeError: 'Dog' object has no attribute 'a3' |
hash(object)
返回对象的哈希值,哈希值的寿命只在一个进程内生效。
print(hash('hello'))
---------------------------------------------------------
Output[1]:|2627932989303492551 |
_____________________________________________________________________
print(hash('hi') == hash('hi'))
--------------------------------------------------------
Output[2]:|True |
_____________________________________________________________________
print(hash('good'))
print(hash('Good'))
--------------------------------------------------------
Output[3]:|-7652449007438926521 |
|-2886785185124203416 |