zoukankan      html  css  js  c++  java
  • Python学习笔记【第四篇】:基本数据类型

    变量:处理数据的状态

      变量名 = 状态值 类型

    python中有以下基本数据类型:

      1:整形

      2:字符串类型

      3:Bool类型

      4:列表

      5:元祖(不可变)

      6:字典(无序)

      7:集合 (无序、不重复)

    一、整形 int

    # 定义整形
    a = 10
    

      

    整形的基本方法:

     
        def bit_length(self): 
            """ 返回表示该数字的时占用的最少位数 """
     
        def conjugate(self, *args, **kwargs): # real signature unknown
            """ 返回该复数的共轭复数 """
      
        def __abs__(self):
            """ 返回绝对值 """
       
        def __cmp__(self, y): 
            """ 比较两个数大小 """
    
        def __coerce__(self, y):
            """ 强制生成一个元组 """ 
           
        def __divmod__(self, y): 
            """ 相除,得到商和余数组成的元组 """ 
        
        def __float__(self): 
            """ 转换为浮点类型 """ 
       
        def __getnewargs__(self, *args, **kwargs): # real signature unknown
            """ 内部调用 __new__方法或创建对象时传入参数使用 """ 
    
        def __hash__(self): 
            """如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
       
        def __hex__(self): 
            """ 返回当前数的 十六进制 表示 """ 
    
        def __index__(self): 
            """ 用于切片,数字无意义 """
         
        def __init__(self, x, base=10): # known special case of int.__init__
            """ 构造方法,执行 x = 123 或 x = int(10) 时,自动调用,暂时忽略 """ 
    
        def __int__(self): 
            """ 转换为整数 """ 
    
        def __long__(self): 
            """ 转换为长整数 """ 
    
        def __oct__(self): 
            """ 返回改值的 八进制 表示 """ 
    
        def __pow__(self, y, z=None): 
            """ 幂,次方 """  
    		
        def __str__(self): 
            """转换为人阅读的形式,如果没有适于人阅读的解释形式的话,则返回解释器课阅读的形式"""
      
        def __trunc__(self, *args, **kwargs): 
            """ 返回数值被截取为整形的值,在整形中无意义 """
    

     

    二、字符串 string 

    # 定义字符串方式
    str = "hello world"
    

      字符串常用方法:

     
        def capitalize(self):  
            """ 首字母变大写 """
     
        def center(self, width, fillchar=None):  
            """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """
      
        def count(self, sub, start=None, end=None):  
            """ 子序列个数 """
        
        def decode(self, encoding=None, errors=None):  
            """ 解码 """
         
        def encode(self, encoding=None, errors=None):  
            """ 编码,针对unicode """
          
        def endswith(self, suffix, start=None, end=None):  
            """ 是否以 xxx 结束 """
         
        def expandtabs(self, tabsize=None):  
            """ 将tab转换成空格,默认一个tab转换成8个空格 """
     
        def find(self, sub, start=None, end=None):  
            """ 寻找子序列位置,如果没找到,返回 -1 """
        
        def format(*args, **kwargs): # known special case of str.format
            """ 字符串格式化,动态参数,将函数式编程时细说 """
     
        def index(self, sub, start=None, end=None):  
            """ 子序列位置,如果没找到,报错 """
     
        def isalnum(self):  
            """ 是否是字母和数字 """
          
        def isalpha(self):  
            """ 是否是字母 """
           
        def isdigit(self):  
            """ 是否是数字 """
          
        def islower(self):  
            """ 是否小写 """
       
        def join(self, iterable):  
            """ 字符串之间以什么连接 """
    
        def ljust(self, width, fillchar=None):  
            """ 内容左对齐,右侧填充 """
    
        def lower(self):  
            """ 变小写 """
    
        def lstrip(self, chars=None):  
            """ 移除左侧空白 """
            
        def partition(self, sep):  
            """ 分割,前,中,后三部分 包含分隔符"""
    
        def replace(self, old, new, count=None):  
            """ 替换 """
       
        def rsplit(self, sep=None, maxsplit=None):  
         	"""从右边分割"""
    
        def rstrip(self, chars=None):  
            """去除右边空格"""
         
        def split(self, sep=None, maxsplit=None):  
            """ 分割, maxsplit最多分割几次 """
           
        def splitlines(self, keepends=False):  
            """ 根据换行分割 """
       
        def startswith(self, prefix, start=None, end=None):  
            """ 是否起始 """
     
        def strip(self, chars=None):  
            """ 移除两段空白 """
     
        def swapcase(self):  
            """ 大写变小写,小写变大写 """
           
        def translate(self, table, deletechars=None):  
            """
            转换,需要先做一个对应表,最后一个表示删除字符集合
            """
     
        def upper(self):  
            """ 转小写"""
    
        def zfill(self, width):  
            """方法返回指定长度的字符串,原字符串右对齐,前面填充0。"""
    

     

    三、布尔值 Bool

      真或假
      1 或 0
    # 定义Bool类型方式
    b = True
    b = False
    

      

    四、列表 list

    # 定义列表的方式
    li = [0, 1, 2, 3]
    # 或者
    li = list()
    

      列表常用方法

     
        def append(self, p_object): # real signature unknown; restored from __doc__
            """ 追加元素 """
           
        def count(self, value): # real signature unknown; restored from __doc__
            """ 统计列表元素个数 """ 
    
        def extend(self, iterable): # real signature unknown; restored from __doc__
            """ 合并集合 """
           
        def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
            """ 返回指定的值索引 """ 
    
        def insert(self, index, p_object): # real signature unknown; restored from __doc__
            """ 在指定的索引处插入 """
        
        def pop(self, index=None): # real signature unknown; restored from __doc__
            """ 随机删除 """ 
    
        def remove(self, value): # real signature unknown; restored from __doc__
            """ 移除 """
           
        def reverse(self): # real signature unknown; restored from __doc__
            """ 反转输出 """
            pass
    
        def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
            """ 按顺序从小到大输出 """
      
        def __len__(self): # real signature unknown; restored from __doc__
            """ 返回列表长度 """
         
    
        def __reversed__(self): # real signature unknown; restored from __doc__
            """ 反转输出 """
     
        def __setitem__(self, i, y): # real signature unknown; restored from __doc__
     
    

      

    五、元祖 tuple 

    # 定义元祖方式
    t = (1,2,)
    # 或者
    t = tuple()
    

     元祖常用方法

        def count(self, value): # real signature unknown; restored from __doc__
     
        def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
     
        def __add__(self, y): # real signature unknown; restored from __doc__
     
        def __contains__(self, y): # real signature unknown; restored from __doc__
     
        def __len__(self): # real signature unknown; restored from __doc__
       

      

    六、字典 dict

    # 定义字典方式
    d = {'张三':19,'李四':20}
    # 或者
    d = dict()
    

      字典常用方法

     
        def clear(self): # real signature unknown; restored from __doc__
            """ 清除内容 """
     
        def copy(self): # real signature unknown; restored from __doc__
            """ 浅拷贝 """
     
        @staticmethod # known case
        def fromkeys(S, v=None): # real signature unknown; restored from __doc__
            """   """
     
        def get(self, k, d=None): # real signature unknown; restored from __doc__
            """ 根据key获取值,d是默认值 """
     
        def has_key(self, k): # real signature unknown; restored from __doc__
            """ 是否有key """
      
        def items(self): # real signature unknown; restored from __doc__
            """ 所有项的列表形式 """
     
        def iteritems(self): # real signature unknown; restored from __doc__
            """ 项可迭代 """
     
        def iterkeys(self): # real signature unknown; restored from __doc__
            """ key可迭代 """
     
        def itervalues(self): # real signature unknown; restored from __doc__
            """ value可迭代 """
     
        def keys(self): # real signature unknown; restored from __doc__
            """ 所有的key列表 """
     
        def pop(self, k, d=None): # real signature unknown; restored from __doc__
            """ 获取并在字典中移除 """
     
        def popitem(self): # real signature unknown; restored from __doc__
            """ 获取并在字典中移除 """
     
        def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
            """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
     
        def update(self, E=None, **F): # known special case of dict.update
            """ 更新  """
     
        def values(self): # real signature unknown; restored from __doc__
            """ 所有的值 """
     
        def viewitems(self): # real signature unknown; restored from __doc__
            """ 所有项,只是将内容保存至view对象中 """
     
        def viewkeys(self): # real signature unknown; restored from __doc__
            """ D.viewkeys() -> a set-like object providing a view on D's keys """
     
        def viewvalues(self): # real signature unknown; restored from __doc__
            """ D.viewvalues() -> an object providing a view on D's values """
      
        def __contains__(self, k): # real signature unknown; restored from __doc__
     
        def __delitem__(self, y): # real signature unknown; restored from __doc__
    
    

     七、集合 set

       set集合,是一个无序且不重复的元素集合

       集合常用方法

     
        def add(self, *args, **kwargs): # real signature unknown
            """ 添加元素 """ 
     
        def clear(self, *args, **kwargs): # real signature unknown
            """ 清除内容""" 
    		
        def copy(self, *args, **kwargs): # real signature unknown
            """ 浅拷贝  """ 
     
        def difference(self, *args, **kwargs): # real signature unknown
            """ A中存在,B中不存在 """ 
     
        def difference_update(self, *args, **kwargs): # real signature unknown
            """ 从当前集合中删除和B中相同的元素""" 
     
        def discard(self, *args, **kwargs): # real signature unknown
            """ 移除指定元素,不存在不保错  """ 
     
        def intersection(self, *args, **kwargs): # real signature unknown
            """  交集  """ 
    		
        def intersection_update(self, *args, **kwargs): # real signature unknown
            """ 取交集并更更新到A中 """
      
        def isdisjoint(self, *args, **kwargs): # real signature unknown
            """ 如果没有交集,返回True,否则返回False"""
     
        def issubset(self, *args, **kwargs): # real signature unknown
            """  是否是子序列"""
      
        def issuperset(self, *args, **kwargs): # real signature unknown
            """  是否是父序列"""
        
        def pop(self, *args, **kwargs): # real signature unknown
            """ 移除元素 """ 
     
        def remove(self, *args, **kwargs): # real signature unknown
            """  移除指定元素,不存在保错  """ 
     
        def symmetric_difference(self, *args, **kwargs): # real signature unknown
            """ 对称差集 """ 
     
        def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
            """  对称差集,并更新到a中 """
        
        def union(self, *args, **kwargs): # real signature unknown
            """   并集  """
     
        def update(self, *args, **kwargs): # real signature unknown
            """  更新 """
      

      集合的交并差集

    # 列表
    li_one = [1, 2, 3, 4]
    li_two = [1, 4, 5, 6]
    
    # 列表转为set集合
    set_one = set(li_one)
    set_two = set(li_two)
    
    # 求交集(两个集合公共的)
    print("intersection方法:", set_one.intersection(set_two))
    print("&符号:", set_one & set_two)
    
    # 求并集(两个集合合并去重)
    print("union方法:", set_one.union(set_two))
    print("|符号:", set_one | set_two)
    
    # 求差集(第一个集合减去第二个集合去除共有的剩下自己的和他有的)
    print("difference方法:", set_one.difference(set_two))
    print("-符号:", set_one - set_two)
    

      结果:

    引用

    在python中,值是靠引用来传递来的。

    我们可以用id()来判断两个变量是否为同一个值的引用。 我们可以将id值理解为那块内存的地址标示。

    >>> a = 1
    >>> b = a
    >>> id(a) 
    13033816
    >>> id(b)   # 注意两个变量的id值相同
    13033816
    >>> a = 2
    >>> id(a)   # 注意a的id值已经变了
    13033792
    >>> id(b)   # b的id值依旧
    13033816
    >>> a = [1, 2]
    >>> b = a
    >>> id(a)
    139935018544808
    >>> id(b)
    139935018544808
    >>> a.append(3)
    >>> a
    [1, 2, 3]
    >>> id(a)
    139935018544808
    >>> id(b)       # 注意a与b始终指向同一个地址
    139935018544808
    

      

    切片

    切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。

    切片的语法:[起始:结束:步长]

    注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。

    我们以字符串为例讲解。

    如果取出一部分,则可以在中括号[]中,使用:

        name = 'abcdef'
    
         print(name[0:3]) # 取 下标0~2 的字符
    

      

    可迭代对象:能被for 循环的就是可迭代对象

    分类

      可变:列表、字典

      不可变:字符串、int 、元祖、Bool

      注意:如何查看当前类型是否可变——通过id()方法查看,如果第一次和第二次值不一样说明当前类型是不可变类型

      直接访问:数字   

      访问顺序:可以通过索引和下标访问

          字符串、列表、元祖(有序)

      映射:字典(无序的)

      容器:列表、元祖、字典 

  • 相关阅读:
    JS高级-虚拟DOM
    JS高级-异步
    tomcat server.xml中文版
    java中的等于
    eclipse version
    angularjs中父,子,兄之间controller值得传递
    《那一天,那一月,那一年,那一世》-------仓央嘉措
    用jsonp格式的数据进行ajax post请求变成get
    git常用指令
    让div支持placeholder属性/模拟输入框的placeholder属性
  • 原文地址:https://www.cnblogs.com/wendj/p/9149477.html
Copyright © 2011-2022 走看看