Python study notes --- second week

1. Integer
symbol: int
use: for calculating and comparing
to python3: are all integers
python2: Integer, Long Long
123-- Decimal Binary transfer method: 2 decimal addition, modulo the number of rows from the bottom up ; print (bin (decimal))
10101-- binary decimal: 1 * 2 ** 0 + 0 + 1 * 1 * 2 * 2 * 2 * 2 + 0 + 1 * 2 ** 3 ** 4 print (int ( "binary", 2))
2. Boolean
integer Boolean turn == print (bool (digital)), as long as the integer is nonzero True
Boolean transfected integer == print (int (True / False)), Boolean True = 1, False = 0
string Boolean turn == print (bool ( "") ), unless the contents of the string is False
Boolean to string == print (str (True / False))
3. string
python, as long as it is a quoted string
role: for storing small amounts of data
strings in each letter or character element is referred
3.1 index (subscript)
symbol: a [ index position] - the index position can not exceed the maximum index

from left to right row, row start from 0; row from right to left, starting from -1 rows
3.2 slice
the result is the original data itself
print (a [start position: termination ]) - care regardless of the end (start position, end position is not included)
Index value may exceed the index position, the first default starting position is a position when the index is not written, a terminating position of the last

3.3 step
print (a [1: 2: 1])
The default is one, determines the direction of search steps , once for each length determining look (the step is positive: from left to right; negative steps: right to left)
[:: - 1] - string reversing
the direction towards the starting position It does not point to the end position, takes something less than
3.4 methods string
string data type is immutable, ordered
a.upper () - all uppercase
a.lower () - all lowercase
a.startswith ( "content" ) - starts with, returns a Boolean value print ( "", 1,2) - 1 ( starting position) 2 (end position)
a.endswith () - to what end, returns a Boolean value
a.count ( "") - the number of elements of the statistical occurrence of
a.strip () - default off head and tail ends of the space, newline \ n-, tab \ t a.strip ( "off content") - - removing the head and tail ends of the specified content, if there are two specified elements, each element is removed again, together again removed again
a.split () - segmentation, segmentation default spaces, carriage returns, tabs, a .split ( "", 2) --2 ( the number of times divided, from left to right)
a.replace ( "content to be replaced," "Replaced") - Replace all a.replace ( "the replace contents "," replace contents "

name = "{} year {}" format ( "", "") -. filled sequentially
name = "{1} year {0}" format ( "" , "") -. filled by index
name = "{name} year {age}" format (name = "", age = "") -. be filled by the name of
3.6 is series
for determining
a.isdigit () - the contents of the string is determined not all numbers (digits), returns a Boolean value
a.isdecimal () - judgment is not a decimal number
a.isalnum () - is determined not alphanumeric, Chinese
a.isalpha () - is determined not letters, Chinese
4.for cycle
format: for i in a:

Indent Print (i)
for - Keyword
i - variable name (can be freely modified)
in - Keyword
a - iterables (python data types except int, and Boolean values, the rest could iteration)
for cycle when the cycle has been assigned


... -- 和pass功能一样

5.列表
符号:list
数据类型之一,存储大量的不同类型的数据,别的语言称为数组
列表是一种有序的容器,支持索引
列表是一种可变数据类型 -- 原地修改
字符串中只要占一个位置就是一个元素,列表中只要是逗号分隔的都是元素
格式lst = [" "," "] -- (用逗号隔开)

5.1 列表的增加
lst.append() --追加,在最末尾的地方进行添加

lst.insert(1," ") -- 插入 1(索引位置,插在索引位置前) ----尽量少用,会导致效率变低

lst.extend() -- 迭代添加

5.2 列表的删除
lst.remove() -- 按元素的名字删除,只能删除第一个,从左往右删除

lst.pop() -- 弹出,默认删除最后一个,并且具有返回值,返回值为删除内容

lst.pop(1)-- 按索引删除,返回值字符串不带引号,repr()查看当前数据的原生态

lst.clear() -- 清空
del lst[1] -- 按索引删除
del lst[1:2] -- 按切片删除
del lst[1:2:1] -- 按步长删除
5.3 列表的修改
lst[2] = 80 --通过索引进行修改 2(索引位置) 80(替换的内容)

lst[1:2] -- 通过切片进行修改,默认步长为一,修改的内容必须是可迭代的对象,修改的内容可多可少,不能空(多的时候扩展位置,少的时候自动补位)

lst[1:2:2] -- 步长不为一的时候,被替换的内容和替换的内容数量要一致

5.4 列表的查找
for循环
索引
5.5 列表的嵌套
一层一层查找,[ ]-- 代表一个元素
6.元组
符号:tuple
格式:tu=(" ", " ")
python数据类型之一,有序的,不可变,相当于不可变的列表
只支持查:for循环
元组的方法:
统计 -- tu.count()

获取索引 -- tu.index() 通过元素查询索引

元组的用处:在配置文件中使用
元组的嵌套

7.范围
符号:range
range的诞生是为了解决不能循环数字
range是一个可迭代对象
print(range(1,10) )-- python3中打印range是自己本身

print range(1,10) --python2中打印range是列表
range(10) -- 10代表终止位置,起始位置默认为零,顾头不顾尾
range(1,10,1) -- 步长默认为一
8.字典
python数据结构之一
符号:dict
格式:dic={"key":"value"} -- 键值对 键必须是不可变的数据类型(可哈希),且唯一;值任意
字典是可变数据类型,无序的,所有操作都是通过键
8.1 字典的增加
dic["键"]="值" -- 添加键值对 暴力添加

dic.setdefault("键","值") -- 无则添加,有则不添加 内置步骤:1.先查看键是否在字典2.不存在的时候进行添加

8.2 字典的删除
dic.pop("键") -- 通过键删除,具有返回值,返回的是被删除的值

dic.clear() -- 清空
del dic -- 删除整个字典
del dic["键"] -- 通过键进行删除

8.3 字典的改
dic["键"]="值",有则覆盖,没有就添加

dic.update(字典) --括号里的字典级别高于原字典

8.3 字典的查找
dic.get("键") -- 通过键获取值 ,只有键没有值时查询不到返回None;有键值对时返回值

dic.setdefault("键") -- 通过键获取值,查询不到返回None

dic["键"] -- 通过键获取值,查询不到就报错
dic.keys() -- 获取到的是高仿列表---键
dic.values() -- 获取到的是高仿列表---值
高仿列表支持迭代,不支持索引
list(dic.keys())--可转换成列表
通过for循环查
for i in dic
print(i) -- 查看所有的键

print(dic.get(i)) -- 查看所有的值

print(list(dic.items())) -- 查看所有键值对

for i in dic.items(): print(i[0],i[1])

8.4 字典的嵌套
嵌套查找时按照键一层一层找
常用数据类型:字典,列表,字符串,数字
9.解构
作用:省去使用下标


解构可用于元组,列表,字符串,字典,但应注意等号两边的元素个数要一致

for a,b in dic.items()

10.小数据池
== 判断两个值是否相等,is 是 -- 判断两边的内存地址是否相同
id()查看内存地址
小数据池支持的类型:int,str,bool
int: -5~256(范围)
str: 字母,数字 长度任意,符合驻留机制
规则:字符串(字母,数字)进行乘法时,总长度不能超过20
特殊符号(中文和符号)进行乘法的时候只能乘以0
11.代码块
一个py文件,一个函数,一个模块,终端(cmd)中每一行都是代码块
支持:int,str,bool
字符串:定义字符串的时候可以是任意的
规则:字符串(字母,数字)进行乘法时,总长度不能超过20
特殊符号(中文和符号)进行乘法的时候只能乘以0或者1
小数据池和代码块同在的时候先执行代码块
驻留机制:目的:节省内存空间,提升效率(减少了开辟空间和销毁空间的耗时)
12.集合
符号:set
格式:s={1,2,3,4}
集合就是一个没有值的字典 无序,可变,集合的元素是不可变的
天然去重:输出的元素没有重复的
集合的增
s=set() -- 空集合
s.add() -- 整个添加
s.update()/set() -- 迭代添加
set(" ") -- 迭代添加
删除
s.remove() -- 通过元素删除
s.clear() -- 清空
s.pop() --随机删除(最小的)

先删后加

for 循环
其他操作
s-s1 --差集

s&s1 --交集

s|s1 --并集

s^s1 -- 反交集

s>s1 -- 判断s1是不是s的子集
s<s1 -- 判断s1是不是s的父集(超集)
frozenset({1,2,3}) -- 冻结集合(变成不可变的)
13.深浅拷贝
格式:浅拷贝a[:]/a.copy() 深拷贝b=copy.deepcopy(a)
浅拷贝:
只拷贝第一层元素,只有修改第一层(不可变)的时候源数据不受影响
给可变数据类型添加的时候源数据会受影响
深拷贝
import copy (导入模板)
深拷贝:不可变数据类型共用,可变数据类型新开辟一个空间
赋值:将多个变量名指向同一个内存地址
14.基础数据类型补充
str
a.capitalize() -- 首字母大写
a.title() --每个单词首字母大写
a.swapcase --大小写转换
a.center(长度) -- 居中
a.center(长度,"填充内容") -- 填充
a.find("") -- 通过元素查找索引,查找不到返回-1
a.index("") -- 通过元素查找索引,查找不到报错
"拼接的元素".join([1,2,3]) -- 拼接,将列表转成字符串
字符串进行加和乘时都是开辟新空间
list
list("12345") -- 列表的另外一种定义
lst.index() --通过元素查找索引
lst.sort() -- 排序(默认升序)
lst.sort(reverse=True) -- 降序
lst.reverse() -- 反转




列表在进行乘法的时候,元素是共用的
元组(tuple)
tu=(1) 源数据类型
tu1=("alex") 源数据类型
tu2=(1,) 元组
tu3=() 元组

dict
dic.popitem() 随机删除 python3.6版删除最后一个键值对
popitem返回的是被删除的键值对
dic.fromkeys("abc",[]) # 批量创建键值对


fromkeys 第一个参数必须是可迭代对象,会将可迭代对象进行迭代,成为字典的键.第二个参数是值(这个值是共用的)
fromkeys 共用的值是可变数据类型就会有坑,不可变数据类型就没事
15.总结
可变,不可变,有序,无序
可变:list,dict,set
不可变:int,str,bool,tuple
有序:list,tuple,str
无序:dict,set
取值方式
索引:list,tuple,str
键:dict
直接:int,bool,set
数据类型转换
str -- int
int -- str
str -- bool
bool -- str
int -- bool
bool -- int
list -- tuple: list(tu)
tuple -- list: tuple(list)
list -- set: list(s)
set -- list
set -- tuple
tuple -- set
***重要:list -- str

str -- list

目前字典转换,自己实现方法
16.以后会遇到的坑
lst = [1,2,3]
for i in lst:
lst.append(4)
print(lst)
死循环,在加4的同时列表lst会一直增加

lst = [11,22,33,44]
for i in lst:
    lst.remove(i)
print(lst)
这段程序的目的是把列表里的数据全部删除,但列表和for具有自动补位,删掉11之后会22会补到11的位置,下次再删就是33,22和    44会跳过去

lst = [11,22,33,44]
for i in lst:
    del lst[-1]
print(lst)
这段程序从后面删除,删除到33的时候,程序会结束,会留下11和22

lst = [11,22,33,44]
for i in range(len(lst)):
   del lst[-1]
print(lst)
借用列表的长度循环,可以实现

for i in range(len(lst)):
   lst.pop()
print(lst)
同上,可以删空列表

lst = [11,22,33,44]
lst1 = lst[:]
for i in lst1:
    lst.remove(i)
print(lst)
拷贝lst的元素,通过循环lst1的长度,删空lst

使用for循环清空列表元素内容
    1.从后向前删除 2.创建一个新的容器,循环新的容器删除旧的容器内容
面试题
    lst = [1,[2]]
    lst[1] = lst
    print(lst)
    答案: [1, [...]]  循环
字典和集合
    字典和集合在遍历(循环)时不能修改原来的大小(字典的长度),可以进行修改值
    ![](https://img2018.cnblogs.com/blog/1754444/201908/1754444-20190810165752876-933593647.png)

17.二次编码
编码
ascii码:支持英文,数字,符号,不支持中文,英文一个字节
GBK(国标):支持中文,英文,数字,符号,英文一个字节,中文2个字节
Unicode(万国码):支持中文,英文,数字,符号,都是4个字节
utf-8:支持中文,英文,数字,符号,英文1个字节,欧洲2个字节,亚洲3个字节
字节用于存储和传输
a.encode("gbk/utf-8") -- 编码
a.decode("gbk/utf-8") -- 解码
必会:
python3:默认编码unicode
python2:默认编码ascii
python2不支持中文
重要
encode() # 编码
decode() # 解码
用什么编码就要用什么解码
网络传输一定是字节

Guess you like

Origin www.cnblogs.com/sunyongchao/p/11332776.html