python基礎-人機互動和運算子
阿新 • • 發佈:2020-11-27
# 1、接收使用者的輸入 # 在Python3:input會將使用者輸入的所有內容都存成字串型別 # username = input("請輸入您的賬號:") # "egon" # print(username, type(username)) # age = input("請輸入的你的年齡: ") # age="18" # print(age, type(age)) # age=int(age) # int只能將純數字的字串轉成整型 # print(age > 16) # int("12345") # int("1234.5") # int("1234abc5")
('hello world') # 只輸出一個值 hello world >>> print('first','second','third') # 一次性輸出多個值,值用逗號隔開 first second third # 預設print功能有一個end引數,該引數的預設值為"\n"(代表換行),可以將end引數的值改成任意其它字元 print("aaaa",end='') print("bbbb",end='&') print("cccc",end='@') #整體輸出結果為:aaaabbbb&cccc@
# 在python2中: # raw_input():用法與python3的input一模一樣# input(): 要求使用者必須輸入一個明確的資料型別,輸入的是什麼型別,就存成什麼型別 # >>> age=input(">>>>>>>>>>>>>>>>>>>>>: ") # >>>>>>>>>>>>>>>>>>>>>: 18 # >>> age,type(age) # (18, <type 'int'>)# >>> # >>> x=input(">>>>>>>>>>>>>>>>>>>>>: ") # >>>>>>>>>>>>>>>>>>>>>: 1.3 # >>> x,type(x) # (1.3, <type 'float'>) # >>> # >>> x=input(">>>>>>>>>>>>>>>>>>>>>: ") # >>>>>>>>>>>>>>>>>>>>>: [1,2,3] # >>> x,type(x) # ([1, 2, 3], <type 'list'>) # >>> # 2。字串的格式化輸出 # 2.1 % # 值按照位置與%s一一對應,少一個不行,多一個也不行 # res="my name is %s my age is %s" %('egon',"18") # res="my name is %s my age is %s" %("18",'egon') # res="my name is %s" %"egon" # print(res) # 以字典的形式傳值,打破位置的限制 # res="我的名字是 %(name)s 我的年齡是 %(age)s" %{"age":"18","name":'egon'} # print(res) # %s可以接收任意型別 # print('my age is %s' %18) # print('my age is %s' %[1,23]) # print('my age is %s' %{'a':333}) # print('my age is %d' %18) # %d只能接收int # print('my age is %d' %"18")
# 1、格式的字串(即%s)與被格式化的字串(即傳入的值)必須按照位置一一對應 # ps:當需格式化的字串過多時,位置極容易搞混 print('%s asked %s to do something' % ('egon', 'lili')) # egon asked lili to do something print('%s asked %s to do something' % ('lili', 'egon')) # lili asked egon to do something # 2、可以通過字典方式格式化,打破了位置帶來的限制與困擾 print('我的名字是 %(name)s, 我的年齡是 %(age)s.' % {'name': 'egon', 'age': 18}) kwargs={'name': 'egon', 'age': 18} print('我的名字是 %(name)s, 我的年齡是 %(age)s.' % kwargs)
# 2.2 str.format:相容性好 # 按照位置傳值 # res='我的名字是 {} 我的年齡是 {}'.format('egon',18) # print(res) # res='我的名字是 {0}{0}{0} 我的年齡是 {1}{1}'.format('egon',18) # print(res) # 打破位置的限制,按照key=value傳值 # res="我的名字是 {name} 我的年齡是 {age}".format(age=18,name='egon') # print(res) # 瞭解知識 """ 2.4 填充與格式化 # 先取到值,然後在冒號後設定填充格式:[填充字元][對齊方式][寬度] # *<10:左對齊,總共10個字元,不夠的用*號填充 print('{0:*<10}'.format('開始執行')) # 開始執行****** # *>10:右對齊,總共10個字元,不夠的用*號填充 print('{0:*>10}'.format('開始執行')) # ******開始執行 # *^10:居中顯示,總共10個字元,不夠的用*號填充 print('{0:*^10}'.format('開始執行')) # ***開始執行*** 2.5 精度與進位制 print('{salary:.3f}'.format(salary=1232132.12351)) #精確到小數點後3位,四捨五入,結果為:1232132.124 print('{0:b}'.format(123)) # 轉成二進位制,結果為:1111011 print('{0:o}'.format(9)) # 轉成八進位制,結果為:11 print('{0:x}'.format(15)) # 轉成十六進位制,結果為:f print('{0:,}'.format(99812939393931)) # 千分位格式化,結果為:99,812,939,393,931 """ # 2.3 f:python3.5以後才推出 x = input('your name: ') y = input('your age: ') res = f'我的名字是{x} 我的年齡是{y}' print(res)
# 瞭解f的新用法:{}內的字串可以被當做表示式執行
# res=f'{10+3}'
# print(res)
# 1、算數運算子 # print(10 + 3.1) # print(10 + 3) # print(10 / 3) # 結果帶小數 # print(10 // 3) # 只保留整數部分 # print(10 % 3) # 取模、取餘數 # print(10 ** 3) # 取模、取餘數 # 2、比較運算子: >、>=、<、<=、==、!= # print(10 > 3) # print(10 == 10) # # print(10 >= 10) # print(10 >= 3) # name=input('your name: ') # print(name == 'egon') # 3、賦值運算子 # 3.1 =:變數的賦值 # 3.2 增量賦值: # age = 18 # # age += 1 # age=age + 1 # # print(age) # # age*=3 # age/=3 # age%=3 # age**=3 # age=age**3 # 3.3 鏈式賦值 # x=10 # y=x # z=y # z = y = x = 10 # 鏈式賦值 # print(x, y, z) # print(id(x), id(y), id(z)) # 3.4 交叉賦值 m=10 n=20 # print(m,n) # 交換值 # temp=m # m=n # n=temp # print(m,n) # m,n=n,m # 交叉賦值 # print(m,n) # 3.5 解壓賦值 salaries=[111,222,333,444,555] # 把五個月的工資取出來分別賦值給不同的變數名 # mon0=salaries[0] # mon1=salaries[1] # mon2=salaries[2] # mon3=salaries[3] # mon4=salaries[4] # 解壓賦值 # mon0,mon1,mon2,mon3,mon4=salaries # print(mon0) # print(mon1) # print(mon2) # print(mon3) # print(mon4) # mon0,mon1,mon2,mon3=salaries # 對應的變數名少一個不行 # mon0,mon1,mon2,mon3,mon4,mon5=salaries # 對應的變數名多一個也不行 # 引入*,可以幫助我們取兩頭的值,無法取中間的值 # 取前三個值 # x,y,z,*_=salaries=[111,222,333,444,555] # *會將沒有對應關係的值存成列表然後賦值給緊跟其後的那個變數名,此處為_ # print(x,y,z) # print(_) # 取後三個值 # *_,x,y,z=salaries=[111,222,333,444,555] # print(x,y,z) # x,*_,y,z=salaries=[111,222,333,444,555] # print(x,y,z) # salaries=[111,222,333,444,555] # _,*middle,_=salaries # print(middle) # 解壓字典預設解壓出來的是字典的key x,y,z=dic={'a':1,'b':2,'c':3} print(x,y,z)
# 1、可變不可變型別 # 可變型別:值改變,id不變,證明改的是原值,證明原值是可以被改變的 # 不可變型別:值改變,id也變了,證明是產生新的值,壓根沒有改變原值,證明原值是不可以被修改的 # 2、驗證 # 2.1 int是不可變型別 # x=10 # print(id(x)) # x=11 # 產生新值 # print(id(x)) # 2.2 float是不可變型別 # x=3.1 # print(id(x)) # x=3.2 # print(id(x)) # 2.3 str是不可變型別 # x="abc" # print(id(x)) # x='gggg' # print(id(x)) # 小結:int、float、str都被設計成了不可分割的整體,不能夠被改變 # 2.4 list是可變型別 # l=['aaa','bbb','ccc'] # print(id(l)) # l[0]='AAA' # print(l) # print(id(l)) # 2.5 dict # dic={'k1':111,'k2':222} # print(id(dic)) # dic['k1']=3333333333 # # print(dic) # print(id(dic)) #2.6 bool不可變 # 關於字典補充: # 定義:{}內用逗號分隔開多key:value, # 其中value可以是任意型別 # 但是key必須是不可變型別 # dic={ # 'k1':111, # 'k2':3.1, # 'k3':[333,], # 'k4':{'name':'egon'} # } # # dic={ # 2222:111, # 3.3:3.1, # 'k3':[333,], # 'k4':{'name':'egon'} # } # print(dic[3.3]) # dic={[1,2,3]:33333333} # dic={{'a':1}:33333333}條件定義
# 2、什麼是條件?什麼可以當做條件?為何要要用條件? # 第一大類:顯式布林值 # 2.1 條件可以是:比較運算子 # age = 18 # print(age > 16) # 條件判斷之後會得到一個布林值 # 2.1 條件可以是:True、False # is_beautiful=True # print(is_beautiful) # 第二大類:隱式布林值,所有的值都可以當成條件去用 # 其中0、None、空(空字串、空列表、空字典)=》代表的布林值為False,其餘都為真
1 # 一:not、and、or的基本使用 2 # not:就是把緊跟其後的那個條件結果取反 3 # ps:not與緊跟其後的那個條件是一個不可分割的整體 4 # print(not 16 > 13) 5 # print(not True) 6 # print(not False) 7 # print(not 10) 8 # print(not 0) 9 # print(not None) 10 # print(not '') 11 12 # and:邏輯與,and用來連結左右兩個條件,兩個條件同時為True,最終結果才為真 13 # print(True and 10 > 3) 14 15 # print(True and 10 > 3 and 10 and 0) # 條件全為真,最終結果才為True 16 # print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3) # 偷懶原則 17 18 # or:邏輯或,or用來連結左右兩個條件,兩個條件但凡有一個為True,最終結果就為True, 19 # 兩個條件都為False的情況下,最終結果才為False 20 # print(3 > 2 or 0) 21 # print(3 > 4 or False or 3 != 2 or 3 > 2 or True) # 偷懶原則 22 23 # 二:優先順序not>and>or 24 # ps: 25 # 如果單獨就只是一串and連結,或者說單獨就只是一串or連結,按照從左到右的順訊依次運算即可(偷懶原則) 26 # 如果是混用,則需要考慮優先順序了 27 28 # res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3 29 # print(res) 30 # 31 # # False False False 32 # res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3 33 # print(res) 34 35 36 37 res=3>4 and ((not 4>3) or 1==3) and ('x' == 'x' or 3 >3) 38 print(res)not and or
1 # 1、成員運算子 2 # print("egon" in "hello egon") # 判斷一個字串是否存在於一個大字串中 3 # print("e" in "hello egon") # 判斷一個字串是否存在於一個大字串中 4 5 # print(111 in [111,222,33]) # 判斷元素是否存在於列表 6 7 # 判斷key是否存在於字典 8 # print(111 in {"k1":111,'k2':222}) 9 # print("k1" in {"k1":111,'k2':222}) 10 11 # not in 12 # print("egon" not in "hello egon") # 推薦使用 13 # print(not "egon" in "hello egon") # 邏輯同上,但語義不明確,不推薦 14 15 # 2、身份運算子 16 # is # 判斷的是id是否相等成員運算子
1 # print(1) 2 # print(2) 3 # print(3) 4 # if 條件: 5 # 程式碼1 6 # 程式碼2 7 # 程式碼3 8 # print(4) 9 # print(5) 10 ''' 11 語法1: 12 if 條件: 13 程式碼1 14 程式碼2 15 程式碼3 16 17 ''' 18 # age = 60 19 # is_beautiful = True 20 # star = '水平座' 21 # 22 # if age > 16 and age < 20 and is_beautiful and star == '水平座': 23 # print('我喜歡,我們在一起吧。。。') 24 # 25 # print('其他程式碼.............') 26 27 28 ''' 29 語法2: 30 if 條件: 31 程式碼1 32 程式碼2 33 程式碼3 34 else: 35 程式碼1 36 程式碼2 37 程式碼3 38 ''' 39 40 # age = 60 41 # is_beautiful = True 42 # star = '水平座' 43 # 44 # if age > 16 and age < 20 and is_beautiful and star == '水平座': 45 # print('我喜歡,我們在一起吧。。。') 46 # else: 47 # print('阿姨好,我逗你玩呢,深藏功與名') 48 # 49 # print('其他程式碼.............') 50 51 52 ''' 53 語法3: 54 if 條件1: 55 程式碼1 56 程式碼2 57 程式碼3 58 elif 條件2: 59 程式碼1 60 程式碼2 61 程式碼3 62 elif 條件2: 63 程式碼1 64 程式碼2 65 程式碼3 66 ''' 67 # score=73 68 # if score >= 90: 69 # print('優秀') 70 # elif score >= 80 and score < 90: 71 # print('良好') 72 # elif score >= 70 and score < 80: 73 # print('普通') 74 75 # 改進 76 # score = input('請輸入您的成績:') # score="18" 77 # score=int(score) 78 # 79 # if score >= 90: 80 # print('優秀') 81 # elif score >= 80: 82 # print('良好') 83 # elif score >= 70: 84 # print('普通') 85 86 87 ''' 88 語法3: 89 if 條件1: 90 程式碼1 91 程式碼2 92 程式碼3 93 elif 條件2: 94 程式碼1 95 程式碼2 96 程式碼3 97 elif 條件2: 98 程式碼1 99 程式碼2 100 程式碼3 101 ... 102 else: 103 程式碼1 104 程式碼2 105 程式碼3 106 ''' 107 # score = input('請輸入您的成績:') # score="18" 108 # score=int(score) 109 # 110 # if score >= 90: 111 # print('優秀') 112 # elif score >= 80: 113 # print('良好') 114 # elif score >= 70: 115 # print('普通') 116 # else: 117 # print('很差,小垃圾') 118 # 119 # print('=====>') 120 121 122 ''' 123 if巢狀if 124 ''' 125 age = 17 126 is_beautiful = True 127 star = '水平座' 128 129 if 16 < age < 20 and is_beautiful and star == '水平座': 130 print('開始表白。。。。。') 131 is_successful = True 132 if is_successful: 133 print('兩個從此過上沒羞沒臊的生活。。。') 134 else: 135 print('阿姨好,我逗你玩呢,深藏功與名') 136 137 print('其他程式碼.............')if 條件