005 python语法_002
Posted 汁虫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了005 python语法_002相关的知识,希望对你有一定的参考价值。
/* 时间:2018/10/01 目录: 一: list 1 基本用法 二: 字典 1 语法1 2 语法2
3 语法3 三: 集合
1 用法 - {}
2 用法 - set
3 集合运算
*/
一: list
1 基本用法
# coding:utf-8 list = [] print(list) print(type(list)) nInt = 12 fFloat = 12.12 bTrue = True bFalse = False strString = "12.12" # 尾部插入 list.append(nInt) list.append(fFloat) list.append(True) list.append(False) list.append(strString) print(list) # 指定插入 list.insert(1, "insert") # 插入位置 - 索引为1 print(list) # 删除尾部 list.pop() print(list) # 指定删除 list.pop(2) # 删除位置 - 索引为1 print(list) # 指定删除 del list[0] # 删除位置 - 索引为0 print(list) # 数据修改 list[0] = 22 print(list)
[] <class \'list\'> [12, 12.12, True, False, \'12.12\'] [12, \'insert\', 12.12, True, False, \'12.12\'] [12, \'insert\', 12.12, True, False] [12, \'insert\', True, False] [\'insert\', True, False] [22, True, False]
二: 字典
1 字典可以嵌套字典
2 字典内容是无序的
3 字典key值是唯一的
1 语法1
# coding:utf-8 Dictionary = { 1: 999999, 12.12: "12.12", "a": "", "b": 123, "dict": { 1: 123456, 12.12: "12.12", "a": "", "b": 123, "test": { \'KK\': "mm", "jj": "nn" } } } print(Dictionary.keys()) # 查询 print(Dictionary[1]) print(Dictionary["dict"][1]) print(Dictionary["dict"]["test"]["KK"]) print(Dictionary) print(Dictionary["b"]) # 增加内容 Dictionary["asd"] = "123456" print(Dictionary) # 修改内容 Dictionary[1] = "111111" # 进行覆盖 print(Dictionary) Dictionary["asd"] = "111111" # 进行覆盖 print(Dictionary) # 删除内容 del(Dictionary["dict"]) print(Dictionary) print(Dictionary.keys())
dict_keys([1, 12.12, \'a\', \'b\', \'dict\']) 999999 123456 mm {1: 999999, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'dict\': {1: 123456, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'test\': {\'KK\': \'mm\', \'jj\': \'nn\'}}} 123 {1: 999999, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'dict\': {1: 123456, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'test\': {\'KK\': \'mm\', \'jj\': \'nn\'}}, \'asd\': \'123456\'} {1: \'111111\', 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'dict\': {1: 123456, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'test\': {\'KK\': \'mm\', \'jj\': \'nn\'}}, \'asd\': \'123456\'} {1: \'111111\', 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'dict\': {1: 123456, 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'test\': {\'KK\': \'mm\', \'jj\': \'nn\'}}, \'asd\': \'111111\'} {1: \'111111\', 12.12: \'12.12\', \'a\': \'\', \'b\': 123, \'asd\': \'111111\'} dict_keys([1, 12.12, \'a\', \'b\', \'asd\'])
2 语法2
# coding:utf-8 dicts = {"username":"zhangsan", "password":123456} print(dicts.keys()) print(dicts.values()) print(dicts.items()) for key, value in dicts.items(): print("%r is %r" %(key, value))
dict_keys([\'username\', \'password\']) dict_values([\'zhangsan\', 123456]) dict_items([(\'username\', \'zhangsan\'), (\'password\', 123456)]) \'username\' is \'zhangsan\' \'password\' is 123456
3 语法3
# coding:utf-8 nNum = 255 strNum = "123" token = "xxxxxxx" dict = { "nNum": nNum, "strNum": strNum, "strName": "My Name is %s, My age is %d" %(strNum, nNum), "list": [1, 2, nNum, 3], "token": token } print(dict)
{\'nNum\': 255, \'strNum\': \'123\', \'strName\': \'My Name is 123, My age is 255\', \'list\': [1, 2, 255, 3], \'token\': \'xxxxxxx\'}
三: 集合
1 集合是一个无序的不重复元素序列。
2 使用大括号{ }或者set()函数创建集合,
3 创建一个空集合必须用set()而不是{ },因为{ }是用来创建一个空字典。
1 用法 - {}
# coding:utf-8 basket = {\'apple\', \'orange\', \'apple\', \'pear\', \'orange\', \'banana\'} # 显示打印 print(basket) # 显示集合 - 元素去重 print(len(basket)) # 显示个数 - 元素个数 print(type(basket)) # 显示类型 - basket # 判断包含 print(\'orange\' in basket) # 判断包含 print(\'abc\' in basket) # 判断包含 print(\'abc\' not in basket) # 判断不包含 print(\'orange\' not in basket) # 判断不包含 # 增加元素 basket.add("kkk") print(basket) basket.update({"ZZZ"}) print(basket) # 删除元素 basket.remove(\'apple\') print(basket)
# Run Result {\'orange\', \'banana\', \'pear\', \'apple\'} 4 <class \'set\'> True False True False {\'orange\', \'kkk\', \'banana\', \'pear\', \'apple\'} {\'orange\', \'kkk\', \'banana\', \'pear\', \'apple\', \'ZZZ\'} {\'orange\', \'kkk\', \'banana\', \'pear\', \'ZZZ\'}
2 用法 - set
# coding:utf-8 thisSet = set(("google", "baidu", "bing")) print(thisSet) # 显示集合 print(type(thisSet)) # 显示类型 thisSet.add("Facebook") # 增加元素 print(thisSet) # 显示集合
# Run Result {\'baidu\', \'google\', \'bing\'} <class \'set\'> {\'Facebook\', \'baidu\', \'google\', \'bing\'}
3 集合运算
# coding:utf-8 a = set(\'abracadabraK\') b = set(\'alacazam\') print(a) print(type(a)) print(b) print(type(b)) print(a - b) # 显示元素 - b集合中不包含a集合 print(b - a) # 显示元素 - a集合中不包含b集合 print(a | b) # 显示元素 : a和b集合所有元素 - 不重复 print(a & b) # 显示元素 - a和b集合都包含的元素 print(a ^ b) # 显示元素 - 不同时包含于a和b的元素
# Run Result {\'a\', \'d\', \'c\', \'K\', \'b\', \'r\'} <class \'set\'> {\'z\', \'m\', \'a\', \'l\', \'c\'} <class \'set\'> {\'K\', \'b\', \'r\', \'d\'} {\'l\', \'z\', \'m\'} {\'z\', \'m\', \'d\', \'a\', \'c\', \'K\', \'l\', \'b\', \'r\'} {\'a\', \'c\'} {\'K\', \'b\', \'z\', \'m\', \'l\', \'r\', \'d\'}
以上是关于005 python语法_002的主要内容,如果未能解决你的问题,请参考以下文章