习题练习1
习题1
三角形三条边长度分别为3、7、9,计算这个三角形的三角度数(用弧度制表示)
>>> import math >>> a = 3 >>> b = 7 >>> c = 9 >>> cosC = (a**2 + b**2 - c**2) / c**2 >>> C = math.acos(cosC) >>> print(C) 1.8587081466565707
习题2
对于字符串: 'you need python'
- 分别得到三个单词
- 按照从左向右的顺序,各一个字符取一个
- 按照从右向左的顺序,各一个字符取一个
- 将字符串反序
>>> s = 'you need python' >>> s.split() ['you', 'need', 'python'] >>> s[::2] 'yune yhn' >>> s[::-2] 'nhy enuy' >>> s[::-1] 'nohtyp deen uoy'
习题3
字符串'hello',字母h的左侧和o的右侧都有空格,使用字符串的方法去除空格
将字符串"you need python"中的空格用"*"替代
>>> h = ' hello ' >>> h ' hello ' >>> h.strip() 'hello'
>>> s = 'you need python' >>> '*'.join(s.split()) 'you*need*python'
习题4
编写程序,实现以下操作:
- 通过键盘输入数字,作为圆的半径
- 计算圆的周长和面积,并分别打印显示出来,结果保留两位小数
import math n = input('please input a number:') n = float(n) circle = 2 * math.pi * n area = math.pi * n**2 print('the circle is:', round(circle, 2)) print('the area is:', round(area, 2))
please input a number:5
the circle is: 31.42
the area is: 78.54
习题5
编写程序,实现如下功能
- 询问用户姓名和年龄
- 计算10年之后的年龄
- 打印出用户的姓名,极其现在和10年后的年龄
name = input('your name:') age = input('your age:') after_ten = int(age) + 10 print("-"*10) print("your name is {0}. \nyou ate {1} years, now. \nAfter the years, you are {2}".format(name, age, after_ten))
your name:zhangsan
your age:30
----------
your name is zhangsan.
you ate 30 years, now.
After the years, you are 40