Python初学者笔记--第二天(基础练习题)

1. 设计程序,一次输入用户多个数据,分别以列表或字典的方式存储到变量
list1 = input("请输入一组数据,以逗号分割:").split(',')     #例如,输入: lisa,女,23"
dict1 = {"name":list1[0],"sex":list1[1],"age":list1[2]}    #值存入字典
print(dict1)                          #输出:{'name': 'lisa', 'sex': '女', 'age': '23'}
2. 把字符串中的最后一个“行”,替换为“**”
str1 = "三百六十行,行行出状元。行行有行行行规,行规行行行行,行规不行行行不行。行了行规才真行,真行行行也能行。说你行你就行不行也行,说你不行就不行行也不行。"
str2 = str1[::-1]
str3 = str2.replace("行","**",1)
str4 = str3[::-1]
print(str4)
3. 输入邮箱地址,类似jack1999@163.com,从中获取用户名,及邮箱服务器地址。效果如下:
  >> 请输入您的电子邮箱:> jack1999@163.com
  >> 用户名为:jack1999
  >> 邮箱服务器为:pop3.163.com 和 smtp.163.com
email = input("请输入您的电子邮箱:>")
mails = email.split("@")
print(mails)
print("用户名:",mails[0])
print("pop3服务器地址:","pop3."+mails[1])
print("smtp服务器地址:","smtp."+mails[1])
4. 创建一个变量用于存放3件商品的基本信息,商品属性包括(商品编号、商品名称、商品价格),之后完成对商品1的价格修改,以及商品2的删除操作,以及商品3的价格删除
pro1=['商品1',"华为荣耀",6000]
pro2=['商品2',"苹果平板",6700]
pro3=['商品3',"小米旗舰版",5000]
list1=[pro1,pro2,pro3]
list1[0][2]=800
list1.remove(pro2)
list1[1].pop(2)
print(list1)
5. 按照阶梯计税法,实现对个税的计算:
     5000内不扣税,5000-10000内扣税3%, 10000--30000内10%,30000以上部分%20
salary = int(input('请输入您的工资额度:'))
if salary < 5000:
    print("%f 小于5000,不用缴税")
elif salary <= 10000:
    st = (salary-5000)*0.03
elif salary <= 30000:
    st = (salary-10000)*0.1
    st += (10000-5000) * 0.03
else:
    st = (salary - 30000) * 0.2
    st += (20000-10000) * 0.1
    st += (10000-5000) * 0.03
print('应缴纳税额为',st)
print('扣税后工资为', salary - st)
6. 对用户通过一个input语句获取的一组数据,先进行去重,再进行去排序,然后打印输出
strs = input("请输入一组数据,以逗号分割:")   
list1 = strs.split(',')  
set1 = set(list1)  # 集合去重
list2 = list(set1)
list2.sort()       # 排序
print(list2) 
7. 编程实现计算任意月份的天数
year = int(input())
month = int(input())
set1 = [1,3,5,7,8,10,12]
set2 = [4,6,9,11]
result = ""
if year < 0 or month < 1 or month > 12:
    result = "非法输入"
elif month in set1:
    result = 31
elif month in set2:
    result = 30
elif month ==2 and (year%4==0 and year%100!=0 or year%400==0):
    result = 29
elif month ==2:
    result = 28
print(result)
8. nextDay判断
year = int(input())
month = int(input())
day = int(input())
set1 = [1, 3, 5, 7, 8, 10, 12]
set2 = [4, 6, 9, 11]
max_days = 0      # 获取当前月份对应的最大天数
if month in set1:
    max_days = 31
elif month in set2:
    max_days = 30
elif month == 2 and (year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
    max_days = 29
elif month == 2 and not (year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
    max_days = 28
else:
    print("month非法")
    exit(-1)
# 根据day及max_days计算下一天
if day > max_days:
    print("day非法")
    exit(-1)
elif day == max_days:
    month += 1
    day = 1
elif day < max_days:
    day += 1
# 对12月做特殊处理
if month == 13:
    year += 1
    month = 1
    day = 1
print(year, month, day)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值