python学习笔记(一)模块

python学习笔记(一)模块

python模块类似Java中的库,HTML中引入的外部文件。
如需创建模块,只需将所需代码保存在文件扩展名为 .py 的文件

怎样使用模块?

1、创建文件mymodule.py

def greeting(name):
  print("Hello, " + name)
  person1 = {
  "name": "Bill",
  "age": 63,
  "country": "USA"
}

2、使用import导入模块

import mymodule
mymodule.greeting("Bill")
a = mymodule.person1["age"]
print(a)

Hello, Bill
63

使用模块中函数的语法
**模块名.函数名字
**
模块名.对象名[下标]

为模块创建别名

import mymodule as mx

python中的内建模块

1、platform

import platform
x = platform.system()
print(x)
#Windows

2、有一个内置函数可以列出模块中的所有函数名(或变量名)。dir()

import platform
x = dir(platform)
print(x)
#['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']

注释:dir() 函数可用于所有模块,也可用于您自己创建的模块。

仅仅导入模块中的某个函数或者对象

from mymodule import person1
print (person1["age"])#注意这里没有模块名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nefelibat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值