《python无师自通》第八章 模块

导入模块

import math


print(math.pow(2,3))

import random


print(random.randint(0,100))

import statistics

nums = [1,5,3,6,4,5]
print(statistics.mean(nums))

检查是不是模块

>>> import keyword
>>> keyword.iskeyword("for")
True

导入自己的模块

import hello
a = hello.print_hello()
print(a)

挑战练习

1.调用 statistics 模块中不同于示例中提到的函数。
在这里插入图片描述
2.创建一个名为 cubed 的模块,在其中写一个函数:接受一个数字作为参数,返
回该数字的立方。并在另一个模块中导入并调用该函数。

def cubed_num(x):
    return x ** 3

import cubed
x = int(input("请输入数字:"))

result = cubed.cubed_num(x)
print(result)
发布了42 篇原创文章 · 获赞 0 · 访问量 267

猜你喜欢

转载自blog.csdn.net/qq_43169516/article/details/103933816