print ("Enter name:")

name_list = []

for i in range(0,5):

    s=input()

    name_list.append(s)

name_list.sort()

d=input("revese num of list:")

print (d)

del name_list[int(d)]

print (d)

modify=input("input you want modify")

name_list.insert(int(d),modify)

name_list.sort()

name_list.sort(reverse=TRUE) #反向的排序

print(name_list[3])

print (name_list)


python中的列表自带的函数还有

name_list.extend(['A','B','C']) #连续添加多个

name_list.remove('C') #直接移去一个值。

若查找列表中的元素可以直接

if 'a' in name_list:

    print("found it")

name_list.pop() # 弹出末尾元素



注意当你赋值一个列表应该这样

new_list = name_list[:] #对的

new_list = name_list #错的

若是

new_list = ("nihao","wohao","dajiahao") #注意这样是不可改变


说双重列表

row1 = [1,2,3]

row2 = [4,5,6]

row3 = [7,8,9]

table = [row1,row2,row3]



定义函数

def function(): #函数也可以带参数与c 相似

python的变量作用空间和c语言相同,


想强制申请为全局变量。

global ss ##申请为了全局变量



python中的对象


class Ball:  #创建个类,注意这里类中只有方法,没有属性。

    def bounce(self):

        if self.direction == "down":

            self.direction ="up" 


myBall = Ball() #创建个对象

myBall.direction ="down" #在申请好对象后可以随意定属性!

myBall.color ="red"

myBall.size = "small"

print ("I just created a ball.")

print ("My ball is ", myBall.size)

print ("My ball is",myBall.color)

print ("My ball's direction is",myBall.direction)

print ("Now I'm going to bounce the ball")

myBall.bounce()

print ("Now the ball's direction is",myBall.direction)



这里申明了类似于构造函数

class Ball:

    def __init__(self,color,size,direction):

        self.color = color

        self.size =size

        self.direction = direction

    def bounce (self):

        if self.direction == "down":

            self.direction = "up"

myBall = Ball("red","small","down")

print ("I just created a ball.")

print ("My ball is",myBall.size)

print ("My ball is ",myBall.color)

print ("Now I'm going to bounce the ball")

myBall.bounce()

print ("Now the ball's direction is",myBall.direction)