## Happy journey of simple Python: general application of Python basic syntax

Defonds 2020-11-13 04:49:23
happy journey simple python general

# 1. Addition of two numbers

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 intfloat or complex And so on . You can use the same data type or Python Supported data types intfloat or complex To carry the additive value .

## 1.1. grammar

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 ,intfloat 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
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 .

## 1.3. Add two floating-point numbers

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
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 .

## 1.4. Add integers and floats

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 .

## 1.5. Add two complex numbers

Python Support complex number . In this example, we add two complex numbers and see the result .

``````a = (1 + 8j)
b = (6 + 4j)
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 .

# 2. Randomly generate a positive or negative number

## 2.1. Generate a random number

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 .

## 2.2. randInt() The grammar of

Before you call randInt() You have to introduce random package ：

``````import random
``````

randInt() The grammar is as follows ：

``````randomnumber = random.randint(minimum, maximum)
``````

among

• [minimum, maximum] Is the range of random numbers
• randint() Returns an integer
Generating random numbers can be used to simulate uniform probability events 、 gambling 、 Statistical sampling and so on .

## 2.3. Python Example of generating random numbers in

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 .

## 2.4. Generate random negative numbers

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 ：

## 2.5. Summary

In this paper , We've learned how to do this through detailed examples Python To generate a random number .