Home > Article > Backend Development > How to save code and run it in python
Python is a scripting language that uses text files (with a .py extension) to save code. There are two ways to run the code: enter exec(open("file path").read()) in the interactive interpreter; or enter the python file path in the command prompt. Make sure Python is added to the system path.
How to save and run code using Python
Python is a scripting language, which means that its code Typically not executed as a compiled format, but rather interpreted and executed directly by the interpreter. This makes the process of saving and running Python code very simple.
Saving Code
To save your Python code, simply create a text file and use .py as the file extension. For example, the file hello.py has the following content:
<code class="python">print("Hello, world!")</code>
Running Code
There are two main ways to run Python code:
<code>python</code>
This will start the Python interactive interpreter. You can then run the code by typing:
<code>>>> exec(open("hello.py").read())</code>
<code>python hello.py</code>
This will run the hello.py file directly using the Python interpreter.
Note:
The above is the detailed content of How to save code and run it in python. For more information, please follow other related articles on the PHP Chinese website!