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 Buka Situs yang Diblokir di Tahun 2026, Trik Rahasia Tanpa VPN!
  • Inilah Cara Menghilangkan Iklan di HP Xiaomi dan POCO Paling Ampuh Tanpa Root, Update April 2026
  • Inilah 7 Rekomendasi HP 1 Jutaan Terbaik yang Layak Kalian Lirik, Speknya Nggak Kaleng-Kaleng!
  • Inilah 7 Cara Mengembalikan Foto yang Terhapus Permanen di HP, Ternyata Masih Bisa Diselamatkan!
  • Inilah Cara Mengatasi Baterai Boros Setelah Update HyperOS yang Paling Ampuh
  • Inilah Pokémon Champions 2026, Game Battle Kompetitif Terbaru dan Cara Download-nya yang Perlu Kalian Tahu!
  • Inilah Doods Viral: Pengertian, Bahaya, dan Kenapa Kalian Harus Ekstra Waspada!
  • Gini Caranya Ngebangun Bisnis AI yang Menguntungkan dalam 48 Jam Saja!
  • Pengertian “He is Risen” Adalah?
  • Inilah Rekomendasi HP Snapdragon Paling Murah dengan RAM 8 GB Terupdate April 2026
  • Inilah 12 Kampus Negeri di Jogja yang Jarang Diketahui, Ternyata Banyak yang Kasih Kuliah Gratis!
  • Inilah Rekomendasi HP Tecno Kamera Terbaik 2026, Spek Gahar Harga Tetap Pelajar!
  • Apa itu PPU UTBK? Ini Rahasia Taklukkan Skor Tinggi di SNBT 2026 Tanpa Harus Menghafal!
  • Inilah Alasan Kenapa Lapisan Es Greenland Ternyata Bisa Bergerak Kayak Adonan yang Dipanaskan
  • Inilah Kode Redeem FC Mobile 10 April 2026 dan Rahasia Panen Pemain OVR 117
  • 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!
  • 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