python學習--for迴圈詳解

來源:互聯網
上載者:User

標籤:abc   統計   err   python學習   nbsp   遍曆   sci   code   ***   

For 迴圈

一、使用for迴圈實現簡易功能

‘‘‘乘法表1--9乘法表for迴圈實現數字遍曆‘‘‘for i in range(1,10): # 行中        for s in range(1,i+1):  # 列中        print(‘%s X %s=%s   ‘%(s,i,s*i),end =‘‘)    print()‘‘‘星星實現星星以三角形排列‘‘‘for i in range(1,9):    # 行    for s in range(len(range(1,9))-i):        print(‘ ‘,end =‘‘)    for n in range(1,2*i):  # 列        print(‘*‘,end = ‘‘)        print()    ‘‘‘金額小寫=>大寫對迴圈數字進行拆分對數組合‘‘‘list_num = [‘零‘,‘壹‘,‘貳‘,‘三‘,‘肆‘,‘伍‘,‘陸‘,‘柒‘,‘捌‘,‘玖‘]list_U = [‘園‘,‘拾‘,‘佰‘,‘仟‘,‘萬‘]input_num = input(‘輸入數字:‘)len_U = len(input_num)for i in input_num:    len_U -=1    print("{0}{1}".format(list_num[int(i)],list_U[len_U]),end=‘‘)print(‘整‘)

運行:

E:\python_VS_code\directory[目錄]>D://py3.6//python.exe e:/python_VS_code/directory[目錄]/demo0802/py_for_mult.py1 X 1=11 X 2=2   2 X 2=41 X 3=3   2 X 3=6   3 X 3=91 X 4=4   2 X 4=8   3 X 4=12   4 X 4=161 X 5=5   2 X 5=10   3 X 5=15   4 X 5=20   5 X 5=251 X 6=6   2 X 6=12   3 X 6=18   4 X 6=24   5 X 6=30   6 X 6=361 X 7=7   2 X 7=14   3 X 7=21   4 X 7=28   5 X 7=35   6 X 7=42   7 X 7=491 X 8=8   2 X 8=16   3 X 8=24   4 X 8=32   5 X 8=40   6 X 8=48   7 X 8=56   8 X 8=641 X 9=9   2 X 9=18   3 X 9=27   4 X 9=36   5 X 9=45   6 X 9=54   7 X 9=63   8 X 9=72   9 X 9=81       *      ***     *****    *******   *********  *********** ****************************輸入數字:1234壹仟貳佰三拾肆園整

 

二、推導式:

列表推導式:va=[rxp for out in list if (out 條件)]獲得兩個列表交集va3=[10,20,30,40]va4=[20,30,50,60,70]Va5=[ num for num in va3 if num in va4  ]獲得並集va6=[ num for num in  va3 if num not in  va4 ]print(va6)print(va4+va6)獲得差集va7=[ num for num in  va4 if num not in  va3 ]print(va7)--------------------------------------------------------------

  # 獲得0-99之間的 奇數的二倍值
 va2=[i*2 for i in range(100) if i%2!=0 ]

 print(‘va2,‘,va2)


產生器 對象(元組推導式)gen=( i for i in range(5)) print(gen) # for i in gen: # print(i)------------------------------------------------------------ 字典推導式mcase = {‘a‘: 10, ‘b‘: 34, ‘A‘: 7, ‘Z‘: 3,‘B‘:20,‘b‘:50}var={keyexcept:vaExcept for key in 字典對象 }# if k.lower() in [‘a‘,‘b‘,‘z‘] 判斷條件mcase_frequency = { k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) #第三部執行 for k in mcase.keys()#第一步 if k.lower() in [‘a‘,‘b‘,‘z‘] #第二部判斷執行 true 執行第三部-------------------------------------------------------集合推導式mcase_key={key for key in mcase if key== key.lower()}

 

 

三,同樣要求的三種不同解決方式

‘‘‘統計字串中數字和字母的個數1.使用異常處理非數字 try...except2.使用內建函數判定非數字  isdigit()3.使用字元轉換判斷數字字母範圍。ord() 字元轉ascii碼 chr() ascii碼轉字元‘‘‘str_for = ‘adashgdkahfqa223354564‘num = 0str_num = 0‘‘‘第一種‘‘‘for i in str_for:    try:        int(i)        num +=1    except ValueError:        str_num +=1print(‘數字:‘,num,‘\n‘‘字母:‘,str_num)‘‘‘第二種‘‘‘num1 = 0str_num1 = 0for i in str_for:    if i.isdigit() == True:        num1 +=1    else:        str_num1 +=1print(‘數字:‘,num1,‘\n‘‘字母:‘,str_num1)‘‘‘第三種‘‘‘num2 = 0str_num2 = 0for i in str_for:    if ord(‘0‘) < ord(i) <ord(‘9‘):        num2 +=1    elif ord(‘a‘) <= ord(i) <=ord(‘z‘) or ord(‘A‘) <= ord(i) <= ord(‘Z‘):        str_num2 += 1print(‘數字:‘,num2,‘\n‘‘字母:‘,str_num2)

 

 

四、推導式解決【金額大小寫轉換,字典推導式練習】

list_num = [‘零‘,‘壹‘,‘貳‘,‘三‘,‘肆‘,‘伍‘,‘陸‘,‘柒‘,‘捌‘,‘玖‘]list_U = [‘園‘,‘拾‘,‘佰‘,‘仟‘,‘萬‘]input_num = input(‘輸入數字:‘)len_U = len(input_num)num_info = [int(input_num)//10**i%10 for i in range(len_U-1,-1,-1)]for i in num_info:    len_U -=1    print(‘%s%s‘%(list_num[i],list_U[len_U]),end = ‘‘)print(‘整‘)‘‘‘字典對象初始化定義資料把 Key是 str類型長度大於等於5的資訊擷取用推導式‘‘‘dict_1 = {‘a‘:12,123:‘abcd‘,‘12345‘:True,‘sadasdds‘:‘18895‘,‘asd‘:‘d‘}dict_infer = {k:v for k,v in dict_1.items() if type(k) ==str and len(k) >= 5}print(dict_infer)

 


python學習--for迴圈詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.