stay Python in , You can use the arithmetic addition operator + Add two numbers . This operator takes two operands and returns the result of the addition .Python The number in can be int、float or complex And so on . You can use the same data type or Python Supported data types int、float or complex To carry the additive value .
The operation syntax of arithmetic addition is as follows :
result = operand1 + operand2
+ Operators for numbers operand1 and operand2 Add and return the result . In the example above , We store the results in a file called result Variables in .
To quickly demonstrate the operator , You can open it Python Shell Then run the following operation :
>>> 25 + 63
88
>>> a = 32
>>> b = 87
>>> a + b
119
Before you add, you need to make sure that the variables you pass in are numbers , such as ,int、float or complex.
In the next example , We're going to add two integers , And print the results to the standard console output .
a = 1
b = 6
# add two integer numbers
sum = a + b
# display the sum
print(sum)
Execution and output :
a and b The data types of are all integers , So the result of addition is also an integer .
In the next example , We're going to add two floating-point data , And print the results to the standard console output .
a = 1.5
b = 6.3
# add two float numbers
sum = a + b
# display the sum
print(sum)
Execution and output :
so , The result of adding two floating-point types is also floating-point type .
In the next example , We created a floating-point variable and an integer variable , And then use + The operator adds it , And then print the results .
a = 1.5
b = 6
# add a float and an int
sum = a + b
# display the sum
print(sum)
Execution and output :
In the example above ,a For floating point b Is an integer . When you add two different data types , The data of the lower type will be promoted to the type of the data of the higher type . So here , When you add integers and floats , Integers are promoted to floating point types , The data type of the operands with the higher data type becomes the data type of the result .
Python Support complex number . In this example, we add two complex numbers and see the result .
a = (1 + 8j)
b = (6 + 4j)
# add two complex numbers
sum = a + b
# display the sum
print(sum)
Execution and output :
In the addition of complex numbers , Add the real parts of two numbers , Then add the imaginary parts of the two numbers .
In this article, we learned how to add two different types of data .
Random numbers are used in such things as signal processing 、 Data analysis 、 Statistics and other applications have attracted attention . stay Python You can introduce modules in random And use the function randInt() To generate a random number .
Before you call randInt() You have to introduce random package :
import random
randInt() The grammar is as follows :
randomnumber = random.randint(minimum, maximum)
among
In the next example , We will be in the range of [10, 152] To generate a random number .
import random
randomnumber = random.randint(10, 152)
print(randomnumber)
Execution and output :
so ,randint() Each execution of the function returns a different random number .
You can also provide a negative range for generating random numbers . In the next example , We will be in the range of [-100, -21] To generate a random number .
import random
randomnumber = random.randint(-100, -21)
print(randomnumber)
Execution and output :
In this paper , We've learned how to do this through detailed examples Python To generate a random number .