with Complete Solutions
Lab 1 – Python
Basics IDE:
Visual Studio Code
To run Python in Visual Studio Code, follow these steps:
Install Python: Ensure Python is installed on your system.
Install the Python Extension: Open VS Code, go to the
Extensions view (Ctrl+Shift+X), search for "Python" by
Microsoft, and install it.
Install Code Runner (Optional but Recommended): In the
Extensions view, search for "Code Runner" and install it. This extension
provides a convenient "Run" button.
Open or Create a Python File: Open the folder containing your
Python files in VS Code, or create a new file with a. pie extension.
Write Your Python Code: Enter your Python code into the editor.
Run the Code:
o Using Code Runner: If you installed Code Runner, click the "Run
Python File" button (a play icon) in the top right corner of the
editor.
o Using the Terminal: Open the integrated terminal (Ctrl+`)
and navigate to the directory of your .pie file. Then, execute
the file using python
your_file_name.py (or python3 your_file_name.py depending on
your Python installation).
o Without Debugging: Go to Run > Run Without
Debugging or use the keyboard shortcut Ctrl+F5.
o With Debugging: Go to Run > Start Debugging or use
the keyboard shortcut F5.
The output of your Python script will appear in the integrated terminal or the
,output panel within VS Code. If you are using Code Runner and need user
input, ensure "Code Runner: Run in Terminal" is enabled in your VS Code
settings.
1
,PyCharm
Running Python code in PyCharm can be accomplished through several
methods, primarily involving Run Configurations.
1.Running a Python Script:
Right-click in the editor:
Open the Python file you want to run in the editor. Right-click anywhere
within the editor and select "Run 'your_script_name'". PyCharm will
automatically create a temporary Run Configuration and execute the script,
displaying the output in the Run tool window.
Green Play Button in Gutter:
Locate the green "play" arrow icon in the gutter next to the if name ==
“main “ : block or at the top of the file. Clicking this will run the current script.
Run Menu:
Go to Run > Run in the main menu, or use the shortcut Alt + Shift + F10
(Windows/Linux) or Ctrl + Alt + R (Mac). This will bring up a menu of
available Run Configurations; select the one corresponding to your script.
Run Configuration Dropdown:
At the top right of the PyCharm window, there is a dropdown menu for Run
Configurations. Select your desired configuration and click the green play
button next to it.
2.Running a Selection in the Python Console:
Select Code:
Highlight the specific lines of Python code within the editor that you wish to
execute.
Execute Selection:
Right-click on the selected code and choose "Execute Selection in Python
Console," or use the shortcut Alt + Shift + E. This will run the selected code in
the interactive Python console, allowing you to see immediate results and
interact further.
3.Debugging:
Set Breakpoints:
Click in the gutter next to the line numbers to set breakpoints where you want
the execution to pause.
, 2