Python的數據類型

1. python的數據類型
數值
字符串
列表
元組
字典
2.數值類型
整型
長整形
浮點型
a=1.0
b=5.0
c=2.545
round(a) 輸出的是浮點型,四捨五入,最後一個小數點是偶數。
round(c,2)保留2位小數點,在四捨五入,最後一位是偶數
a = 2.4440
b = 5.1230
c = 10
print (round(c))
print(a/b)
print (round((a/b),2))



複數型complex
python對複數提供內嵌支持,這是其他大部分軟件所沒有的
-3.14j 8.32e-36j
>>num = 3.14j
>>type(num)
<type 'complex'>

整數
a = 456
type(a) = int


a.字符串
定義字符串三種方法:
str = ‘this is a string’
str = “this is a string”
str = ‘“this is a string”’
三重引號除了能定義字符串還能做註釋

-字符串是個序列
a='abcde'

0是第一個取值,2是第二個。不包括c
0可以省略a[:2]
a[1:] "bcde"
a[-1] 是從後面取

a[: : ] 後面是step值如圖:


默認是從左到右 也可以從右到左
-1 表示反方向,從右到左。如圖


b.字符串常用的操作方法
我們可以通過函數dir() 來查看字符串的操作
a = "12478python"
print (dir(a))

常用的有:
find 查找字符串,如果找到返回字符串首字母匹配的下標信息,如果找不到返回-1
a = "12478python"
print(a.find("python")) 
>>5
a = "12478python"
print(a.find("java"))
>> -1

replace 替換字符串
a = "12478python"
print(a.replace("python","java"))
>>12478java


split 拆分字符串。通過指定分隔符對字符串進行切片,並返回分割後的字符串列表
a = "www.vfx.ttd.com"
print(a.split('.'))
>>['www', 'vfx', 'ttd', 'com']
分割次數3次
a = "www.vfx.ttd.com.ffd"

print(a.split('.',3))
>>['www', 'vfx', 'ttd', 'com.ffd']



strip 當左邊有空格右邊有空格,我們想去掉空格就用這個方法
s ="   kk henrenzhen"
print (s.strip())
經常使用lstrip去掉左邊的空格,rstrip去掉右邊空格

format
%s 代表的是字符串,%d代表是整形
name = "zhoukaivfx"
print ("hello" + name)
print ("hello %s") %name
print ('hello {0}').format(name)
輸出都一樣的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章