Tutorial Python untuk bukan pengatur cara/Mengguna Modul: Perbezaan antara semakan

Daripada Wikibooks
Kandungan dihapus Kandungan ditambah
Algazel (bincang | sumb.)
Permulaan penterjemahan
 
Algazel (bincang | sumb.)
Tiada ringkasan suntingan
Baris 23: Baris 23:
{{Nota penterjemah|Ingat, Python hanya boleh berbahasa Inggeris. Itu sebab dalam outputnya, Python mengeja bulan dan hari dalam bahasa itu}}
{{Nota penterjemah|Ingat, Python hanya boleh berbahasa Inggeris. Itu sebab dalam outputnya, Python mengeja bulan dan hari dalam bahasa itu}}


(I skipped some of the output, but I think you get the idea.) So what does the program do? The first line <code>import calendar</code> uses a new command <code>import</code>. The command <code>import</code> loads a module (in this case the <code>calendar</code> 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 http://docs.python.org/library/. If you look at the documentation for the calendar module, it lists a function called <code>prcal</code> that prints a calendar for a year. The line <code>calendar.prcal(year)</code> uses this function. In summary to use a module <code>import</code> it and then use <code>module_name.function<code> for functions in the module. Another way to write the program is:
(Saya mengabaikan sebahagian daripada output tapi saya fikir anda dapat bayangannya.) Jadi, apa yang dilakukan oleh program ini? Baris pertama <code>import calendar</code> mengguna perintah baharu, <code>import</code>. Perintah <code>import</code> memuatkan modul (dalam kes ini modul <code>calendar</code>). Untuk melihat perintah-perintah yang terdapat dalam modul-modul standard, lihat dalam rjukan perpustakaan python (sekiranya anda memuat turunnya) atauapun pergi ke to http://docs.python.org/library/. Jika anda melihat dokumentasi bagi modul <code>calendar</code>, ia menyenaraikan sebuah fungsi bergelar <code>prcal</code> yang mencetak kalendar bagi setahun. Baris <code>calendar.prcal(year)</code> mengguna fungsi ini. Secara ringkas, untuk mengguna modul, guna <code>import</code> untuk mengimportnya dan kemudian guna <code>module_name.function<code> bagi fungsi-fungsi dalam modul itu. Cara lain untuk menulis program ini ialah:


<source lang="python">
<source lang="python">
from calendar import prcal
from calendar import prcal


year = input("Type in the year number: ")
tahun = input("Taipkan nombor tahun: ")
prcal(year)
prcal(tahun)
</source>
</source>



Semakan pada 11:22, 7 Februari 2013

Ini latihan menaip bab ini (namakannya cal.py (import sebenarnya mencari fail bernama calender.py dan membacanya ke dalam program. Jika fail ini dinamakan calender.py dan ia melihata "import calendar" ia akan cuba membaca dirinya, yang tidak dapat dilakukannya dengan baik). :):

import calendar
tahun = input("Taipkan nombor tahun: ")
calendar.prcal(tahun)

Dan ini sebahagian output yang saya dapat:

Taipkan nombor tahun: 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        
TODO
TODO

Nota penterjemah:
Ingat, Python hanya boleh berbahasa Inggeris. Itu sebab dalam outputnya, Python mengeja bulan dan hari dalam bahasa itu

(Saya mengabaikan sebahagian daripada output tapi saya fikir anda dapat bayangannya.) Jadi, apa yang dilakukan oleh program ini? Baris pertama import calendar mengguna perintah baharu, import. Perintah import memuatkan modul (dalam kes ini modul calendar). Untuk melihat perintah-perintah yang terdapat dalam modul-modul standard, lihat dalam rjukan perpustakaan python (sekiranya anda memuat turunnya) atauapun pergi ke to http://docs.python.org/library/. Jika anda melihat dokumentasi bagi modul calendar, ia menyenaraikan sebuah fungsi bergelar prcal yang mencetak kalendar bagi setahun. Baris calendar.prcal(year) mengguna fungsi ini. Secara ringkas, untuk mengguna modul, guna import untuk mengimportnya dan kemudian guna module_name.function bagi fungsi-fungsi dalam modul itu. Cara lain untuk menulis program ini ialah:

from calendar import prcal

tahun = input("Taipkan nombor tahun: ")
prcal(tahun)

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):

from time import time, ctime

prev_time = ""
while True:
    the_time = ctime(time())
    if prev_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 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

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, so while 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 line from 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

Rewrite the High_low.py program from section Decisions 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.

Templat:Solution

Tutorial Python untuk bukan pengatur cara/Navigation