Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Makalah
  • Ke-NU-an
  • Kabar
  • Search
Menu

How to Executing Python Code

Posted on April 2, 2012

Before we can really explore the Python language we need to know how to execute Python code. We will show this by reviewing a tiny example program that is just one line long.

We must use a plain text editor for working with Python files.★ On Windows it is possible to use Notepad, but IDLE includes a suitable Python editor designed specifically for editing Python code: Simply start IDLE and then click File→New Window.

We will type the following line into a file, called hello.py:

[code lang=”python”]print "Hello World"[/code]

Note that no semicolon is necessary: In Python newline acts as a statement separator. Also, we do not need a newline, “\n”, in the string, since print automatically adds a newline unless we suppress it with a trailing comma.

Assuming that we have saved the code in the file hello.py (in the directory C:\pyqt\chap01 if using Windows), we can start up a console (click Start→All Pro- grams→Accessories→Console on Windows XP—sometimes Console is called Command Prompt; or run Terminal.app from /Applications/Utilities on Mac OS X), change to that directory, and execute the program like this:

[code lang=”bash”]C:\>cd c:\pyqt\chap01
C:\pyqt\chap01>hello.py[/code]

As long as Python is correctly installed, Windows will recognize the .py file extension and give the file to python.exe to execute. The program will print “Hello World” on the console as we would expect.★

On Mac OS X and Linux we must explicitly run the interpreter by typing its name and the file’s name at the console’s prompt, like this:

[code lang=”python”]% python hello.py[/code]

This will work providing that Python is installed and in your PATH. Alternatively, for Linux and Mac OS X, we can add an additional “shebang” (shell execute) comment line which tells the operating system to use a Python interpreter, making the hello.py file two lines long:

[code lang=”python”]#!/usr/bin/env python print "Hello World"[/code]

For this to work on Mac OS X and Linux, the file’s permissions must be set correctly. For example, at the console prompt in the same directory as the file, enter chmod +x hello.py to make the file executable.

Python comments start with “#” and continue until the end of the line. This means that it is perfectly safe to add the “shebang” line to all Python programs, since the comment is ignored on Windows but on Linux it tells the operating system to execute the file using a Python interpreter. Appendix A shows how to associate the Python interpreter with .py and .pyw files on Mac OS X.

When we speak of executing a Python program, what happens behind the scenes is that Python reads the .py (or .pyw) file into memory, and parses it, to get a bytecode program that it then goes on to execute. For each module that is imported by the program, Python first checks to see whether there is a precom piled bytecode version (in a .pyo or .pyc file) that has a timestamp which corresponds to its .py file. If there is, Python uses the bytecode version; otherwise, it parses the module’s .py file, saves it into a .pyc file, and uses the bytecode it just generated. So, unlike Java, we don’t have to explicitly bytecode-compile any modules, whether they are supplied with Python or are ones we have written ourselves. And in most Python installations, the supplied modules are compiled as part of the installation process so as to avoid having to compile them whenever a Python application that uses them is run.

Now, It’s easy Python toh?

Terbaru

  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual
  • Rusia Memblokir Roblox Karena Distribusi ‘Propaganda LGBT’
  • Google Gemini Redesain Web Total di Desember 2025, Fokus UX yang Lebih Baik
  • Apa itu Google Workspace Studio? Tool Baru untuk Pembuat Konten?
  • Cara Menggunakan Xbox Full-Screen Experience di Windows
  • Korea Tahan Tersangka Terkait Penjualan Video Intim dari Kamera CCTV yang Diretas
  • Kebocoran Galaxy Buds 4 Mengungkap Desain dan Fitur Baru, Mirip Apple?
  • Sudah Update Windows KB5070311 dan Apa Saja Yang Diperbaiki?
  • Cara Menonaktifkan Fitur AI Actions (Tindakan AI) di Menu Windows Explorer
  • Microsoft Edge AI vs. OpenAI’s Atlas Browser: Perbandingan dan Perbedaan Utama
  • Cara Memasang Folder Sebagai Drive di Windows 11
  • Cara Memperbaiki Error 0xC1900101 0x40021 pada Update Windows 11
  • Malware Glassworm Serang Lagi VSCode, Hati-hati!
  • Walmart dan Google Bermitra untuk Kamera Rumah Google Home: Pengalaman Langsung
  • Gemini Dapat Bisa Atur Perangkat Rumah Melalui Home Assistant Pakai Suara, Desember 2025
  • Asahi, Produsen Bir Jepang, Akui Kebocoran Data 15 Juta Pelanggan
  • Google Messages Ada Fitur Baru: Pesan Grup, Mode Gelap dan Integrasi dengan Google Duo
  • 5 Laptop ASUS Terbaik dengan Tampilan Mewah dan Build Quality Premium
  • Pria di Balik Serangan ‘Twin Wifi’ Mencuri Wifi, Dikenakan Hukuman 7 Tahun Penjara
  • Google Kembangkan Fitur Baru untuk Tugas di Keep, Lebih Terintegrasi dengan Kalender
  • Google Akan Luncurkan Laptop dan Ponsel Android Baru di Tahun 2025: Murah & Spesifikasi Tinggi
  • Samsung Galaxy Z-Fold Tri-Fold: Harga dan Spesifikasi Resmi Terungkap
  • Federasi Sepak Bola Prancis (FFF) Mengungkap Pelanggaran Data Setelah Serangan Siber
  • Google Perbarui Desain Akun Google Web, Fokus pada Kemudahan Penggunaan dan Integrasi
  • Google Tingkatkan Batas Gratis Gemini 3 Pro untuk Pengembang dan Bisnis
  • Google Perkenalkan ‘Circle to Search’: Cara Baru Menggunakan AI untuk Pencarian
  • OpenAI Terpapar Data Pelanggan Melalui Pelanggaran Vendor Mixpanel, API Terpengaruh
  • Error External Drive Extraction Tidak Terdeteksi di VM Virtual Hyper-V
  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual

©2025 emka.web.id | Design: Newspaperly WordPress Theme