Python Basic Learning Notes (11) Functions and modules

Source: Internet
Author: User

Resources:

1. Basic Python Tutorial

2. http://www.runoob.com/python/python-functions.html

3. http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

4. http://www.runoob.com/python/python-modules.html

5. http://www.pythoner.com/

6. http://blog.csdn.net/leexide/article/details/39908279

  ? function

  The parameters of Python are all reference passes.

The function syntax is as follows:

def functionname (Parameters):    " Function _ Document string "    function_suite   return [expression]

Instance:

def printme (str):    " print an incoming string to a standard display device "   Print Str    return

Python supports default parameters and variable length parameters, and the default parameters are defined and used with other programming languages. Examples of the use of indefinite length parameters are as follows:

#!/usr/bin/python#-*-coding:utf-8-*- #Writable Function DescriptiondefPrintinfo (arg1, *vartuple):"Print any parameters passed in"   Print "Output:"   Printarg1 forVarinchvartuple:Printvarreturn; #Call the Printinfo functionPrintinfo (10);p Rintinfo (70, 60, 50);

Python uses a lambda expression to create an anonymous parameter:

Lambda Syntax:

Lambda [Arg1 [, Arg2,..... argn]]:expression

Instance:

Lambda x, y = 5:x + ysum (3)

  ? Module

A module is a file that holds Python code (for example,. py) and is also a Python object with a random name attribute used to bind or reference. Where functions, classes, and variables can be defined, as well as executable code.

Import statement Syntax:

import module1[, module2[,... Modulen]

From...import statement Syntax:

 from import name1[, name2[, ... Namen]]

  

 from Import *

Use of global variables:

# !/usr/bin/python # -*-coding:utf-8-*-  =def  Addmoney (    ): Global money print      Moneyaddmoney ()print money

The Dir (module_name) function returns a list of all modules, variables, and functions that are defined in a module.

The Globals () and locals () functions return the names of global or local command spaces, respectively.

Reload (module_name) function, re-import a module.

  

  ? Package

A package is a folder that contains at least __init__.py files.

Python Basic Learning Notes (11) Functions and modules

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.