python第六節

來源:互聯網
上載者:User

標籤:class   eve   集合   put   數列   asc   調用   mes   ext   

  1. 日誌

Import logging

# LOG_FORMAT="%(asctime)s - %(levelname)s - %(message)s"
# logging.basicConfig(filename=‘aaa.txt‘,level=logging.DEBUG,format=LOG_FORMAT)
# def test():
#     try:
#         a=int(input("請輸入一個被除數:"))
#         b=int(input("請輸入一個除數:"))
#         print(a/b)
#         return
#     except (ValueError):
#         logging.debug("只能輸入數字!")
#     except ZeroDivisionError:
#         logging.info("除數不能為0")
#     else:
#         print("else...")
#     finally:
#         print("程式結束")
# test()

 

2.推導
# number=[x for x in range(100) if x % 3 == 0]
#
# def squared(x):
#     return  x*x
# multiple=[squared(i) for i in range (30) if i % 3 is 0]
# print(multiple)
#
# mcase ={‘a‘:10,‘b‘:34}
# mcase_frequency = {v:k for k,v in mcase.items()}
# print(mcase_frequency)

#例一
# squared={x**2 for x in [1,1,2]}
# print(squared)
#Output:set([1,4])
#用集合推導建字串長度的集合
# strings=[‘a‘,‘is‘,‘with‘,‘if‘,‘file‘,‘exception‘]
# {len(s) for s in strings}
#
# name=[‘bob‘,‘tom‘,‘alice‘,‘jerry‘,‘wendy‘,‘smith‘]
#
# n=[(x,y) for x in range(5) if x%2==0 for y in range(5) if y%2==1]
# print(n)
m=[[1,2,3],
    [4,5,6],
    [7,8,9]]

# x=[m[i][i] for i in range (3)]
# print(x)

n=[[2,2,2],[3,3,3],[4,4,4]]
y=[[m[a][b]*n[a][b] for b in range(3) ] for a in range(3)]
print(y)

 

  1. 迭代器(Iterable)

兩個基本方法:__iter__()  __next__()

  1. 產生器

第一種方式:將列表的[]改為(),裡面可以寫推導

產生器只要調用next()方法即可得到下一個值

第二種方式:通過斐波那契數列

def feibonaqie():

a=b=1

print(a)

print(b)

for i in range(7):

    yield(a+b)

a,b=b,a+b

F=feibonaqie()

print(F)

調用next()方法 

python第六節

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.