print 2.7 和 3.0 的區別是 2.0 ===> print "hello world" 3.0 ===> print("hello world")1 print("hello world")變數賦值 臨時存儲數據name = '20'age = 43print(name,age...
-
print 2.7 和 3.0 的區別是 2.0 ===> print "hello world" 3.0 ===> print("hello world")
1 print("hello world")
-
變數賦值 臨時存儲數據
name = '20' age = 43 print(name,age)
-
輸入值用變數來存儲
# 3.0 輸入是input("please input your text") # 2.X版本里有是raw_input("please input your text") # 2.7里 input() 括弧內輸入的是什麼就會給輸出什麼類型,然並卵,3.0去掉 # 3.0里如果要用2.7里的input() eq:eval(input("please input yous text")) name = input("please your Nick name") print(name)
-
if ... else ... 縮進 eq: int(var):把var強轉成int
luckNum = int(input("please input your luck number")) if luckNum == 1: print("i want say no") elif luckNum > 10: print("no come on baby") else: print("finally say yes")
-
while迴圈
inputNum = 0 bingoNum = 13 count = 0 while count < 3: inputNum = int(input("please input yours num:")) count = count+1 if(count ==3): print("try too many ") break if(inputNum > bingoNum): print("input number is bigger!") elif(inputNum<bingoNum): print("input number is small!") elif(inputNum == 13): print("congratulation") break
-
字元串拼接 %s表占位 %s。。。賦值 %(val...)company = input("conpany:")
company = input("conpany:") companyNum = input("number:") print("info:\n company"+company+"\nnumber"+companyNum) # “+”拼接字元串每加一次需要單獨的開闢一個記憶體 print("info{} \n company: %s \n number: %s" % (company, companyNum)) #不會單獨的開闢一塊空間 節省記憶體
-
字元串方法
testString = "testsss1" print(testString.lower()) #變小寫 print(testString.upper()) #變大寫 print(testString.split("g"))#用 “g” 去分割 print(testString.count("s"))#統計“s”的次數 print(testString.replace("1","2"))
-
列表 List的使用
nameList = ['jerry','tom','jeck','Alice'] print(nameList) print(nameList[1]) #取值 nameList.append("jerry") print(nameList) #尾部添加數據 print(nameList.index("jerry")) #找到jerry的索引值 print(nameList.count("jerry")) #統計list包含字元串的count nameList.insert(0,"lulu") #根據索引插入數據 print(nameList) nameList.sort() print(nameList) #按照ASKII碼去排序 nameList.reverse() #反轉倒敘 print(nameList) print(len(nameList)) # len(list)獲取list長度 print(dir(list)) #查看list有哪些方法可用
-
list進階
nameList = [1,2,4,'ok','yes'] print(nameList[0:2]) # 結果是 【1,2】 0 2代表索引 print(nameList[0:2:2]) # 表示可以從0~2的隔2個切一下 print(nameList[-3]) #取後面的倒著取 print(nameList[-2:]) #比如取出 ok 和 yes print(range(10)) #range(0, 10) print(type(range(10))) #結果 <class 'range'> if 4 in nameList: print("is true") #判斷列表是是否包含某個值
-
元組
#yuanzu = (1,3,4,5,5,6,) 兩個方法 count和index list和元組可以互相轉換 yuanzu = (1,3,4,5,5,6,) list(yuanzu) type(yuanzu) #結果是 list tuple(yuanzu) type(yuanzu) #結果是tuple
-
運算符
# 1. ** 冪 2**3 2的三次方 # 2. 整除 1//2 =0 2//2=0 2.X里 # 3. 2.X里 <>不等於 3.X去掉 # 4. and java里並且的意思 or 或者 not 非真 # 5.Is 和Isnot type(list) is list 結果 true
-
continue跳出本次迴圈,繼續下一次迴圈
for i in range(10): if i > 3: continue print(i) 結果 0 1 2 3