python- character encoding format string, binary conversion, data type, a list of tuples, dictionaries summary

table of Contents:

A. Character Encoding

II. String Format

III. Hex conversion

IV. Data types and operations

V. string conversion

VI. List

VII. Tuple

VIII. Dictionary

 

A . Character Encoding:

Computers were invented by the Americans, the first character encoding is ASCII, only the provisions of the correspondence between English alphanumeric and some special characters and numbers. Can only be represented by 8 bits (one byte), namely: 2 ** 8 = 256, therefore, can only represent the ASCII code 256 symbols

ascii one byte representing a character;

Common unicode character represents a 2-byte, uncommon word requires 4 bytes;

UTF-8 letters are encoded as a byte, 3 bytes characters typically have only a very rare character is encoded into bytes 4-6.

Example:

Letters x, is represented by ascii 120 decimal, binary 01111000

Chinese characters beyond the scope of the ASCII code, Unicode encoded decimal 20013 binary 0,100,111,000,101,101.

Letter x, represents a binary 0,000,000,001,111,000 with unicode, so unicode compatible ascii, also compatible with all nations, is the world standard

This time the garbage problem is gone, all the documents we used but a new problem has arisen, if our documents are in English throughout, you use unicode ascii space-consuming than twice as many in the storage and transmission is very low effect

In the spirit of conservation, and the emergence of the Unicode encoding into a "variable length code" UTF-8 encoding. UTF-8 encoded according to Unicode character to a different size of the figures 1-6 encoded into bytes, commonly used letters are encoded into a byte characters typically 3 bytes, only a rare character will be encoded into bytes 4-6. If you want to transfer the text contains a lot of English characters, use UTF-8 encoding will be able to save space:

character

Ascll

Unicode

Utf-8

x

01000001

00000000 01000001

01000001

in

I can not express

01001110 00101101

11100100 10111000 10101101

Can also be found from the table above, UTF-8 encoding has an additional advantage is that ASCII encoding can actually be seen as part of UTF-8 encoding, so only supports ASCII encoding a large number of legacy software in UTF- continue working under 8 encoding.

ASCII: only save English and Latin characters, a character occupies one byte, 8

gb2312: can store more than 6700 Chinese, 1980

gbk1.0: save more than 20,000 characters, 1995

gb18030: deposit 27,000 Chinese, 2000

Unicode: Unicode: utf-32 character 4 bytes

                 A character Utf-16 2 bytes, or more than two.

                 Utf-8 English ASCII code to a memory, a Chinese 3 bytes

Statement encoding - * - coding: utf-8 - * -

gbk not know the default utf-8, utf-8 is a subset of Unicode

The default Unicode and other backward compatible gbk

Python3 internal default unicode, file default encoding is utf-8

Phase One: Modern computer originated in the United States, the first born is also based on considerations of ASCII English

ASCII: a representative of a character Bytes (English characters / all of the other characters on the keyboard), 1Bytes = 8bit, 8bit may represent 0-2 ** 8-1 kinds of changes that can represent 256 characters.

ASCII was originally used only after seven, 127 numbers, has been completely all the characters on the keyboard to represent the (English characters / keyboard all the other characters), and later to Latin will be encoded into the ASCII table, also the highest level took up

Phase II: In order to meet Chinese and English, the Chinese custom GBK

GBK: 2Bytes on behalf of a Chinese character, 1Bytes represent an English character

In order to meet other countries, various countries have their own custom coding

Japan allocated to the Japanese Shift_JIS, the Republic of Korea to the Korean ed in Euc-kr

Stage Three: country has its own standards, will inevitably clash, the result is, in a multilingual mix of text, displayed will be garbled. How to solve this problem? ? ?

White said the nature of the garbage problem is not uniform, if we can unify the world, the world can only use one specified text symbols, then the use of a unified coding, then the garbage problem will cease to exist,

Many places or older systems, application software will use a variety of code, this is a problem left over from history. So we must find a solution or coding schemes, need to meet at the same time:

1, compatible with the nations of characters

2, and all over the world have character encoding mapping relationship so that it can be converted into any national character encoding

This is the unicode (fixed length), with a unified 2Bytes represents a character, although 2 ** 16-1 = 65535, but it can be stored 100w + unicode characters as unicode to store mapping relationships with other encodings, unicode and precisely not a character code table in the strict sense

For it is clear that throughout the text are in English speaking, unicode formula is undoubtedly more than double the storage space (binary ultimately electrically or magnetically stored in a storage medium)

Phase IV: so have the UTF-8 (variable length, stands for Unicode Transformation Format), the English characters only 1Bytes said of Chinese characters 3Bytes, to keep more of Bytes with other uncommon words.

Two . Python string formatting

Python supports output format string. Although this can include very complicated expressions, the most basic usage is to insert a value into a string character string format for% s.

E.g:

print("My name is %s and weight is %d kg!" % ('Zrh', 20))

Output:

My name is Zrh and weight is 20 kg!

If you want to output% in the formatted output, it is necessary to use the format %%

E.g:

Print ( " I am the% s, my schedule has been completed %% 80 " % ( ' ZRH ' ))

Output:

I zrh, my schedule has been completed 80%

Three . Hex conversion

Hex is hex digits, we used the hexadecimal include: binary, octal, decimal and hexadecimal, when the difference between them is that the number of operations every few into one. 2 into every example is a binary, decimal i.e. we used is 0-9 every 10 into a hexadecimal 10 to 15 with A, B, C, D, E, F is represented.

1. binary decimal turn

Method: decimal modulo 2 addition method, i.e., decimal number 2 addition, and the remainder is the number of weights on the bits, the quotient obtained by continuing addition to 2, so step down operation continues until the quotient is zero so far.

 

 

2. Binary Coded Decimal

Methods are: the right to expand by a binary number, that was adding decimal numbers.

The first of the 7 th 1 * 2, because the number of bits is 8, 8-1 = 7, and so on.

 

 

Four . Data Types

There are five standard Python data types:

Numbers (numbers) V = 1 int class

Boolean value (Boolean) V = True (bool class)

String (String) V = "Good" (str category)

List (list) V = [ "good", "best"] (list classes)

Tuple (tuple) V = ( "good", "best") (tuple class)

Dictionary (dictionary) V = { "name": "zrh", "age": 20} (dict class)

 

 

1.int class (remember a)

BIT_LENGTH () indicates the current decimal digit represented by a binary number, the least used.

Code Example:

count = 16
v = count.bit_length()
print(v,"---",count)

Output:

5 --- 16

2.str class

For strings, built-in command after the execution, the original value remains unchanged.

2.1: upper () converts a string lowercase to uppercase

Code Example:

name = "zrh"
v = name.upper()
print(v,"---",name)

Output:

VIE --- WAW

2.2: lower () converts the string in all uppercase characters to lowercase

Code Example:

name = "ZrH"
v = name.lower()
print(v,"---",name)

输出结果:

zrh --- ZrH

2.3:capitalize()将字符串的第一个字符转换为大写

代码示例:

name = "zrh"
v = name.capitalize()
print(v,"---",name)

输出结果:

Zrh --- zrh

2.4:strip()去除首尾空格

代码示例:

name = " zrh "
v = name.strip()
print(v+"---",name)

输出结果:

zrh---  zrh

2.5:lstrip()截掉字符串左边的空格或指定字符

2.6:rstrip()删除字符串字符串末尾的空格

2.7:replace(str1, str2 , max)将字符串中的 str1 替换成 str2,如果max指定,则替换不超过max次

代码示例:

content = "人生自古谁无死,早死晚死都得死"
v = content.replace("","*",2)
print(v,"---",content)

输出结果:

人生自古谁无*,早*晚死都得死 --- 人生自古谁无死,早死晚死都得死

2.8: len(string)返回字符串长度

代码示例:

content = "人生自古谁无死,早死晚死都得死"
v = len(content)
print(v)

输出结果:

15

2.9:[]根据索引取值

代码示例:

#索引值从0开始计算不是从1
content = "人生自古谁无死,早死晚死都得死"
v = content[0]       #取字符串的第一个字符
v1 = content[-1]     #-代表从后往前找,1代表第一个,所以-1代表正向的最后一个
v2 = content[0:5]    #从0开始取到索引值为4的对象,不包括索引值为5的对象,相当于数学中的左闭右开区间
v3 = content[8:]     #从索引值为8的对象开始取到最后一个
v4 = content[0:15:2]  #从0开始隔一个取一个,一直取到14,2表步长表示各一个取一个,3就表示隔2个
print(v,v1,v2,v3,v4)

输出结果:

人 死 人生自古谁 早死晚死都得死 人自谁死早晚都死

2.10 :split(“str”,num)

以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num+1 个子字符串

代码示例:

content = "人生自古谁无死,早死晚死都得死"
v = content.split("",2)
print(v)

输出结果:

['人生自古谁无', ',早', '晚死都得死']

2.11 :.isdecimal()判断当前字符串中是否全部都是数字

代码示例:

v = "a123"
c = v.isdecimal()
print(c)

输出结果:

False

五.字符串转换

1.数字转字符串 str(对象)

2.字符串转数字 int(对象)

对象必须是形为数字,才能转换

Int(string)就会报错

3.数字转布尔值 bool(对象)

bool(0)是False

其他不是0的数都是True

4.字符串转布尔值 bool(对象)

bool(“”)是False

其他任何字符串都是True

注意:

代码示例:

a = 9 or 2>3
print(a)

输出结果:

9

代码示例:

a = 0 or 2>3
print(a)

输出结果:

False

代码示例:

a = 0 or 6
print(a)

输出结果:

6

代码示例:

a = 0 or 2<3
print(a)

输出结果:

True

六.列表

列表是Python中最基本的数据结构。列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。

如下所示:

list = ["one","two",3,4,"five"]

列表可以进行 增、删、改、查。如果列表只有一个对象,在后面也要加上,

列表中的元素可以是 数字、字符串、布尔值、列表(列表的嵌套)

1.切片:

list[1:]      #从1开始取到最后 
list[1:-1]    #从1开始取到倒数第二值
list[1:-1:1]   #从左到右一个一个去取,取到倒数第二值
list[1::2]     #左到右隔一个去取
list[3::-1]     #从3开始从右到左一个一个取,注意索引值不变

2.增  添加:

2.1:append()

append(“str”)将数据插到最后一个位置

代码示例:

list = ["one","two",3,4,"five"]
list.append("six")
print(list)

输出结果:

['one', 'two', 3, 4, 'five', 'six']

2.2:insert()

根据索引值位置将数据插入到任意一个位置

代码示例:

list = ["one","two",3,4,"five"]
list.insert(2,"two2")     #想把新对象插在什么位置就输入相应的索引值
print(list)

输出结果:

['one', 'two', 'two2', 3, 4, 'five']

3.改  修改:

想要修改首先得用切片把相应的值取出来,在进行赋值即可。

代码示例:

list = ["one","two",3,4,"five"]
list[1] = 2   #将索引值为1的对象取出来,再重新赋值
print(list)

输出结果:

['one', 2, 3, 4, 'five']

需求:将list = ["one","two",3,4,"five"]这个列表里的two 和 4 修改成 2 和 four

代码示例:

list = ["one","two",3,4,"five"]
list[1:4:2] = [2,"four"]
print(list)

输出结果:

['one', 2, 3, 'four', 'five']

注意:再list[1:4:2] = [2,"four"]中,因为list[1:4:2]输出得是一个列表,所以等号右边也必须是个列表

4.删除:

4.1:remove

remove只能删除一个,并且()里填写的是对象内容

代码示例:

list = ["one","two",3,4,"five"]
list.remove("two")     #删除two
print(list)

输出结果:

['one', 3, 4, 'five']

4.2 :pop 

pop删除的时候()里是填写索引值,并且还可以将删除数据返回出来,如果括号里面不填索引值,即pop(),则默认删除最后一个值。

代码示例:

list = ["one","two",3,4,"five"]
list.pop(1)    #删除 two
print(list)

输出结果:

['one', 3, 4, 'five']

4.3 :del什么都可以删除

代码示例:

list = ["one","two",3,4,"five"]
del list[0]    #删除 one
print(list)

输出结果:

['two', 3, 4, 'five']

5.列表的其他操作

5.1 :count:计算某元素出现次数

代码示例:

list = ["one","two",3,4,"five"]
v = list.count("two")   #计算two出现的次数
print(v)

输出结果:

1

5.2:extend:用于在列表末尾一次性追加另一个序列中的多个值

代码示例:

a = [1,2,3]
b = [4,5,6]
a.extend(b)     #把b加到a里面
print(a)
print(b)

输出结果:

[1, 2, 3, 4, 5, 6]

[4, 5, 6]

5.3:index根据内容找位置,输出得是第一个匹配内容的索引位置

代码示例:

list = ["one","two",3,4,"five"]
T = list.index("five")      #查找five的索引值
print(T)

输出结果:

4

5.4 合集

1.reverse:用于反向列表中元素

2.sort:对原列表进行排序

reverse -- 排序规则,reverse = True 降序(由大到小), reverse = False 升序(由小到大)(默认)

3.in:查一个数据在不在列表内

4.type:身份判断:判断一个对象是不是列表

代码示例:

list0 = ["one","two",str(3),str(4),"five"]
list0.reverse()             #反向列表中元素
print(list0)
list0.sort(reverse=True)    #由大到小de对原列表进行排序
print(list0)
a = "six" in list0         #判单six在不在列表里
print(a)
b = type(list0) is list     #判断list0是不是列表
print(b)

输出结果:

['five', '4', '3', 'two', 'one']

['two', 'one', 'five', '4', '3']

False

True

6.列表练习题:

list = ["one","two",3,4,"five","天才"]

把list列表中的天才的 天 改成 蠢

代码示例:

list = ["one","two",3,4,"five","天才"]
v = list[5].replace("","")
list[5] = v
print(list)

输出结果:

['one', 'two', 3, 4, 'five', '蠢才']

注意:字符串不能通过索引值修改,只能通过索引值取出来。(⬇)

七.元组

Python 的元组与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

1. 创建空元组

tup1 = ()

2. 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用:

代码示例:

tup1 = (50)
print(type(tup1))     # 不加逗号,类型为整型
tup1 = (50,)
print(type(tup1))     # 加上逗号,类型为元组

输出结果:

<class 'int'>

<class 'tuple'>

3. 元组可以使用下标索引来访问元组中的值

4. 可以对元组进行连接组合

5.元组可以计算长度len()

6.元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组

7.重点:

元组的儿子不能修改,但是孙子可以,元组的元素不能修改,但是元组的元素的元素是可以修改的。

代码示例:

tuple1 = ("one","two","three",[1,2,"zrh"],(1,2,3),"four")
tuple1[3][1] = 3
print(tuple1)
tuple1[3].append("q")
print(tuple1)

输出结果:

('one', 'two', 'three', [1, 3, 'zrh'], (1, 2, 3), 'four')

('one', 'two', 'three', [1, 3, 'zrh', 'q'], (1, 2, 3), 'four')

八.字典

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对,用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中

键必须是唯一的,但值则不必。

值可以取任何数据类型,但键必须是不可变类型。

不可变类型:整型、字符串、元组

可变类型:字典、列表

格式:变量名 = {键:值,键:值}

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}

1.:dict.get

1.1通过键取值

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}
print(dict1.get("name"))

输出结果:

zrh

1.2()参数,如果键不存在,就用后面的结果当作默认值。

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}
print(dict1.get("key",999))

输出结果:

999

2.:dict.keys() 、 dict.values() and dict.items()

经常和for循环一起使用

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}
a = dict1.keys()         #查看所有键
print(type(a))           #查看a的类型
print(a)
print(dict1.values())    #查看所有值
print(dict1.items())     #查看所有键值对

输出结果:

<class 'dict_keys'>

dict_keys(['name', 'age', 'height'])

dict_values(['zrh', 20, 75])

dict_items([('name', 'zrh'), ('age', 20), ('height', 75)])

3.增加键值对

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}
dict1["hobby"] = "eat"
print(dict1)

输出结果:

{'name': 'zrh', 'age': 20, 'height': 75, 'hobby': 'eat'}

如果增加的键已经存在,那就是改的功能。

4.删除

代码示例:

dict1 = {
    "name":"zrh",
    "age":20,
    "height":75
}
del dict1["name"]     #删除指定键值对
print(dict1)
dict1.clear()
print(dict1)         #清空字典中所有键值对,但空字典还存在
dict2 = {
    "name":"zrh",
    "age":20,
    "height":75
}
a = dict2.pop("name")    #通过键去删除,并可以返回相应的值
print(a)
print(dict2)
b = dict2.popitem()
print(b)
print(dict2)            #随机删除一对键值对,并且返回相相应键值对

输出结果:

{'age': 20, 'height': 75}

{}

zrh

{'age': 20, 'height': 75}

('height', 75)

{'age': 20}

5.嵌套

字典里面可嵌套字典或者列表都可以,列表页都可以嵌套字典。

在修改时,遇到字典用键,遇到列表用索引值,然后查找出来之后赋值即可,其他操作一样,反正一句话:

遇到字典用键,遇到列表用索引值。

Guess you like

Origin www.cnblogs.com/zrh918/p/11608667.html