"Consolidate the Foundation of Python"
**1. Number **
Numbers are a basic data type that must exist in almost all computer languages. In Python, numbers are also quite typical and ubiquitous.
Strictly speaking, in Python, a number is not a strict type of object, but a group of similar types.
In order to fully understand Python's numbers, we will further explain the meaning of numbers at all levels and the various operation rules of numbers from the following aspects.
1、 Number is a constant
In Python, a number is defined as a constant, and the size of this constant is the literal size of the number, so it is also called a literal constant.
For example: 1,2,3,4,5
Do you see the numbers above? Yes, it is strange not to see it. Isn't it 5 numbers? !
Yes, you can see clearly, it is 5 numbers.
But in an object-oriented language like Python, remember that everything is an object, right? So, they are not only 5 numbers, they are also 5 objects, which seems to be in line with the description of the Python language.
Since it is an object, it has a type and a value. So, what type are these 5 objects? Their types are called constants, what about their values? Of course, what you see is what its value is. Therefore, the values of these 5 objects are 1, 2, 3, 4, and 5.
Haha, does it feel like it's coming back again?
It’s really a bit convoluted, it’s clearly 1, 2, 3, 4, 5.
Why should I say that they are 5 objects,
Then their values are 1, 2, 3, 4, 5?
Isn't this schizophrenia?
Haha! Don't be schizophrenic about this little thing.
I really feel that it’s entangled, so there is no need to be too entangled
Just know that they are numbers (constants),
Then know that they have size,
That is valuable,
Moreover, their value is themselves.
OK, it still split me,
However, this time I probably understood it.
***2、***Numbers can be converted to strings
In addition to numbers being constants, we also know that strings are also constants. Regarding the knowledge points of strings, we will talk carefully in the next class. Here, I just want to emphasize again that these two constants can be transformed into each other.
E.g:
str(1)==’1’
int(‘1’)==1
The result of these two expressions is true.
However, please note that the conversion of numbers into strings will always succeed. But this does not mean that the conversion of a string to a number will be successful.
E.g:
The result of int('abc') conversion will report an error.
***3、***Numbers can be assigned to variables
There is no doubt that the invention of variables is significant.
Otherwise, we will always stick to the days of arithmetic operations. Are numbers really numbers? Or numbers? Such days will be long and boring.
Therefore, I always think that algebra is simpler than arithmetic. Setting an x (unknown number) is such a wisdom to solve the troubles of many teenagers who have been tortured to death by arithmetic problems.
Now, it has changed, and it has come to the language of the computer as a variable, and continues to save the boy of the past!
Yes, it is the same x, now it is called a variable, and we can assign a value to it. What you give him, it has what. Yes, you can give it a number, and it will have the value of the number. We will discuss variables in another article.
For example: x = 3
At this time, the number 3 is assigned to the x variable, so the variable named x has the value 3. The important thing is that from now on, we are no longer schizophrenic, x is the name of the variable, and 3 is the value of x. Clearly understand!
Of course, you can also assign an expression to x.
For example: x = (2 + 1)*2
At this time, the value of x becomes 6.
Well, yes, the value of x has changed, it is not always a constant value like a number.
***4、***Number operation
(1) Addition and subtraction
a = 1
b = 2
c = 3
a + b + c
6
That's right, the addition and subtraction of variables is the addition and subtraction of variable values!
(2) Multiplication
a = 1
b = 2
c = 3
a * b * c
6
Well, variable multiplication is still variable value multiplication!
(3) Division
a = 6
b = 4
a/b
1.5
It seems that this is fine. Yes, since Python 3.0, division is no longer so easy to produce moths. I say this to remind everyone that the version of Python2 is still popular on the market. If you come across, or you happen to be using the version of Python2, you have to pay attention.
Its result will be like this:
a/b
1
Huh? Shouldn't a/b be equal to 1.5? Is rounding equal to 2?
Yes, because in the Python2 version, for division operations, if the divisor and dividend are both integers, the result is only the integer part of the quotient, and the remainder is automatically removed.
What if I want to get an accurate calculation result (including decimals)?
The method is very simple, only need one of the divisor or the dividend to be a decimal (called a floating point number to be precise), and you can get a complete quotient with a fractional part.
such as:
float(a)/b
1.5
a/float(b)
1.5
So, remind everyone that when you try to use division in the program code, you have to be careful, especially for users who are still using the Python2 version.
**For multiplication and division operations, Python also gives two special cases, ** and //. **
what does this mean? Multiplying and dividing?
no! It should be said that it means power and division.
E.g:
2**3
8
It represents 2 to the 3rd power, which can also be said to be 3 doubles, so the result is equal to 8. This is easier to understand.
**However, if we use // in our division, sometimes it is easier to understand the result of the operation. **
Look at the example first.
6 //4
1
6.0 //4
1.0
6 //4.0
1.0
6 //-4
-2
what does this mean? This means that if you only want to get the result of dividing two numbers and rounding it up, you can use // to calculate, and it will only give you an evenly divided result.
However, its rounding method is worthy of our attention. It is not a simple rounding, but the so-called rounding down method.
What does this mean? That is to say, when the result of division is a decimal, refer to the method of rounding. If the result of rounding makes the value larger, round it; conversely, if it makes it smaller, round it. Therefore, when the result of this division is a positive number, the fractional part is usually discarded. If it is a negative number, refer to rounding and enter when the remainder is greater than or equal to 5.
Finally, please pay attention to the data type of its result. If the divisor and dividend are both integers, the result is also an integer. Otherwise, as long as one of the items is a floating point number, the result is still a floating point number.
(3) Comparison operation
The comparison operation of numbers is relatively simple, basically equivalent to the comparison operation in our mathematics.
x>y: x is greater than y;
x>=y: x is greater than or equal to y;
x==y: x is equal to y;
x <y:x小于y;
x<=y: x is less than or equal to y;
x!=y: x is not equal to y.
**It should be noted that for comparison operations, there are only two results of their operations: true or false. **
(4) Collection
Why do we think of sets when talking about number operations? In fact, the main thing is not when computing, more likely when processing data, the collection will help us a lot!
For example, we have a numeric list a, as follows:
a = [1,2,2,3,4,4,5,6,7]
Now, when we process this list a, we hope to obtain a list with the duplicate numbers removed. If you use the stupid method, you can think of iterative loops. Through loop comparison, the number of duplicates is eliminated.
However, now that Python has collections, the problem is much simpler.
a = set(a)
a
{1,2,3,4,5,6,7}# Set deduplication
a = list(a)
[1,2,3,4,5,6,7]# Convert to list
See how powerful the collection is! Especially when the amount of data is relatively large, it is extremely powerful.
(5) Built-in mathematical function module
In addition to regular addition, subtraction, multiplication, and division, sometimes we also encounter many other types of operations and number processing. The magical Python provides us with a wealth of built-in functions to help us solve problems.
abs(x) #Go to absolute value
divmod(a,b) #Take the remainder and the quotient
float(x) #Convert to floating point
int(x) #convert to integer
pow(x,y[,z]) #Power operation (x to the y power)
range(x) #Generate a sequence of integers starting from 0 and not greater than x
round(x,n) #Round up, keep n decimal places
sum() #Sum a sequence of numbers
bin(x) #convert to binary
oct(x) #Convert to octal
hex(x) #convert to hexadecimal
The above list is for reference only, there are more built-in functions. If necessary, you can find more built-in functions and usages from the official Python documentation.
Recommended Posts