Python基础-第二天
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础-第二天相关的知识,希望对你有一定的参考价值。
本篇内容:
1.列表(list)
2.元组(tuple)
3.字典(dict)
4.字符串的操作
一、列表(list)
1.定义列表
list_name = ["元素1", "元素2"]
●一对中括号[]就是定义列表;
●元素是字符串的话要用双引号或单引号引起来;
2.取列表中的元素
●显示列表的全部内容
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> print(city) [‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘]
●取列表中单个元素
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> print(city[0]) beijing >>> print(city[3]) nanjing >>> print(city[-1]) nanjing # -1就代表列表的最后一个元素
上面是通过下标取列表中的元素,下标从零开始。
再补充一个小内容,通过for循环取元素:
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> for place_name in city: print(place_name) -----以下为显示的内容----- beijing shanghai guangzhou nanjing
3.切片
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> print(city[0:2]) [‘beijing‘, ‘shanghai‘] # 显示的是下标0到2之间的所有元素,但不包括下标2的内容。 >>> print(city[-3:-1]) [‘shanghai‘, ‘guangzhou‘] # 注意,从列表中取下标的顺序是从左往右。 >>> print(city[:2]) [‘beijing‘, ‘shanghai‘] # 如果开始下标为0,可以省略不写。 >>> print(city[1:]) [‘shanghai‘, ‘guangzhou‘] # 如果结束下标是-1,也可以省略不写。 >>> print(city[:]) [‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘] # 当开始下标和结束下标都省略了,就是代表列表中所有的元素 >>>city = ["beijing", "shanghai", "guangzhou", "nanjing", "wuhan"] >>> print(city[0:-1:2]) #[开始下标,结束下标,步长] [‘beijing‘, ‘guangzhou‘] #注意,最后的wuhan没有显示,是顾头不顾尾的 >>> print(city[::2]) [‘beijing‘, ‘guangzhou‘, ‘wuhan‘] #省略了开头和结尾,wuhan就显示了。
4.增加元素
●追加:
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> city.append("wuhan") >>> print(city) [‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘, ‘wuhan‘] # 增加内容的方式为追加,所以新加的内容是在列表最后面
●插入:
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> city.insert(1, "wuhan") >>> print(city) [‘beijing‘, ‘wuhan‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘] # 将新增的内容插在指定下标处
5.更改列表中的内容
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> city[3] = "wuhan" >>> print(city) [‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘wuhan‘]
6.删除列表中的内容
●使用del删除
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> del city[0] >>> print(city) [‘wuhan‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘] >>> del city #删除整个列表
●使用remove删除
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> city.remove("shanghai") >>> print(city) [‘beijing‘, ‘wuhan‘, ‘guangzhou‘, ‘nanjing‘]
7.查找列表中的元素
●使用index查找唯一的元素
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> print(city.index("shanghai")) 1 # 显示的是该元素的下标
●使用count查找相同元素的个数
>>> city = ["beijing", "shanghai", "guangzhou", "beijing"] >>> print(city.count("beijing")) 2 # 显示的是beijing这个元素总共有多少个
8.清空列表中的元素
>>> city = ["beijing", "shanghai", "guangzhou", "nanjing"] >>> city.clear() >>> print(city) []
9.合并列表
>>> city1 = ["beijing", "shanghai"] >>> city2 = ["guangzhou", "nanjing"] >>> city1.extend(city2) >>> print(city1, city2) [‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘] [‘guangzhou‘, ‘nanjing‘] # 注意,city2列表还在哦!
二、元组(tuple)
1.什么是元组
元组其实跟列表差不多,也是存一组数,只不过它一旦创建,便不能再修改,只能切片和查询,也就是说它只有2个方法,一个是count,一个是index。
2.元组的定义
tuple_name = ("元素1", "元素2")
3.取元组中的元素
>>> city = ("beijing", "shanghai", "guangzhou", "nanjing") >>> print(city) (‘beijing‘, ‘shanghai‘, ‘guangzhou‘, ‘nanjing‘) >>> print(city[0]) beijing >>> print(city[0:2]) (‘beijing‘, ‘shanghai‘) # 分片和列表是一样的哦
4.查找元组中的元素
>>> city = ("beijing", "shanghai", "guangzhou", "nanjing") >>> print(city.index("shanghai")) 1 # 显示的是该元素的下标 >>> city = ("beijing", "shanghai", "guangzhou", "beijing") >>> print(city.count("beijing")) 2 # 显示的是beijing这个元素总共有多少个
三、字典(dict)
1.介绍字典
字典一种key-value的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
2.字典的定义
>>> dict_name = { "key1": "value1", "key2": "value2", >>> }
●字典中key是唯一的,这也说明字典天生具有去重功能;
●字典没有下标,查找字典内容是通过key去查找;
●字典是无序的,每次显示字典内容时,都会发现内容排列的顺序都不一样;
3.查找字典中的内容
>>> city = { "beijing": "haidianqu", "shanghai": "pudongxinqu", "guangdong": "guangzhou", >>> } >>> print(city["beijing"]) haidianqu >>> print(city.get("beijing")) haidianqu >>> print(city.get("hubei")) None # 当不确定key是否存在时,可以使用这种方法,如果没有该key就返回None,而不会报错 >>>
4.修改和添加内容
>>> city = { "beijing": "haidianqu", "shanghai": "pudongxinqu", "guangdong": "guangzhou", >>> } >>> city["beijing"] = "chaoyangqu" # 有该key时就是修改内容 >>> print(city) {‘shanghai‘: ‘pudongxinqu‘, ‘guangdong‘: ‘guangzhou‘, ‘beijing‘: ‘chaoyangqu‘} >>> city["hubei"] = "wuhan" # 没有该key时,就增加内容 >>> print(city) {‘shanghai‘: ‘pudongxinqu‘, ‘beijing‘: ‘chaoyangqu‘, ‘hubei‘: ‘wuhan‘, ‘guangdong‘: ‘guangzhou‘}
5.删除
>>> city = { "beijing": "haidianqu", "shanghai": "pudongxinqu", "guangdong": "guangzhou", >>> } >>> del city["beijing"] # 将指定的key删掉 >>> print(city) {‘guangdong‘: ‘guangzhou‘, ‘shanghai‘: ‘pudongxinqu‘} >>> city.pop("shanghai") # 将指定的key删掉 >>> print(city) {‘guangdong‘: ‘guangzhou‘}
6.判断key是否存在
>>> city = { "beijing": "haidianqu", "shanghai": "pudongxinqu", "guangdong": "guangzhou", >>> } >>> print("beijing" in city) # 指定的key存在时返回True,不存在时返回False。 True
7.合并字典
>>> city1 = { "beijing": "haidianqu", "shanghai": "pudongxinqu", >>> } >>> city2 = { "beijing": "chaoyangqu", "guangdong": "guangzhou", >>> } >>> city1.update(city2) >>> print(city1) {‘shanghai‘: ‘pudongxinqu‘, ‘beijing‘: ‘chaoyangqu‘, ‘guangdong‘: ‘guangzhou‘} # 两个字典中都有相同的key,但value不同,合并后的value是city2中的value # 两个字典中不一样的key,直接是合并后字典的内容
8.在for循环中使用
>>> city = { "beijing": "haidianqu", "shanghai": "pudongxinqu", "guangdong": "guangzhou", >>> } >>> for place_name in city: >>> print(place_name) guangdong beijing shanghai # 只显示key哦 >>> for place_name in city: >>> print(place_name, city[place_name]) guangdong guangzhou beijing haidianqu shanghai pudongxinqu # 这样做key和value就都显示出来了
四、字符串的操作
>>> print("清单".center(10, "+")) ++++清单++++ # 总共只会显示出10个字符串,指定内容如果没有10个字符串,在两端分别用+补齐
>>> print("--\t--".expandtabs(tabsize=5)) -- -- # Tab键等于5个空格
>>> print("abcdefghijk".find("d")) 3 # 这里返回的是下标哦,是不是瞬间想到了字符串也能做切片。 >>> print("abcdefghijk"["abcdefghijk".find("d"):7]) defg # 依然是顾头不顾尾
>>> print("abc{d}e{f}".format(d="y", f="z")) abcyez # 内容替换
>>> print("1".isdigit()) True >>> print("a".isdigit()) False # 判断是不是一个整数
>>> print("abcD".isalpha()) True >>> print("abcd1".isalpha()) False # 判断内容中是否只有字母
>>> print("abcdefg".islower()) True # 判断内容中是不是只有小写字母 >>> print("ABCD".isupper()) True # 判断内容中的英文是不是都是大写
>>> print("#".join(["a", "b", "c"])) a#b#c # 将列表转换成字符串,并且以指定分隔符隔开
>>> print("aBCD".lower()) abcd # 大写变小写 >>> print("Abcd".upper()) ABCD # 小写变大写
>>> print("bcadeafgh".split("a")) [‘bc‘, ‘de‘, ‘fgh‘] # 指定分隔符,将内容转化成列表
本文出自 “12031302” 博客,谢绝转载!
以上是关于Python基础-第二天的主要内容,如果未能解决你的问题,请参考以下文章