整型,浮點型以及字符串數據類型


前言:

在python中,數據類型有:整型,浮點型,字符串,列表,元組,字典,集合,布爾型。今天要介紹的是整型,浮點型以及字符串類型。

 

一、整型:

整數類型即是整型,哈哈哈,這不是廢話嗎?

整型 -1, -1, 0, 2, 99

 

 

a = 10
b = -10
c = 0

print(a, b, c)
print(type(a))
print(type(b))
print(type(c))

執行結果:

10 -10 0
<class 'int'>
<class 'int'>
<class 'int'>

Process finished with exit code 0

我們可以看到,10, -10, 0 都是整型,都是 int 類型。

 

二、浮點型:

有小數的數據類型,稱為浮點型。

浮點型 -1.25, 0.0, 1.2, 3.90 

 

 

a = 10.1
b = -10.0
c = 0.0

print(a, b, c)
print(type(a))
print(type(b))
print(type(c))

執行結果:

10.1 -10.0 0.0
<class 'float'>
<class 'float'>
<class 'float'>

Process finished with exit code 0

我們可以看到,10.1 ,0.0, -10.0 這些帶小數點的,都屬於 float 型。

 

三、字符串類型:

在python中,字符串類型由引號括起來。

字符串類型 'hello','python', 'java'

 

 

a = 'hello'
b = 'j'
c = 'world '

print(a, b, c)
print(type(a))
print(type(b))
print(type(c))

執行結果:

hello j world 
<class 'str'>
<class 'str'>
<class 'str'>

Process finished with exit code 0

我們可以看到,只要是杯引號括起來的,都是 str 類型。

 

四、表達式:

表達式是由值與操作符的組合

 

五、字符串的連接與復制:

對於字符串, + 號運輸符,可以將兩個字符串進行拼接。

a = 'hello'
b = ' world '

c = a + b
print(c)

執行結果:

hello world 

我們可以看到 + 號對於數字來說,是數字運算,而對於字符串來說,是拼接。

b = ' world '
n = 3

print(b*n)

執行結果:

 world  world  world 

我們可以看到,字符串 * 整數 相當於復制了字符串整數次。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM