English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Python에서는 수학 연산에 사용되는 몇 가지 표준 라이브러리 메서드가 있습니다. 예를 들어, 수학, 논리, 관계, 비트 등 연산.연산자모듈에서 찾을 수 있습니다.
먼저 사용하기 전에, 우리는 운영자 표준 라이브러리 모듈을 가져와야 합니다.
import operator
이 섹션에서는 비트 연산과 컨테이너 연산에 사용되는 연산자 함수를 볼 것입니다.
먼저, 우리는 계산 연산 기능을 볼 것입니다. 이들은 다음과 같습니다.
순번 | 기능 및 설명 |
---|---|
1 | add(x,y) 이 |
2 | sub(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)))
출력 결과
Add: 101 Subtract: 11 Multiplication: 2520 True division: 1.2444444444444445 Flor division: 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