搜索

学习《Python编程 从入门到实践》第二、三天


发布时间: 2022-11-24 23:43:00    浏览次数:76 次

近期比较忙,宝宝的预产期是12月17日,老婆每天都跟我说准备要生了。

所以昨天看了一会就做家务活了,练习完后忘记写日记了。今天给补上。

 

为什么突然想学编程呢?

其实是平常觉得压力大的时候喜欢打游戏,

除了一些3A巨作,有时候还沉迷于微信小程序上的养成类脑残游戏(不是针对某些游戏啊我只是觉得蛮脑残的但我又很沉迷于此哈哈)

有一天突然在想,有没有可能我自己也能写一个游戏出来呢?

于是趁着今年考完了司法考试以及经济师的空档,

打开了电脑,写起了代码,多多少少有点弃笔从戎的感觉。

刚开始看网上免费的网课,有样学样地搭起了开发环境,下载了各式各样的软件,

可谓是有模有样、装备齐全;

随后想着一边上网课,一边刷一遍《Python编程 从入门到实践》这本书吧,因为书里面第二部分就是教人写游戏的

后续的可视化内容我也蛮感兴趣的。

一步步学吧,希望自己可以坚持下去!

加油!!!

 

记录这两天学过的知识点:

第二天的知识点:

一、字符串替换

替换:.replace

(“被替换的字符/子序列”,“要替换为的内容”)/(“被替换的字符/子序列”,“要替换为的内容”,1)

message = input('请说话:')
print(message)#我去你大爷的家里玩
data = message.replace('大爷'"**")
data = message.replace('大爷'"**",2)#替换第二个“大爷”
print(data)

 

二、字符串切割

切割:.split 切割成列表

(“根据什么东西进行分割”)、(“根据什么东西进行分割”,1)

.rsplit是从右向左切割

 

三、判断字符串是否可以转换成数字

转换数字:.isdigit(得到布尔值类型)

 

四、索引从0而不是1开始

bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0])#trek

-1返回最后一个列表元素

 

五、修改列表元素

1、修改列表元素:选中某一个,直接替换

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)

2、在列表末尾添加元素:.append

在列表中添加元素,在列表末尾添加元素
motorcycles = ['honda','yamaha','suzuki']
motorcycles.append('ducati')
print(motorcycles)

3、在列表中插入元素:.insert

#在列表中插入元素
motorcycles = ['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
 
第三天的知识点:

一、使用del语句删除元素

motorcycles = ['honda','yamaha','suzuki']
del motorcycles[0]
print(motorcycles)

 

二、使用方法pop()删除元素

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)

方法pop()删除列表末尾的元素,并接着使用它的值。(默认删除最后最后一个。

 

三、弹出列表中任何位置处的元素

motorcycles = ['honda','yamaha','suzuki']
first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")

每当使用pop()时,被弹出的元素就不再在列表中了。

是否选用del or pop():

如果删除后不再用,使用del

如果删除后还要继续使用,使用pop()

 

四、根据值删除元素

使用remove()

#根据值删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.remove('honda')
print(motorcycles)

使用remove()删除值后使用

motorcycles = ['honda','yamaha','suzuki','ducati']
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()}is too expensive for me!")

注意:方法remove()只删除第一个指定的值,如果要删除的值可能在列表中出现多次,就需要使用循环来确保将每个值都删除。

 

五、使用方法sort()对列表永久排序

前提:列表所有值为小写。

按字母顺序排序

#使用方法sort()对列表永久排序
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

按与字母顺序相反的顺序排列,需向sort()传递参数reverse=True。

cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)

注意:reverse=True 中的“True"首字母需要大写。

修改皆为永久性。

 

六、使用函数sort()对列表临时排序

#使用函数sorted()对列表临时排序
cars = ['bmw','audi','toyota','subaru']
print('Here is the original list:')
print(cars)
print('\nHere is the the sorted list:')
print(sorted(cars))
print('\nHere is the original list again:')
print(cars)

 

七、倒着打印列表

#倒着打印列表
cars = ['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)

 

八、确定列表的长度

#确定列表的长度
cars = ['bmw','audi','toyota','subaru']
print(len(cars))
 

 

 
免责声明 学习《Python编程 从入门到实践》第二、三天,资源类别:文本, 浏览次数:76 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 11:43:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/tomyin/p/16923867.html