Note that the effect of + + and append () is the same as the list=list+[6,7] difference

Source: Internet
Author: User
# What is the difference between these two pieces of code? List1 = [1,2,3,4,5] List2 = [1,2,3,4,5] def proc (mylist): mylist=mylist+[6,7] def proc2 (mylist): Mylist.append (6

) Mylist.append (7) # Can You explain the results given by the print statements below?
Print "Demonstrating proc" Print List1 proc (list1) Print list1 print print "Demonstrating proc2" Print List2 proc2 (list2)  Print List2 ' demonstrating proc [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] demonstrating proc2 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6,  7] "# Python has a special assignment syntax: + =. Here is a example:list3 = [1,2,3,4,5] List3 = [6, 7] # does this behave like List1 = List1 + [6,7] or list2.append ([6 , 7])? 

Write A # procedure, proc3 similar to proc and PROC2, but for + =. def proc3 (mylist): MyList + = [6,7] List4 = [1,2,3,4,5] proc3 (LIST4) #>>> list4 [1, 2, 3, 4, 5 , 6, 7] #注意, after calling PROC3 (LIST4), List4 is now attached with the element [6,7], as PROC2. Therefore, we find that using + + and adding values to variables (append).

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.