码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 牛客刷题——Python入门总结


    在这里插入图片描述

    🤵‍♂️ 个人主页: @北极的三哈 个人主页

    👨‍💻 作者简介:Python领域优质创作者。

    📒 系列专栏:《Python入门学习》《牛客题库-Python篇》

    🌐推荐《牛客网》——找工作神器|笔试题库|面试经验|实习经验内推,求职就业一站解决

    牛客在线编程 Python入门篇

      • 01 输入输出
        • NP1.Hello World!
        • NP2.多行输出
        • NP3.读入字符串
        • NP4.读入整数数字
        • NP5.格式化输出
        • NP6.牛牛的小数输出
      • 02 类型转换
        • NP8 为整数增加小数点
        • NP9 十六进制数字的大小
      • 03 字符串
        • NP10 牛牛最好的朋友们
        • NP11 单词的长度
        • NP12 格式化输出(二)
        • NP13 格式化输出(三)
        • NP14 不用循环语句的重复输出
        • NP15 截取用户名前10位
      • 04 列表
        • NP16 发送offer
        • NP17 生成列表
        • NP18 生成数字列表
        • NP19 列表的长度
        • NP20 增加派对名单(一)
        • NP21 增加派对名单(二)
        • NP22 删除简历
        • NP23 删除好友
        • NP24 淘汰排名最后的学生
        • NP25 有序的列表
        • NP26 牛牛的反转列表
        • NP27 朋友们的喜好
        • NP28 密码游戏
        • NP29 用列表实现栈
        • NP30 用列表实现队列
        • NP31 团队分组
      • 05运算符
        • NP32 牛牛的加减器
        • NP33 乘法与幂运算
        • NP34 除法与取模运算
        • NP35 朋友的年龄是否相等
        • NP36 谁的数字大
        • NP37 不低于与不超过
        • NP38 牛牛的逻辑运算
        • NP39 字符串之间的比较
        • NP40 俱乐部的成员
        • NP41 二进制位运算
        • NP42 公式计算器
      • 06 条件语句
        • NP43 判断布尔值
        • NP44 判断列表是否为空
        • NP45 禁止重复注册
        • NP46 菜品的价格
        • NP47 牛牛的绩点
        • NP48 验证登录名与密码
      • 07 循环语句 NP49-NP61
        • NP49 字符列表的长度
        • NP50 程序员节
        • NP51 列表的最大与最小
        • NP52 累加数与平均值
        • NP53 前10个偶数
        • NP54 被5整除的数字
        • NP55 2的次方数
        • NP56 列表解析
        • NP57 格式化清单
        • NP58 找到HR
        • NP59 提前结束的循环
        • NP60 跳过列表的某个元素
        • NP61 牛牛的矩阵相加
      • 08 元组 NP62-66
        • NP62 运动会双人项目
        • NP63 修改报名名单
        • NP64 输出前三同学的成绩
        • NP65 名单中出现过的人
        • NP66 增加元组的长度
      • 09 字典 NP67-75
        • NP67 遍历字典
        • NP68 毕业生就业调查
        • NP69 姓名与学号
        • NP70 首都
        • NP71 喜欢的颜色
        • NP72 生成字典
        • NP73 查字典
        • NP74 字典新增
        • NP75 使用字典计数
      • 010 内置函数 NP76 - NP93
        • NP76 列表的最值运算
        • NP77 朋友的年龄和
        • NP78 正数输出器
        • NP79 字母转数字
        • NP80 数字的十六进制
        • NP81 数字的二进制表示
        • NP82 数学幂运算
        • NP83 错误出现的次数
        • NP84 列表中第一次出现的位置
        • NP85 字符的类型比较
        • NP86 字符子串的查找
        • NP87 子串的数量
        • NP88 句子拆分
        • NP89 单词造句
        • NP90 修正错误的字母
        • NP91 小数位修正
        • NP92 公式计算器
        • NP93 创建集合
      • 011 面向对象 NP94 - NP100
        • NP94 函数求差
        • NP95 兔子的数量
        • NP96 球的表面积
        • NP97 班级管理
        • NP98 修改属性1
        • NP99 修改属性2
        • NP100 重载运算
      • 012 正则表达式 NP101 - NP103
        • NP101 正则查找网址
        • NP102 提取数字电话
        • NP103 截断电话号码
        • **`推 荐:牛客题霸-经典高频面试题库`**


    01 输入输出

    Python输入输出学习链接
    在这里插入图片描述

    NP1.Hello World!

    在线编程跳转链接

    str = "Hello World!"
    print(str)
    
    • 1
    • 2

    NP2.多行输出

    在线编程跳转链接

    str1 = 'Hello World!'
    str2 =  'Hello Nowcoder!' 
    print(str1)
    print(str2)
    
    • 1
    • 2
    • 3
    • 4

    NP3.读入字符串

    在线编程跳转链接

    str = input("")
    print(str)
    
    • 1
    • 2

    NP4.读入整数数字

    在线编程跳转链接

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

    NP5.格式化输出

    在线编程跳转链接

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

    NP6.牛牛的小数输出

    在线编程跳转链接

    x=float(input()) 
    print('%0.2f'%x)
    
    • 1
    • 2

    02 类型转换

    Python基本数据类型
    在这里插入图片描述### NP7 小数化整数
    在线编程跳转链接

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

    NP8 为整数增加小数点

    在线编程跳转链接

    a = float(int(input())) 
    print(f'{a:.1f}', type(a), sep='\n')
    
    • 1
    • 2

    NP9 十六进制数字的大小

    在线编程跳转链接

    num=input() 
    num=int(num,16) 
    print(num)
    
    • 1
    • 2
    • 3

    03 字符串

    Python基本数据类型字符串
    在这里插入图片描述

    NP10 牛牛最好的朋友们

    在线编程跳转链接

    a = input() 
    b = input() 
    print("".join([a,b]))
    
    • 1
    • 2
    • 3

    NP11 单词的长度

    在线编程跳转链接

    print(len(input())
    
    • 1

    NP12 格式化输出(二)

    在线编程跳转链接

    name = input()
    print(f'{name.lower()}')
    print('%s'%name.upper())
    print('{}'.format(name.title()))
    
    • 1
    • 2
    • 3
    • 4

    NP13 格式化输出(三)

    在线编程跳转链接

    name = input()
    print(name.strip())
    
    • 1
    • 2

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

    在线编程跳转链接

    print(input()*100)
    
    • 1

    NP15 截取用户名前10位

    在线编程跳转链接

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

    04 列表

    Python组合数据类型列表
    在这里插入图片描述
    在这里插入图片描述


    NP16 发送offer

    在线编程跳转链接

    offer_list = ['Allen', 'Tom']
    
    
    for i in range(len(offer_list)):
        print('{}, you have passed our interview and will soon become a member of our company.'.format(offer_list[i]))
         
         
    for str_i in offer_list:
        if str_i == 'Tom':
            print('Andy, welcome to join us!' )
        else:
            print('{}, welcome to join us!'.format(str_i) )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    NP17 生成列表

    在线编程跳转链接

    print(input().split(" "))
    
    • 1

    NP18 生成数字列表

    在线编程跳转链接

    print([int(i) for i in input().split(" ")])
    
    • 1

    NP19 列表的长度

    在线编程跳转链接

    print(len(input().split(" ")))
    
    • 1

    NP20 增加派对名单(一)

    在线编程跳转链接

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

    NP21 增加派对名单(二)

    在线编程跳转链接

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

    NP22 删除简历

    在线编程跳转链接

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

    NP23 删除好友

    在线编程跳转链接

    lst = input().split(" ")
    l = input()
    lst.remove(l)
    print(lst)
    
    • 1
    • 2
    • 3
    • 4

    NP24 淘汰排名最后的学生

    在线编程跳转链接

    name = input().split(" ")
    for i in range(3):
        name.pop(-1)  
    print(name)
    
    • 1
    • 2
    • 3
    • 4

    NP25 有序的列表

    在线编程跳转链接

    my_list = ['P','y','t','h','o','n']
    print(sorted(my_list))
    print(my_list)
    my_list.sort(reverse=True)
    print(my_list)
    
    • 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']
    friends = []
    friends.append(name)
    food = ['pizza', 'fish', 'potato', 'beef']
    friends.append(food)
    number = [3, 6, 0, 3]
    friends.append(number)
    print(friends)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP28 密码游戏

    在线编程跳转链接

    num = input()
    ls = []
    for i in num:
        ls.append((int(i)+3)%9)
    print(f"{ls[2]}{ls[3]}{ls[0]}{ls[1]}")
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP29 用列表实现栈

    在线编程跳转链接

    stack = [1, 2, 3, 4, 5]
    stack.pop(-1)
    print(stack)
    stack.pop(-1)
    print(stack)
    num = eval(input())
    stack.append(num)
                 
    print(stack)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP30 用列表实现队列

    在线编程跳转链接

    queue = [1, 2, 3, 4, 5]
    queue.pop(0)
    print(queue)
    queue.pop(0)
    print(queue)
    num = eval(input())
    queue.append(num)
    print(queue)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP31 团队分组

    在线编程跳转链接

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

    05运算符

    Python中的运算符
    在这里插入图片描述


    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 除法与取模运算

    在线编程跳转链接

    x = int(input())
    y = int(input())
     
    print(x//y, end=" ")
    print(x%y)
    print('{:.2f}'.format(x/y))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP35 朋友的年龄是否相等

    在线编程跳转链接

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

    NP36 谁的数字大

    在线编程跳转链接

    x, y = map(int, input().split(" "))
    print(x>y, x<y, sep="\n")
    
    • 1
    • 2

    NP37 不低于与不超过

    在线编程跳转链接

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

    NP38 牛牛的逻辑运算

    在线编程跳转链接

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

    NP39 字符串之间的比较

    在线编程跳转链接

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

    NP40 俱乐部的成员

    在线编程跳转链接

    s = input().split(" ")
    name = input()
    print(name in s)
    
    • 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 条件语句

    Python语言流程控制结构
    在这里插入图片描述


    NP43 判断布尔值

    在线编程跳转链接

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

    NP44 判断列表是否为空

    在线编程跳转链接

    my_list = []
    if len(my_list) == 0:
        print('my_list is empty!')
    else:
        print('my_list is not empty!')
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP45 禁止重复注册

    在线编程跳转链接

    #为了不区分大小写把判断的都转成大写
    current_users=[i.upper() for i in ['Niuniu','Niumei','GURR','LOLO']]
    
    #因为输出的字符串要保持原来的样子,这个列表内容不能改
    new_users=['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
    for i in new_users:
        # 通过upper()来调整大写后再判断
        if i.upper() in current_users:  
            print(f"The user name {i} has already been registered! Please change it and try again!")
        else:
            print(f"Congratulations, the user name {i} is available!")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    NP46 菜品的价格

    在线编程跳转链接

    s = input()
    if s == 'pizza':
        print(10)
    elif s == 'rice':
        print(2)
    elif s == 'yogurt':
        print(5)
    else:
        print(8)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP47 牛牛的绩点

    在线编程跳转链接

    d = {'A': 4.0, 'B': 3.0, 'C': 2.0, 'D': 1.0, 'F': 0}
    
    s = 0
    c = 0
    while True:
        grade = input()
        if grade == "False":
            break
        credit = int(input())
        c += credit
        s += d.get(grade)*credit
    
        
    print('{:.2f}'.format(s/c))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    NP48 验证登录名与密码

    在线编程跳转链接

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

    07 循环语句 NP49-NP61

    Python语言流程控制结构

    在这里插入图片描述

    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(f'Hi, {i}! Welcome to Nowcoder!')
    print("Happy Programmers' Day to everyone!")
    
    • 1
    • 2
    • 3
    • 4

    NP51 列表的最大与最小

    在线编程跳转链接

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

    NP52 累加数与平均值

    在线编程跳转链接

    ls = list(map(int, input().split(" ")))
    s = 0
    for i in ls:
        s += i
    print("{} {:.1f}".format(s, s/len(ls)))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP53 前10个偶数

    在线编程跳转链接

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

    NP54 被5整除的数字

    在线编程跳转链接

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

    NP55 2的次方数

    在线编程跳转链接

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

    NP56 列表解析

    在线编程跳转链接

    ls = [i for i in range(10)]
    print(ls)
    
    • 1
    • 2

    NP57 格式化清单

    在线编程跳转链接

    ls = ['apple', 'ice cream', 'watermelon', 'chips', 'hotdogs', 'hotpot']
    while True:
        ls.pop(-1)
        print(ls)
        if len(ls) == 0:
            break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP58 找到HR

    在线编程跳转链接

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

    NP59 提前结束的循环

    在线编程跳转链接

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

    NP60 跳过列表的某个元素

    在线编程跳转链接

    for i in range(1, 16):
        if i == 13:
            continue
        print(i, end=" ")
    
    • 1
    • 2
    • 3
    • 4

    NP61 牛牛的矩阵相加

    在线编程跳转链接

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

    08 元组 NP62-66

    元组学习链接:http://t.csdn.cn/RqgMt
    在这里插入图片描述

    NP62 运动会双人项目

    在线编程跳转链接

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

    NP63 修改报名名单

    在线编程跳转链接

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

    NP64 输出前三同学的成绩

    在线编程跳转链接

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

    NP65 名单中出现过的人

    在线编程跳转链接

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

    NP66 增加元组的长度

    在线编程跳转链接

    t = tuple(range(1, 6))
    print(t)
    print(len(t))
    
    t1 = t + tuple(range(6, 11))
    print(t1)
    print(len(t1))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    09 字典 NP67-75

    Python组合数据类型字典
    在这里插入图片描述

    NP67 遍历字典

    在线编程跳转链接

    # 创建一个字典 operators_dict
    operators_dict = {'<': 'less than','==': 'equal'}
     
    # 先打印一行
    print('Here is the original dict:')
    # 在使用 for 循环 遍历 使用 sorted 函数 排序 包含 operators_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循环
    for key, value in sorted(operators_dict.items()):
        print(f'Operator {key} means {value}.')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    NP68 毕业生就业调查

    在线编程跳转链接

    survey_list = ['Niumei', 'Niu Ke Le', 'GURR', 'LOLO']
    result_dict = {'Niumei': 'Nowcoder', 'GURR': 'HUAWEI'}
     
    for i in survey_list:
        if i in result_dict.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
    • 8

    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:
        key, value = i['name'], i['Student ID']
        print(f"{key}'s student id is {value}.")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    NP70 首都

    在线编程跳转链接

    cities_dict = {'Beijing': {'capital': 'China'},
                  'Moscow': {'capital': 'Russia'},
                  'Paris': {'capital': 'France'}}
     
    for city in sorted(cities_dict.keys()):
        city_value = cities_dict[city]
        for item in city_value:
            print(f'{city} is the {item} of {city_value[item]}!')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP71 喜欢的颜色

    在线编程跳转链接

    result_dict = {
        'Allen': ['red', 'blue', 'yellow'],
        'Tom': ['green', 'white', 'blue'],
        'Andy': ['black', 'pink']
    }
    for i in sorted(k for k in result_dict):##列表生成式生成key的列表
        print("%s's favorite colors are:" % i)
        for x in result_dict[i]:
            print(x)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP72 生成字典

    在线编程跳转链接

    a =input()
    b = input()
    names = a.split()
    language = b.split()
    dict_a = dict(zip(names,language))
    print(dict_a)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    NP73 查字典

    在线编程跳转链接

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

    NP74 字典新增

    在线编程跳转链接

    letter = input()
    word = input()
    d = {
        "a": ["apple", "abandon", "ant"],
        "b": ["banana", "bee", "become"],
        "c": ["cat", "come"],
        "d": "down",
        letter: word,
    }
    d[letter] = word
    print(d)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    NP75 使用字典计数

    在线编程跳转链接

    list1=list(input())
    dict1={}
    for i in list1:
        if i in dict1:
            dict1[i]+=1
        else:
            dict1[i]=1
    print(dict1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    010 内置函数 NP76 - NP93

    在这里插入图片描述
    在这里插入图片描述

    NP76 列表的最值运算

    在线编程跳转链接

    s = input().split(" ")
    ls = []
    for i in s:
        ls.append(int(i))
    print(max(ls), min(ls), sep = '\n')
    
    • 1
    • 2
    • 3
    • 4
    • 5

    NP77 朋友的年龄和

    在线编程跳转链接

    ls = [int(i) for i in input().split(" ")]
    print(sum(ls))
    
    • 1
    • 2

    NP78 正数输出器

    在线编程跳转链接

    n = eval(input())
    print(abs(n))
    
    • 1
    • 2

    NP79 字母转数字

    在线编程跳转链接

    print(ord(input()))
    
    • 1

    NP80 数字的十六进制

    在线编程跳转链接

    print(hex(int(input())))
    
    • 1

    NP81 数字的二进制表示

    在线编程跳转链接

    print(bin(int(input())))
    
    • 1

    NP82 数学幂运算

    在线编程跳转链接

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

    NP83 错误出现的次数

    在线编程跳转链接

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

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

    在线编程跳转链接

    s = list(input().split(" "))
    print(s.index('NiuNiu'))
    
    • 1
    • 2

    NP85 字符的类型比较

    在线编程跳转链接

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

    NP86 字符子串的查找

    在线编程跳转链接

    long_str = input()
    print(long_str.find('NiuNiu'))
    
    • 1
    • 2

    NP87 子串的数量

    在线编程跳转链接

    patten = input()
    print(patten.count('Niu'))
    
    • 1
    • 2

    NP88 句子拆分

    在线编程跳转链接

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

    NP89 单词造句

    在线编程跳转链接

    lst = []
    while True:
        w = input()
        if w == '0':
            break
        lst.append(w)
    print(' '.join(lst))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    NP90 修正错误的字母

    在线编程跳转链接

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

    NP91 小数位修正

    在线编程跳转链接

    f = float(input())
    print(round(f, 2))
    
    • 1
    • 2

    NP92 公式计算器

    在线编程跳转链接

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

    NP93 创建集合

    在线编程跳转链接

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

    011 面向对象 NP94 - NP100

    Python中的类和对象

    Python面向对象三大特征
    在这里插入图片描述

    NP94 函数求差

    在线编程跳转链接

    def cal(a, b):
        return a - b
    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
        if n==2:
            return 3
        else:
            return f(n-1)+f(n-2)
    n=int(input())
    print(f(n))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    NP96 球的表面积

    在线编程跳转链接

    import math as mt
     
    def vol(n):
        return 4*mt.pi*(n**2)
     
    list1=[1, 2, 4, 9, 10, 13]
    for n in list1:
        print(round(vol(n),2))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NP97 班级管理

    在线编程跳转链接

    class Student:
        def __init__(self, name, stu_num, score, grade):
            self.name = name
            self.stu_num = stu_num
            self.score = score
            self.grade = grade
        #__str__方法用于返回对象的描述信息,如果不使用__str__方法,直接print,或者return,返回的是对象的内存地址。
        def __str__(self): 
            return ("%s's student number is %s, and his grade is %d. He submitted %s assignments, each with a grade of %s"
                    % (self.name, self.stu_num, int(self.score), len(self.grade.split()), self.grade))
     
    name1 = input()
    stu_num1 = input()
    score1 = input()
    grade1 = input()
    stu = Student(name1, stu_num1, score1, grade1)
    print(stu)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    NP98 修改属性1

    在线编程跳转链接

    class employee:
        def __init__(self,name,salary,age=-1):
            self.name=name
            self.salary=salary
            self.age=age
        def printclass(self):
            if self.age==-1:
                print("Error! No age")
            else:
                print("{}'salary is {}, and his age is {}".format(self.name,self.salary,self.age))
    a=input()
    b=int(input())
    c=int(input())
    e=employee(a,b)
    e.printclass()
    e.age=c 
    e.printclass()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    NP99 修改属性2

    在线编程跳转链接

    class Employee():
        def __init__(self, name, salary) -> None:
            self.name = name
            self.salary = salary
        def printclass(self):
            print(f"{self.name}'salary is {self.salary}, and his age is {self.age}")
             
     
    name = input()
    salary = int(input())
    age = int(input())
     
    e = Employee(name, salary)
    if hasattr(e,'age'):
        e.printclass()
    else:
        print(False)
        setattr(e,"age",age)
        e.printclass()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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):
            self.x = x1 + x2
            self.y = y1 + y2
             
         
    x1, y1 = map(int, input().split()) # 1.输入第一行两个数字
    x2, y2 = map(int, input().split())  # 1.输入第二行两个数字
     
    c1 = Coordinate(x1, y1)  # 2. 调用类
    c1.__add__()  # 3. 调用__add__()函数,实现两组数据分别对应相加
    c1.__str__()  # 4. 调用__str__()函数,打印(相加之后的x, 相加之后的y)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    012 正则表达式 NP101 - NP103

    在这里插入图片描述

    NP101 正则查找网址

    在线编程跳转链接

    import re
    http = input()
    result = re.match('https://www',http,re.I)
    print(result.span())
    
    • 1
    • 2
    • 3
    • 4

    NP102 提取数字电话

    在线编程跳转链接

    import re
    #导入模块
     
    p=r'[-A-Za-z]+'
    #第一个‘-’表示字符‘-’,A-Za-z表示匹配大小字母
    #‘+’号表示连续匹配
     
    text=input()
    #输入测试字符串
     
    print(re.sub(p,'',text))
    #利用sub()函数过滤再输出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    NP103 截断电话号码

    在线编程跳转链接

    import re
    s = input()
    r = re.match('[0-9-]+',s)
    print(r.group())
    
    • 1
    • 2
    • 3
    • 4

    推 荐:牛客题霸-经典高频面试题库

    🌐 找工作神器-|笔试题库|面试经验|大厂面试题 👉 点击链接进行注册学习
    在这里插入图片描述

  • 相关阅读:
    ios上架上传构建版本的windows工具
    Linux | 网络服务管理相关不完全总结
    2021CCPC 哈尔滨(B D E I J)
    Spring FactoryBean 源码讲解
    python自动解析301、302重定向链接
    canvas 基础 和 动图案例
    计算机毕业设计JavaVUE商场库存管理系统(源码+系统+mysql数据库+lw文档)
    Java使用动态规划算法思想解决01背包问题
    一款GoFrame+Vue+ElementUI后台管理框架
    Java系列之:var关键字
  • 原文地址:https://blog.csdn.net/m0_68744965/article/details/127948375
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号