message = "'xxx','sss"
print(message)
# 将字符串首字母大写
title = "title"
title = title.title()
print(title)
# 将字符串全部大写
title1 = title.upper()
print(title1)
# 将字符串全部小写
title2 = title1.lower()
print(title2)
# 合并字符串
firstName = "yuan"
lastName = "tuo"
fullName = firstName + lastName
print(fullName)
# 上述操作的整合
message = "Hello " + fullName.title() + "!"
print(message)
# rstrip() 可以去除尾部空白
message = " xxxx "
print(message.rstrip())
# lstrip() 可以去除首部空白
print(message.lstrip())
# strip() 可以去除两端空白
print(message.strip())
print(2 ** 9)
# 下面这种写法是错误的
# print(2 *** 9)
Python将带小数点的数字都称之为浮点数
有时候位数是不确定的
# 会输出0.30000000000000004
print(0.1 + 0.2)
age = 23
print("Hello My Love,Your age is " + str(age))
bicycles = ['trek', 'redline', 'specialized']
print(bicycles)
# 通过索引访问元素
print(bicycles[0])
# 取出列表第一个元素并且首字母大写
print(bicycles[0].title())
# 通过-号访问倒数的元素
print(bicycles[-1])
# 通过索引直接修改
bicycles = ['trek', 'redline', 'specialized']
bicycles[0] = "test"
print(bicycles)
# 在列表末尾添加元素
bicycles = ['trek', 'redline', 'specialized']
bicycles.append("test1")
print(bicycles)
# 通过insert()将元素插入指定位置
bicycles = ['trek', 'redline', 'specialized']
bicycles.insert(1, "test")
print(bicycles)
# 通过del删除指定位置的元素
bicycles = ['trek', 'redline', 'specialized']
del bicycles[1]
print(bicycles)
bicycles = ['trek', 'redline', 'specialized']
test = bicycles.pop()
print(test)
print(bicycles)
bicycles = ['trek', 'redline', 'specialized']
test = bicycles.pop(1)
print(test)
print(bicycles)
# 使用sort()方法按照首字母大小写进行反向排序
# 需要注意的是在Python中,True And False 是首字母大写
cars = ["bmw", "audi", "subaru"]
cars.sort(reverse=True)
print(cars)
cars = ["bmw", "audi", "subaru"]
print(sorted(car s))
print(cars)
# 使用reverse()方法对列表进行永久排序,但是并非是按照字母顺序进行排序,而是按照先后顺序进行倒着排序
cars = ["bmw", "audi", "subaru"]
cars.reverse()
print(cars)
cars = ["audi", "bmw", "benz"]
for car in cars:
print(car)
cars = ["audi", "bmw", "benz"]
for car in cars:
print(car)
print(car.title()+" is very good")
print("test")
# 这个语句看似会打印1到5,但实际上只会打印1到4
for val in range(1, 5):
print(val)
# 使用list()将数字转化为列表
list = list(range(1, 5))
print(list.insert(0, 2))
print(list)
# 2到15之间的偶数
evenNumbers = list(range(2, 15, 2))
print(evenNumbers)
# 创建1到10的数平方的列表
squares = []
for val in range(1, 11):
squares.append(val**2)
print(squares)
print(max(squares))
print(min(squares))
print(sum(squares))
# 使用列表解析
squares = [val**2 for val in range(1, 15)]
print(squares)
players = ["yuan", "han", "su", "wang"]
print(players[0:2])
# 全部打印
print(players[:])
# 打印倒数三个
print(players[-3:])
# 遍历切片
for val in players[-2:]:
print(val.title())
# 复制列表 可以发现使用切片复制是两个不相干的实体,是一种完全复制
newPlayers = players[:]
print(newPlayers)
newPlayers.append("xxx")
print(players)
print(newPlayers)
# 可以发现通过变量名赋值,并非完全复制,两个变量名字指向的是同一个对象
print("-----")
newPlayers = players
newPlayers.append("xxx")
print(players)
print(newPlayers)
for val in dimensions:
print(val)
dimensions = (100, 80)
dimensions = (200, 100)
for val in dimensions:
print(val)
cars = ['bmw', 'benz', 'audi']
if "bmw" in cars:
print("在里面")
if "bmwa" not in cars:
print("不在里面")
else:
print("在里面")
cars = []
if (cars):
print("car")
else:
print("空的")
2019年3月30日23:00:35
第五章比较简答,感觉没有必要浪费太多时间在这个上面,简单的写点就好了。语法规则上感觉和C,Java差不多的。应该没什么问题。
alien0 = {"color" : "blue", "size": "12"}
print(alien0["color"])
print(alien0["size"])
alien0 = {"color" : "blue", "size": "12"}
alien0["name"] = "yuan"
print(alien0)
alien0 = {"color" : "blue", "size": "12"}
alien0["color"] = "red"
print(alien0)
alien0 = {"color" : "blue", "size": "12"}
del alien0["color"]
print(alien0)
alien0 = {"color" : "blue", "size": "12", "exe" : "10"}
for key, val in alien0.items():
print(key)
print(val)
alien0 = {"color" : "blue", "size": "12", "exe" : "10"}
alien1 = {"color" : "red", "size": "12", "exe" : "10"}
alien2 = {"color" : "yellow", "size": "12", "exe" : "10"}
aliens = [alien0, alien1, alien2]
for alien in aliens:
print(alien)
# 使用range()生产多个外星人
alien = []
alien0 = {"color": "blue", "size": "12", "exe": "10"}
for val in range(30):
alien.append(alien0)
for val in alien:
print(val)
# 使用切片修改后5个外星人信息
for val in alien[-5:]:
if val['color'] == 'blue':
val['size'] = '15'
for val in alien:
print(val)
favoriteLanguages = {
"yuan" : ["ruby", "java", "python"],
"tuo": ["ruby", "php", "python"],
}
for key, val in favoriteLanguages.items():
print(key + "最喜欢的语言是:")
for v in val:
print(v)
users = {
"yuan": {
"firstName": "yuan",
"lastName": "tuo",
"location": "sc"
},
"tuo": {
"firstName": "tuo",
"lastName": "yuan",
"location": "cs"
}
}
for key, val in users.items():
fullName = val["firstName"] + val["lastName"]
location = val["location"]
print(fullName + " " + location)
message = input("Tell me your name!")
print("Hello:" + message)
# 未验证的用户信息
unconfirmedUsers = ["alice", "mike", "yuan"]
# 已经验证的用户信息
confirmedUsers = []
# 一直验证,直到该列表为空时
while unconfirmedUsers:
# 获得当前验证的用户信息
currentUser = unconfirmedUsers.pop()
# 打印正在验证信息
print("Verifying user is " + currentUser.title())
confirmedUsers.append(currentUser)
print("\nThe following users has been confirmed:")
# 显示已经验证的人的信息
for val in confirmedUsers:
print(val)
pets = ["cat", "cat", "dog", "pig"]
while "cat" in pets:
pets.remove("cat")
print(pets)
def greet_user():
print("hello")
greet_user()
def greet_user1(username):
print("Hello: " + username.title())
greet_user1("yuan")
def hello(first_name, last_name):
print("Hello:" + first_name.title() + last_name.title())
hello(first_name="yuan", last_name="tuo")
def hello(first_name, last_name = "xxx"):
print("Hello:" + first_name.title() + last_name.title())
hello(first_name="yuan")
def get_full_name(first_name, last_name):
full_name = first_name.title() + " " + last_name.title()
return full_name
my_name = get_full_name("yuan", "tuo")
print(my_name)
def get_full_name(first_name, last_name, middle_name=''):
if middle_name:
full_name = "test"
else:
full_name = first_name.title() + " " + last_name.title()
return full_name
my_name = get_full_name("yuan", "tuo", "xx")
def get_name(first, last):
return {"first": first, "last": last.title()}
person = get_name("yuan", "tuo")
print(person)
while True:
first_name = input("Please input your first name!")
last_name = input("Please input your last name!")
full_name = get_full_name(first_name, last_name)
print(full_name)
def greet_user(names):
for name in names:
print("Hello " + name.title())
namess = ["yuan", "tuo", "han"]
greet_user(namess)
def print_models(unprinted_designs, completed_designs):
"""
模拟打印每个设计
:param unprinted_designs:
:param completed_designs:
:return:
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
print("Printing model:" + current_design)
completed_designs.append(current_design)
my_unprinted_designs = ["case1", "case2", "case3"]
my_completed_designs = []
print_models(my_unprinted_designs, my_completed_designs)
print(my_completed_designs)
print(my_unprinted_designs)
def print_models(unprinted_designs, completed_designs):
"""
模拟打印每个设计
:param unprinted_designs:
:param completed_designs:
:return:
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
print("Printing model:" + current_design)
completed_designs.append(current_design)
my_unprinted_designs = ["case1", "case2", "case3"]
my_completed_designs = []
# 使用[:] 就可以避免函数修改列表了
print_models(my_unprinted_designs[:], my_completed_designs)
print(my_completed_designs)
print(my_unprinted_designs)
def make_pizza(*toppings):
for topping in toppings:
print(topping)
make_pizza('mushrooms', 'green peppers', 'extra cheese', "test")
def build_profile(first, last, **user_info):
profile = {}
profile["first_name"] = first.title()
profile["last_name"] = last.title()
for key, val in user_info.items():
profile[key] = val
return profile
my_profile = build_profile("yuan", "tuo", location="石窝", sex="男")
print(my_profile)