python语言程序设计_梁勇—第一章习题练习及答案

1、显示三个不同的消息编写程序显示welcome to python,welcome to Comeputer Science,Programming is fun

import math

def print_message():
    print('welcome to python')
    print('welcome to Computer Science')
    print('Programming is fun')


print_message()

2、显示同样的消息5次

def print_5_times():
    for i in range(5):
        print('Welcome to python')


print_5_times()

3、显示一种模式 由F组成的F

def print_F():
    print('FFFFFFF')
    print('FF')
    print('FFFFFFF')
    print('FF')
    print('FF')

print_F()

4、打印表格


def print_excel():
    print('a    a^2    a^3')
    for a in range(1,5):
        print(a**1,a**2,a**3,sep='     ')


print_excel()

5、级数求和1+2+…+9

def calculate_num():
    sum = 0
    for i in range(1,10):
        sum += i

    print("级数求和1+2+....+9=",sum)
    print("级数求和1+2+....+9= %d"%sum)

calculate_num()

6、近似pi的求解

def calculate_pi():
    pi1 = 4*(1-1/3+1/5-1/7+1/9-1/11)
    print(pi1)


calculate_pi()

7、圆的面积和周长

def Caculate():
    radius = eval(input("请输入圆的半径:"))
    area = radius**2 * math.pi
    perimeter = 2 * radius * math.pi
    print("圆的半径为%.2f  圆的周长为%.2f"%(area,perimeter))


Caculate()

8、计算平均速度

def caculate_of_average_speed():
    distance = eval(input("请输入路程:"))
    time = eval(input("请输入耗费的时间:"))
    average_speed = distance/time
    print("平均速度为:%.2f"%average_speed)


caculate_of_average_speed()

9、人口预测

def projected_population():
    population = 3120324986
    people_born_every_second = 7
    people_dead_every_second = 13
    new_immigration = 45
    the_time_of_everyyear = 365 * 24 *60*60*5
    annual_population_bored_increase = the_time_of_everyyear // people_born_every_second
    annual_population_immigration = the_time_of_everyyear // new_immigration
    annual_population_dead = the_time_of_everyyear // people_dead_every_second
    annual_population_increase = population + annual_population_bored_increase + annual_population_immigration - annual_population_dead
    the_total_of_5_years_population = annual_population_increase
    print("5年后的总人口为:%d"%the_total_of_5_years_population)


projected_population()

-------------------------强烈推荐这本书,十分详细,适合自学初学者有兴趣的小伙伴可以私我获取资料

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值