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

  • Apa itu Benwit/Bensin Sawit? Benarkah Bisa Jadi Solusi Bahan Bakar Masa Depan atau Cuma Hoaks Belaka?
  • Inilah 4 Tablet 5G Termurah April 2026 yang Kencang dan Worth It untuk Kerja!
  • Inilah Rincian UKT Unesa 2026 Jalur SNBP dan SNBT, Cek Biaya Kuliahmu di Sini!
  • Inilah Bocoran Event Free Fire 10 April 2026, Ada Diskon 90 Persen dan Kode Redeem Gratis!
  • Inilah Deretan HP Murah April 2026, dari Infinix NFC Hingga Realme dengan Baterai Super Besar dan Update Penting IGRS
  • Inilah Alasan Google Kena Sanksi Teguran Terkait PP Tunas dan Perlindungan Anak di YouTube
  • Inilah Spesies Baru Homalomena dari Sumatera yang Berhasil Diidentifikasi Melalui Media Sosial
  • Inilah Cara Download FF Advance Server 2026 Apk yang Aman dan Update Misteri Bawah Laut Terbaru!
  • Inilah 10 PTS Terbaik di Indonesia Versi Uniranks 2026, Referensi Mantap Buat Kalian Calon Mahasiswa Baru!
  • Inilah Daftar Lengkap Pusat UTBK 2026 di Jawa Tengah, Cek Lokasi dan Alamat Kampusnya Biar Nggak Salah Alamat!
  • Inilah Alasan Kenapa Hasil TKA Jadi Kunci Penting di Jalur Prestasi SPMB 2026, Orang Tua Wajib Tahu!
  • Inilah Alasan Kenapa Situs Bumiayu Dianggap Lebih Tua dari Sangiran dan Jadi Kunci Sejarah Jawa
  • Inilah Cara Cerdik Larva Kumbang Hitam Eropa Meniru Aroma Bunga untuk Menipu Lebah
  • Inilah 45 Planet Berbatu yang Paling Berpotensi Jadi Rumah Kedua Manusia di Masa Depan
  • Inilah Cara Ambil Kursus Online Gratis dari Harvard University untuk Asah Skill Digital Kalian!
  • Inilah Rincian UKT UGM 2026 Jalur SNBP dan SNBT, Calon Mahasiswa Wajib Tahu!
  • Inilah Daftar 25 Universitas Terbaik di Inggris 2026 Versi The Guardian yang Wajib Kalian Lirik!
  • Inilah Alasan University of Oxford Masih Jadi Juara Dunia di QS Rankings 2026, Empat Jurusan Ini Nggak Ada Lawannya!
  • Inilah 17 Rahasia Tips Sukses dari Produk Digital, Yuk Simak!
  • Inilah Peluang Lolos SNBT Unair 2026, Cek Keketatan Jurusan Pilihanmu Sekarang!
  • Hapus PR! China Baru Saja Rombak Aturan Sekolah, Inilah Caranya Mereka Ngurangin Beban Stres Siswa
  • Inilah Daftar Negara Paling Sopan di Dunia, Ternyata Jepang Juaranya Bukan Eropa!
  • Inilah Alasan Kenapa Ada Gurun yang Letaknya Malah di Pinggir Laut, Kok Bisa?
  • Inilah Cara Mengajukan bluExtraCash BCA Digital, Pinjaman Online Tanpa Agunan yang Bisa Cair Kilat Sampai 50 Juta
  • Inilah Cara Mengisi Berita Acara Pengecekan Penyimpanan Eksternal Bantuan Pemerintah Lewat CrystalDiskInfo, Gampang Banget!
  • Belum Tahu? Inilah Cara Pasang FF Kipas Org OB52 Apk Biar Nggak Mentok Logo dan Bisa Jadi Sultan!
  • Belum Tahu? Inilah FF Kipas Custom Free 32 Bit APK, Solusi Lag atau Malah Bikin Masalah?
  • Inilah Daftar Kode Redeem Blox Fruits Terbaru dan Cara Pakainya Biar Cepat Level Max!
  • Ini Trik Supaya YouTube Shorts Kalian Ranking 1 di Pencarian
  • Inilah Daftar Kode Redeem Fish It Roblox Terbaru April 2026 dan Cara Klaimnya Biar Mancing Makin Gacor!
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Use VoxCPM2: The Complete Tutorial for Professional Voice Cloning and AI Speech Generation
  • Complete tutorial for Creao AI: How to build smart AI agents that automate your daily tasks
  • How to Streamline Your Digital Workflow with TeraBox AI: A Complete Tutorial for Beginners
  • How to Run Google Gemma 4 Locally: A Beginner’s Guide to Tiny but Mighty AI Models
  • A Beginner Tutorial on Cloning Website Source Code Using ChatGPT and AI Logic Reconstruction
  • 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