zoukankan      html  css  js  c++  java
  • Python 笔记——3 数据类型

     

    上一张讲了Python的数据运算,今天来讲Python的内置数据类型(Built-in Types)的用法。

    1、布尔运算符

      布尔运算符有三个,按优先级排列,分别是与(and)、或(or)、非(not)

      由于not的优先级非常低,因此not x == y 和 not (x == y)是没有区别的,但是 x == not y,会报错。因为not 比 ==的优先级要低。

    2、比较运算

      比较运算详情看下表。

    符号 含义
    > 大于
    >= 大于等于
    < 小于     
    <= 小于等于
    != 不等于
    == 等于
    is 对象类型判断
    is not 非该对象类型

    3、数字类型

      在Python中,有三种数字类型,int、float和complex。

      对于数字类型,除了加减乘除以外,还有些比较常用的函数。

      abs(x)作用是对x取绝对值,int(x)是把x转化为int类型输出,float(x)是把x转化为浮点类型,complex(x,y)是生成一个x+y j的复数。

      c.conjugate()是求复数c的共轭复数,divmod(x,y)得出的结果是(x/y,x%y),pow(x,y)是一个幂函数,作用是求x的y次幂。

    >>> abs(-3)
    3
    >>> int(3.3)
    3
    >>> float(3)
    3.0
    >>> complex(3,3)
    (3+3j)
    >>> complex(3,3).conjugate()
    (3-3j)
    >>> divmod(5,3)
    (1, 2)
    >>> pow(2,2)
    4

       在所有的实数类型中,还有可以使用到以下几个函数。不过在Python2.X和3.X系列中,功能不大一样。

       首先,要使用这几个语句,要使用import语句导入math。

       math.trunc(x)的功能是截取数字的整数部分,round(x,n)的功能是将x精确到某一位,假如没有输入n,则取整。

       math.floor(x)的功能是获取不大于x的整数,math.ceil(x)的功能是获取不小于x的整数。

    >>> import math
    >>> math.trunc(4.4)
    4
    >>> round(2.25,1)
    2.2
    >>> round(5/2)
    2
    >>> math.floor(3.9)
    3
    >>> math.ceil(4.1)
    5

    4、序列类型--List、tuple、range

      4.1通用的序列操作

      首先讲in 和 not in,这两个函数可以在序列类型甚至是字符串中使用。in的功能是查询一个元素是否在该序列或者字符串中,而not in则相反。

    >>> "ap" in "apple"
    True
    >>> "ap" in "orange"
    False
    >>> 1 in [1,2,3]
    True
    >>> 4 not in [1,2,3,4]
    False

       +和*这两个运算符的功能在前面字符串讲解的时候已经讲到过了,*表示把前面的对象重复n遍,+表示前后两个对象的结合。

    >>> lists = [[1]] * 3
    >>> lists
    [[1], [1], [1]]
    >>> lists + [2]
    [[1], [1], [1], 2]

       像前面一章介绍的,a[x],a[x:y],a[x:y:z]。用于获取序列中,某个值或者某几个值。a[x:y:z]表示在序列a中,从x开始,到y,每个z个元素取一个。 

    >>> lists = [1,2,3]
    >>> lists[1]
    2
    >>> lists[1:2]
    [2]
    >>> lists[0:3]
    [1, 2, 3]
    >>> lists[0:3:1]
    [1, 2, 3]
    >>> lists[0:3:2]
    [1, 3]

      len(x)用来求x的长度,max(x)、min(x)分别用来求最大、最小值,y.count(x)计算x在y中出现的次数,x.index(i,j,k),i表示要查找的元素,j为开始位置,k为结束位置,这个函数返回i在x中出现的第一个位置。一旦这个元素没有在要查找的范围内,就会报ValueError的错误。

    >>> lists = [1,2,2,3,3,3]
    >>> len(lists)
    6
    >>> max(lists)
    3
    >>> min(lists)
    1
    >>> lists.count(3)
    3
    >>> lists.index(3,0,4)
    3

      4.2可变序列类型操作

      除了最基本的赋值以外,还包括s.append(x),s.clear(),s.insert(i, x),s.pop([i]),s.remove(x),s.reverse()这几个函数。append(x)就是在序列的最后附加一个x,clear()的功能是清除序列中所有元素,insert(i,x)是在序列的位置i上插入一个元素,pop([i])是返回这个元素并出栈,remove(x)是去掉这个序列中的第一个元素x,erverse()是将序列反置。

    >>> lists[0] = 4
    >>> lists = [1,2,3]
    >>> lists
    [1, 2, 3]
    >>> lists = [1,2,3]
    >>> lists[0] = 4
    >>> lists[0]
    4
    >>> lists[1:2] = [3,2]
    >>> lists
    [4, 3, 2, 3]
    >>> lists.append(5)
    >>> lists
    [4, 3, 2, 3, 5]
    >>> del lists[4]
    >>> lists
    [4, 3, 2, 3]
    >>> lists.clear()
    >>> lists
    []

       4.3 字符串

      字符串不得不说是程序中最常用的变量,而且它的函数非常多,在这里介绍几个常用的。

    >>> "abc".capitalize()    #首字母大写
    'Abc'
    >>> "abc".isalpha()       #是否全部是字母
    True
    >>> "abc123".isalpha() 
    False
    >>> "123".isdecimal()   #是否全部是十进制数
    True
    >>> "abc".isdecimal()    
    False
    >>> "123".isdigit()         #是否全部是数字
    True
    >>> "abc123".isalnum()    #是否是文字和数字
    True
    >>> "if".isidentifier()      #是否是合法的标识符
    True
    >>> "123".isidentifier()  
    False
    >>> "abc".islower()        #是否都是大写
    True
    >>> "ABC".isupper()        #是否都是小写    
    True 
    >>> "abcabcabc".count("ca",4,8)  #查看"ca"在字符串中第四位开始到第八位出现多少次
    1
    >>> "a,b,c".split(',')           #用','分割字符串
    ['a', 'b', 'c']
    >>> 

      4.4 set和frozenset

      这两个是python的集合,区别是前者可变,后者不可变。集合一般用来测试成员变量、移除在序列中的重复成员、数学运算等等。但集合不提供index、slicing等功能。创建一个集合可以使用set([iterable])。下面演示几个set的方法。

      

    >>> sets = set([1,2,3])                    #创建set
    >>> sets
    {1, 2, 3}
    >>> 1 in sets                                   #1是否在集合sets中
    True
    >>> other = set([1,2,3,4])
    >>> other2 = set([1,2,3])
    >>> sets <= other                             #<=的意思是真包含
    True
    >>> sets < other                               #<的意思是包含
    True
    >>> sets < other2                             #很明显{1,2,3}和{1,2,3}不是包含关系
    False
    >>> sets.union(other)                         #求sets和other的并集
    {1, 2, 3, 4}
    >>> sets.intersection(other)                 #求sets和other的交集
    {1, 2, 3}
    >>> sets.difference(other)                   #从sets中寻找与other不同的元素
    set()
    >>> other.difference(sets)             
    {4}    

      4.6 mapping

      mapping是一个哈希类的对象,或者我们叫他字典。他的每一个元素都是键值对(key:value或是value:key)。

      使用方法如下:

      

    >>> a = dict(one=1,two=2)
    >>> b = {'one':1,'two':2}
    >>> c = dict(zip(['one','two'],[1,2]))
    >>> d = dict([('two',2),('one',1)])
    >>> e = dict({'two':2,'one':1})
    >>> a == b == c == d == e
    True
    >>> a.keys()
    dict_keys(['one', 'two'])
    >>> a.values()
    dict_values([1, 2])

      

      这一章大致介绍到这里,下一章介绍流程控制。一些没有介绍到的函数,后面用到的话会作介绍。

  • 相关阅读:
    1062 Talent and Virtue (25 分)
    1083 List Grades (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1121 Damn Single (25 分)
    1120 Friend Numbers (20 分)
    1084 Broken Keyboard (20 分)
    1092 To Buy or Not to Buy (20 分)
    数组与链表
    二叉树
    时间复杂度与空间复杂度
  • 原文地址:https://www.cnblogs.com/44iter/p/3149215.html
Copyright © 2011-2022 走看看