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

歡迎訪問 生活随笔!

生活随笔

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

python

使用Python作为计算器

發布時間:2023/12/13 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Python作为计算器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數值

1.python支持基本的數學運算符,而且應用python你可以像寫數學公式那樣簡單明了。

eg:

>>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.6

2.除法運算符號‘/’常常返回的是float類型,而‘//’返回的是整形,求余數符號是‘%’

eg:

>>> 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 3.'**'符號表示求冪操作,‘=’用來賦值操作。

*變量不可以沒有賦值就使用,所以做好在聲明變量時就給賦值。


字符串

1.字符串常量可以使用單引號或者雙引號包含,不過習慣上常常使用雙引號,如果為了在字符串中出現單引號或者雙引號,可以使用反斜杠‘\’進行聲明。

eg:

>>> '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" 2.如果你不想以\字符被解釋為特殊字符,你可以通過在第一次報價,添加一個R使用原始字符串

eg:

>>> print('C:\some\name') # here \n means newline! C:\some ame >>> print(r'C:\some\name') # note the r before the quote C:\some\name 3.字符串可以跨多個行。一種方式是使用三引號:“”“…”“”或“”“…”。字符串的結尾會自動包含在字符串中,但是可以通過在行的結尾加上\\來防止這一點。

eg:

print("""\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """) 4.字符串可以通過‘+’來進行連接和通過‘*’進行重復。

eg:

>>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium'

5.兩個或兩個以上的字符串(即那些包含在引號)下可以彼此自動連接。

注:幾年只有被引號所包含的字符串常量才支持這樣的操作,字符串變量和字符串表達式都不支持。

eg:

>>> '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 6.字符串支持下標訪問和區域訪問。

eg:

>>> word = 'Python' >>> word[0] # character in position 0 'P' >>> word[5] # character in position 5 'n' >>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho' >>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python' 7.字符串不支持修改和越界訪問。

eg:

>>> 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 >>> word[0] = 'J' ... TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' ... TypeError: 'str' object does not support item assignment *如果想修改,可以通過定義新的變量。

8.len()函數可以返回字符串的長度。

eg:

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

列表

1.在python中,列表是最常用的類型之一。列表中可以包含相同類型的數據,也可以包含不同類型的數據。

eg:

>>> squares = [1, 4, 9, 16, 25] >>> squares [1, 4, 9, 16, 25] 2.列表和字符串一樣支持下標訪問和區域訪問,支持‘+’號操作。

eg:

>>> squares[0] # indexing returns the item 1 >>> squares[-1] 25

>>> squares[:] [1, 4, 9, 16, 25] >>> squares + [36, 49, 64, 81, 100] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 3.列表可以對其中的元素進行修改。

>>> cubes = [1, 8, 27, 65, 125] # something's wrong here >>> 4 ** 3 # the cube of 4 is 64, not 65! 64 >>> cubes[3] = 64 # replace the wrong value >>> cubes [1, 8, 27, 64, 125] append 4.append函數是往列表后添加數據,len函數返回列表長度

eg:

>>> cubes.append(216) # add the cube of 6 >>> cubes.append(7 ** 3) # and the cube of 7 >>> cubes [1, 8, 27, 64, 125, 216, 343] >>> letters = ['a', 'b', 'c', 'd'] >>> len(letters) 4




總結

以上是生活随笔為你收集整理的使用Python作为计算器的全部內容,希望文章能夠幫你解決所遇到的問題。

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