Python Study Notes (6) abstraction (II)

Source: Internet
Author: User

6.4.5 reversal process

We have already discussed how to collect parameters as tuples and dictionaries. But in fact, if you use * and *, you can perform the opposite operation.

>>> def add(x,y): return x+y>>> params=(1,2)>>> add(*params)3

You can use the same technology to process dictionaries.

>>> def hello_3(greeting ='hello',name='world'):print '%s, %s'%(greeting,name)>>> params={'name':'daxiao','greeting':'i love'}>>> hello_3(**params)i love, daxiao

6.5 Scope

Like C, variables defined within a function are valid only within the function.

Read global variables:

>>> def combine(temp):print temp+temp2>>> temp2='b'>>> combine('a')ab

Change global variables

>>> x=1>>> def change():global xx+=1>>> change()>>> x2

6.6 Recursion

6.6.1 example

Fibonacci series:

ef f(i):if(i==1):return 1else:if(i==2):return 1else:return f(i-1)+f(i-2)

6.6.2 Binary Search

Omitted.

This chapter is relatively simple in general. After all, it is all the content you have learned before.

Summary

Abstraction

Function Definition

Parameters

Scope

Recursion

Function Programming



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.