溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python數(shù)值運算和字符串有哪些

發(fā)布時間:2021-11-20 15:45:52 來源:億速云 閱讀:106 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Python數(shù)值運算和字符串有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python數(shù)值運算和字符串有哪些”吧!

1、運算符

 +,-,* 和 / 與其它語言一樣,括號 (()) 用于分組

2、int 和 float

整數(shù)(例如,2, 4, 20 )的類型是 int,帶有小數(shù)部分的數(shù)字(例如,5.0, 1.6)的類型是 float。

3、/ 、 //、 %

除法(/)永遠(yuǎn)返回一個浮點數(shù)。如要只返回整數(shù)結(jié)果(丟掉任何小數(shù)部分),可以使用 // 運算符(floor division);要計算余數(shù)可以使用 %

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2  # result * divisor + remainder
17
4、** 冪乘方

還可以使用 ** 運算符計算冪乘方 [1]:

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
5、 = 賦值

等號( '=' )用于給變量賦值。賦值之后,在下一個提示符之前不會有任何結(jié)果顯示:

>>> width = 20
>>> height = 5*9
>>> width * height
900
變量在使用前必須 “定義”(賦值),否則會出錯:

>>> # try to access an undefined variable
... n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
6、整數(shù)和浮點數(shù)的混合計算中,整數(shù)會被轉(zhuǎn)換為浮點數(shù):

>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5
7、交互模式中,最近一個表達式的值會賦給變量 _。這樣我們就可以把它當(dāng)作一個桌面計算器,很方便的用于連續(xù)計算,例如:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
此變量對于用戶是只讀的。不要嘗試給它賦值 —— 你只會創(chuàng)建一個獨立的同名局部變量,它屏蔽了系統(tǒng)內(nèi)置變量的魔術(shù)效果。

除了 int 和 float,Python 還支持其它數(shù)字類型,例如 Decimal 和 Fraction。Python 還內(nèi)建支持 復(fù)數(shù),使用后綴 j 或 J 表示虛數(shù)部分(例如,3+5j)。二、字符串
1、引用方式:單引號或雙引號

python中可以用單引號 ('...') 或雙引號 ("...") 標(biāo)識 字符串。 可以用來轉(zhuǎn)義引號:

>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn't'  # use ' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> ""Yes," he said."
'"Yes," he said.'
>>> '"Isn't," she said.'
'"Isn't," she said.'
在交互式解釋器中,輸出的字符串會用引號引起來,特殊字符會用反斜杠轉(zhuǎn)義。雖然可能和輸入看上去不太一樣,但是兩個字符串是相等的。

2、r---原始字符串

如果你前面帶有 的字符被當(dāng)作特殊字符,你可以使用 原始字符串,方法是在第一個引號前面加上一個 r:

>>> print('C:somename')  # here n means newline!
C:some
ame
>>> print(r'C:somename')  # note the r before the quote
C:somename
3、字符串文本分成多行顯示。

一種方法是使用三引號:"""...""" 或者 '''...'''。行尾換行符會被自動包含到字符串中,但是可以在行尾加上 來避免這個行為。下面的示例: 可以使用反斜杠為行結(jié)尾的連續(xù)字符串,它表示下一行在邏輯上是本行的后續(xù)內(nèi)容:

print("""
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
將生成以下輸出(注意,沒有開始的第一行):

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
4、字符串可以由 + 操作符連接(粘到一起),可以由 * 表示重復(fù):

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
5、相鄰的兩個字符串文本自動連接在一起。:

>>> 'Py' 'thon'
'Python'
它只用于兩個字符串文本,不能用于字符串表達式:

>>> prefix = 'Py'
>>> prefix 'thon'  # can't concatenate a variable and a string literal
  ...
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
  ...
SyntaxError: invalid syntax
如果你想連接多個變量或者連接一個變量和一個字符串文本,使用 +:

>>> prefix + 'thon'
'Python'
這個功能在你想切分很長的字符串的時候特別有用:

>>> text = ('Put several strings within parentheses '
            'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'6、字符串也可以被截取(檢索)。類似于 C ,字符串的第一個字符索引為 0 。

Python沒有單獨的字符類型;一個字符就是一個簡單的長度為1的字符串。:

>>> word = 'Python'
>>> word[0]  # character in position 0
'P'
>>> word[5]  # character in position 5
'n'
索引也可以是負(fù)數(shù),這將導(dǎo)致從右邊開始計算。例如:

>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'
請注意 -0 實際上就是 0,所以它不會導(dǎo)致從右邊開始計算。
7、除了索引,還支持 切片。索引用于獲得單個字符,切片 讓你獲得一個子字符串:

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'
注意,包含起始的字符,不包含末尾的字符。這使得 s[:i] + s[i:] 永遠(yuǎn)等于 s:

>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
切片的索引有非常有用的默認(rèn)值;省略的第一個索引默認(rèn)為零,省略的第二個索引默認(rèn)為切片的字符串的大小。:

>>> word[:2]  # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]  # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
有個辦法可以很容易地記住切片的工作方式:切片時的索引是在兩個字符 之間 。左邊第一個字符的索引為 0,而長度為 n 的字符串其最后一個字符的右界索引為 n。例如:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
文本中的第一行數(shù)字給出字符串中的索引點 0…6。第二行給出相應(yīng)的負(fù)索引。切片是從 i 到 j 兩個數(shù)值標(biāo)示的邊界之間的所有字符。

對于非負(fù)索引,如果上下都在邊界內(nèi),切片長度就是兩個索引之差。例如,word[1:3] 是 2 。

試圖使用太大的索引會導(dǎo)致錯誤:

>>> word[42]  # the word only has 6 characters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
Python 能夠優(yōu)雅地處理那些沒有意義的切片索引:一個過大的索引值(即下標(biāo)值大于字符串實際長度)將被字符串實際長度所代替,當(dāng)上邊界比下邊界大時(即切片左值大于右值)就返回空字符串:

>>> word[4:42]
'on'
>>> word[42:]
''
Python字符串不可以被更改 — 它們是 不可變的 。因此,賦值給字符串索引的位置會導(dǎo)致錯誤:

>>> word[0] = 'J'
  ...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
  ...
TypeError: 'str' object does not support item assignment
如果你需要一個不同的字符串,你應(yīng)該創(chuàng)建一個新的:

>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'
內(nèi)置函數(shù) len() 返回字符串長度:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)

到此,相信大家對“Python數(shù)值運算和字符串有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI