Python Syntax
Now, when we have got our systems ready for writing and executing python programs, we shall now write our very first python program.
We can use our text editors or the code editor that we have got with us. Alternatively, when we downloaded and installed python into our systems, it has come up with the Python IDLE, which stands for Integrated Development and Learning Environment. It is completely written in python. So, you can use that as well. Simply you have to search for IDLE in the windows search box, and you can find and execute it. Alternatively, you can use the command prompt(terminal) as well, to write the python programs.
Let’s try the terminal first, and then we will move on to our IDE as well.
Basic Python Syntax
First of all, just launch your command prompt(terminal), type python there, and hit enter.
Once you do that, you get to see the version that you have installed in your system and some other information
You would be seeing something like this → >>>
It is waiting for some input.
So, here, we are going to write some instructions now.
All we are going to do is just print something as an output –
so, you have to just write the following command in order to print your output on the same terminal
>>> print(“Welcome to gyanipandit.com”)
Hit enter after writing this. The output is whatever is inside the double quotes. (Welcome to gyanipandit.com in our case here).
You can refer to the following image to see the input and output for the above example.
C:\Users\Administrator>python Python
3.x.x (here are some other details, which might vary)
Type “help”, “copyright”, “credit’s” or “license” for more information.
>>> print(“Hello from gyanipandit”)
Hello from gyanipandit
>>>
As you can see, we could write our python code here in the terminal. We can make use of the IDLE as well, which comes when we install python into our systems.
You can search for the IDLE on your operating system, and when you open the IDLE, the python shell opens, with some information, which is similar to what we saw on the terminal after typing python there.
You can open a new file from there (clicking on to file option, and then clicking the new file), and then save it as a python file (with ctrl + s, and then just name the file).
So, now you have an empty python file, in which you can write some code. Let’s write the simple print statement there, which prints something. Have a look at the below program, which explains the same thing.
As you can see, we have written the simple instruction into the file now, and it is time to run the program. To run the program, you simply have to click on the run menu in the menu bar. After clicking on it, you can find the run module option there, clicking on which, the code executes.
In the output, you can simply see whatever we had written in the print statement.
So, as you can see, we can make use of the terminal, and the python IDLE as well, to write programs.
Now, let’s do the same thing inside the code editor, which we had downloaded previously. From now, we are going to use Visual studio code more often to write and run the python programs.
So, follow the given steps to write the same code using the code editor-
- Create a folder in your computer, where you can keep all the python files that you are going to create throughout the course, and even after the course. You can open that folder with VSCode, just by right-clicking on the folder and clicking on the open with code option, or after opening the Visual studio code, you can go to the file option, and then click on the open folder, and then open that folder. Create a new file in the folder, with the python extension (.py) (for example basic.py) and write the below instruction in the file –
print(“Hello from GyaniPandit”)
- Now you have to run this program using a terminal. You can use your own command prompt available in your system, or you can also use the command prompt from the IDE. It is available in the terminal option in the menu bar.
- Then type python basic.py (To execute the python program, simply type python, followed by a space, and then the program name with extension.py)
- this will give the output on the terminal.
Have a look at the below program, which explains the above steps that we did.
As you can see, this image bundles all the above steps here. You can see that we have created a python file, with a.py extension, and then written some instructions over here. After this, to run the program, we have brought in the terminal, which can be opened from the new terminal option from the menu bar. In the terminal, to execute the program, we simply typed python, followed by the name of the program, which is python.py, and once we hit enter, we see the output.
So, this way, we have executed a basic program in python. Now we can move on to other concepts in python. But this way, you can understand how we executed the programs on the terminal, in the IDLE, or in the code editor Visual studio code as well.
Well, as we will write more programs in python, you will get familiar with the language. It is recommended to explore and try some new programs after you get much more familiar with the python language. Here, we have written our first program as well.