python期末考試複習

期末考試複習

補修的python跟着大一一塊兒學,考試確定不會出難,因而就敲了一些代碼,把他們放到博客上,來記錄一下python

代碼都是一段一段的,且python代碼不是不少,因此我都寫到了一個文件裏,做爲練習git

# 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')
相關文章
相關標籤/搜索