Have you ever seen these mathematical operation symbols?

Introductory tutorials, case source code, learning materials, readership

Please visit:  python666.cn

Hello everyone, welcome to Crossin's programming classroom!

Mathematical operations are indispensable operations in programming.

Among them, addition, subtraction, multiplication and division are relatively simple, just like the writing in normal mathematics. It should be noted that in most programming languages, multiplication is *, not ×; division is /, not ÷. The same is true for Python.

Participating in the calculation can be a value, or a variable representing a number:

print(3 * 7)
a = 12
print(a / 3)

output:

21
4.0

The result of the division (/) operation in Python 3 is a decimal, even if the result is divisible. So a / 3 in the above example is 4.0 instead of 4.

If you just need to get the divisible result, you can use two forward slashes (//):

print(8 / 2)
print(8 // 2)
print(9 // 2)

output:

4.0
4
4

It can be seen that when it is not divisible, the result of the // operation will discard the remainder.

If you want to get this remainder, you can use the modulo operation, and the symbol is the percent sign (%):

print(9 % 2)
print(9 % 5)

output:

1
4

We've also seen % before in string formatting. For different types of data, it plays a different role: for two numbers, it is modulo, and for two strings, it is formatted.

Another very common mathematical operation is squaring, or exponentiation. In many programming languages, powers are represented by the ^ symbol. However, in Python, ^ represents the "bitwise XOR" bit operation (it doesn't matter if you don't understand this operation, you usually don't use it), and the symbol representing the power is two multiplication signs (* *):

a = 8
b = a ^ 2   # 8和2按二进制位进行与操作
print(b)
b = a ** 2  # 8的2次方
print(b)

output:

10
64

Python also supports an operation called "assignment operator", which is to add an assignment operator (=) after the mathematical operator, indicating that the calculated result is assigned to the variable on the left. For example, a += 1 is equivalent to a = a + 1, which is a simplified way of writing. Addition, subtraction, multiplication, and division, including multiplication, integer division, and modulus, can all be written in the form of assignment operators:

a = 7
a **= 2
print(a)    # 7*7=49
a //= 2
print(a)    # 49//2=24
a %= 2
print(a)    # 24%2=0

output:

49
24
0

In some languages, a += 1 and a -= 1 can be further simplified into a++ and a--, but unfortunately Python does not support it.

Finally, given a piece of code, can you see what it does?

x1 = 1
y1 = 2
x2 = 3
y2 = 4
dist = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
print(dist)

What else do you want to know about math operations in Python? Welcome to discuss in the message area.


The following is the video time, welcome everyone to pay attention, like, and forward:


Crossin's second book " Operation on Code: Using Python and ChatGPT to Efficiently Get Excel Data Analysis " is now on the market.

Click here to view the introduction of the previous book "Action on Code: Learning Python Programming with Zero Basics"

5e0f3b979fe6ff1ae96e3c18deb3edb8.jpeg

This book explains the ideas, methods and practical applications of processing and analyzing data from the perspective of the combined use of Python and Excel. Whether you are a learner who wants to engage in data analysis or an office worker in other occupations, you can master the skills of Python to analyze data through the study of this book. The book innovatively introduces ChatGPT into teaching, uses ChatGPT to answer questions and provides practical training codes, and introduces some practical skills of using ChatGPT to assist learning, bringing a new way of learning to learners.

Readers and friends of the official account can contact me in the background after purchase and join the reader exchange group. Crossin will open the accompanying reading mode for you and answer all your questions when reading this book.

Thank you for retweeting and liking ~


_Previous article recommendation_

5 methods of string formatting, the third is simple and flexible


If you want to learn about paid quality courses and teaching Q&A services

Please reply in Crossin's programming classroom : 666

81f9dc401e4ee79a14120ae16854f40a.jpeg

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/132419473