Python基础语法重温第二天

###字典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))


在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值