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.
yoyostudytonight
is not same as yoYoStudytonight
For window's path C:\folderA\folderB
relative python program path should be C:/folderA/folderB
;
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.
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")
''
, 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"""
#
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
\
at the end of each line to explicitly denote line continuation. For example,
sum = 123 + \
456 + \
789
Expressions enclosed in ( )
, [ ]
or { }
brackets don't need a backward slash for line continuation. For example,
vowels = ['a', 'e', 'i',
'o', 'u']
{ }
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.
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!")
You can write and execute this code in IDLE, or you can save this code in a python code file, name it test.py(you can name it anything, just keep the extension of the file as .py
).
To run the test.py 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:
python test.py
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.