English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pythonでは、算術、論理、関係、ビットなどの数学演算に使用される標準ライブラリメソッドもあります。これらのメソッドは演算子モジュールの下に見つけることができます。
まず、それを使用するには、operatorスタンダードライブラリモジュールをインポートする必要があります。
import operator
このセクションでは、ビット演算やコンテナ操作に使用される演算子関数について見ていきます。
首先,我们将看到算术运算功能。这些如下。
番号 | 機能と説明 |
---|---|
1 | 加(x,y) この |
2 | 子(x,y) この |
3 | mul(x,y) この |
4 | truediv(x,y) この |
5 | floordiv(x,y) この |
6 | mod(x,y) この |
7 | 戦俘(x,y) この |
#Arithmetic Operators import operator print('Add: ' + str(operator.add(56, 45))) print('Subtract: ' + str(operator.sub(56, 45))) print('Multiplication: ' + str(operator.mul(56, 45))) print('True division: ' + str(operator.truediv(56, 45)) # same as a / b print('Floor division: ' + str(operator.floordiv(56, 45)) #same as a // b print('Mod: ' + str(operator.mod(56, 45)) #same as a % b print('pow: ' + str(operator.pow(5, 3)))
しゅつりょくせいけい
加算: 101 減算: 11 乗算: 2520 トゥルー除算: 1.2444444444444445 フロア除算: 1 Mod: 11 pow: 125
この演算子モジュールには、<、<=、>、>=、==、!=などの関係演算子も含まれています。
演算子機能は以下の通りです-
番号 | 機能と説明 |
---|---|
1 | lt(x,y) この |
2 | le(x,y) この |
3 | eq(x,y) この |
4 | gt(x,y) この |
5 | ge(x,y) この |
6 | ne(x,y) この |
#Relational Operators import operator print('Less Than: ' + str(operator.lt(5, 10))) print('Less Than Equal: ' + str(operator.le(10, 10))) print('Greater Than: ' + str(operator.gt(5, 5))) print('Greater Than Equal: ' + str(operator.ge(5, 5))) print('Equal to: ' + str(operator.eq(12, 12))) print('Not Equal to: ' + str(operator.ne(15, 12)))
しゅつりょくせいけい
Less Than: True Less Than Equal: True Greater Than: False Greater Than Equal: True Equal to: True Not Equal to: True