About judging and looping statements
Python's judgment and looping statements are very intuitive and read close to natural language.
Judgment statement if
The standard if statement is the following structure:
if expression: if_suite
If expression is not 0 or true, then the If_suite code block is executed. Otherwise, the next statement is executed.
Also, the conditional statement for Python has an else statement.
if expression: if_suiteElse: else_suite
and Elif (ELSE_IF) statements
if expression1: if_suiteelif expression2: elif_suiteElse : else_suite
Looping statements
The while loop structure is as follows
while expression: while_suite
Similar to C. Only the loop body is not enclosed in parentheses and is represented by indentation only.
For loop structure
Python's for Loop and C's for loop have a big difference. Python's for accepts an iterative object, such as a sequence or iterator, as an argument, iterating over one of the elements at a time.
Such as:
for inch ['apple','banana','orange' ]: Print item
The For loop of Python accepts an iterator object. If we want to make it look like a traditional for loop, we can use the number sequence to make him look like a counting loop.
for inch [0,1,2]: ... Print ITEM012
Because the range of values can be particularly large, it can be cumbersome to write through handwriting each time, so Python provides a range () built-in function to generate this list.
for in range (3): ... Print ITEM012
There are several ways that the range () function can be called. The full syntax requires this to call it
Range (start, end, step)
When you do not give step, the default is 1, step can not be 0.
>>> Range (2, 3) [2, 5, 8, one,, +]>>> Range (3, 7) [3, 4, 5, 6]& Gt;>> Range (5, 2,-1) [5, 4, 3]
The range () also uses two shorthand syntax formats:
Range (end) range (start, end)
Range (end) is to accept a value, and start defaults to 0,step by default to 1.
Range (start, end) is almost the same as the full version, except that Step uses 1 by default.
List parsing
for in range (4)] squared:print i0149
List resolution means that you can use a for in a row
Loop to put all the values in the list.
File
Open the file
' R ')
The file_name variable contains the string name of the file that we want to open, Access_mode ' r ' means read, ' W ' means write, ' a ' means add, ' + ' means read-write ' B ' means binary access. If Access_mode is not provided, the default value is ' R '. If open () succeeds, a file object handle is returned. All subsequent operations must be done through this file handle. But after a file object is returned, we can access some of its methods, such as ReadLines () and close (). The method properties of a file object must also be accessed through the period property identifier.
Properties are data-related items, which can be simple data values or executable objects, such as functions and methods. Classes, modules, files and complex numbers, and so on, have properties.
Use the Period property identifier method to access the object properties. This means adding a period between the object name and the property name: Object.attribute
Errors and exceptions
Python allows programs to detect errors at run time. An exception is thrown when an error is detected, showing the details of the exception.
To add error detection and exception handling to your code, simply encapsulate them in a try-except statement. The code group after try is the code that you intend to manage. The code group after except is the code that handles the error.
try : FileName = raw_input ( ' ' for eachline in Fobj: print Eachline, fobj.close () except IOError, E: print " file Open Error: , E
Function
Functions are similar to other languages. must be defined before the call, no return returns none
The function is defined as follows
def function_name ([arguments]): " Optional Documentation String " Function_suite
The DEF keyword is followed by the function name, which is the parameter required by the function. Ends with a colon (:), followed by an indent to represent the body of the function.
Default parameters for functions
A function parameter can have a default value, provided in the form of an assignment statement, and if the call does not provide a parameter, it takes the value as the default.
>>>defFoo (debug=True): .....'determine if in debug mode with default argument'... ifDebug: ...Print 'In debug mode'... Print ' Done'...>>>foo ()inchDebug Modedone>>>foo (False) done
Module
The modules are organized into separate files with Python code that is related to each other. Can contain executable code, functions and classes, or combinations of these things.
When you create a Python source file, the name of the module is the file name without the. py suffix. Once a module is created, it can be imported from another module using the import statement.
Import Module_name
After the import is complete, the module's properties (functions and variables) are accessed through the Period property identifier method
Module.function () module.variable
2015/8/18 python basic usage (2)