python基本输入与格式化_Python导学基础(三)输入、格式化输出、基本运算符
一:輸入
1、python3中的input
inp_username=input("請輸入您的密碼:") # "18"
print(inp_username)
print(type(inp_username))
age=input('your age: ') # age="18"
age=int(age) # 轉換數據類型 前提是:字符串中包含的必須是純數字
print(type(age))
print(age > 10) # "18" > 10
int('123123123asdf') # 報錯整型必須純數字
2、在python2中的Input
有一個input:要求程序的使用者必須輸入一個明確的數據類型(了解)
特點是:輸入什么類型,就會被直接存成什么類型,程序中無需轉換直接使用就好
在python2中raw_input與python3的input用法是一模一樣
二:格式化輸出
print('asdfjasdfasfasdf')
print("my name is %s my age is" %"egon") #
inp_name=input('請輸入您的名字:')
inp_age=input('請輸入您的年齡:')
print("my name is %s my age is %s" %(inp_name,inp_age))
# %s是可以接收任意類型的
print("my age is %s" %18)
print("my age is %s" %[1,2,3])
# 了解%d必須要求一個數字
print("my age is %d" %18)
# print("my age is %d" %"18") 錯誤
三:基本運算符
1、算數運算符
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # 保留小數部分
print(10 // 3) # 只保留整數部分
print(10 % 3) # 取余數,取模
print(10 ** 3)
2、比較運算符:
x=10
y=10
print(x == y) # =一個等號代表的是賦值
x=3
y=4
print(x != y) # 不等于
x=3
y=4
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
print(10 <= 10) # True
3、賦值運算符
age=18
age=age + 1 # 賦值運算
age+=1 # 賦值運算符,age=age+1
print(age)
age *= 10 # age=age*10
age = 10 # age=age10
age /= 10 # age=age/10
age //= 10 # age=age//10
age -= 10 # age=age-10
print(age)
?1、增量賦值
x = 10
x += 1
?2、交叉賦值
a = 10
b = 20
print(a,b)
temp=b # temp=20
b=a # b = 10
a=temp
a,b = b,a # python一行代碼搞定
print(a,b)
?3、鏈式賦值
a=7
b=a
c=b
d=c
a = b = c = d = 7
print(a,b,c,d)
?4、解壓賦值: 取開頭和結尾的幾個值
salaries=[33,44,55,66,77]
x=salaries[0]
y=salaries[1]
z=salaries[2]
a=salaries[3]
b=salaries[4]
# 左邊變量名的個數與右面包含值的個數相同,多一個不行,少一個也不行
x,y,z,a,b=salaries
print(x,y,z,a,b)
salaries=[33,44,55,66,77,88,99]
x,y,z,*abc=salaries
#x,y,z會對應列表salaries的前三個值,然后*會把剩余的值存放一個列表,然后賦值給abc
print(x,y,z)
print(abc)
# _當變量名,代表該變量值是無用的
x,y,z,*_=salaries # 取前三個值
print(x,y,z)
print(_)
salaries=[33,44,55,66,77,88,99]
*_,m,n=salaries # 取后兩個值
print(_)
print(m,n)
salaries=[33,44,55,66,77,88,99]
x,y,z,*_,m=salaries # 取后兩個值
print(x,y,z)
print(m)
d={'a':1,'b':2,'c':3}
x,y,z=d
print(x,y,z)
4、邏輯運算符
?and: 邏輯與,and是用來連接左右兩個條件,只有在左右兩個條件同時為True,最終結果才為True,但凡有一個為False,最終結果就為False
print(10 > 3 and True)
print(10 < 3 and True and 3 > 2 and 1==1)
?
?or: 邏輯或,or是用來連接左右兩個條件,但凡有一個條件為True,最終結果就為True,除非二者都為False,最終結果才為False。
print(True or 10 > 11 or 3 > 4)
print(False or 10 > 11 or 3 > 4)
print(False or 10 > 9 or 3 > 4)
# False or (True and True)
# False or True
res=(True and False) or (10 > 3 and (3 < 4 or 4==3))
print(res)
?
?not: 把緊跟其后那個條件運算的結果取反
print(not 10 > 3)
# False or (False and False)
# False or False
res=(True and False) or (not 10 > 3 and (not 3 < 4 or 4==3))
print(res)
①、優先級:not > and > or
1、not與緊跟其后的那個條件是不可分割的
2、如果條件語句全部由純and、或純or鏈接,按照從左到右的順序依次計算即可
print(True and 10 > 3 and not 4 < 3 and 1 == 1)
print(False or 10 < 3 or not 4 < 3 or 1 == 1)
3、對于既有and又有or鏈接的語句,以and為中心把左右兩個條件用括號括起來
res=(10 == 9 and 0 < 3) or ('' == 'egon' and 0> 3) or not True or ('egon' == 'dsb' and 333 > 100) or 10 > 4
print(res)
②、短路運算=>偷懶原則
?所有的數據類型的值都自帶布爾值,所以值可以直接被當成條件使用0、None、空三種值對應的布爾值為False,其余全為True
if 0:
print('ok')
else:
print('====>') # ====>
if 3 and []:
print('真')
else:
print('假') # 假
and運算會返回當前計算位置的值
res=0 and 123 # 0
res=111 and 123 # 123
print(res)
if 111 and 123:
print('ok') # ok
else:
print('no')
x=''
if x:
print('不為空')
else:
print("為空") #為空
print(1 or 0) # 1
print(0 and 2 or 1) # 1
print(0 and 2 or 1 or 4) # 1
5、身份運算符( 以后補充)
總結
以上是生活随笔為你收集整理的python基本输入与格式化_Python导学基础(三)输入、格式化输出、基本运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 承诺员工1人1套房!董明珠被传与王自如恋
- 下一篇: “辣”个男人回来了?特斯拉CEO马斯克已