• 【牛客编程题】python入门103题(输入&类型,字符串&列表&字典&元组,运算&条件&循环,函数&类&正则)


    【牛客编程题】python入门103题(输入&类型,字符串&列表&字典&元组,运算&条件&循环,函数&类&正则)
    做题链接:https://www.nowcoder.com/exam/oj?page=1&tab=Python%E7%AF%87&topicId=314

    文章目录

    01 输入输出

    NP1 Hello World!

    print("Hello World!")
    
    • 1

    NP2 多行输出

    print("Hello World!")
    print("Hello Nowcoder!")
    
    • 1
    • 2

    NP3 读入字符串

    s = input()
    print(s)
    
    • 1
    • 2

    NP4 读入整数数字

    a = int(input())
    print(a)
    print("")
    
    • 1
    • 2
    • 3

    NP5 格式化输出(一)

    s = input()
    print("I am %s and I am studying Python in Nowcoder!"%s)
    
    • 1
    • 2

    NP6 牛牛的小数输出

    x = float(input())
    print("%.2f"%x)
    
    • 1
    • 2

    02 类型转换

    NP7 小数化整数

    x = float(input())
    print(int(x))
    
    • 1
    • 2

    NP8 为整数增加小数点

    x = int(input())
    print("%.1f" % x)
    print("")
    
    • 1
    • 2
    • 3

    NP9 十六进制数字的大小

    x = int(input(), 16)
    print(x)
    
    • 1
    • 2

    03 字符串

    NP10 牛牛最好的朋友们

    a, b = input(), input()
    print(a+b)
    
    • 1
    • 2

    NP11 单词的长度

    a = input()
    print(len(a))
    
    • 1
    • 2

    NP12 格式化输出(二)

    str = input()
    print(str.lower())
    print(str.upper())
    print(str.title())
    
    • 1
    • 2
    • 3
    • 4

    NP13 格式化输出(三)

    s = input()
    print(s.strip(" \t"))
    
    • 1
    • 2

    NP14 不用循环语句的重复输出

    a = input()
    print(a*100)
    
    • 1
    • 2

    NP15 截取用户名前10位

    a = input()
    print(a[:10])
    
    • 1
    • 2

    04 列表

    NP16 发送offer

    offer_list=['Allen','Tom']
    for i in offer_list:
        print('%s, you have passed our interview and will soon become a member of our company.' %i)
    offer_list[1]='Andy'
    for i in offer_list:
        print('%s, welcome to join us!' %i )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP17 生成列表

    x = input().split(' ')
    print(x)
    
    • 1
    • 2

    NP18 生成数字列表

    a = input().split(' ')
    b = []
    for i in a:
        b.append(int(i))
    print(b)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP19 列表的长度

    x = input().split(' ')
    print(len(x))
    
    • 1
    • 2

    NP20 增加派对名单(一)

    x = input().split(' ')
    x.append('Allen')
    print(x)
    
    • 1
    • 2
    • 3

    NP21 增加派对名单(二)

    x = input().split(" ")
    x.insert(0,"Allen")
    print(x)
    
    • 1
    • 2
    • 3

    NP22 删除简历

    x = input().split(" ")
    x.pop(0)
    print(x)
    
    • 1
    • 2
    • 3

    NP23 删除好友

    x = input().split(" ")
    y = input()
    x.remove(y)
    print(x)
    
    • 1
    • 2
    • 3
    • 4

    NP24 淘汰排名最后的学生

    x = input().split()
    x.pop()
    x.pop()
    x.pop()
    print(x)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP25 有序的列表

    my_list=['P','y','t','h','o','n']
    print(sorted(my_list))
    print(my_list)
    print(sorted(my_list,reverse=True))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP26 牛牛的反转列表

    num=[3, 5, 9, 0, 1, 9, 0, 3]
    num.reverse()
    print(num)
    
    • 1
    • 2
    • 3

    NP27 朋友们的喜好

    name = ['Niumei','YOLO','Niu Ke Le','Mona']
    food = ['pizza','fish','potato','beef']
    number = [3,6,0,3]
    friends = []
    friends.append(name)
    friends.append(food)
    friends.append(number)
    print(friends)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP28 密码游戏

    a = input()
    li = []
    for i in a:
        x = (int(i)+3)%9
        li.append(x)
    li[0],li[2] = li[2],li[0]
    li[1],li[3] = li[3],li[1]
    for i in li:
        print(i,end="")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP29 用列表实现栈

    stack = [1, 2, 3, 4, 5]
    for i in range(0,2):
        stack.pop()
        print(stack)
    a = input()
    stack.append(int(a))
    print(stack)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP30 用列表实现队列

    queue = [1, 2, 3, 4, 5]
    for i in range(0,2):
        queue.pop(0)
        print(queue)
    a = int(input())
    queue.append(a)
    print(queue)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP31 团队分组

    group_list = ['Tom','Allen','Jane','William','Tony']
    print(group_list[0:2])
    print(group_list[1:4])
    print(group_list[-2:])
    
    • 1
    • 2
    • 3
    • 4

    05 运算符

    NP32 牛牛的加减器

    x = int(input())
    y = int(input())
    print(x+y)
    print(x-y)
    
    • 1
    • 2
    • 3
    • 4

    NP33 乘法与幂运算

    x = int(input())
    y = int(input())
    print(x*y)
    print(x**y)
    
    • 1
    • 2
    • 3
    • 4

    NP34 除法与取模运算

    a = int(input())
    b = int(input())
    print('{0} {1}'.format(a//b,a%b))
    print('{:.2f}'.format(float(a)/b))
    
    • 1
    • 2
    • 3
    • 4

    NP35 朋友的年龄是否相等

    a = input().split()
    a = list(map(int,a))
    print(a[0]==a[1])
    
    • 1
    • 2
    • 3

    NP36 谁的数字大

    a = input().split()
    a = list(map(int,a))
    print(a[0]>a[1])
    print(a[0]<a[1])
    
    • 1
    • 2
    • 3
    • 4

    NP37 不低于与不超过

    k, x, y = input().split(" ")
    print(k<=x)
    print(k>=y)
    
    • 1
    • 2
    • 3

    NP38 牛牛的逻辑运算

    x, y = map(int,input().split())
    print(x and y)
    print(x or y)
    print(not x)
    print(not y)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP39 字符串之间的比较

    s1 = input()
    s2 = input()
    print(s1 == s2)
    print(s1.lower() == s2.lower())
    
    • 1
    • 2
    • 3
    • 4

    NP40 俱乐部的成员

    a = input().split()
    b = input()
    print(b in a)
    
    • 1
    • 2
    • 3

    NP41 二进制位运算

    x, y = map(int, input().split(' '))
    print(x&y)
    print(x|y)
    
    • 1
    • 2
    • 3

    NP42 公式计算器

    x, y, z, k = map(int, input().split())
    print((x+y)*(z-k))
    
    • 1
    • 2

    06 条件语句

    NP43 判断布尔值

    x = int(input())
    if x==1:
        print("Hello World!")
    else:
        print("Erros!")
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP44 判断列表是否为空

    li = []
    if li:
        print("my_list is not empty!")
    else:
        print("my_list is empty!")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP45 禁止重复注册

    new_users = ['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
    current_user = ['Niuniu','Niumei','GURR','LOLO']
    current_user = [i.lower() for i in current_user]
    for i in new_users:
        if i.lower() in current_user:
            print('The user name {} has already been registered! Please change it and try again!'.format(i))
        else:
            print('Congratulations, the user name {} is available!'.format(i))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP46 菜品的价格

    food = {'pizza':10,'rice':2,'yogurt':5,'others':8}
    x = input()
    if x not in food.keys():
        x = 'others'
    for i in food.keys():
        if i==x:
            print(food[i])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP47 牛牛的绩点

    a = {'A':4.0, 'B':3.0, 'C':2.0, 'D':1.0, 'F':0}
    sum1 = 0
    sum2 = 0
    while True:
        x = input()
        if x.lower()=='false':
            break
        y = int(input())
        sum1 += a[x]*y
        sum2 += y
    print("%.2f"%(sum1/sum2))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    NP48 验证登录名与密码

    username = input()
    password = input()
    if username == 'admis':
        if password == 'Nowcoder666':
            print('Welcome!')
        else:
            print("user id or password is not correct!")
    else:
        print("user id or password is not correct!")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    07 循环语句

    NP49 字符列表的长度

    my_list = ['P', 'y', 't', 'h', 'o', 'n']
    print('Here is the original list:')
    print(my_list)
    print(' ')
    print('The number that my_list has is:')
    print(len(my_list))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP50 程序员节

    users_list=['Niuniu' ,'Niumei' , 'Niu Ke Le' ]
    for i in  users_list:
        print( 'Hi, %s! Welcome to Nowcoder!' %i)
    print("Happy Programmers' Day to everyone!")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP51 列表的最大与最小

    li = [ i for i in range(10,51) ]
    print(li)
    print(li[0],li[-1])
    
    
    • 1
    • 2
    • 3
    • 4

    NP52 累加数与平均值

    age = list(map(int,input().split(" ")))
    print(sum(age), round(sum(age)/len(age),1))
    
    • 1
    • 2

    NP53 前10个偶数

    li = [i for i in range(0,20,2)]
    for i in li:
        print(i)
    
    • 1
    • 2
    • 3

    NP54 被5整除的数字

    li = [i for i in range(1,51) if i%5 == 0]
    for i in li:
        print(i)
    
    • 1
    • 2
    • 3

    NP55 2的次方数

    li = []
    for i in range(1,11):
        li.append(2**i)
    for i in li:
        print(i)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP56 列表解析

    print([ i for i in range(0,10)])
    
    • 1

    NP57 格式化清单

    li = ['apple', 'ice cream', 'watermelon', 'chips', 'hotdogs', 'hotpot']
    while li:
        li.pop()
        print(li)
    
    • 1
    • 2
    • 3
    • 4

    NP58 找到HR

    users_list=['Niuniu','Niumei','HR','Niu Ke Le','GURR','LOLO']
    for i in users_list:
        if i=="HR":
            print('Hi, HR! Would you like to hire someone?')
        else:
            print('Hi, {}! Welcome to Nowcoder!'.format(i))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP59 提前结束的循环

    list_num = [3, 45, 9, 8, 12, 89, 103, 42, 54, 79]
    x = int(input())
    for item in list_num:
        if item == x:
            break
        print(item)
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP60 跳过列表的某个元素

    [print(i,end=" ") for i in range(1,16) if i!=13]
    
    • 1

    NP61 牛牛的矩阵相加

    n=int(input())
    a=[[1*n,2*n,3*n],[4*n,5*n,6*n],[7*n,8*n,9*n]]
    print(a)
    
    
    • 1
    • 2
    • 3
    • 4

    08 元组

    NP62 运动会双人项目

    tuple1 = (input(), input())
    print(tuple1)
    
    • 1
    • 2

    NP63 修改报名名单

    a=('Niuniu','Niumei')
    print(a)
    try:
        a[1]='Niukele'
    except:
        print('The entry form cannot be modified!')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP64 输出前三同学的成绩

    t = tuple(input().split(" "))
    print(t[:3])
    
    • 1
    • 2

    NP65 名单中出现过的人

    a = ('Tom', 'Tony', 'Allen', 'Cydin', 'Lucy', 'Anna')
    print(a)
    x = input()
    if x in a:
        print('Congratulations!')
    else:
        print('What a pity!')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP66 增加元组的长度

    a = tuple(range(1, 6))
    print(a)
    print(len(a))
    b = tuple(range(6,11))
    c = a+b
    print(c)
    print(len(c))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    09 字典

    NP67 遍历字典

    operators_dict={'<':'less than','==':'equal'}
    print('Here is the original dict:')
    for key,value in sorted(operators_dict.items()):
        print(f'Operator {key} means {value}.')
    
    operators_dict['>']='greater than'
    print()
    print('The dict was changed to:')
    for key,value in sorted(operators_dict.items()):
        print(f'Operator {key} means {value}.')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    NP68 毕业生就业调查

    name=['Niumei','Niu Ke Le','GURR','LOLO']
    result={'Niumei': 'Nowcoder','GURR': 'HUAWEI'}
    for i in name:
            if i in result.keys():
                print(f'Hi, {i}! Thank you for participating in our graduation survey!')    
            else:
                print(f'Hi, {i}! Could you take part in our graduation survey?')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP69 姓名与学号

    my_dict_1 = {'name': 'Niuniu', 'Student ID': 1}
    my_dict_2 = {'name': 'Niumei', 'Student ID': 2}
    my_dict_3 = {'name': 'Niu Ke Le', 'Student ID': 3}
    dict_list = []
    
    dict_list.append(my_dict_1)
    dict_list.append(my_dict_2)
    dict_list.append(my_dict_3)
    
    for i in dict_list:
         print(f"{i['name']}'s student id is {i['Student ID']}.")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    NP70 首都

    cities_dict={'Beijing': {"Capital": 'China'},
                 'Moscow': {"Capital": 'Russia'},
                 'Paris': {"Capital": 'France'}
                }
    for i in sorted(cities_dict.keys()):
        print('%s is the capital of %s!'%(i,cities_dict[i]['Capital']))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP71 喜欢的颜色

    result_dict={'Allen':['red','blue','yellow'],'Tom':['green','white','blue'],'Andy':['black','pink']}
    for key in sorted(result_dict.keys()):
        print(f"{key}'s favorite colors are:")
        for value in result_dict[key]:
            print(value)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP72 生成字典

    name = list(input().split())
    lang = list(input().split())
    dict1 = {name:lang for name,lang in zip(name,lang)}
    print(dict1)
    
    • 1
    • 2
    • 3
    • 4

    NP73 查字典

    dict1 = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
    a = input()
    for x in dict1[a]:
        print(x,end=' ')
    
    • 1
    • 2
    • 3
    • 4

    NP74 字典新增

    dic={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
    x = input()
    y = input()
    dic[x] = y
    print(dic)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP75 使用字典计数

    s = input()
    dict1 = dict()
    for ch in s:
        if ch in dict1:
            dict1[ch] += 1
        else :
            dict1[ch] = 1
    print(dict1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    10 内置函数

    NP76 列表的最值运算

    x = list(map(int, input().split()))
    print(max(x))
    print(min(x))
    
    
    • 1
    • 2
    • 3
    • 4

    NP77 朋友的年龄和

    x = list(map(int, input().split()))
    print(sum(x))
    
    • 1
    • 2

    NP78 正数输出器

    x = int(input())
    print(abs(x))
    
    • 1
    • 2

    NP79 字母转数字

    s = input()
    print(ord(s[0]))
    
    • 1
    • 2

    NP80 数字的十六进制

    x = int(input())
    print(hex(x))
    
    • 1
    • 2

    NP81 数字的二进制表示

    x = int(input())
    print(bin(x))
    
    • 1
    • 2

    NP82 数学幂运算

    x, y = map(int,input().split())
    print(pow(x,y))
    print(pow(y,x))
    
    • 1
    • 2
    • 3

    NP83 错误出现的次数

    x = list(map(int,input().split()))
    print(x.count(0))
    
    • 1
    • 2

    NP84 列表中第一次出现的位置

    x = input().split()
    print(x.index('NiuNiu'))
    
    • 1
    • 2

    NP85 字符的类型比较

    x = input()
    print(x.isalpha())
    print(x.isdigit())
    print(x.isspace())
    
    • 1
    • 2
    • 3
    • 4

    NP86 字符子串的查找

    s = input()
    print(s.find("NiuNiu"))
    
    • 1
    • 2

    NP87 子串的数量

    s = input()
    print(s.count("Niu"))
    
    • 1
    • 2

    NP88 句子拆分

    x = input().split(' ')
    print(x)
    
    • 1
    • 2

    NP89 单词造句

    x = []
    while 1:
        s = input()
        if s=="0": break
        else : x.append(s)
    print(" ".join(x))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP90 修正错误的字母

    x = input()
    print(x.replace('a*','ab'))
    
    • 1
    • 2

    NP91 小数位修正

    x = float(input())
    print(round(x,2))
    
    
    • 1
    • 2
    • 3

    NP92 公式计算器

    s = input()
    print(eval(s))
    
    • 1
    • 2

    NP93 创建集合

    s = set(input().split(' '))
    print(sorted(list(s)))
    
    • 1
    • 2

    11 面向对象

    NP94 函数求差

    def cal(x, y):
        return x-y
    x = int(input())
    y = int(input())
    print(cal(x,y))
    print(cal(y,x))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP95 兔子的数量

    def f(n):
        if n == 1: return 2
        elif n == 2 : return 3
        else : return f(n-1)+f(n-2)
    x = int(input())
    print(f(x))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP96 球的表面积

    import math
    for i in [1, 2, 4, 9, 10, 13]:
        print("%.2f"%(4*math.pi*i**2))
    
    • 1
    • 2
    • 3

    NP97 班级管理

    class Student:
        def __init__(self):
            self.name = input()
            self.Id = input()
            self.score =input()
            self.homework = input().split()
        def __str__(self):
                return (f"{self.name}'s student number is {self.Id}, and his grade is {self.score}. He submitted {len(self.homework)} assignments, each with a grade of {' '.join(self.homework)}")
    stu = Student()  
    print(stu)
    
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    NP98 修改属性1

    class Employee:
        def __init__(self,name,salary):
            self.n = name
            self.s = salary
        def printclass(self):
            try:
                print(f"{self.n}'salary is {self.s}, and his age is {self.age}")
            except:
                print("Error! No age")
    
    e = Employee(input(),input())
    e.printclass()
    e.age = input()
    e.printclass()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    NP99 修改属性2

    class Employee:
        def __init__(self,name,salary):
            self.name = name
            self.salary = salary
        def printclass(self):
            try:
                print("{}'salary is {}, and his age is {}".format(self.name,self.salary,self.age))
            except:
                print(hasattr(e,'age'))
                
    e = Employee(input(),input())
    e.printclass()
    e.age = input()
    e.printclass()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    NP100 重载运算

    class Coordinate:
        def __init__(self, x, y):
            self.x = x
            self.y = y 
            
        def str(self):
            print((self.x, self.y))
            
        def add(self):
            list1 = list(map(int,self.x.split()))
            list2 = list(map(int,self.y.split()))
            self.x = list1[0] + list2[0]
            self.y = list1[1] + list2[1]
    
    x = input()
    y = input()
    c1 = Coordinate(x,y)
    c1.add()
    c1.str()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    12 正则表达式

    NP101 正则查找网址

    import re
    print(re.match('https://www+',input()).span())
    
    • 1
    • 2

    NP102 提取数字电话

    import re
    print(re.sub('\D', '', input()))
    
    • 1
    • 2

    NP103 截断电话号码

    import re
    print(re.match("[0-9|-]+", input()).group() )
    
    • 1
    • 2
  • 相关阅读:
    LeetCode 热题 HOT 100 第七十九天 416. 分割等和子集 中等题 用python3求解
    DSPE-PEG-TH,TH-PEG-DSPE,磷脂-聚乙二醇-PH响应性细胞穿膜肽TH
    学C语言的第一节课
    Mysql 中如何导出数据?
    黑马JVM总结(二十六)
    Android12开发之窗口模糊功能的实现
    重写 hashcode()真有那么简单嘛?
    设计模式之美总结(重构篇)
    【AI】机器学习——线性模型(线性回归)
    (0 , _login.default) is not a function ES6,小程序浮点数精度问题
  • 原文地址:https://blog.csdn.net/qq_33957603/article/details/126310811