PythonClassChy/python27HomeWork/python/FloraHomeWork_0213.py

191 lines
7.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
=================================
Author: Flora Chen
Time: 2020/2/13 19:49
-_- -_- -_- -_- -_- -_- -_- -_-
=================================
"""
"""
1、 将字符串中的单词位置反转“hello xiao mi” 转换为 “mi xiao hello”
(提示:通过字符串分割,拼接,列表反序等知识点来实现)
"""
str1 = 'hello xiao mi'
str_list = str1.split(' ')
str_list.reverse()
# 如果是直接打印 这样就可以了
print('{} {} {}'.format(str_list[0], str_list[1], str_list[2]))
# 但是如果要变成一个新的变量,增加如下操作
new_str = ' '.join(str_list)
# new_str = ' '.join((str_list[0], str_list[1], str_list[2]))
print(new_str)
new_str2 = '{} {} {}'.format(str_list[0], str_list[1], str_list[2])
print(new_str2 )
"""
2、字典的增删查改操作 某比赛需要获取你的个人信息,编写一段代码要求如下:
1、运行时分别提醒输入 姓名、性别、年龄 ,输入完了,请将数据通过字典存储起来,
2、数据存储完了然后输出个人介绍格式如下: 我的名字XXX今年XXX岁性别XX喜欢敲代码
3、有一个人对你很感兴趣平台需要您补足您的身高和联系方式
4、平台为了保护你的隐私需要你删除你的联系方式
5、你为了取得更好的成绩 你添加了一项自己的擅长技能。
"""
# 1
name = input('请输入您的姓名:')
gender = input('请输入您的性别:')
age = input('请输入您的年龄:')
info = {'name': name, 'gender': gender, 'age': int(age)}
# 2
print('我的名字{},今年{}岁,性别{},喜欢敲代码'.format(info['name'], info['age'], info['gender']))
# 3
height = input('请输入您的身高:')
phone = input('请输入您的联系方式:')
info.update({'height': float(height), 'phone': phone})
print('我的名字{},今年{}岁,性别{},身高{:.2f},联系方式{},喜欢敲代码;'.format(info['name'], info['age'], info['gender'], info['height'], info['phone']))
# 4
info.pop('phone')
print('我的名字{},今年{}岁,性别{},身高{:.2f},喜欢敲代码;'.format(info['name'], info['age'], info['gender'], info['身高']))
# 5
skill = input('请输入您擅长的技能:')
info['skill'] = skill
print('我的名字{},今年{}岁,性别{},身高{:.2f},擅长{},喜欢敲代码;'.format(info['name'], info['age'], info['gender'], info['height'], info['skill']))
"""
3、利用下划线将列表li=[“python”,“java”,“php”]的元素拼接成一个字符串,然后将所有字母转换为大写,
"""
li = ['python', 'java', 'php']
# 方法一
str_li = li[0] + '_'+ li[1] + '_' + li[2]
# 方法二
str_li2 = '_'.join(li)
# str_li2 = '_'.join((li[0], li[1], li[2]))
print('方法一:', str_li.upper())
print('方法二:', str_li2.upper())
"""
4、利用切片把 'http://www.python.org'中的python字符串取出来
"""
url = 'http://www.python.org'
# 方法一
print(url[11:17])
# 方法二
print(url[-10:-4])
"""
5、编写一个买橘子的计算器
运行代码提示输入橘子的价格(要考虑小数的情况),
然后随机生成斤数1-100之间整数最后计算应付金额控制台输出如下信息(所有数据输出时都要保留两位小数)
输出内容格式“您购买的橘子为xx.xx斤每斤xx.xx元应支付金额为xx.xx”
"""
import random
orange_price = float(input('请输入橘子的价格:'))
weight = random.randint(1, 100)
total_price = orange_price * weight
print('您购买的橘子为{:.2f}斤,每斤{:.2f}元,应支付金额为{:.2f}'.format(weight, orange_price, total_price))
"""
=================================
Notes
Author: Flora Chen
-_- -_- -_- -_- -_- -_- -_- -_-
=================================
******字典dict******
1. 字典的定义
花括号{}表示字典,字典中的元素是由键值(key:value)对组成的,每个元素用逗号隔开。
字典是没有下标索引的其键key就是索引。
示例:
dic = {}
dict2 = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
print(type(dic)) # 输出结果:<class 'dict'>
print(dict2) # 输出结果:{'name': 'flora', 'age': 18, 'phone': '10220020200'}
print(dict2['name']) # 输出结果flora
2. 字典中的相关规范
字典中的键不能重复。
字典中的键只能使用不可变类型(字符串,数值类型,元组)的数据(通常是用字符串)。
字典中的值可以是任何数据类型。
扩展:
不可变类型的数据:数值类型,字符串,元组
可变类型的数据:列表,字典,集合
3. 字典的相关操作
添加一个元素
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
dic['height'] = 156
print(dic)
# 输出结果:{'name': 'flora', 'age': 18, 'phone': '10220020200', 'height': 156}
添加多个元素
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
dic.update({'height': 156, 'heavy': '56kg'})
print(dic)
# 输出结果:{'name': 'flora', 'age': 18, 'phone': '10220020200', 'height': 156, 'heavy': '56kg'}
修改元素:键已存在就是修改。否则是新增。
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
dic['phone'] = '18956423668'
print(dic)
# 输出结果:{'name': 'flora', 'age': 18, 'phone': '18956423668'}
删除元素
pop():通过键去删除指定的键值对,返回键对应的值
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
res = dic.pop('phone')
print(res) # 输出结果10220020200
print(dic)
# 输出结果:{'name': 'flora', 'age': 18}
popitem():删除最后添加进去的键值对,以元组的形式返回一个键值对
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
res = dic.popitem()
print(res) # 输出结果:('phone', '10220020200')
print(dic)
# 输出结果:{'name': 'flora', 'age': 18}
查找元素
通过键进行索引取值,键不存在会报错
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
res = dic['phone']
print(res) # 输出结果10220020200
get()通过键获取对应的值键不存在不会报错但是会返回None
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
res = dic.get('phone')
print(res) # 输出结果10220020200
res2 = dic.get('heg') #键不存在
print(res2) # 输出结果None
获取字典中的所有键,所有值,所有键值对
keys():获取字典中所有的键
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
print(dic.keys()) # 输出结果dict_keys(['name', 'age', 'phone'])
print(list(dic.keys())) # 可通过list()转换成列表
# 输出结果:['name', 'age', 'phone']
values():获取字典中所有的值
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
print(dic.values()) # 输出结果dict_values(['flora', 18, '10220020200'])
print(list(dic.values())) # 可通过list()转换成列表
# 输出结果:['flora', 18, '10220020200']
items():获取字典中所有的键值对
示例:
dic = {'name': 'flora', 'age': 18, 'phone': '10220020200'}
print(dic.items()) # 输出结果dict_items([('name', 'flora'), ('age', 18), ('phone', '10220020200')])
print(list(dic.items())) # 可通过list()转换成列表
# 输出结果:[('name', 'flora'), ('age', 18), ('phone', '10220020200')]
"""