一、冒泡排序
'''冒泡排序'''
# coding:utf-8
list = [1, 13, 5, 35, 6, 9, 10]
s = range(len(list))[::-1]
print(s)
for i in s:
for j in range(i):
if list[j] > list[j +1 ]:
list[j], list[j + 1] = list[j + 1], list[j]
print(list)
def bubbleSort(nums):
for i in range(len(nums)-1): # 这个循环负责设置冒泡排序进行的次数
for j in range(len(nums)-i-1): # j为列表下标
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
return nums
nums = [5,2,45,6,8,2,1]
print bubbleSort(nums)
二、字符串切片
# 字符串切片,取a中的abcd
a = "apbocidu"
# 方法一:
b = a[::2]
print(b)
# 方法二:
c = []
for i in range(len(a)):
if i % 2 == 0:
c.append(a[i])
print("".join(c))
三、字符串切割
# 字符串切割,"hello_world_hehe"转换成"hello","world","hehe"
a = "hello_world_hehe"
b = a.split("_")
print(b)
四、字符串合并
# 字符串合并,["a","b","c","d"]合并成"abcd"
a = ['a', 'b', 'c', 'd']
b = "".join(a) # join连接字符串
print(b)
五、列表生成式
# 列表生成式
a = [x*((-1)**(x+1)) for x in range(1, 101)]
print(a)
print(sum(a))
六、斐波那契数列(递归)
# 递归(菲波那切数列)
a = 0
b = 1
while b < 100:
print(b, end=',')
a, b =b, a+b