python學習-------python中的控制流程語句,python控制語句

來源:互聯網
上載者:User

python學習-------python中的控制流程語句,python控制語句

編寫函數對象,測試for語句,使用range函數列印一個字串偶數項的字母,使用zip函數進行並行迭代操作,使用zip將兩個列表構造成字典

#!/usr/bin/pythone

#encoding:utf8  ##避免中文亂碼報錯:SyntaxError: Non-ASCII character '\xe6' in file

def testwhile(var1):
    cnt=1;
    while cnt<=var1:
        print(cnt)
        cnt=cnt+1
    else:
        print(cnt,"is done")
def testfor():
    list1=[(1,'a'),(3,'b'),(4,'c')]
    dict1=dict(list1)
    for key in dict1:
        print(key,'=>',dict1[key])
    else:
        print("testfor dict is done")
    #list2=list(dict.items())
    for (a,b) in list(dict.items(dict1)):
        print(a,"=>",b)
    else:
        print("testfor items is done")
def testrange():
    for cnt in range(-3,2):
        print(cnt,str(cnt)+"aaa")
    else:
        print("testrange is done")
##列印一個字串偶數項的字母,使用len()函數
strs="weojslkjdfvcnzd"
def print2char():
    print(range(1,len(strs),2))
    newstr=''
    for pos in range(1,len(strs),2):
        print(pos,"->",strs[pos])
        newstr += strs[pos]
    else:
        print("new word is ",newstr)
        print("print2char is done")
##使用zip進行並行迭代操作,使用zip將兩個列表構造成字典
key=[1,3,5,7]
value=['a1','a2','a3','a4']
def double_iteration():
    print(zip(key,value))
    print("double_iteration is done")
"""每個模組都有個名為__name__的內建屬性,python會自動化佈建該屬性;如果檔案是以頂層程式檔案執行,在啟動時,__name__就會設定為字串__main__"""
if __name__=='__main__':
    testwhile(10)
    testfor()
    testrange()
    print2char()
    double_iteration()

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

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.