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

  • Begini Cara Hapus Akun DANA Premium Secara Permanen!
  • Bukan Cuma Mitos! Ternyata Dinasti Song Punya Tempat Nongkrong Mirip PIK, Begini Isinya
  • Bukan Fiksi! Ini Bukti Manusia Punya ‘Kekuatan Super’ Akibat Mutasi Genetik dan Lingkungan
  • Dikelilingi Cincin Api, Kok Kalimantan Malah Nggak Punya Gunung Berapi? Ini Jawabannya!
  • Misteri Newgrange: Kuburan Batu 5.000 Tahun yang Menyimpan Peta Galaksi, Kok Bisa?
  • Transformasi Ekstrem Pesawat IL-96: Ganti 4 Mesin Jadi 2 Mesin Raksasa PD-35, Emang Bisa?
  • Apakah Aplikasi Cash Reels Penipu?
  • Ini Daftar Kode Redeem Fish It Roblox Terbaru Januari 2026 buat Panen Hadiah!
  • Main Game Dapat Duit? Cek Dulu Fakta Shop Shelf Master Sebelum Nyesel!
  • Monitor Putih Sejutaan Spek Dewa, Acer S243y Bikin Ngiler!
  • Kapsul Luar Angkasa China Ini Bisa Mendarat Selembut Awan, SpaceX Bakal Ketar-ketir?
  • Kereta Maglev 500 KM/Jam: Proyek Gila 1.100 Triliun dari Jepang
  • Kenapa Perusahaan China Mulai Ungguli Amerika di Sektor AI dan Otomotif
  • Mau Beli iPhone 17 Tapi Budget Mepet? Ini Daftar Alternatif iPhone yang Masih Layak Dibeli
  • Apa itu OFTV? Pengertian OnlyFans TV: Platform Streaming Gratis Tanpa Iklan dari OnlyFans
  • Heboh Video 5 Menit Elga Puruk Cahu, Kok Bisa Seviral Itu? Ini Link Downloadnya?
  • Cara Mengatasi Masalah Windows Deactivated Setelah Kloning Disk
  • Investasi Bitnest Janjikan Profit Stabil, Yakin Aman? Cek Dulu Faktanya Sebelum Nyesel!
  • Cara Mengatasi Masalah Klik Mouse Tidak Berfungsi di Windows
  • Apple Dikabarkan Bikin iPhone Layar Lengkung 4 Sisi, Niru Xiaomi?
  • Inikah HP Samsung Terawet? Samsung Diam-diam Uji Baterai 20.000 mAh
  • Ini Deretan HP Murah RAM 12 GB yang Bisa Bikin Multitasking Ngebut!
  • Ini Trik Rahasia Dapat Candy Blossom di Grow a Garden, Nggak Cuma dari Event!
  • Siap-siap Boros! Ini Bocoran Skin Starlight Januari 2026 dan Update Seru M7
  • Moto X70 Air Pro Bakal Punya Kamera Periskop Canggih!
  • Ternyata Nggak Semua Aplikasi Bisa QRIS CPM di Alfamart, Ini Penjelasannya!
  • Lagi Order Tiba-tiba Gojek Error? Jangan Panik Dulu, Coba Langkah Praktis Ini!
  • Belum Tahu? Inilah Cara Melihat Kode Verifikasi Email Saya 6 Digit yang Sering Bikin Bingung!
  • Belum Tahu? Ini Cara Dapat Akses Premium Viu & Vidio Gratis Pakai Axis!
  • Belum Tahu? Inilah Fakta Kamera 0,5 di Samsung Galaxy A05s, Jangan Salah Beli!
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Inilah Cara Mengamankan Aplikasi LLM dengan AI Guardrails agar Tidak Halusinasi dan Bocor Data
  • Prompt AI Poster Vecna Stranger Things
  • Inilah Cara Menghancurkan Model AI Raksasa Hanya Bermodal 250 Dokumen
  • Tutorial Cara Menjalankan LLM Private di Laptop Sendiri, Aman dan Gratis Tanpa Internet
  • Begini Cara Buat Generator Stiker WhatsApp Otomatis Menggunakan Python dan OpenAI GPT-Image-1
  • Masih Ragu Soal Privasi OnlyFans? Ini Fakta dan Risiko Keamanan Digital yang Wajib Kalian Tahu!
  • Apa itu Praktik Pelacakan Data Ilegal? (Belajar dari Kasus Denda Apple di Italia)
  • OnlyFans Aman Nggak Sih? Cek Dulu Fakta dan Risiko Keamanannya Sebelum Kalian Daftar!
  • Apa itu Elga Puruk Cahu? Ini Alasan Video 5 Menitnya Viral Banget!
  • Apa Itu Kerentanan XSS N8N? Ini Pengertian dan Definisi Bahaya XSS yang Mengintai
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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