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

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了牛客编程题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!")

NP2 多行输出

print("Hello World!")
print("Hello Nowcoder!")

NP3 读入字符串

s = input()
print(s)

NP4 读入整数数字

a = int(input())
print(a)
print("<class 'int'>")

NP5 格式化输出(一)

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

NP6 牛牛的小数输出

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

02 类型转换

NP7 小数化整数

x = float(input())
print(int(x))

NP8 为整数增加小数点

x = int(input())
print("%.1f" % x)
print("<class 'float'>")

NP9 十六进制数字的大小

x = int(input(), 16)
print(x)

03 字符串

NP10 牛牛最好的朋友们

a, b = input(), input()
print(a+b)

NP11 单词的长度

a = input()
print(len(a))

NP12 格式化输出(二)

str = input()
print(str.lower())
print(str.upper())
print(str.title())

NP13 格式化输出(三)

s = input()
print(s.strip(" \\t"))

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

a = input()
print(a*100)

NP15 截取用户名前10位

a = input()
print(a[:10])

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 )

NP17 生成列表

x = input().split(' ')
print(x)

NP18 生成数字列表

a = input().split(' ')
b = []
for i in a:
    b.append(int(i))
print(b)

NP19 列表的长度

x = input().split(' ')
print(len(x))

NP20 增加派对名单(一)

x = input().split(' ')
x.append('Allen')
print(x)

NP21 增加派对名单(二)

x = input().split(" ")
x.insert(0,"Allen")
print(x)

NP22 删除简历

x = input().split(" ")
x.pop(0)
print(x)

NP23 删除好友

x = input().split(" ")
y = input()
x.remove(y)
print(x)

NP24 淘汰排名最后的学生

x = input().split()
x.pop()
x.pop()
x.pop()
print(x)

NP25 有序的列表

my_list=['P','y','t','h','o','n']
print(sorted(my_list))
print(my_list)
print(sorted(my_list,reverse=True))

NP26 牛牛的反转列表

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

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)

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="")

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)

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)

NP31 团队分组

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

05 运算符

NP32 牛牛的加减器

x = int(input())
y = int(input())
print(x+y)
print(x-y)

NP33 乘法与幂运算

x = int(input())
y = int(input())
print(x*y)
print(x**y)

NP34 除法与取模运算

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

NP35 朋友的年龄是否相等

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

NP36 谁的数字大

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

NP37 不低于与不超过

k, x, y = input().split(" ")
print(k<=x)
print(k>=y)

NP38 牛牛的逻辑运算

x, y = map(int,input().split())
print(x and y)
print(x or y)
print(not x)
print(not y)

NP39 字符串之间的比较

s1 = input()
s2 = input()
print(s1 == s2)
print(s1.lower() == s2.lower())

NP40 俱乐部的成员

a = input().split()
b = input()
print(b in a)

NP41 二进制位运算

x, y = map(int, input().split(' '))
print(x&y)
print(x|y)

NP42 公式计算器

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

06 条件语句

NP43 判断布尔值

x = int(input())
if x==1:
    print("Hello World!")
else:
    print("Erros!")

NP44 判断列表是否为空

li = []
if li:
    print("my_list is not empty!")
else:
    print("my_list is empty!")

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))

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])

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))

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!")

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))

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!")

NP51 列表的最大与最小

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

NP52 累加数与平均值

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

NP53 前10个偶数

li = [i for i in range(0,20,2以上是关于牛客编程题python入门103题(输入&类型,字符串&列表&字典&元组,运算&条件&循环,函数&类&正则)的主要内容,如果未能解决你的问题,请参考以下文章

牛客编程题零基础入门前端之73题(html,css,ES5,WebAPI)

牛客编程题python数据分析49题(pandas库的使用)

牛客刷题总结——Python入门01:输入输出字符串类型转换

牛客编程题GO语言入门46题

基础编程题之牛客网星际密码

牛客网上的java编程题,自己电脑运行没问题,但是在牛客网上运行失败,不知道原因在哪里