python -- while循環,格式化輸出,運算符,初識編碼

1、while循環python

   一、語法編碼

        while   條件:spa

                  循環體(結果)code

   若是條件爲真,則直接執行結果),而後再次判斷條件,知道條件爲假,中止循環。blog

 

while True:
    print('你是誰呢')

 

 退出循環:一、break   二、改變條件utf-8

while true:
    content = input('請輸入你想要唱的歌曲,輸入aq退出:')
    if content == 'aq':
        break
    print('你唱的是什麼:',content)

  

count = 1
while count <=3:  # 最多輸入三次
    # count = 1  # 次數,死循環
    song = input('請輸入你想要唱的歌曲:')
    print('你想要唱的歌曲是:',song)
    # 改變count
    count = count + 1

 

 三、流程控制-break 和continueci

 

break:馬上跳出這次循環,完全結束unicode

continue:中止當前循環,繼續執行下一次循環字符串

count = 1
while count <=3:
    song = input('請輸入你想要唱的歌曲:')
    if song == '':
        continue # 中止本次循環,繼續執行下一次循環,不會完全中斷循環
    print('你想要唱的歌曲是:',song)
    count = count + 1

  

一些程序input

 # 輸出 1-100
num = 1
while num <= 100:
    print(num)
    num = num + 1

#請輸入1-100之間的偶數
num = 1
while num <=100:
    a = num%2
    if a == 0:
        print(num)
    num = num + 1

#請輸入1-100之間的奇數
num = 1
while num <=100:
    a = num%2
    if a == 1:
        print(num)
    num = num + 1


#請輸入1-100的和
sum = 0
num = 1
while num <= 100:
    sum = sum + num
    num = num + 1
print(sum)

  

# 用戶輸入個數. 判斷這個數是不是一個質數
num = int(input('請輸入一個數:'))
i = 2
if num < i and num >= 0:
    print('這不是一個質數')
else:
    while i < num:
        if num % i == 0:
            print('這不是一個質數')
            break
        i += 1
    else:
        print('這是一個質數')

  

2、格式化輸出

    %s  表示字符串的佔位,是全能佔位,既能夠是字符串也能夠是數字

    %d       表示數字的佔位,只能佔位數字

    注:     若是一句話中使用了格式化輸出,%就是佔位,若是想正常顯示%,需寫爲%%,錶轉義

name = input('你的名字是什麼:')
gender = input('你的性別是什麼:')
star = input('你最喜歡的明星是誰:')
science =input('你最喜歡的科學家是:')
# 鏈接
print("個人名字是"+name+",性別"+gender+",我喜歡的明星是"+star+",我喜歡的科學家是"+science)
# 格式化%s
print("個人名字是%s,性別是%s,喜歡的明星是%s,喜歡的科學家是%s"%(name,gender,star,science))
# 新版本可支持
print(f"個人名字是'{name},性別是{gender},喜歡的明星是{star},喜歡的科學家是{science}")

  

3、運算符

      計算機能夠進行的運算有不少種:算術運算(+   -   *   /   %  **(冪) //(整除)),比較運算(==    !=   <>   >   <   >=   <=),邏輯運算(and   or   not),賦值運算,成員運算,身份運算,位運算

      着重解釋邏輯運算

      and   而且,左右兩端同時爲真,結果才能爲真

      or    或者,左右兩端有一個爲真,結果則爲真

      not   非 ,非真即假,非假即真

      混合運算

      運算順序: ()  -> not  ->   and  ->   or

     當出現  x  or  y  時,判斷 x 是否爲0,若是 x == 0 則爲 y ,不然返回  x

     當出現 x  and  y  時,狀況正好和  or 相反

     True 當成 1,False當成 0 .

print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)   #True
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 )  #False
print(8 or 3 and 4 or 2 and 0 or 9 and 7)   #8
print(0 or 2 and 3 and 4 or 6 and 0 or 3)   #4
print(6 or 2 > 1)   #6
print(3 or 2 > 1 )   #3
print(0 or 5 < 4)  # False
print(5 < 4 or 3)  #3
print(2 > 1 or 6)  # True
print(3 and 2 > 1)   # True
print(0 and 3 > 1)  #0
print(2 > 1 and 3)   #3
print(3 > 1 and 0 )  #0
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) # 2


成員運算
ad = input("請輸入你的廣告:")
if "最" in ad or "第一" in ad or "全球" in ad:
    print("不合法的")

 

     4、初識編碼

            一、ASCII碼  爲8bit(1 byte(字節)),有256個碼位,用到了前128個,首位都是0,包括大小寫英文字母,一些符號。

             二、GBK ,國標碼,兼容ASCII碼,16bit(2字節)

             三、unicode  對全部的編碼進行統一,萬國碼,32bit(4字節)

             四、utf-8    可變長度的unicode

                1byte = 8 bit

                 1 KB = 1024 byte

                 1 MB = 1024 KB

                 1 GB =  1024 MB

                  1 TB = 1024 GB

                  1 PB = 1024 TB

相關文章
相關標籤/搜索