Monday, June 27, 2022

Writing your first program in Python.

The first Python program that we are going to create will consist of only print built-in function. After the program is written the different procedures of running your first Python program will be shown. To write your first program simply use txt editor such as Notepad or Notepad++ on your operating system and save the file with .py extension.
Example 1 Create the Python script that will print out the following sentences:
  • Helo World!
  • This is my first Python Program.
  • Programming in Python is awesome!!!!
  • There's a long way to the programming expert if you like Python programming language.
  • The end of my Python program. Goodbye.
Name the script as "My_1._Py_Program.py".
Solution: Using Notepad or Notpead++ or Visual Studio code or any other simple text editor open New file and type in the following code:
print("Hello World!")
print("This is my first Python Program.")
print("Programming in Python is awesome!!!!")
print("There's a long way to be a programming expert if you like Python programming language.")
print("The end of my Python program. Goodbye.")
As seen from the previous block of code the only function that was used is the print function. The print is the built-in Python function that prints the specified message to the screen, or another standard output device. The message inside the function can be a string or any other object since it will be converted to a string before it is shown on the screen. As already mentioned in the description of Exercise 1 save the file as "My_1._Py_Program.py".

How to run your first program/Python script?

There are mutltiple ways to run Python script and here some of them will be covered.
  • Run your first python script from CommandPrompt or Terminal.
  • Run your first python script from Spyder.
  • Run your first python script from Ipython.
  • Run your first python script from Visual Studio Code.

Run your first python script from Command Prompt or Terminal.

To run the script from Command Prompt on Windows or Terminal on Linux operating system you have to navigate to directory of your saved Python script and then type in the following code.
python My_1._Py_Program.py
The first word is the name of the program that we are trying to call i.e. Python and the second is the name of the Python script. In newer versions of Python you have to write py instead of python.
py My_1._Py_Program.py
Either way the output of the My_1._Py_Program.py is given below.
Hello World!
This is my first Python Program.
Programming in Python is awesome!!!!
Ther's a long way to the programming expert if you like Python programming language
The end of my Python program. Goodbye.

How to run your first Program from Spyder?

To run your first Python script from Spyder first you need to open it. In Windows operating system if you have installed Anaconda distribution go to Start Menu then open Anaconda3 (64-bit) and click on the icon Spyder (Anaconda 3) and double click on it. When the Spyder program opens go to File->Open and navigate to a folder where you saved your "My_1._Py_Program.py". Then select the script and click Open. To run it just press F5 on your keyboard or click on the play button in the Toolbar. The output of the running script in the Spyder is given below.
Hello World!
This is my first Python Program.
Programming in Python is awesome!!!!
Ther's a long way to the programming expert if you like Python programming language
The end of my Python program. Goodbye.
Instead of going to File -> Open we could click on the Open icon in the toolbar and navigate to a folder where you saved your Python script.

How to run Python script from IPython?

To run the script from IPython first you need to open the IPython program. In Windows operating system if you installed the Anaconda distribution of Python, go to Start Menu and type IPython. When you find it double-click on it to open it. After the IPython is opened you have to import os library which will be used to write a file path where you saved your Python script and then you can run your Python script by typing the %run My_1._Py_Program.py. The entire code on how to run a python script is given below.
import os
filepath = "C:\\Users\\User\\FolderWithPythonScript"
os.chdir(filepath)
%run My_1._Py_Program.py
With use of os.chdir(filepath) command we have changed current working directory of IPython session. If you want after os.chdir(filepath) you can write os.getpwd() to see current working directory. After os.chdir(filepath) using %run command we have run the Python script. The output of the script is given below.
Hello World!
This is my first Python Program.
Programming in Python is awesome!!!!
Ther's a long way to the programming expert if you like Python programming language
The end of my Python program. Goodbye.

How to run python script from Visual Studio Code ?

To run the Python script from Visual Studio Code first you have to isntall the Visual Studio Code. After it is installed you have to install Python Extension or if you already installed Anaconda distribution of Python create connection with VSCode.

How to install Python Extension on VSCode ?

If you want to install Python Extension click on the link. Along with the Python extension, you need to install a Python interpreter. To install Python interpreter click on the link.

How to connect Anaconda with VSCode?

If you already have the Anaconda distribution of Python and want to connect it with VSCode then we want to make VSCode recognize the Anaconda installation so it can access installed packages for autocomplete, syntax highlighting, and error checking. VSCode should automatically recognize Anaconda. The proof of VSCode recognition of installed Anaconda would be the notification "Python 3.x ('base': conda)" in the lower-left corner of VSCode.
If for some reason you don't see the notification then press CTRL+SHIFT+P and type select interpreter. You should see "Python: Select Interpreter". If the Python interpreter is not visible then you have to find "Enter Interpreter Path" and then click on Find. This will be File Explorer and you have to find the location where you installed the Anaconda environment. In File Explorer, you have to find and select "python.exe" and that's it.
To run our first script you have to open it inside VSCode and click on the Play button which will automatically run our script. The output is shown below.
Hello World!
This is my first Python Program.
Programming in Python is awesome!!!!
Ther's a long way to the programming expert if you like Python programming language
The end of my Python program. Goodbye.

Conclusion

As seen in this post we have written our first Python script and run it using Command Prompt, Spyder, IPython, and VSCode. In the further posts we will be developing and executing Python scripts in Spyder.

No comments:

Post a Comment