《python编程:从入门到实践》查漏补缺(1)列表,多条件语句,字典,while语句


1,关于列表 list 的操作
#coding=gbk
##这是python编程:从入门到实践  的一些编程实践
#主要是为了查漏补缺
#1,列表
color=['red','white','blue']
print(color)

print(color[0])
print(color[0].title()) #首字母大写
print(color[-1])    #得到最后一个列表元素
for i in range(len(color)):
    print(color[i])
#在列表任意位置弹出
color.pop(0)
print(color)    #['blue', 'white']
#列表的增删改查   : 
#增加:    append insert ; 
#删除    del  pop  pop[i]  根据元素值来删除元素:color.remove('red'); 
#修改    insert  color[i]='values'; 
#查找     rfind lfind; 
color.append('red')
color.append('yellow')
color.append('whatever')

#组织列表
color=['red','white','blue']
#使用sort()
color.sort(reverse=True)    #将列表进行永久性排序,不能使用print (color.sort())
print (color)   #输出['white', 'red', 'blue'],依靠字母进行排序

#使用reverse()只是反转列表的元素, 并不是按字母的相反的顺序进行排列
color1=['red','white','blue']
color1.reverse()
print('倒序排列输出: ')
print(color1)   #['blue', 'white', 'red']

#使用sorted(iterable,reverse=True)进行临时排序
color=['red','white','blue']
s=sorted(color,reverse=True)    #增加参数reverse 为True
print(s)    #['blue', 'red', 'white'],记得是:sorted(parameter)

#第四章:操作列表
print('列表的操作')
colors=['red','white','blue','yellow','black']
for color in colors:
    print(color)    #打印出所有的元素
    
#列表解析,将for循环和创建新元素的代码合并成一行, 并自动附加新的元素
squres=[value**2 for value in range(1,11)]
print(squres)   #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squres[-3:])  #输出squres的最后3个元素 [64, 81, 100]

#复制列表
my_color=['red','white','blue','yellow','black']
my_friend_color=my_color[:]
my_color.append('food')
my_friend_color.append('fruit')
print(my_color)         #['red', 'white', 'blue', 'yellow', 'black', 'food']
print(my_friend_color)  #['red', 'white', 'blue', 'yellow', 'black', 'fruit']
#如果使用赋值形式, 则不能实现此功能
my_food=['apple','nuddle']
my_friend_fod=my_food
my_food.append('bananna')
my_friend_fod.append('chicken')
print(my_food)      #['apple', 'nuddle', 'bananna', 'chicken']
print(my_friend_fod)#['apple', 'nuddle', 'bananna', 'chicken']      两个人的元素都赋值了

2,多个条件

#检查多个条件
age_0=15;age_1=23
print(age_0 > 13 and age_1 < 45)  #输出为True
print(age_1 > 12 or age_1 > 12)  #True
#检查特定值是否包含在列表中
colors=['red','white','blue','yellow','black']
print('red' in colors)  #True

#确保列表不为空
color1=[]
if color1:  #列表为空时, 为False
    print(';列表不为空')
else:
    print('这是一个空列表')    #输出:  这是一个空列表
    
#使用多个列表
colors1=['red','white','blue','yellow','black'] #商店里有的颜色
my_color1=['red','green','yellow']  #我需要的颜色
for color in my_color1:
    if color in colors1:
        print('this is my color-'+color+'.')
    else:
        print("sorry, store don't have this color")
print('finish choosing color')
# this is my color-red.
# sorry, store don't have this color
# this is my color-yellow.
# finish choosing color
3,关于字典dict的操作

#第六章:字典---->键值对
#1,遍历字典中的键值对: for key , value in dict.items():  
#名字可以变化: for name , language in dict.items():
#2,遍历字典中的键  :  for key in dict.keys():
#3,遍历字典中的值: for value in dict.values():

#指出2个朋友喜欢的颜色
all_color={'bob':'red','smith':'yellow','jack':'black','james':'white'}
friends=['smith','james']
for color in all_color.keys():
    print(color.title())
    if color in friends:
        print('hello , my friend --'+color.title()+
              '  i see you favorite color-'+all_color[color].title())
#输出:
# Bob
# James
# hello , my friend --James  i see you favorite color-White
# Smith
# hello , my friend --Smith  i see you favorite color-Yellow
# Jack

#嵌套,在字典中嵌套列表
all_color={'bob':['red'],'smith':['yellow','red'],'jack':['black','white'],'james':['white']}
for name, colors in all_color.items():
    if len(colors) > 1 :
        print('\n'+name.title()+'喜欢的颜色有几种:')
        for color in colors:
            print('\t'+str(color.title()))
    else:
        print('\n'+name.title()+'喜欢一种颜色:')
        for color in colors:
            print('\t'+str(color.title()))      #只有一种颜色时,为使其不是['red']这种形式,使用循环
# Jack喜欢的颜色有几种:
#     Black
#     White
# 
# Smith喜欢的颜色有几种:
#     Yellow
#     Red
# 
# Bob喜欢一种颜色:
#     Red
# 
# James喜欢一种颜色:
#     White

4,关于while语句的操作

#第7章:用户输入和while循环
#在要求很多条件满足的是才能继续运行的程序中, 可以定义一个变量, 用于判断是否整个程序处于活跃的状态。这个变量就称作为 标志
# prompt='\n 输入一些什么东东吧'
# prompt+='\n 输入 quit 时,将退出程序'
# active = True
# while active:
#     message=input(prompt)
#     if message == 'quit':
#         active=False
#     else:
#         print(message)
#  输入一些什么东东吧
#  输入 quit 时,将退出程序haha
# haha
# 
#  输入一些什么东东吧
#  输入 quit 时,将退出程序hello
# hello
# 
#  输入一些什么东东吧
#  输入 quit 时,将退出程序quit

#使用while循环处理列表
user=['james','bob','jack']
confirmed_user=[]
while user:
    current_user=user.pop()
    print('已经验证的成员:'+current_user.title())
    confirmed_user.append(current_user)
print("所有验证的成员")
for u in confirmed_user:
    print(u.title())
# 已经验证的成员:Jack
# 已经验证的成员:Bob
# 已经验证的成员:James
# 所有验证的成员
# Jack
# Bob
# James

#删除特定值的所有元素
pets=['dog','cat','rabbit','cat','fish','cat']
while 'cat' in pets :
    pets.remove('cat')
print(pets)  #['dog', 'rabbit', 'fish']


猜你喜欢

转载自blog.csdn.net/qq_40587575/article/details/80806904