與使用者互動

1、什麼是使用者互動
使用者互動就是人往計算機裡輸入資料,計算機輸出的結果
2、為什麼要與使用者互動
為了讓計算機能夠像人一樣與使用者溝通交流
3、怎麼使用
3.1、輸入(input)
username = input('請輸入您的使用者名:')
#解釋:将input擷取到的使用者輸入綁定給變量名username,并且input擷取到的使用者輸入全部都會轉換成字元串
3.2、輸出(print)
1、格式為print(),其中列印多個元素時,需要用逗号隔開。
2、預設print功能有一個end參數,該參數的預設值為“\n”(代表換行),可以将end參數的值改成任意其他字元
print('hello',end='')
print('world')
#得到helloworld
print('hello',end='*')
print('world')
#得到hello*world
格式化輸出
1、概念
将字元串中某些内容替換掉再輸出就是格式化輸出
2、使用
占位符%s:表示可以接受任意類型的值
占位符%d:隻能接受數字
使用方式為:先使用占位符占位,在使用%按照位置一一替換
參考一
name = input("name:")
age = input("age:")
sex = input("sex:")
job = input("job:")
msg = """
name : %s
age : %s
sex : %s
job : %s
"""
print(msg% (name, age, sex, job))
參考二
res = '親愛的%s你好!你%s月的話費是%s,餘額是%s'
print(res % ('tom', 11, 100, 0))
print(res % ('jon', 11, 300, 20))
print(res % ('jake', 11, 500, -20))
基本運算符
算術運算符
比較運算符
指派運算符
1、增量運算符
2、鍊式指派
x=10
y=10
z=10
x=y=z=10
3、交叉指派
m = 1
n = 2
#m和n的值做交換
#方法一:
x = m
m = n
n = x
print(m,n)
#得出m=2,n=1
#方法二:
m,n = n,m
print(m,n)
#得出m=2,n=1
4、解壓指派
muns = [111, 222, 333, 444, 555]
# 正常操作
a = muns[0]
b = muns[1]
c = muns[2]
d = muns[3]
e = muns[4]
# 解壓指派
a, b, c, d, e, = muns[0][1][2][3][4]
# 進階
a, *_ = muns
a, *_, e = muns
注:當元素比較多,而我們隻想取一兩個值時,可以用 *_ 比對
字元串、字典、清單、元組、集合類型都支援解壓指派
邏輯運算符
注:當三個邏輯運算符混合使用時,是有優先級的。三者的關系是not>and>or,同一級預設從左往右計算。但是我們在編寫的時候最好人為的規定好優先級
連續多個and
2 > 1 and 1 != 1 and True and 3 > 2 # 判斷完第二個條件,就立即結束,得的最終結果為False
False
連續多個or
2 > 1 or 1 != 1 or True or 3 > 2 # 判斷完第一個條件,就立即結束,得的最終結果為True
True
三個混合使用
3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3
False
(3>4 and 4>3) or (1==3 and 'x' == 'x') or 3 >3
False
成員運算
注意:雖然下面兩種判斷可以達到相同效果,但是推薦使用第二種,因為not in語義更加明确
not 'lili' in ['jack','tom','robin']
True
'lili' not in ['jack','tom','robin']
True
身份運算
判斷兩個資料,值和記憶體位址是否相等。其中符号==隻判斷值,is判斷記憶體位址
注意:值相等記憶體位址不一定相等,記憶體位址相等值一定相等