http://www.python.org/
python是一种弱化数据类型的脚本语言
>>> 17/3
>>> 5.666666666666667
>>> a=1111111111111111111133 * 222222222222222222
>>> a
246913580246913580004864197530864197526
>>> 1j+9
(9+1j)
>>> abs(1j+9)
9.055385138137417
word = 'Python'
print(word[:2] + word[3:])
>>> Pyhon
若给定超出范围的索引,会抛出异常
Traceback (most recent call last):
File "D:/app/pycharm/codes/exercise/JDcollage/1_数据结构/2_字符串.py", line 12, in
print(word[42])
IndexError: string index out of range
squares = [1, 2, 4, 9, 16, 25]
print(squares)
# 直接在原有链表后添加新链表
print(squares + [36, 49, 64, 81, 100])
# 调用系统的函数append添加链表
squares.append(121)
print(squares)
>>> 结果
[1, 2, 4, 9, 16, 25]
[1, 2, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 2, 4, 9, 16, 25, 121]
#可输出链表的值
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i,v)
>>> 结果
0 tic
1 tac
2 toe
# 混合遍历,作为栈使用
fruit = ['apple', 'pear', 'pinapple']
food = ['chicken', 'meal', 'rice']
for m, n in zip(fruit, food):
print('fruit?:food?'.format(m, n))
>>> 结果
fruit?:food?
fruit?:food?
fruit?:food?
# 若两边的长度不一样,取最小长度
由key和value组成
# 定义一个字典
tel = {'jack': 4098, 'sape': 4139}
# 添加一个字典键值对
tel['guido'] = 4128
# 输出字典
print(tel)
>>> 结果
{'jack': 4098, 'sape': 4139, 'guido': 4128}
# 删除字典键值对
del tel['sape']
print(tel)
>>> 结果
{'jack': 4098, 'guido': 4128}
# 添加的元素默认最后面
tel['zkk'] = 4098
print(tel)
>>> 结果
{'jack': 4098, 'guido': 4128, 'zkk': 4098}
# 显示key,value值
print(list(tel.keys()))
print(list(tel.values()))
>>> 结果
['jack', 'guido', 'zkk']
[4098, 4128, 4098]
# 三联表达式
print({x: x ** 2 for x in (2, 4, 6)})
>>> 结果
{2: 4, 4: 16, 6: 36}
# 遍历字典key,value
apple = {'1': 'apple1', '2': 'apple2', '3': 'apple3'}
for key, value in apple.items():
print(key, value)
>>> 结果
1 apple1
2 apple2
3 apple3
和链表相比占用内存少
# 定义一个元祖
t = 12345, 54321, 'hello!'
print(t[0], t[2])
>>> 结果
12345 hello!
# 输出元祖
print(t)
>>> 结果
(12345, 54321, 'hello!')
# 合并元祖
u = t, (1, 2, 3, 4, 5)
print(u)
>>> 结果
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
# 不能对元祖元素进行更改
t[0] = 8888
print(t)
>>> 结果
t[0] = 8888
TypeError: 'tuple' object does not support item assignment
和别的编程语言基本上没啥区别
定义函数
def 函数名:
def scope_test():
def do_local():
spam = 'local spam'
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = 'global spam'
spam = "test spam"
do_local()
print("nmeafter local assignt:", spam)
do_nonlocal()
print("after nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)
scope_test()
print("in global scope:", spam)
结果为:
nmeafter local assignt: test spam
after nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
in global scope: global spam
图片:D:\app\有道云笔记\pic\类和继承.png
PEP8规范
增强代码的可读性
- 一致性建议:坚持就要一直坚持下去,不能中途更换风格
- 代码的布局:
- 缩进:四个空格为代码段,行最大长度建议80字符,换行时可以用斜线进行换行
- 制表符or空格
- 行的最大长度
- 类名:首字母大写单词串(CapWords)的约定
- 全局变量名:一般全部大写字母命名
- 函数名:函数名都为小写,可用下划线风格单词增加可读性
- 永远都不要用字符I,1,l,i,O,0,o,
- 对空值的判断应该永远用:is或者is not来做,当你的本意是if x is not None时,写成if x要小心。例如当你测试一个默认为None的变量或参数是否被设置为其他值时,这个其他值可能是一个在布尔上下文中为假的值
- 对象类型的比较应该始终用isinstance()代替直接比较类型。例如:
yes:if isinstance(obj, int):
no: if type[obj] is type(1):