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 5 Rekomendasi Sepeda Lipat Listrik Jarak Tempuh Terjauh Biar Mobilitas Kalian Makin Praktis!
  • Inilah 30 Ucapan Hari Kartini 2026 Paling Bermakna untuk Caption Media Sosial dan Pesan Personal yang Inspiratif
  • Inilah Cara Daftar Manajer Koperasi Merah Putih, Peluang Karir BUMN dengan 30.000 Formasi
  • Inilah 5 Rekomendasi Bedak Two Way Cake Anti Oksidasi Supaya Wajah Nggak Terlihat Kusam dan Gelap
  • Inilah Syarat dan Cara Mengurus Surat Sehat Jasmani Rohani untuk Daftar Manajer Kopdes Merah Putih
  • Inilah Alasan Harga Plastik Naik dan 7 Alternatif Kemasan Ramah Lingkungan yang Lebih Hemat buat UMKM
  • Inilah Profil Syekh Ahmad Al Misry dan Rekam Jejaknya yang Sedang Viral di Media Sosial
  • Inilah Alasan HP Nokia Jadul Harganya Masih Selangit dan Banyak Diburu Kolektor di Tahun 2026
  • Inilah HP Samsung A Series Terbaru yang Pakai Layar AMOLED, Mewah Tapi Harganya Terjangkau Banget!
  • Inilah Vivo T5 Pro, HP Midrange dengan Baterai 9.020 mAh yang Siap Comeback ke Indonesia!
  • Inilah Cara Daftar Manajer Koperasi Desa Merah Putih 2026, Ada 30.000 Formasi yang Bisa Kalian Lamar!
  • Inilah Alasan Kenapa Kemkomdigi Ancam Blokir Wikipedia dan Dampak Seriusnya Buat Masa Depan Pendidikan Kita
  • Inilah Cara Cek BLT Desa 2026 Tahap 2, Cair Rp900 Ribu Langsung ke Tangan Kalian!
  • Inilah Cara Cek PIP Lewat HP 2026 dan Panduan Lengkap Pencairannya Biar Dana Nggak Hangus
  • Inilah 51 Kode Redeem FF Aktif 17 April 2026, Buruan Klaim Skin Blue Angelic dan Bundle Langka!
  • Inilah Huawei Mate 80 Pro, Sang Legenda Flagship yang Kembali dengan Kamera True-to-Colour dan Ketahanan Luar Biasa
  • Inilah Deretan iPhone Terlawas yang Masih Layak Dibeli Tahun 2026, Performa Tetap Ngebut dan Harga Terjangkau!
  • Inilah HP Oppo Paling Worth It di Tahun 2026, Spesifikasi Gahar Tapi Harga Tetap Merakyat!
  • Inilah Daftar Link Nonton Film Ilegal yang Diblokir Komdigi dan Alasan Kenapa Kalian Harus Waspada
  • Inilah Lenovo Legion Y70 (2026), HP Gaming yang Siap Guncang Pasar dengan Teknologi AI
  • Inilah Rekomendasi HP Oppo Murah Spek Mewah Tahun 2026 yang Nggak Bakal Bikin Kantong Kalian Jebol!
  • Inilah 15 Situs Nonton Film Gratis Legal 2026, Bye-Bye Iklan Ganggu dan Drama Situs Ilegal!
  • Inilah Beasiswa Bekraf Digital Talent 2026: Solusi Buat Kalian yang Mau Jago AI dan Makin Produktif
  • Inilah Cara Memilih Power Bank yang Aman Dibawa ke Pesawat Agar Tidak Disita Petugas
  • Inilah Bocoran Harga dan Spesifikasi Redmi K90 Max, HP Gaming Dimensity 9500 Termurah yang Segera Dirilis!
  • Inilah RedMagic Gaming Tablet 5 Pro, Monster Gaming dengan Layar 185Hz dan Chipset Snapdragon 8 Elite yang Bikin Melongo!
  • Inilah Kenapa IGRS Trending di X, Bocorkan Spoiler Penting Game 007 First Light Hingga Ending!
  • Inilah 5 HP Murah Terbaru dengan Baterai Jumbo yang Siap Masuk Indonesia 2026, Ada yang Sampai 7.800 mAh!
  • Inilah Alasan Kenapa Samsung Galaxy S27 Bakal Pakai 50 Persen Chipset Exynos, Qualcomm Mulai Ditinggalkan?
  • Inilah Vivo Y31d Pro, Smartphone “Badak” dengan Baterai 7000mAh dan Ketahanan Standar Militer
  • 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 the Wan 2.7 AI model: A complete walkthrough
  • How to Run Minimax M2-7 Locally: A Step-by-Step Guide for Beginners
  • How to Build Self-Driving Codebases with Async Agents and NVIDIA Hardware
  • How to create a professional AI influencer using Apob AI
  • How to Use AI-Powered Tools to Level Up Your Stock Market Research Game with Claude Code
  • New Text Document
  • 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?

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