Python Learning Notes-function (i) Parameters

Source: Internet
Author: User
Tags pear

The first is that the strings, numbers and tuples are immutable as function parameters, but the data structures such as lists and dictionaries can be changed.

def change (n): n[0]= ' apple ' names=[' banana ', ' pear ']n=names[:]change (n) print names,n[' banana ', ' pear '] [' apple ', ' pear ‘]

Keep the original list intact after you modify it.

Suppose you now want to write a program that stores names, ages, departments and can make contact queries based on these three things:

Because it is querying multiple elements, first think of the dictionary of this data structure, consider building a large dictionary storage within the existence of three dictionary names,age,dept.

First create a dictionary initialization function:


DEF init (data): data[' names ']={} data[' age ']={} data[' dept ']={}storage={}print Init (storage) None


Before getting the information, write a Get information function, the data dictionary, label names, age and Dept, info is specific personal information, such as Jack,25,finance and Lee HR, then data[age][25] will return ' Jack, 25,finance ' and ' lee,25,hr ' two.

Def lookup (data,label,info): return Data[label].get (Info)

Next, find out how to put the value in the dictionary:

Def store (Data,infos):     linfo=infos.split ()      labels  =  ' name ', ' Age ', ' dept '      for label,info in zip (labels, Linfo):  #并行遍历两个list         people=lookup (data, label,  Info)          if people:             people.append (infos)   #如果已存在键, add only values.         else:             data[label][info]=[infos]  #如果不存在, a new key value is created. note here [info] represents a list of strings. Mylist={}init (MyList) store (mylist, ' lee 26 hr ') store (mylist, ' zhang 27 hr ') store (mylist, ' LEE  26 engineering ') lookup (mylist, ' name ', ' LEE ') {' dept ':  {' HR ':  [' lee 26 hr ',  ' Zhang 27 hr '],  ' Engineering ':  [' lee 26 engineering ']},  ' age ':  {':  [' lee 26 hr ',  ' lee 26 engineering '],  ':  [ ' Zhang 27 hr ']},  ' name ':  {' LEE ':  [' lee 26 hr ',  ' lee 26  Engineering '],  ' ZHANG ':  [' Zhang 27 hr '}}

When you call the function again, you can provide the name of the parameter, and the code is easier to read:

def hello1 (greeting,name): print '%s,%s '% (greeting,name) hello1 (name= ' Shuwei ', greeting= ' hello ') hello1 (name= ' Hello ') , greeting= ' Shuwei ') Hello,shuweishuwei,hello


When defining a function, the keyword argument can give the function a default value, for example:

def hello1 (greeting= ' Hello ', name= ' Shuwei '):

See below for an example of how to collect a parameter narimoto a group or dictionary:


def printpa (x,y,z=3,*pospar,**keypar): print x, y, z print pospar print keyparprintpa (1,2,3,4,5,6,foo=1,bar=2) PR INTPA (1,2,foo=1,bar=2) printpa (1,2,3,4,5,6,7,8,foo=1) printpa (All-in-all, ' I love You ', foo=1) printpa ("I Love You", foo= 1)
1 2 3 (4, 5, 6) {' Foo ': 1, ' Bar ': 2}1 2 3 () {' Foo ': 1, ' Bar ': 2}1 2 3 (4, 5, 6, 7, 8) {' foo ': 1}1 2 3 (' I Love you ',) {' foo ': 1}1 2 I Love You () {' foo ': 1}

So * allows you to enter an indefinite number of arguments when defining a function, representing a split string or list when called.



Can I nest another in one function? Of course:


def npower (factor): def cal (num): Return num**factor return Caln=npower (5) print n (2)



Call Npower (5) for the first time, and assign N, which is equivalent to assigning CAL to N, then N (2) is equivalent to Cal (2), return to 2**5==32, we call the Cal () in npower () the phenomenon is called closure (closure)




Python Learning Notes-function (i) Parameters

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.