"Python core Programming" answer Chapter 8th

Source: Internet
Author: User
Tags for in range

8-1:

(a): C, E

(b): D, E

(c): B, E

8-2:

#!/usr/bin/env pythonPrint 'Input Three numbers to F, T, I:'F= Int (Raw_input ('F:')) T= Int (Raw_input ('T:')) I= Int (Raw_input ('I:')) forIteminchRange (F, t+1, i):PrintItem

8-3:

(a): Range (10)

(b): Range (3, 19, 3)

(c): Range (-20, 861, 220)

8-4:

def isprime (num):      = NUM/2 while     count > 1:        if num% count = = 0            :return  False        -= 1    if num = = 1:        return  false      return True

8-5:

def getfactors (num):     = []    for in range (1, num+1):        if num% i = =  0:            ans.append (i)     return ans

8-6:

defisprime (num): Count= NUM/2 whileCount > 1:        ifNum% Count = =0:returnFalse Count-= 1ifnum = = 1:        returnFalsereturnTruedefgetfactors (num): ans= []     forIinchRange (1, num+1):        ifnum% i = =0:ans.append (i)returnansdefprimefactorization (num): Factors=getfactors (num) primefactor= [item forIteminchFactorsifIsPrime (item)] ans= []     forIinchPrimefactor: whilenum% i = =0:ans.append (i) Num= num/IreturnAns

8-7:

def getfactors (num):     = []    for in range (1, num+1):        if num% i = =  0:            ans.append (i)     return ans def isperfect (num):     = SUM (getfactors (num) [: -1])    if factorsum = = num        :return 1    Else:        return 0

8-8:

def factorial (n):     = 1     for in range (1, n+1):        *= Item    return ans

8-9:

defFibonacci (N):#Recursive method    ifn = = 1orn = = 2:        return1returnFibonacci (N-2) + Fibonacci (n-1)defFIBONACCI2 (n):#non-recursive methodA, B = 1, 1ifn = = 1orn = = 2:        return1 forXinchRange (n-2): A, B= B, A +breturnB

8-10: Update later

8-11:

num = Int (raw_input ('Enter Total number of names:'))Printnames={}wrongnum=0 forIteminchrange (num): Name= Raw_input ('Please enter name%d:'%Item)if ','  not inchName:wrongnum+ = 1Print "wrong format ... should is last, first"        Print "You have do this %d time (s) already. Fixing input ..."%Wrongnum Ln, fn=name.split () NAMES[FN]=LNElse: LN, fn= Name.split (',') FN=Fn.strip () Names[ln]=fnPrintPrint "The sorted list (by last name) is:" forLastNameinchsorted (names):Print '%s,%s'% (LastName, Names[lastname])

8-12:

1 defmybin (num):2Ans ="'3      whilenum >0:4b = str (num% 2)5Ans = b +ans6num = NUM/27     ifLen (ANS) < 5:8Ans ='0'* (5-len (ans)) +ans9     returnansTen  One defmyoct (num): AAns ="' -      whilenum >0: -b = str (num% 8) theAns = b +ans -num = NUM/8 -     returnans -  + defMyhex (num): -Ans ="' +Adict = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'a', 11:'b', 12:'C', 13:'D', 14:'e', 15:'F'} A      whilenum >0: atb = adict[num% 16] -Ans = b +ans -num = num/16 -     returnans -  -begin = Int (Raw_input ('Enter begin value:')) inend = Int (Raw_input ('Enter End Value:')) - if< End < 127: to     Print "%5s\t%5s\t%5s\t%5s\t%5s"% ("DEC","BIN","OCT","HEX","acsii") +     Print '-'* 40 -      forIinchRange (begin, (end+1)): the         ifI < 32: *c ="' $         Else:Panax Notoginsengc =chr (i) -         Print "%5d\t%5s\t%5s\t%5s\t%5s"%(i, Mybin (i), myoct (i), Myhex (i), c) the Else: +     Print "%5s\t%5s\t%5s\t%5s"% ("DEC","BIN","OCT","HEX") A     Print '-'* 32 the      forIinchRange (begin, (end+1)): +         Print "%5d\t%5s\t%5s\t%5s"% (i, mybin (i), myoct (i), Myhex (i))

8-13: Update later

"Python core Programming" answer Chapter 8th

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.