Basic Syntax and Hello World! program in Python

In this tutorial we will try to understand the syntax of python programming. Although, syntax is something that you will understand as you will see more and more programs and examples, but there are a few things that you must know before hand.

Python Syntax Rules

  1. Python is case sensitive. Hence a variable with name yoyostudytonight is not same as yoYoStudytonight

  2. For path specification, python uses forward slashes. Hence if you are working with a file, the default path for the file in case of Windows OS will have backward slashes, which you will have to convert to forward slashes to make them work in your python script.

    For window's path C:\folderA\folderB relative python program path should be C:/folderA/folderB

  3. In python, there is no command terminator, which means no semicolon ; or anything.

    So if you want to print something as output, all you have to do is:

    print "Hello, World!"

    Oops! we just shared the first python program too with you. Yes, just one single statement.

  4. In one line only a single executable statement should be written and the line change act as command terminator in python.

    To write two separate executable statements in a single line, you should use a semicolon ; to separate the commands. For example,

    print "Hello, World!" ; print "This is second line"

    Live Example →

  5. In python, you can use single quotes '', double quotes "" and even triple quotes ''' """ to represent string literals.
    word = 'word'
    sentence = "This is a one line sentence."
    para = """This is a paragraph 
    which has multiple lines"""

  6. In python, you can write comments in your program using a # at the start. A comment is ignored while the python script is executed.
    # this is a comment
    print "Hello, World!"
    # this is a 
    # multiline comment

  7. Line Continuation: To write a code in multiline without confusing the python interpreter, is by using a backslash \ at the end of each line to explicitly denote line continuation. For example,
    sum =  123 + \
           456 + \

    Live Example →

    Expressions enclosed in ( ), [ ] or { } brackets don't need a backward slash for line continuation. For example,

    vowels = ['a', 'e', 'i',
              'o', 'u']

  8. Blank lines in between a program are ignored by python.

  9. Code Indentation: This is the most important rule of python programming. In programming language like Java, C or C++, generally curly brackets { } are used to define a code block, but python doesn't use brackets, then how does python knows where a particular code block ends. Well python used indentation for this.

    It is recommended to use tab for indentation, although you can use spaces for indentation as well, just keep in mind that the amount of indentation for a single code block should be same.

    if True:
    print "Yes, I am in if block"
    # the above statement will not be 
    # considered inside the if block

    the correct way would be,

    if True:
        # this is inside if block 
        print "Yes, I am in if block"

    Also, the following code will give error, as the statements are differently indented:

    if True:
        # this is inside if block 
        print "Yes, I am in if block"
        # this will give error
            print "me too"

    again, the correct way to do so is to keep all the statements of a particular code block at same indentation.

    if True:
        # this is inside if block 
        print "Yes, I am in if block"
        print "me too"

So these are some basic rules that you must know so that it becomes easier for us to learn various concepts of python programming in the coming tutorials.

First Python Program

We already shared the first python program with you while learning the syntax. Yes, we were not joking, it's just one single line, nothing above it, nothing below it. To print Hello, World! on screen, all you have to do is:

print "Hello, World!"

Live Example →

You can write and execute this code in IDLE, or you can save this code in a python code file, name it can name it anything, just keep the extension of the file as .py).

To run the python script, open IDLE, go to the directory where you saved this file using the cd command, and then type the following in command prompt or your terminal:


This will execute the python script and will show you the output in the line below.

From the next tutorial we will start learning the various concepts of python programming language.