📄 node14.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Using Modules</TITLE>
<META content="Using Modules" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK
href="node14_files/easytut.css" rel=STYLESHEET><LINK href="node15.html"
rel=next><LINK href="node13.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node15.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html330><IMG align=bottom alt=next border=0 height=24
src="node14_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html326><IMG align=bottom alt=up border=0 height=24
src="node14_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html"
name=tex2html320><IMG align=bottom alt=previous border=0 height=24
src="node14_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html328><IMG align=bottom alt=contents border=0 height=24
src="node14_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html331>More on Lists</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html327>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html"
name=tex2html321>Dictionaries</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html329>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A
name=CHILD_LINKS><STRONG>Subsections</STRONG></A>
<UL>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html#SECTION001410000000000000000"
name=tex2html332>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION001400000000000000000>Using Modules</A> </H1>Here's this
chapter's typing exercise (name it cal.py)<A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/footnode.html#foot634"
name=tex2html3><SUP><IMG align=bottom alt=[*] border=1
src="node14_files/footnote.png"></SUP></A>: <PRE>import calendar
year = input("Type in the year number:")
calendar.prcal(year)
</PRE>And here is part of the output I got: <PRE>Type in the year number:2001
2001
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
</PRE>(I skipped some of the output, but I think you get the idea.) So what does
the program do? The first line <TT>import calendar</TT> uses a new command
<TT>import</TT>. The command <TT>import</TT> loads a module (in this case the
<TT>calendar</TT> module). To see the commands available in the standard modules
either look in the library reference for python (if you downloaded it) or go to
<TT>http://www.python.org/doc/current/lib/lib.html</TT>. The calendar module is
described in 5.9. If you look at the documentation lists a function called
<TT>prcal</TT> that prints a calendar for a year. The line
<TT>calendar.prcal(year)</TT> uses the function. In summary to use a module
<TT>import</TT> it and then use module_name.function for functions in the
module. Another way to write the program is: <PRE>from calendar import prcal
year = input("Type in the year number:")
prcal(year)
</PRE>This version imports a specific function from a module. Here is another
program that uses the Python Library (name it something like clock.py) (press
Ctrl and the 'c' key at the same time to kill the program): <PRE>from time import time, ctime
prev_time = ""
while(1):
the_time = ctime(time())
if(prev_time != the_time):
print "The time is:",ctime(time())
prev_time = the_time
</PRE>With some output being: <PRE>The time is: Sun Aug 20 13:40:04 2000
The time is: Sun Aug 20 13:40:05 2000
The time is: Sun Aug 20 13:40:06 2000
The time is: Sun Aug 20 13:40:07 2000
Traceback (innermost last):
File "clock.py", line 5, in ?
the_time = ctime(time())
KeyboardInterrupt
</PRE>The output is infinite of course so I canceled it (or the output at least
continues until Ctrl+C is pressed). The program just does a infinite loop and
each time checks to see if the time has changed and prints it if it has. Notice
how multiple names after the import statement are used in the line <TT>from time
import time, ctime</TT>.
<P>The Python Library contains many useful functions. These functions give your
programs more abilities and many of them can simplify programming in Python.
<P>
<H1><A name=SECTION001410000000000000000>Exercises</A> </H1>
<P>Rewrite the high_low.py program from section <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html#firsthighlow">5.2</A>
to use the last two digits of time at that moment to be the 'random' number.
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html330><IMG align=bottom alt=next border=0 height=24
src="node14_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html326><IMG align=bottom alt=up border=0 height=24
src="node14_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html"
name=tex2html320><IMG align=bottom alt=previous border=0 height=24
src="node14_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html328><IMG align=bottom alt=contents border=0 height=24
src="node14_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node15.html"
name=tex2html331>More on Lists</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html327>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html"
name=tex2html321>Dictionaries</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html329>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -