python编程从入门到实战1-3章

 

print('hellow world')  
""" 多行注释"""
#大小写
print('i love you')
mssage='hellow world'
print(mssage)
name=('ada lovelace')
print(name.title())
print(name.upper())
print(name.lower())
# 字符串拼接 【建议使用join】
first_name="ada"
last_name="lovelace"
full_name=first_name+" "+last_name
print(full_name)
print("hello,"+full_name.title()+"!")
mssage="Hello,"+full_name.upper()+"!"
print(mssage)

print("\n python\n")


#删除空白 末端rstrip() 前端lstrip.() 两端strip()
favorite_language=' python '
print(favorite_language.rstrip())#末尾
print(favorite_language.lstrip())#前端
print(favorite_language)
print(favorite_language.strip())#两端

print("为了避免语法错误,单双引号适当混合使用(即:系统无法判定结束位置")
#数字
print(2+3)
print(3-2)
print(3/2)
print(2*3)
print(3 ** 2)#平方
print(2+3*4)
print((2+3)*4)
#浮点数和c语言一样
print(3*0.1)
print('结果包含的小数点可能是不确定的')
#使用函数str()避免类型错误 str()
age=23
mssage="happy "+str(age)+"rd brithday"
print(mssage)

#列表
bicycles=['trek','cannondale','redline','specialized']
print(bicycles)
#访问列表元素
print(bicycles[0])

print(bicycles[0].title())
print(bicycles[0].upper())
#访问最后一个元素
print(bicycles[-1])
#使用列表中的值
mssage='My first bicycle was a '+bicycles[0].title()+'.'
print(mssage)
#修改、添加和删除元素(指定)
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
#在列表末尾中添加元素 .append()
motorcycles.append('ducati')
print(motorcycles)

motorcycles=[]
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
#在列表中插入元素 .insert()
motorcycles.insert(0,'qianjiang')
print(motorcycles)
#从列表中删除元素
# 1 永久删除知道位置 del
del motorcycles[0]
print(motorcycles)
#使用del得知道位置
# 2 弹出删除法可再次利用(栈) pop()
popde_motorcycles = motorcycles.pop()
print(motorcycles)
print(popde_motorcycles)
#弹出知道位置的元素
first_owned = motorcycles.pop(0)
print(first_owned)
#根据值删除元素
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
motorcycles.remove('yamaha')
print(motorcycles)

motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive='ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
#组织列表
#使用sort()队列表进行永久排序 sort()
cars=['bms','audi','toyota','subaru']
print(cars)
cars.sort()
print(cars)
#反向排序 sort(reverse=ture)
cars=['bms','audi','toyota','subaru']
cars.sort(reverse=True) # 大写 直接读源文件修改
print(cars)
#使用sorted()队列表进行临时修改 sorted() 临时修改

print('\nHere is the original list:')
print(cars)
print('here is the sorted list')
print(sorted(cars))
print('the original is not change')
print(cars)
print('临时反向排序向sorted()传递参数reverse=Ture')

print('你不会')
#倒着打印列表 **(不用管顺序)仅仅是反转元元素排列顺序 .reverse()
cars=['bms','audi','toyota','subaru']
print(cars)
cars.reverse() #永久修改元素顺序 想要恢复仅仅需再次倒转即可
print(cars)
#确定列表长度(即元素个数)
cars=['bms','audi','toyota','subaru']
len(cars)
print(len(cars))
#使用列表时候避免索引出错 元素开头是0
#访问最后一个元素时候用-1 仅当列表为空时候 这种访问会出错

 

 

猜你喜欢

转载自www.cnblogs.com/vwei/p/9756421.html