Day 7 对数据类型的总结、input输入及函数定义
复习前6天的内容
Python基本数据类型之一
python基本数据类型之二:
- 列表复习联系
问题:用remove,结合for或者while删除列表内容:
bicycles = ['trek', 'cannondale', 'redline', 'trek', 'specialized']
用while实现删除’trek’:
bicycle_to_remove = 'trek'
print ("Bicycles before remove: \n")
print (bicycles)
while bicycle_to_remove in bicycles:
bicycles.remove(bicycle_to_remove)
print ("\nBicycles after remove:\n")
print (bicycles)
输出正确:
换成for实现:
bicycle_to_remove = 'trek'
print ("Bicycles before remove: \n")
print (bicycles)
for bicycle_to_remove in bicycles:
bicycles.remove(bicycle_to_remove)
print ("\nBicycles after remove:\n")
print (bicycles)
输出结果错误:
原来,for每循环一次,首先,返回值,也就是当前索引指向的元素,然后遍历的索引都会+1,所以…
-
输入
- input()函数,参数为需要给用户的提示信息,返回值为输入的字符串信息;
-
python基本函数
- int()
- %求模
-
while循环
- break
- continue
-
自定义函数
- def
#定义一个显示宠物类型及名字的函数,参数为类型及名字 def describe_pet(animal_type, pet_name): print ("\nI have a " + animal_type + ".") print ("My " + animal_type + "'s name is " + pet_name.title() + ".") #定义完毕 #调用该函数 describe_pet('hamster', 'harry' )
- 形参,实参,关键字实参;
- 默认值,提供默认值的形参,应该放在不提供默认值的形参之后;
- return定义函数返回值