- 变量不需事先声明就可使用。
- z已赋值,
x=(y=z+1)
语句是错误语句
momthdays=dict(Jan=31,Feb=28,Mar=31,Apr=30)
这样定义是正确的
下面程序的运行结果是:
b=[1,2,3] b[2]=b s = 0 for row in b: if type(row)==list: for elem in row: s += elem else: s+=row print(s)
错误:
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
--3*2+5
的值是1
!
计算斐波那契数:
def fastFib(n,memo={}): if n==0 or n==1: return 1 try: return memo[n] except KeyError: result = fastFib(n-1,memo) + fastFib(n-2,memo) memo[n] = result #存进字典 return result print(fastFib(20))
将列表中的每一个数都转成
str
:s=[1,2,3] s=map(str,s) print(list(s))
找完数:
import math flag = 0 m,n=map(int,input().split()) for i in range (m,n+1): s = [1] for k in range (2,int(math.sqrt(i)+1)): if i % k == 0: s.append(k) s.append(int(i/k)) if sum(s) == i: flag = 1 s.sort() s = map(str, s) print('{:d} = {}'.format(i,' + '.join(s))) if(flag == 0): print("None")
字典键值互换:
reversedic={v:k for k,v in dic.items()}
d1=dict(zip(d.values(),d.keys()))