常量即指不变的量,例如,pai=3.141592653..., 或在程序运行过程中不会改变的量。在Python中没有一个专门的语法代表常量,约定俗成用变量名全部大写代表常量。例如:
AGE_OF_OLDBOY = 56
在c语言中有专门的常量定义语法,const int count = 60; 一旦定义为常量,更改即会报错。
用户交互
name = input("What is your name:")
print("Hello " + name )
执行脚本后,发现程序会等输入姓名后才能往下继续走。
让用户输入多个信息:
name = input("What is your name:")
age = input("How old are you:")
hometown = input("Where is your hometown:")
print("Hello ",name , "your are ", age , "years old, you came from",hometown)
执行输出:
What is your name:Wu qianqian
How old are you:21
Where is your hometown:ShiJiaZhuang
Hello Wu qianqian your are 21 years old, you came from ShiJiaZhuang
注释代码注释分为单行和多行注释。 单行注释用#,多行注释可以用三对双引号""" """。
例:
def subclass_exception(name, parents, module, attached_to=None):
"""
Create exception subclass. Used by ModelBase below.
If ‘attached_to‘ is supplied, the exception will be created in a way that
allows it to be pickled, assuming the returned exception class will be added
as an attribute to the ‘attached_to‘ class.
"""
class_dict = {‘__module__‘: module}
if attached_to is not None:
def __reduce__(self):
# Exceptions are special - they‘ve got state that isn‘t
# in self.__dict__. We assume it is all in self.args.
return (unpickle_inner_exception, (attached_to, name), self.args)
def __setstate__(self, args):
self.args = args
class_dict[‘__reduce__‘] = __reduce__
class_dict[‘__setstate__‘] = __setstate__
return type(name, parents, class_dict)