『pythonコアプログラミング第2版』第5章練習解析

5081 ワード

1.
通常の整形、すなわち標準整形は最も一般的なデジタルタイプである.その値範囲は-2*31~~2*31-1、すなわち-2 147 483 648~~2 147 483 647です.pythonの長い整形はCや他のコンパイル言語とは全く異なり、機械がサポートできるメモリサイズにのみ関係しています.つまり、pythonは大きな整形を簡単に表すことができます.
2.
def mul(a ,b):
    return  a*b
def mul(a ,b):
    return  a*b

if __name__=='__main__':
    a = input()
    b = input()
    print mul(a, b)

3.
def socre(num):
    if num>=90 and num <=100:
        return 'A'
    elif num >=80:
        return 'B'
    elif num >=70:
        return 'C'
    elif num >= 60:
        return 'D'
    else:
        return 'F'
    
if __name__=='__main__':
    n = input()
    print socre(n)
4.
# -*- coding: utf-8 -*- 
def year(num):
    if (num % 4==0 and num % 100 !=0) or (num % 400 ==0):
        return '    '
    else:
        return '     '
    
if __name__=='__main__':
    n = input()
    print year(n)
 5. 
  
# -*- coding: utf-8 -*- 
def money(num):
    num = num *100
    a = num // 25
    b = (num - a *25)//10
    c = (num - a* 25 - b *10) //5
    d = (num -a * 25 - b * 10 - c * 5)
    return a+b+c+d
    
if __name__=='__main__':
    n = input()
    print money(n)

6.
# -*- coding: utf-8 -*- 
def test(a,s,b):
    if s == '+':
        return a+b
    elif s == '-':
        return a-b
    elif s == '*':
        return a*b
    elif s == '/':
        return a // b
    elif s == '%':
        return a%b
    elif s == '**':
        return a**b
    else:
        return 0
    
    
if __name__=='__main__':
    ss = raw_input()
    if len(ss) == 3:
        print test(float(ss[0]),ss[1],float(ss[2]))
    else:
        print test(float(ss[0]),ss[1:3],float(ss[3]))

8.
# -*- coding: utf-8 -*- 
import math    
    
if __name__=='__main__':
    ss = input()
    print '     :',ss*ss
    print '     :',ss*ss*6
    print '     :',ss*ss*ss
    print '   :',ss*ss*math.pi
    print '   :',ss*ss*math.pi*4
    print '   :',ss*ss*ss*math.pi*4/3.0

9.
8 と10
い と
10.
# -*- coding: utf-8 -*- 
   
def change(f):
    return (f-32)*(5.0/9.0)    
if __name__=='__main__':
    ss = input()
    print change(ss)

11.
a,b
# -*- coding: utf-8 -*- 
   
def num():
    num1=[]  #  
    num2=[]  #  
    for i in range(20):
        if i %2 == 0:
            num1.append(i)
        else:
            num2.append(i)
    print '  :'
    for i in num1:
        print i
    print '
:' for i in num2: print i if __name__=='__main__': num()

d.
# -*- coding: utf-8 -*- 
   
def num(num1,num2):
    if (num1%num2==0) or (num2%num1==0):
        return True
    else:
        return False
    
    
       
if __name__=='__main__':
    num1 = input()
    num2 = input()
    print num(num1,num2)
    

13.
# -*- coding: utf-8 -*-

def test(h , m):
    min= h * 60 + m
    return min

if __name__ == '__main__':
    while True:
        h = input()
        m = input()
        if (h < 23 and h > -1) and (m < 60 and m > -1):
            print 'The time is %d minuters' % test (h,m)
        else:
            print 'input error'

15
# -*- coding: utf-8 -*- 

def gongyueshu(m , n):
    if m< n:
        min = m
    else:
        min = n
    for i in range(min , 0 ,-1):
        if m % i ==0 and n % i ==0:
            return i
    return 0

def gongbeishu(m , n):
    l = gongyueshu(m,n)
    return m * n / l

if __name__ == '__main__':
    m = input()
    n = input()
    print '     :',gongyueshu(m, n)
    print '     :',gongbeishu(m, n)

16.
 
# -*- coding: utf-8 -*-

def Payment(cost,total):
    count = 0
    print '             Amount Remaining'
    print 'Pymt#   Paid        Balance'
    print '-----   ------   --------'
    while True:
        print '%-2d       $%.2f      $%6.2f'%(count,total,cost)
        if cost - total >=0:
            cost = cost-total
        else:
            if cost !=0:
                print '%-2d       $%.2f       $%6.2f'%(count+1,cost,0)
            break
        count += 1

if __name__=='__main__':
    cost = input('Enter opening balance:')
    total = input('Enter monthly payment:')
    Payment(cost,total)
17.
 
# -*- coding: utf-8 -*-
import random
if __name__=='__main__':
    N = random.randint(1,100)
    list = []
    for i in range(N):
        n = random.randint(0,2**31-1)
        list.append(n)
    list.sort()
    print list