Pantheon (python) data types

1. Number (number)

1. Integer type

    正负都可以的整数。(-1 0 1 )

2. Floating point

    就是小数,小数可以用数学写法(1.1  2.33),也可以用科学计数法表示(1.1e6   2.33e6)

3. Boolean value

一个布尔值只有 True 、False 两种值。
布尔值可以进行 与(and)或(or)非(not) 计算

Integer and floating-point are stored differently in the computer. Integer operations are always accurate, and floating-point types will have rounding deviations.

Second, the string (string)

Generally refers to any text enclosed in single quotation marks '' or double quotation marks "".
r'' or r"" means that the characters in quotation marks have no meaning and are in plain text form.

a=bbb
print('a')     #输出的就是 bbb
print(r'a')    #输出的就是 a

3. List (list)

A list is an ordered collection from which elements can be added and removed at any time.

a=[1,2,3,4,5]           #一般用中括号 [ ] 来括起来

The index value starts at 0, and -1 is the starting position from the end.

print (a)                   # 输出完整列表
print (a[0])              # 输出列表第一个元素
print (a[1:3])           # 从第二个开始输出到第三个元素
print (a[1:])             # 输出从第二个元素开始的所有元素
len(a)                      #统计 list 元素个数
a.pop(2)                 #删除第3个元素(索引值为2的元素)

Fourth, tuple (tuple)

A tuple is very similar to a list, but a tuple cannot be modified once initialized.
Because tuples are immutable, the code is safer. If possible, use tuple instead of list.

a=(1,2,3,4,5)        #一般用小括号 () 来括起来
a= ()                      # 空元组
a= (1,)                   # 一个元素,需要在元素后添加逗号

Five, set (set)

A set is an unordered sequence of non-repeating elements.
Mainly doing membership testing and removing duplicate elements.
Sets can be created using curly braces { } or the set() function. Note: To create an empty set, you must use set() instead of { }, because { } is used to create an empty dictionary.

a={1,2,3,1}
print(a)             #输出{1,2,3},重复自动去掉
b={0,1,4}
print(a - b)     # a和b的差集      {2,3}
print(a | b)     # a和b的并集       {0,1,2,3,4}
print(a & b)     # a和b的交集      {1}
print(a ^ b)     # a和b中不同时存在的元素  {0,2,3,4}

6. Dictionary

The full name of dict is dictionary, which is also called map in other languages. It uses key-value (key-value) storage and has extremely fast search speed.
Dictionaries exist as key:value pairs. keys are immutable types, and within a dictionary, the value of key must be unique.

a={A:1,B:2,C:3}
print(a[B])       #输出 2

The order in which the dict is stored has nothing to do with the order in which the keys are placed.

Comparing dict and list, dict has the following characteristics:

1、查找和插入的速度极快,不会随着key的增加而变慢;
2、需要占用大量的内存,内存浪费多。

And list is the opposite:

1、查找和插入的时间随着元素的增加而增加;
2、占用空间小,浪费内存很少。

So, dict is a way to trade space for time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976218&siteId=291194637