Second, python2.x basic content 2__python

Source: Internet
Author: User
Tags variable scope
2.1 Piece StructureIf statement if condition expression: #注意有 Colon code block #注意 indent else: #与if对齐 code Block
Elif statement If condition: code block elif Condition: code block ... elif condition: code block else: code block
Conditional nesting equal indent to same conditional structure

Guess number game:

Run Result:

2.2range and Xrange produce a regular set of data syntax: Range (start,end,step = 1) #start: Start value, end: Final, Step: stride, cannot be 0, default is 1 range (star T,end) range (end) #0开始, with no final value above all generate a real list
Xrange () #生成器, how much is generated, the syntax is the same as the range; there is no xrange in Python3, when the range feature in Python3 displays Xrange list (xrange (10)) as Xrange.
2.3 Loop while loop while condition: #注意有冒号 code block #注意缩进 for loop explicit loop number syntax for a variable in iteration Like: #注意冒号 the code block can iterate over the object that is, the value is ergodic, in Python, can be iterated objects mainly string (string), List (list), tuple (tuple), Dictionary (dictionary), file (file) For can be used in list resolution and builder expressions: List parsing consists of a pair of brackets, first an expression, then a For loop, and finally a condition

The builder expression uses parentheses, like List parsing, but instead of creating a list, it returns the builder, as well as the range () function in xrange () and Python3 in Python2, which is typically used when the data is large.

2.4 break,continue and else in the loopBreak statement out of the current loop structure continue statement stop the current loop, re-enter the loop (that is, stop the statement after continue) in the while loop, to determine whether the loop condition is satisfied (meet to skip the statement after continue , re-enter the loop) in the For loop, which is used to determine if the iteration is in the end loop, if the loop code terminates from the break, jumps out of the loop and ends the loop, then executes the code in else, while it can be paired with else (normal end while, Code in the line else)2.5 Custom FunctionsCreation of custom functions, Syntax def function name (parameters): #参数可写可不写, the general write function, code block first write docstring, that is, the code block to view the comment code to see a function of the docstring, you can: the name of the print function. __doc__ default parameters, which provide default parameters in the form of assignment statements, can change the default parameters are not allowed, and default parameters typically need to be placed on the last key parameter of the argument list to write the name of the parameter directly, once the keyword parameter is used, the following parameter must use the keyword parameter keyword parameter is to allow the caller to vary the parameter in the parameter list by using the argument name parameter pass function to pass the function name as an argument to another function lambda function, anonymous function lambda parameter list: Table Up #注意冒号, returns the expression result

2.6 Recursive recursion must have boundary conditions, that is, to stop the recursive conditions recursive code more concise, more in line with natural logic, easier to understand the efficiency of recursive execution is not high, system resource consumption is larger than the cycle, generally no cyclic features, but there are recursive features, such as Hanoi 2 .7 variable Scope Global variables in the program code body part of the variable local variable in the function variable if the global variable and local variable have the same name, follow the inner mask outer layer Python global variables cannot be assignable within functions Global statement emphasizes global variables, making full A local variable can be assignable within a function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.