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 [40]: 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 [46]: a = 32 In [47]: a Out[47]: 32 In [48]: b = 0x20 In [49]: b Out[49]: 32 In [50]: c = 0b100000 In [51]: c Out[51]: 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 [68]: a = 0.000000002 In [69]: a Out[69]: 2e-09 In [70]: b = 76000000000000000.0 In [71]: b Out[71]: 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 [72]: 3 + 4 Out[72]: 7 In [73]: 9-10 Out[73]: -1 In [74]: 39 + 18/3 Out[74]: 45.0 In [75]: 3 + 18*3 Out[75]: 57 In [76]: (50 - 30) / 2 Out[76]: 10.0 In [77]: 19 / 2 Out[77]: 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 [81]: 19 // 3 # quotient Out[81]: 6 In [82]: 19 % 3 # Modulo division Out[82]: 1 In [83]: 6 * 3 + 1 # Quotient × Divisor + remainder Out[83]: 19
In addition, the operator to calculate the power in Python is:**
In [84]: 8 ** 2 Out[84]: 64 In [85]: 8 ** 3 Out[85]: 512
String: str
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 [86]: 'Ape Anthropology Python' Out[86]: 'Ape Anthropology Python' In [87]: 'Ape man\'learn\'Python' Out[87]: "Ape man'learn'Python" In [88]: "Ape man'learn'Python" Out[88]: "Ape man'learn'Python" In [89]: "Ape man\"learn\"Python" Out[89]: 'Ape man"learn"Python' In [90]: '''hi! ...: hello''' Out[90]: '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 [91]: print('C:\good\named-files') # In this string \ n is a character that represents a newline character C:\good amed-files In [92]: 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 [93]: 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 [94]: None == False Out[94]: False In [95]: None == 0 Out[95]: False In [96]: type(None) Out[96]: NoneType In [97]: type(0) Out[97]: int In [98]: type(False) Out[98]: bool In [99]: 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.
summary
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.
practice
(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.