0
点赞
收藏
分享

微信扫一扫

Python学习笔记(5)


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()

举报

相关推荐

0 条评论