Python learning entry to master: numeric data types

Python learning entry to master: numeric data types

1. Numbers

When we were very young, parents began to teach us to count, from 1 to 10, smart children can count more.

Python supports 3 numeric types

  1. Integer (int), usually called integer or integer. This concept is the same as the integer we learned in elementary school. Python3 has no size limit for integers.
  2. Floating point (float), floating point data consists of integer part and decimal part
  3. Complex number (complex), composed of real part and imaginary part, hardly used in programming

2. Data Type

Now, please open the interactive interpreter and follow my code to learn and understand int and float

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3
>>> 5.5
>>> type(3)
>>> type(5.5)

In the interactive interpreter, the print function will be used by default to output the data you input. In the example, I input an int data and a float data, and use the type function to check their types.

Understanding the data type is crucial, because different data types have different uses and they have different capabilities. The type function is specifically used to view the type of a data. In the future, you will often use this function.

3. Numerical operations

After learning int and float, you have to do something with them. Here, you don’t need any professional computer knowledge. You only need to know the four simple arithmetic operations of addition, subtraction, multiplication and division. Open the interactive interpreter and follow me.

>>> 4 + 5
>>> 5 * 4
>>> 8/2
>>> 6-2
>>> 4> 5
>>> 6 >= 6

We perform 4 arithmetic operations in the interpreter, and the interpreter will tell us the result immediately. This is the program. In addition to the 4 arithmetic operations, you can also compare the size of two numbers. Unlike the 3 arithmetic, the result is True Or False, about them, I will explain in the next article.

The calculations that numbers can perform are more than these. Today is just a preliminary understanding. When introducing operators, you will learn more calculation methods.

4. Type conversion

The content mentioned above hardly exceeds the scope of your elementary school knowledge, but the following content is not.

When programming, we often convert the type of data. For example, we will convert a float type to int type data to meet our specific operation requirements. There are also cases where int type data is converted to float type. In interactive Perform the following operations in the interpreter

>>> float(33)
>>> int(22.34)

In this article, you have learned two built-in functions, int() and float(), and you will learn more type conversion functions later.

5. After-class practice questions

The content of this tutorial is relatively simple, but I still recommend that you carefully complete the exercises after class. Believe me, the quality of programming linguistics has nothing to do with speed, but with the accuracy of concept understanding and the proficiency of knowledge mastery.

5.1 Data type recognition

Please directly state the type of the following data

1. 4343
2. 43.53
3. 0.0
4. 43.

Remember your answer, then verify your answer in the interactive interpreter

>>> type(4343)
>>> type(43.53)
>>> type(0.0)
>>> type(43.)
>>> type(0)

5.2 Data type conversion

Please directly state the execution result of the following code

1. int(3.14)
2. float(3)
3. int(float(3))
4. float(int(3.14))

Remember your answer, then verify your answer in the interactive interpreter

>>> int(3.14)
>>> float(3)
>>> int(float(3))
>>> float(int(3.14))

Remember to follow comments, reposts, and favorites

Reference: Python learning from entry to proficiency: numeric data types-Cloud + Community-Tencent Cloud