First Python Program

 There are different ways of writing and running programs in Python: 

1) How to Execute Python Program Using Command Prompt & Notepad:

 Create a Python file i.e <filename>.py in any directory using windows default editor notepad, write your program and run it. You can use the Windows command prompt to execute the Python code. 

Here <filenaame> is user defined. User can keep any name of the file. 

.py should be the extension of the file.

Example

 Here is the simple code of Python given in the Python file demo.py. It contains only single line code of Python which prints the text “Hello World!” on execution. 



 So, how you can execute the Python program using the command prompt. To see this, you have to first open the command prompt using the ‘window+r’ keyboard shortcut. Now, type the word ‘cmd’ to open the command prompt. 

This opens the command prompt with the screen as given below.



Change the directory location to the location where you have just saved your Python .py extension file.

We can use the cmd command ‘cd’ to change the directory location. Use ‘cd..’ to come out of directory and “cd” to come inside of the directory. Get the file location where you saved your Python file. 

To execute the Python file, you have to use the keyword ‘Python’ followed by the file name with <filename>.py.

See the example given in the screen above with the output of the file.




2) Using VSCode to Execute Python Program:

 1 : Install VS Code on Windows
 
First, Open VS Code official website i.e  https://code.visualstudio.com/  in the browser and download the VS code.

Launch the setup wizard by clicking downloaded vscode.exe file and install VS Code.

After installation, open VSCode and go to Extensions View (click the square icon on the left sidebar of vscode or press Ctrl+Shift+X).

Search for "Python" by Micrisoft in extension search bar and install it.

[Fig : Installation of Python extension in VSCode]

2 : Create and Run a Python File
  1. Open a folder in VS Code where you want to save your code (File > Open Folder).

  2. Create a new file: File > New File, then Save As something like demo.py.

  3. Write your Python code:

     

  1. To run the script:

    • Right-click in the editor and select "Run Python File in Terminal", or

    • Press Ctrl + F5.













Comments