python基本操作

mix=['gang',1]
print(mix)
empty=[]
print(empty)
for i in range(0,4):
    empty.append(i)
#增加 插入
empty.extend([5,6])#extend 增加多个
empty.insert(0,7) #insert (索引,值)
print(empty)#[7, 0, 1, 2, 3, 5, 6]
empty.sort()#[0, 1, 2, 3, 5, 6, 7]
print(empty)
#删除
list=[1,4,5,7,8,9,3]
list.remove(8)
print(list)
del list[1]
print(list)
#列表分片
number=[1,2,3,4,5,6,7,8,9]
print(number)
print(number[1:3])
print(number[:3])
print(number[::-1])

 

 

 

#递归 函数调用自身
#非递归
def factorial(n):
    result=n
    for i in range(1,n):
        result*=i
    return result
result=factorial(5)
print(result)
#递归
def factorial2(n):
   if n==1:
       return 1
   else:
       return n*factorial2(n-1)
result=factorial2(5)
print(result)



你可能感兴趣的:(python基本操作)