current position:Home>Chapter 2: Fundamentals of python-1 grammar

Chapter 2: Fundamentals of python-1 grammar

2022-01-30 06:07:14 Small horn

Python grammar

The goal is

In this article , Your basic study will be Python grammar , For a quick start Python Language .

Spaces and indents

If you have used other programming languages , Such as Java、c# or C/C++, You will know that these languages use semicolons (;) To separate statements .

However ,Python Use spaces and indents to construct code structures .

Here's a paragraph Python Code :

#  Definition main Function print 
def main():
    i = 1
    max = 10
    while (i < max):
        i = i + 1

#  call main
 Copy code 

The meaning of the code doesn't matter to you now . Notice the code structure .

At the end of each line , You can't see any semicolons used to end statements . Code uses indentation to format code .

Organize your code by using indents and spaces ,Python The code has the following advantages :

  • First , You won't be like in other programming languages ( Such as Java or c#) Missing the start or end code of the block as in .
  • second , The coding style is basically unified . If you have to maintain other developers' code , Then the code looks the same as your code .
  • Third , Compared with other programming languages , The code is more readable and clear .


Comments are as important as code , Because they describe why you write a piece of code .

When Python When the interpreter executes code , It ignores comments .

stay Python in , A line of comments is marked with a pound sign (#) start , Followed by comments . for example :

#Python Single-line comments 
 Copy code 

Python Other types of annotations are also supported .

Statement connection

Python Use line breaks to separate statements . It puts each statement on a line .

However , A long statement can be made by using a backslash () Characters span multiple lines .

The following example demonstrates how to use backslashes () Characters connect statements in the second line :

if (a == True) and (b == False) and \
   (c == True):
    print(" Statement connection ")
 Copy code 


The identifier is Python Identify variables in 、 function 、 modular 、 Names of classes and other objects .

The name of the identifier must be a letter or underscore . The following characters can be letters 、 Number or underscore .

Python Identifiers are case sensitive . for example ,counter and Counter It's a different identifier .

Besides , You can't use Python Keyword to name the identifier .


Some words are in Python It has a special meaning . They are called keywords .

Here are Python Keyword list in : False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise

False class finally is
return None continue for
True def from nonlocal
and del global not
as elif if or
assert else import pass
break except in raise

Python It's a constantly evolving and changing language . So its keywords will continue to increase and change .

Python Provides a special module keyword, Used to list keywords .

To find the current keyword list , You can use the following code :

import keyword

 Copy code 

Literal of a string

Python Use single quotes (')、 Double quotes (")、 Triple single quotes (''') And triple double quotes (""") To represent the literal value of the string .

String literals need to use the same type of quotation marks . for example , If you start a string with a single quotation mark , You need to end the string with the same single quotation mark .

Here are some examples of string literals :

s = ' This is a string '
s = " Double quote string "
s = '''  Strings can span 
         Multiple lines  '''
 Copy code 


  • Python The statement ends with a newline character .
  • Python Use spaces and indents to organize the code structure .
  • stay Python in , Identifiers are used to identify variables 、 function 、 modular 、 Name of class, etc .
  • Comments describe why the code works . They are Python The interpreter ignored .
  • Use single quotes 、 Double quotes 、 Three quotation marks or triple double quotation marks to represent the string .

copyright notice
author[Small horn],Please bring the original link to reprint, thank you.

Random recommended