python入门(二)

函数

  Built-in Functions  
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
delattr() hash() memoryview() set()  

abs()   Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.返回number类型的绝对值。参数应为整数或是浮点数。如果是复数,则取模∣z∣=√(a^2+b^2)

all()    Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:如果迭代对象中的所有元素都为true或迭代对象为空则返回true。空字符串,空列表,空元祖返回true。

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
View Code

any()  Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:迭代对象中任一元素为true,则返回true。迭代对象为空,返回false。

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
View Code

ascii()  Return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr()using \x\u or \U escapes. 返回一个对象的可打印的表示形式的字符串。如果不是ascII码,则返回\x\u 或 \U的方式。

bin()  Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. Some examples:将一个整型转化为带“0b”前缀的字符串。结果是一个有效的python表达式。如果参数不是Int类型,那么,他需要定义__index__()方法返回一个整数。

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
View Code

bool() Return a Boolean value, i.e. one of True or Falsex is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. The bool class is a subclass of int (see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances are False and True (see Boolean Values).返回一个布尔值 。

bytearray()  Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Bytearray Operations.返回一个新的字节数组。字节数组是由0--256之间的数组成的可变序列。基本不理解,跳过。

isinstance()  Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.如果参数是某个数据类型的实例或子集则返回true。如果这种类型指的是元祖,只要检验的参数是任何一种类型的实例即返回true。

定义函数

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。

空函数

def nop():
    pass

参数检查

调用函数时,如果参数个数不对,Python解释器会自动检查出来,并抛出TypeError,参数类型不对无法检查。

def my_abs(x):
    if not isinstance(x, (int, float)):
        raise TypeError('bad operand type')
    if x >= 0:
        return x
    else:
        return -x
View Code

返回多个值(返回一个tuple)

import math

def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny
View Code

默认参数(必选参数在前,默认参数在后)

def power(x, n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
View Code

 

posted @ 2018-01-10 21:35  Merrys  阅读(206)  评论(0编辑  收藏  举报