1. 程式人生 > >2018.7.27筆記

2018.7.27筆記

一個 pan 全能 utf str 徹底 輸入 停止 asc

一. 循環
while 條件:
代碼塊(循環體)
else:
當上面的條件為假. 才會執行

執行順序:
判斷條件是否為真. 如果真. 執行循環體. 然後再次判斷條件....直到循環條件為假. 程序退出
當條件為True時,這是個死循環.
例題1.數數 1-100
count = 1
 while count < =100:
print(count)
count = count + 1
2. break和continue
break: 停止當前本層循環
continue: 停止當前本次循環. 繼續執行下一次循環(用來排除一些內容)
例題2.
讓用戶一直去輸入內容, 並打印. 直到用戶輸入q的時候退出程序
(1)while True:
content = input("請輸入一句話,(輸入q退出程序):")
if content == ‘q‘:
break # 打斷. 終止當前本層循環
print(content)
flag = True
(2)while flag:
content = input("請輸入一句話,(輸入q退出程序):")
if content == ‘q‘:
flag = False # 打斷. 終止當前本層循環
print(content)
(3)while True:
content = input("請輸入一句話,(輸入q退出程序):")
if content == ‘q‘:
continue # 停止當前本次循環. 繼續執行下一次循環
print(content)
(4)count = 1
while count <= 20:
if count == 10:
break # 不會觸發else的執行, while...else...是一個整體. break的時候徹底的停止這個整體
print(count)
count = count + 1

else: # 當上面的條件不成立的時候執行這個else中的代碼
print("數完了")
二. 格式化輸出
%s 占位字符串,全能的 什麽都能接
%d 占位數字
例題3.
name="alex"
age = 38
hobby = "浪"
location = "湖邊"
print(age+"歲的"+name+"在"+location+"喜歡"+hobby)
print("%s歲的%s在%s喜歡%s" % (age, name, location, hobby))
例題4.
name = input("請輸入名字:")
age = input("請輸入年齡:")
job = input("請輸入你的工作:")
hobby = input("請輸入你的愛好:")
s = ‘‘‘------------ info of %s -----------
Name : %s
Age : %s
job : %s
Hobbie: %s
------------- end -----------------‘‘‘ % (name, name, age, job, hobby)

print(s)
例題5.
如果你的字符串中出現了%s這樣的格式化的內容. 後面的%都認為是格式化.如果想要使用%. 需要轉義 %%
name = ‘sylar‘
print("我叫%s, 我已經學習了2%%的python了" % (name))
print("我叫周潤發. 我已經活了50%了")


三. 運算符
1.+ - * / % //
%:判斷奇偶數,判斷是不是某個數或某個數的倍數
//: (整除,地板除,計算商)
**:(次冪)5**2=25
例題
print(1+1)
print(1-1)
print(1*2)
print(1/2)
print(10%3) # 計算余數 10/3=3......1

n = 49
if n % 2 == 1:
print("奇數")
else:
print("偶數")
print(10//3) # 整除. 地板除. 計算商
print(5**3) # 5的2次冪 m**n m的n次冪
2.比較運算符
==(等於) !=(不等於) <>(不等於) <= >= < >
例題
a = 10
b = 20
print(a == b) # 等於
print(a != b) # 不等於
3.賦值運算
+= -= *= /= %= //=
例題
a = 1
b = 2
a += b # a = 3 a+=b => a = a + b
# a *= b => a = a * b
print(a)
print(b)
4.邏輯運算
1. and 並且的含義. 左右兩端同時為真. 結果才能是真.
2. or 或者的含義. 左右兩端有一個是真. 結果就是真. 所有的條件都是假. 結果才是假
3. not 取反 非真既假, 非假既真
順序: () => not => and => or 相同的運算. 從左往右算
print(1>2 and 4<6 or 5>7)#False
print(1 > 2 or 3 > 4)#false
print(5>3 or 4<6)#True
print(5>3 or 4>6)#True
print(3>4 or 4<3 and 1==1) # False
print(1 < 2 and 3 < 4 or 1>2 ) # True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # True
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # False
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False
x or y 如果x是0 返回y, 如果x是非零, 返回x
print(1 or 2) # 1
print(1 or 0) # 1
print(0 or 1) # 1
print(0 or 2) # 2
print(0 or 1 or 2 or 3)
print(3 or 0 or 1 or 0 or 2)
and和or相反. 不要去總結and. 記住or
print(1 and 2) # 2
四. 編碼
1. ascii. 最早的編碼. 至今還在使用. 8位一個字節(字符)
2. GBK. 國標碼. 16位2個字節.
3. unicode. 萬國碼. 32位4個字節
4. UTF-8. 可變長度的unicode.
英文: 8位. 1個字節
歐洲文字:16位. 2個字節
漢字. 24位. 3個字節
8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
五.in 和 not in
in:在裏面
not in:不在裏面
例題
content = input("請輸入你的評論:")
if "馬化騰" not in content:
print("你的言論不和諧")
else:
print(content)
 

2018.7.27筆記