CHAPTER 8 General Syntaxes:- PART ONE OF CHAPTER 8

CHAPTER 8 General Syntaxes:- PART ONE OF CHAPTER 8

In this chapter we will learn something just to try some codes. We will learn the same things 
in detail later. All we need to do now is just try to write some code in Python and see how it 
works. At the same time, we will learn about the general syntaxes used often in Python

Create the main( ) function

As I said, Python scripts are almost like human language. You need not use a lot of special 
characters or symbols. All you need to remember is that “indentation” plays a very 
important role in Python. When you apply some special conditions inside your code, this 
indentation is important.
Few things repeat in every code. So you can write it out in a separate file and just use 
them in every new Python file. The general syntax structure code looks like this:

<code>

#!/usr/bin/python3

def main():

    rint("I am a general syntax Python file")

if __name__ == "__main__":

    main()

<code>

Save this file as “general-syntax.py”. When you execute this file, it will say or print out: 
“I am a general syntax Python file.”
The very first line, “#!/usr/bin/python3”, denotes the path of Python interpreter. 
The greatness of Python is that it remains same in every operating system. In the second 
part we have defined a main() function and, under that main() function, we can call 
any function. Without a main() function, you cannot call a function before it is defined. 
Consider this example:

<code>

#!/usr/bin/python3

def main():

          print("I am a general syntax Python file")

          LetUsDoSomething()

def LetUsDoSomething():

        print("I am doing something")

if __name__ == "__main__":

        main()

<code>

Now it will give a nice output like this:

I am a general syntax Python file
I am doing something

Suppose you don’t have any main() function. Now if you want to call the function 
LetUsDoSomething() before that function is defined, it will give an error.
Try this code and see the error:

<code>

#!/usr/bin/python3

LetUsDoSomething()

def LetUsDoSomething():

      print("I am doing something")

<code>

It says: NameError LetUsDoSomething() is not defined. You can always call it after the function is defined. In that case, you don’t need the main() function defined. But in a long line of code where many functions are involved, it is not always possible to maintain it. To solve that problem, it is a good practice to define the main() function first. After that you can write any function after the main() function and call it inside the main().

Indentation and White Space

      

They play a very vital role when you work with Python.
Indentation or white space is very, very important. Before you start learning Python, 
you need to understand this properly. Consider this code:

<code>

# coding=utf-8

def main():

        print('A line inside main function.')

print("A line outside main function.")

if __name__ == main():main()

<code>

Look at this code. The print() function inside the main() function has been indented. 
It has about four spaces. And the second print() function is outside the main() function. 
And look at the code; it falls on the same line with the main() function. So when we run 
this program, the outside print() function executes first. And the output is like this:

//output

A line outside main function.

A line inside main function.

//output ended

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow