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.
-
Open a folder in VS Code where you want to save your code (
File > Open Folder
). -
Create a new file:
File > New File
, thenSave As
something likedemo.py
. -
Write your Python code:
-
To run the script:
-
Right-click in the editor and select "Run Python File in Terminal", or
-
Press
Ctrl + F5
.
-
Comments
Post a Comment