The basic syntax and variable python
- python statement indentation
Python code block using code logic indentation alignment means, the number of spaces Python each code block may be any indentation, but make sure the same segment block statement must contain the same number of spaces indentation. It recommends the use of a single tab or two spaces or four spaces in each code block indentation level, remember not mix.
- Python multi-line statement
The sentence is too long, resulting in the editor window width when the display is not completely full. You can use '' in line with the line of the statement into multiple lines.
- Python guide
In Python language, quotation for the presentation string. Can use single quote ( '), double quote ( "), tris quotation marks (' ''), quotation marks must be used in pairs string program for single and double quotation marks represent; Three allow a quoted string across a plurality of rows, the string can contain line breaks, tabs, and other special characters, triple quotes is also used in the program notes.
- Python identifiers
Python language identifiers used for naming variables, keywords, functions, objects and other data. Identifier names need to follow the following rules.
① can _ (underline) combination of letters (lowercase or uppercase A-Z a-z), numbers (0-9), but can not begin with a number;
② not contain any special characters except _ such as:%, #, &, commas, spaces, and so on;
③ not contain white space (line feeds, spaces and tabs called whitespace);
④ identifier can not be a Python language keywords and reserved words;
⑤ identifier is case-sensitive, num1 and Num2 are two different identifiers.
⑥ named identifiers to be meaningful, so see to know the name of Italy.
- Python keywords
Python pre-defined part of a special significance identifier, for the use of their own language. This part of the identifier called keywords or reserved words, can not be used for other purposes, otherwise it will cause a syntax error, with the development of the Python language, which is reserved keywords will vary. Table 1 lists the keywords Python reserved.
Keyword | meaning |
---|---|
yield | The return value from the function for sequentially |
False | Boolean value indicating false, and the relative True |
None | He represents nothing, own data types NoneType |
True | Boolean value indicating true, and contrary False |
and | Expressions for calculation, a logical AND operation |
as | For type conversion |
assert | If asserted, for determining the value of a variable or a conditional expression is true |
break | Break loop execution |
class | It is used to define the class |
continue | Out of this cycle, the next cycle continues |
def | Function or method used to define the |
of the | The value of a variable or deleted sequences |
elif | Conditional statements, and if, else in combination |
else | Conditional statement, and if, else used in combination, and also be used for abnormal loop |
except | Contains the operation code blocks catch exceptions, and try, finally combination |
finally | After the statement abnormality, abnormality, the code block is always executed finally included, and try, except in combination |
for | for loop |
from | Import module for use in conjunction with import |
global | Define global variables |
if | Conditional statements, and elif, else in combination |
import | Module for importing, from the use in combination with |
in | Determining whether the variable in the sequence |
is | Determine whether a variable is an instance of a class |
lambda | Anonymous function definitions |
nonlocal | A function for identifying the external scope |
not | Expressions for calculation logic NOT operation |
or | Expressions for calculation, logical OR |
pass | Empty class, method or function placeholder |
raise | An exception is thrown operation |
return | Functions for returning results from |
try | It contains statements may appear abnormal, and except, finally used in combination |
while | while loop |
with | Python simplified statement |
variable
Address storage unit named by an identifier referred to as a variable, is used to store data to be acquired by the identifier value of the variable, the variable may be assigned. Means for variable assignment is to assign a value to a variable, after the assignment is completed, the variable storage unit stores the point is assigned a value, as the assignment operator '= in Pyhton language, + =, - =, * =, / =,% =, ** =, = //. "
When a program uses variables to store data, you must first declare a variable before you can use.
Can assign values to variables in the variable declaration, it may not be assigned. When declaring variables need to declare data type, Python automatically select a data type match.