The conditions and loops of the Python Basic Learning Code

Source: Internet
Author: User
Def func1 (): alist = [' Cathy ', ' Terry ', ' Joe ', ' health ', ' Lucy '] for I in range ( -1,-len (alist) -1,-1): Print I,al Ist[i]def Func2 (): alist = [' Cathy ', ' Terry ', ' Joe ', ' health ', ' Lucy '] for I,name in Enumerate (alist): print '%d %s '% (i,name) import Randomdef func3 (): alist = [' Cathy ', ' Terry ', ' Joe ', ' health ', ' Lucy '] blist = [Random.randint (i,10 ) for I in range (5)] for a, b in Zip (alist,blist): Print A,bdef FUNC4 (): num = 4 count = NUM/2 while C ount > 0:if num% Count = = 0:print count, ' is the largest factor of ', Num break cou NT-= 1def showmaxfactor (num): Count = Num/2 while count > 1:if num% count = = 0:print num,        ' Largest factor is ', count break count-= 1 return True else:print num, ' is prime '    return Falsedef Func5 (): For Eachnum in Range (10,60): Showmaxfactor (Eachnum) def func6 (): alist = Range (5) return map (Lambda x:x * * 2,alist) def func7 (): alist = [x * * 2 for X in range (5)] return alistdef Func8 (): Return filter (lambda x:x% 2 , Range (Ten)) def FUNC9 (): return [x for x in range] if x% 2]def func10 (): Return [(X+1,y+1) for x in range (3) for  Y in range (5)]import osdef func11 (): F = open (' client.py ', ' R ') print Os.stat (' client.py '). St_size print Len ([Word For Line.split (")") F.seek (0) print sum ([Len (word) for line in F for word in line.split (') )] def cols (): Yield 3 yield 5def func12 (): Alist = [1,2,4,6] x = ((i,j) for I in Alist for J in Cols ()) fo     R A in X:print adef func13 (): F = open (' client.py ', ' r ') longest = 0 Alllines = f.readlines () f.close ()    For line in Alllines:linelen = Len (Line.strip ()) if Linelen > longest:longest = Linelen return Longestdef Func14 (): F = open (' client.py ', ' r ') Alllinelen = [Len (X.strip ()) for x in F] f.close () re Turn Max (Alllinelen) defFUNC15 (): Return Max (Len (X.strip ()) for x in open (' client.py ', ' R ')) def func16 (x, Y, z): Alist = [] for I in range            , y+1,z): Alist.append (i) return alistdef getfactors (num): For I in Range (1,num+1): if num% i = = 0:             Print idef isperfect (num): sum = 0 count = NUM/2 while Count > 0:for i in Range (1,count+1):        If num% i = = 0:sum + = I count-= 1 if sum = = Num:return True Else: return falsedef Fibonacci (num): if num = = 1:return [1] if num = = 2:return [all] list = [ ] If num > 2:for i in Range (3,num+1): List.append (List[-1]+list[-2]) return listdef Conve RT (): start = Int (raw_input (U ' enter start value: ')) end = Int (raw_input (U ' Enter end value: ')) for I in Range (start,end+1): pri NT "Dec bin Oct Hex" print '-' * print "%d%s%s%s"% (I,bin (i), Oct (i), Hex (i))

The above is the Python Basic Learning code conditions and the content of the loop, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.