Python学习笔记(5)
标准库函数Python提供了很多标准库函数,用于完成很多通用任务。之前已经用过input(),raw_input(),range()等函数。
一些库函数已经在Python解释器中内建,因此可以直接调用。还有很多库函数则放在模块下的文件中。这些模块在安装Python时已一并复制,要调用这些放在模块内的库函数,必须使用import()语句导入。
1)产生随机数import random
def main():
number = random.randint(1,10)
print 'The number is', number
main()
结果:The number is 8
例2:
import random
def main():
for count in range(5):
number = random.randint(1,100)
print number
main()
结果:
63
8
1
61
36
例3:
import random
def main():
again = 'y'
while again=='y' or again=='Y':
print 'Rolling the dice...'
print 'Their values are:'
print random.randint(1,6)
print random.randint(1,6)
again = raw_input('Roll them again? (y=yes): ')
main()
2)函数返回值使用return expression语句。
def main():
first_age = input('Enter your age:')
second_age = input('Enter yoru best friend/'s age:')
total = sum(first_age, second_age)
print 'Together you are', total, 'years old.'
def sum(num1,num2):
result = num1+num2
return result
main()
3)返回多个值使用return expression1, expression2, etc.
def get_name():
first = raw_input('Enter your first name:')
last = raw_input('Enter your last name:')
return first, last
print get_name()
结果:
Enter your first name:Mr.
Enter your last name:Jack
('Mr.', 'Jack')
4)math模块import math
def main():
number = input('Enter a number: ')
square_root = math.sqrt(number)
print 'The square root of',number,'is',square_root
main()
结果:
Enter a number: 645
The square root of 645 is 25.3968501984
math模块的常用函数:
acos(x) 反余弦
asin(x) 反正弦
atan(x) 反正切
ceil(x) 大于或等于x的最小整数
sin(x) 正弦
cos(x) 余弦
tan(x) 正切
degrees(x) 弧度转角度数
exp(x) 返回e的x次方
floor(x) 小于或等于x的最大整数
hypot(x,y) 从原点(0,0)到(x,y)的直线距离
log(x) x的自然对数
log10(x) x以10为底的对数
radians(x) 角度数转弧度
sqrt(x) 平方根
例子:
import circle
import rectangle
def main():
choice = 0
while choice !=5:
display_menu()
choice = input('Enter your choice:')
if choice == 1:
radius = input("Enter the circle's radius: ")
print 'The area is',circle.area(radius)
elif choice == 2:
radus = input("Enter the circle's radius:")
print 'The circumference is', circle.circumference(radius)
elif choice == 3:
width = input("Enter the rectangle's width:")
length = input("Enter the rectangle's length:")
print 'The area is', rectangle.area(width,length)
elif choice == 4:
width = input("Enter the rectangle's width:")
length = input("Enter the rectangle's length:")
print 'The perimeter is', rectangle.perimeter(width,length)
elif choice == 5:
print 'Exiting the program...'
print 'OK'
else:
print 'Error: invalid selection.'
def display_menu():
print ' MENU'
print '1) Area of a circle'
print '2) Circumference of a circle'
print '3) Area of a rectangle'
print '4) Perimeter of a rectangle'
print '5) Quit'
main()
分享到:
相关推荐
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
很适合零基础学习Python的同学。
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python学习笔记
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
Python学习笔记.pdf
1、Python环境搭建 ...5、Python中的分支结构 5.1 单向分支-if语句 5.2 双向分支-if 5.3 多分支-if 5.4 分支结构的嵌套 5.5 条件表达式 6、Python中的循环结构 6.1 while循环 6.2 for循环 6.3 循环结构嵌套
python学习笔记python学习笔记python学习笔记python学习笔记python学习笔记
Python 学习笔记
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
python 学习笔记资料python 学习笔记资料python 学习笔记资料python 学习笔记资料
王纯业的Python学习笔记非常适合初学者入门的Python教程
python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf ...
Python学习笔记--皮大庆
王纯业版《Python学习笔记》.pdf
python学习笔记+源码练习,简单易懂,让你从入门到不放弃
王纯业的Python学习笔记
python基础学习笔记