s=list(range(1,20,2))print(s)
i=[]for i in range(3,30):if i%3==0:print(i,end=" ")print('\n')
ss=[]for s in range(1,10):
ss.append(s**3)print(ss)
haha=[k**3for k in range(1,10)]#列表解析
print(haha)print(haha[0:3]) #切片
print(haha[:3])print(haha[3:])print(haha[1:5])print(haha[-3:])
my_food=['ice cream','carrot','pizza']print(my_food)
god_food=my_food#[:]
god_food.append('hot dog')print(god_food)
q1='i am cxk,do you believe ?'print(q1[0:10-1])
yuan=('1','haha')#元组
print(yuan[0])print(yuan[1])print(yuan)for i in yuan:print(i)#yuan[0]='ss' X
yuan1=(0,2,0)print(yuan1)
yuan1=(0,0,0)print(yuan1)
cars=['audi','bmw','subaru','tony']for car in cars:if car=='tony':print(car.upper())else:print\
(car.title())
carcar='Audi'if carcar.lower()=='audi':print('false')else:print('ture')
a={1:'aaa','2':'bbb',3:'ccc'}#dict
print(a[1])print(a['2'])
a[4]='ddd'print(a)
del a['2']print(a)
b={5:'aaa',4:'bbb',3:'ccc',1:"ddd"}#dict
for k,v in b.items():print(k,v)print("-------------------------")for k, v in sorted(b.items()):print(k, v.title())for k in b.keys():print(k)for v in b.values():print(v)
#嵌套
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':15}
alien_2={'color':'blue','points':25}
alien=[alien_0,alien_1,alien_2]for alien in alien:print(alien)
print('==================================')print("第七章:用户输入和while循环")print('==================================')#message=input('Tell me something,and I will repeat it back you:')#print(message)#name=input('Please enter your name:')#print('hello!,'+name + "!")#promot='if you tell me who you are,we will kill!'#promot+='\nenter your name:'#name=input(promot)#print('\nhello,'+name+'!')#age=int(input('How old are you?'))#print('You are '+str(age)+' yeaes!It is so old!0.0')
#求模运算符
#print(4%3)#print(14%3)#print(3%3)#judge_even_or_odd#number=int(input("enter a number,and I'll tell you it's even or odd:"))#if number%2==0:# print("\nThe number "+str(number)+" is even!")#else:# print("\nThe number " + str(number) + " is odd!")#while循环简介#current_number=1#while current_number<=5:# print(current_number)# current_number+=1
#让用户选择何时退出
#s="\nTell me something,and I will repeat it back you:"#s+="\nEnter 'quit' to end the program."#message=""#while message!='quit':# message=input(s)# print(message)#s="\nTell me something,and I will repeat it back you:"#s+="\nEnter 'quit' to end the program."#message=""#while message!='quit':# message=input(s)# if message !='quit':# print(message)
#使用标志
#active=True#while active:# a="Enter something"# a+="\nEnter 'quit' end the program:"# message=input(a)# if message =='quit':# active=False# else:# print(message)
#使用break退出循环
#while 1:# a="Enter something"# a+="\nEnter 'quit' end the program:"# message=input(a)# if message =='quit':# break# else:# print(message)
#在循环中使用continue#current_number=0#while current_number<10:# current_number+=1# if current_number%2==0:# continue# print(current_number)
#避免无限循环
#x=1#while x<=10:# print(x)
# #x+=1 没有此处,无限循环
#使用while循环处理列表和字典
#unconfirmed_users=['apple','mango','banana'] #unconfirmed 未经确定的 confirm 确认,确定#confirmed_users=[]#while unconfirmed_users:# current_users=unconfirmed_users.pop()# print("Verifying user:"+current_users.title()) #verify v核实; 查对; 核准; 证明; 证实;# confirmed_users.append(current_users)#print("\nThe following users have been confirmed:")#for confirmed_users in confirmed_users:# print(confirmed_users.title())
#删除包含特定值的所有列表元素
#pets=['dog','cat','dog','goldfish','cat','rabbit','cat']#print(pets)#while 'cat' in pets:# pets.remove('cat')#print(pets)
#使用用户输入来填充词典
#responses={}#polling_active=True #polling poll过去分词 投票,民意测验 v.n.#while polling_active: #or while 1:# name=input("What is your name?")# response=input("Which mountain would you like to climb smeday?")# responses[name]=response# repeat=input("Would you like to let another person respond?(yes/no)")# if repeat=='no':# polling_active=0 #break#print("\n---Poll Results---")#for name,response in responses.items():# print(name+" would like to climb "+response+".")
print('==================================')print("第八章:函数")print('==================================')#def greet_user():# print("hello!")#greet_user()
#可调用函数多次
#def describe_pet(animal_type,pet_name):# print("I have a "+animal_type+".")# print("My "+animal_type+"'s name is "+pet_name.title()+".")#describe_pet('hamster','harry') #hamster 仓鼠#describe_pet('dog','willie')
#位置形参的顺序很重要
#def describe_pet(animal_type,pet_name):# print("I have a "+animal_type+".")# print("My "+animal_type+"'s name is "+pet_name.title()+".")#describe_pet('harry','hamster') #hamster 仓鼠
#关键字实参
#def describe_pet(animal_type,pet_name):# print("I have a "+animal_type+".")# print("My "+animal_type+"'s name is "+pet_name.title()+".")#describe_pet(pet_name='harry',animal_type='hamster') #hamster 仓鼠
#默认值
#def describe_pet(pet_name,animal_type='dog'):# print("I have a "+animal_type+".")# print("My "+animal_type+"'s name is "+pet_name.title()+".")#describe_pet('willie')
#返回值
#def get_formatted_name(first_name,last_name):# full_name=first_name+' '+last_name# return full_name.title()#print(get_formatted_name('xu','hu')+' NB')
#让实参变成可选的
#def get_formatted_name(first_name,last_name,middle_name=''):# if middle_name:# full_name = first_name + ' '+middle_name+' '+ last_name# else:# full_name = first_name + ' ' + last_name# return full_name.title()#print(get_formatted_name('xu','hu','hu')+' NB')
#返回词典
#def bulid_person(first_name,last_name):# person={'first':first_name,'last':last_name}# return person#print(bulid_person('xu','jingwei'))
#结合使用函数和while循环
#def bulid_person(first_name,last_name,age=''):# person={'first':first_name,'last':last_name}# if age:# person['age']=age# return person#print(bulid_person('xu','jingwei',22))#def bulid_person(first_name,last_name):# full_name=first_name+" "+last_name# return full_name#while 1:# print('Please enter your name:')# print("(Enter 'q' at any key to quit)")# f_name=input("First name:")# if f_name=='q':# break# l_name=input("Last name:")# s=bulid_person(f_name,l_name)# print(s)
#传递列表
#def greet_users(names):# for name in names:# msg="Hello, "+name.title()+"!"# print(msg)#user_names=['wang','God','dog']#greet_users(user_names)
##传递任意数量的实参
#def make_pizza(*toppings):# print(toppings)#make_pizza('tomato')#make_pizza('apple','orange','water')
#