Coding:
1,is = =
= = the value is compared.
is memory address.
for int STR: There is a concept of a small data pool.
Why is there a small data pool? Save memory.
INT-5 ~ 256
STR all letters, numbers and a single letter want to multiply the number by no more than 20.
Other data types: No concept of small data pools,
L1 = [1] L2 = [1]
Big environment: Python3:
PYTHON3: encoded Unicode in STR memory
What if I want to store, or transfer strings (files) directly to STR? No
Workaround: The bytes type has occurred.
For English:
STR: representation: s = ' Alex '
Memory Encoding: Unicode
Bytes: representation: s = B ' Alex '
Memory Encoding: Non-Unicode (can be utf-8,gbk,gb2312 ...)
For Chinese:
STR: representation: s = ' China '
Memory Encoding: Unicode
Bytes: expression form: s = B ' xe3\xf2\ ... '
Memory Encoding: Non-Unicode (can be utf-8,gbk,gb2312 ...)
You want to store, transfer a file, or string: str---> bytes
Str.encode (' utf-8 ')----bytes
Content supplement: int, str, tuple:
#int
# str
s = ' #只能是以至少一个空格组成的字符串 (all spaces)
# Print (S.isspace ())
#tuple
# tu = (1)
# TU1 = (1,)
# Print (Tu,type (TU))
# Print (Tu1,type (TU1))
# tu = (' Alex ')
# TU1 = (' Alex ',)
# Print (Tu,type (TU))
# Print (Tu1,type (TU1))
Tu = ([+/-)]
TU1 = ([+],)
Print (Tu,type (TU))
Print (Tu1,type (TU1))
#当元组只有一个元素组成并且没有 ', '
# What is the data type of the element, and what data type is the whole?
Tu.append (' 333 ')
Print (TU)
List, Dict:
#list
L1 = [111,222,333,444,555]
# for I in range (len (L1)):
# # i = 0 i = 1 i = 2 i = 3
# print (L1)
# del L1[i]
# print (L1) # [222,333,444,555] [222,444,555] [222,444]
# print (L1)
# L1 = [111,222,333,444,555] Deletes an element indexed as an odd digit.
# L1.pop (222)
# L1.remove (1)
# Method One:
# L1 = [111,222,333,444,555]
# L2 = []
# for I in range (len (L1)):
# if I% 2 = = 0:
# l2.append (L1[i])
# L1 = L2
# print (L1)
#方法二:
# L1 = [111,222,333,444,555]
# for I in range (1,len (L1)):
# If I <= 2:
# L1.pop (i)
# print (L1)
#方法二:
# L1 = [111,222,333,444,555,666,777]
# #
# del L1[1::2]
# print (L1)
# L1 = [111,222,333,444,555]
# for I in range (len (L1) -1,0,-1):
# if I% 2 = = 1:
# del L1[i]
# print (L1)
#在循环一个列表时, if you delete some or some kind of element, you are prone to error.
#fromkeys ()
# dic = Dict.fromkeys (' abc ', ' Alex ')
# Print (DIC)
# Dic1 = Dict.fromkeys ([1,2,3],[])
# Print (DIC1)
# dic1[1].append (' Alex ')
# Print (DIC1)
DiC = {' K1 ': ' Alex ', ' K2 ': ' Taibai ', ' K3 ': ' Day ', ' name ': ' Wusir '}
#不可变的数据类型: Can be hashed
# for I in DIC:
# if ' K ' in I:
# del Dic[i]
# L1 = []
# for I in DIC:
# if ' K ' in I:
# l1.append (i)
# print (L1)
#
# for K in L1:
# del Dic[k]
# Print (DIC)
#在循环一个字典时, you may get an error if you delete some key-value pairs.
#数据类型之间的转化:
#int <---> str
#int <---> bool
#str <--->.bool
# split
#str <------> List
# Join
# tuple <---> list
# L1 = [+]
# tu = tuple (L1)
# L2 = List (TU)
# Print (TU,L2)
#dict专属类型: Conversion with List
# dic = {' K1 ': ' Alex ', ' K2 ': ' Taibai ', ' K3 ': ' Day ', ' name ': ' Wusir '}
# Print (List (Dic.keys ()))
# Print (List (dic.values ()))
# Print (List (Dic.items ()))
Depth copy:
# L1 = [1,2,3,4]
# L2 = L1
# L1.append (5)
# Print (L1,L2)
#对于赋值运算, it is common to point to a memory address.
# S1 = ' Alex '
# s2 = S1
# S1 = ' Alex2 '
# Print (S1,id (S1))
# Print (S2,id (S2))
#copy
# L1 = [+]
# L2 = L1.copy ()
# l1.append (666)
# Print (L1,id (L1))
# Print (L2,id (L2))
# L1 = [1,[22,33],2,3]
# L2 = L1.copy ()
# l1.append (666)
# Print (L1,id (L1))
# Print (L2,id (L2))
# L1 = [1,[22,33,],2,3]
# L2 = L1.copy ()
# l1[1].append (666)
# Print (L1,id (L1), id (l1[1]))
# Print (L2,id (L2), ID (l2[1]))
#对于浅copy来说, the first layer is a separate memory address, starting from the second layer, all point to the same memory address, a change is changed.
# import Copy
#
# L1 = [1,[22,33,],2,3]
# L2 = copy.deepcopy (L1)
# l1.append (777)
# l1[1].append (666)
# Print (L1,id (L1), id (l1[1]))
# Print (L2,id (L2), ID (l2[1]))
#对于深copy, regardless of the number of layers, in memory are two separate memory addresses.
L1 = [1,[1,2],2,3]
L2 = l1[:] # is a shallow copy
L1[1].append (111)
Print (L1,L2)
Python Basics 2