Python语言程序设计Y.Daniel Liang练习题Chapter3

list3.1

import math

print("exp(1.0)=", math.exp(1))
print("log(2.78)=", math.log(math.e))
print("log10(10,10)=", math.log(10,10))
print("sqrt(4.0)=", math.sqrt(4.0))

print("sin(PI/2)=", math.sin(math.pi/2))
print("cos(PI/2)=", math.cos(math.pi/2))
print("tan(PI/2)=", math.tan(math.pi/2))
print("degrees(1.57)=", math.degrees(1.57))
print("radians(90)=", math.radians(90))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

list3.2

import math

x1, y1, x2, y2, x3, y3 =eval(input("Enter three points: "))
a = math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3))
b = math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3))
c = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))

A = math.degrees(math.acos((a * a -b * b - c * c) / (-2 * b * c)))
B = math.degrees(math.acos((b * b -a * a - c * c) / (-2 * a * c)))
C = math.degrees(math.acos((c * c -b * b - a * a) / (-2 * a * b)))

print("The three angles are ", round(A * 100) / 100.0,
      round(B * 100) / 100.0, round(C * 100) / 100.0)

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

list3.3

import turtle

turtle.write(("\u6B22\u8FCE \u03b1 \u03b2 \u03b3"))
turtle.done()

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

list3.4

# Receive the amount
amount = eval(input("Enter an amount, for example, 11.56: "))

# Convert the amount to cents
remainingAmount = int(amount * 100)

# Find the number of one dollars
numberOfOneDollars = remainingAmount // 100
remainingAmount = remainingAmount % 100

# Find the number of quarters in the remaining amount
numberOfQuarters = remainingAmount // 25
remainingAmount = remainingAmount % 25

# Find the number of dimes in the remaining amount
numberOfDimes = remainingAmount // 10
remainingAmount = remainingAmount % 10

# Find the number of nickels in the remaining amount
numberOfNickels = remainingAmount // 5
remainingAmount = remainingAmount % 5

# Find the number of pennies in the remaining amount
numberOfPennies = remainingAmount

# Display the results
print("Your amount", amount, "consists of\n",
      "\t", numberOfOneDollars, "dollars\n",
      "\t", numberOfQuarters, "quarters\n",
      "\t", numberOfDimes, "dimes\n",
      "\t", numberOfNickels, "nickels\n",
      "\t", numberOfPennies, "pennies")

Python语言程序设计Y.Daniel Liang练习题Chapter3 

 

list3.5

import turtle

# Set pen thickness to 3 pixels
# Pull the pen up

# Pull the pen down
# Draw a triangle

turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.circle(40, steps = 4) # Draw a square

turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.circle(40, steps = 5) # Draw a pentagon

turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.circle(40, steps = 6) # Draw a hexagon

turtle.penup()
turtle.goto(200, -50)
turtle.pendown()
turtle.circle(40) # Draw a circle

turtle.done()

Python语言程序设计Y.Daniel Liang练习题Chapter3 

list3.6

import turtle

turtle.pensize(3) # Set pen thickness to 3 pixels
turtle.penup() # Pull the pen up
turtle.goto(-200, -50)
turtle.pendown() # Pull the pen down
turtle.begin_fill()# Begin to fill color in a shape
turtle.color("red")
turtle.circle(40, steps = 3) # Draw a triangle
turtle.end_fill()# Fill the shape

turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("blue")
turtle.circle(40, steps = 4) # Draw a square
turtle.end_fill()

turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("green")
turtle.circle(40, steps = 5) # Draw a pentagon
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("yellow")
turtle.circle(40, steps = 6) # Draw a hexagon
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(200, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("purple")
turtle.circle(40) # Draw a circle
turtle.end_fill() # Fill the shape
turtle.color("green")
turtle.penup()
turtle.goto(-100, 50)
turtle.pendown()
turtle.write("Cool Colorful Shapes",
             font = ("Time", 18, "bold"))
turtle.hideturtle()

turtle.done()

Python语言程序设计Y.Daniel Liang练习题Chapter3


Check3.1

import math

print("math.sqrt(4)=", math.sqrt(4))
print("math.sin(2 * math.pi)=", math.sin(2 * math.pi))
print("math.cos(2 * math.pi)=", math.cos(2 * math.pi))
print("min(2, 2, 1)=", min(2, 2, 1))
print("math.log(math.e)=", math.log(math.e))
print("math.exp(1)=", math.exp(1))
print("max(2, 3, 4)=", max(2, 3, 4))
print("abs(-2.5)=", abs(-2.5))
print("math.ceil(-2.5)=", math.ceil(-2.5))
print("math.floor(-2.5)=", math.floor(-2.5))
print("round(3.5)=", round(3.5))
print("round(-2.5)=", round(-2.5))
print("math.fabs(2.5)=", math.fabs(2.5))
print("math.ceil(2.5)=", math.ceil(2.5))
print("math.floor(2.5)=", math.floor(2.5))
print("round(-2.5)=", round(-2.5))
print("round(2.6)=", round(2.6))
print("round(math.fabs(-2.5))", round(math.fabs(-2.5)))

Python语言程序设计Y.Daniel Liang练习题Chapter3

Check3.5

print("1 = ", ord('1'))
print("A = ", ord('A'))
print("B = ", ord('B'))
print("a = ", ord('a'))
print("b = ", ord('b'))

print("40 = ", chr(40))
print("59 = ", chr(59))
print("79 = ", chr(79))
print("85 = ", chr(85))
print("90 = ", chr(90))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

Check3.6

print("\\")
print("\"")

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.7

#Unicode的写法是以 \u 开始的,且有范围,范围为 \u0000 - \uFFFF之间,

Check3.8

x = input("Enter a character:")
ch = chr(ord(x) +3)
print(ch)

Python语言程序设计Y.Daniel Liang练习题Chapter3 

 

Check3.9

x = input("Enter a character:")
y = input("Enter a character:")
print(ord(y) - ord(x))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.10

title = "Chapter " + str(1)
print(title)

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.11

sum = 2 + 3
print(sum)
s = '2' + '3'
print(s)

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.12

"""在python里,所有数据(包括数字和字符)都是objects。
   python里的方法\函数就是methods。"""

Check3.13

"""
用id()和type()这两个函数即可
"""

Check3.14

"""
option (b)
"""

Check3.15

s = "\tGeorgia\n"
a = s.lower()
b = s.upper()
print(a, b)

Python语言程序设计Y.Daniel Liang练习题Chapter3 

 

Check3.16

s = " \tGood\tmorning\n"
a = s.strip()
print(a)

Python语言程序设计Y.Daniel Liang练习题Chapter3 

 

Check3.17

"""
format()函数返回的是string型的,即字符型
"""

Check3.18

"""
格式里规定的长度大于数字的长度时,就四舍五入
"""

Check3.19

print(format(57.467657, "9.3f"))
print(format(12345678.923, "9.1f"))
print(format(57.4, ".2f"))
print(format(57.4, "10.2f"))

Python语言程序设计Y.Daniel Liang练习题Chapter3 

 

Check3.20

print(format(57.467657, "9.3e"))
print(format(12345678.923, "9.1e"))
print(format(57.4, ".2e"))
print(format(57.4, "10.2e"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.21

print(format(5789.467657, "9.3f"))
print(format(5789.467657, "<9.3f"))
print(format(5789.4, ".2f"))
print(format(5789.4, "<.2f"))
print(format(5789.4, ">9.2f"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.22

print(format(0.457467657, "9.3%"))
print(format(0.457467657, "<9.3%"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.23

print(format(45, "5d"))
print(format(45, "<5d"))
print(format(45, "5x"))
print(format(45, "<5x"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Check3.24

print(format("Programming is fun", "25s"))
print(format("Programming is fun", "<25s"))
print(format("Programming is fun", ">25s"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 


Section3.1

import math

r = eval(input("Enter the length from the center to a vertex: "))
s = 2 * r * math.sin(math.pi / 5)
area = 3 * math.sqrt(3) * s * s / 2.0
print("The area of the pentagon is ", format(area, ".2f"))

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Section3.4

import math

s = eval(input("Enter the side: "))
area = (5 * s * s) / (4 * math.tan(math.pi / 5))
print("The area of the pentagon is ", area)

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Section3.6

s = eval(input("Enter the ASCII code: "))
print("The charater is ", chr(s))

Python语言程序设计Y.Daniel Liang练习题Chapter3

Section3.7

import time
t = round(time.time())
yu = t % 27 #yu的范围是0到26,正好是26个字母
print(yu)

s = chr(65 +yu)
print(s)

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Section3.10

import turtle

turtle.write("\u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7 \u03b8")
turtle.done()

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 

Section3.13

import turtle
import math

turtle.pensize(3) # Set pen thickness to 3 pixels

turtle.right(30) #旋转30度
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("red")
turtle.circle(70, steps = 6) # Draw a hexagon
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(0, 70 * math.sin(math.pi/3)-20)
turtle.pendown()

turtle.color("white")
turtle.write("STOP", font = ("Times", 20, "bold"))

turtle.hideturtle()
turtle.done()

Python语言程序设计Y.Daniel Liang练习题Chapter3  

Section3.19

import turtle

a, b = eval(input("Enter first point:"))
c, d = eval(input("Enter second point:"))
x = "(" + str(a) + "," + str(b) + ")"
y = "(" + str(c) + "," + str(d) + ")"
turtle.penup()
turtle.goto(a, b)
turtle.pendown()
turtle.write(x)

turtle.goto(c, d)
turtle.write(y)

turtle.hideturtle()
turtle.done()

 Python语言程序设计Y.Daniel Liang练习题Chapter3

 


上一篇:【JavaScript】创建全0的Array


下一篇:图解 JavaScript 数组方法