Running Python scripts by using Anaconda prompt
After installing Anaconda, we start to write some Python code in the Anaconda prompt. The following line of code is used to display a text message (such as Hello World) to the screen in the Python 3.6.4:
print("Hello World");
How to execute this line of code in the Anaconda prompt? Here is solution:
- Open the Anaconda prompt and type python command
- Type above line of code
The result can look like the following screenshot:
If I want to execute more than one code of line at a time as follows:
print("Hello World");
print ("My name is Ngoc Minh")
I can either write two (or more) lines of code in the same line as the following screenshot:
or I can add a trailing backslash (\) at the end of each statement and Python will prompt me with three dots (…) to enter code in the next line:
>>> print("Hello World");\
...print("My name is Ngoc Minh");
Another way is to put all our Python code in a .py file. I love this way! My steps:
- Open an Editor, such as Notepad, and type some Python code
- Typing PythonEx01.py in the File name, choosing All Filles in the Save as type, choosing a location (in this case is D:\LearnML), and clicking the Save button
- Come back the Anaconda prompt. If you are running Python runtime, exit it by using the exit() command. Type the following command:
python D:\LearnML\PythonEx01.py
The result can look like the following screenshot:
And now, we can program Python in the Anaconda prompt.