python编程6.21.mid
程序员文章站
2024-02-11 14:01:46
...
#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.")
上一篇: 如何把应用跑在android上