Basic Python Syntax
In this tutorial we shall be looking at the syntax of how to code in Python.
It is one of the easiest ways in which one can compile a program.
Let us look at it with the help of a classic example.
First you need to open you IDE or your command prompt.
If you are using command prompt, then type “python” and then press enter so that is can understand that now you will be coding in python.
In case of IDE, go to file, click on new project, give it a name and you are good to go.
After this, let us see what we need to type in order to achieve our target that is, to print Hello World on the screen.
It is very simple; all you need to do is just type:
and there you are, you will see Hello World on your screen.
Here, you can put the text in single quotes as well as double quotes, it does not make any difference.
This is the output that you will see after a successful execution of your program.
If you have studied our tutorial on computer programming, there we mentioned some of the key words that were used in Python. But incase you missed it, we will mention them in this tutorial as well.
These are the list of the key words that are used in python as it they are mentioned above. So, try to avoid using them if you are just writing anything and you don’t want to use them for the purpose they are created.
One of the most important things that you should keep in mind is the indentation in Python. If your statements are not in the correct indentation, then they would not be executed.
Like if we want to write an if statement, then it is supposed to look like this:
if one: print(“one”)
We will see one on the screen if it satisfies the if condition.
But what if we write the same code as:
if one: print(“one”)
the code would have shown an error saying indentation error.
The statement inside the if block will shift towards the right. This is called indentation.
Point to be noted is that you need to put a colon after the conditional statements, loops etc. we will look at it in later tutorials.
Let us try to understand the concept of indentation with the help of a code.
if c =='mathematics': e = float(input(print('What Is 3 * 4 / 5 + 3 ?'))) if e == 5.4: count+=1 else: print('Incorrect Answer') count -= 1 f = int(input(print('What Is 5! * 4! ?'))) if f == 2880: count+=1 else: print('Incorrect Answer') count -= 1 g = int(input(print('What Is The Smallest Prime Number'))) if g == 2: count+=1 else: print('Incorrect Answer') count -= 1 print('Total Marks Obtained Are :',count) elif c =="gk": h = int(input(print('How Many Union Territories Are There In India ?'))) if h == 7: count +=1 else: print('Incorrect Answer') count -= 1 i= input(print('What is The Capital Of Arunachal Pradesh ?')) if i =='itanagar': count +=1 else : print('Incorrect Answer') count -= 1 j = input(print('What Is The State Flower Of Haryana ? ')) if j == 'lotus': count += 1 else: print('Incorrect Answer') count -= 1 k = input(print('Which State Has Almond As Its State Tree ?')) if k == 'jammu and kashmir': count += 1 else: print('Incorrect Answer') count -= 1 print('Total Marks Obtained Are:',count)
I do not expect you to get the meaning of the code, it is just for the understanding about the concept of indentation.
What if you want to comment something in your program?
A comment is a statement which is there in the program but does not get executed as the compiler or interpreter ignores those statements. Let us see how we can do it in Python.
c = “Python” if c =='mathematics': e = float(input(print('What Is 3 * 4 / 5 + 3 ?'))) if e == 5.4: #we are in the if block count+=1 else: #we are in the else block print('Incorrect Answer') count -= 1 f = int(input(print('What Is 5! * 4! ?'))) if f == 2880: count+=1
If we want to comment out something, we do it by using “#”.
For next line we use “\n” as we did in C, C++ etc.
I hope that you guys enjoyed this tutorial and learned something from it.
We shall be talking more about such interesting features of python in the coming tutorials. I will recommend you try these concepts by your self as it will help you in learning things faster. See you guys in the next one