Python语言程序程序设计-第一章习题解答

作为一名非计算机专业毕业的学生,一直想学Python用来解决工作中遇到的问题,但是对计算机一直摸不到好的门路,于是买到了Python语言程序程序设计这本书,用做习题的形式,积累自己的学习成果,相信不怕慢,只怕站。

# 1.1

print "Welcome to Python"

print "Welcome to Computer Science"

print "Program is fun"

# 1.2

print "Welcome to Python" * 5

# 1.3

print "F"*7," "*3,"U"," "*5,"U"," "*3,"N"*2," "*4,"N"*2

print "F"*2," "*4," "*3,"U"," "*5,"U"," "*3,"N"*3," "*3,"N"*2

print "F"*7," "*3,"U"," "*5,"U"," "*3,"N"*2,"","N"*1," "*1,"N"*2

print "F"*2," "*4," "*3,"","U"," "*3,"U"," "*4,"N"*2," "*2,"N","N"*2

print "F"*2," "*4," "*5,""*3,"U"*3," "*2," "*3,"N"*2," "*3,"N"*3

# 1.4

value = [1,2,3,4]

print "a"," "*4,"a^2"," "*2,"a^3"," "*4

print "1"," "*4,"1"," "*5,"1"," "*4

print "2"," "*4,"4"," "*5,"8"," "*4

print "3"," "*4,"9"," "*5,"27"," "*4

print "4"," "*4,"16"," "*4,"64"," "*4

# 1.5

solve = (9.5*4.5 - 2.5*3)/(45.5-3.5)

print solve

# 1.6

sum = 0

for i in range(10):

sum+=i

print sum

# 1.7

from __future__ import division

pi = 0.0

for i in range(1,8):

pi += 4*(((-1)**(i+1))/(2*i-1))

print pi

from __future__ import division

pi = 0.0

for i in range(1,11):

pi += 4*(((-1)**(i+1))/(2*i-1))

print pi

# 1.7

import numpy as np

class sl():

"""docstring for sl"""

def __init__(self, radius):

super(sl, self).__init__()

self.radius = radius

def area():

s = np.pi*self.radius*self.radius

return s

def perimeter():

l = 2*np.pi*self.radius

return l

solve = sl()

radius = 5

print solve.area(radius)

def area(radius):

s = np.pi*radius*radius

return s

def perimeter(radius):

l = 2*np.pi*radius

return l

r = 5

# print "area is %s" % area(r),"perimeter is %s" perimeter(r)

print area(r),perimeter(r)

# 1.9

def s(width,height):

s = width*height

return s

def l(width,height):

l = 2*width+2*height

return l

print s(4.5,7.9),l(4.5,7.9)

# 1.10

def milePerHour(m,s,mile):

enmile = mile/1.6

hour = (m*60+s)/3600

enmile_h = enmile/hour

return enmile_h

print milePerHour(40,30,14)

# 1.11

def predictPop(year):

a0 = 3120324986

s = year*365*3600

predict_pop = a0+(s/7.0+s/45.0-s/13.0)

return predict_pop

print predictPop(1)

print predictPop(2)

print predictPop(3)

print predictPop(4)

print predictPop(5)

# 1.12

import turtle

turtle.forward(100)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(200)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.left(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

# 1.13

import turtle

turtle.right(0)

turtle.forward(200)

turtle.right(180)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(180)

turtle.forward(200)

# 1.14

import turtle

turtle.right(60)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

# 1.15

import turtle

turtle.right(60)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

turtle.right(120)

turtle.forward(200)

turtle.left(120)

turtle.forward(100)

turtle.left(120)

turtle.forward(100)

# 1.16

import turtle

turtle.color('red')

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.circle(50)

turtle.color('blue')

turtle.penup()

turtle.goto(100,0)

turtle.pendown()

turtle.circle(50)

turtle.color('orange')

turtle.penup()

turtle.goto(0,-100)

turtle.pendown()

turtle.circle(50)

turtle.color('yellow')

turtle.penup()

turtle.goto(100,-100)

turtle.pendown()

turtle.circle(50)

# 1.17

import turtle

import math

import numpy as np

from __future__ import division

turtle.penup()

turtle.goto(-39,48)

turtle.write("(-39,48)")

turtle.pendown()

turtle.goto(50,-50)

# turtle.pendown()

turtle.write("50,-50")

turtle.done()

# 1.18

import turtle

# import math

# import numpy as np

# from __future__ import division

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

turtle.right(144)

turtle.forward(200)

# 1.19

import turtle

turtle.penup()

turtle.goto(40,-69.28)

turtle.pendown()

turtle.goto(-40,-69.28)

turtle.goto(-80,-9.8)

turtle.goto(-40,69)

turtle.goto(40,69)

turtle.goto(80,0)

turtle.goto(40,-69.28)

turtle.done()

# 1.20

import turtle

turtle.goto(100,0)

turtle.goto(150,50)

turtle.goto(50,50)

turtle.goto(0,0)

turtle.goto(0,100)

turtle.goto(100,100)

turtle.goto(100,0)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(50,150)

turtle.goto(150,150)

turtle.goto(100,100)

turtle.penup()

turtle.goto(50,150)

turtle.pendown()

turtle.goto(50,50)

turtle.penup()

turtle.goto(150,150)

turtle.pendown()

turtle.goto(150,50)

turtle.done()

# 1.21

import turtle

turtle.color("blue")

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.circle(100)

turtle.penup()

turtle.goto(0,0)

turtle.pendown()

turtle.write("6")

turtle.penup()

turtle.goto(0,183)

turtle.pendown()

turtle.write("12")

turtle.penup()

turtle.goto(-96,100)

turtle.pendown()

turtle.write("9")

turtle.penup()

turtle.goto(90,100)

turtle.pendown()

turtle.write("3")

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(0,180)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(-90,100)

turtle.penup()

turtle.goto(0,100)

turtle.pendown()

turtle.goto(80,100)

你可能感兴趣的:(Python语言程序程序设计-第一章习题解答)