Syntax and Rules in Python

Keywords: Syntax, Rules, Comments

Hello World Learn Python

Syntax and rules in Python are how the programmer should write his code, much like grammar works in languages. The compiler don’t care about the syntax – but it is important for programmers to adhere to the correct syntax, otherwise it will be very difficult for other programmers to understand your code.

Why do I need to know the right syntax?

As a programmer, it is important to write with the right syntax to get structured and easily understandable code. Especially in large programming projects, there are often several different programmers working in the project. Then it is extra important that everyone writes in the same way, otherwise it is impossible to get an overview of what is happening in the code. Each programming language has its own syntax and it differs slightly between different programming languages. The syntax in Python is international, so everyone follows the same standards.

The syntax in Python is international, so everyone follows the same standards.

Syntax in Python

The syntax in Java is learned gradually as you progress and learn new programming features. Therefore, in each section we show you how the syntax for that section. However, some examples to give you a hint of different syntaxes in Python:

  • Python is case-sensitive, which means, for example, Name and name have different meanings
  • The standard is to use English names in programming
  • All variables should start with a lowercase letter, for example,  var = 4
  • Functions begin with lowercase
  • Classes begins with a capital letter

We will go into more detail about how variables, functions and classes work in the coming chapters!

Rules and Tips in Python

There are certain rules that the programmer must follow when programming. If you break any rule, you will get a so-called compilation error, in other words,  the compiler does not understand how to translate the code, and then the computer does not know what to do. The program will then crash.

As with syntax, rules are something you learn gradually, but it is still important to start thinking about how to structure and write your programs. Some basic rules in Python is:

  • The first letter of a variable, function, or class must be one of the letters (a-z) or (A-Z). Numbers or special characters such as & and% are not allowed
  • Special characters cannot be used in names
  • There are reserved words, such as and, if, else, break, import, and more, which are not allowed in naming. All reserved words can be found here
  • Python is sensitive to indentations, that marks a block segment in your code

Add comments to the code in Python

A hashtag # creates a comment in the code. These are ignored by the compiler. You can thus comment on your code so that it is easy to understand what is happening.


print("Hello World!") # This is the comment --> ignored by the compiler

Giving the print:

Hello World!