D. How to Install Python and Get Started - ZamaZoe/1.-Kurro-REST-API-Tutorial_Introduction GitHub Wiki
Introduction
Welcome!
In this section, we will help you kick-start your Python journey by assisting you with the setup process. The setup is suitable for any personal computer.
We will start by installing Python on Windows and Mac PCs, and then we'll conclude with a Python overview from a computer science perspective, highlighting its versatile capabilities. Follow these steps to install Python:
Prerequisite:
Visual Studio code
The Steps to Install Python Program with Pip Package
- First, to download Python, click on this link: Python downloads.
- Select the link corresponding to your operating system version on the Python website (downloads section), as shown in the following screenshot.
- Select and download Python version 3.10.10. We will develop our REST API using Python 3.10.10.
- For Windows, download:
- For macOS, download:
- Next, to install Python, double-click the file you downloaded.
- Then, in the installation window, tick the bottom box, which reads 'add python.exe to PATH', and select the Install Now tab.
- The installation status will appear in the following window; please wait for it to finish, as it may take a while.
- Another window will appear when the installation is complete, stating that the setup was successful.
- You are now ready to create and run a Python program.
The Steps to Create and Run a Python Program, Pt. 1
Step 1: Follow these steps to create an empty folder for your project:
- Navigate to your file system in Windows Explorer or Mac Finder; create an empty folder from here. You may name this folder 'First_Python_Program.' In this folder, you will store your project files.
Step 2: Follow these steps to launch Visual Studio Code and get started:
- To launch the Visual Studio Visual IDE, locate the VS Code icon on your desktop and double-click on it.
- Select the 'Open Folder' tab from the file menu; refer to the screenshot below for guidance.
- Navigate to the empty folder created in step 1 (First_Python_Program) and select or double-click the folder; again, you could create a new folder in this tab if you did not follow step 1.
- The folder will open as the root directory for this project; should you get a warning stating, 'Do you trust the authors?' Please press 'Yes' to continue using that folder.
- Now, you are all set to write your first Python program.
Step 3: Use the following steps to create a text file:
We will create a text file within the IDE(Integrated Development Environment) and write our first line of code before writing our first Python program. Here are the steps to create a text file:
- Navigate to your folder. Select the first tab named 'New File;' enter the file's name, and you can call this file 'My_program.txt;' follow the example shown in the screenshots below:
Note:
- Dot py (.py) is the actual extension for Python programs, but it's not a requirement for this specific program.
- The basis for using dot-txt (.txt) files is to help you understand crucial Python concepts.
- For your filename, you must insert underscores between spaces.
Step 4: Use the following steps to write and execute your first Python program:
- Navigate to the file 'My_program.txt' we created in step 3 to write a Python program within an IDE. Write your first line of code, as shown in the following screen; this program will output the sum of '
3+4
.'
- To save the file, navigate to the menu bar, select the file tab, and click the 'Save' option; alternatively, you may type '
CTRL + S
' from your keyboard.
- To execute this program to get results, navigate to the menu bar and select the 'Terminal' tab, then select the option 'New Terminal' to open the command-line interface that interacts with your computer. See the following screenshots:
- Execute or run the following commands on the command-line interface:
- For Windows users, run this command: '
python My_program.txt
.' - For Mac users, run this command: '
python3 My_program.txt
.'
Results:
- Our program returned an output of 7. See the following screenshot:
- The key to the above steps aims to help you start with the Visual Studio Code (VS Code) IDE.
- Also, it helps you to comprehend that a Python program is akin to a text file. You write and execute it using the Python command.
The Steps to Create and Run A Python Program, Pt. 2
Again, the key to the above steps in Pt. 1 aims to help you start with the Visual Studio Code(VS Code) IDE. You write and execute the code using the Python command.
This tutorial will show you an excellent way to execute Python programs using the Run button. This IDE is missing a run button; hence, we must install the Python extension. Below are the steps to create and run the Python program more efficiently.
Step 1: Use the following steps to install Python extensions:
- Navigate to the left side of the primary sidebar and select the four-block icon. Here, you will search for an extension suitable for executing Python commands.
- To search for the Python extensions, click the search window, type 'Python Intel,' and select the first extension, 'Python IntelliSense (Pylance), Linting.'
- Press "Install" if not already installed; see the following screenshot.
Step 2: Use the following steps to activate the 'Run button:'
- To activate the 'Run button,' you must return to the 'File Explorer' tab, right-click on your file 'My_program.txt,' and then select the 'Rename' option.
- Rename your file to 'My_program.py.'
- The 'Python Run Button' will appear at the top right corner of the IDE.
Note:
- The run button will only be visible if the file has a '
.py
' extension. - Therefore, all Python programs must have a 'dot-py' extension.
- Hence, Python continually searches for the 'dot-py' extension before executing the program.
Step 3: Use the following steps to select a Python interpreter:
Before running the Python program, it is necessary to choose the interpreter. Follow the steps below to learn how to select a Python interpreter:
- To select a Python interpreter, go to your menu bar, select the 'View' tab, and then the 'Command Palette' option. You can also use the shortcut '
CTRL + Shift + P
' on your keyboard.
- Select the tab named 'Python Interpreter.'
- Select Python version 3.10.
- The chosen interpreter will appear in the bottom-right corner of the IDE. If you want to change this version, click on the version at the bottom, which will take you back to point 4 of step 4.
Step 4: Use the following steps to run the Python program:
- To run the Python program, we will proceed with the code from Part 1, allowing us to compare outputs for Part 1 and Part 2. To execute this program, click the 'Run button.'
- Once more, our program generated an output of 7, mirroring Part 1.
Conclusion:
As you have seen from the steps in Part 1 and Part 2, you can use two distinct methods when executing Python programs:
- When executing Python programs, you have multiple options. In step 1, we executed the Python command '
py -3
' with the file's name. This command invokes the Python interpreter and executes the specified file. - Another convenient option is to use an integrated development environment (IDE) or code editor that offers a "Run button'' similar to how we executed our program in step 2.
- IDEs such as PyCharm, Visual Studio Code, or Spyder have this feature that lets you run Python programs with a simple click of the 'Run Button,' eliminating the need for manual command entry. This approach is practical when working with larger projects that involve multiple files or when you prefer a graphical interface for running your code.
- Always remember to name your files with '
dot py
' at the end to use the Run button. IDE extensions will work better if you have your file names with 'dot py
.' - To further your Python skills, follow this link: The Python Tutorial.
- Before venturing into this tutorial, follow this link to understand 'What is a REST API?'