Python Training Course V
Python Training Course V
2
Standalone Programs
Save to test1.py
Save to test2.py
3
Modules and the import Statement
4
Import a Module A module is just a file
of Python code.
weatherman.py
report.py
5
Import a Module
6
Import a Module with Another Name
7
Module Search Path
9
Packages You’ll need one more thing in the
sources directory: a file named
__init__.py. This can be empty, but
Python needs it to treat the
directory containing it as a
package.
10
Function of enumerate
A new built-in function,
enumerate(), will make
certain loops a bit clearer.
enumerate(thing), where
thing is either an iterator
or a sequence, returns a
iterator that will return (0,
thing[0]), (1, thing[1]), (2,
thing[2]), and so forth.
11
練習題
12
The Python Standard Library
13
Handle Missing Keys with setdefault()
14
Handle Missing Keys with defaultdict() (I)
defaultdict 的 argument 為
function ,
而 int() = 0
15
Handle Missing Keys with defaultdict() (II)
16
Count Items with Counter()
列出 top 1
17
Count Items with Counter()
18
The Python 3 Standard Library by Example
19
More Batteries: Get Other Python Code
PyPI Python Packages Index
https://pypi.python.org/pypi
github
https://github.com/Python
readthedocs
https://readthedocs.org/
20
練習題
運用 standard library 中的 smtplib 寄一封
email 到自己的信箱
提示:請參考
The Python 3 Standard Library by Example 書
中的範例
or
官方網站
https://docs.python.org/3/library/smtplib.htm
21