The essence of a computer is calculation. Internally, it is the change of bits of 0 and 1, and externally, it is the change of data. So what data can computers process? In essence, data is stored in bytes. In terms of performance, they are integers, floating-point numbers, strings and so on. Python's basic data types are no more than this.
Integers and floating-point numbers are the same as in mathematics, and strings are combinations of various characters.
Value type: integer (int), floating point number (float)
int and float are keywords reserved for integers and floating-point numbers in Python.
Integers and floating-point numbers (that is, decimals) are the same as we learned in primary school:
Integer in Python: int
Including positive integers, zero and negative integers: 3, 0, - 20, etc. In daily life, the integers we contact are decimal, and binary and hexadecimal are also very common in computers.
Binary integers are prefixed with 0b. Because they are binary, they only contain 0 and 1 characters, such as 0b101 and 0b1000. If it contains characters other than 01, it is wrong:
In : b = 0b1003 File "<ipython-input-40-df926afcdae8>", line 1 b = 0b1003 ^ SyntaxError: invalid syntax
The hexadecimal integer is prefixed with 0x and contains 6 characters from 0-9a-f, such as 0x23ab and 0xfd39803a. The same hexadecimal can only contain the 16 characters specified in it. However, the six letters A-F are not case sensitive, which means that A-F is also a legal hexadecimal character.
Binary legal 01 has two characters, and hexadecimal legal 0-9a-f has 16 characters in total, which is the same as decimal 0-9 with 10 characters in total.
In : a = 32 In : a Out: 32 In : b = 0x20 In : b Out: 32 In : c = 0b100000 In : c Out: 32
In addition, as a subtype of integers, Boolean represents "yes and no" in the program, and its value has and only has two: True and False. It is an important concept of conditional judgment and comparison operation in future programming. We will talk about boolean type later.
Floating point numbers in Python: float
Floating point numbers are decimals in mathematics and are also commonly used in numerical calculation. The writing method in Python is the same as that in mathematics, such as 0.235, 3.1415926, - 6.5 and so on. However, if the value is very large or small, the floating point number will become scientific counting method: 9.8e+16, 2e-10.
In : a = 0.000000002 In : a Out: 2e-09 In : b = 76000000000000000.0 In : b Out: 7.6e+16
Numerical operation: Python as interpreter
When it comes to numerical types, we have to talk about numerical calculation. After all, numbers are used to calculate.
Like mathematics, Python supports four operations between integers and floating-point numbers, addition, subtraction, multiplication and division: +, -, *, /, which are also grouped by parentheses (). For example:
In : 3 + 4 Out: 7 In : 9-10 Out: -1 In : 39 + 18/3 Out: 45.0 In : 3 + 18*3 Out: 57 In : (50 - 30) / 2 Out: 10.0 In : 19 / 2 Out: 9.5
It also supports the calculation of quotient and remainder in mathematics, such as:
a ÷ b = q...r
(13 ÷ 3 = 4...1)
Where, a is the divisor, b is the divisor, q is the quotient and r is the remainder
The operator for quotient (floor division) in Python is / /,
The operator for calculating the remainder (also known as modulo division, modulo operation and modulo operation) is%. Modulo division is widely used in programming, such as the judgment of odd and even numbers.
In : 19 // 3 # quotient Out: 6 In : 19 % 3 # Modulo division Out: 1 In : 6 * 3 + 1 # Quotient × Divisor + remainder Out: 19
In addition, the operator to calculate the power in Python is:**
In : 8 ** 2 Out: 64 In : 8 ** 3 Out: 512
In our human language, numerical values correspond to calculations and words correspond to records. In Python language, string is also used for recording. It is composed of a string of characters (including ASCII, Chinese characters, etc.).
Strings can be written in many forms:
- Single quotation mark: 'a string enclosed by single quotation marks that can be embedded with "double quotation marks'
- Double quotation marks: "strings enclosed by double quotation marks can be embedded with 'single quotation marks'"
- Three quotation marks: '' 'enclosed by three consecutive single quotation marks'', "" "enclosed by three consecutive double quotation marks" ""
In particular, the three quotation mark string can be written in multiple lines, and the white space between multiple lines (including newline character, space and Tab key) is part of the string.
At the same time, you can use backslash \ to escape characters:
In : 'Ape Anthropology Python' Out: 'Ape Anthropology Python' In : 'Ape man\'learn\'Python' Out: "Ape man'learn'Python" In : "Ape man'learn'Python" Out: "Ape man'learn'Python" In : "Ape man\"learn\"Python" Out: 'Ape man"learn"Python' In : '''hi! ...: hello''' Out: 'hi!\nhello'
Backslash \ will escape the characters after it to special characters, such as \ n to line feed. If you want to cancel this escape, you can use the original string, that is, add r before the string:
In : print('C:\good\named-files') # In this string \ n is a character that represents a newline character C:\good amed-files In : print(r'C:\good\named-files') # The string \ n is the original two characters: ` \ ` and ` n ', because it is preceded by r. C:\good\named-files
Null value: None
Python defines a special None to represent null value, which is different from False and 0, but its logical value is False. You can see the following code to deepen your understanding:
In : None + 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-93-3fd8740bf8ab> in <module> ----> 1 None + 1 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' In : None == False Out: False In : None == 0 Out: False In : type(None) Out: NoneType In : type(0) Out: int In : type(False) Out: bool In : if None: ...: print('i am True') ...: else: ...: print('i am False') ...: i am False
If the function you define does not return a value, Python will automatically return None.
In this section, we learned that Python can be used as a computer to perform addition, subtraction, multiplication, division, quotient and other operations of basic types such as integers and floating-point numbers, as well as the representation of strings.
(1) Open the Python shell (or Ipython) to perform four operations on integers and floating-point numbers, including addition, subtraction, multiplication and division, the use of parentheses, and the power, quotient and modulo division of integers.
(2) Familiar with several representations of strings, combined with their own search to understand the "escape" in programming.