欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python期末考试复习

程序员文章站 2023-10-31 12:18:10
期末考试复习 补修的python跟着大一一起学,考试肯定不会出难,于是就敲了一些代码,把他们放到博客上,来记录一下 代码都是一段一段的,且python代码不是很多,所以我都写到了一个文件里,作为练习 ......

期末考试复习

补修的python跟着大一一起学,考试肯定不会出难,于是就敲了一些代码,把他们放到博客上,来记录一下

代码都是一段一段的,且python代码不是很多,所以我都写到了一个文件里,作为练习

# python期末考试编程题


# 往文件写东西
# fp = open(r'file/test.txt','a+')
# print('hellow',file=fp)
# fp.close()


# x = list(range(500))
# print(x)
# for item in x:
#   t = 5**5
#   print(item) 

# 优化上面代码
# x = list(range(100))
# t = 5**5
# for item in x:
#   print(item)
#   print(t)



# 生成随机数对偶数下标并降序排列,奇数下标不变
# import random
# x = [random.randint(0,100) for i in range(20)]
# print(x)
# y = x[::2]
# print(y)
# y.sort(reverse=true)
# x[::2]=y
# print(x)



# def join(list,sep=none):
#   return (sep or ',').join(list)
# print(join(['1','b','c'])) # 1,b,c
# print(join(['1','b','c'],':')) # 1:b:c





# def sum(a,b=3,c=5):
#   return sum([a,b,c])
# print(sum(a=8,c=2))
# print(sum(8))
# print(sum(8,2))

# # 13
# # 16
# # 15


# def sum(*p):
#   return sum(p)
# print(sum(3,5,8))
# print(sum(8))
# print(sum(8,2,10))
# # 16
# # 8
# # 20



# # 编写函数判断奇偶数
# import math
# def isprime(v):
#   n = int(math.sqrt(v)+1)
#   for i in range(2,n):
#       if v%i==0:
#           return 'no'

#       else:
#           return 'yes'
# print(isprime(21))



# # 模拟内置sorted函数
# def sorted(v):
#   t=v[::]
#   r=[]
#   while t:
#       tt = min(t)
#       r.append(tt)
#       t.remove(tt)
#   return r
# a = ['1','2','3','4','5','6','1','2','3','6','5']

# print(sorted(a))



# # 生成10个随机数,对前十个升序,对后十个降序
# import random
# x = [random.randint(0,100) for i in range(20)]
# print(x)
# y=x[0:10]
# y.sort() #原地排序 
# x[0:10]=y
# y=x[10:20]
# y.sort(reverse=true)
# x[10:20]=y
# print(x)



# # #判断是否为闰年
# x=input('please input an integer of 4 digit meaning the year:')
# x=eval(x)
# print(x)
# if x%400==0 or (x%4 ==0 and not x%100==0):
#   print('yes')
# else:
#   print('no')



# # 计算分段函数
# x = input('please input x:')
# x = eval(x)
# if x<0 or x>=20:
#   print(0)
# elif 0<=x<=5:
#   print(x)
# elif 5<=x<10:
#   print(3*x-5)
# elif 10<=x<20:
#   print(0.5*x-2)




# # 下列程序不能运行,因为是只读属性

# class test:
#   def _init_(self,value):
#       self._value=value
#   @property
#   def value(self):
#       return self._value
# t = test(3)
# t.value = 5
# print(t.value)




# # 统计数字出现频率
# import random
# x = [random.randint(1,20) for i in range(50)]
# #print(x)
# r = dict()
# #print(r)
# for i in x:
#   r[i] = r.get(i,0)+1
# #print(r)
# for k,v in r.items():
#       print(k,v)



# # 程序文件名
# def main():
#   if __name__=='__main__':
#       print(1)
#   else:
#       print(2)
# main()




# a=[1,2,3,4]
# print(a)
# print(a[-4])




# x= [111,2,33]
# x=sorted([111,2,33])
# key = lambda x: -len(str(x))
# print(key)

# print(max([1111,2,222]))



# print((3,))
# print({1,2,})
# print([1,2,])



# print([i for i in range(100)if i%13 == 0])


# x=[1,3,5,7,1,2]
# x[len(x)+2:]=[1,2]
# print(x)


# def expend_list(list,num):
#   if len(list)>num:
#       return
#   for i in range(1,):

print(132+321)
print('132'+'321')