让我们来深入学习一点Python
学习一下变量吧 字符定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 message="Hello Python World" print (message)"hello world" 'hello world' name="ada lovelace" print (name.title())print (name.upper())print (name.lower())
字符串中使用变量 1 2 3 4 5 6 first_name="ada" last_name="lovelace" full_name=f"{first_name} {last_name} "
空白操作 1 2 3 4 5 6 favorite_language=" python " print (favorite_language.strip())print (favoriet_language.Lstrip())print (favorite_language.rstrip())
==在使用字符串时,应当注意单双引号的匹配;==
整数 1 2 3 4 5 6 7 8 2 +3 2 -3 2 *3 2 /3 2 **3 2 +3 *4 (2 +3 )*4
浮点数
数中的下划线 1 2 universe_age=14_000_000_000 print (universe_age)
同时给多个变量赋值 1 2 x,y,z=0 ,0 ,0 print (x,y,z)
常量
注释 ==用#来对这一行后面的内容进行注释== 注释需要有意义才方便其他程序员理解
列表是什么东西呢 创建一个列表 1 2 3 4 5 6 bicycles=['trek' ,'cannondale' ,'redline' ,'specilize' ] print (bicycle)print (bicycle[0 ])
==当你想要访问最后一个元素时,可以将索引指定为-1== ==因此你可以指定的索引范围为从-n到n-1==
修改列表元素 1 2 3 4 motobicycle=['honda' ,'yamaha' ,'suzuki' ] print (motobicycle)moto[0 ]='ducati' print (motobicycle)
添加 1 2 3 4 5 6 7 motobicycle=['honda' ,'yamaha' ,'suzuki' ] print (motobicycle)motobicycle.append("ducati" ) print (motobicycle)
插入 1 2 3 4 5 6 motobicycle=['honda' ,'yamaha' ,'suzuki' ] motobicycle.insert(0 ,'ducati' ) print (motobicycle)
删除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 motobicycle=['honda' ,'yamaha' ,'suzuki' ] del motobicycle[0 ]print (motobicycle)motobicycle=['honda' ,'yamaha' ,'suzuki' ] popped_motobicycle=motobicycle.pop() print (motobicycle)print (popped_motobicycle)//弹出任意位置的元素 first_motobicycle=motobicycle.pop(0 ) //根据元素的值删除元素 motobicycle=['honda' ,'yamaha' ,'suzuki' ] motobicycle.remove('honda' )
组织列表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cars=['bnw' ,'audi' ,'toyota' ,'subaru' ] cars.sort() print (cars)当向sort()方法传值reverse=True 时;按相反的字母顺序排列 print (cars.sort(reverse=True ))cars=['bnw' ,'audi' ,'toyota' ,'subaru' ] print (cars)print (cars.sorted ())print (cars)cars=['bnw' ,'audi' ,'toyota' ,'subaru' ] print (cars.reverse())cars=['bnw' ,'audi' ,'toyota' ,'subaru' ] print (cars.len ())
对列表进行一些华丽花哨的操作吧 遍历整个列表(循环) 1 2 3 4 magicians=['alice' ,'david' ,'carolina' ] for magician in magicians: print (magician)
缩进 ==python中十分注重缩进问题,在进行for循环时,如果没有代码缩进,系统会因为找不到循环代码而报错,在不应该用缩进时使用缩进,系统也会报错:== (1)不要忘记缩进 (2)不要忘记缩进额外的代码行 (3)不要有不必要的缩进 (4)注意循环后不必要的缩进
创建数值列表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 for value in range (1 ,5 ): print (value) numbers=list (range (1 ,6 )) even_numbers=list (range (2 ,11 ,2 )) print (even_numbers)square=[] for value in range (1.11 ): square.append(value**2 ) print (square)min (square)max (square)sum (square)列表解析: square=[value**2 for value in range (1 ,11 )] print (square)
使用列表的一部分(切片) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 players=['charles' ,'martina' ,'michael' ,'florence' ,'eli' ] print (players[0 :3 ])print (players[1 :4 ])print (players[:4 ])print (players[2 :])print (player[-3 :])players=['charles' ,'martina' ,'michael' ,'florence' ,'eli' ] print ("here are the first three players in my team" )for player in players[:4 ]: print (player.title()) my_foods=['pizza' ,'falafel' ,'carrot cake' ] friend_foods=my_foods[:] print (my_foods)print (friend_foods)my_foods.append('cannoli' ) friend_foods.append('ice cream' ) print (my_foods)print (friend_foods)friend_foods=my_foods my_foods.append('cannoli' ) friend_foods.append('ice cream' ) print (my_foods)print (friend_foods)可以看到,不使用切片时,时将变量friend_foods与my_foods关联,而不是将my_foods的副本拷贝到friend_foods上
元组 1 2 3 4 5 6 7 8 9 10 11 python将不能修改的值称为'不可改变的' ,而不可变的列表称为'元组' dimensions=(200 ,50 ) print (dimensions[0 ])print (dimensions[1 ])但我们尝试修改元组元素时,会报错 如 :dimensions[0 ]=250 但元组变量是可以改变的 需要重新定义整个列表 dimensions=[200 ,50 ] dimensions=[400 ,50 ]
if语句好像有点帅气 if语句 1 2 3 4 5 6 cars=['audi' ,'bmw' ,'subaru' ,'toyota' ] for car in cars: if car == 'bmw' print (car.upper()) else : print (car.title())
条件测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 car = 'bmw' car == 'bmw' car='AUdi' car=='audi' requested_toppings=['mushroom' ,'onion' ,'david' ] 'mushroom' in requested_toppings
f语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 if age >= 18 : print ('you are old enough to vote' ) if age >= 18 : print ('you are old enough to vote' ) else : print ('sorry,you are too young to vote' ) if age < 4 : print ('Your admission cost is $0' ) elif : print ('Your admission cost is $25' ) else : print ('Your admission cost is $40' ) 在 if -elif -else 结构中 else 语句可以用用更详细的elif 语句代替如果想要只执行一个代码块,用 if -elif -else 结构 如果想执行多个,使用多个独立的if 语句
使用if语句处理列表
字典(难道我要背单词) 一个简单的字典 1 2 3 alien_0={'color' :'green' ,'points' :5 } print (alien_0['color' ])print (alien_0['points' ])
while循环(我要怎么跳出来呢) 函数绝对会很帅的吧 类是个什么东东 文件操作有点深奥