First Python Program
In this section we will discuss the basics of Python and complete a simple task to print Hello World in the console.
Python offers two ways to run programs:
- Using Interactive interpreter prompt
- Using a script file
Interactive interpreter prompt
Python provides three functions to execute Python statements one by one under the dialog symbol. This is a better choice when we care about the consequences of each line of the Python program
To open an interactive session, open a terminal (or command prompt) and type python (or python3 if you have both Python2 and Python3 installed on your system).
It will open the following command where we can run Python commands and check the results in the console.
After writing the print statement, press the Enter key.
Using a script file (Script Mode Programming)
The interpreter suggests that it is best to run a single line of code. However, we cannot type numbers into the terminal every time. It is not suitable for writing more than one line.
Format, we can write multiple lines of code to a file for later processing. To do this, we need to open an editor such as Notepad, create a file named and save it with the .py extension, which means “Python”. Now we will use the format to use the example above.
print (“hello world”); #here, we have used print() function to print the message on the console
To run this file named as first.py, we need to run the following command on the termina
Step – 1: Open the Python interactive shell, and click “File” then choose “New”, it will open a new blank script in which we can write our code.
Step -2: Now, write the code and press “Ctrl+S” to save the file.
The output will be shown as follows.
Step – 4:Â Apart from that, we can also run the file using the operating system terminal. But, we should be aware of the path of the directory where we have saved our file.
- Open the command line prompt and navigate to the directory.