Finally, after all that time in the console and the interpreter, this time we'll start writing and running python scripts from a file.
Real programming starts here ๐ฅ๐ฅ๐ฅ
About Python Scripts
A python script is a file holding code in the Python language, usually with the .py extension. The python interpreter later reads that file and runs it line by line.
A python script can be written with any text editor. The simplest examples of a text editor are Notepad (Windows) and Nano (Linux). Still, to make the work easier, writing python scripts in a Code Editor such as VS Code, Sublime Text, or Atom comes highly recommended.
Making a Simple Python Script
To make a python script you only need to write the script you want in a file with the .py extension. Writing it is no different from writing code in the interactive Python interpreter.
Open your favorite text editor or code editor and type the following code. Notepad is fine, or anything else.
print("Halo dunia")
nama = "Adi"
umur = 18
print("Nama saya",nama ,"umur saya", umur, "tahun")
When you're done, save that file with the .py extension. For example hello.py
Done ๐ Our python script is finished. Next we just try running it.
Running a Python Script
To run a python script, first we still need to open a terminal. Once it's open, point the terminal's directory at the directory where hello.py is stored.
If you're in the right directory, type the following command
$ python3 hello.py
or
$ python hello.py
If it's right, output like this comes out
Halo dunia
Nama saya Adi umur saya 18 tahun
Yayy... congratulations, the python script ran successfully ๐ฅณ๐ฅณ
Using VS Code as a Code Editor (Recommended)
Using VS Code as a python code editor comes highly recommended. Why?๐ค Because VS Code has a range of features that make writing Python code much easier for us.
VS Code provides an integrated terminal along with the code editor. So we don't have to bother opening a terminal and a code editor in 2 different apps.
To run a Python script with VS Code, follow these steps.
1. Installing VS Code
To install VS Code, go to the download page for the operating system you use, then install it as usual.
2. Making a folder just for python scripts
After VS Code is installed, the next step is making a folder just for holding all our python files.
This folder can be made anywhere, for example Document > Python, or Home > Python.
The point of making a dedicated folder is to make running things easier.
3. Opening the folder you made through VS Code
Next is opening the folder you made earlier. To open a folder, click File -> Open Folder. Or use the keyboard shortcut by pressing Ctrl+K then Ctrl+O.
๐ท Illustration: the VS Code interface with an Explorer panel on the left showing the project's folder structure.
Once the folder is open, its name shows up in the Explorer panel on the left. For example, if your folder is named python, all our code files end up neatly organized there.
4. Making a python script file with VS Code
To make a python file, click the "+" sign on the right side of the folder name. Then enter a file name with the .py extension. Example: hello.py
The next step is writing python code in the file you made. Use your own code, or the example code from earlier.
5. Running a Python Script with the Integrated Terminal
The last step is running the Python script. To open the integrated terminal in VS Code, press ctrl+`. A Terminal window comes out at the bottom of the editor.
Once that's open, you can type the command to run the Python file as explained earlier (for example python3 hello.py). The text our code prints comes out right in the bottom terminal panel, yayy ๐ฅณ๐ฅณ
๐ท Illustration: the VS Code editor panel showing source code at the top and the script's execution output in the integrated terminal.
Wrapping Up
When we use the interactive interpreter, we can only write short commands. With a python script, we can write longer and more complex programs. That makes arranging code easier.
Even so, the interactive terminal is still very useful for trying code whose behavior we don't know yet. Rather than the hassle of making a file and all that, just open the interactive terminal (as a calculator, for example)
That's it for this one, hope it helps.
Next time we'll start using python scripts to cover the I/O system in Python 3.
See ya... ๐