###字典dict
d = {'ISBN': '2567852', 'Title': '入门大法', 'Price': 50.00}
#添加键值对
d['Author'] = 'Marry' #1
dep = {'Date': '2019-1-1'}
d.update(dep) #2
#检索,如果没有返回None
d.get('price', None)
#dict构造函数声明字典
emp = dict(name='Make', age=20, job='dev')
print(emp)
#遍历键值
for k, v in d.items():
print('{key} --- {value}'.format(key=k, value=v))
#嵌套字典
emp = {'age': 20, 'name':{'first': 'Jee', 'last': 'Lee'}}
d = emp.get('name')
emp['name']['first']
#排序sorted()可迭代对象排序
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
ks = d.keys()
for k in sorted(ks):
print(k, d.get(k))
print(d)
###元组
t = (1, 2, 3, 4, 5)
res = [x**2 for x in t]
print(res)
###文件
#创建文件
myfile = open('hi.txt', 'w')
myfile.write('hello\n')
myfile.write('hello world!')
myfile.close()
l = open('hi.txt').readlines()
for line in l:
print(line)
#f.read()
print(l)
#pickle
import pickle
d = {'a': 1, 'b': 2}
f = open('data.pl', 'wb')
pickle.dump(d, f)
f.close()
f = open('data.pl', 'rb')
data = pickle.load(f)
print(data)
#临时打开后释放
with open('hi.txt') as f:
for line in f.readlines():
print(line)
#print函数
#打印结果保存
s = '我是谁'
# print(s,file=open('data.txt', 'w', encoding='utf8'))
##
def add(x):
print(x+10)
dict = {'add': add,
'update': lambda x: print(x*2),
'delete': lambda x: print(x*3)
}
def method(x):
print('默认')
dict.get('delte',method)(10)
##
score = 59
result = '及格' if score >= 60 else '不及格'
# print(result)
##while循环
x = 'studypython'
while x:
# print(x,end=' ')
x = x[1:]
#
a, b = 0, 10
while a < b:
print(a)
a += 1
#
x = 10
while x:
x -= 1
if x%2 != 0:
continue
print(x)
#
# while True:
# name = input('请输入姓名:')
# if name == 'stop':
# break
# age = input('请输入年龄:')
# print('你好:{name},你的年龄是:{age}'.format(name=name, age=age))
for i in range(1, 7):
if i == 6:
found = True
print('找到了', i)
break
else:
print('没找到')
#
# sum = 0
# for i in (1, 2, 3, 4, 5):
# sum += i
# print(sum)
#遍历字典
# emp = {'name': 'Tom',
# 'job': 'dev',
# 'age': 20}
# for key in emp:
# print('{}-->{}'.format(key, emp.get(key)))
#
# s1 = 'www.baidu.com'
# s2 = 'www.google.com'
#
# res = []
#
# for i in s1:
# if i in s2:
# res.append(i)
# print(res)
#
# l = [i for i in s1 if i in s2]
# print(l)
#返回索引及内容
s = 'www.baidu.com'
for index, item in enumerate(s):
print('{}) {}'.format(index, item))