Python data type Python provides 6 standard data types by default:
1. Number类型 数值类型2. String类型 字符串类型3. List类型 列表类型4. Tuple类型 元组类型5. Dict类型 字典类型6. Set类型 集合类型
1. Number type numeric classification: Altogether 4 kinds of small categories:
1. 整型 int 例:30 2. 浮点型 float 例:30.33. 布尔型 bool 例:True or False4. 复数 complex 例:6+8j
1. Integral type
Integral types include positive integers, 0, and negative integers.
There are 4 types of writing formats commonly used:
十进制: 范围:0-9二进制: 范围:0-1八进制: 范围:0-7十六进制: 范围:0-9A-F
2. Float type (i.e. decimal type)
2 formats for declaring floating-point types:
小数点方式: 变量 = 3.14科学计数法: 变量 = 314e-2
3. Boolean type
2 Ways to express a Boolean type:
True 表示对象为真False 表示对象为假
4. plural
The complete structure of the complex number:
(实数+虚数) 例:6+8j
Real numbers: All the values that exist in real life are real numbers.
Imaginary number: Numbers that are not real (the square of I is-1 as the basic unit), the number that does not exist
数学:i*2 = -1 i就是虚数的基本单身 1i,2i,3i...计算机中:用j来表示i即可
Two ways to declare a complex number:
表达式方式: 变量 = 实数 + 虚数 例:num = 3 + 4j特定功能: 变量 = complex(实数,虚数值) 例:num = complex(5,3)
2. String type (literal type) strings are commonly written in 6 ways:
1. 单引号字符串 例:‘Hello Word!‘2. 双引号字符串 例:"Hello Word!"3. 三引号字符串 例:‘‘‘ Hello Word!‘‘‘4. 转义符字符串 例:"This‘s a pen!" or ‘This\‘s a pen‘5. 自然字符串 例:r‘Hello Word!\n‘6. Unicode字符串 例:u‘Hello Word!‘
1. Single quotation marks: When the content contains a large number of double quotation marks, it is appropriate to use single quotation marks to declare the string 2. Double quotation marks: When the content contains a large number of single quotes, it is appropriate to use double quotation marks to declare the string 3. Three-quote: for multiple (row) content, single and double quotes are present in cases where the string is declared
Note: in general, use single quotes!
4. Escape character: Change the meaning of a string by some format
Common escape characters are as follows:
转义符 描述\(在行尾时) 续航符\\ 反斜杠\‘ 单引号\" 双引号\a 响铃\b 退格(Backspace)\e 转义\000 空\n 换行\v 纵向制表符\t 横向制表符\r 回车\f 换页\oyy 八进制数,yy代表字符 例:\o12代表换行\xyy 十六进制数,yy代表字符 例:\x0a代表换行\other 其他的字符以普通格式输出
5. Meta strings (i.e. natural strings)
Adding the letter R or R before any string, all escape characters in the current string are not escaped when used, and are common in regular expressions.
3. List Type
A list is a sequential combination of some data that can be arbitrarily modified and manipulated after the combination.
格式: 变量 = [值,值,值,...]标识符: []
How to create a list:
1. lie = list((1,2,4))2. lie = list([1,2,4])3. lie = [1,2,4]4. lie = list(tuple((1,23,4)))5. lie = list(tuple([1,23,4]))
4. Type of tuple
A tuple is a sequential combination of some data and cannot be modified after it is combined.
格式: 变量 = 值,值,值,... or 变量 = (值,值,值,...)标识符: ,
How to create a tuple:
1. tu = 1,2,4,2. tu = (1,3,4,)3. tu = tuple((1,2,3,))4. tu = tuple([1,2,3,])5. tu = tuple(list((1,3,4,)))6. tu = tuple(list([1,3,4,]))
5. Dict Type
A dictionary is a set of unordered data combinations that have a key-value mapping relationship, which can be modified and manipulated.
格式: 变量 = {键:值,键:值,...}标识符:{}
How to create a dictionary:
1. dic = {1:324,2:345}2. dic[1] = ‘3435‘ **注**: 指定key创建的字典,一次只能指定一个key3. dic = dict({1:324,2:345})
6. Set type
A collection is an unordered collection of data that can be modified after a combination, and is inherently free of duplicate data functionality.
格式: 变量 = {值1,值2,值3,...}标识符:{}
How to create a collection:
1. se = set(‘abc‘) **注**:调用set()函数创建集合时,每次只能添加一个元素2. se = {‘a‘,‘b‘}
Python data type