Python LiveTUT Learning Series Day-2 Syntax and Structure

By | October 27, 2011

 Welcome to Day 2 Training on Livetut Python 3.2 –Syntax and Structure

Before getting started, I believe u had Python and PyScripter installed in your PC if not view the previous day post on Installing Python.

Learning syntax and structure of python is quite easy to make the work simpler let us discuss with the following code.

1  #!/usr/bin/env  python

2 print(“Hello Livetut Viewers”)

3. Extension of python File is .py  where I save this file as hello.py

In above piece of code if we examine  #!/usr/bin/env  python  and break them.

  1. # – this is used as comment indicator which is similar to  // in C,C++.
  2. !/usr/bin/env  python  – this code begins with !(exclamatory symbol ) and followed by the path                     /usr/bin/env  python  which tells us to include the Python Interpreter in our program.

Note: Path various depend upon the Operating system i.e if you install python in Linux then it will look like  #!/usr/bin/ python

Ok now let us create new Python file from FILE->New->New Python Module or Click New icon above the Main Menu toolbar

Creating Python New Module

If you create new python file you will notice above code . Ok Now what does below code mean?

[code]def main():
pass
if __name__ == '__main__':
main()[/code]
Well I didn't write it. Hmm Actually it is created automatically which is define main function and call them according in the program. Then arise a question Can we write program without main function? Answer to this question at present yes we can.

From above code we found other declaration too i.e.

def main():

here def is keyword used to define new function followed by function name and : the syntax will look like
#syntax of defining new function
[code]
def fun_name():

code here

Now lets get back to above code , we can write the same code in below way also

def main():
pass

main()
[/code]
It will successfully work but reason why we use” if __name__ == '__main__': ”  this part of it here which allows us to only be run when the above run's as main module. Later we will discuss about class and modules in python.

I believe now you can understand basic structure of python.

In python it is important to know that unlike in C,C++ and other programming languages  python never use {} or special characters to indicate body of function or code block. It means that they uses only white space or consistent indentation to denote body of function/code. Now lets see the below example
[code]
def main():
print(“this is 1st line of code”)
print(“this is 2nd line of code”)

if __name__ == '__main__':
main()
[/code]
Output:

this is 1st line of code
this is 2nd line of code

In above snippet main function has two print statement as body of code.

Note that, white space or indentation can be one or more but it should be consistent.  If the write the above code like this
[code]
def main():
print(“this is 1st line of code”)
print(“this is 2nd line of code”)

if __name__ == '__main__':
main()[/code]

It will pop up error namely “unindent does not match any outer indentation level”. And if you write in this manner below
[code]
def main():
print(“this is 1st line of code”)
print(“this is 2nd line of code”)

if __name__ == '__main__':
main()
[/code]
See In above code ,”print(“this is 2nd line of code”)” is no long belong to main function because it is indented in same length of other line of code.

Output of the above program will look like

this is 2nd line of code
this is 1st line of code

Here “this is 2nd line of code” executes 1st and main function get called and then  “this is 1st line of code” get executed.

Note: Make sure you use indentation correctlyin your program to run them correctly.

Hope I believe today's was helpful to you and feel free to post your comment / doubt we are ready to answer your questions.

Thank you and Stay turned for next lesson

Ramakrishnan

zp8497586rq