3 Built-in Data Types

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,384评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,845评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,148评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,640评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,731评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,712评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,703评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,473评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,915评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,227评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,384评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,063评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,706评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,302评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,531评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,321评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,248评论 2 352

推荐阅读更多精彩内容