第一节课的Python基础知识

Python内置的数据类型

1、number

数字数据类型:包括int(整型,在这里也代表长整型)、float(浮点型)、complex(复数类型)、Bool(布尔类型)。

2、string

字符串:由数字、字母、下划线所组成的一串字符,并且第一个字符不能为数字类型。

3、list

标识符为 [] 中括号。
是一种有序的集合,可变,它支持字符、数字、字符串甚至可以包含列表(所谓嵌套)。

  1. 追加
    使用的是append方法,追加到list的最末尾

    >>> list1 = ['dog','cat','panda',True,15,['xiaole','YT'],'hangry']
    >>> list1.append('thirsty')
    >>> list1
    ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry', 'thirsty']
    
  2. 插入元素
    insert方法,指定特定的下标和要插入的元素。

    >>> list1 = ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry', 'thirsty']
    >>> list1.insert(2,'monkey')
    >>> list1
    ['dog', 'cat', 'monkey', 'panda', True, 15, ['xiaole', 'YT'], 'hangry', 'thirsty']
    
  3. 删除
    使用方法pop(),不带参数,直接删除并返回末尾元素的值。

    >>> list1 = ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry', 'thirsty']
    >>> list1.pop()
    'thirsty'
    >>> list1
    ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry']
    
  4. 删除指定位置元素
    使用方法pop(index),带参数index,直接删除指定下标index并返回删除元素的值。

    >>> list1 = ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry']
    >>> list1.pop(4)
    15
    >>> list1
    ['dog', 'cat', 'panda', True, ['xiaole', 'YT'], 'hangry']
    
  5. 替换
    直接赋值替换即可。

    >>> list1 = ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry']
    >>> list1[0] = 'cute dog'
    >>> list1
    ['cute dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry']
    
  6. 排序
    使用方法sort()。

    >>> list2 = [7,6,8,10,12,25,33,16]
    >>> list2.sort()
    >>> list2
    [6, 7, 8, 10, 12, 16, 25, 33]
    
  7. 倒序
    使用方法reverse()。

    >>> list2 = [7,6,8,10,12,25,33,16]
    >>> list2.reverse()
    >>> list2
    [33, 25, 16, 12, 10, 8, 7, 6]
    
  8. 查找
    找出指定下标的元素,二位数组查找嵌套元素。

    >>> list1 = ['dog', 'cat', 'panda', True, 15, ['xiaole', 'YT'], 'hangry']
    >>> list1[4]
    15
    >>> list1[5][1]
    'YT'
    >>> list1[5]
    ['xiaole', 'YT']
    
  9. +和*的用法
    +是拼接list列表,栗子:

    >>> list1 = [1,2,3]
    >>> list2 = [4,5,6]
    >>> print (list1+list2)
    [1, 2, 3, 4, 5, 6]
    

    *是将一个列表打印多遍,栗子:

    >>> list1 = [1,2,3]
    >>> print (list1 * 2)
    [1, 2, 3, 1, 2, 3]
    

4、tuple

元组,tuple和list非常类似,但是tuple一旦初始化就不可修改,标识符为 () 小括号。
注:当tuple元素只有一个的时候,需要一个逗号消除歧义,例如:

>>> tuple1 = (1)
>>> type(tuple1)
<class 'int'>
>>> tuple2 = (1,)
>>> type(tuple2)
<class 'tuple'>

5、dict

dict全称dictionary,字典,在其它语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度,标识符为 {} 大括号,也有人称之为花括号。

  1. 查找

    >>> dict1 = {'xiaole':98,'xiaohua':65,'xiaoqi':64}
    >>> dict1['xiaohua']
    65
    
  2. 遍历

    1. 遍历key值。
      第一种方法,栗子:
      people = {'first_name':'zhang','age':21,'last_name':'lele','city':'xian'}
      for key in people:
      	print(key+',   ',end='')
      
      运行结果:
      first_name,   age,   last_name,   city,
      
      第二种方法,栗子:
      people = {'first_name':'zhang','age':21,'last_name':'lele','city':'xian'}
      for key in people.keys():
          print(key+',   ',end='')
      
      运行结果:
      first_name,   age,   last_name,   city,
      
    2. 遍历value值
      栗子:
      people = {'first_name':'zhang','age':'21','last_name':'lele','city':'xian'}
      for value in people.values():
      	print(value+',   ',end='')
      
      运行结果:
      zhang,   21,   lele,   xian,
      
    3. 遍历字典项
      栗子:
      people = {'first_name':'zhang','age':'21','last_name':'lele','city':'xian'}
      for item in people.items():
          print(item)
      
      运行结果:
      ('first_name', 'zhang')
      ('age', '21')
      ('last_name', 'lele')
      ('city', 'xian')
      
    4. 遍历字典项
      两种写法,可以带括号,也可以不带,栗子:
      people = {'first_name':'zhang','age':'21','last_name':'lele','city':'xian'}
      for key,value in people.items():	#不带括号时
          print(key+'  '+value)
      for (key, value) in people.items():	#带括号时
          print(key + '  ' + value)
      
  3. 增加

    >>> dict1 = {'xiaole':98,'xiaohua':65,'xiaoqi':64}
    >>> dict1['xiaoliu'] = 87
    >>> dict1
    {'xiaole': 98, 'xiaohua': 65, 'xiaoqi': 64, 'xiaoliu': 87}
    
  4. 删除
    使用方法popitem(),随机删除一组元素。

    >>> dict1 = {'xiaole':98,'xiaohua':65,'xiaoqi':64}
    >>> dict1.popitem()
    ('xiaoqi', 64)
    >>> dict1
    {'xiaole': 98, 'xiaohua': 65}
    
  5. 移除
    使用方法pop,删除指定键的一组元素。

    >>> dict1 = {'xiaole': 98, 'xiaohua': 65}
    >>> dict1.pop('xiaole')
    98
    >>> dict1
    {'xiaohua': 65}
    
  6. 清除
    使用方法clear(),清除所有的元素。

    >>> dict1 = {'xiaole':98,'xiaohua':65,'xiaoqi':64}
    >>> dict1.clear()
    >>> dict1
    {}
    
  7. 合并
    普通的字典合并三种方法。

    1. dict(dict1,**dict2),栗子:

      >>> dict1 = {'location':'xian','code':10023}
      >>> dict2 = {'name':'lele','sex':'boy'}
      #相当于将dict2更新至dict1上面
      #先取出dict1,再执行dict1.update(dict2)
      >>>> print (dict(dict1,**dict2))
      {'location': 'xian', 'code': 10023, 'name': 'lele', 'sex': 'boy'}
      
    2. dict(dict1.items()+dict2.items()),栗子:(仅限于Python2)

      >>> dict1 = {'location':'xian','code':10023}
      >>> dict2 = {'name':'lele','sex':'boy'}
      >>> dict(dict1.items()+dict2.items())
      {'code': 10023, 'sex': 'boy', 'location': 'xian', 'name': 'lele'}
      
    3. dict3 = {} dict3.update(dict1) dict3.update(dict2),栗子:

      >>> dict1 = {'location':'xian','code':10023}
      >>> dict2 = {'name':'lele','sex':'boy'}
      >>> dict3 = {}
      >>> dict3.update(dict1)
      >>> dict3.update(dict2)
      >>> dict3
      {'location': 'xian', 'code': 10023, 'name': 'lele', 'sex': 'boy'}
      
  8. 字典取值

    >>> vals = {'name':'zhangsan'}
    >>> vals['name']
    'zhangsan'
    >>> vals['age']
    # 报错,没有attribute  'age'
    >>> vals.get('age',18)
    18
    # 不报错,如果没有属性attribute就返回18
    

6、set

其他小结

方法range()的使用

生成一个范围的list集合。

  1. 当有一个参数时,从0开始,生成参数个元素。

    >>> list(range(100))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
    
  2. 当有两个参数时,从第一个参数开始,到第二个参数减1,若参数不符合规则,返回空。

    >>> list(range(1,5))
    [1, 2, 3, 4]
    >>> list(range(10,5))
    []
    

获取一个list集合的其中部分元素

先定义一个list集合。

>>> a = list(range(100))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
	
  1. 两个参数
    如果两个元素都存在。

    1. 两个参数都符合规则时,从第一个参数作为索引开始,到第二个参数作为索引减1。

      >>> a = list(range(100))
      >>> a[0:10]
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      >>> a[90:99]
      [90, 91, 92, 93, 94, 95, 96, 97, 98]
      
    2. 两个参数不符合规则时。

      1. 两个参数都存在时,并且后面的值比前面的大,返回空。

        >>> a = list(range(100))
        >>> a[99:90]
        []
        
      2. 当第一个参数不存在时,从起始位置一直到第二个参数减1部分截取。

        >>> a = list(range(100))
        >>> a[:40]
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
        

        当第二个参数不存在时,从第一个参数作为索引一直到list结束部分截取。

        >>> a = list(range(100))
        >>> a[60:]
        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
        

        当参数为负数的时候,可将它转化为证书即可:
        最终结果 = list长度 + (负数参数)

字符串格式化

  1. %d
    对数字的填充。

    >>> 'https;//2515***6@qq.com?page=%d'%2
    'https;//2515***6@qq.com?page=2'
    >>> 'https;//2515***6@qq.com?page1=%d&page2=%d'%(2,3)
    'https;//2515***6@qq.com?page1=2&page2=3'
    
  2. %s
    对字符串的填充。

    >>> 'https;//2515***6@qq.com?name=%s'%'xiaole'
    'https;//2515***6@qq.com?name=xiaole'
    >>> 'https;//2515***6@qq.com?name1=%s&name2=%s'%('xiaole','xiaohua')
    'https;//2515***6@qq.com?name1=xiaole&name2=xiaohua'
    
  3. %f

  4. %x

  5. format
    一对一的去匹配,当带参数0,1时可以颠倒匹配的顺序,详情代码第五六行。

    >>> 'https;//2515***6@qq.com?name1={}&name2={}'.format('huahua','tiantian')
    'https;//2515***6@qq.com?name1=huahua&name2=tiantian'
    >>> 'https;//2515***6@qq.com?name1={0}&name2={1}'.format('huahua','tiantian')
    'https;//2515***6@qq.com?name1=huahua&name2=tiantian'
    >>> 'https;//2515***6@qq.com?name1={1}&name2=	{0}'.format('huahua','tiantian')
    'https;//2515***6@qq.com?name1=tiantian&name2=huahua'
    

注:本文章的信息有的还有空缺,后续会将其查漏补缺,初学者,望见谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值