Topics

Syntax Rules and First Program

Welcome to another tutorial of the python scripting course, here you will learn about python programming syntax. However, the syntax is not that difficult to understand, because as you go through more programs and examples you will understand what it is and how to use it. But, first, you must understand the rules of Python syntax.

 

Python Syntax Rules

  • Python is case-sensitive. Therefore, a variable with the name myfirstprogram is not the same as MyFirstProgram.
  • In the case of path specification, python adopts the use of forwarding slashes; so when working with a file, the default path for the file take for instance a Window OS, will have a backward slash, that will convert to forward slashes to make them working in your Python script.
  • Example: for window's path C:folderAfolderB; the relative python program path should be C:/folderA/folder.
  • Also, there is no command terminator in Python, that is to say, no semicolon “;“ or anything.
  • Therefore, to print something as an output, it can be done like this:
print ("Hello, World!")
  • In one line, only a single executable statement should be written. Also, a line change act as a command terminator in python.
  • In other to write two separate executable statements in a single line, a semicolon “;” is used to separate the commands. 
print ("Hello, World!") ; print ("This is second line")
  • We can use single quotes '', double quotes "" and even triple quotes ''' """ to represent string literals in Python.
word = 'word'
sentence = "This is a one line sentence."
para = """This is a paragraph 
which has multiple lines"""
  • We can write comments in our program using an # 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
  • Line Continuation: in other to write a code in multiline without confusing the python interpreter, a backslash is used at the end of each line to explicitly denote line continuation. 
sum =  123 + 
       456 + 
       789
  • Note that expressions enclosed in ( ), [ ] or { } brackets do not require backward slash for line continuation.
vowels = ['a', 'e', 'i',
          'o', 'u']
  • Blank lines present in between a program are ignored by python.
  • Code Indentation: It is the most important rule of python programming. In Java, C, or C++ programs, generally curly brackets { } are used to define a code block, but python doesn't use brackets but uses indentation to know when a particular code block ends.

However, it is recommended to use a tab for indentation, but spaces can be used for indentation. All you need to know is that the amount of indentation for a single code block should be uniform.

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

But, the correct way is:

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

In this example, the codes will give an 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")

Now, this is the correct indentation to keep all the statements.

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

 

First Python Program

You have already seen your first python program in one of the above sessions. It is just a single line. To print Hello, World! On the screen, do the following:

print ("Hello, World!")

Output:

Hello, World!

This code can be written and executed in the IDLE or it could be saved in a python code file, just name it test.py ( though, you can use any name, just know that the extension of the file should be .py).

Now, in other to run the test.py python script, open IDLE, go to the directory where the file was saved using the cd command, and then type the following in the command prompt or your terminal:

python test.py

Therefore, this will execute the python script and then show you the output in the line below.