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

  • Game & Aplikasi Android Terbaik Saat Diskon Liburan Natal! Jangan Ketinggalan!
  • Apa Itu Game Prison Escape Journey? Ini Pengertian dan Cara Mainnya untuk Pemula
  • Pixelated 083: Tier List The Gang – Ranking Game Android Terbaik!
  • “Listrik Gratis” dari Solar Panel Cuma Mitos?! Ini Sisi Gelap PLTS
  • Samsung Galaxy Z-Fold: Uji Jatuh Bebas yang Mengguncang Keandalan Lipatannya
  • Google One 2026: Apa yang Akan Jadi Fitur Utama dan Harga yang Diharapkan?
  • Apa Itu Error 0x800704f8? Ini Pengertian dan Cara Mengatasinya
  • Android Akhirnya Dapat GPS Darurat di India! Setelah Hampir 10 Tahun
  • Apa Itu GetContact Premium? Ini Pengertian dan Cara Daftarnya
  • Android Maze Figure: Koleksi Baru Google yang Bikin Penggemar Bergairah!
  • Google Update Besar-besaran Desember 2025: Apa yang Akan Memengaruhi Pengalaman Anda?
  • Masih Pusing Hitung Gaji Manual? Waktunya Pakai Aplikasi HR
  • Apa Itu Dustruco? Ini Pengertian dan Cara Pasangnya di HP Kalian
  • Apa Itu Aplikasi Dooie Live? Ini Pengertian dan Cara Pakai Aplikasinya untuk Cari Jodoh
  • Apa Itu Battle Emote Jefri Nichol dan Om Telolet Om di MLBB? Ini Penjelasannya
  • Apa itu Game Luna Mobile dan Bagaimana Cara Menangnya?
  • Apa Itu Kompensasi Sistem Trail Mobile Legends? Ini Penjelasan dan Cara Klaim Hadiahnya
  • Apa Itu Update Google Pixel 2 Desember 2025? Ini Penjelasannya!
  • Ini Cara Reset Desil di Aplikasi Cek Bansos Biar Valid (Update Januari 2026)
  • Apa Itu EZNET Wireless dan Fiber Optic? Ini Perbedaan dan Pengertian Lengkapnya
  • Pengertian Rework Magic Wheel dan Rank Mythic Eternal: Apa itu Perubahan Sistem Baru Mobile Legends?
  • Apa Itu Diamond Combo? Pengertian Game Puzzle Viral yang Katanya Bisa Hasilkan Cuan
  • Apa Itu Showbox? Pengertian, Fungsi, dan Cara Menggunakannya di Android
  • Cara Mengatasi Fitur Monet Facebook Pro Tiba-tiba Hilang
  • Google Bikin Kejutan! Pixel 10 Diskon Gila-gilaan di YouTube Premium
  • Apa Itu Google CC? Ini Pengertian Agen Produktivitas AI Eksperimental Terbaru
  • Apa Itu Ultras Seblak di eSport? Pengertian dan Fenomena Baru Suporter eSport
  • Android 16: Animasi Folder Baru yang Mengubah Cara Kita Berinteraksi!
  • Android 16: Notifikasi Lokasi ‘Blue Dot’ – Fitur Baru yang Perlu Kalian Ketahui!
  • Apa Itu Risiko Auto Click di Event Spongebob Mobile Legends? Ini Penjelasannya
  • 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
  • Jelang Natal 2025 Fireworks.AI Beri Update Baru di NVIDIA Nemotron 3? Ini Penjelasan Lengkapnya
  • Apa Itu Elestio VibeCoder? Ini Pengertian dan Penjelasan Lengkapnya
  • Apa Itu Elestio Get A Team? Ini Pengertian Karyawan AI Digital
  • Apa itu RunPod? Ini Pengertian dan Tutorial Cara Deploy Pod Pertamamu
  • Apa Itu Migrasi Pod di RunPod? Ini Pengertian dan Cara Kerjanya
  • Gila! 574 Penjahat Siber Diciduk Interpol di Afrika, Kok Bisa Jaringannya Segede Ini?
  • Apa Itu Regulasi Drone Asing FAA? Ini Pengertian dan Implikasinya
  • Apa Itu Insiden Data Breach Nissan? Ini Kronologi dan Penjelasannya
  • Apa Itu Skandal Instacart? Pengertian Dark Pattern dan Refund 940 Miliar
  • Apa Itu Kerentanan UEFI? Pengertian Celah Keamanan DMA pada Booting Awal
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

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