1. 程式人生 > >python-->基本數據類型

python-->基本數據類型

dex alt -a capital 進行 元組(tuple) 例如 cells wap

Python Number(數字)

數字數據類型用於存儲數值,它們是不可變數據類型。這意味著,更改數字數據類型的值會導致新分配對象。當為數字數據類型分配值時,Python將創建數字對象

Python 數字類型

  • int(有符號整數) - 它們通常被稱為整數或整數。它們是沒有小數點的正或負整數。
  • float(浮點實數值) - 也稱為浮點數,它們表示實數,並用小數點寫整數和小數部分。 浮點數也可以是科學符號,Ee表示10的冪 -
  • complex(復數) - 復數是以a + bJ的形式,其中ab是浮點,J(j)表示-1的平方根(虛數)。數字的實部是a,虛部是b。復數在Python編程中並沒有太多用處

Python 數字類型轉換

Python可將包含混合類型的表達式內部的數字轉換成用於評估求值的常用類型。 有時需要從一個類型到另一個類型執行明確數字轉換,以滿足運算符或函數參數的要求。

  • int(x)x轉換為純整數。
  • float(x)x轉換為浮點數。
  • complex(x, y)xy轉換為具有實部為x和虛部為y的復數。xy是數字表達式
技術分享圖片
print(type(int(1.6)))
print(type(int(6)))
print(type(float(1)))
print(type(float(1.6)))
---------------------------------------------
<class
int> <class int> <class float> <class float>
數字類型轉換

Python 數字進制轉換

2進制

8進制

10進制

16進制

2進制

-

bin(int(x, 8))

bin(int(x, 10))

bin(int(x, 16))

8進制

oct(int(x, 2))

-

oct(int(x, 10))

oct(int(x, 16))

10進制

int(x, 2)

int(x, 8)

-

int(x, 16)

16進制

hex(int(x, 2))

hex(int(x, 8))

hex(int(x, 10))

-

Python 字符串

字符串是 Python 中最常用的數據類型。我們可以使用引號( ‘ 或 " )來創建字符串。

Ps:python中字符串是不可變對象,所以所有修改和生成字符串的操作的實現方法都是另一個內存片段中新生成一個字符串對象

大小寫轉換(lowerupper

>>> print(‘ab XY‘.lower())
ab xy
>>> print(‘ab XY‘.upper())
AB XY

titlecapitalize

ps:前者返回S字符串中所有單詞首字母大寫且其他字母小寫的格式,後者返回首字母大寫、其他字母全部小寫的新字符串

>>> print(‘ab XY‘.title())
Ab Xy
>>> print(‘abc DE‘.capitalize())
Abc de

swapcase

ps:swapcase()S中的所有字符串做大小寫轉換(大寫-->小寫,小寫-->大寫)

>>> print(‘abc XYZ‘.swapcase())
ABC xyz

isalpha,isdecimal,isdigit,isnumeric,isalnum

ps:測試字符串S是否是數字、字母、字母或數字。對於非Unicode字符串,前3個方法是等價的

>>> print(‘34‘.isdigit())
True
>>> print(‘abc‘.isalpha())
True
>>> print(‘a34‘.isalnum())
True

islower,isupper,istitle

ps:判斷是否小寫、大寫、首字母大寫。要求S中至少要包含一個字符串字符,否則直接返回False。例如不能是純數字。istitle()判斷時會對每個單詞的首字母邊界判斷

>>> print(‘a34‘.islower())
True
>>> print(‘AB‘.isupper())
True
>>> print(‘Aa‘.isupper())
False
>>> print(‘Aa Bc‘.istitle())
True
>>> print(‘Aa_Bc‘.istitle())
True
>>> print(‘Aa bc‘.istitle())
False
>>> print(‘Aa_bc‘.istitle())
False
# 下面的返回False,因為非首字母C不是小寫
>>> print(‘Aa BC‘.istitle())
False

isspace,isprintable,isidentifier

ps:分別判斷字符串是否是空白(空格、制表符、換行符等)字符、是否是可打印字符(例如制表符、換行符就不是可打印字符,但空格是)、是否滿足標識符定義規則

>>> print(‘ ‘.isspace())
True
>>> print(‘ \t‘.isspace())
True
>>> print(‘\n‘.isspace())
True
>>> print(‘‘.isspace())
False
>>> print(‘Aa BC‘.isspace())
False

isprintable

ps:判斷是否是可打印字符

>>> print(‘\n‘.isprintable())
False
>>> print(‘\t‘.isprintable())
False
>>> print(‘acd‘.isprintable())
True
>>> print(‘ ‘.isprintable())
True
>>> print(‘‘.isprintable())
True

isidentifier

ps:判斷是否滿足標識符定義規則。標識符定義規則為:只能是字母或下劃線開頭、不能包含除數字、字母和下劃線以外的任意字符

>>> print(‘abc‘.isidentifier())
True
>>> print(‘2abc‘.isidentifier())
False
>>> print(‘abc2‘.isidentifier())
True
>>> print(‘_abc2‘.isidentifier())
True
>>> print(‘_abc_2‘.isidentifier())
True
>>> print(‘_Abc_2‘.isidentifier())
True
>>> print(‘Abc_2‘.isidentifier())
True

center

ps:S.center(width[, fillchar])將字符串居中,左右兩邊使用fillchar進行填充,使得整個字符串的長度為widthfillchar默認為空格。如果width小於字符串的長度,則無法填充直接返回字符串本身(不會創建新字符串對象)

>>> print(‘ab‘.center(4,‘_‘))
_ab_
>>> print(‘ab‘.center(5,‘_‘))
__ab_

*使用默認的空格填充並居中字符串
>>> print(‘ab‘.center(4))
 ab 
>>> print(len(‘ab‘.center(4)))
4
*width小於字符串長度
>>> print(‘abcde‘.center(3))
abcde

ljustrjust

ps:ljust()使用fillchar填充在字符串S的右邊,使得整體長度為widthrjust()則是填充在左邊。如果不指定fillchar,則默認使用空格填充,如果width小於或等於字符串S的長度,則無法填充,直接返回字符串S(不會創建新字符串對象)

>>> print(‘xyz‘.ljust(5,‘_‘))
xyz__
>>> print(‘xyz‘.rjust(5,‘_‘))
__xyz

zfill

ps:0填充在字符串S的左邊使其長度為width。如果S前右正負號+/-,則0填充在這兩個符號的後面,且符號也算入長度。如果width小於或等於S的長度,則無法填充,直接返回S本身(不會創建新字符串對象)

技術分享圖片
>>> print(abc.zfill(5))
00abc
 
>>> print(-abc.zfill(5))
-0abc
 
>>> print(+abc.zfill(5))
+0abc
 
>>> print(42.zfill(5))
00042
 
>>> print(-42.zfill(5))
-0042
 
>>> print(+42.zfill(5))
+0042
View Code

count

ps:

S.count(sub[, start[, end]])返回字符串S中子串sub出現的次數,可以指定從哪裏開始計算(start)以及計算到哪裏結束(end),索引從0開始計算,不包括end邊界。

技術分享圖片
>>> print(xyabxyxy.count(xy))
3
 
# 次數2,因為從index=1算起,即從‘y‘開始查找,查找的範圍為‘yabxyxy‘
>>> print(xyabxyxy.count(xy,1))
2
 
# 次數1,因為不包括end,所以查找的範圍為‘yabxyx‘
>>> print(xyabxyxy.count(xy,1,7))
1
 
# 次數2,因為查找的範圍為‘yabxyxy‘
>>> print(xyabxyxy.count(xy,1,8))
2
View Code

endswithstartswith

ps:

S.endswith(suffix[, start[, end]])與S.startswith(prefix[, start[, end]]),endswith()檢查字符串S是否已suffix結尾,返回布爾值的TrueFalsesuffix可以是一個元組(tuple)。可以指定起始start和結尾end的搜索邊界。同理startswith()用來判斷字符串S是否是以prefix開頭。

技術分享圖片
*suffix是普通的字符串時
>>> print(abcxyz.endswith(xyz))
True
 
# False,因為搜索範圍為‘yz‘
>>> print(abcxyz.endswith(xyz,4))
False
 
# False,因為搜索範圍為‘abcxy‘
>>> print(abcxyz.endswith(xyz,0,5))
False
>>> print(abcxyz.endswith(xyz,0,6))
True

*suffix是元組(tuple)時,只要tuple中任意一個元素滿足endswith的條件,就返回True。
>>> print(abcxyz.endswith((ab,xyz)))
True
 
# tuple中‘ab‘和‘xy‘都不滿足條件
>>> print(abcxyz.endswith((ab,xy)))
False
 
# tuple中的‘z‘滿足條件
>>> print(abcxyz.endswith((ab,xy,z)))
True
View Code

find,rfindindex,rindex

ps:S.find(sub[, start[, end]])、S.rfind(sub[, start[, end]])?、S.index(sub[, start[, end]])、S.rindex(sub[, start[, end]]),find()搜索字符串S中是否包含子串sub,如果包含,則返回sub的索引位置,否則返回"-1"。可以指定起始start和結束end的搜索位置。index()find()一樣,唯一不同點在於當找不到子串時,拋出ValueError錯誤。rfind()則是返回搜索到的最右邊子串的位置,如果只搜索到一個或沒有搜索到子串,則和find()是等價的。同理rindex()

技術分享圖片
>>> print(abcxyzXY.find(xy))
3
>>> print(abcxyzXY.find(Xy))
-1
>>> print(abcxyzXY.find(xy,4))
-1
 
>>> print(xyzabcabc.find(bc))
4
>>> print(xyzabcabc.rfind(bc))
7
View Code

in

ps:使用in操作符來判斷字符串S是否包含子串sub,它返回的不是索引位置,而是布爾值

技術分享圖片
>>> xy in abxycd
True
>>> xyz in abxycd
False
View Code

replace

ps:S.replace(old, new[, count]),將字符串中的子串old替換為new字符串,如果給定count,則表示只替換前countold子串。如果S中搜索不到子串old,則無法替換,直接返回字符串S(不創建新字符串對象)

技術分享圖片
>>> print(abcxyzoxy.replace(xy,XY))
abcXYzoXY
>>> print(abcxyzoxy.replace(xy,XY,1))
abcXYzoxy
>>> print(abcxyzoxy.replace(mn,XY,1))
abcxyzoxy
View Code

splitrsplitsplitlines

ps:S.split(sep=None, maxsplit=-1)、S.rsplit(sep=None, maxsplit=-1)、S.splitlines([keepends=True]),都是用來分割字符串,並生成一個列表。split()根據sepS進行分割,maxsplit用於指定分割次數,如果不指定maxsplit或者給定值為"-1",則會從做向右搜索並且每遇到sep一次就分割直到搜索完字符串。如果不指定sep或者指定為None,則改變分割算法:以空格為分隔符,且將連續的空白壓縮為一個空格。rsplit()split()是一樣的,只不過是從右邊向左邊搜索。splitlines()用來專門用來分割換行符。雖然它有點像split(‘\n‘)split(‘\r\n‘),但它們有些區別,見下文解釋。

技術分享圖片
>>> 1,2,3.split(,)
[1, 2, 3]
 
>>> 1,2,3.split(,,1)
[1, 2,3]  # 只分割了一次
 
>>> 1,2,,3.split(,)
[1, 2, ‘‘, 3] # 不會壓縮連續的分隔符
 
>>> <hello><><world>.split(<)
[‘‘, hello>, >, world>]
 
# sep為多個字符時
>>> <hello><><world>.split(<>)
[<hello>, <world>]
 
# 不指定sep時
>>> 1 2 3.split()
[1, 2, 3]
 
>>> 1 2 3.split(maxsplit=1)
[1, 2 3]
 
>>>   1  2  3  .split()
[1, 2, 3]
 
>>>   1  2  3 \n.split()
[1, 2, 3]
 
# 顯式指定sep為空格、制表符、換行符時
>>>  1 2 3 \n.split( )
[‘‘, 1, ‘‘, 2, ‘‘, 3, ‘‘, \n]
 
>>>  1 2 3 \n.split(\t)
[ 1 2 3 \n]
 
>>>  1 2\n3 \n.split(\n)
[ 1 2, 3 , ‘‘] # 註意列表的最後一項‘‘
 
>>> ‘‘.split(\n)
[‘‘]
View Code

splitlines

ps:splitlines()中可以指定各種換行符,常見的是\n\r\r\n。如果指定keependsTrue,則保留所有的換行符

技術分享圖片
>>> ab c\n\nde fg\rkl\r\n.splitlines()
[ab c, ‘‘, de fg, kl]
 
>>> ab c\n\nde fg\rkl\r\n.splitlines(keepends=True)
[ab c\n, \n, de fg\r, kl\r\n]

*將split()和splitlines()相比較一下
>> ‘‘.split(\n)
[‘‘]      # 因為沒換行符可分割
 
>>> One line\n.split(\n)
[One line, ‘‘]
 
#### splitlines()
>>> "".splitlines()
[]       # 因為沒有換行符可分割
 
>>> Two lines\n.splitlines()
[Two lines]
View Code

join

ps:S.join(iterable)將可叠代對象(iterable)中的字符串使用S連接起來。註意,iterable中必須全部是字符串類型,否則報錯。

技術分享圖片
>>> L=python
>>> _.join(L)
p_y_t_h_o_n
View Code

python-->基本數據類型