Zero-based learning of Python programming - different operations: arithmetic, relational and logic (1)

57ceb17f38f94b0b9dd5f27302e771a7.gif

  • About the author: A cloud computing network operation and maintenance personnel, sharing the technology and dry goods of network and operation and maintenance every day. 

  •  Motto: Keep your head down and hurry on your way, be respectful

  • Personal homepage: Homepage of Netdou

c7565d29c1014a06a8b9831658899c64.jpeg

Table of contents

 

  foreword

learning target

1. Operation

1. Arithmetic operations

 2. Addition operation

 3. Subtraction operation

4. Multiplication operation

5. Division operation

6. Remainder operation

7. Divide operation

 8. Power operation


 

  foreword

Whether it is Python programming or programming in other languages, operations are inseparable. This chapter will explain the operations related to these data types, mainly including arithmetic operations, relational operations and logical operations.


learning target

  • Master arithmetic operations.
  • Master relational operators and relational operations.
  • Master logical operators and logical operations.

1. Operation

1. Arithmetic operations

Commonly used arithmetic operations in Python include addition, subtraction, multiplication, division, remainder, integer division, power, etc. Participating in calculations can be numerical values ​​or variables. Let's give examples one by one.

a573bba37e63482fa8be55c8970381f0.png


 2. Addition operation

Addition operations can be performed between integers, floating-point numbers, and Boolean data in Python. Note that in the operation, the Boolean data True represents the integer 1, and False represents the integer 0. Strings can be added to strings, but strings cannot be added to other types of data.

[Example 4-1]
Write the following program in shell mode.

  • Lines 1 and 2: The sum of the integer 100 plus 100 is 200.
  • Lines 3 to 4: The sum of the floating point number 3.14 and the integer 2 is 5.140000000000001. The inaccurate calculation of the floating point number is due to the error caused by the mechanism of the computer storing the floating point number.
  • Lines 5-6: The sum of the Boolean value True and the integer 100 is 101. Lines 7-8: The sum of the Boolean value False and the decimal 30 is 3.0.
  • Lines 9-10: The result of adding the string "abc" and the string "123" is "abc123".
1.>>> 100 + 100
2.200
3.>>> 3.14 + 2
4.5.140000000000001
5. >>> True +100
6.101
7. >>> False + 3.0
8.3.0
9. >>> "abc" + "123"10.'abc123'

 3. Subtraction operation

Similar to addition, subtraction can be performed between integers, floating-point numbers, and Boolean data in Python. Strings do not support subtraction with other types of data, and strings cannot be subtracted from strings, that is, strings do not support subtraction.

[Example 4-2]

Write the following program in shell mode.

  • Lines 1~2: The result of subtracting 100 from the integer 200 is 100
  • Lines 3 to 4: The result of subtracting the integer 2 from the floating point number 3.0 is 1.0
  • Lines 5-6: The result of subtracting the integer 3 from the Boolean value True is -2
  • Lines 7-8: The result of subtracting the Boolean value False from the integer 3 is 3
1.>>>200-100
2. 100
3.>>3.0-2
4.1.0
5. >>> True - 3
6.-2
7. >>> 3 - False
8.3

4. Multiplication operation

Multiplication operations are supported between integers, floating-point numbers, and Boolean data in Python. Strings only support multiplication with integer type data. Note that the multiplication symbol is an asterisk "*".

[Example 4-3]
Write the following program in shell mode.

  • Lines 1-2: The result of multiplying the integer 100 by 2 is 200.
  • Lines 3-4: The result of the floating point number 1.14 and the integer 2 is 6.28.
  • Lines 5-6: The result of the integer 100 and the value Tnae is 100.
  • Lines 7-8: Integer 100 and Boolean value Falsc evaluates to 0.
  • Lines 9-10: String "abcdefabcdef" for the string "abcdefabcdef" with the result of the integer 2.
  • Lines 11-12: The result of multiplying the character "abke" by the integer 0 is an empty character.
1.))> 100·2
2. 200
3. 39) 3.14·2
4.6.28
5.>)> 100*True
6. 1ee
7. >>> 18a · False
8.0
9.>>>"abcdef* *2
10. 'abcdefabcdef'
11.>>> "abc"·0
12.''

5. Division operation

The division symbol in Python is a slash "/", not a backslash "\". In Python, integers, floating-point numbers, and Boolean data can be divided, but strings do not support division. In addition, 0 cannot be used as a divisor, otherwise ZeroDivisionError will be reported.

[Example 4-4]
Write the following program in shell mode.

  • Lines 1-2: The result of dividing the integer 100 by 2 is 50.0.
  • Lines 1-4: The result of dividing the floating point number 4.4 by the integer 2 is 2.2.
  • Lines 5-6: Integer 100 and Boolean Troe result in 100.0.
  • It can be easily seen that the results of the division operation are all floating point numbers.
1.>>> 100 / 2
2. 50.0
3.0) 4,4 / 2
4. 2.2
5. >>> 106 / True
6. 100.0

6. Remainder operation

In addition to the commonly used addition, subtraction, multiplication and division operations in Python, there is also a very useful operation - the remainder operation, which can easily help us find the remainder after dividing two numbers; the symbol of the remainder operation is a percent sign "%".

[Example 4-5]
Write the following program in shell mode.

 

  • Lines 1-2: The integer 100 divided by 2 has a remainder of 0.
  • Lines 3-4: The integer 5 divided by 2 has a remainder of 1.
  • Lines 5-6: The integer 10 divided by the float 3.5 has a remainder of 3.0.
  • It can be seen that when floating-point numbers are involved in the operation, the remainder operation is still valid, but the result is floating-point number type data.
1.>>>100 %2
2.8
53.>>> 5 % 2
4.1
5.>>>10%3.5
6.3.0

7. Divide operation

The integer division operation corresponds to the remainder operation, and the integer division operation is to calculate the integer quotient after dividing two numbers; the symbol of the integer division operation is a double slash "//".

[Example 4-6]
Write the following program in shell mode.

  • Lines 1-2: The rounded result of dividing the integer 5 by 2 is 2.
  • Lines 3-4: The rounded result of dividing the integer 99 by 6 is 16.
  • Lines 5 to 6: The result of dividing the integer 10 by the floating point number 3.5 is 2.0.
  • It can be seen that when floating-point numbers are involved in the operation, the integer division operation is still valid, but the result is floating-point number type data.
1.» 5 // 2
2.2
3. >>> 99 // 6
4. 16
5. >>> 10 // 3.5
6. 2.0

 8. Power operation

The power operation, that is, the power operation, is used to calculate the nth power of a number. Note that the exponentiation symbol in Python is two multiplication signs "**", not the "^" in many languages ​​(which represents the "bitwise AND" operation in Python).

[Example 4-7]
Write the following program in shell mode.

  • Lines 1-2: Calculate 2 to the 3rd power, and the result is 8.
  • Lines 3-4: Calculate 9 to the power of 0.5, and the result is 3.
  • Lines 5-6: Calculate -1.5 to the power of 0.5, and the result is an imaginary number.
1. >>> 2 **3
2. 8
3. >>> 9**0.5
4. 3
5. >>> (-1.5) ** 0.5
6. (7.499399432689231e-17+1.2247448713915893)

Creation is not easy, please pay attention, like, collect, thank you~    

 

Guess you like

Origin blog.csdn.net/yj11290301/article/details/131499436