input
input() 默认返回的是str类型
end seq
斐波纳契数列
a, b = 0, 1
while b < 1000:
print(b, end=',')
a, b = b, a+b
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
>>> a=10;b=388;c=98
>>> print(a,b,c,sep='@')
10@388@98
条件控制
while 1:
a = int(input('请输入您猜的数字'))
if a < 100 and 100 - a < 4:
print('接近啦 再大一点!')
elif a < 100:
print('差的有点远 需要大大大')
elif a > 100 and a - 100 < 4:
print('接近啦 需要小一点')
elif a > 100:
print('小一点啊 小小小!')
else:
print('厉害啦 答对啦!')
break
注意 与,或,非 对应 and,or,not
位运算对应:
&
|
^
~
<< >>
循环
for语句
for <variable> in <sequence>:
<statements>
else:
<statements>
range() 第三个参数为步长
for i in range(0, 10, 3) :
print(i)
break: 跳出循环
continue: 跳出循环继续执行
pass: 站位语句
乘法表
for i in range(1, 10):
for j in range(1, i + 1):
print(i, 'x', j, '=', i * j, end='\t')
print('\n')
注意range第二个参数是开区间的
迭代器
iter() 和 next()
yield(): 生成器是一个返回迭代器的函数 就是一个迭代器
在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回yield的值。并在下一次执行 next()方法时从当前位置继续运行
函数
默认参数必须放在最后面
不定长参数: 加*
def printinfo(name, *work):
print(name)
for i in work:
print(work)
return
printinfo('wang',(23,23,23,23))
还可以转化为字典
>>> def func(country,province,**kwargs):
... print(country,province,kwargs)
...
>>> func("China","Sichuan",city = "Chengdu", section = "JingJiang")
China Sichuan {'city': 'Chengdu', 'section': 'JingJiang'}
匿名函数
lambda
sum = lambda arg1, arg2: arg1 + arg2;
变量作用域
只有模块(module),类(class)以及函数(def、lambda)才会引入新的作用域
L (Local) 局部作用域
E (Enclosing) 闭包函数外的函数中
G (Global) 全局作用域
B (Built-in) 内建作用域
(越来越大 后面包含前面 L
将列表当做堆栈使用
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
将列表当作队列使用
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
列表推导式
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [[x, x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> 3*x for x in vec if x > 3]
[12, 18]
嵌套列表
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
//以下实例将3X4的矩阵列表转换为4X3列表:
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
遍历技巧
在字典中遍历时,关键字和对应的值可以使用 items()
在序列中遍历时,索引位置和对应值可以使用 enumerate()
同时遍历两个或更多的序列,可以使用 zip() 组合
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
有多个列表需要遍历时,需要zip,除了用’{0}{1}’.format(q,a)的方法,还可以使用%s方法(两者效果一样一样的):
questions=['name','quest','favorite color']
answers=['qinshihuang','the holy','blue']
for q,a in zip(questions,answers):
print('what is your %s? it is %s' %(q,a))
print('what is your {0}? it is {1}'.format(q,a))
另外:reversed() sorted