Python quick learning 04: loop & amp; Function

Source: Internet
Author: User

In this article, the directory loop for while interrupt function defines the function call for loop in Python, The for loop is like the foreach iteration in the shell script. In Python, for accepts iteratable objects (such as sequences) as its parameters, one element of each iteration. Basic Structure of the in sequence of the for element: statement example for a in [3, 4.4, 'LIFE']: print (a) # This loop is used to extract data from the table [3, 4.4, 'LIFE'] to retrieve an element, assign the element to a, and then execute the for operation (print ). Introduce a new Python function range () idx = range (5) print (idx) # output [0, 1, 2, 3, 4]. Therefore, you can use range () in the () for a in range (10): print a ** 2 while loop basically constructs the while condition: statement # while will repeatedly execute the statement affiliated to it until the condition is False) example while I <10: print (I) I = I + 1 interrupt loop continue # In a loop execution, if you encounter a continue, skip this execution, perform the next operation break # stop executing the entire loop continue example for I in range (10): if I = 2: continue print (I) # When the loop is executed to I = 2, The if condition is true, and the continue is triggered, skipping this execution (not executing Print) to continue the next execution (I = 3 ). Break example for I in range (10): if I = 2: break print (I) # When the loop is executed to I = 2, The if condition is true, triggering break, the entire cycle stops. Some operations of a function are affiliated with a function. When you want to implement the same operation in the future, you only need to call the function name, without repeating all the statements. A function must be defined before it is called. If the function does not have a return statement, it will automatically return the None object. Python is called by reference. This means that changes to parameters in the function will affect the original object. But in fact, only a mutable object will be affected. For an immutable object, its behavior is similar to calling by value. In this example, def addMe2Me (x): return (x + x) # can also sum = x + x return sum to show the affiliation expressed by colons and indentation. Sum = x + x # can be used as the return (x + x) # value returned by the internal operation of the function, that is, the output function. Python functions allow no return value, that is, no return. Return can return multiple values (multi-value operations) separated by commas. Returns a tuple (value table ). Return a, B, c # is equivalent to return (a, B, c) if the function does not have a return statement, it will automatically return the None object. In the above example, we test the function print (addMe2Me (4.25) print (addMe2Me (10) print (addMe2Me ('python ')) print (addMe2Me ([-1, 'abc']) Output 8.520 PythonPython [-1, 'abc',-1, 'abc'] # + operator in non-numeric type.

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.