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
- s[ i : j ] = characters i,…, j - 1
- s[ i : j : m] = characters i, i + m, …, i + m[ (j - i) /m ]
- s[ -i, -j ] = characters n- i, … , n - j -1
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
- list.append( value) append value to the end of the llist.
- len(list) return the number of elements in the list.
- list.extend( list 2 ) append the value of list 2 to the end of the list.
- list.pop( index ) return the value at the given index and remove the value of the list.
- list.remove( value ) remove the first occurence of value from the list.
- list.count( value ) count the number of occurence of value in the list.
- del x[ slice ] delete the elements in slice.
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
- set.add(element) append element to a set
- len(x) returns the number of elements in the set
- set.difference(set 2) return the elements in set which are not in set 2.
- set.intersection(set 2) return the elements of x which are also in set.
- set.remove(element) remove element from the set.
- set.union(set 2) return the set containing all elements of set and set2.
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)
网友评论