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

  • Kenapa Indomart Point Bisa Kalahkan Bisnis Kafe?
  • Inilah Cara Mendapatkan Rotten Seed di Fisch Roblox, Lokasi Rahasia di Toxic Grove Buat Unlock Toxic Lotus!
  • Inilah Cara Zakat Crypto Kalian Bisa Jadi Pengurang Pajak Berdasarkan Aturan Resmi Pemerintah!
  • Inilah Perbandingan Airwallex vs Payoneer 2026: Jangan Sampai Profit Kalian Ludes Gara-Gara Biaya Admin!
  • Inilah Roadmap 7 Tahap Bangun Bisnis Digital dari Nol Biar Nggak Cuma Putar-Putar di Tempat!
  • Inilah Cara Tetap Gajian dari YouTube Meski View Masih Ratusan, Penasaran?
  • Inilah Alasan Akun TikTok Affiliate GMV 270 Juta Kena Banned Permanen!
  • Inilah Bahaya Astute Beta Server APK, Jangan Sembarang Klik Link Download FF Kipas 2026!
  • Inilah Bahaya Nonton Film di LK21 dan IndoXXI, Awas Data Pribadi dan Saldo Rekening Kalian Bisa Ludes!
  • Inilah Kronologi & Video Lengkap Kasus Sejoli Tambelangan Sampang Viral, Ternyata Gini Awal Mulanya!
  • Inilah Alasan Kenapa Koin Nego Neko Shopee Nggak Bisa Dipakai Bayar Full dan Cara Rahasia Dapetinnya!
  • Inilah Cara Menjawab Pertanyaan Apakah di Sekolahmu Sudah Ada IFP/PID dengan Benar dan Profesional
  • Inilah Fakta Isu Roblox Diblokir di Indonesia 2026, Benarkah Akan Ditutup Total?
  • Inilah Penyebab dan Cara Mengatasi FF Kipas My ID Verify UID Biar Akun Tetap Aman
  • Inilah Deretan HP RAM 8GB Harga di Bawah 2 Juta Terbaik 2026, Spek Dewa Tapi Nggak Bikin Kantong Jebol!
  • Contoh Cara Buat SK Panitia TKA 2026
  • Inilah Cara Download Point Blank ID Versi Terbaru 2026, Gampang Banget Ternyata!
  • Inilah Persiapan Lengkap Gladi Bersih TKA 2026 SD dan SMP: Jadwal, Teknis Proktor, dan Aturan yang Wajib Kalian Tahu
  • Inilah Aturan Lengkapnya, Apakah Zakat dalam Bentuk Barang Bisa Mengurangi Pajak Penghasilan Kalian?
  • Inilah Kenapa KBLI Sangat Penting Buat Bisnis Digital dan Gini Caranya Biar Kalian Nggak Salah Pilih Kode
  • Inilah Fitur Keren ONLYOFFICE Docs 9.3, Cara Baru Edit PDF dan Dokumen Lebih Efisien!
  • Inilah Cara Banjir Komisi Shopee Affiliate Hanya Dalam 7 Hari Saja
  • Cara Download Aplikasi BUSSID Versi 3.7.1 Masih Dicari dan Link Download Aman Mediafire
  • Inilah Cara Lengkap Mengajukan SKMT dan SKBK di EMIS GTK 2026, Biar Tunjangan Cair Tanpa Drama!
  • Inilah Kenapa Bukti Setor Zakat Kalian Harus Ada NPWP-nya, Jangan Sampai Klaim Pajak Ditolak!
  • Inilah Cara Jadi Clipper Video Sukses Tanpa Perlu Tampil di Depan Kamera
  • Inilah Cara Upload NPWP dan Rekening di EMIS GTK Madrasah Terbaru, Jangan Sampai Tunjangan Terhambat!
  • Inilah Fakta di Balik Video Ukhti Sholat Mukena Pink Viral yang Bikin Geger Media Sosial
  • Belum Tahu? Inilah Langkah Pengisian Survei Digitalisasi Pembelajaran 2026 Biar Nggak Salah
  • Ini Kronologi Video Viral Ibu Tiri vs Anak Tiri Ladang Sawit Tiktok
  • How to Master Cloud Infrastructure with Ansible and Terraform
  • How to Fix VirtualBox Stuck on Saving State: A Complete Guide
  • How to Run Windows Apps on Linux: A Complete Guide to WinBoat, WINE, and Beyond
  • Build Your Own AI Development Team: Deploying OpenClaw and Claude Code on a VPS!
  • How to Measure Real Success in the Age of AI: A Guide to Software Metrics That Actually Matter
  • How to LLM Finetuning with FPT AI Factory
  • New ComfyUI Released, The App Mode is Amazing
  • How to Master Kilo Code for VS Code: Use Parallel AI Agents to Build Software Faster than Ever Before
  • 4 Massive Claude Code 2.0 Updates You Must Master
  • How to Build Your Own Website from Scratch: A Beginner’s Guide to Mastering HTML and CSS
  • 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

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