基础知识学习
变量和简单数据类型
字符串
修改字符串的大小写
name.title()
首字母大写
name.upper()
全大
name.lower()
全小
在字符串中使用变量
1 2 3 4 first_name = 'ada' last_name = 'lovelace' full_name = f"{first_name} {last_name} " print (full_name)
1 2 3 4 5 name = "alice and judy" hobby = "like playing basketball" full_sen. =f"{name.title()} {hobby} " message = f"hey, do u knoe {full_sen} !" print (message)
用f来创建消息,并赋给变量
删除空白
常用于在存储用户输入前对其进行清理
lstrip()
首
rstrip()
尾
strip()
首尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 print ("test.1" ) name = "hello eric" sent = f"{name.title()} ,would u like to learn some Python today?" print (sent)print ("\n" )print ("test.2" ) celebrity = "albert einstein" sentence_ce = f'{celebrity.title()} once said, "A person who never made a mistake necer tried anything new."' print (sentence_ce)print ('\n' )print ("test.3" ) famous_person = 'al es' famous_person = famous_person.title()print (f"original:'{famous_person} '" )print (f"original:'{famous_person.lstrip()} '" )print (f"original:'{famous_person.restrip()} '" )print (f"original:'{famous_person.strip()} '" )
列表
homework
1 2 3 4 5 6 7 8 9 names = ['sun yang' ,'wang xinheng' ]print (name[0 ].title()) message = f"hello, {name[0 ].title()} " print (message) commute = ['car' ,'bicycle' ,'motorcycle' ] message_second = f"I would like to own a Honda {commute[2 ]} " print (message_second)
修改添加和删除
修改
添加
append()
在末尾
insert(location,'')
插入
删除
无后续访问 使用del del motorcycles[0]
有后续访问 使用pop pop(0)
指定值删除 remove()
组织列表
永久排序
方法
cars.sort()
cars.sort(reverse = True)
临时排序
函数
sorted(cars)
sorted(cars,reverse = True)
倒着打印
方法
cars.reverse()
确定列表长度
函数
len(cars)
操作列表
homework
1 2 3 4 5 6 7 8 9 10 11 12 13 pizzas = ['meat' ,'cheese' ,'mushroom' ]for pizza in pizzas: print (f"I like {pizza} pizza." ) print (f"I like {pizza} pizza very much." ) print ("I really love pizza" )print ("\n" ) animals = ['cat' ,'dog' ,'rabbit' ]for animal in animals: print (f"A {animal} would make a great pet." ) print (" xxx" )
创建数值列表
创建数字列表
numbers = list(range(1,6))
有步长
numbers = list(range(2,11,2))
计算前10个整数的平方
1 2 3 4 5 square = []for value in range (1 ,11 ) square.append(value**2 ) print (value)
列表解析
1 2 square = [value**2 for value in range (1 ,11 )]print (square)
描述性+表达式
对数字列表执行简单的统计计算
min(digits)
max(digits)
sum(digits)
使用列表的一部分
切片与遍历
1 2 players = ['hey' ,'jude' ,'jack' ,'eli' ]print (players[-3 ::2 ])
仍然是左闭右开
players[:4]
从列表开始
players[2:]
终止于列表结尾
players[-3:]
最后三名队员
players[-3::2]
每隔两个元素提取一个
遍历:
for player in players[:3]
遍历前三名队员
复制列表
1 2 3 4 myfoods = ['pizza ' ,'falafel' ,'carrot cake' ] friendfoods = myfoods[:] myfoods.append('cannoli' ) friendfoods.append('ice cream' )
friend_foods=my_foods
两个列表关联 ,添加新元素会同时出现
元组
不可改变的列表
dimension = (200,50)
只包含一个元素的元组 dimension(200,)
If 语句
homework
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 message = 'hello' message2 = 'HELLO' if message.upper() == message2: print ("Upper is true" ) if message2.lower() == message: print ("Lower is true" ) print (message2>message) number = [0 ,2 ,3 ] number2 = [0 ,1 ,2 ,3 ]print (number[0 ]==number2[0 ])if 0 in number: print ("0 in it" )else : print ("0 not in it" )
elif
处理列表
确定列表是否为空
使用多列表
1 2 3 4 5 6 current_users=['admin' ,'jack' ,'john' ,'amy' ,'lucy' ] new_users=['admin' ,'jack' ,'HAH' ,'QUeen' ,'King' ] new_current_users = [user.title() for user in current_users] sequences = list (range (1 ,10 ))
字典
使用字典
访问字典中的值
1 2 alien_0 = {'color' :'green' ,'age' :'1' }print (alien_0['color' ])
键-值
添加键值
直接添加
1 2 alien_0['x_position' ] = 0 alien_0['y_position' ] = 0
创建空键值然后添加
修改值
直接修改
1 alien_0['color' ] = 'yellow'
删除键值对
有类似对象组成的字典
1 2 3 4 5 6 7 favorite_languages = { 'jen' :'python' , 'sarah' :'c' , } language = favorite_language['sarah' ].title()print (f"She likes {language} ." )
用get()来访问值
在指定的键不存在时返回一个默认值
1 2 3 4 alien_0 = {'color' :'green' ,'speed' :'medium' } point_value = alien_0.get('poionts' ,'no point vlaue assigned' )print (point_value)
also can be:
1 2 3 4 alien_0 = {'color' :'green' ,'speed' :'medium' } message = 'no point vlaue assigned' point_value = alien_0.get('poionts' ,message)print (point_value)
遍历字典
.items()
遍历所有的键值对
.keys()
遍历所有的键
.values()
遍历所有的值
1 2 3 4 5 6 7 8 9 favorite_language = { 'jen' :'python' , 'sarah' :'c' , 'edward' :'ruby' , 'phil' :'python' , }for name,language in favorite_languages.items(): print (f"{name.title()} 's favorite langauge is {language.title()} " )
为了剔除重复项 用set集合
1 2 for language in set (favorite_language.values): print (language.title())
嵌套
1 2 3 4 5 6 7 8 9 10 11 12 aliens = []for alien_number in range (30 ): new_alien = {'color' :'green' ,'points' :5 ,'speed' :'slow' } aliens.append(new_alien) for alien in alien[:3 ]: if alien['color' ] == 'green' alien['color' ] == 'yellow' for alien in aliens[:5 ]: print (alien)
1 2 3 4 5 6 7 8 9 10 pizza = { 'crust' :'thick' , 'topping' :['mushroom' ,'extra cheese' ], }print (f"you ordered a {pizza['crust' ]} -crust pizaa" "withfollowing toppings:" ) for topping in pizza['toppings' ]: print ("\t" +topping)
讲一个键关联到多个值的时候,可以在字典中嵌套一个列表
字典嵌套字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 users = { 'aeinstein' :{ 'first' :'al' , 'last' :'ein' , 'location' :'princeton' } 'mucrie' :{ 'first' :'ma' , 'last' :'curie' , 'locatin' :'paris' } }for username, user_info in users.items(): print (f"{username} " ) full_name = f"{user_info['first' ]} {user_info['last' ]} location = user_info['location']
用户输入
message = input{"prompt:"}
int数值输入
1 2 height = input ("how are u" ) height = int (height)
循环
1 2 3 4 5 6 7 8 active = True while active: topping = input ("please input ur toppings:" ) if topping == 'quit' : active = False else : print ( )
处理列表和字典
1 2 3 while user : current_user = user .pop ()
1 2 3 4 pets = ['dog' ,'cat' ,'goldfish' ]while 'cat' in pets: pets.remove ('cat' )
函数
1 2 3 4 def dislay_message (): print ("I learned xxx" ) display_message()
1 2 3 4 def make_shirt(size ,message ) : printt(..) make_shirt('large ',message ='o ')
1 2 3 4 def describe_city(city ,contru ='China') describe_city('shanghai ') describe_city('hangzhou ','china ')
1 2 3 4 5 def get_fommatted_name (first_name,last_name,middle_name='' ): if middle_name: fll`` `` ` return full_name.title()
1 2 3 middle_name ='' age =None
输入
怎么输出字典
怎么输出列表