您好,欢迎访问一九零五行业门户网

浅谈Python中的数据类型

数据类型:
float — 浮点数可以精确到小数点后面15位int — 整型可以无限大bool — 非零为true,零为falselist — 列表
float/int:
运算符:
/ — 浮点运算除
 // — 当结果为正数时,取整; 11//5 =2; 11//4 = 2
当结果为负数时,向下取整;-11//5=-3; -11//4=-3
当分子分母都是float,结果为float型
** —  计算幂; 11**2 =121
% — 取余
其他数学运算:
1.分数:
import fractions;
fractions.fraction(1,3) — 1/3
import math;
—math.sin()
—math.cos()
—math.tan()
—math.asin()
math.pi —3.1415926…
math.sin(math.pi/2) — 1.0
math.tan(math.pi/4) — 0.9999999999…
math.sin(); math
list:
创建: a_list = [‘a', ‘b', ‘mpilgrim', ‘z', ‘example']
a_list[-1] — ‘example'
a_list[0] — ‘a'
a_list[1:3] — [‘b', ‘mpilgrim', ‘z']
a_list[:3] — [‘a', ‘b', ‘mpilgrim' ]
a_list[3:] — [‘z', ‘example']
a_list[:]/a_list — [‘a', ‘b', ‘mpilgrim', ‘z', ‘example']
*注:a_list[:] 与a_list 返回的是不同的list,但它们拥有相同的元素
a_list[x:y]— 获取list切片,x指定第一个切片索引开始位置,y指定截止但不包含的切片索引位置。
向list添加元素:
a_list = [‘a']
a_list = a_list + [2.0, 3] — [‘a', 2.0, 3]
a_list.append(true) — [‘a', 2.0, 3, true]
a_list.extend([‘four','ω']) — [‘a', 2.0, 3, true,'four','ω']
a_list.insert(0,'ω') — [‘ω','a', 2.0, 3, true,'four','ω']
list其他功能:
a_list = [‘a', ‘b', ‘new', ‘mpilgrim', ‘new']
a_list.count(‘new') — 2
a_list.count(‘mpilgrim') — 1
‘new' in a_list — true
a_list.index(‘new') — 2
a_list.index(‘mpilgrim') — 3
a_list.index(‘c') — through a exception because ‘c' is not in a_list.
del a_list[1] — [‘a', ‘new', ‘mpilgrim', ‘new']
a_list.remove(‘new') — [‘a', mpilgrim', ‘new']
注:remove只删除第一个'new'
a_list.pop() — 'new'/[‘a', mpilgrim' ](删除并返回最后一个元素)
a_list.pop(0) — ‘a' / [‘mpilgrim'] (删除并返回第0个元素)
空列表为假,其他列表为真。
元组(元素是不可变的列表):
定义:与列表的定义相同,除了整个元素的集合用圆括号而,不是方括号闭合
a_tuple = (“a”, “b”, “mpilgrim”, “z”, “example”)
a_tuple = (‘a', ‘b', ‘mpilgrim', ‘z', ‘example')
tuple 只能索引,不能修改。
元组相对于列表的优势:
1.速度快
2.“写保护”,更安全
3.一些元组可以当作字典键??
内置的tuple()函数接受一个列表参数并将列表转化成元组
同理,list()函数将元组转换成列表
同时赋多个值:
v = (‘a',2, true)
(x,y,z) = v — x=‘a', y=2, z=true
range() — 内置函数,进行连续变量赋值
(monday, tuesday, wednesday, thursday, friday, saturday, sunday) = range(7)
monday — 0
    thursday — 3
    sunday — 6
        range() — 内置函数range()构建了一个整数序列,range()函数返回一个迭代器。
集合(里面的值是无序的):
创建集合:用逗号分隔每个值,用大括号{}将所有值包括起来。
    a_set = {1}
    type(a_set) —
    以列表为基础创建集合:
    a_list = [‘a', ‘b', ‘mpilgrim', true, false, 42]
    a_set = set(a_list)
    a_set — {‘a', ‘b', ‘mpilgrim', true, false, 42}
    a_set = set() — 得到一个空的set
    a_dic = {} — 得到一个空的dic    
    修改集合:
a_set = {1,2}
    a_set.add(4) — {1,2,4}
    len(a_set) — 3
    a_set.add(1) — {1,2,4}
    a_set.update({2,4,6}) — {1,2,4,6}
    a_set.update({3,6,9}, {1,2,3,5,8,13}) — {1,2,3,4,5,6,8,9,13}
    a_set.update([15,16]) — {1,2,3,4,5,6,8,9,13,15,16}
    a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
    a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
    a_set.remove(15) —{1,2,3,4,5,6,8,9,13}
    a_set.remove(15) — through a exception
    a_set.pop() — return 1 / {2,3,4,5,6,8,9,13}
    注:a_set.pop()随机删掉集合中的某个值并返回该值。
    a_set.clear() — set()
    a_set.pop() — through exception.
集合的其他运算:
a_set = {2,3,4,5,6,8,9,13}
    30 in a_set — false
    4 in a_set — true
    b_set  = {3,4,10,12}
    a_set.union(b_set) — 两个集合的并
    a_set.intersetion(b_set) — 两个集合的交集
    a_set.difference(b_set) — a_set中有但是b_set中没有的元素
    a_set.symmetric_difference(b_set) — 返回所有只在一个集合中出现的元素
    a_set.issubset(b_set) — 判断a_set是否是b_set的子集
    b_set.issuperset(a_set) — 判断b_set是否是a_set的超集
    在布尔类型上下文环境中,空集合为假,任何包含一个以上元素的集合为真。
字典(键值对的无序集合):
创建字典:
a_dic = {‘server':'db.diveintopython3.org',
    ‘databas':'mysql'}
    a_dic[‘server'] — ‘db.diveintopython3.org'
    a_dic[‘database'] — ‘mysql' 
   修改字典:
a_dic[‘user'] = ‘mark'  — {'user': 'mark', 'server': 'db.diveintopython3.org',     'database':     ‘blog'}
    a_dic[‘database'] = ‘blog' —  {'user': 'mark', 'server': 'db.diveintopython3.org',     'database': ‘blog'}
    a_dic[‘user'] = ‘bob' — {'user': 'bob', 'server': 'db.diveintopython3.org',     'database':     ‘blog'}
    a_dic[‘user'] = ‘mark' — {'user': 'bob', ‘uuser': 'mark', 'server':     'db.diveintopython3.org', 'database': ‘blog'}
    注:1.在字典中不允许有重复的键。对现有键赋值将会覆盖原有值;
    2.随时可以添加新的键值对;
    3.字典键区分大小写。
    混合值字典:
suffixes = { 1000:[‘kb', ‘mb', ‘gb', ‘tb', ‘pb', ‘eb', ‘zb', ‘yb'],
        1024: [‘kib', ‘mib', ‘gib', ‘tib', ‘pib' , ‘eib', ‘zib', ‘yib']}
    len(suffixes) — 2
    1000 in suffixes — true
    suffixes[1024] — [‘kib', ‘mib', ‘gib', ‘tib', ‘pib' , ‘eib', ‘zib', ‘yib']
    suffixes[1000][3] — ‘tb'
空字典为假, 所有其他字典为真
以上所述就是本文的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息