Python学习笔记【二】

Python的冒泡排序:

 python冒泡排序

# !/usr/bin/python
#  -*- coding: UTF-8 -*-

=  xrange(0, 8 )
print  x[0]
print  x[ 7 ]
def  bubbleSort(numbers):
    
for  j  in  xrange(len(numbers)  -   1 , - 1 , - 1 ):
        
for  i  in  xrange(j):
            
if  numbers[i]  >  numbers[i + 1 ]:
                numbers[i],numbers[i + 1 ] = numbers[i + 1 ],numbers[i]
            
print  numbers
            
def  main():
    numbers = [ 23 , 12 , 9 , 15 , 6 ]
    bubbleSort(numbers)
if   __name__ == ' __main__ ' :
    main()

 Python的基本数据结构:

1.元组 

元组:range([start],stop,[step])
       map(function,sequence[,sequence,...])#if function==None :map()返回一个由参数sequence组成的元组或列表
元组
# !/usr/bin/python
#  -*- coding: UTF-8 -*-

tuple  =  ( " apple " " banana " " grape " " orange " )
print  tuple[ - 1 ]
print  tuple[ - 2 ]
tuple2  =  tuple[ 1 : 3 ]
tuple3  =  tuple[0: - 2 ]
tuple4  =  tuple[ 2 : - 2 ]
print  tuple2
print  tuple3
print  tuple4

fruit1  =  ( " apple " " banana " )
fruit2  =  ( " grape " " orange " )
tuple  =  (fruit1, fruit2)
print  tuple
print   " tuple[0][1] = " ,tuple[0][ 1 ]
print   " tuple[1][1] = " ,tuple[ 1 ][ 1 ]
# print tuple[1][2]

#打包
tuple  =  ( " apple " " banana " " grape " " orange " )
#解包
a, b, c, d  =  tuple
print  a, b, c, d


    

 

元组的只读性:

 

# !/usr/bin/python
#  -*- coding: UTF-8 -*-

tuple  =  ( " apple " " banana " " grape " " orange " )
# tuple[0] = "a"   #错误  元组只读的。
# t = ("apple",)
# t = ()
print  tuple[0]
print  tuple[ - 1 ]
print  tuple[ - 2 ]
# print t[0]    #错误 越界

 元组的遍历:

 

遍历元组
# !/usr/bin/python
#  -*- coding: UTF-8 -*-

# 使用range()循环遍历
tuple  =  (( " apple " " banana " ),( " grape " " orange " ),( " watermelon " ,),( " grapefruit " ,))
for  i  in  range(len(tuple)):
    
print   " tuple[%d] : "   %  i,  ""  ,
    
for  j  in  range(len(tuple[i])):
        
print  tuple[i][j],  ""  ,
    
print

# 使用map()循环遍历  map解包
=  0
for  a  in  map(None,tuple):
    
print   " tuple[%d] : "   %  k,  ""  ,
    
for  x  in  a:
        
print  x,  ""  , 
    
print
    k  +=   1

 

 

 


 2.列表
# !/usr/bin/python
#  -*- coding: UTF-8 -*-

list  =  [ " apple " " banana " " grape " " orange " ]
print  list
print  list[ 2 ]
list.append( " watermelon " )
list.insert( 1 " grapefruit " )
print  list
list.remove( " grape " )
print  list
# list.remove("a")
print  list.pop()
print  list

list  =  [ " apple " " banana " " grape " " orange " ]
print  list[ - 2 ]
print  list[ 1 : 3 ]
print  list[ - 3 : - 1 ]
list  =  [[ " apple " " banana " ],[ " grape " " orange " ],[ " watermelon " ],[ " grapefruit " ]]
for  i  in  range(len(list)):
    
print   " list[%d] : "   %  i,  ""  ,
    
for  j  in  range(len(list[i])):
        
print  list[i][j],  ""  ,
    
print

# ################################运行结果#####################################
[ ' apple ' ' banana ' ' grape ' ' orange ' ]
grape
[ ' apple ' ' grapefruit ' ' banana ' ' grape ' ' orange ' ' watermelon ' ]
[ ' apple ' ' grapefruit ' ' banana ' ' orange ' ' watermelon ' ]
watermelon
[ ' apple ' ' grapefruit ' ' banana ' ' orange ' ]
grape
[ ' banana ' ' grape ' ]
[ ' banana ' ' grape ' ]
list[0] :  apple  banana 
list[ 1 ] :  grape  orange 
list[ 2 ] :  watermelon 
list[ 3 ] :  grapefruit 

 

其它:

function()可以返回多个值

你可能感兴趣的:(python)