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.
print ("Hello, World!")
print ("Hello, World!") ; print ("This is second line")
word = 'word'
sentence = "This is a one line sentence."
para = """This is a paragraph
which has multiple lines"""
# this is a comment
print ("Hello, World!")
# this is a
# multiline comment
sum = 123 +
456 +
789
vowels = ['a', 'e', 'i',
'o', 'u']
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")
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.