目录
2 Python的所有对象均可用于布尔测试,且任何值为0的数字对象的布尔值是False
6 Python语言为解析执行或解释执行,与边编译和边执行不同
1 Python的内存管理
1 Python可以使用del释放资源:
list0 = [1, 2, 3]
del(list0[0])
print(list0)
2 变量不必事先声明
3 变量无需指定类型:Python为弱类型语言,如下变量a可以被修改为任意类型
a = 1
print(a, type(a))
a = 2.1
print(a, type(a))
a = [1, 2, 3]
print(a, type(a))
2 Python的所有对象均可用于布尔测试,且任何值为0的数字对象的布尔值是False
list1 = [0, 0]
print(bool({}))
print(bool(list1[1]))
3 Python字符串
1 Python字符串并不以\0作为结束
print("asdfsadf\0asdfas") #anaconda3 & pycharm打印结果: asdfsadfasdfas
2 即可使用单引号创建字符串,也可使用双引号创建字符串(主要是方便打印 ' 或者 " );三引号字符串可以包含换行回车等特殊符号;可使用转义符“ \ ”打印字符“ “ ”;可使用“ * ”对字符串进行复制;r 可以取消转义符生效
print('a"b') # 打印结果:a"b
print("a'b") # 打印结果:a'b
print("""a
b
""")
打印结果:
a
b
print("a\"b") # 打印结果:a"b
print("ab" + "c" * 2) #打印结果:abcc
print(r"hello\nworld") # r 取消\n生效。打印结果:hello\nworld
3 字符串的相关操作
#查找
a = "Hello"
a.index([目标字符串], [开始位置], [结束位置])#若不存在该字符串则报错,查找区间左闭右开
a.find([目标字符串], [开始位置], [结束位置])#若不存在则返回-1,查找区间左闭右开
print(a.index("llo", 1, len(a))) #打印结果:2
print(a.index("llo", 2, 4)) #报错:ValueError: substring not found
print(a.find("llo")) #打印结果:2
print(a.find("llo", 2, 4)) #打印结果:-1
#切片操作:
a[start : end : step] -> a[开始位置 : 结束位置 :步长]
开始位置不填时默认为0
结束位置不填时默认为len(a)
步长不填时默认为1
print(a[1 : ]) #打印结果:ello word
print(a[ : 3]) #区间左闭右开[0, 3),打印结果:Hel
print(a[ : : 2]) #打印结果:Hlowr
print(a[1 : 4 : 2]) #区间左闭右开[1, 4),打印结果el
4 格式化打印
{}为占位符 可将变量放入其中进行格式化打印
name = "Phil"
age = 23
print(f"Hello, {name}. You are {age}.") #打印结果:Hello, Phil. You are 23.
print(f"\"{name}\" \"{age}\"!") #py3.7以后,打印结果:"Phil" "23"!
4 字典
因字典的key要求不可变,可以使用元组(tuple)作为字典的key,但不能使用list作为元组的键
print({(1, 2, 3) : "a"}) #打印结果:{(1, 2, 3): 'a'}
print({[1, 2, 4] : "a"}) #报错:TypeError: unhashable type: 'list'
不可变类型:整型、浮点型、字符串、元组,在传入函数时只是值传递
5 Python语句
python 无三目运算符
x = 1
y = 2
min = x if x < y else y
print(min) #打印结果:1
max = x > y ? x : y #报错:SyntaxError: invalid syntax
6 Python语言为解析执行或解释执行,与边编译和边执行不同
解释:在运行过程中才会被翻译成目标CPU指令
编译:将高级语言翻译成机器码,生成可执行文件
详解:https://blog.csdn.net/Mr_Cat123/article/details/78697655
7 Python语言块的划分是以缩进完成的
8 函数返回值
def fun():
pass
print(fun()) #打印结果:None
9 Python 类
1 自定义类默认继承object系统类
Pycharm里面 Ctrl+鼠标左键 点击object查看:
class object:
""" The most base type """
def __delattr__(self, *args, **kwargs): # real signature unknown
""" Implement delattr(self, name). """
pass
def __dir__(self, *args, **kwargs): # real signature unknown
""" Default dir() implementation. """
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass
def __format__(self, *args, **kwargs): # real signature unknown
""" Default object formatter. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass
def __init_subclass__(self, *args, **kwargs): # real signature unknown
"""
This method is called when a class is subclassed.
The default implementation does nothing. It may be
overridden to extend subclasses.
"""
pass
def __init__(self): # known special case of object.__init__
""" Initialize self. See help(type(self)) for accurate signature. """
pass
def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass
@staticmethod # known case of __new__
def __new__(cls, *more): # known special case of object.__new__
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __reduce_ex__(self, *args, **kwargs): # real signature unknown
""" Helper for pickle. """
pass
def __reduce__(self, *args, **kwargs): # real signature unknown
""" Helper for pickle. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __setattr__(self, *args, **kwargs): # real signature unknown
""" Implement setattr(self, name, value). """
pass
def __sizeof__(self, *args, **kwargs): # real signature unknown
""" Size of object in memory, in bytes. """
pass
def __str__(self, *args, **kwargs): # real signature unknown
""" Return str(self). """
pass
@classmethod # known case
def __subclasshook__(cls, subclass): # known special case of object.__subclasshook__
"""
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__().
It should return True, False or NotImplemented. If it returns
NotImplemented, the normal algorithm is used. Otherwise, it
overrides the normal algorithm (and the outcome is cached).
"""
pass
__class__ = None # (!) forward: type, real value is "<class 'type'>"
__dict__ = {}
__doc__ = ''
__module__ = ''
2 类的实例方法必须创建对象后才可以调用
3 类的类方法可以使用对象和类名来调用
4 类的静态属性可以用类名和对象来调用
5 类中的方法第一个参数是指向当前对象的指针self,可以重命名self为任意标识符