初学python(二)——基础中的基础

自己按照书《Python编程从入门到实践》进行学习,一个小阶段后进行一点点整理,仅供参考。编译器为Geany。

几点总的说明

  • true 和 false 的首字母为大写

检查是否相等,只用 in 关键字即可,检查时区分大小写,多个条件判断的关键字为elif

cars = ['audi','bmw','subaru','toyota']

for car in cars:
	if car == 'bmw':
		print(car.upper())
	else:
		print(car.title())

与或逻辑判断

age_0 = 22
age_1 = 18
print(age_0 >= 21 and age_1 >= 21)
print(age_0 >= 21 or age_1 >= 21)

toppints = ['mushrooms','onions','pineapple']
print('mushrooms' in toppints)

banners = ['andrew','carolina','david']
user = 'marie'
if user not in banners:
	print(user.title()+", you are post a response if you wish.")

一个完整的示例

available_toppings = ['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings = ['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings:
	if requested_topping in available_toppings:
		print("Adding "+requested_topping)
	else:
		print("sorry")
print("\nFinshed making your pizza!")

字典

字典的定义与删除,注意格式,类比map进行记忆学习

alien = {'color':'green','points':5}
print(alien['color'])
print(alien['points'])
del alien['color']#删除color关键字对应的值
print(alien)

给字典增加键值
直接定义即自动在末尾添加,不关心键值存储时候的顺序,只关心键值直接的关联关系

alien_0 = {'color':'green','points':5}
print(alien_0)
alien_0['x_positon']=0
alien_0['y_positon']=25
print(alien_0)

alien_1 = {}
#创建了一个空字典
alien_1['color'] = 'green'
#添加键值

具有多对键值时,注意命名格式,每行后都写“,”
注意print换行语句的格式

favorite_languages={
	'jen':'python',
	'sarah':'c',
	'edward':'ruby',
	'phil':'python',
}
print("jen's favorite language is " +
	  favorite_languages['jen']+
	  ",")

遍历字典中的键值对 item、键 key、值 value

for name,language in sorted(favorite_languages.items()):#按字母顺序进行排序
	print(name.title()+"'s favorite language is "+language.title()+".")
	
for name in favorite_languages.keys():
	print(name.title())

for language in set(favorite_languages.values()):#set确保不重复
	print(language.title())

利用切片改变字典的值

aliens = []
for alien_number in range(0,20):
	new_alien = {'color':'green','points':'5','speed':'slow'}
	aliens.append(new_alien)
for alien in aliens[0:3]:
	if alien['color']=='green':
		alien['color']='yellow'
		alien['points']=10
		alien['speed']='medium'
for alien in aliens[0:5]:
	print(alien)

字典中包含列表

pizza = {
	'crust':'thick',
	'toppings':['mushrooms','extra cheese'],
}
print("you ordered a "+pizza['crust']+"-crust pizza"+
	  "with the following toppings:")
for topping in pizza['toppings']:
	print("\t"+topping)

字典中存储字典(一个完整的例子)
使用者包括使用者的姓名(键),姓名代表的使用者有姓、名和地址

users = {
	'aeinstein':{
		'first':'albert',
		'last':'aeinstein',
		'location':'princeton',
	},
	'mcurie':{
		'first':'marie',
		'last':'mcurie',
		'location':'paris',
	}
}
for username,userinfo in users.items():
	print("\nUsername: "+username)
	full_name = userinfo['first']+" "+userinfo['last']
	location = userinfo['location']
	print("\tFull name: "+full_name.title())
	print("\tlocation: "+location.title())

while的用法与其他语言中的相同,break和continue用法也相同,因此直接给出一个例子

unconfirmed_users = ['alice','brian','candance']
confirmed_users = []
while unconfirmed_users:
	current_user = unconfirmed_users.pop()
	print("Verifying user: "+current_user.title())
	confirmed_users.append(current_user)
print("\nThe following users have been confirmed")
for confirmed_user in confirmed_users:
	print(confirmed_user.title())

#remove方法去掉指定的元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
	pets.remove('cat')
print(pets)
发布了14 篇原创文章 · 获赞 11 · 访问量 2534

猜你喜欢

转载自blog.csdn.net/qq_43425914/article/details/98658719