[ python ] 格式化輸出、字符集、and/or/not 邏輯判斷

格式化輸出

  %: 佔位符python

    s: 字符串ide

    d: 數字學習

    %%: 表示一個%, 第一個%是用來轉義編碼

 

實例:spa

name = input('姓名:')
age = int(input('年齡:'))

print('我叫%s, 個人年齡:%d,個人學習進度3%%.' %(name, age))

# 執行結果:
# 姓名:hkey
# 年齡:20
# 我叫hkey, 個人年齡:20,個人學習進度3%.

 

初始編碼

最初的編碼是由美國提出,當時只規定了 ASCII碼用來存儲字母及符號,後來爲了解決全球化文字的差別,建立了萬國碼:unicodecode

  在 unicode中,blog

    1個字節表示了全部的英文、特殊字符、數字等等;utf-8

    一箇中文須要 4個字節表示,32位 就很浪費。unicode

 

後來,從 unicode 升級到 utf-8,  UTF-8 是Unicode的實現方式之一字符串

  在 utf-8 中,一個文字用 3 個字節來存儲。

 

00000001    8位bit == 1個字節(byte)

1byte      1024byte(字節) == 1KB

1KB        1024KB == 1MB

1MB        1024MB == 1GB

1GB        1024GB == 1TB

 

and or not 邏輯判斷

判斷優先級(重點):() > not > and > or

 

練習1: 判斷下面返回結果 (提示:根據 () > not > and > or 來進行判斷)

1,3>4 or 4<3 and 1==1
# 3>4 or False
# False

2,1 < 2 and 3 < 4 or 1>2 
# True or 1>2 
# True

3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
# True or False
# True

4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
# False or False or 9 < 8
# False

5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# False or False and 9 > 8 or 7 < 6
# False or False or 7 < 6
# False or False
# False

6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# False and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# False or False and 9 > 8 or 7 < 6
# False or False or 7 < 6
# False or False
# False

 

上面是條件判斷,也能夠直接進行數字的判斷:

x or y  x爲非零,則返回x, 不然返回 y

print(1 or 2)   # 1
print(3 or 2)   # 3
print(0 or 2)   # 2
print(0 or 100) # 100

當 or 前面的數字不爲0的時候,則返回前面的數字;
當 or 前面的數字爲0,則返回後面的數字。

 

x and  y x爲True,則返回y,與 or 正好相反

print(1 and 2)  # 2
print(0 and 2)  # 0

當 and 前面的數字非0,則返回後面的數字;
當 and 前面的數字爲0,則返回0.

 

數字和布爾值之間的轉換,遵循如下兩條規則:

(1)數字轉換爲 bool值:非零轉爲bool值爲:True;0 轉換爲bool值爲:False

(2)bool值轉換爲數字:True 爲:1; False 爲 0

 

做業題:

1. 使用while循環輸入1,2,3,4,5,6 8,9,10

2. 求 1-100 的全部數的和

3. 輸出 1-100 的全部奇數

4. 輸出 1-100 的全部偶數

5. 1-2+3-4+5 ...99的全部數的和

6. 用戶登陸(三次機會重試)

 

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey


# 做業題:
# 1. 使用while循環輸入1,2,3,4,5,6 8,9,10

count = 0
while count < 10:
    count += 1 # 等價於 count = count + 1
    if count == 7:
        continue    # continue 結束本次循環,開始下一次循環,continue 如下的代碼都再也不執行
    print(count)

# 2. 求 1-100 的全部數的和

num = 0
for i in range(1, 101):  # range取一個範圍 1, 100的全部數字,經過for循環遍歷相加
    num += i
print(num)

# 3. 輸出 1-100 的全部奇數
print(list(range(1, 101, 2)))
# 4. 輸出 1-100 的全部偶數
print(list(range(2, 101, 2)))
# 5. 1-2+3-4+5 ...99的全部數的和
sum = 0
count = 1
while count < 100:
    if count % 2:   # count 對 2取餘若是爲 0 則該條件不成立,說明 count 爲偶數,count 對 2取餘若是不爲 0 則該條件成立,說明 count 爲奇數
        sum += count    # 奇數作加法
    else:
        sum -= count    # 偶數作減法
    count += 1
print(sum)

# 總結:
#     在bool值中,0 None 空 爲 False,其餘都爲 True

# 6. 用戶登陸(三次機會重試)
count = 0
while True:
    user = input('username:')
    pwd = input('password:')
    if user == 'admin' and pwd == '123':
        print('登陸成功.')
        break
    else:
        print('用戶名密碼不正確,請重試。')
        count += 1
    if count == 3:
        print('登陸驗證超過三次,登陸失敗.')
        break
做業題答案
相關文章
相關標籤/搜索