Python Learning Notes (eight) function return multiple values, list deduction and exchange values of two variables

Source: Internet
Author: User

function return multiple values:

Supplementary Knowledge Points: Write code to get less use of global variables:
1. Unsafe

2. Will always occupy the memory does not release

function multiple return values, the return values are placed in a tuple and returned

 1  def   Hello (a,b,c,d):  2  return   a,b,c,d  3  Res=hello (  " YBQ  , "  ede  , "  RFV  , "  deq   " )  4  print  (res) #   (' Ybq ', ' Ede ', ' Dede ', ' DEQ ') 

List Deduction formula:

1 #List-derived2nums=[0,1,3,4,5,6,7]3 #Convert the nums int type to a string, method one:4new_nums=[]5  forNinchNums:6n=str (n)7 new_nums.append (n)8 Print(new_nums)#[' 0 ', ' 1 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ']9 #Convert the nums int type to a string, method two:TenNEW_NUMS=[STR (N) forNinchNums#List-derived conversions, simplifying code One Print(new_nums)#[' 0 ', ' 1 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ']

Exchange values for two variables:

1 method One:2A=13b=24tmp=05tmp=a6A=b7b=tmp8 Print(A, B)#2,19 Ten Method Two: OneA=1 Ab=2 -B,a=A, b - Print(A, B)#for Python 2,1 the  - Method Three: -A=1 -b=2 +a=a+b -b=a-b +A=a-b#Add and Subtract methods Exchange two variable values

Python Learning Notes (eight) function return multiple values, list deduction and exchange values of two variables

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.