python零基础学习笔记(九)-python数据类型三个小测验

python零基础学习笔记(九)-python数据类型三个小测验

1. python格式化输出

代码:

# 实战1-格式化输出个人信息
print('-----------------------------')
info = '''
    name = {0}
    age = {1}
    address = {2}
'''
print(info.format('Uae', 23, 'Xian'))
print('-----------------------------')

结果:
结果1

2. 列表增删改查

代码:

# 实战2-列表的增删改查
# 添加同学
students = ['zhangsan', 'lisi', 'wanger']
newstudent = 'mazi'
students.append(newstudent)
print('newstudents are: %s' % (students))
# 合并班级
students2 = ['a', 'b', 'c', 'd']
students2.extend(students)
print('newstudents are: %s' % (students2))
# 换班
movestudent = 'wanger'
students2.remove(movestudent)
print('new students are: %s' % (students2))
# 更新姓名
stuname = 'lisisi'
stuindex = students2.index('lisi')
students2[stuindex] = stuname
print('new students are: %s' %(students2))

结果:
结果2

3. 将json数据转换成字典

代码:

# 实战3-json<->dict
import json
# json<-dict
yourinfo = {'name': 'wangwei', 'age': 10000}
jsonyourinfo = json.dumps(yourinfo)
print(jsonyourinfo)
print(type(jsonyourinfo))  # str是json的一种格式
# json->dict
dictinfo = json.loads(jsonyourinfo)
print(dictinfo)
print(type(dictinfo))
# 取值
print(dictinfo['age'])
# 注意json格式
aaa = '{"name": "zhangsan"}'
print(json.loads(aaa))

结果:
结果3

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页