python习题练习(一)

本文通过多个实例展示Python编程的基础知识,包括程序设计、数学计算、数据分析等,深入理解Python语言特性。

参考书籍:python程序设计

chapter 1

5.

print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1:"))
n = eval(input("How many times shoule I print?"))
for i in range(n):
    x = 3.9 * x * (1 - x)
    print(x)

chapter2

问题分析-程序规格说明-设计-实现-测试/调试-维护

4.

def convert():
    celsius = eval(input("What's the Celsius temperature?"))
    fahrenheit = 9/5 * celsius + 32
    print("The temperature is ", fahrenheit, "degrees Fahrenheit.")

for i in range(5):
    convert()

5.

def main():
    celsius = 0
    for i in range(11):   
        fahrenheit = 9/5 * celsius + 32
        print("Celsius", celsius, "equals to Fahrenheit", fahrenheit, ".")
        celsius += 10
main()

7.

def main():
    print("This program calculates the future value.")

    year = eval(input("How may years do  you want?"))
    principal = eval(input("How much the principal each year?"))
    apr = eval(input("Enter the annual interest rate:"))

    value = principal
    for i in range(year):
        value = (1 + apr) * value
        value += principal
    print("The value in", year, "years is: ", value)

main()

12.

def main():
    print("This program calculates the expression's result.")

    for i in range(100):
        expr = eval(input("Enter an expression:"))
        print(expr)

main()

chapter3

10.

import math

def main():
    print("This program calculates the length of ladder.")

    angle = eval(input("Enter the angle:")) # degree
    radians = angle * math.pi / 180 # radians
    height = 10
    length = height / math.sin(radians)
    print("The length of ladder is", length, ".")

main()

11.

def main():
    print("This program calculates the sum of n natural numbers.")

    num = eval(input("Please enter the number:"))
    s = 0
    for i in range(num + 1):
        s += i
    print("The sum is", s, ".")

main()

15.

import math
def main():
    num = eval(input("Please input the number:"))
    s = 0
    for n in range(num):
        s = s + (-1)**(n) * 4/(2 * n + 1)
    error = math.pi - s
    print("The sum is:", s, ".")
    print("The error is,", error, ".")

main()

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值