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 Menggabungkan Grid Foto Online dan Hapus Background Foto
  • Kenapa Youtuber Mulai Harus Hati-hati Pakai AI: Bisa Digugat dan Kehilangan Hak Cipta!
  • Inilah Alasan Kenapa Sumbangan ke Tempat Ibadah Biasa Nggak Bisa Langsung Jadi Pemotong Pajak!
  • Belum Tahu? Inilah Cara Pasang Iklan Meta Ads untuk Sales WiFi Supaya Banjir Closingan!
  • Inilah Alur Pengerjaan EMIS GTK 2026 yang Benar dari Awal Sampai Akhir
  • Inilah 27 Sekolah Kedinasan untuk Lulusan SMK 2026, Bisa Kuliah Gratis dan Langsung Jadi CPNS!
  • Inilah Cara Kuliah S2 di Inggris dengan GREAT Scholarship 2026: Syarat Lengkap, Daftar Kampus, dan Tips Jitu Biar Lolos!
  • Belum Tahu? Inilah Alasan Non-Muslim Juga Bisa Ngurangin Pajak Pake Sumbangan Keagamaan Wajib!
  • Inilah Kenapa Zakat ke Pondok Pesantren Mungkin Nggak Bisa Jadi Pengurang Pajak, Yuk Cek Syaratnya!
  • Inilah Caranya Daftar SMA Unggul Garuda Baru 2026 yang Diperpanjang, Cek Syarat dan Link Resminya!
  • Cara Cek Pencairan KJP Plus Tahap 1 Januari 2026 Beserta Daftar Nominal Lengkapnya
  • Lengkap! Inilah Kronologi Meninggalnya Vidi Aldiano Berjuang Melawan Kanker
  • Inilah Cara Tarik Data PKH di EMIS 4.0 Agar Bantuan Siswa Tetap Cair!
  • Inilah Trik Jitu SEO Shopee untuk Pemula: Jualan Laris Manis Tanpa Perlu Bakar Duit Iklan!
  • Inilah Peluang Emas Jadi Karyawan BUMN Tanpa Ngantre: Program Ikatan Kerja ULBI 2026
  • Inikah Daftar CPNS Kemenkeu 2026? Cek 48 Jurusan yang Paling Dibutuhkan!
  • Inilah 4 Beasiswa Khusus Warga ASEAN dengan Peluang Lolos Lebih Tinggi, Kalian Wajib Tahu!
  • Inilah Alasan Ribuan Dosen ASN Melayangkan Surat Keberatan Soal Tukin 2020-2024 yang Belum Cair
  • Cara Dapat Diamond Free Fire Gratis 2026, Pemain FF Harus Tahu!
  • Inilah Cara Mengisi Presensi EMIS GTK IMP 2026 Terbaru Biar Tunjangan Lancar
  • Inilah Trik Hashtag Viral Supaya Video Shorts Kalian Nggak Sepi Penonton Lagi
  • Inilah Jawabannya, Apakah Zakat Fitrah Kalian Bisa Mengurangi Pajak Penghasilan?
  • Inilah Caranya Supaya Komisi TikTok dan Shopee Affiliate Tetap Stabil Pasca Ramadhan!
  • Inilah 10 Kesalahan Fatal Saat Beli Properti yang Bisa Bikin Kalian Bangkrut!
  • Belum Tahu Cara Masuk Simpatika Terbaru? Ini Cara Login PTK EMIS GTK IMP 2026 Supaya Cek TPG Jadi Lebih Gampang!
  • Inilah Cara Bikin Konten Animasi AI Cuma Modal HP Supaya Bisa Gajian Rutin dari YouTube
  • Inilah Alasan Kenapa Zakat ke Ormas yang Belum Diakui Negara Nggak Bisa Dipakai Buat Ngurangin Pajak!
  • Inilah Cara Belanja di Indomaret Pakai Shopee PayLater yang Praktis dan Bikin Hemat!
  • Inilah 10 Jurusan Terfavorit di Universitas Negeri Semarang Buat SNBT 2026, Saingannya Ketat Banget!
  • Belum Tahu? Inilah Cara Mudah Membuat Akun dan Login EMIS GTK IMP 2026 yang Benar!
  • How to Setting Up a Pro-Level Security System with Reolink and Frigate NVR
  • How to Install DaVinci Resolve on Nobara Linux and Fix Video Compatibility Issues Like a Pro
  • How to Master GitHub’s New Power Tools: Copilot CLI, Dashboards, and More!
  • How to Create and Configure DNS Server on RHEL 10
  • How a Security Professional Bypassed a High-Security Building Using Just a Smartphone and a QR Code
  • How to Build Your First AI App with Lovable AI Today!
  • OpenClaw Tutorial: A Step-by-Step Guide to Coding Your Very First Website from Scratch!
  • Seedance 2.0 Is Here! Unlimited + Completely Uncensored AI Video Gen
  • A Step-by-Step Guide to the Qwen 3.5 Small Model Series
  • What new in Google’s Workspace CLI?
  • 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 Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ

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