Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Non-Programmer's Tutorial for Python 2.6/Using Modules

From Wikibooks, open books for an open world
<Non-Programmer's Tutorial for Python 2.6

Here's this chapter's typing exercise (name it cal.py).import actually looks for a file named calendar.py and reads it in. If the file is named calendar.py and it sees an "import calendar" it tries to read in itself which works poorly at best.)):

importcalendaryear=input("Type in the year number: ")calendar.prcal(year)

And here is part of the output I got:

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 Su1  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 1115 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

(I skipped some of the output, but I think you get the idea.) So what does the program do? The first lineimport calendar uses a new commandimport. The commandimport loads a module (in this case thecalendar module). To see the commands available in the standard modules either look in the library reference for python (if you downloaded it) or go tohttp://docs.python.org/library/. If you look at the documentation for the calendar module, it lists a function calledprcal that prints a calendar for a year. The linecalendar.prcal(year) uses this function. In summary to use a moduleimport it and then usemodule_name.function for functions in the module. Another way to write the program is:

fromcalendarimportprcalyear=input("Type in the year number: ")prcal(year)

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 terminate the program):

fromtimeimporttime,ctimeprev_time=""whileTrue:the_time=ctime(time())ifprev_time!=the_time:print"The time is:",ctime(time())prev_time=the_time

With some output being:

The time is: Sun Aug 20 13:40:04 2000The time is: Sun Aug 20 13:40:05 2000The time is: Sun Aug 20 13:40:06 2000The time is: Sun Aug 20 13:40:07 2000Traceback (innermost last): File "clock.py", line 5, in ?    the_time = ctime(time())KeyboardInterrupt

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 (True is always true, sowhile True: goes forever) 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 linefrom time import time, ctime.

The Python Library contains many useful functions. These functions give your programs more abilities and many of them can simplify programming in Python.

Exercises

[edit |edit source]

Rewrite the High_low.py program from sectionDecisions to use a random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this.

Solution

Rewrite the High_low.py program from sectionDecisions to use an random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this.

fromrandomimportrandintnumber=randint(0,99)guess=-1whileguess!=number:guess=input("Guess a number: ")ifguess>number:print"Too high"elifguess<number:print"Too low"print"Just right"


Previous: DictionariesFront PageNext: More on Lists
Retrieved from "https://en.wikibooks.org/w/index.php?title=Non-Programmer%27s_Tutorial_for_Python_2.6/Using_Modules&oldid=3676579"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp