zoukankan      html  css  js  c++  java
  • 公用函数与模块

    1 #! /usr/bin/env python
    2  
    3 def fib(n):
    4 a,b = 0,1
    5 while(b < n):
    6 print b
    7 a,b = b,a+b
    8
    9 def fib2(n):
    10 a,b = 0,1
    11 result = []
    12 while(b<n):
    13 result.append(b)
    14 a,b = b,a+b
    15 return result

    save the file as fibo.py, then enter python enviroment as below:

    E:\PyTest>python
    Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import fibo
    >>> fibo.fib(20)
    1
    1
    2
    3
    5
    8
    13
    >>> from fibo import *
    >>> fib(20)
    1
    1
    2
    3
    5
    8
    13

    Pay attention to the difference between import modulename and from modulename import module

    Work for fun,Live for love!
  • 相关阅读:
    设计模式之装饰者模式
    每天一点点
    生财有道
    地图的移动和缩放
    钱分割
    位运算
    ref和out
    使用startCoroutine制定倒计时
    静态类和单例类
    Awake和Start
  • 原文地址:https://www.cnblogs.com/allenblogs/p/1836874.html
Copyright © 2011-2022 走看看