codecamp

Python模块

1.模块介绍

  • 一个.py的文件就是一个模块,包里面一定要带一个__init__.py的模块。
  • 模块能够让复杂的程序简单化,便于代码管理,避免重复造轮子
  • Python有很多内置模块与第三方库,用的时候通过pip下载调用就可以了,非常方便。

2.练习

需要注意的是:建立函数的时候,要明白return返回不一定是像函数那章节说的那样一定后面要跟一个什么,在这儿模块内函数return后面什么都不放,在调用的时候再传递参数。

2.1

"""
建立模块
1.新建一个文件`fuction1.py`
2.把下面代码放进去


!/usr/bin/python
Filename: fuction1.py
"""
def hello1():
    num_one = int(input("请输入乘法表:"))
    num_two = 1
    while num_two <= 5:
        num_result = num_one * num_two
        print("%r×%r=%r" % (num_one, num_two, num_result))
        num_two = num_two + 1
    while num_one >= 0:
        num_one = int(input("请输入乘法表:"))
        num_two = 1
        while num_two <= 5:
            num_result = num_one * num_two
            print("%r×%r=%r" % (num_one, num_two, num_result))
            num_two = num_two + 1
    return

"""
调用模块
1.新建一个文件`mode.py`
2.把下面代码放进去
3.运行


!/usr/bin/python 
Filename: mode.py
"""
import fuction1      #先调用模块:   import语句 模块名称
fuction1.hello1()    #然后调用函数:   模块名称.函数名

2.2

"""
!/usr/bin/python
Filename: fuction2.py
"""
def hello2(b,c):
    a=b+c
    print(a)
    return

"""
!/usr/bin/python
Filename: mode.py
"""
import fuction2
fuction2.hello2(50,5)

2.3

"""
!/usr/bin/python
Filename: fuction3.py
"""
def hello3(name):
    print("hello",name)
    return

"""
#!/usr/bin/python
## Filename: mode.py
"""
import fuction3
fuction3.hello3("zhangzexiang")

Python函数
Python初步认识软件工程
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Python学习技巧

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }