第四章 列表操作
4.1遍历整个列表:可能会发生变化的数值,列表可修改
names= [ 'niuniu' , 'ate' , 'choupi' , 'yeeeeeee' ]
for name in names:
print ( name)
4.1.2遍历中的缩进
names= [ 'niuniu' , 'ate' , 'choupi' , 'yeeeeeee' ]
for name in names:
print ( name)
print ( f" { name. title( ) } ,that is a trick" )
print ( f"i can not to see you , { name. title( ) } \n" )
print ( f"that is ok!" )
4.3创建数值列表
4.3.1 使用range()函数
range(i,m)输出从i到m-1
range(m) 打印从0到m-1
for i in range ( 1 , 5 ) :
print ( i)
print ( '\n' )
for m in range ( 5 ) :
print ( m)
4.3.1 使用range()函数创建列表
range()函数指定步长
list2= list ( range ( 2 , 11 , 2 ) )
print ( "list2=range(2,11,2):" )
print ( list2)
print ( '\n' )
list3= list ( range ( 2 , 11 , 1 ) )
print ( "list3=range(2,11,1):" )
print ( list3)
print ( '\n' )
简单查看列表最大最小求和
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ]
print ( "min is:" , min ( numbers) )
print ( '\n' )
print ( "max is:" , max ( numbers) )
print ( '\n' )
print ( "sum is:" , sum ( numbers) )
print ( '\n' )
4.4使用列表中的一部分
切片
numbers = [ 'a' , 's' , 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' ]
print ( "numbers[0:5] is:" , numbers[ 0 : 5 ] )
print ( '\n' )
print ( "numbers[:5] is:" , numbers[ : 5 ] )
print ( '\n' )
print ( "numbers[0:5:1] is:" , numbers[ 0 : 5 : 1 ] )
print ( '\n' )
print ( "numbers[0:5:2] is:" , numbers[ 0 : 5 : 2 ] )
print ( '\n' )
print ( "numbers[1:5] is:" , numbers[ 1 : 5 ] )
print ( '\n' )
print ( "numbers[-2:] is:" , numbers[ - 2 : ] )
print ( '\n' )
遍历切片
names= [ 'aqw' , 'sere' , 'dqer' , 'frewc' , 'gcxzx' , 'haae' , 'jadsc' , 'keeee' , 'leczc' ]
print ( "please output 6 names:" )
for i in names[ : 6 ] :
print ( i. title( ) )
复制列表
my_fav_food= [ 'rice' , 'dumplings' , 'noddles' , 'water' , 'beer' ]
fri_fav_foods= my_fav_food
print ( "my favorite foods is:" , my_fav_food)
print ( "\nmy friend fav_foods is:" , fri_fav_foods)
4.5元组:不可修改的元素
定义元组
遍历元组
修改元组
第五章 if语句
car= 'bmw'
print ( f"car == 'bwm': { car == 'bwm' } " )
print ( f"car == 'bmw': { car == 'bmw' } " )
print ( '\n' )
red= 'Dior'
print ( f"red.lower(): { red. lower( ) } " )
print ( red)
print ( '\n' )
dinner= 'dumplings'
if dinner != 'rice' :
print ( "yes" )
print ( '\n' )
age_0= 11
age_1= 22
print ( f"age_0>=10 and age_1>20: { age_0>= 10 and age_1> 20 } " )
print ( f"age_0>=15 and age_1>20: { age_0>= 15 and age_1> 20 } " )
print ( '\n' )
list1= [ 'qqqq' , 'www' , 'eee' , 'rrr' ]
print ( f"'eee' in list1: { 'eee' in list1} " )
print ( f"'aaa' in list1: { 'aaa' in list1} " )
第六章 字典
第七章 用户输入和while循环
第八章 函数
第九章 类
第十章 文件和异常