当前位置:网站首页>Python basic syntax

Python basic syntax

2020-11-08 11:42:00 Autumn maple

Lines and indents :

python The most distinctive feature is the use of indentation to represent code blocks , You don't need braces  {} .

The number of indented spaces is variable , But the statements of the same code block must contain the same number of indented spaces

If you like to program, you can add some Q Group 768671345, You can get free learning materials when you join the group

Multi line statement :
Python It's usually a sentence written one line at a time , But if the sentence is long , We can use backslash (\) To implement multiline statements

 

  stay [], {}, or () Multiple lines in , No need to use backslash (\)

 

 

String truncation :

grammar : Variable [ Header subscript : Tail subscript : step ]

 

 

Blank line :

Functions or methods of classes are separated by empty lines , Represents the beginning of a new piece of code . Classes and function entries are also separated by a blank line , To highlight the beginning of the function entry .

Blank lines are not the same as code indentation , Air travel is not Python Part of grammar . Don't insert blank lines when writing ,Python The interpreter will not run wrong . But blank lines are used to separate two pieces of code with different functions or meanings , Easy to maintain or refactor the code in the future .

 

Multiple statements make up a code group :

Indent the same set of statements into a block of code , I call it code .

image if ,while,def and class Such a compound sentence , The first line starts with keywords , And colon (:) end , One or more lines of code following this line form a code group .

 

import And from...import

Import the whole module into (somemodule), Format   import somemodule

Import a function from a module , Format :from somemodule import  somefunction

Import multiple functions from a module , The format is : from somemodule import firstfunc, secondfunc, thirdfunc

Import all functions of a function into , Format from somemodule import *

If you like to program, you can add some Q Group 768671345, You can get free learning materials when you join the group

版权声明
本文为[Autumn maple]所创,转载请带上原文链接,感谢