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 Cerita Hangat di Balik Muscab PKB Kebumen: Dari Urusan Dapur Hingga Strategi Pemenangan 2029!
  • Inilah Kronologi dan Identitas 4 WNI yang Disandera Perompak Somalia di Kapal MT Honour 25
  • Inilah Alasan PKB Kaji Ulang Ambang Batas DPRD Biar Sejalan Sama Putusan MK
  • Inilah Jadwal Pengumuman Hasil TKA SD dan SMP 2026 dan Cara Cek Skor Kalian Secara Online
  • Inilah HP Gaming Vivo Terbaik 2026 yang Paling Gahar, Main Game Berat Nggak Pake Ngelag!
  • Inilah Potensi Pajak Selat Malaka yang Bikin Rame, Ternyata Gini Cara Mainnya Biar Nggak Melanggar Hukum Internasional
  • Inilah Alasan Kenapa Sinkhole Sering Muncul di Indonesia dan Cara Mengenali Tanda-Tandanya Supaya Kalian Tetap Aman
  • Inilah Program PJJ 2026 untuk Anak Tidak Sekolah, Cara Mudah Masuk SMA Tanpa Harus ke Kelas Tiap Hari!
  • Inilah Program SPMB 2026 PJJ Khusus Anak Tidak Sekolah, Solusi Buat yang Pengen Balik Belajar!
  • Inilah Cara Kuliah di Al-Azhar Mesir Lewat Jalur Kemenag 2026, Lengkap dengan Syarat dan Jadwalnya!
  • Inilah Jadwal Lengkap Jalur Mandiri Unud 2026, Persiapkan Diri Kalian Sebelum Menyesal!
  • Inilah 8 Universitas Swasta Terbaik di Indonesia Versi THE Asia University Rankings 2026 yang Bisa Jadi Pilihan Kuliah Kamu
  • Inilah Jadwal Terbaru SSU ITB 2026 yang Diperpanjang, Lengkap dengan Syarat dan Rincian Biayanya!
  • Inilah 10 Jurusan Kuliah Paling Dicari Perusahaan Tahun 2026, Cek Daftarnya Biar Nggak Salah Pilih!
  • Inilah Cara Daftar Beasiswa Tut Wuri Handayani 2026, Kesempatan Emas Buat PNS Kemendiktisaintek Tingkatkan Karier!
  • Inilah Ketentuan Lengkap TKA Susulan 2026 SD dan SMP, Cek Syarat dan Jadwal Resminya Di Sini!
  • Inilah Kurikulum Berbasis Cinta Madrasah: Panduan Lengkap dan Link Download PDF Terbaru 2026
  • Inilah Kronologi Mencekam Kecelakaan KA Argo Bromo Anggrek Tabrak KRL di Bekasi Timur yang Bikin Jalur Kereta Lumpuh Total
  • Inilah Alasan Kenapa Hari Libur dan Tanggal Penting Selalu Ditulis Pakai Warna Merah di Kalender
  • Inilah Cara Daftar Jalur Prestasi Politeknik PU Semarang 2026, Kesempatan Kuliah di Kampus Kementerian PU!
  • Inilah Cara Cek Bansos PKH dan BPNT 2026 Lewat HP, Lengkap dengan Jadwal Cair dan Besaran Dananya!
  • Inilah Alasan Kemdiktisaintek Bakal Tutup Banyak Jurusan Kuliah yang Nggak Relevan dengan Industri
  • Inilah Status Libur Hari Pendidikan Nasional 2026 dan Sejarah Penting di Baliknya
  • Inilah Daftar Libur Mei 2026 yang Bikin Full Senyum, Siapkan Rencana Liburan Kalian Sekarang!
  • Inilah Daftar Universitas Terbaik di Jepang Versi THE Asia University Rankings 2026, Kampus Mana yang Jadi Incaran Kalian?
  • Inilah Alasan Kenapa Kemdiktisaintek Bakal Tutup Sejumlah Prodi dan Fokus ke 8 Industri Strategis
  • Inilah Sosok Richard Aldrich McCurdy, Penguasa Asuransi yang Terjerat Skandal di Masa Gilded Age
  • Inilah Alasan Suhu Bumi Naik Drastis dan Cara Kita Menghadapi Ancaman Cuaca Ekstrem
  • Apa itu Pasukan Perdamaian PBB?
  • Inilah 25 Universitas Paling Internasional di Dunia 2026, Ternyata Kampus di Asia Mulai Merajai!
  • How to Transform Your Windows 11 Interface into a Sleek and Modern Aesthetic Masterpiece
  • How to Understand Google’s New TPU 8 Series for Massive AI Training and Inference
  • How to Level Up Your PC Gaming Experience with the New Valve Steam Controller and Its Advanced Features
  • 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 set up your own OpenClaw autonomous AI agent to manage your work and digital life efficiently
  • Xiaomi MiMo-V2.5-Pro Full Test: How to Build Incredible AI-Powered Projects with A Trillion-Parameter Guide for Young Developers!
  • NVIDIA Nemotron 3 Omni is Released!
  • How to use Google Veo 3 for free and generate high-quality AI videos without any expensive subscriptions or complex software
  • How to build professional AI projects that turn your GitHub portfolio into a job magnet
  • 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