Python Learning Notes (3)-control flow, functions

Source: Internet
Author: User

  1. Control flow Statements
    If, while, for, break, continue
    from the effect of the final effect, the students have no difference in other languages. The only thing to be aware of is syntax, and Python is so amazing and loving in syntax.

    Use of control flow statements
    1  whileTrue:2input_number = Int (Raw_input ('Enter an integer'))3 4     ifInput_number = =0:5          Break6 7     elifInput_number% 2 = =0:8         Print 'Input_number:', Input_number9         ContinueTen  One     Print 'Looping ...' A  - Print ' Over'
    (sentiment: In fact, as a programmer, once mastered a programming language, really should not be afraid to learn new language. )
  2. Function
    Keyword defining a function: "def":
    1 def SayHello (): 2     Print ' Hello world! ' 3 4

    It is important to note that the function must already be defined before the function is used, that is, the function call is always after the function is defined.

    To define a function with parameters:

    1 defSay (message, Times=1, who_say='programe'):2     PrintMessage,who_say * Times3 4Say'Hello')5Say' World', 2)6Say'Hello', who_say='He')

    (Note that parameters that do not have a default value cannot be placed after a parameter with a default value)

    It's nice to be here. Provides default values for setting parameters, and can be assigned by keyword. This means that, especially in the case of many parameters, you can only assign values to parameters that need to be assigned, rather than having to pass in the preceding arguments because one of the arguments is placed later.

    Python is truly natural and easy to read and understand, such as the code just now, you can read the code semantics probably understand that this statement is: Print the message specified number of times

    Print message * times


    Return statement
    You can also use return to end a function in a function, or return a value at the same time. If you do not use the return statement manually, each function will also imply a sentence "return none",none means no value.

  3. DocString
    The document string, defined as the first string within the function, is equivalent to a C # document comment, can generate a document, and can be viewed at any time by running "print functionname.__doc__"
    1 defPrintmax (x, y):2     " "Prints The maximum of the numbers.3 4 The values must be integers." "5x = Int (x)#Convert to integers, if possible6y =Int (y)7 8     ifX >y:9         PrintX'is maximum'Ten     Else: One         PrintY'is maximum' A  -Printmax (3, 5) - PrintPrintmax.__doc__

Python Learning Notes (3)-control flow, functions

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.