Python & WxPython

14/07/10

Interpreter Yang Interaktif

Salah satu kelebihan python adalah python memiliki interpreter yang interaktif. Hal ini sangat membantu programmer untuk mencoba berbagai syntax sebelum menulisnya kedalam script program.


Code python dapat dijalankan dengan 2 cara, yaitu dijalankan sebagai script dan dijalankan didalam interpreter interaktif python.

Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>


Pesan diatas berada di paling awal dari interpreter. Kita dapat melihat informasi versi python yang digunakan. Untuk keluar dari interpreter, ketik Ctrl+D / Ctrl+Q atau menggunakan perintah quit(). Sekarang saatnya kita mencobanya …

HELP()

Untuk menampilkan bantuan informasi kita dapat menggunakan perintah help(). Perintah help() dapat digunakan dengan 2 cara, yaitu dengan menggunakannya beserta object yang diinginkan, contohnya help(int). Kedua adalah dengan mengetikan perintah help() didalam interpreter yang akan merubah mode interpreter ‘>>>’ menjadi mode ‘help>’.

Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> help()

Welcome to Python 2.7! This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help>

Setelah kita berada dalam mode ‘help>’ kita dapat langsung menggunakannya dengan memasukan keywords atau object yang diinginkan. Contohnya adalah keywords.

help> keywords

Here is a list of the Python keywords. Enter any keyword to get more help.

and  elif if print
as else import raise
assert except in return
break exec is try
class finally lambda while
continue for not with
def from or yield
del global pass

help>

Jika kita mengetik salah satu keywords diatas, maka interpreter akan memberikan informasi yang bersangkutan dengan keywords tersebut. Contohnya adalah if.

help> if
The ``if`` statement
********************

The ``if`` statement is used for conditional execution:

if_stmt ::= "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section *Boolean operations*
for the definition of true and false); then that suite is executed
(and no other part of the ``if`` statement is executed or evaluated).
If all expressions are false, the suite of the ``else`` clause, if
present, is executed.

Related help topics: TRUTHVALUE

help>

Disamping keywords kita juga dapat mendapatkan informasi tentang topics. Untuk mengetahui macam-macam topics, cukup dengan mengetikan perintah topics kedalam mode ‘help>’

help> topics

Here is a list of available topics. Enter any topic name to get more help.

ASSERTION DEBUGGING LITERALS SEQUENCEMETHODS2
ASSIGNMENT DELETION LOOPING SEQUENCES
ATTRIBUTEMETHODS DICTIONARIES MAPPINGMETHODS SHIFTING
ATTRIBUTES DICTIONARYLITERALS MAPPINGS SLICINGS
AUGMENTEDASSIGNMENT DYNAMICFEATURES METHODS SPECIALATTRIBUTES
BACKQUOTES ELLIPSIS MODULES SPECIALIDENTIFIERS
BASICMETHODS EXCEPTIONS NAMESPACES SPECIALMETHODS
BINARY EXECUTION NONE STRINGMETHODS
BITWISE EXPRESSIONS NUMBERMETHODS STRINGS
BOOLEAN FILES NUMBERS SUBSCRIPTS
CALLABLEMETHODS FLOAT OBJECTS TRACEBACKS
CALLS FORMATTING OPERATORS TRUTHVALUE
CLASSES FRAMEOBJECTS PACKAGES TUPLELITERALS
CODEOBJECTS FRAMES POWER TUPLES
COERCIONS FUNCTIONS PRECEDENCE TYPEOBJECTS
COMPARISON IDENTIFIERS PRINTING TYPES
COMPLEX IMPORTING PRIVATENAMES UNARY
CONDITIONAL INTEGER RETURNING UNICODE
CONTEXTMANAGERS LISTLITERALS SCOPING
CONVERSIONS LISTS SEQUENCEMETHODS1

help>

Perintah topics memberikan informasi yang berguna kepada kita mengenai bahasa pemrograman python.

Code Python

Sekarang saatnya kita mencoba code python didalam interpreter yang interaktif

>>> 156 + 23
179
>>> 333 - 123
210
>>> 13 * 10
130
>>> 100 / 25
4
>>>

Selain berguna untuk proses kalkulasi, interpereter juga dapat menjalankan ekspresi yang hasilnya akan langsung ditampilkan.

>>> a = 5
>>> b = 2
>>> a*b
10
>>> a**b
25
>>> a == b
False
>>> a > b
True
>>> a = b
>>> a
2
>>>

Selain itu, interpreter juga dapat menjalankan fungsi dan proses looping.

>>> for x in range(10):
            print x

0
1
2
3
4
5
6
7
8
9

>>> def namaFungsi():
a = 10
b = 74
c = a - b
print c

>>> namaFungsi()
-64
>>>

Selamat mencoba...

Tidak ada komentar:

Posting Komentar