【牛客编程题】python入门103题(输入&类型,字符串&列表&字典&元组,运算&条件&循环,函数&类&正则)
做题链接:https://www.nowcoder.com/exam/oj?page=1&tab=Python%E7%AF%87&topicId=314
print("Hello World!")
print("Hello World!")
print("Hello Nowcoder!")
s = input()
print(s)
a = int(input())
print(a)
print("" )
s = input()
print("I am %s and I am studying Python in Nowcoder!"%s)
x = float(input())
print("%.2f"%x)
x = float(input())
print(int(x))
x = int(input())
print("%.1f" % x)
print("" )
x = int(input(), 16)
print(x)
a, b = input(), input()
print(a+b)
a = input()
print(len(a))
str = input()
print(str.lower())
print(str.upper())
print(str.title())
s = input()
print(s.strip(" \t"))
a = input()
print(a*100)
a = input()
print(a[:10])
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 )
x = input().split(' ')
print(x)
a = input().split(' ')
b = []
for i in a:
b.append(int(i))
print(b)
x = input().split(' ')
print(len(x))
x = input().split(' ')
x.append('Allen')
print(x)
x = input().split(" ")
x.insert(0,"Allen")
print(x)
x = input().split(" ")
x.pop(0)
print(x)
x = input().split(" ")
y = input()
x.remove(y)
print(x)
x = input().split()
x.pop()
x.pop()
x.pop()
print(x)
my_list=['P','y','t','h','o','n']
print(sorted(my_list))
print(my_list)
print(sorted(my_list,reverse=True))
num=[3, 5, 9, 0, 1, 9, 0, 3]
num.reverse()
print(num)
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)
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="")
stack = [1, 2, 3, 4, 5]
for i in range(0,2):
stack.pop()
print(stack)
a = input()
stack.append(int(a))
print(stack)
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)
group_list = ['Tom','Allen','Jane','William','Tony']
print(group_list[0:2])
print(group_list[1:4])
print(group_list[-2:])
x = int(input())
y = int(input())
print(x+y)
print(x-y)
x = int(input())
y = int(input())
print(x*y)
print(x**y)
a = int(input())
b = int(input())
print('{0} {1}'.format(a//b,a%b))
print('{:.2f}'.format(float(a)/b))
a = input().split()
a = list(map(int,a))
print(a[0]==a[1])
a = input().split()
a = list(map(int,a))
print(a[0]>a[1])
print(a[0]<a[1])
k, x, y = input().split(" ")
print(k<=x)
print(k>=y)
x, y = map(int,input().split())
print(x and y)
print(x or y)
print(not x)
print(not y)
s1 = input()
s2 = input()
print(s1 == s2)
print(s1.lower() == s2.lower())
a = input().split()
b = input()
print(b in a)
x, y = map(int, input().split(' '))
print(x&y)
print(x|y)
x, y, z, k = map(int, input().split())
print((x+y)*(z-k))
x = int(input())
if x==1:
print("Hello World!")
else:
print("Erros!")
li = []
if li:
print("my_list is not empty!")
else:
print("my_list is empty!")
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))
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])
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))
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!")
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))
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!")
li = [ i for i in range(10,51) ]
print(li)
print(li[0],li[-1])
age = list(map(int,input().split(" ")))
print(sum(age), round(sum(age)/len(age),1))
li = [i for i in range(0,20,2)]
for i in li:
print(i)
li = [i for i in range(1,51) if i%5 == 0]
for i in li:
print(i)
li = []
for i in range(1,11):
li.append(2**i)
for i in li:
print(i)
print([ i for i in range(0,10)])
li = ['apple', 'ice cream', 'watermelon', 'chips', 'hotdogs', 'hotpot']
while li:
li.pop()
print(li)
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))
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)
[print(i,end=" ") for i in range(1,16) if i!=13]
n=int(input())
a=[[1*n,2*n,3*n],[4*n,5*n,6*n],[7*n,8*n,9*n]]
print(a)
tuple1 = (input(), input())
print(tuple1)
a=('Niuniu','Niumei')
print(a)
try:
a[1]='Niukele'
except:
print('The entry form cannot be modified!')
t = tuple(input().split(" "))
print(t[:3])
a = ('Tom', 'Tony', 'Allen', 'Cydin', 'Lucy', 'Anna')
print(a)
x = input()
if x in a:
print('Congratulations!')
else:
print('What a pity!')
a = tuple(range(1, 6))
print(a)
print(len(a))
b = tuple(range(6,11))
c = a+b
print(c)
print(len(c))
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}.')
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?')
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']}.")
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']))
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)
name = list(input().split())
lang = list(input().split())
dict1 = {name:lang for name,lang in zip(name,lang)}
print(dict1)
dict1 = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
a = input()
for x in dict1[a]:
print(x,end=' ')
dic={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
x = input()
y = input()
dic[x] = y
print(dic)
s = input()
dict1 = dict()
for ch in s:
if ch in dict1:
dict1[ch] += 1
else :
dict1[ch] = 1
print(dict1)
x = list(map(int, input().split()))
print(max(x))
print(min(x))
x = list(map(int, input().split()))
print(sum(x))
x = int(input())
print(abs(x))
s = input()
print(ord(s[0]))
x = int(input())
print(hex(x))
x = int(input())
print(bin(x))
x, y = map(int,input().split())
print(pow(x,y))
print(pow(y,x))
x = list(map(int,input().split()))
print(x.count(0))
x = input().split()
print(x.index('NiuNiu'))
x = input()
print(x.isalpha())
print(x.isdigit())
print(x.isspace())
s = input()
print(s.find("NiuNiu"))
s = input()
print(s.count("Niu"))
x = input().split(' ')
print(x)
x = []
while 1:
s = input()
if s=="0": break
else : x.append(s)
print(" ".join(x))
x = input()
print(x.replace('a*','ab'))
x = float(input())
print(round(x,2))
s = input()
print(eval(s))
s = set(input().split(' '))
print(sorted(list(s)))
def cal(x, y):
return x-y
x = int(input())
y = int(input())
print(cal(x,y))
print(cal(y,x))
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))
import math
for i in [1, 2, 4, 9, 10, 13]:
print("%.2f"%(4*math.pi*i**2))
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)
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()
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()
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()
import re
print(re.match('https://www+',input()).span())
import re
print(re.sub('\D', '', input()))
import re
print(re.match("[0-9|-]+", input()).group() )