As Python is one of the most popular high-level programming languages, it’s of utmost importance to know how to run a python script. A python script is nothing but a code written in python & structured into a file. The file contains an extension ‘.py
’ or ‘.pyw
’ if it’s for windows.
Also Read: How to take User Input in Python
Let’s start by downloading & installing a python interpreter to run a python script.
Here is a basic python script to print ‘Hello World!
’
print('Hello World!')
From the above example, we can see a ‘print()
’ function is used to print a written text within the parenthesis. The text can be written within a single quote or a double quote if the text contains a paragraph.
If you are no beginner & belong to other programming languages fundamentals, you may have noticed that there’s no semicolon is used at the end of a statement in python. Also, no need for any exclusive file inclusion to run a basic python script, unlike the C language.
Assuming you’ve installed the python interpreter, let’s check if the interpreter is working properly or not by opening the command prompt on your desktop & typing the following command.
python -v
Typing this command will give us the python interpreter version installed on your desktop.
Python 3.9.0
Let’s Explore the Four Optional Ways to Run a Python Script.
- Interactive Mode
- Command Line
- Text Editor (VS Code)
- IDE (PyCharm)
Interactive Mode:
Here, you can run your python script line by line in a linear sequence. Interactive mode can be activated by opening the command prompt on your desktop & typing ‘python
‘ & hitting Enter
.
name = "Arjun"
print("Hello, my name is " + name)
Hello, my name is Arjun
Command Line
A ‘python
‘ code is written before the file name in command prompt to run a Python script in a ‘.py
‘ file in a command-line prompt. In the following code, you can write your own filename instead of ‘hello.py
‘
python hello.py
Hello World!
Text Editor (VS Code)
Following steps should be followed in order to run the Python script on a Text Editor such as a Visual Studio Code.
- Press ‘Ctrl+Shift+X’ on your keyboard after locating the extension section on your VS.
- Search & Install the extension name ‘python’ & ‘code runner’ in the searc bar of your VS (Visual Studio)
- Restart the VS code
- Create a new file with the proper extension ‘hello.py’ & you can write the below code in the prompt.
print('Hello World!')
- Now, right click in open space & select the option ‘Run Code’ to run the program.
Hello World!
IDE (PyCharm)
The following steps can be accounted to run a Python script on an Integrated Development Environment such as (PyCharm)
- Create a new project.
- Name the project as ‘TechsTricks‘ & click on Create.
- Select to open the root directory with the project name associated with the last step & open it.
- Navigate to New & select the ‘Python.file‘ option. Now, Rename the file as ‘hello’ (you can change the name as per your convinience.)
- ‘.
py
‘ extention will be assigned automatically after allocating the file name. - You can now write the following programme.
print('Hello World!')
You can run this python script by right-clicking on the open area & selecting the option ‘Run File in Python Console‘ Or, simply pressing the green play button on the top right of your desktop.
Hello World!
Learn New Programming Concepts with TechsTricks each week.