1. Review of the content of yesterday
dictionary:
increase:
dic['name'] = 'alex' Overwrite if there is, no responsibility to add
dic.setdefault('name','wusir') is responsible for unchanged, not responsible for adding.
delete:
pop: dic.pop(key, 'no such key-value pair') has a return value, which returns the corresponding value.
popitem is randomly deleted. Has a return value.
clear clears the dictionary.
the
del dic['name']
del dic At the memory level, deletes the dictionary.
change:
dic['name'] = 'alex' Overwrite if there is, no responsibility to add
update adds all key-value pairs from one dictionary, overwriting, to another dictionary.
dic2.update(dic1)
check:
dic['name']
dic.get('name1') returns None by default without this key, and the return value can be set.
for loop.
dic.keys() dic.values() dic.items() All return data types similar to lists, which can be traversed without indexes. list(dic.keys())
3, is == id small data pool
is whether the memory address is the same
assignment operator == compares the same value
name1 = 'alex'
name2 = 'alex'
print(name1 == name2)
id Query the memory address.
Small data pool. Int str is within a certain range, if the two values are the same, in order to save memory, share a memory address.
int -5 ~ 256
str: 1, if there are non-letter elements, it is not a small data pool.
2, A single letter *int (21) exists in a small data pool. The rest of the types do not exist.
4, code two
ASCII: One character is represented by 1 byte (8 bits), special characters of numbers and letters.
unicode: Universal code
A character is represented by four bytes"
A:0000 0001 0000 0001 0000 0001 0000 0001
Medium: 0000 0001 0000 0001 0000 1001 0000 0001
utf-8:
a: 0000 0001
Europe: 0000 0001 0100 0001
Asia: 0000 0001 0000 0001 1100 0001
gbk: national standard
a:0001 0001
Medium: 0000 0001 0000 0001
1. The binary codes between different codebooks cannot identify each other, and it is easy to report errors or generate garbled characters.
2. The file storage and transmission of the computer are all 0101010 (gbk, utf-8, ascii, gb2312, etc.) cannot be unicode.
with open('02 作业讲解.py',encoding='utf-8') as f1:
print(f1.read(),type(f1.read()))
name = "alex"
name1 = b"alex"
print(name,type(name))
print(name1,type(name1))
name = "中国".encode('utf-8')
name1 = b"alex"
print(name,type(name))
print(name1,type(name1))
str ---> bytes
s1 = '中国'
b1 = s1.encode('utf-8') # encode 编码
# b2 = s1.encode('gbk')
# print(b1)
# print(b2)
s2 = b1.decode('utf-8') # decode 解码
print(s2)
utf-8 bytes ---> gbk bytes
s1 = b"alex"
print(s1.capitalize())
Major premise: python3x, coding.
type of data:
int
str
bytes : all methods that str has, bytes have.
bool
list
tuple
dict
set
The encoding method in python3x memory is unicode
English:
str: representation name = "alex"
Internal encoding: unicode
bytes: representation: name1 = b"alex"
Internal encoding: non-unicode
Chinese:
str: expression name = "China"
Internal encoding: unicode
bytes: representation: b'\xe4\xb8\xad\xe5\x9b\xbd'
Internal encoding: non-unicode