Python Learning Note 11 _ function returns multi-valued, list-generated, loop-multivariate, parameter-in-format declarations

Source: Internet
Author: User
Tags generator generator generator

One, function returns multiple values

1, if the function returns multiple values, it will put these values into a tuple
2, if the function returns more than one value, you can also use multiple variables to receive

def say ():     = 1    = 2    = 3    return  num1,num2,num3print#  If a function returns multiple values, it will put these values in a tuple, (1, 2, 3)Res1,res2,res3 = Say ()# When multiple values are returned, print can be received with multiple variables   #1print#2print#3
ii. List-generated

L = [I for I in Range (1,101,2)]

List-generated, can only be used in the list

Write the loop first, and then take the I out of the loop, you can do some simple operations (0, convert to string, etc.)

Import= random.sample (range (1,34), 6= []# for num in red_num:#       tmp = str (num). Zfill (2)#     new_num.append (TMP)for in Red_num]# List-generated print(new_num)#[' 16 ', ' 20 ', ' 07 ', ' 18 ', ' 13 ', []

Generate an odd number within 100

 for  in range (1,101,2)]# outside is [] is a listprint(l)
third, generator

L1 = (I for I in range (1,11,2))

Replace [] in list generation with (), the print generator itself will display generator instead of list

L = [i forIinchRange (1,11,2)]#outside is [] is a list#Space change time, memory consumption is large, but no longer CPU#list It is to put all the elements in the memory, here will apply for 5 memory spacePrint(l)#[1, 3, 5, 7, 9]L1 = (i forIinchRange (1,11,2))#outside is () the word is generator#The Generator generator saves memory than the list, it is the time of each loop, it calculates an element according to the rule, and puts it into memory .#generator performance is better than the list, only a piece of memory space, each call and then go according to the rules to calculatePrint(L1)#<generator Object <genexpr> at 0x00000000030fe4c0> forIinchL1:Print(i)#1 3 5) 7 9
four or three meta-expressions

c = A If a > b else b

A = 5= 3#  If a > B:#     c = a#  Else:# C10/>c = bifelse# If A is greater than B, a = c, otherwise c =bprint(c)
v. Cyclic two-dimensional arrays
L = [    [1,2,3,4],    ['a','b','C','D']] forA,b,c,dinchL#There are multiple elements in a two-dimensional array that can be looped    Print(a)#1,a    Print(b)#2,b
Vi. declaration into the parameter format

Simply for code readability, and does not force the conversion type

def My (name:str):# tells others that name is STR but does not force conversions, L:list tells others to pass in the list   print( Name) my ('hello') my (['123', 123]# Enter the parameter is list also can, [' 123 ', 123]

Python Learning Note 11 _ function returns multi-valued, list-generated, loop-multivariate, parameter-in-format declaration

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.