python的第三天
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)
本文出自 “姑蘇城” 博客,請務必保留此出處http://ji123.blog.51cto.com/11333309/1965091
python的第三天