Python Learning Notes (vii) function pass List

Source: Internet
Author: User

Use functions to process elements in a list.

def greet_users (names): "
    send a greeting to a user in the list" for
    name in names:
        msg = "Hello," + name.title () + "!"
        Print (msg)
user_names = [' A John ', ' Zhao Si ', ' Wang er ']
greet_users (user_names)


----------result
:
    Hello, John!
    Hello, Zhao!
    Hello, Wang!   

1. Modify the list in the function
when you do not use a function,

**unprinted_designs = [' iphone ', ' robot ', ' cars ']
completed_models = [] While
unprinted_designs:
    current_designed = Unprinted_designs.pop ();
    Print ("Printing models:" + current_designed)
    completed_models.append (current_designed)
print ("\nthe Following models have been printed: ") for
model in Completed_models:
    print (model)


----------
* * result:**

Printing models:cars
Printing models:robot
Printing the

models:iphone Models have been printed:
cars
Robot
iphone

Next, use two functions to implement the above functionality.
The first function is used to print the data from the first list while moving the element to the second empty list. The second function is used to print the data in the empty list.

def print_models (Unprinted_designs, completed_models): While
    unprinted_designs:
        current_designed = Unprinted_designs.pop ()
        print ("Printing models:" + current_designed)
        completed_models.append (Current_ Designed
def show_comleted_models (completed_models): for
    model in Completed_models:
        print (model)
unprinted_designs = [' iphone ', ' robot ', ' cars ']
completed_models = []
print_models (unprinted_designs, Completed_models)
show_comleted_models (completed_models)


----------
**result**
Printing Models:cars
Printing models:robot
Printing models:iphone
cars
Robot
iphone

2. Prohibit function modification list
function (list_name[:]) to call a copy of functions

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.