Difference between revisions of "Python: Penggunaan Interpreter"

From OnnoWiki
Jump to navigation Jump to search
Line 5: Line 5:
 
==Mode Interaktif==
 
==Mode Interaktif==
  
When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:
+
Jika command di baca dari tty, interpreter berada di mode interaktif. Dalam mode interaktif, command selanjutnya biasanya >>>; untuk line selanjutnya
 +
 
 +
In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:
  
 
  $ python3.6
 
  $ python3.6

Revision as of 10:26, 13 February 2017

Passing Argument

Ketika diketahui oleh interpreter, nama script dan argumen tambahan akan di ubah menjadi list string dan assign variabel argv dalam modul sys. Kita dapat mengaksis list tersebut dengan mengeksekusi import sys. Panjang string paling tidak satu; jika tidak ada script dan tidak ada argumen yang diberikan, sys.argv[0] adalah string kosong. Jika nama script yang diberikan adalah '-' (artinya standard input), sys.argv[0] di set menjadi '-'. Jika perintah -c digunakan, sys.argv[0] di set menjadi '-c'. Jika modul -m digunakan, sys.argv[0] di set ke full name dari located module. Opsi yang ada setelah perintah -c atau modul -m tidak akan di ambil oleh Python interpreter option processing tapi masuk ke sys.argv untuk di tangani oleh command atau module.

Mode Interaktif

Jika command di baca dari tty, interpreter berada di mode interaktif. Dalam mode interaktif, command selanjutnya biasanya >>>; untuk line selanjutnya

In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:
$ python3.6
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Continuation lines are needed when entering a multi-line construct. As an example, take a look at this if statement:

>>>
>>> the_world_is_flat = True
>>> if the_world_is_flat:
...     print("Be careful not to fall off!")
...
Be careful not to fall off!

For more on interactive mode, see Interactive Mode.