PYTHON學習-運算符&數據類型&I/O
注:該類博文爲博主在學習過程中總結的筆記。
1.print()輸出函數
在控制檯輸出一段文本信息(用一對雙引號標記)
1.1 正常輸出
print(“文本信息”)默認換行
>>>print("Hello World!")
Hello World!
>>>
print("Hello",end="\n")
# \n換行符 \t製表符
print("World!")
Hello
World!
Python在輸出一段數據時,默認結尾加換行符"\n",同時也可加end指定結尾符號
1.2 連續輸出
print(“文本信息1”,“文本信息2”,…)
前者與後者之間有一個空格 默認換行
>>> print("H","e","l","l","o","W","o","r","l","d","!")
H e l l o W o r l d !
>>>
1.3 拼接型輸出
print(“文本信息1” “文本信息2”)
在此處不爲加法,而是用於字符串的拼接,爲連接符
>>> print("Hello" "World!")
HelloWorld!
>>>
1.4 重複輸出
print(“文本信息”*n)
該文本信息重複n次
>>> print("haha"*3)
hahahahahaha
>>> print("haha\n"*3)
haha
haha
haha
>>>
1.5 輸出數字
首先,0x->16進制整數,0b->2進制整數,0o->8進制整數
注:該0b、0o、0x只標識之後的數,python輸出的都爲十進制數
>>> print(10) #十進制數
10
>>> print(0b10010) #二進制數
18
>>> print(0o225) #8進制數
149
>>> print(0x95) #16進制數
149
2.type()查看數據類型
type(數據):查看該數據的類型
>>> type("1") #字符串類型
<class 'str'>
>>> type(1) #整型
<class 'int'>
>>> type(True) #布爾類型
<class 'bool'>
>>> type(3.14) #浮點類型
<class 'float'>
>>>
3.算數運算符
- * / // ** %
3.1 /加法運算
在加法運算中:
1.當 兩邊都爲字符串時,則 爲連接符
2.當 兩邊一邊是字符串,另一邊爲非字符串時 報錯,因爲字符串不能和其他非字符串數據相加
>>> print(3 4)
7
>>> print(1 3.14)
4.140000000000001
>>> print("xixi" "hehe")
xixihehe
>>> print("1" 1)
Traceback (most recent call last):
File "<pyshell#82>", line 1, in <module>
print("1" 1)
TypeError: can only concatenate str (not "int") to str
>>>
3.2 -/加法運算
在減法運算中,字符串不能進行減法運算
>>> print(3-1)
2
>>> print("xixi"-"xi")
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
print("xixi"-"xi")
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> print(3.14-1)
2.14
>>>
3.3 */乘法運算
在乘法運算中:
1.*號兩邊不能都爲字符串
2.*號可以進行字符串的重複打印操作
3.當在乘法運算中出現bool值時,任何數(包括字符串)乘以True等於它本身
4.任何數乘以False都等於0,當字符串乘以False時爲空
>>> print(3*4)
12
>>> print("abc"*3)
abcabcabc
>>> print(3*True)
3
>>> print(3*False)
0
>>> print(0*True)
0
>>> print(-1*True)
-1
>>> print(3.14*False)
0.0
>>> print(True*"True")
True
>>> print(False*"True")
>>>
3.4 /除法運算
在除法運算中,是數學除法
注:在 -*/
運算中bool值,也能進行數學運算,True=1,Flase=0
>>> print(4/2)
2.0
>>> print(4/3)
1.3333333333333333
>>> print(False/2)
0.0
>>> print(True/2)
0.5
>>>
//整除運算
>>> print(10//3)
3
>>>
4.4 %求餘運算
求餘運算-模運算,在有規律,有周期性的問題中常用
例1:
1月1日爲週一,問:1月18號爲周幾?
1%7=1 -> 1-週一 2-週二...6-週六 0-周天
18%7=4
則1月18日爲週四
例2:
在五子棋中,黑棋先行,則0代表黑棋,1代表白棋
n%2 0 1 n=1 2 3 ...
4.6 **冪運算
>>> print(2**4)
16
>>> print(9**0.5)
3.0
>>> print(3**-2)
0.1111111111111111
>>> print(16**0.25)
2.0
>>>
4.數據類型
4.1 int()整型函數
int()函數可以進行取整操作
注:int()不能進行四捨五入操作,只能進行取整操作,四捨五入可用round()函數。
>>> int(3.14)
3
>>>
>>>> int(4.7)
4
>>> round(4.7)
5
>>>
int()函數是將()內的數字字符串轉爲數字
- int("",2)是指""內的數字字符串爲2進制格式
- int("",8)是指""內的數字字符串爲8進制格式
- int("",n)是指""內的數字字符串爲n進制格式
注:int類型只支持2~36進制,且輸入的數字字符串必須和進制相匹配
>>> int("123")
123
>>> type(int("123"))
<class 'int'>
>>> int("123") 123
246
>>> int("10010101",2)
149
>>> int("10010101",10)
10010101
>>> int("2341",8)
1249
>>> int("abcdefghijklmnopqrstuvwxyz",36)
8337503854730415241050377135811259267835
>>> int("99",7)
Traceback (most recent call last):
File "<pyshell#146>", line 1, in <module>
int("99",7)
ValueError: invalid literal for int() with base 7: '99'
>>>
>>>
4.2 str()字符串類型函數
str():將一切的數據轉化爲字符串
>>> print("123" str(123))
123123
>>> print(int("123") 123)
246
4.3 float()浮點類型函數
將數字字符串轉換爲浮點類型數字
>>> float(1)
1.0
>>> float(1.231231231231231232134512352135213412)
1.2312312312312312
>>> type(float("123213.213123"))
<class 'float'>
>>>
4.4 bool()布爾類型函數
bool類型的值只有兩個,True&&False
bool(n)n不是0或None結果都爲True
>>> bool(1)
True
>>> bool(0)
False
>>> bool(-1)
True
>>> bool(None)
False
>>> bool(0.1)
True
>>>
5.input()輸入函數
input()輸入函數,它輸入的一切皆爲字符串
>>> input()
abc
'abc'
>>>
我們在編程中通常要輸入各種各樣的數據,而且很少爲字符串,所以此時需要進行函數嵌套的形式來達到輸入數據類型的可控制性
最常見的爲eval()函數
a=int(input("請輸入一個數據:"))
請輸入一個數據:2
>>>
>>>> a
2
>>> a = eval(input("請輸入一個數:"))
請輸入一個數:123
>>> a
123
>>>
>>>> int("04")
4
>>> eval("04")
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
eval("04")
File "<string>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
>>>
6.編程練習題
6.1 (計算表達式)編寫程序顯示下面表達式的結果
a=9.5
b=4.5
c=2.5
d=3
e=45.5
f=3.5
print((a*b-c*d)/(e-f))
6.2 (近似π)可以使用下面的公式計算
a=4
b=1
c=3
d=c 2
e=c 4
f=c 6
g=c 8
h=c 10
j=c 12
print("第一個算式的結果爲:",(a*(b-c**-1 d**-1-e**-1 f**-1-g**-1)))
print("第二個算式的結果爲:",(a*(b-c**-1 d**-1-e**-1 f**-1-g**-1 h**-1-g**-1)))
6.3(圓的面積和周長)使用下面的公式編寫程序,顯示半徑是5.5的圓的面積和周長
area = radius × radius × π
perimeter = 2 × radius × π
π=3.14
radius=float(input("請輸入圓的半徑:"))
area = radius*radius*π
perimeter=2*radius*π
print("則該圓的面積爲:",area)
print("則該圓的周長爲:",perimeter)
6.4(將設置溫度轉化爲華氏溫度)編寫一個從控制檯讀取攝氏溫度並將它轉變爲華氏溫度並予以顯示的程序,轉換公式如下
fahrenheit = (9 / 5) * celsius 32
示例:
celsius=int(input("Enter a degree in Celsius:"))
fahrenheit = (9 / 5) * celsius 32
print(celsius,"Celsius is",fahrenheit,"Fahrenheit")
6.5(計算圓柱體的體積)編寫一個讀取圓柱的半徑和高並利用下面的公式計算圓柱體底面積和體積的程序:
area = radius * radius * π
volume = area * length
示例:
π=3.14
radius,length=eval(input("Enter the radius and length of a cylinder:"))
area = radius * radius * π
volume = area * length
print("The area is",area)
print("The volume is",volume)
Enter the radius and length of a cylinder:2,3
The area is 12.56
The volume is 37.68
>>>
6.7(計算年數和天數)編寫一個程序,提示用戶輸入分鐘數(例如:1000000),然後將分鐘轉換爲年數和天數並顯示的程序。爲了簡單起見,假定一年有365天
示例:
minutes = eval(input("Enter the number of minutes: "))
years = int(minutes/525600)
days = int(((minutes%525600)/60)/24)
print(minutes,"minutes is approximately",years,"years and",days,"days")
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days
>>>
6.8(分割數字)編寫一個程序,提示用戶輸入四位整數並以反向順序顯示,這裏是一個示例運行。
示例:
a = int(input("Enter an integer:"))
b = a%10
c = ((a//10)%10)
d = ((a//100)%10)
e = ((a//1000)%10)
print(b)
print(c)
print(d)
print(e)
Enter an integer:3156
6
5
1
3
>>>