Lewati ke konten utama
  1. Posts/

Python: Eksekusi Kode

·405 kata·2 menit· 0 · 0 ·
python
Syahravi
Penulis
Syahravi
Seorang penggemar teknologi dan pengembang perangkat lunak yang membara!! 😄.
Daftar isi
Python Dasar - This article is part of a series.
Part 2: This Article

Python adalah bahasa yang fleksibel, banyak cara kode Python dapat dijalankan.

Ada yang paling mudah itu interpreted rather than compiled.

Dapat berarti bahwa kode bisa dieksekusi line by line.

Pada bagian ini, kita akan memahami Python interpreter, IPython interpreter, Self-contained Scripts, dan Jupyter notebook.


Python Interpreter #

Hal paling dasar == eksekusi kode Python line by line dengan Python interpreter.

Bila kita install Python pada situs resminya (Python.org) (lihat: Python: Introduction), maka sudah ada apa yang disebut Python interpreter ini.

Ketik python pada command prompt (terminal pada Mac OS X dan Unix/Linux) atau pada CMD yang biasa kita kenal pada Windows.

$ python
Python x.x.x (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Dengan berjalannya interpreter, maka kita bisa eksekusi kode line by line.

Kita pahami operator sederhana:

>>> 1 + 1
2
>>> x = 2
>>> x * 4
8

IPython Interpreter #

Jika dirasa Python interpreter belum cukup.

Kita pahami IPython (for Interactive Python).

ketik ipython pada command prompt:

$ ipython
Python x.x.x |Continuum Analytics, Inc.| (default, Vdatetime) 
Type "copyright", "credits" or "license" for more information.

IPython x.x.x -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

Mari kita pahami dengan hal yang sama:

In [1]: 1 + 1
Out[1]: 2

In [2]: x = 2

In [3]: x * 4
Out[3]: 8

Penomeran di atas cukup berguna untuk pemahaman terkait kode yang kita tulis line by line.


Self-contained Python Scripts #

Eksekusi line by line mungkin hanya mainan.

Ada baiknya kita tulis kode sebanyak mungkin lalu kita eksekusi seperlunya.

Yang terpenting, kode yang kita tulis tersimpan di .py extension.

Mari kita buat file bernama hello-world.py dan buka dengan text editor.

# file: hello-world.py
print("Eksekusi hello-world.py")
x = 2
print("Result is", x * 4)

hello-world.py adalah script dan belum dieksekusi.

Eksekusi script dengan ketik python 'filename'

python hello-world.py
Eksekusi hello-world.py
Result is 8

Untuk membuat program yang lebih kompleks ada baiknya gunakan self-contained scripts seperti contoh di atas yaa.


Jupyter notebook #

A useful hybrid dengan interactive terminal dan self-contained script adalah Jupyter notebook.

Sebuah format dokumen yang memungkinkan kita untuk eksekusi kode line by line, dan berbagai fitur canggih lainnya.

Pokoknya mantep dah, paket komplit.

Lihat selengkapnya di Jupyter Project

Jupyter
Try Jupyter » https://jupyter.org/try

Python Dasar - This article is part of a series.
Part 2: This Article

Related

Python: User Input
·203 kata·1 menit· 0 · 0
python
Python: Virtual Environments
·233 kata·2 menit· 0 · 0
python
Python Dasar : Mengenal dan Menggunakan Bahasa Pemrograman Python
·524 kata·3 menit· 0 · 0
python
Menampilkan Teks Berwarna pada Python
·211 kata·1 menit· 0 · 0
python
Python: Manipulasi Datetime
·248 kata·2 menit· 0 · 0
python
Cara Membuat Github Project
·254 kata·2 menit· 0 · 0
edukasi-teknologi github