大一新生寒假自学python(一)

本文介绍了Python初学者对序列(如字符串)相关操作符(如in和notin)、函数(如index和切片)以及列表的创建、修改、遍历、排序等基础操作的实践与理解,展示了如何使用Python进行元素查找、索引获取、元素插入与删除等基本操作。
摘要由CSDN通过智能技术生成

因为python作者已经学了相当长的时间,因此作者将从目前学到地方开始记录,对于已经学过的知识点,会将个人认为和C语言不同的地方进行选择性的记录

序列的相关操作符和函数

判断某一元素是否在序列中,in为在序列中,not in为不在;若符合条件即输出"True"否则输出“False”

s='helloworld'
#判断元素是否在序列中
print('e'in s)
print('v'in s)
print('v'not  in s)
print('e'not in s)
运行结果
True
False
True
False

使用index()函数输出某一元素在该序列中第一次出现的下标

#输出某一元素在序列中的下标
print(s.index('o'))
运行结果
6

输出该序列中最长(max())和最短(min())的元素

#输出序列中最长和最短的元素
print(max(s))
print(min(s))
运行结果
w
d

序列的切片操作

s='helloword'
#s[开始的下标:结束的下标(不包含下标):步调]
s1=s[0:5:2]
print(s1)
#将输出hlo
#默认开始和步调为0和1
print(s[:5:1])
print(s[:5:])
#默认结束到最后
print(s[0::1])
print(s[5::])
print(s[5:])
print(s[::2])
#当其中一数为负数时对序列逆向输出
print(s[::-1])
print(s[-1:-11:-1]) 
运行结果
hlo
hello
hello
helloword
word
word
hlood
drowolleh
drowolleh

列表的创建和操作

#列表创建的方式一
lst=['hello','world','234354']
print(lst)
#列表创建的方式二
lst1=list('hello word')
print(lst1)
#range(开始的数,结束的数(不包含本身),步调)
lst2=list(range(1,10,2))
print(lst2)
#将列表进行连接
print(lst+lst1+lst2)
#列表*数字表示将该列表输出几次
print(lst*3)
#len()列表的长度
print(len(lst),len(lst1))
#输出列表中最长和最短的元素
print(max(lst))
print(min(lst))
#lst.count()输出元素出现的次数lst.index()输出元素第一次出现的索引
print(lst.count('hello'))
print(lst.index('hello'))
运行结果
['hello', 'world', '234354']
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd']
[1, 3, 5, 7, 9]
['hello', 'world', '234354', 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd', 1, 3, 5, 7, 9]
['hello', 'world', '234354', 'hello', 'world', '234354', 'hello', 'world', '234354']
3 10
world
234354
1
0
[123, 123, 123]

列表的删除:使用del lst对列表进行删除

lst3=[123,123,123]
print(lst3)
#del删除列表
del lst3
print(lst3)
#将出现NameError: name 'lst3' is not defined. Did you mean: 'lst'?的错误

列表的遍历

第一种遍历方式:使用for对列表进行遍历

lst=['hello','ghfg','asdawedwad','wdadwdwa','wdawa']
#使用for对列表进行遍历item可理解为元素
for item in lst:
    print(item)
运行结果
hello
ghfg
asdawedwad
wdadwdwa
wdawa

第二种遍历方法:使用for加索引进行遍历i为索引

#使用for利用索引遍历列表
for i in range(0,len(lst)):
    print(i,'-->',lst[i])
运行结果
0 --> hello
1 --> ghfg
2 --> asdawedwad
3 --> wdadwdwa
4 --> wdawa

第三种遍历方法:使用enumerate(lst,start=)枚举函数(类似于C语言中的结构体)对列表进行遍历其中index为序号,item为元素,序号默认为0也可进行手动修改

#使用enumerate()枚举函数对列表进行遍历
for  index,item in enumerate(lst):
    print(index,item)#index是序号不是索引
#手动修改序号值
for index,item in enumerate(lst,start=1):#start可省略不写
    print(index,item)
运行结果
0 hello
1 ghfg
2 asdawedwad
3 wdadwdwa
4 wdawa
1 hello
2 ghfg
3 asdawedwad
4 wdadwdwa
5 wdawa

列表的特殊操作

增加元素的操作lst.append()

lst1=['hello','wprld','python']
#id()为列表的地址
print(lst1,id(lst1))
#增加元素的操作
lst1.append('wxb')
print(lst1,id(lst1))
运行结果
['hello', 'wprld', 'python'] 2550213955968
['hello', 'wprld', 'python', 'wxb'] 2550213955968

插入(lst.insert(索引,需要插入的元素))及删除(lst.remove(插入的元素);lst.pop(索引);lst.clear()清除所有元素)元素

#插入元素
lst1.insert(1,100)
print(lst1,id(lst1))
#删除列表中的元素
lst1.remove('wxb')
print(lst1)
#使用pop()(括号为元素索引)取出列表元素并删除
print(lst1.pop(1))
print(lst1)
#清除列表中的所有元素
lst1.clear()
print(lst1,id(lst1))
运行结果
['hello', 100, 'wprld', 'python', 'wxb'] 2550213955968
['hello', 100, 'wprld', 'python']
100
['hello', 'wprld', 'python']

列表的反向输出(lst.reverse())和复制(需要建立一个新的列表来复制lst.copy())

#列表的反向输出
lst1.reverse()
print(lst1)
#列表的复制
new_lst=lst1.copy()
print(lst1,new_lst)
运行结果
['python', 'wprld', 'hello']
['python', 'wprld', 'hello'] ['python', 'wprld', 'hello']

根据索引修改元素

#根据索引修改元素
lst1[1]='myspl'
print(lst1)
运行结果
['python', 'myspl', 'hello']

列表的排序

sort(key=,reverse=False)对于数字的使用:reverse默认为False且为升序将其更改为True时则为降序;key为自行设置的排序条件

lst=[9,7,6,5,4,3,2,0,1]
print(lst)
#升序排列列表中的元素
lst.sort()
print(lst)
lst.sort(reverse=True)
print(lst)
运行结果
[9, 7, 6, 5, 4, 3, 2, 0, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 9]
[9, 7, 6, 5, 4, 3, 2, 1, 0]

sort()对于字母的使用:reverse默认为False且为升序且从大写字母开始将其更改为True时则为降序从小写字母开始;key为自行设置的排序条件(str.lower为忽略大小写)

lst1=['apple','banana','Cat','Orange']
#升序先大写字母后小写字母
lst1.sort()
print(lst1)
#降序先小写在大写
lst1.sort(reverse=True)
print(lst1)
#忽略大小写
lst1.sort(key=str.lower)
print(lst1)
运行结果
['Cat', 'Orange', 'apple', 'banana']
['banana', 'apple', 'Orange', 'Cat']
['apple', 'banana', 'Cat', 'Orange']

lst.sored()函数使用不同于lst.sore()其需要定义一个新的列表储存排序后的列表

lst=[9,7,6,5,4,3,2,0,1]
asc_lst=sorted(lst)
#升序
print(asc_lst)
#降序
desc_lst=sorted(lst,reverse=True)
print(desc_lst)
lst1=['apple','banana','Cat','Orange']
#忽略大小写排序
new_lst1=sorted(lst1,key=str.lower)
print(new_lst1)
运行结果
[0, 1, 2, 3, 4, 5, 6, 7, 9]
[9, 7, 6, 5, 4, 3, 2, 1, 0]
['apple', 'banana', 'Cat', 'Orange']

作为python的初学者会有语言或者专业上的漏洞希望各位大佬能够知无不言言无不尽

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值