python编程6.21.mid

这篇博客主要探讨了Python编程的高级技巧和实践经验,包括模块化、异常处理、装饰器和上下文管理器的使用,以及如何优化代码性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#coding:utf-8
#621中午

age = 23
message = "happy "+ str(age) +"rd birthday"
print(message)
#age=23
#message="happy"+ age +"rd birthday"
#print(message)
#出现了类型错误:只能将str(而不是“int”)连接到str,age是一个int型,python不知道是2还是3
#这个时候可以用非字符串值表示字符串


#在python中列表用[]表示
bicycles =['trek','cannondale','redline','specialized']
print(bicycles[0].title()) #用title()得到第一个元素首字母大写输出,注意索引是从0开始
print(bicycles[1].title())
print(bicycles[-1].title())   #-1得到最后一个元素

message ="my favorite bicycles was a" + bicycles[0].title()+"."
print (message)

#列表
motorcycles =['hondda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
motorcycles.append('ducati')   #用append把元素ducati添加到列表末尾,不减少,直接添加
print(motorcycles)



#用append直接给空列表添加元素
motorcycles =[]
motorcycles.append('honda') 
motorcycles.append('yamaha')   
motorcycles.append('suzuki....') 
print(motorcycles)


#用方法insert可以添加任何元素在列表的新位置
motorcycles =['hondda','yamaha','suzuki']
motorcycles.insert(0,'ducati')  
print(motorcycles)

#用del语句删除列表中的元素
motorcycles =['hondda','yamaha','suzuki']
del motorcycles[0] 
del motorcycles[0]  # 连续删除2次索引为0的元素,有继承
print(motorcycles)


#方法pop()可以删除列表中末尾的元素,并让你能够接着用它
motorcycles =['hondda','yamaha','suzuki'] 
print(motorcycles)
poped_motorcycle = motorcycles.pop()  #把motorcycles中末尾元删除并储存到poped_motorcycle中p
print(motorcycles)
print(poped_motorcycle)             #检验poped_motorcycle的值是否为删除的末尾元素
motorcycles =['hondda','yamaha','suzuki']
last_owned =motorcycles.pop()
print("The last motorcycle I owned wsa a " + last_owned.title())    #title记得加()

#删除的用法
motorcycles =['hondda','yamaha','suzuki','ducadi'] 
print(motorcycles)
too_expensive='ducadi'
motorcycles.remove(too_expensive)
print(motorcycles)
print("\nA " +too_expensive.title()+ " is too rxpensive for me.")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值