We hope you installed python in your PC and you are ready to start your programming journey now.
In this tutorial, we are using notepad as our editor to write the python code.
After writing, we are using the python interpreter to execute/run our program.
What is interpreter?
An interpreter is a program that takes part of reading all of the source code from a file that we wrote using an editor.
It will read our code one line at a time. and evaluates every character of the code for correct syntax in our coding!
After the interpreter finished entire code evaluation, It will conversate to the compiler to compile entire code.
What is compiler?
A compiler is also a program. Since electronic devices only understand binary data/instructions, It takes the job of converting the human written code into binary instructions. After interpreter reads every line in the code and if it founds all code is syntactically correct, It will then informs to the compiler and the compiler will start the conversion. Let us see how we can utilize these 2 tools for our python program.
Writing your first python code
Before writing the python code, open notepad, make sure there are no text contents, set your screen reader symbol level to “all” to speak out each and every character in the file.
Within the first line, write the following code:
Saving your python program
After writing the code, press ctrl+s to open save as dialog box. If you saving for the first time, the dialog box will open, else the file will be saved using previous saving settings.
After the save as dialog opens, follow below steps:
- Type the name of the file along with the .py format at the end of the file.
- Press tab key and in the save as type combo box, choose all files.
- Choose the location where you want to save the file and activate save button.
Explanation of the code
Instructs python to output the text called “Hello world!” on the screen.
print is a function in python that have the capacity of displaying the given text contents. The contents is usually written within the brackets and quotation mark.
Like print, there are various functions in python, that is used for various purpose.
After function does its job, It may return any data and we can use that data for any purpose.
We will learn deeply about functions in later tutorials.
Running our python program
After we write our program and if we want to run our program, in most of the programming languages, we should compile the program into machine language from the human readable language. But, in python, Its not like that. We can skip the compilation step and we can just run the program using the python interpreter. Compilation is optional in python.
Running our python program through command prompt
To run the python program through command prompt, follow below steps:
- In file explorer, Navigate to the directory where you stored your file of source code,
- Press alt+d to navigate to the address bar, Clear all text in the address bar, type “cmd” without quotation mark to open command prompt and press enter,
- After command prompt window opens, type python “your file name.py” to run your python program,
- Optional: If command prompt gives output like “python is not recognized as an internal or external command, operable program or batch file.”, type c:\windows\py.exe “your file name.py” to run python program alternatively,
- To listen the output of the program, use the NVDA review text commands.
Compiling python program
If you don’t want to see your code by others or if you want to use code of existing file in another file, you will compile the python code. For compiling, follow the steps of running python code and while you are in command prompt, type python -m “Your file name.py” in the command prompt.
A new .pyc extension file will be generated inside the __pycache__ directory. That file will be the compiled python file.