python函数学习1

上一篇 / 下一篇  2019-09-19 14:48:37 / 个人分类:python相关

函数分类函数作用函数名例子运行结果
 剔除字符串开头的空白lstrip()favorite_language = ' python '
favorite_language.lstrip()
‘python ’
 剔除字符串右边的空白rstrip()favorite_language = ' python '
favorite_language.rstrip()
‘ python’
 剔除字符串两端的空白strip()favorite_language = ' python '
favorite_language.strip()
‘python’
 以首字母大写的方式显示英文名称title()name = "ada loveLace"
print(name.title())
Ada Lovelace
 全部大写upper()name = "Ada LoveLace"
print(name.upper())
ADA LOVELACE
 全部小写lower()name = "Ada LoveLace"
print(name.lower())
ada lovelace
 换行符\n  
 制表符\t  
 换行制表符\n\t  
 字符转化str()age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
Happy 23rd Birthday!
 注释用#标识#  
 数组,列表,
用-1索引访问倒数第一个元素;
-2返回倒数第二个列表元素;以此类推
   
 修改列表元素 motorcycles = ['honda', 'yamaha' 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
 
 列表中添加元素.append()motorcycles = ['honda', 'yamaha' 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
 
 列表中插入元素.insert()motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
 列表中删除元素,无法再访问它了。del语句motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)
['yamaha', 'suzuki']
 使用pop()删除元素,
删除列表末尾的元素
pop()motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
['honda', 'yamaha', 'suzuki']

['honda', 'yamaha']

suzuki
 从列表中删除元素,可接着使用它的值remove()  
 对列表进行永久性排序sort()  
 对列表进行临时性排序sorted()>>> cars = ['bmw', 'audi', 'toyota', 'subaru']
>>> print("here is the original list:")

>>> print(cars)

>>> print("\nHere is the sorted list:")


>>> print(sorted(cars))
here is the original list:

['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:

['audi', 'bmw', 'subaru', 'toyota']
 倒着打印列表;是永久性地修改列表元素的排列顺序,但随时可恢复,只要再用一次该函数;reverse()>>> cars = ['bmw', 'audi', 'toyota', 'subaru']
>>> print(cars)

>>> cars.reverse()
>>> print(cars)

>>> cars.reverse()
>>> print(cars)
['bmw', 'audi', 'toyota', 'subaru']

['subaru', 'toyota', 'audi', 'bmw']

['bmw', 'audi', 'toyota', 'subaru']
 列表的长度len()  
 生成一串数字;
一般不包含第二个值
range()for value in range(1,6)
   print(value)
1
2
3
4
5
 转化成列表list()numbers = list(range(1,6))
print(numbers)
[1, 2, 3, 4, 5]
 使用列表的一部分,到达第二个索引前面的元素后停止;列表名([])players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
['martina', 'michael', 'florence']
 复制列表 my_foods = ['pizza', 'falafel', 'carrotcake', 'cannoli','ice cream']
friend_foods = my_foods[:]
my_foods.append('cannoli002')
friend_foods.append('ice cream002')
print("My favorite pizzas are:")
for my_food in my_foods[:]:
 print(my_food.title())
print("My friend's favorite pizzas are:")
for friend_food in friend_foods[:]:
 print(friend_food.title())
My favorite pizzas are:
Pizza
Falafel
Carrotcake
Cannoli
Ice Cream
Cannoli002
My friend's favorite pizzas are:
Pizza
Falafel
Carrotcake
Cannoli
Ice Cream
Ice Cream002
 检查值是否在列表中in / not inbanned_users = ['andrew', 'carolina', 'david' ]
user = 'marie'
if user not in banned_users:
 print(user.title() + ",you can post a response if you wish.")
Marie,you can post a response if you wish.
if语句if-else语句 alien_color = ['green', 'yellow', 'red']
if 'green' in alien_color:
 print("player get 5 points")
else:
 print("player get 10 points")
player get 5 points
 if-elif-else语句 alien_color = ['green', 'yellow', 'red']
if 'yellow' in alien_color:
 print("player get 10 points")
elif 'red' in alien_color:
 print("player get 5 points")
else :
 print("green player get 5 points")
player get 10 points
 多个elif代码 alien_color = ['green', 'yellow', 'red']
if 'green' in alien_color:
 print("player get 5 points")
elif 'yellow' in alien_color:
 print("player get 10 points")
elif 'red' in alien_color:
 print("player get 10 points")
player get 5 points
 多个if代码 alien_color = ['green', 'yellow', 'red']
if 'green' in alien_color:
 print("shoot green player get 5 points")
if 'yellow' in alien_color:
 print("shoot yellow player get 10 points")
if 'red' in alien_color:
 print("shoot red player get 15 points")
shoot green player get 5 points
shoot yellow player get 10 points
shoot red player get 15 points

TAG:

 

评分:0

我来说两句

Open Toolbar