关键词
1.关键词 -> 代码骨架
2.特点:拼写,大小写固定,不能用于变量名
3.基础词 : is, None,global,not,with,as,or,yield,assert,import,in
4.流程词 : True,False,try,finally,except,continue,for,if,elif,else,break,raise
5.函数词 : def,return,lambda,pass
6.对象词 : class
变量
内置数据类型:1.字符串 2.数字
固定注释
#!/usr/bin/env Python
# coding:utf-8
python 程序要素:内置数据类型,内置数据结构,内置函数,标准库,第三方库,自定义,逻辑空置语句,异常处理
代码规范
1.不要用中文命名
2.注释的多种样式 #独立一行或与代码同一行
3.变量命名,位置 驼峰命名方式 eg:all_memo/allMemo/AllMemo
4.函数命名 eg:def add():
5.类命名 eg:class Memo 尽量大写
6.项目工程目录规划 eg:尽量数字+字母链接
7.依赖包说明 eg:import datetime 导入
8.readme 自己包说明(自述文件)
9.不能用关键字
10.变量声明位置
11.TODO(相当于注释下一步)
1.关键字
dir 字符串列表 显示参数 dir()
dir (__builtin__) #显示内置参数
dir (__builtin__)
['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'BlockingIOError',
'BrokenPipeError',
'BufferError',
'BytesWarning',
'ChildProcessError',
'ConnectionAbortedError',
'ConnectionError',
'ConnectionRefusedError',
'ConnectionResetError',
'DeprecationWarning',
'EOFError',
'Ellipsis',
'EnvironmentError',
'Exception',
'False',
'FileExistsError',
'FileNotFoundError',
'FloatingPointError',
'FutureWarning',
'GeneratorExit',
'IOError',
'ImportError',
'ImportWarning',
'IndentationError',
'IndexError',
'InterruptedError',
'IsADirectoryError',
'KeyError',
'KeyboardInterrupt',
'LookupError',
'MemoryError',
'ModuleNotFoundError',
'NameError',
'None',
'NotADirectoryError',
'NotImplemented',
'NotImplementedError',
'OSError',
'OverflowError',
'PendingDeprecationWarning',
'PermissionError',
'ProcessLookupError',
'RecursionError',
'ReferenceError',
'ResourceWarning',
'RuntimeError',
'RuntimeWarning',
'StopAsyncIteration',
'StopIteration',
'SyntaxError',
'SyntaxWarning',
'SystemError',
'SystemExit',
'TabError',
'TimeoutError',
'True',
'TypeError',
'UnboundLocalError',
'UnicodeDecodeError',
'UnicodeEncodeError',
'UnicodeError',
'UnicodeTranslateError',
'UnicodeWarning',
'UserWarning',
'ValueError',
'Warning',
'WindowsError',
'ZeroDivisionError',
'__IPYTHON__',
'__build_class__',
'__debug__',
'__doc__',
'__import__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'abs',
'all',
'any',
'ascii',
'bin',
'bool',
'breakpoint',
'bytearray',
'bytes',
'callable',
'chr',
'classmethod',
'compile',
'complex',
'copyright',
'credits',
'delattr',
'dict',
'dir',
'display',
'divmod',
'enumerate',
'eval',
'exec',
'filter',
'float',
'format',
'frozenset',
'get_ipython',
'getattr',
'globals',
'hasattr',
'hash',
'help',
'hex',
'id',
'input',
'int',
'isinstance',
'issubclass',
'iter',
'len',
'license',
'list',
'locals',
'map',
'max',
'memoryview',
'min',
'next',
'object',
'oct',
'open',
'ord',
'pow',
'print',
'property',
'range',
'repr',
'reversed',
'round',
'set',
'setattr',
'slice',
'sorted',
'staticmethod',
'str',
'sum',
'super',
'tuple',
'type',
'vars',
'zip']
2.变量
(1) 保存数据供后面代码使用
(2) 代码结构普更清晰
(3) 修改代码更方便
# name = 值,左边是右边的引用
动态类型 强类型
a = 1
a
1
type(a)
int
a = '1'
a
'1'
type(a)
str
3.命名规则
(1) 大小写 (2)有意义单词 (3)不与关键词重复 (4)连接符(5)不能是中文
number = 3
num = 4
number
3
type(number)
int
num
4
type(num)
int
name = 'et'
name
'et'
type(name)
str
result = 1+5
result
6
4.数字
+加 -减 *乘 /除 // 整除 取整 %取余 数学计算顺序 (括号,乘除,加减)
int 整形 定点数
float 浮点型 浮点数 善变 一种懂二进制 一种不懂二进制 十进制与二进制会有精度的损失
3+5
8
3-5
-2
3*5
15
3/5
0.6
3 // 5
0
5 // 3
1
3 % 5
3
5 % 3
2
6.6 % 5
1.5999999999999996
type(True)
bool
type(False)
bool
5.内置类型 内置函数和标准库
导入 import random random随机
randint 随机整数 ratio 比例 比率
import random
random.randint(2, 7)
5
import random
ratio = 0.7
numbers1 = random.randint(1,10)
numbers2 = random.randint(1,10) * ratio
numbers3 = random.randint(1,10)
print(numbers1,int(numbers2),numbers3)
9 7 10
6.数学计算库 math (数学库)
内置函数 round方法
round 四舍五入 floor 取小 ceil 取大 pow 次方 hypot 直角三角形斜边计算 exp 指数函数 pi π
import math
round(3.7)
4
math.floor(3.7)
3
math.ceil(3.7)
4
math.pi
3.141592653589793
math.exp(2)
7.38905609893065
math.pow(2,8)
256.0
math.hypot(3,4)
5.0
math.hypot(3*3,4*4)
18.35755975068582
math.sin(math.pi/2)
1.0
math.log2(7)
2.807354922057604
7.字符串 str/string
name = input('name:')
name:et
type(name)
str
name = ('et')
name
'et'
type(name)
str
"""
et
1
2
3
4
5
6
7
"""
'\net\n1\n2\n3\n4\n5\n6\n7\n\n'
"1 \
2 \
3 \
"
'1 2 3 '
基础数据结构list
(1)list
(2)增删改查
[] 就叫一个列表
type([])
list
1.增
append 追加到文件末尾 insert 插入
my_list1=[2,4,6,8,10]
my_list1
[2, 4, 6, 8, 10]
my_list2 = list([2,4,6,8,10])
my_list2
[2, 4, 6, 8, 10]
my_list3 = list((2,4,6,8,10))
my_list3
[2, 4, 6, 8, 10]
列表尽量使用相同类型 嵌套列表 嵌套一般用结构化列表
more_list = ['222',['333'],'4,5,6']
more_list
['222', ['333'], '4,5,6']
more_list2 = [['2,3,4'],['5,6,7'],[8,9] ]
more_list2
[['2,3,4'], ['5,6,7'], [8, 9]]
str_list = []
str_list
[]
str_list.append('a')
str_list.append('b')
str_list
['a', 'b']
str_list1 = ['c*3']
str_list1
['c*3']
str_list2 = ['c'*3]
str_list2
['ccc']
str_list3 = [['c']*3]
str_list3
[['c', 'c', 'c']]
str_list4 = ['[c]'*3]
str_list4
['[c][c][c]']
my_list1
[2, 4, 6, 8, 10]
my_list1.insert(2,7)
my_list1
[2, 4, 7, 6, 8, 10]
my_list1.insert(0,1)
my_list1
[1, 2, 4, 7, 6, 8, 10]
2.查
切片 [] [开始:结束:步长]
长度内置方法 len ;count 数数
可用单数表示一个 ,复数表示多个
index 查询某个数值排在第几位
my_list1
[1, 2, 4, 7, 6, 8, 10]
my_list1[0]
1
my_list1[-1]
10
my_list1[:3]
[1, 2, 4]
my_list1[3:]
[7, 6, 8, 10]
len(my_list1)
7
my_list1.count(2)
1
my_list1
[1, 2, 4, 7, 6, 8, 10]
my_list1.append(2)
my_list1
[1, 2, 4, 7, 6, 8, 10, 2]
my_list1.count(2)
2
my_list1[:3]
[1, 2, 4]
my_list1.index(2)
1
my_list1.index(7)
3
3.改
reverse() 取反 (::-1) 取反
my_list1
[1, 2, 4, 7, 6, 8, 10, 2]
my_list1[2]=[5]
my_list1
[1, 2, [5], 7, 6, 8, 10, 2]
my_list1.reverse()
my_list1
[2, 10, 8, 6, 7, [5], 2, 1]
my_list1[::-1]
[1, 2, [5], 7, 6, 8, 10, 2]
4.删 pop 取出 抛出
my_list1
[2, 10, 8, 6, 7, [5], 2, 1]
my_list1.pop(0)
2
my_list1
[10, 8, 6, 7, [5], 2, 1]
my_list1.pop()
2
my_list1
[10, 8, 6, 7, [5]]
my_list1.append(2)
my_list1.append(3)
my_list1
[10, 8, 6, 7, [5], 2, 3]
my_list1.pop()
3
my_list1
[10, 8, 6, 7, [5], 2]
对象: 数据,值 属于属性 ; 动作,能力 属于方法