Python 基础(变量、常量、注释)

 

Python 基础(变量、常量、注释)

一、变量

声明变量

1 name = "Dabby"

变量定义规则

    1. 变量名只能是 字母、数字或下划线的任意组合
    2. 变量名的第一个字符不能是数字
    3. 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

定义方式

  驼峰体(比较难读懂变量代表的意思)

1 AgeOfDabby = 56
2 NumberOfStudents = 80

  下划线(推荐,易读)

1 age_of_oldboy = 56 
2 number_of_students = 80

 

 

二、常量

常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量(定义常量时变量通常用全部大写表示)

1 AGE_OF_DABBY = 18
2 COLNUMS = ['id','name','age','phone','dept','enroll_date']

 

  

三、注释

代码注释分单行和多行注释, 单行注释用 #,多行注释可以用三对双引号 “”“ ”“”

1 def print_log(message,log_type):  # 处理输出语句的格式
2     if log_type == 'info':  # 打印一般信息
3         print('\033[33;1m%s\033[0m'%message)
4     if log_type == 'error':  # 打印警报信息
5         print('\033[31;1m%s\033[0m'%message)
 1 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
 2     """
 3     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
 4     
 5     Prints the values to a stream, or to sys.stdout by default.
 6     Optional keyword arguments:
 7     file:  a file-like object (stream); defaults to the current sys.stdout.
 8     sep:   string inserted between values, default a space.
 9     end:   string appended after the last value, default a newline.
10     flush: whether to forcibly flush the stream.
11     """
12     pass

代码注释原则:

    1. 不用全部加注释,只需要在自己觉得重要或不好理解的部分加注释即可
    2. 注释可以用中文或英文,但绝对不要拼音 
posted @ 2018-09-18 23:47  暴风里的蜗牛  阅读(128)  评论(0)    收藏  举报