Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • 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

  • Inilah Cara Mengubah Lahan Kosong Jadi Uang Lewat Strategi Land Banking
  • Ini Trik Supaya Gajian YouTube Shorts Tembus Puluhan Juta dari Penonton Bule!
  • Jangan Sampai Keliru! Begini Cara Cek Total Jam Linear dan Non Linear di EMIS GTK IMP 2026
  • Cuma Nonton Drama Pendek Bisa Cair Uang Tunai? Bongkar Habis Kebenaran Aplikasi FunFlick di Sini!
  • Apa itu Resetter Epson L3210?
  • Ini Loh Ukuran A4, F4, A3, B5, A5 di Canva Biar Hasil Cetakan Kalian Nggak Terpotong!
  • Inilah Cara Transfer Pulsa Telkomsel Paling Update 2026, Lengkap dengan Biaya dan Syarat Terbarunya!
  • Inilah Xolo.io, Solusi Praktis Buat Kalian yang Pengen Bangun Startup Skala Global Tanpa Harus Pindah ke Luar Negeri
  • Inilah Caranya Lapor SPT Tahunan Lewat Coretax Supaya Nggak Kena Denda
  • Inilah Alasan Kenapa Software House Lokal Susah Dapat Insentif Pajak R&D dan Isu Amortisasi Pegawai yang Bikin Pusing
  • Inilah Alasan Kenapa Developer Game Indonesia Lagi Curhat Soal Pajak: Kasus Toge Productions
  • Inilah Alasan Ilmiah Kenapa Lampu Lalu Lintas Pakai Warna Merah, Kuning, dan Hijau!
  • Nonton Drama Dapat Duit? Ini Kebenaran Soal FunFlick yang Wajib Kalian Tahu!
  • Ini Cara Isi NPWP dan Rekening di EMIS GTK IMP 2026 Biar Tunjangan Lancar
  • Inilah Trik Farming AFK Roblox Fish It 24 Jam Tanpa Bikin HP Panas!
  • Hobi Nonton Drama Jadi Cuan? Ini Cara Ngebanyakin Diamond FreeReels Tanpa Ribet!
  • Panduan Lengkap Ukuran Banner Canva untuk Desain yang Menarik
  • Apakah ViaPaypal.id Penipuan?
  • Beda BRIVA dan Rekening? Ini Penjelasannya!
  • Pahami Perbedaan Kode SIEX, SIPX, dan SISX dengan Mudah!
  • Arti SPT Sebelumnya Tidak Ada dari BPS yang Perlu Kalian Pahami
  • Kode Error 205 di BCA Mobile: Penyebab dan Solusi Lengkap
  • Solusi Cepat Saat Voucher Axis Tidak Bisa Diproses
  • Qris BCA Error? Ini Solusi yang Bisa Kalian Coba
  • Blokir Nomor WA Tanpa Harus Tambah ke Daftar Hitam, Begini Caranya!
  • Isu SKTP Februari 2026 Sudah Terbit Ternyata Cuma Hoaks? Cek Jadwal Resminya Di Sini
  • Apa itu Mihari Novel? Aplikasi Baca Novel Dibayar
  • Cara Mengatasi NIK Belum Ditemukan di DTKS Saat Daftar KIP Kuliah, Jangan Panik Dulu!
  • Inilah 3 Karakteristik Pembagian Masyarakat Menurut Sibrani yang Bikin Kita Paham Struktur Sosial
  • Inilah Cara Mengatasi Status Bansos Atensi YAPI NTPN Tidak Ditemukan Biar Bantuan Tetap Cair!
  • What is Docker Digest Watching? Understanding the New Standard in Docker 8.2
  • What is LibreOffice Online? A Guide to the Community-Driven Cloud Office Suite
  • Lutris 0.5.21 Adds Steam Sniper Runtime & New Emulators: What’s New?
  • Wine 11.3 Released: New Features and Improvements Explained
  • How to Discover Hidden Risks in Arch AUR Packages with Taur, the New Rust Tool
  • Prompt AI Menyusun Script Pola Suara Karakter agar Brand Jadi Ikonik
  • Prompt AI untuk Merancang Karakter Brand yang Ikonik
  • Prompt AI Audit Konten Sesuai Karakter Brand
  • Prompt AI Merubah Postingan LinkedIn Jadi Ladang Diskusi dengan ChatGPT
  • Prompt AI: Paksa Algoritma LinkedIn Promosikan Konten Kalian
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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