py速成练习¶
DAY 1. 循环输出¶
- 直接输出:
for i in range(2000,3201):
if(i%7==0 and i%5!=0):
print(i,end=',')#具体设置是由Print的参数决定
#print 参数:print(*bojects,sep=' ',end='n',file=sys.stdout)
object:一次输出的对象(可以多个)
sep:多个对象的间隔值
end:设定以什么东西结尾(结尾符号)
file:要写入的文件对象
- 通过列表:
l=[]
for i in range(2000,3201):
if(i%7==0 and i%5!=0):
l.append(str(i))
print(','.join(l))
- 利用print多对象输出:
print(*(i for i in range(2000,3201) if(i%7==0 and i%5!=0)),sep=',') #类似 i for i in range(): if(): print
DAY 2. 列表输入¶
- 有分隔符的输入:
input().split('sep')
#spe为分隔符,输入一个List
list.tuple()
#转换成tuple元组
- 类定义:
class io:
def getstr(self):
self.s=input()
def prtstr(self):
print(self.s.upper())
t=io()
t.getstr()
t.prtstr()
- 列表计算
#直接用列表算
from math import sqrt
c,h=50,30
def cal(temp):
return sqrt((2*c*temp)/h)
l=input().split(',')
for i in range(0,len(l)):
if i!=len(l)-1:
print((int)(cal(int(l[i]))),end=',')
else:
print((int)(cal(int(l[i]))))
#solution: 用一个新的list存储结果再输出
from math import sqrt
C,H = 50,30
def calc(D):
return sqrt((2*C*D)/H)
print(",".join([str(int(calc(int(i)))) for i in input().split(',')]))
- 二维数组:类用vector<>建立二维数组
DAY3¶
-
字符串数组排序:
for i in l: if l.count(i)>1: l.remove(i) l.sort() print(" ".join(l)) #答案: l=input().split() for i in l: if l.count(i)>1: l.remove(i) l.sort() print(" ".join(l))
-
python二进制转换为十进制:
l=input().split(',')
for i in l:
t=int(i,2)
if(t%5==0):
print(i)
#利用int进行转换,int(字符串,原进制数)
-
python的数字和字符串转换:
l=[] for i in range(2000,2050): t=int(i) a=int(t%10)#个位 b=int(t/10%10)#十位 c=int(t/100%10)#百位 d=int(t/1000%10)#千位 if(a%2==0 and b%2==0 and c%2==0 and d%2==0): l.append(str(i)) print(",".join(l))
-
python统计对应种类字符的数量:
l=input() le=0 di=0 for i in l: if((i<='z' and i>='a')or (i<='Z' and i>='A')): le+=1 if(i<='9' and i>='0'): di+=1 print("LETTERS %d"%le) print("DIGITS %d"%di) #输出的另一种写法 print("LETTERS {0}\nDIGITS {1}".format(letter,digit)) OR print(f"LETTERS {letter}\n{digits}") # two different types of formating method is shown in both solution
Day4¶
-
统计大小写个数:
word = input() upper,lower = 0,0 for i in word: if 'a'<=i and i<='z' : lower+=1 if 'A'<=i and i<='Z': upper+=1 #输出: print("UPPER CASE %d\nLOWER CASE %d"%(upper,lower)) #或者 print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
-
求a+aa+aaa+aaaa的值,a为输入:
a=int(input())
sum,t=0,0
for i in range(1,5):
t+=a
sum+=t
t*=10
print(sum)
Day5¶
-
对于输入的一串数字对其中的奇数求平方然后添加到列表当中再输出:
l=input().split(',') res=[] for i in l: t=int(i) if(t%2==1): res.append(str(t*t)) print(",".join(res))
-
python按行输入不定长数据:
import sys res=0 while True: t=input().split() if not t: break if(t[0]=='D'): res+=int(t[1]) else: res-=int(t[1]) print(res) ##答案 total = 0 while True: s = input().split() if not s: # break if the string is empty break cm,num = map(str,s) # two inputs are distributed in cm and num in string data type if cm=='D': total+=int(num) if cm=='W': total-=int(num) print(total)
Day6¶
-
打印指定格式的字符:
l=input().split(',') def le_l(x): re=0 for j in x: if(j>='a' and j<='z'): re+=1 return re def le_u(x): re=0 for j in x: if(j>='A' and j<='Z'):re+=1 return re def num(x): re=0 for j in x: if(j>='0' and j<='9'):re+=1 return re def ch(x): re=0 for j in x: if(j=='$' or j=='#' or j=='@'):re+=1 return re res=[] for i in l: if(len(i)>=6 and len(i)<=12): if(le_l(i)>=1 and le_u(i)>=1 and num(i)>=1 and ch(i)>=1): res.append(i) print(','.join(res))
-
python按优先级排序:(此题有点问题)
# 答案代码(ACW上环境无法实现) lst = [] while True: s = input().split(',') if not s[0]: # breaks for blank input break lst.append(tuple(s)) lst.sort(key= lambda x:(x[0],int(x[1]),int(x[2]))) # here key is defined by lambda and the data is sorted by element priority 0>1>2 in accending order print(lst)
DAY 7¶
- 求0->n之间能整除7的数字:
n=input()
def cal(x):
for i in range(0,int(x)+1):
if(i%7==0):
print(i)
cal(n)
- 计算经过上下左右四个方向移动之后的欧几里得距离:
import math x=0 y=0 for i in range(0,5): s=input() t=[s.split(" ")] if(t[0]=="UP"): y+=int(t[1]) elif(t[0]=="DOWN"): y-=int(t[1]) elif(t[0]=="LEFT"): x-=int(t[1]) elif(t[0]=="RIGHT"): x+=int(t[1]) else: continue print(int(round(math.sqrt(x*x+y*y))))
DAY8¶
-
统计单词的出现数量:
l=input().split() word=sorted(set(l))#对其进行排序,由于是set集合所以不重复 for i in word: print("{0}:{1}".format(i,l.count(i))) #使用pprint(本质与上面同理) from pprint import pprint p=input().split() pprint({i:p.count(i) for i in p})
-
直接计算平方:
n=input()
print(int(n)**2)
# a^b的运算:
a,b=input().split()
print(int(a)**int(b))
-
打印python的官方文档:
print(str.__doc__) print(sorted.__doc__) def pow(a,b): #this is a pow program return int(a)**int(b) print(pow(2,3)) print(pow.__doc__)
-
类名与成员变量一致:
class algo: name="algo" def __init__(self,name): self.name=name dp=algo("dpp") print("{0},{1}".format(dp.name,algo.name)) merge=algo("me") print("{0},{1}".format(merge.name,algo.name)) #输出: dpp,algo me,algo
DAY9¶
-
定义一个函数计算a+b:
def s(a,b): return int(a)+int(b) x,y=input().split() print(s(x,y)) #lambda表达式 sum = lambda n1,n2 : n1 + n2 # here lambda is use to define little function as sum print(sum(1,2))
-
定义一个能够把数字转换成字符串的函数:
def prtstr(x): print(str(x)) a=input() prtstr(a) #lambda表达式 conv = lambda x : str(x) n = conv(10) print(n) print(type(n)) # checks the type of the variable
-
定义一个接收两个字符串整数然后返回a+b的函数:
def su(a,b):
print(int(a)+int(b))
su("2","3")
#lambda表达式
sum = lambda s1,s2 : int(s1) + int(s2)
print(sum("10","45")) # 55
- 定义一个接收两个字符串整数然后输出两个数的函数:
def prt(a,b):
print(a+b)
prt(a,b)
#lambda表达式
sum = lambda s1,s2 : s1 + s2
print(sum("10","45")) # 1045
- 打印较长的字符串,如果一样长就逐行打印
def prt(a,b):
if(len(a)>len(b)):
print(a)
elif(len(a)<len(b)):
print(b)
else:
print(a)
print(b)
prt("hello","waaaa")
#lambda表达式
def printVal(s1,s2):
len1 = len(s1)
len2 = len(s2)
if len1 > len2:
print(s1)
elif len1 < len2:
print(s2)
else:
print(s1)
print(s2)
s1,s2=input().split()
printVal(s1,s2)
DAY9¶
question 26 函数计算两数之和
def sum(a,b):
return a+b
a=1
b=2
print(sum(a+b))
question 27 函数转换: int->string
def convetr(a):
return str(a)
print(3)
def sum(a,b):
print(int(a)+int(b))
sum("233",'22')
#lambda表达式 lambda 参数:表达式
sum=lambda s1,s2:int(s1)+int(s2)
question 29 函数接受两个输入的String变量连接并且打印
def sum(a,b):
print(a+b)
#sum1 lambda a,b:a+b
a=input()
b=input()
sum(a,b)
question 30 定义一个函数,打印两个输入的String变量当中长的那个
def longer(a,b):
if(len(a)>len(b)):
print(a)
elif(len(a)==len(b)):
print(a+b)
else:
print(b)
a,b=input().split()
longer(a,b)
Day10¶
question 31 定义一个函数,打印一个\([1,20]\)的字典,值是下标的平方
def print_dic(): for i in range(1,21): res.append(i*i) res=[] print_dic() print(res)
Ps: 1. python中的list,set,dictionary的区别 2. python中的comprehensions方法快速建立list,set or dictionary
question 32 定义一个函数可以生成字典(具体要求同上),但是函数只可以打印下标(keys)
def print_dic2(): for i in range(1,21): res.append(i*i) res=dict() print_dic2(res.keys())
Ps: 字典推导式: