01-python3 basic - basic data types

Python3 basic data types

Standard data types

Python3 There are six standard data types:

  • Number (Digital)
  • String (String)
  • List (list)
  • Tuple (tuple)
  • Set (collection)
  • Dictionary (dictionary)

Python3 of six standard data types:

  • Immutable data (3): Number The (digital), String (String), Tuple (tuple);
  • Variable data (3): List (list), Dictionary (dictionary), Set (collection).
digital
基本方式
    a, b, c, d = 20, 5.5, True, 4+3j
    print(type(a), type(b), type(c), type(d))
    # 通过isinstance判断类型
    print(isinstance(a,int))
    
+ isinstance 与 type 的区别
1、type()不会认为子类是一种父类类型。
2、isinstance()会认为子类是一种父类类型。

+ del 删除引用,类似php中的 unset
del var1,var2

+ 数值运算符
基本的运算符:
    + - * /得到浮点数 //取整 %取余 **乘方
String (String) immutable
变量[头下标:尾下标]

+ 原始字符串 r
print(r'Run\noob')
会原样输出,不会发生转义

+ 运算
通过+进行拼接,通过 * 号进行重复
List (list) variable
变量[头下标:尾下标]

+ 可以通过下标进行截取列表
list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]
tinylist = [123, 'runoob']
 
print (list)            # 输出完整列表
print (list[0])         # 输出列表第一个元素
print (list[1:3])       # 从第二个开始输出到第三个元素
print (list[2:])        # 输出从第三个元素开始的所有元素
print (tinylist * 2)    # 输出两次列表
print (list + tinylist) # 连接列表

+ 列表基础操作
a = [1, 2, 3, 4, 5, 6]
a[2:5] = []
a #[1,2,6]
Tuple (tuple) is similar to a list, but can not be modified
tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )
tinytuple = (123, 'runoob')
 
print (tuple)             # 输出完整元组
print (tuple[0])          # 输出元组的第一个元素
print (tuple[1:3])        # 输出从第二个元素开始到第三个元素
print (tuple[2:])         # 输出从第三个元素开始的所有元素
print (tinytuple * 2)     # 输出两次元组
print (tuple + tinytuple) # 连接元组

string, list and tuple belongs Sequence (sequence).

Set (collection)
集合(set)是由一个或数个形态各异的大小整体组成的,构成集合的事物或对象称作元素或是成员。
基本功能是进行成员关系测试和删除重复元素。
可以使用大括号 { } 或者 set() 函数创建集合
# 注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
示例代码:
    student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
    print(student)   # 输出集合,重复的元素被自动去掉
    # 成员测试
    if 'Rose' in student :
        print('Rose 在集合中')
    else :
        print('Rose 不在集合中')

    # set可以进行集合运算
    a = set('abracadabra')
    b = set('alacazam')

    print(a)

    print(a - b)     # a 和 b 的差集
    print(a | b)     # a 和 b 的并集
    print(a & b)     # a 和 b 的交集
    print(a ^ b)     # a 和 b 中不同时存在的元素
Dictionary (dictionary)
字典(dictionary)是Python中另一个非常有用的内置数据类型。
列表是有序的对象集合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典是一种映射类型,字典用 { } 标识,它是一个无序的 键(key) : 值(value) 的集合。
# 键(key)必须使用不可变类型。
# 在同一个字典中,键(key)必须是唯一的。
示例代码:
    dict = {}
    dict['one'] = "1 - 菜鸟教程"
    dict[2]     = "2 - 菜鸟工具"

    tinydict = {'name': 'runoob','code':1, 'site': 'www.runoob.com'}

    print (dict['one'])       # 输出键为 'one' 的值
    print (dict[2])           # 输出键为 2 的值
    print (tinydict)          # 输出完整的字典
    print (tinydict.keys())   # 输出所有键
    print (tinydict.values()) # 输出所有值

Data Conversion:

Several built-in functions may be performed to convert between data types. The function returns a new object that represents the converted value.

function description
[int(x ,base]) The integer x is converted to a
float(x) The transition to a floating point x
[complex(real ,imag]) Creating a complex
str(x) The object is converted to a string x
repr (x) The string object is converted to an expression x
eval(str) Python expression for calculating effective in the string, and returns an object
tuple(s) Converting the sequence s is a tuple
list(s) Converting the sequence s is a list of
set(s) Converted into a set of variable
dict(d) Create a dictionary. d must be a (key, value) tuples.
frozenset(s) Is converted to a set of immutable
chr(x) Convert an integer to a character
words (x) A character into its integer value
hex(x) Convert an integer to a hexadecimal string
oct(x) Convert an integer to an octal string

Guess you like

Origin www.cnblogs.com/turboni/p/11449437.html