A byte of Python (excerpt 02)

Source: Internet
Author: User
Tags uppercase letter

A byte of Python fourth operator and expression


Operator Precedence

(operators typically combine left-to-right, that is, operators with the same precedence are evaluated in left-to-right order.) For example, 2 + 3 + 4 is calculated as (2 + 3) + 4. Some operators, such as the assignment operator, are combined right-to-left, that is, a = b = c is processed to a = (b = c). )

An expression

Fifth Chapter Control Flow
if语句:    if(): ...    if(): ... else: ...    if(): ... elif(): ... else ...    int(raw_input())    (guess = int(raw_input(‘Enter an integer : ‘))对于 raw_input  函数来说是一个字符串,通过 int  把这个字符串转换为整数,并把它存储在变量 guess 中)while语句:    while(): ...    while(): ... else ...for循环:    for..in 是另外一个循环语句,它在一序列的对象上递归。即逐一使用队列中的每个项目    for i in range(1, 5):        print i    else:        print ‘The for loop is over‘    (例如, range(1,5) 给出序列 [1, 2, 3, 4]。默认地,range的步长为1。如果我们    为range提供第三个数,那么它将成为步长。例如,range(1,5,2)给出[1,3]。记住,    range向上延伸到第二个数,即它不包含第二个数。)break语句:    break 语句是用来终止循环语句的,即哪怕循环条件没有称为 False 或序列还没有被完全递归,也停止执行循环语句。如果你从for或while循环中终止 ,任何对应的循环 else 块将不执行。    len() -- 输入字符串的长度通过内建的len 函数取得。continue语句:    continue 语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
Sixth Chapter function
A function is a program segment that is reused. They allow you to give a name to a statement, and then you can run the block of statements any number of times in your program using that name. This is called a call function. We have used many of the built-in functions, such as Len and range. The function is defined by the DEF keyword. The DEF keyword is followed by the identifier name of a function followed by a pair of parentheses. Parentheses can include some variable names, which end with a colon. Next is a piece of statement, which is the function body. The following example shows that this is actually quite simple: def sayHello (): print ' Hello world! ' # block belonging to the Functionsayhello () # Call the Functio    n function Parameters: The parameter is specified internally within the parentheses of the function definition, separated by commas.    Def Printmax (A, B): If a > B:print A, ' is maximum ' else:print B, ' is maximum ' Printmax (3, 4) # directly give literal values local variables: When you declare variables within a function definition, they are not related to other variables that have the same name outside the function, that is, the variable name is local to the function.    This is called the scope of the variable. Using the global statement makes it clear that the variable is defined outside the block. Default parameter value: For some functions, you may want some of its parameters to be optional, and if the user does not want to provide values for these parameters, the parameters will use the default values. (the default parameter value should be immutable) only those arguments at the end of the formal parameter list can have default parameter values. Key parameters: If you have a function that has many parameters and you want to specify only a subset of them, you can assign values to these parameters by naming them-this is called the key parameter- We use the name (the keyword) instead of the location (the method we have been using previously) to assign an argument to the function. Return statement: The return statement is used to jump out of a function from a function.    We can also choose to return a value from the function. A return statement with no return value is equivalent to return none.    None is a special type of Python that represents nothing. (for example, if the value of a variable is none, you can indicate that it has no value.) Unless you provide your own return statement, each functionContains the return None statement at the end. The pass statement represents an empty block of statements in Python.    Docstrings:python has a fantastic feature, called a document string, which is often referred to as docstrings. The string in the first logical line of the function is the document string for this function. Docstrings also applies to modules and classes. The Convention for a document string is a multiline string whose first line begins with an uppercase letter and ends with a period. The second line is a blank line, and the third line starts with a detailed description. It is strongly recommended that you follow this convention when using a document string in your function.

A byte of Python (pick)

Related Article

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.