(03)数据类型

type() 查看一个变量名的类型

■ 对象
    拥有3个特性   a.身份 用id() 可以查    b.类型  用type() 查    c.值

■ 数字
   ● 整型 int  -2,147,483,648  到  2,147,483,647    超过就会变成长
              long
   ● 浮点 float   数值含有小数点
   ● 复数 complex    c=3.14j

=============序列 start==============
查对象所具有的方法
dir(list)    列出对象所有的方法
help(list.append)   查看具体某一个方法的用法

序列: 可以通过索引和切片来操作数据
  ● len()                       求序列长度
  ● +                           连接2个序列
  ● *                            重复序列元素
  ● in                           判断元素是否在序列中
  ● max()                     返回最大的值
  ● min()                      返回最小的值
  ● cmp(tuple1,tuple2)  比较2个的序列值是否相同

■ 字符串 | 对象名string (不可变)  单引号 '   双引号 "  三引号 """   '''(保持格式)   "" 用于转义符号
               “ ”换行  
      ● 长字符串用三引号 """ 

      ● 原始字符串 用r带头

      ● unicode字符串 用u 带头

      ● 索引  从0开始
           a='abcde'
           print a[0]      
      ● 切片 
           print a[1:4]   不包括结束点,要加1计算
           print a[:4]  开头到第4个
           print a[4:]
           print a[::2] 步长为2
       ● 查一下具有的方法  dir(str)     help(str.xxx)

■ 列表 | 对象名list (可变)
    listmilo=["milo","30","male"]
      ●修改
        listmilo[0]="zou"
      ● 增加
       listmilo.append("12345678")
      ●删除
       listmilo.remove("12345678")  这是列表的方法
       del(listmilo[3])   这是系统函数
       zip()  可从两个列表分别取值,生成了一个新的列表
       ● 查一下具有的方法  dir(list)      help(list.xxx)     

      sqdEvens =[x**2 for x in rang(8) if not x%2] 

■ 元组  | 对象名 tuple (不可变)
   空元组  myempty =()
   单个元组  singleton=(2,)
   一般元组   zoo=("wolf","elephant","penguin")

   t=("milo",30,"male")

       ● 查一下具有的方法  dir(tuple)       help(tuple.xxx)
  
==============序列 end================

■ 字典 | 对象名dict  (可变,最灵活) 唯一的映射类型(哈希表) key=>value
      ●定义
         dic={'name':'milo','age':25,'gender':'male'}     #可以嵌套定义
         print dic['name']
         print dic.get('name')   # 这样,当key 不存在里,不会出现异常

          fdict=dict([['x',1],['y',2]])
          fdict=dict(x=1,y=2)
          fdict=dict({'x':1,'y':2})
          fdict=dict(zip(('x','y'),(1,2)))
          -----{'y': 2, 'x': 1}  

         ddict={}.fromkeys(('x','y'),-1)
          ---{'y': -1, 'x': -1}

      ● 遍历
           ◆   循环
                for key in dict1.keys(): 
                for k in fdict.keys():
          print fdict[k]
  
           ◆   迭代器
                for key in dict1:
                 for k in fdict:
          print fdict[k]

         dict.keys()    # 返回所有的键
         dict.values()  # 返回所有的值         

      ●修改
           ◆ 增加
                fdict['tel']=123456
           ◆ 删除
                dict.pop(key)
                dict.clear()   删除所有字典的元素
                del()   系统函数

       ● 查一下具有的方法  dir(dict)     help(dict.xxx)  

原文地址:https://www.cnblogs.com/toby2chen/p/5197364.html