python的赋值
python赋值直接用如下格式:
variable_name = value
python的选择
python的选择用if-(elif)-else,它的基本套路:
if statement:#the statement is True, it will operate
operations
elif statement:
operations
else:
operations
python的循环
python有两类循环,第一个是for循环,第二个是while循环。
for循环的基本套路:
for x in iterator:
operations
while循环的基本套路:
while statement: #the logical value of statement is True, then it will continue
operations
python的函数:
函数是一系列操作的封装,python函数的基本套路如下:
def function_name(parameters):
operations
python的类
类是面向对象编程的一个特征,类可以说是定义了一个抽象的人,身体和动作都封装在了类里面。python定义类的基本套路是这样的:
class class_name(object):
def __init__(self,parameters):
self.attribute=parameter
def function_name(self,parameter):
operations
python继承类的定义套路:
class class_name(dad_class_name):
def __init__(self,parameters):
dad_class_name.__init__(self,parameters) # parameters是父类的
def
以上是python程序的基本部件。一个python文件可以叫做一个python模块。一个文件夹可以叫做一个python包,但这个文件夹里除了有特定功能的python文件,还要有特殊的__init__.py文件。这个文件里一个__all__列表会规定包里的模块哪个可以用。
__all__ = [moduel_names]
python文件开头文件调用方式:
from library_name import moduel.name
python文件我们一般会用如下代码:
if __name__ = '__main__':
#这行代码的意思是:当模块名为__main__时程序执行(即作为脚本时执行,被作为模块导入时程序不执行if)
operations