[Python 1-6] Python tutorial number


A number is a writing symbol used to represent a number
Chinese digital writing 37, 37
Roman numeral writing XXXVII
Arabic numeral binary writing 100101

First address of the article

Author: Vlad
Source Vlad (official account: fulade_ me)


In programming, numbers are often used to record game scores, represent visual data, and store web application information. Python handles numbers in different ways, depending on how they are used. We usually use integers most frequently. Let’s take a look at how Python manages integers.


In Python, you can add integers+reduce-ride*except/Operation.

>>> 2 + 3 
>>> 3 - 2 
>>> 2 * 3 
>>> 3 / 2 

In a terminal (command line) session, python directly returns the result of the operation. Python uses two multiplication signs for the power operation:

>>> 3 ** 2 
>>> 3 ** 3 
>>> 10 ** 6 

Python also supports operation order, so you can use multiple operations in the same expression. Of course, we can also use parentheses to modify the operation order and let Python perform the operation in the order you specify, as follows:

>>> 2 + 3*4
>>> (2 + 3) * 4 
Floating point number

Python refers to numbers with decimal points as floating-point numbers. Most programming languages use the term, which points to the fact that a decimal point can appear anywhere in a number.
Every programming language must be carefully designed to handle floating-point numbers properly to ensure that the behavior of numbers is normal no matter where the decimal point appears.
For the most part, you don’t need to consider the behavior of floating-point numbers when you use them. You just type in the numbers you want to use, and python usually handles them the way you want them to:

>>> 0.1 + 0.1
>>> 0.2 + 0.2 9 0.4
>>>2 * 0.1
>>>2 * 0.2

However, it should be noted that the number of decimal places contained in the result may be uncertain:

>>> 0.2 + 0.1 
>>> 3 * 0.1 

All languages have this problem and there is nothing to worry about. Python will try to find a way to do as much as possibleaccurateIn some cases, it’s difficult to represent the results accurately, given the way numbers are represented in a computer. For now, just ignore the extra decimal places.

Using the function str() to avoid type errors

You often need to use variable values in messages. For example, if you want to wish someone a happy birthday, you might write something like the following (save the following asbirthday.py):

age = 23
message = "Happy " + age + "rd Birthday!"

You may think that the above code will print a simple birthday greeting:Happy 23rd birthday!. But if you run these codes, you’ll find that they cause errors:

Traceback (most recent call last):
File "birthday.py", line 2, in <module>
message = "Happy " + age + "rd Birthday!"
TypeError: Can't convert 'int' object to str implicitly

This is a type error, which means Python doesn’t recognize the information you’re using. In this example, python found that you used a variable with an integer value (int), but it didn’t know how to interpret the value (see). Python knows that this variable may represent the value 23, or the characters 2 and 3. When using an integer in a string like this, you need to explicitly indicate that you want Python to use the integer as a string. To do this, call the function str (), which lets Python express a non string value as a string:

age = 23
message = "Happy " + str(age) + "rd Birthday!"

In this way, python knows that you want to convert the value 23 into a string, and then display the characters 2 and 3 in the birthday message. After the above processing, the message you expect will be displayed without causing an error:

Happy 23rd Birthday!

In most cases, it’s very easy to use numbers in Python. If the result is unexpected, check that Python interprets numbers as numbers or strings in the way you expect.

6-1 write four expressions, which use addition, subtraction, multiplication and division, but the results are numbers8
In order to use print statements to display results, be sure to bracket these expressions. That is to say, you should write four lines of code similar to the following:
print(5 + 3)
The output should be 4 lines, each of which contains only the number 8. Namedeight.py
6-2 store your favorite number in a variable, then use the variable to create a message, point out your favorite number, and print the message. Namednumber.py

If you want to check the answers to your homework, you can goMy githu warehouse

[Python 1-6] Python tutorial number