3 Built-in Data Types

2015-07-07  本文已影响0人  ArchiChen

basic numeric data type in Python is a 1-dimensional scalar which may be either an integer or a double-precision floating point, depending on the formatting of the number when input.

3.1 Variable names

Variable names with a single leading underscores indicate that the variable is for internal use by a module or class. While indicated to be private, this variable will generally be accessible by calling code.

Double leading underscores, for example __some_private_value indicate that a value is actually private and is not accessible.

3.2 Core Native Data Types

3.2.1 Numeric

integers, floats or complex.

python donnot have a fixed size and so can accommodate numbers which are largeer than maximum the basic integer type can handle.

3.2.1.1 Floating Point

two ways to generate a float

>>> x = 1.0
or
>>> x = float(1)

3.2.1.2 Complex

two ways, first, using j or the function complex()

>>>x = 1j
>>>x = 2 + 3j
>>>x= complex(1)

3.2.1.3 Integers(int and long)

int() can generate an integer

integers can range from -2^31 to 2^31 -1

another integer, known as a long integer, which has no effective range limitation. Long integers are entered using the syntax x = 1L or by calling long(). and Python will automatically convert integers outside of the standard integer range to long integers.

3.2.2 Boolean

keywords: True and False.

also two methods to generate:

x = True
x = bool(1)
x = bool(0)

non-zero, non-empty values generally evaluate to true when evaluated by bool().

Zero or empty values such as bool(0), bool(0.0j), bool(NOne), bool(''), bool([]) are all false.

3.2.3 Strings

3.2.3.1 Slicing a String

n = len(string), the inde of the last character is n - 1, called by s[-1]

3.2.4 List

3.2.4.2 List Function

3.2.5 Tuple

just close to a list but are immutable.

list use [ ] to construct a list, tuple use ( ) and you can also use the function tuple().

Note that tuples containing a single element must contain a comma when created, so that x = (2, ) is assign a tuple to x, while x = (2) assign 2 to x. The latter interprets the parentheses as if they are part of a mathematical formula rather than being used to construct a tuple. x = tuple([2]) can be used to creat a single element tuple.

3.2.6 Xrange

for i in range(1, 10) is fundamental, xrange(a, b, i) creates the sequences that follow the pattern a, a + i, a + 2i.

xrange(b) is the same as xrange(0, b, 1)

3.2.7 Dictionary

Dictionaries are composed of keys(words) and values(definitions). keys must be unique data types(e.g. strings, the most common key), and values can contain any valid Python data type.

3.2.8 Set

set and frozenset only differ in that the latter is immutable, frozenset to set is similar with a tuple to list. Set is not so important but they can be useful when working with messy data.

>>> x = set([’MSFT’,’GOOG’,’AAPL’,’HPQ’,’MSFT’])
>>> x
{’AAPL’, ’GOOG’, ’HPQ’, ’MSFT’}

3.2.8.1 Set Functions

3.3 Python and memory management

To save memory, y = x, x and y point to the same data in computer's memory. x and y sahre the same ID. However, once x is changed, x's ID changed but y's did not, indicating that the data in each variable was stored in different locations. this behavior is both safe and efficient, and common in basic Python immutable types such as int, long, float, complex, string, tuple, frozenset and xrange.

List has a different story. y = x, and x is a list, this assignment does not creat a copy and so change to either variable affect both.

slicing a list copy a list of any immutable parts. if x = [[1, 2], [3, 4]] , y = x, chage to x[0][0] also change y. A function has to be used to copy all mutable parts.

>>> import copy as cp
>>> x=[[0,1],[2,3]]
>>> y = cp.deepcopy(x)
上一篇下一篇

猜你喜欢

热点阅读