Python Basic Syntax

Let’s write our first program in Python and see the basics of how Python compiler interprets the code.

Write your first Python program

While other programming languages might have a comprehensive Hello World program, Python has a very simple one. You can use the print statement to simply write the statement.

print(“Hello World!”)

This will give the below output:

Hello World

Indentation in Python Programs

As you have noticed, there are no ending semi-colons or starting or ending curly brackets in Python. But then how does Python compiler identify blocks of code and starting and ending of a statement? The answer is indentation!

Indentation refers to the spaces at the beginning of a code line. You will get an error if you don’t add these spaces.

if (2 + 2 = 4):
  print(“Adding 2 to 2 gives four.”)

The number of spaces depends on you but there has to be one. The standard is to use even numbers minimum of two or a maximum of four spaces. You can simply use the tab to keep indentations of all blocks the same.

Python Comments

To put code explaining statements in Python called comments, ‘#’ character is used.

#This is a comment.
print("Hello World!")

Multiline Statements

Statements in Python end with a new line typically but if your code line is running long and you want to divide it into two, you can use ‘/’ to do that which is called the line continuation character. 

totalMarks = Maths + \
             Physics + \
             Chemistry

However, the statements contained within brackets do not need to use the line continuation character.

subjects = [Maths, Physics,
           Chemistry, Biology,
           Computer]

This is it for the basics. You will be learning about higher Python concepts in the coming articles.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest