Python Syntax


What is syntax?

Syntax refers to grammar rules in programming. or Python syntax defines rules for writing python code.


For you I have listed python grammar or python rules  as below. this also contains good and wrong code for good understanding.

Sure, here are simple examples illustrating each Python syntax rule, including both correct and incorrect usage:

  • Indentation for Code Blocks
    • Python follows strict Indentation rule, this allows more readable and top to bottom execution of python code correct.
    • Good :-
    • if True:
          print("This is indented correctly.")
    • Wrong :-
    • if True:
      print("This is not indented correctly.")
  • Newline at the End of Statements
    • Its good practice to have new line for each statements
    • Good :-
    • x = 5
      y = 10
      print(x+y)
    • Wrong :-
    • x = 5 y = 10 print(x+y)
  • Colons to Start Indented Blocks
    • Conditional blocks always starts with:, making indentation more sensible
    • Good :-
    • x = 5
      if x == 5:
        print("x is 5.")
    • Wrong :-
    • x = 5
      if x == 5:
      print("x is 5.")
  • Comments Start with Hash (#)
    • Comments helps developer, what is following or code below indented to do or execute
    • Good :-
    • # This is a comment.
      """String Comment"""
    • Wrong:
    • // This is a comment.
  • Variables Assigned using Equals Sign (=)
    • Variable always hold some default value, =, used for assigning values
    • Good :-
    • x = 5
      print(x)
    • Wrong:
    • 5 = x


You see, python syntax is very easy to learn, that's why its been adopted by many companies and programmers specifically new programmers.