日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python基础学习篇-2-数值运算和字符串

發布時間:2025/3/17 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python基础学习篇-2-数值运算和字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、數值運算符

1、運算符

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

2、int 和 float

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

3、/ 、 //、 %

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

>>> 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、 = 賦值

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

>>> 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、整數和浮點數的混合計算中,整數會被轉換為浮點數:

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

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

除了 int 和 float,Python 還支持其它數字類型,例如 Decimal 和 Fraction。Python 還內建支持 復數,使用后綴 j 或 J 表示虛數部分(例如,3+5j)。


二、字符串
1、引用方式:單引號或雙引號

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

>>> '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.'
在交互式解釋器中,輸出的字符串會用引號引起來,特殊字符會用反斜杠轉義。雖然可能和輸入看上去不太一樣,但是兩個字符串是相等的。

2、r---原始字符串

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

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

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

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、字符串可以由 + 操作符連接(粘到一起),可以由 * 表示重復:

>>> # 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'
索引也可以是負數,這將導致從右邊開始計算。例如:

>>> word[-1]? # last character
'n'
>>> word[-2]? # second-last character
'o'
>>> word[-6]
'P'
請注意 -0 實際上就是 0,所以它不會導致從右邊開始計算。



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:] 永遠等于 s:

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

>>> 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
文本中的第一行數字給出字符串中的索引點 0…6。第二行給出相應的負索引。切片是從 i 到 j 兩個數值標示的邊界之間的所有字符。

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

試圖使用太大的索引會導致錯誤:

>>> 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 能夠優雅地處理那些沒有意義的切片索引:一個過大的索引值(即下標值大于字符串實際長度)將被字符串實際長度所代替,當上邊界比下邊界大時(即切片左值大于右值)就返回空字符串:

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

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

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

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


轉載于:https://blog.51cto.com/xqtesting/2359816

總結

以上是生活随笔為你收集整理的Python基础学习篇-2-数值运算和字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。