Python学习

从头学Python(一)

2018-12-27  本文已影响10人  醋溜八酱

因为某些原因(基础没打好),打算从头过一遍Python。

一、学习资料(Python官方文档,当前稳定版为python3.7.1,最新版为3.7.2)

二、内建函数(基础语法掠过了,以后可能会从新看)

Python内建函数(Built-in Function)

(1)abs(x)

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.

返回一个数字的绝对值,传入的参数x可以是整数或者浮点数,如果这个参数(x)是复数,则返回这个复数所的量度(magnitude,或者叫量纲,问了一下朋友,这个单词所表达的意思就是指当前点距离0点的直线距离的值),如果输入的参数类型不是number则会报错。

abs()范例

(2)all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty)

如果迭代器内所有的参数都为True或者迭代器为空,则返回True,否则返回False

官方源码 all()范例

(3)any(iterable)

Return True if any element of the iterable is true. If the iterable is empty, return False.

如果传入的参数(可迭代对象)内的任一参数不为False,则返回True,否则返回False,传入参数为空也返回False。

官方源码 any()范例

(4)ascii(object

As repr(), 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. This generates a string similar to that returned by repr() in Python 2.

类似于Python2中的repr()函数,返回一个可以被表示的字符串,但是那些没有在ascll符号列表内的字符将会以\x,\u或者\U表示。

官方范例 范例结果

(5)bin(x)

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. 

If prefix “0b” is desired or not, you can use either of the following ways.

将整数转换为前缀为“0b”的二进制字符串,返回值是一个有效的Python表达式,如果x不是int对象(类型),则必须定义一个返回整数的__index __()方法。

如果不希望带“0b"前缀,可以使用下面的方法:

测试范例

(6) class bool([x])

Return a Boolean value, i.e. one of True or False. x 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).

Changed in version 3.7: x is now a positional-only parameter.

返回一个bool类型的值,即True或者False,x被转换为True或False。若x是false或者被省略掉,则返回False。bool类输入in类的一个子类....

在3.7版本时,x已经变成一个位置参数了

测试范例

(7)breakpoint

This function drops you into the debugger at the call site. Specifically, it calls sys.breakpointhook(), passing args and kws straight through. By default, sys.breakpointhook() calls pdb.set_trace() expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly import pdb or type as much code to enter the debugger. However, sys.breakpointhook() can be set to some other function andbreakpoint() will automatically call that, allowing you to drop into the debugger of choice.

此函数会将您置于调用站点的调试器中。 具体来说,它调用sys.breakpointhook(),直接传递args和kws。 默认情况下,sys.breakpointhook()调用pdb.set_trace()期望没有参数。 在这种情况下,它纯粹是一个便利功能,因此您不必显式导入pdb或输入尽可能多的代码来进入调试器。 但是,sys.breakpointhook()可以设置为其他一些函数,而breakpoint()会自动调用它,允许你进入选择的调试器。(端点调试的程序版,翻译是google大致可以明白什么意思就ok了)

(8)class bytearray([source[, encoding[, errors]]])

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.

The optional source parameter can be used to initialize the array in a few different ways:

If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

See also Binary Sequence Types — bytes, bytearray, memoryview and Bytearray Objects.

返回一个新的bytes类型的数组,这个数组内的整数是可变的并且每个数的大小都介于0-256,它拥有大多数可变数组常用方法,也拥有bytes类型的常用方法。

- 如果 source 为整数,则返回一个长度为 source 的初始化数组;

- 如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;

- 如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;

- 如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。

- 如果没有输入任何参数,默认就是初始化数组为0个元素(这部分引用菜鸟教程)

测试范例

(9)class bytes([source[, encoding[, errors]]])

Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x <256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.

Accordingly, constructor arguments are interpreted as for bytearray().

Bytes objects can also be created with literals, see String and Bytes literals.

See also Binary Sequence Types — bytes, bytearray, memoryviewBytes Objects, and Bytes and Bytearray Operations.

返回一个新的bytes对象,但是这个对象是一个0-256大小的不可变数组,类似与一个不可变类型的bytearray.重点在于(不可变类型),用法我还真的不了解,希望以后能用上。

测试范例

(10)callable(object)

Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.

New in version 3.2: This function was first removed in Python 3.0 and then brought back in Python 3.2.

如果这个object参数有回调则返回True,否则返回False。即使这个方法返回了true,结果依然可能会报错,但是如果返回False,那么结果肯定不会成功。如果调用callable则会返回一个新实例,前提是这个类可调用(类里定义了__call__()方法)。这个方法在3.0版本中被移除,在3.2版本中又加进来了。。。好奇怪。

测试范例

现在时间是晚上10点整,先写到这,准备收拾东西回家。

上一篇下一篇

猜你喜欢

热点阅读