Loading...
Loading...
00:00:00

Python Syntax Rules

Main Function In Python

Main function is entry point of a program where python code starts execution. However, Python interpreter executes the code from the first line without main function. The execution of the program goes line by line. In python there is no need to write main function . For modularization main function is very important. when a python file is imported in another file main function will be as module execution start point as we defined. __name__ is a special varible for defined function name in a module. here is the example of main function in python -

def main():   # define main function
    print("Hello World!")

if __name__ == "__main__":
    main()   # call the main function if run this file

Syntax Rules For Python Program

For Writing program in python we have use some predefined set of Rules. Some important rules are listed here

  1. Python is case-sensitive programming language that mean "Hello World" and "hello world" is not same. x = 10 or X = "Hello world" is not same variable.
  2. We cannot use predefined python keywords for function's name for defining variables, function, class etc.
  3. Variable name cannot start with number or special character
  4. Class Name must be starts with Capital letter
  5. In python, functions body, class body , loops body is defined by TAB (generally four spaces). For create a block We cannot use Curly braces for functions or class, intead of that we use Indentation

    We cannot write the block of statement like this example, python will throw Error

    if True:
    printf("Hello World")    # Indentation error 
    # other code
  6. we cannot add extra spaces in start of the line or in a block
    # correct indentation
    i = 0
    while i< 10:
        i = i + 1  # indentation is important for while body (this is not extra spaces)
        print(i)   # Tab define body
  7. In python we cannot break the line just like C, C++, java . In python  denotes the line is continue to next line.  is called line continuous character.
    i = 0
    while i         # line is continue to the next line 
    < 10:
        i = i + 1
        print(i)
  8. we can write multiple statement in single line using semicolon ; . semicolon denotes the ends of statement for continue other statement
    if True: print("hello World") ; var1 = 100