When you install python on your machine, it will install the python interpreter or python shell, IDLE, and other required components to implement the applications using the python programming language.
As python is an interpreted programming language, you can execute the Python programs in two ways, i.e., directly write and run the code in python interpreter command-line interface or write code in the source (
.py) file and execute the file from python interpreter. Now, we will learn how to write and run python programs using both methods.
To open the python interpreter command-line interface, search for python in the start menu and click on the Python app like as shown below.
After opening the python interpreter command-line interface, you will see the greater than (
>>>) symbols like as shown below, that means you are ready to start typing your code.
After opening your python interpreter, type
print("Hello World") command in the console and click on the Enter button to print the Hello World text like as shown below.
If you observe the above result, we tried to print a message in the python shell by typing the message
print("Hello World"). After we type a message and click on Enter, immediately it returned the string whatever we mentioned in a terminal.
Instead of a Python shell or interpreter, you can also execute the python code through a command prompt for that open the command prompt, type python, and press the Enter button like as shown below.
Whenever the python started, you will see the greater than (
>>>) symbol that means you are ready to start typing your code. If you want, you can also perform different operations like as shown below.
If you observe the above results, the python shell or interpreter executes a single statement and returns the results. If you want to execute the multiple code lines, you need to create python files to execute the code using a python interpreter.
As python is an interpreted programming language, you can write the python code using different text editors and save those files with
.py extension. After that, you can execute that python (
.py) files either using direct python shell or by enabling the python in command prompt.
You can open any text editor like Notepad, create a file called
Helloworld.py and write the code like as shown below.
print("Welcome to Tutlane.com")
print("Learn Python with Examples")
Now, the python files can execute by using command prompt (cmd) for that open command prompt and navigate to the folder that contains the Helloworld.py file like as shown below.
Generally, the python files can execute by entering a command like
python <Your_File_Name>, so enter a command
python Helloworld.py and hit the enter button.
Instead of the Python shell or command prompt, you can also execute the code using Python IDLE. In the next chapter, we will learn more about Python IDLE to execute the Python programs.
After completing python code execution, if you want to exit or quit the python interpreter or command prompt, enter command
exit() and press Enter to exit the python interpreter prompt.