首页 > 编程语言 >Python学习

Python学习

时间:2024-07-06 21:30:50浏览次数:8  
标签:Python python 学习 int split print input jmu

目录

7-1 jmu-python-判断闰年

7-2 jmu-python-素数

7-3 jmu-python-找字符

7-4 计算表达式(*,//, %)

7-5 客户评级

7-6 运输打折问题

7-7 水仙花数

7-8 生成输入数的乘方表

7-9 输出字母在字符串中位置索引

7-10 通过两个列表构建字典

7-11 jmu-python-重复元素判定

7-12 求集合的最大值和最小值

7-13 字符替换

7-14 合并成绩

7-15 zust-sy7-11输出生肖和星座

7-16 录取排名


7-1 jmu-python-判断闰年

year = int(input())
if (year%4==0 and year%100!=0) or (year%400 ==0) :
    print(f'{year}是闰年')
else :
    print(f'{year}不是闰年')

7-2 jmu-python-素数

num=int(input())
if num<=1:
    print("{} is not prime".format(num))
else:
    for i in range(2,num):
        if num%i == 0:
            print("{} is not prime".format(num))
            break
    else:
        print("{} is prime".format(num))

7-3 jmu-python-找字符

n=input()
i=input()
a=n.find(i)+1
if a==0:
    print(f"can't find letter {i}")
else :
    print(f'index={a}')

7-4 计算表达式(*,//, %)

s = input()
s = s.replace(' ','')
try:
    print(s,eval(s),sep='=')
except:
    print('Invalid operator')

7-5 客户评级

xiaofei = int(input())
xiaofei = xiaofei/12
if xiaofei < 2000: 
    b = 1
elif xiaofei<3000:
    b = 2
elif xiaofei<4000:
    b = 3
elif xiaofei<5000:
    b = 4
else:
    b = 5
for i in range(0,b):    
    print("*",end="")   


7-6 运输打折问题

a,b=input().split(" ")
a,b=float(a),float(b)
if a>0:
    if 0<= b <250 :
        print(round(a*b*1.0))
    elif 250<= b <500:
        print(round(a*b*0.98))
    elif 500<= b <1000:
        print(round(a*b*0.95))
    elif 1000<= b <2000:
        print(round(a*b*0.92))
    elif 2000<= b < 3000:
        print(round(a*b*0.90))
    elif 3000<= b:
        print(round(a*b*0.85))
    else:
        print(0)
else:
    print(0)
        

7-7 水仙花数

n = int(input())
for num in range(10**(n-1),10**n):
    i = num
    sum = 0
    while (i > 0):
        a = i % 10
        sum += a ** n
        i = i // 10
    if sum == num:
        print(sum)

7-8 生成输入数的乘方表

a ,n = input().split()
n = int(n)
a = float(a)
for i in range(n + 1):
    print("{0:.1f}**{1:d}={2:.2f}".format( a,i, pow(a,i)))

7-9 输出字母在字符串中位置索引

str = input()
a, b = input().split()
i = len(str) - 1
str = str[::-1] 
for ch in str:
    if ch == a or ch == b:
        print(i, ch)
    i -= 1

7-10 通过两个列表构建字典

a=input().split(" ")
b=input().split(" ")
dic={}
for i in range(0,len(a)):
    dic[a[i]] = b[i]
a.sort()
result=[]
for i in range(0,len(a)):
    result.append((a[i],dic[a[i]],))
print(result)


7-11 jmu-python-重复元素判定

n=int(input())
true=false=0
for i in range(n):
    list=input()
    a=[]
    a=list.split()
    if len(a) == len(set(a)):
        false += 1
    else:
        true +=1
print("True={}, False={}".format(true,false))

7-12 求集合的最大值和最小值

dic=[-32,3,-55,234,21]
print('{',end = '')
print(', '.join(str(i)for i in dic),end='')
print('}')
print(max(dic))
print(min(dic))

7-13 字符替换

s = input()
result = ""
for i in s:
    if i.isupper():
        i = chr(ord('A') + ord('Z') - ord(i))
        result += i
    else:
        result += i
print(result)

7-14 合并成绩

a = tuple(map(int,input().split(',')))
b = tuple(map(int,input().split(',')))

alls = a+b
str1 = '  ' + '  '.join(f'{s:2}' for s in alls)
print(str1)

7-15 zust-sy7-11输出生肖和星座

def get_zodiac_animal(year):
    # 生肖循环周期为12年,以2000年为龙年开始计算
    animals = ['龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔']
    return animals[(year - 2000) % 12]

def get_constellation(month, day):
    # 星座依据月份和日期判断
    constellations = {
        (1, 20): '水瓶座', (2, 19): '双鱼座', (3, 21): '白羊座',
        (4, 20): '金牛座', (5, 21): '双子座', (6, 22): '巨蟹座',
        (7, 23): '狮子座', (8, 23): '处女座', (9, 23): '天秤座',
        (10, 23): '天蝎座', (11, 22): '射手座', (12, 22): '魔羯座'
    }
    for (m, d), name in constellations.items():
        if month > m or (month == m and day >= d):
            return name
    return '未知星座'

def main():
    birth_date = input()
    year, month, day = int(birth_date[:4]), int(birth_date[4:6]), int(birth_date[6:])
    
    zodiac_animal = get_zodiac_animal(year)
    constellation = get_constellation(month, day)
    
    print(f"您的生肖是:{zodiac_animal}")
    print(f"您的星座是:{constellation}")

if __name__ == "__main__":
    main()

7-16 录取排名

a={}
b=[]
while True:
    name=input()
    if name =='quit':
        break
    score = float(input())
    a[name]=score
items=a.items()
for j in items:
    b.append([j[1],j[0]])
b.sort()
count=len(b)-1
for i in range(1,len(b)+1):
    print(f"第{i}名:{b[count][1]},成绩为{b[count][0]}分")
    count = count-1

标签:Python,python,学习,int,split,print,input,jmu
From: https://blog.csdn.net/qq_43300604/article/details/140233189

相关文章

  • python练习01
    练习if语句age=int(input('请输入年龄'))ifage<10:print('还是个小屁孩')elif10<age<20:print('青春期叛逆的小孩')elif30<age<40:print('老大不小了,赶紧结婚,小屁孩')elif40<age<50:print('家里有个不听话的小屁孩')elif50<......
  • python数据容器(二)元组
    1.数据容器:tuple(元组)(1)定义t1=(1,"Hello",True)t2=()t3=tuple()print(f"t1的类型是:{type(t1)},内容是:{t1}")print(f"t2的类型是:{type(t2)},内容是:{t2}")print(f"t3的类型是:{type(t3)},内容是:{t3}")运行结果:(2)定义单个元素的元素t1=("hel......
  • opencv环境搭建-python
    最近遇到了一些图像处理的需求,所以需要学习一下opencv,来记录一下我的学习历程。安装numpypipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplenumpy安装matplotlibpipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplematplotlib安装opencvpipin......
  • 第一次学习Java的碎碎念
    2024年夏新的学习开始了;今天做了什么?在B站上收藏了黑马程序员学习Java的教学视频,观看了几篇入门教程,暂时学会了如何打开CMD,以及几个常见的CMD命令,例如盘符名称:、dir、cd目录、cd..、cls、exit等等,做了一个练习(利用cmd打开qq),学会了如何把应用程序的路径记录在电脑的环境变量中,使......
  • python数据容器(一)列表list
    思维导图代码1.数据容器入门2.数据容器:list(列表)name_list=['itheima','itcast','python']print(name_list)print(type(name_list))运行结果: name_list=['itheima',666,True]print(name_list)print(type(name_list))运行结果: name_l......
  • Python基础语法
    #1.注释-代码中不参与编译执行的部分就是注释。(不会被翻译成机器码,它的存在不会影响程序的功能)#1)注释的作用#a.对代码进行注解和说明,提高代码的可读性(对人)#b.取消代码的功能##2)添加注释的方法#a.单行注释-在一行注释内容前加#(添加和取消单行注......
  • python和pycharm安装
    一、python和pycharm的作用Python是一种跨平台的计算机程序语言。Python是我们进行项目开发而使用的一门计算机语言,通俗来说就是编写代码,编写完代码之后,我们就需要运行,不然代码是死的,机器是无法识别的,这时我们需要运行Python代码的运行环境和工具。PyCharm带有一整套......
  • python: list
     #去重A=['geovindu','刘杰','江山','河水','刘杰','geovindu','张三','李五']B=[]foriinA:ifinotinB:B.append(i)print(B)C=set(A)......
  • C#学习笔记-事件
    事件  事件是类的一种成员,能够使类或对象具备通知能力。事件用于对象或类间的动作协调和信息传递。假设类A有某个事件,当这个事件发生时,类A会通知并传递事件参数(可选)给有订阅这个事件的类B,类B根据拿到的事件信息对事件进行响应处理。事件模型事件模型的5个组成部分:1、事......
  • 昇思25天学习打卡营第11天|ResNet50图像分类
    文章目录昇思MindSpore应用实践基于MindSpore的ResNet50图像分类1、ResNet50简介2、数据集预处理及可视化3、构建网络构建BuildingBlock构建BottleneckBlock构建ResNet50网络4、模型训练5、图像分类模型推理Reference昇思MindSpore应用实践本系列文章主......