'''
格式化輸出的三種方式,運算符及流程控制之if判斷
'''python
# 格式化輸出的三種方式 # 一.佔位符 程序中常常會有這樣場景:要求用戶輸入信息,而後打印成固定的格式 好比要求用戶輸入用戶名和年齡,而後打印以下格式:`My name is xxx,my age is xxx.` 很明顯,用逗號進行字符串拼接,只能把用戶輸入的名字和年齡放到末尾,沒法放到指定的xxx位置,並且數字也必須通過str(數字)的轉換才能與字符串進行拼接,很是之麻煩,咱們來試一試。 ``` age = 19 print('My name is xxx,my age is '+str(age)) My name is xxx,my age is 19 age = 19 print('My name is xxx,my age is', age) My name is xxx,my age is 19 name = 'nick' age = 19 print('My name is '+name+' my age is '+str(age)) My name is nick my age is 19 ``` 上面使用的方法越看越彆扭,越看越麻煩。這就須要用到佔位符,如:%s(針對全部數據類型)、%d(僅僅針對數字類型) ``` name = 'nick' age = 19 print('my name is %s my age is %s' % (name, age)) my name is nick my age is 19 age = 19 print('my age is %d' % age) my age is 19 ``` **2、format格式化** ``` name = 'nick' age = 19 print("Hello, {}. You are {}.".format(name, age)) Hello, nick. You are 19. name = 'nick' age = 19 print("Hello, {1}. You are {0}-{0}.".format(age, name)) Hello, nick. You are 19-19. name = 'nick' age = 19 print("Hello, {name}. You are {age}-{age}.".format(age=age, name=name)) Hello, nick. You are 19-19. ``` **3、f-String格式化** ``` name = "nick" age = 19 print(f"Hello, {name}. You are {age}.") Hello, nick. You are 19. ``` 大寫的F也適用。 ``` name = "nick" age = 19 print(F"Hello, {name}. You are {age}.") Hello, nick. You are 19. age = 19 print(f'{age*2}') 38 ``` **再給你秀個之後可能會用到的操做。** ``` salary = 6.6666 print(f'{salary:.2f}') 6.67 ``` # 基本運算符 當咱們眼前飄過一隻生物後,咱們會當即得到這個生物的信息,種類、性別、身高、三維,當咱們獲取這些信息的同時,咱們還會立刻對這些信息作一些邏輯處理,如這個生物種類是老虎的時候,咱們會跑開;這個生物是人,性別爲女,可是身高只有一米三時,咱們可能會不自覺地靠近? **1、算術運算符** ``` print(1+2) 3 x = 10 y = 20 res = x+y print(res) 30 # /有零有整除,獲得一個浮點型 print(10/3) 3.3333333333333335 # 地板除,只取整數部分 print(10//3) print(10//4) 3 2 # %:取餘 print(10 % 3) 1 # **,冪 print(10**3) 1000 ``` [![029-基本運算符-算術運算符.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97%E7%AC%A6.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-算術運算符.jpg?x-oss-process=style/watermark) **2、比較運算符** [![029-基本運算符-比較運算符.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-比較運算符.jpg?x-oss-process=style/watermark) ``` pwd = '123' print(pwd != '123') print(pwd == '123') False True l1 = [1, 'a', 3] l2 = [3] print(l1 < l2) # False True try: l3 = [1, 3] print(l1 < l3) # 報錯,列表比較大小僅限於同一位置的對應的值是相同的類型 except Exception as e: print(e) name 'l1' is not defined ``` **3、賦值運算符** [![029-基本運算符-賦值運算符.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-賦值運算符.jpg?x-oss-process=style/watermark) ``` age = 19 age = age + 1 print(age) 20 age = 19 age += 1 print(age) 20 age = 19 age *= 10 print(age) 190 ``` **4、邏輯運算符** [![029-基本運算符-邏輯運算符.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-邏輯運算符.jpg?x-oss-process=style/watermark) ``` # 從左到右的方式找到邏輯運算符,找到邏輯運算符的左邊,左邊成立,再去找到邏輯運算符的右邊 print(3 > 3 and 1 > 2 or 2 > 1) # False True ``` **5、身份運算符** [![029-基本運算符-身份運算符.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-%E8%BA%AB%E4%BB%BD%E8%BF%90%E7%AE%97%E7%AC%A6.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-身份運算符.jpg?x-oss-process=style/watermark) is和==的區別:is用於判斷兩個變量引用對象是否爲同一個(是否在同一塊內存空間中), ==用於判斷引用變量的值是否相等。 ``` x = 257 y = x z = 257 print(f'x is y:{x is y}') print(f'x == y:{x == y}') print(f'x is z:{x is z}') print(f'x == z:{x == z}') x is y:True x == y:True x is z:False x == z:True ``` **6、Python運算符優先級** ``` # Python中True爲1,False爲0 print(True > 0) # True print(True > 2) # Flase ``` [![029-基本運算符-python運算符優先級.jpg?x-oss-process=style/watermark](http://www.chenyoude.com/Python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%94%BE%E5%BC%83/029-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6-python%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BC%98%E5%85%88%E7%BA%A7.jpg?x-oss-process=style/watermark)](http://www.chenyoude.com/Python從入門到放棄/029-基本運算符-python運算符優先級.jpg?x-oss-process=style/watermark) # 流程控制之if判斷 ## 一.語法 if判斷是幹什麼的呢?if判斷實際上是在模擬人作判斷。就是說若是這樣幹什麼,若是那樣幹什麼。對於ATM系統而言,則須要判斷你的帳號密碼的正確性。 **1.1 if** ``` if 條件: 代碼1 代碼2 代碼3 ... # 代碼塊(同一縮進級別的代碼,例如代碼一、代碼2和代碼3是相同縮進的代碼,這三個代碼組合在一塊兒就是一個代碼塊,相同縮進的代碼會自上而下的運行) cls = 'human' gender = 'female' age = 18 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') print('end...') 開始表白 end... ``` **1.2 if...else** if...else表示if成立代碼成立會幹什麼,else不成立會幹什麼。 ``` cls = 'human' gender = 'female' age = 38 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') else: print('阿姨好') 阿姨好 ``` **1.3 if...elif...else** if...elif...else表示if條件1成立幹什麼,elif條件2成立幹什麼,elif條件3成立幹什麼,elif...不然幹什麼。 ``` cls = 'human' gender = 'female' age = 28 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') elif cls == 'human' and gender == 'female' and age > 22 and age < 30: print('考慮下') else: print('阿姨好') 考慮下 ``` **2、if的嵌套** ``` # if的嵌套 cls = 'human' gender = 'female' age = 18 is_success = False if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') if is_success: print('那咱們一塊兒走吧...') else: print('我逗你玩呢') else: print('阿姨好') 開始表白 我逗你玩呢 ``` **3、練習** ``` # 成績評判 score = input("your score: ") score = int(score) if score >= 90: print('優秀') # elif score >= 80 and score < 90: elif score >= 80: print('良好') # elif score >= 70 and score < 80: elif score >= 70: print('普通') else: print('差') your score: 80 良好 ``` **3.2 練習2:模擬登陸註冊** ``` username: nick password: 123 username or password error ```