一.注釋->1.#號注釋;2.'''來作為注釋;
二.Python2中只要有中文,只要中文是程式的一部分,包括注釋,就需要在程式中加上:#coding=utf-8或者#-*-coding=utf-8-*-
三.print的佔位輸入:print("%d",%age); print("%s",%name)
四.輸入:py2:raw_input("請輸入:") py3:input("請輸入:")
input 輸入進來的是str類型;如果是數位話記得int強制轉化;
五.運算子+-*/;其中:運算子//表示的意思為取整,即取商; **表示的是次方;
六.若有多個%s以上的空需要值來填,那麼在後面需要有%加小括弧;
eg: print("姓名是:%s,年齡是:%d,地址是:%s",%(name,age,addr))
七.條件判斷:
if 條件一:
......
else 條件二:
.......
elif 條件三:
.......
else:
.....
八. 如果想讓print列印出的結果不換行,就在小括弧裡面加上“end=”
while j<=5:
print("*",end="")
j+=1
print("")
九.列印三角形
思路:複雜問題簡單化,先研究簡單的套路,然後一步一步走。
先實現列印5行,再實現每行中列印的依次遞減;
i=1while i<=5: j=1 while j<=i: print("*",end="") j+=1 print("") i+=1
十.九九乘法表
print('\t')的使用
i=1while i<=9: j=1 while j<=i: print("%d*%d=%d\t"%(j,i,i*j),end="") j+=1 print("") i+=1
十一.簡單的剪刀石頭布
random.randint(0,2) //產生隨機的0-2之間的數字
import random#1. 提示並擷取使用者的輸入player = int(input("請輸入 0剪刀 1石頭 2布:"))#2. 讓電腦出一個computer = random.randint(0,2)#2. 判斷使用者的輸入,然後顯示對應的結果#if 玩家獲勝的條件:if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1): print("贏了,,,,可以去買奶粉了.....")#elif 玩家平局的條件:elif player==computer: print("平局了,,,洗洗手決戰到天亮....")else: print("輸了,,,回家拿錢 再來....")