3 Built-in Data Types
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)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
>>> import copy as cp
>>> x=[[0,1],[2,3]]
>>> y = cp.deepcopy(x)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.