[PYTHON] [Note] Operators related to four arithmetic operations

List of four arithmetic operations available in Python

This is a summary of the four arithmetic operations that can be used with numeric types. It suddenly came out as a numerical type. It's suddenly difficult. In short, I was convinced that "each data is classified into types, so be careful when handling the data." See below for details. I read it though it seems to be frustrating in the first few lines. I feel like I'm sliding my brain. [What is the data type](http://e-words.jp/w/%E3%83%87%E3%83%BC%E3%82%BF%E5%9E%8B.html#:~:text =% E3% 83% 87% E3% 83% BC% E3% 82% BF% E5% 9E% 8B% E3% 81% A8% E3% 81% AF% E3% 80% 81% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3% E3% 82% B0,% E8% A6% 8F% E7% B4% 84% E3 % 82% 92% E5% AE% 9A% E3% 82% 81% E3% 81% 9F% E3% 82% 82% E3% 81% AE% E3% 80% 82 "What is a data type?")

as a trial

It's addition. Before writing this, I just filled in test.py with the addition formula and executed it in the terminal, but I was at a loss because nothing was displayed, but it is not displayed in the terminal unless it is properly printed. Or you need to type "python" in the terminal and do it in interactive mode. image.png

List

Since it is not output, print is not described. If you output to the terminal, enclose it in print. You can perform ordinary four arithmetic operations.

test.py


x + y #Sum of x and y
x - y #Difference between x and y
x * y #Product of x and y
x / y #quotient of x and y
x % y #The remainder of x divided by y
x ** y #x to the yth power

Operations of different types

These are not just numeric types, they are also applicable to string types. If you try adding "apple" and "banana", the terminal will display apple banana and the result. However, numeric type and character string type cannot be calculated as they are. I got an error when I added the string type apple and the numeric type 2 as they were. If the numeric type 2 is changed to the string type "2", it is calculated as apple2 without any error. In the case of dynamically typed languages like python, you don't have to be so conscious of it, but if you think about it consciously, it may be useful when studying other languages. When I was reading a Java book, it seemed to be difficult because I had to specify the type for the part that says "You'll understand if you do that!"

Recommended Posts

[Note] Operators related to four arithmetic operations
Four arithmetic operations in python
Julia Quick Note [02] Arithmetic formulas, operators
Introduction to TensorFlow-Summary of four arithmetic operations and basic mathematical functions
Four arithmetic operations by machine learning 6 [Commercial]
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Note to daemonize python