Syntax is necessary for Python programming. Syntax are basic rules for writing Python code. For example, if we want to print some text on output, we use print()
function.
print('Hello World!')
print('I am learning Python programming')
Hello World!
I am learning Python programming
Now here we used print()
function, so first we write print
, then we use parentheses (). Between the parentheses we write, Hello World!
This was our text, which we want in the output. We also surrounded our text with single quotes ('Hello World!')
. So this is a syntax, or we can say this is a rule: we have to write our code according to this syntax. For example, if we did not use quotes with text.
print(Hello World!)
SyntaxError: invalid syntax. Perhaps you forgot a comma?
We get SyntaxError in the output because we didn’t follow the correct syntax to write Python code. In Python, text is considered a string data type. And when we write strings, we have to use the quotes. So this is syntax: if you are writing a string, you have to surround that string with quotes.
print(40478)
40478
Here we print a number on the output, but we didn’t use any quotes, and still we get the output without any error. Because this time we are printing an integer number, which is considered as ‘int’ data type in Python. And we can use numbers without using any quotes. So this is another syntax. However, if we try to print this number without parentheses:
print(40478
SyntaxError: '(' was never closed
We get SyntaxError because parentheses are mandatory while printing. So this is another syntax.
Python is a case-sensitive language. It means uppercase and lowercase letters are treated differently. For example:
text1 = 'Python'
text2 = 'Programming'
print(text1)
print(text2)
Python
Programming
Here we created two variables named text1
and text2
. Then we used the print function to print the value of those variables. We can see between the parentheses we write the exact same variable name. If we write variable names like this:
text1 = 'Python'
text2 = 'Programming'
print(text1)
print(Text2)
Python
NameError: name 'Text2' is not defined.
We get "Python"
as the first output, but for the second output, we get an error. Because in 4th line of code, we write print(Text2)
, but we created variable name with text2
, So in Python, T and t have different meanings. So text2
and Text2
are both different.