- # -*- coding: utf-8 -*-
- """
- Created on Sat Jan 27 01:12:05 2018
- @author: Laughing
- Blog:木子网
- WebSite:www.lisen.me
- Email:lisen@lisen.me
- """
- """
- type命令
- """
- #type()命令用于检查值或者变量的类型
- print(type(5)) #<class 'int'>
- print(type(None)) #<class 'NoneType'>
- print(type(print)) #<class 'builtin_function_or_method'>
- """
- 序列
- """
- #序列是一组按照顺序排列的值
- #python内置的三种序列,字符串、元组和列表
- """
- 元组 - 一种不可变序列,可以包含零个或者多个值
- """
- items = (-6,'cat',(1,2))
- print(items) #(-6, 'cat', (1, 2))
- #单元素元组中,最后一个逗号必不可少
- item = (1,)
- print(item) #(1,)
- #我们可以使用type检查一下元组的类型
- print(type(item)) #<class 'tuple'>
- """
- 元组函数
- """
- # in tup #如果元素x在tup元组中返回True
- # len(tup) #元组tup包含的元组个数
- # tup.count(x) #元组tup中x元素的个数
- # tup.index(x) #元组tup中第一个元素x的索引,如果元组中不包含x,引发ValueError异常
- # 使用+ * 连接元组
- pets = ('dog','cat','bird','dog')
- print('bird' in pets) #True
- print(len(pets)) #4
- print(pets.count('dog')) #2
- print(pets.index('cat')) #1
- #print(pets.index('sss')) #ValueError: tuple.index(x): x not in tuple
- tup1 = (1,2,3)
- tup2 = (4,5,6)
- print(tup1 + tup2) #[1, 2, 3, 4, 5, 6]
- print(tup1 * 2) #[1, 2, 3, 1, 2, 3]
- '''
- 列表---列表是可变的
- '''
- #元组的方法在列表中几乎都是存在的,这里不再赘述
- #单列表只含有一个元素时,可以不包含最后的',',这一点跟元组不太一样
- list1 = [1]
- print(list1) #[1]
- #如果是元组,执行一下语句会报错
- #pets[3] = 'frog' #TypeError: 'tuple' object does not support item assignment
- list2 = ['dog','cat']
- list2[-1] = 'frog'
- print(list2) #['dog', 'frog']
- '''
- 列表函数
- '''
- #s.append(x) #像列表s末尾添加元素x
- #s.count(x) #统计列表s中元素x的个数
- #s.extend(lst) #将列表lst添加到列表s中
- #s.index(x) #返回列表s中第一次x的索引
- #s.insert(i,x) #将元素x插入到指定索引i的前面
- #s.pop(i) #删除并返回索引为i的元素
- #s.remove(x) #删除第一个x元素
- #s.reverse() #反转排列顺序
- #s.sort() #将元素按照升序排列
- def numNote(lst):
- msg = []
- for num in lst:
- if str(num).isdigit():
- if(int(num) < 0):
- msg.append(str(num) + '是负数,')
- else:
- msg.append(str(num) + '是正数,')
- else:
- msg.append(str(num) + '不是有效数值')
- print(msg)
- numNote([-1,2,3,'p','9']) #['-1不是有效数值', '2是正数,', '3是正数,', 'p不是有效数值', '9是正数,']
- #下面看一下append和extend的区别
- #append是插入一个元素,而extend是插入一个列表
- list3 = []
- list3.append('cat')
- print(list3) #['cat']
- list3 = []
- list3.extend('cat')
- print(list3) #['c', 'a', 't']
- #使用pop(index)删除指定索引的元素,如果没有指定索引,默认删除最后一个元素
- print(list3.pop(1)) #a
- print(list3.pop()) #t
- print(list3) #['c']
- list3.remove('c')
- print(list3) #[]
- #列表解析
- #python提供了一种快速创建列表的方法
- #比如下面创建一个包含1~10元素的列表
- print([n for n in range(1,11)]) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- #使用列表解析进行筛选
- nums = [-1,0,6,-4-2,3]
- result = [n for n in nums if n >0]
- print(result) #[6, 3]
- '''
- 字典 --- 字典用于存储键值对
- 1、字典键必须是唯一的
- 2、键必须是不可变的
- '''
- color = {'red':1,'blue':2,'green':3}
- print(color) #{'red': 1, 'blue': 2, 'green': 3}
- color['green'] = 4
- print(color['green']) #4
- '''
- 字典函数
- '''
- #d.items() 返回字典d 键-值对组成的视图,字典变化会同步更新视图
- #d.keys() 返回字典键组成的视图,字典变化会同步更新视图
- #d.values() 返回字典值组成的视图,字典变化会同步更新视图
- #d.get(key) 返回key的值
- #d.pop(key) 删除键为key的值,并返回
- #d.popitem() 返回字典d中某一个键值对
- #d.clear() 删除字典d的所有值
- #d.copy() 复制字典
- #d.fromkeys(s,t) 创建一个新字典,其键来自s,值来自t
- #d.setdefault(key,v) 如果key包含在字典d中,则返回其值,否则,返回v并将(key,v)添加到字典d中
- #d.update(e) 将e中的键值对添加到字典d中;e可以是字典,也可以是键值对序列
- colors = {'red':1,'black':2,'green':3,'blue':4}
- ditems = colors.items()
- print(ditems) #dict_items([('red', 1), ('black', 2), ('green', 3), ('blue', 4)])
- print(colors.pop('red')) #1
- print(ditems) #dict_items([('black', 2), ('green', 3), ('blue', 4)])