格式化輸出
只需要把要打印的格式先准備好, 由於里面的 一些信息是需要用戶輸入的,你沒辦法預設知道,因此可以先放置個占位符,再把字符串里的占位符與外部的變量做個映射關系就好啦
name = input("Name:") age = input("Age:") job = input("Job:") hobbie = input("Hobbie:") info = ''' ------------ info of %s ----------- #這里的每個%s就是一個占位符,本行的代表 后面拓號里的 name Name : %s #代表 name Age : %s #代表 age job : %s #代表 job Hobbie: %s #代表 hobbie ------------- end ----------------- ''' %(name,name,age,job,hobbie) # 這行的 % 號就是 把前面的字符串 與拓號 后面的 變量 關聯起來 print(info)
%s就是代表字符串占位符,除此之外,還有%d,是數字占位符, 如果把上面的age后面的換成%d,就代表你必須只能輸入數字.
基本運算符
以下假設變量:a=10,b=20

比較運算
以下假設變量:a=10,b=20

賦值運算
以下假設變量:a=10,b=20

邏輯運算
邏輯運算

針對邏輯運算的進一步研究:
1,在沒有()的情況下not 優先級高於 and,and優先級高於or,即優先級關系為( )>not>and>or,同一優先級從左往右計算。
例題:
判斷下列邏輯語句的True,False。
1,3>4 or 4<3 and 1==1 2,1 < 2 and 3 < 4 or 1>2 3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
2 , x or y , x為真,值就是x,x為假,值是y;
x and y, x為真,值是y,x為假,值是x。

例題:求出下列邏輯語句的值。
8 or 4 0 and 3 0 or 4 and 3 or 7 or 9 and 6
基本循環
while 條件:
# 循環體
# 如果條件為真,那么循環體則執行
# 如果條件為假,那么循環體不執行
|
循環中止語句
如果在循環的過程中,因為某些原因,你不想繼續循環了,怎么把它中止掉呢?這就用到break 或 continue 語句
- break用於完全結束一個循環,跳出循環體執行循環后面的語句
- continue和break有點類似,區別在於continue只是終止本次循環,接着還執行后面的循環,break則完全終止循環
例子:break
count = 0
while count <= 100 : #只要count<=100就不斷執行下面的代碼 print("loop ", count) if count == 5: break count +=1 #每執行一次,就把count+1,要不然就變成死循環啦,因為count一直是0 print("-----out of while loop ------")
輸出
loop 0
loop 1 loop 2 loop 3 loop 4 loop 5 -----out of while loop ------
例子:continue
count = 0
while count <= 100 : count += 1 if count > 5 and count < 95: #只要count在6-94之間,就不走下面的print語句,直接進入下一次loop continue print("loop ", count) print("-----out of while loop ------")
輸出
loop 1
loop 2 loop 3 loop 4 loop 5 loop 95 loop 96 loop 97 loop 98 loop 99 loop 100 loop 101 -----out of while loop ------
12.3,while ... else ..
與其它語言else 一般只與if 搭配不同,在Python 中還有個while ...else 語句
while 后面的else 作用是指,當while 循環正常執行完,中間沒有被break 中止的話,就會執行else后面的語句
count = 0
while count <= 5 : count += 1 print("Loop",count) else: print("循環正常執行完啦") print("-----out of while loop ------")
輸出
Loop 1
Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 循環正常執行完啦 -----out of while loop ------
如果執行過程中被break啦,就不會執行else的語句啦
count = 0
while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循環正常執行完啦") print("-----out of while loop ------")
輸出
Loop 1
Loop 2
-----out of while loop ------

