Python Syntax
The Syntax of Python programming is simple and clean as compared to other languages.
Mostly, a Python program uses the English language words known as keywords. Apart from keywords, a Python program can have variables, functions, classes, modules, packages etc.
First Program of Python Language
If we want to display any message, then we can use print
a statement with a message either in the single quote or double quote.
Let’s see a simple example to print Hello World:
print('Hello World')
As you can see the Syntax of Python use minimum code and it is not much complex if we compare to other programming languages.
Common mistakes in Python Syntax
If you are new to Python programming, there are some programming mistakes you can make. Now let’s have a look at the errors that happen during programming.
print("Welcome to Python Tutorials')
print("Welcome to Python Tutorials")
Here is a list of the mistakes in the above program:
- In line number 1, We can’t use both single quotes and double-quotes in that way. You must start and end with the same quotes.
- In line 2, we can’t use space (indent) before the print statement. Indentation has different rules/meaning in Python, which we will study in loops.
Python Syntax with Variable & Comments
Let us take another example by using variables and comments in Python.
Python variables can be simply defined using the variable name and their value. Single line comment is started with a hash (#) symbol. Once a variable is defined, it can pass in the parenthesis of print statement to show its value.
Here is the simple program to see the syntax of Python variable and comment. We will also learn in details about them in the next chapters.
number = 43 # here is number assigned to a variable
print(number)
43
Python programming tips:
Here is the list of tips in Python Programming for removing the errors and becoming a good programmer.
- Proper Spacing or Indentation: In the Python programming indentation is more important than other programming language. So use proper indentation where it is required, you will come to know step by step where to use spacing or where not to use.
- Comments: Make use of comments in your program so that you or other can understand your code easily in future.
- Proper naming Use proper naming of your functions, and variables in the program. We recommend to use lowercase and underscore (_) to seprate two words.
- File naming: It is wise to use meaningful file names for each programm such as print-message.py, print-variable.py along with proper file extension i.e.
.py
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |