一.Python特点
缺点:若语言类型,变量不需要声明,会出现被改的情况。
二.编码知识
- ASCII表->GB2312(中国)-> GBK->Unicode(全世界通用)存储浪费资源-> utf-8,utf-16,utf-32等出现,用来存储unicode,utf-8 应用最广,因为它向下兼容ASCII码。
- 了解更多1,了解更多2
练习:
小写 a-z (97-122) + 数字 1-9(49-57) +大写 A-Z(65-90)
编程思想:Chr(97)不断累加,就能出来所有a-z
代码:
>>> code=97
>>> s=""
>>> for i in range(26):
s+=chr(code+i)
>>> print s
输出结果:abcdefghijklmnopqrstuvwxyz
>>> code=48
>>> type(chr(48))
<type 'str'>
>>> for i in range(10):
... s+=chr(code+i)
...
>>> print s
abcdefghijklmnopqrstuvwxyz0123456789
变形练习1:acegikmoqsuwy
方法1:
>>> a=""
>>> code=97
>>> for i in range(0,26,2)
SyntaxError: invalid syntax
>>> for i in range(0,26,2):
a+=chr(code+i)
>>> print a
acegikmoqsuwy
>>>
方法2:
>>> code=95
>>> s=""
>>> for i in range(13):
... code=code+2
... s+=chr(code)
...
>>> print s
acegikmoqsuwy
>>>
思想:>>> chr(97) chr(99) chr(101) chr(103)......13)
变形练习2:倒着输出z-a
>>> code=124
>>> s=""
>>> for i in range(13)
SyntaxError: invalid syntax
>>> for i in range(13):
code=code-2
s+=chr(code)
>>> print s
zxvtrpnljhfdb
>>>
>>> code = 123
>>> s=""
>>> for i in range(26):
code=code-1
s+=chr(code)
>>> print s
变形练习2:AaBbCc…..Zz
>>> s=""
>>> for i in range(26):
... s+=chr(ord("A")+i)+chr(ord("a")+i)
...
>>> print s
输出结果:AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
变形练习3:练习:az,by,cx........mn
>>> s=""
>>> for i in range(26):
s+=chr(ord("a")+i)+chr(ord("z")-i)
>>> print s
输出结果:azbycxdwevfugthsirjqkplomnnmolpkqjrishtgufvewdxcybza
>>> s=""
>>> for i in range(13):
s+=chr(ord("a")+i)+chr(ord("z")-i)
>>> print s
输出结果:azbycxdwevfugthsirjqkplomn
变形练习4:字符串加密a->d,d->g,z-c。输入一个字母a,返回是字母d
北京-吴晓华(875821166) 15:20:39
算法:
1 获取当前字母的对应ascii 号码,比如x
2 然后x+3就可以获取加密后的字母ascii 号码
3 如果x+3大于123,则计算差值,从96+3,算出来字符
方法1:
#encoding=utf-8
s="abcxyz"
encoded_result=""
for i in s:
if 122-ord(i)>=3:
encoded_result+=chr(ord(i)+3)
else:
encoded_result+=chr(ord("a")+2-(122-ord(i)))
print encoded_result
方法2:
北京-吴晓华(875821166) 15:46:26
#encoding=utf-8
s="abcxyz"
encoded_result=""
for i in s:
if 122-ord(i)>=3:
encoded_result+=chr(ord(i)+3)
else:
encoded_result+=chr(96+3-(122-ord(i)))
print encoded_result
#x(120)--->a(97) 122-120=2 a1 x:y z,差1个,a
#y(121)--->b(98) 122-121=1 b2 y:z,差2个,b
#z(122)---->c(99) 122-122=0 c3 z:0 差3个,c
变形练习5:作业 解密
Python代码中的中文问题
三个步骤:
- 保存时选择utf-8
- 文件第一行 #encoding声明编码
- 加u
练习:输出一个中文
文件模式下:存成 ANSI等于GBK,建议使用utf-8
C:Usersqiwenqing>E:
E:>python 1.py
输出结果:小七七
扩展:
Mac查询文件什么编码格式的方法:
新建文件:touch t.py
然后查看文件是什么编码格式
1)vim t.py
2)按“:”
3):set fileencoding
- 字节型字符串和unicode型字符串
>>> print type("s")
<type 'str'>
>>> print type(u"s")
<type 'unicode'>
>>> "s" is u"s"
False
>>> #encoding=utf-8
... print type("s")
<type 'str'>
>>> print type("s"+u"a")
<type 'unicode'>
>>>
在内存中存储的是unicode,在文件中多存为utf-8.
decode作用是把某种编码转为unicode。
encode作用是把uincode转为其他编码。
Unicode是个中间状态,任何形式转码都要用到它。
unicode是中间状态,任何编码互转,都要借助unicode作为中转
gbk编码的字符串转为utf-8编码:s.decode("gbk")->unicode-》s.encode("utf-8")
文件有编码设定,向文件写入字符串的时候,需要将字符串编码为与文件
一致的编码后,在进行写入,否则会出现乱码
chardet.detect(不知道编码格式的字符串)
decode("现在需要解码字符的编码格式")-》unicode-》encode("你想转成的编码格式")
转码练习1:
#encoding=utf-8
s = "byte string"
print type(s)
#str to unicode
u = s.decode()
print type(u)
#uncode to str
backToBytes = u.encode()
print type(backToBytes)
输出结果:
转码练习2:声明一个中文unicode,然后编码为utf-8写入文件中,看文件中的编码是什么/ 声明一个中文unicode,然后编码为 GBK写入文件中,看文件中的编码是什么?
#encoding=utf-8
s=u"自己跑没搞过IP!!!!!!!!!!1"
with open("e:\a.txt","w") as fp:
fp.write(s.encode("gbk"))
声明为什么,文件就存储为什么了。
#encoding=utf-8
s=u"自己跑没搞过IP!!!!!!!!!!1"
with open("e:\b.txt","w") as fp:
fp.write(s.encode("utf-8"))
- 安装chardet软件。
- 如何判断是否为unicode
>>> isinstance("a",str)
True
>>> isinstance(u"a",str)
False
>>> isinstance(u"a",unicode)
True
>>> isinstance(u"a",(unicode,str))
True
>>>
- 获取系统编码
#-*- coding: UTF-8 -*-
import sys
print sys.getdefaultencoding()
输出结果:
- 更改系统编码
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
- #-*- coding: UTF-8 -*-
import sys
reload(sys) #去掉此行和下行,则程序出错
sys.setdefaultencoding('UTF-8')
print sys.getdefaultencoding()
print type(u"我")
print type(u"我".encode('utf-8'))
print type(u"我".decode())
#”我”是str类型,encode前,python自动用默认编码进行decode为unicode类型,默认为
ascii,则无法decode
print u"我".encode('utf-8').decode() #会调用默认的编码进行decode在进行encode
(‘utf-8’)
- Help(‘print’)+dir(math) 查函数怎么用。
- 常量只能被赋值一次
- 逻辑值,与或非
- and: 与,中文并且的意思,2个值同为true才可以得出值是True
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False
or:或,中文或者的意思,只要有一个true,结果就是true
>>> True or True
True
>>> True or Fasle
True
>>> False or True
True
>>> False or False
False
非:中文意思取反
>> not True
False
>> not False
True
>>> type(True)
<type 'bool'>
>>> isinstance(True,bool)
True
>>> isinstance(False,bool)
True - 变量
- 删除了内存中的变量,值不一定被删除了,有可能其他变量也是此值。
- 一直往内存中存入不删除,内存一直处于被占用的状态,会出现内存泄漏。测试内存泄漏:需要长时间的压测,长时间的运行它,周五晚到周一早上。App用monkey。
- 变量命名:字母、数字、下划线 但数字不能作为开头。 变量是区分大小写的。一般数据库是不区分大小写的。
- 内置函数不能作为变量,尽量不要使用下划线作为变量的开头。
- His-name 中横线不能用。
- 数据类型
- Numbers 数字:
>>> a=1
>>> type(a)
<type 'int'>
>>> a=1.1
>>> type(a)
<type 'float'>
>>> a=1.2l
SyntaxError: invalid syntax
>>> a=1l
>>> type(a)
<type 'long'>
>>> a=1+1j
>>> type(a)
<type 'complex'>
>>> a=1e5 #科学计数法,1乘以10的5次方
>>> type(a)
<type 'float'>
>>> a=1e-5 #科学计数法,1乘以10的-5次方
>>> type(a)
<type 'float'>
- 字符串 String
Str是内置函数,尽可能不要用保留字命名,否则会影响内置函数的调用。
>>> str="abd"
>>> print str
abd
>>> str(a)
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
str(a)
TypeError: 'str' object is not callable
>>> del str
>>> str(a)
'1e-05'
调试步骤: 1.看下报错的是哪一行;2.查一下报错的是什么意思;3.报错行无错误要看其上下行。
>>> s="ab"
>>> type(s)
<type 'str'>
>>> type(u"a")
<type 'unicode'>
补充:Unicode的英文可以decode,中文不可以。
Cmd窗口支持的是jbk,无法显示utf-8.unicode可以自动转化为jbk显示。
>>> type(a)
<type 'unicode'>
>>> a=u"中国".decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> import sys
>>> sys.getdefaultencoding
<built-in function getdefaultencoding>
>>> sys.getdefaultencoding()
'ascii'
>>> a=u"中国".encode("utf-8")
>>> print a
涓�浗
>>> a=u"中国".encode("utf-8").decode("utf-8")
>>> type(a)
<type 'unicode'>
>>> print a
中国
>>> a=u"中国".encode("utf-8").decode("utf-8").encode("gbk")
>>> print a
中国
>>> type(a)
<type 'str'>
>>> import chardet
>>> chardet.detect(a)
{'confidence': 0.7679697235616183, 'encoding': 'IBM855'}
>>>
>>> a="我"
>>> print a
我
>>> type(a)
<type 'str'>
>>> chardet.detect(a)
{'confidence': 0.0, 'encoding': None}
>>> a
'xcexd2'
>>>
- 只要是str类型,只能调用decode方法,解码为uncode
2 只要是unicode,只能调用encode方法,编码为str - Gbk就是 ansi,编码是utf-8,写进去的时候就是utf-8.没有写的话默认就是ansi。
练习:统计字符串中f字母的个数
>>> f_num = 0
>>> a='qwerfdfgtf'
>>> for i in a:
... if i== 'f':
... f_num += 1
...
>>> print f_num
输出结果:3
17.列表
>>> a=[]
>>> a.append([])
>>> a
[[]]
>>> a[0].append("a")
>>> print a[0][0]
a
三维:
>>> a=[1,"a",[2,"b",[3,"c"]]]
>>> print a[0]
1
>>> print a[2]
[2, 'b', [3, 'c']]
>>> print a[2][1]
b
>>> print a[2][2][0]
3
>>> a[2][2][0]="x"
>>> print a[2][2][0]
x
>>> del a[2][2][0]
>>> print a[2][2]
['c']
- 元组
>>> a=(1,2,"a",["b",3],{1:2})
>>> type(a)
<type 'tuple'>
>>> for i in a:
... print i
...
1
2
a
['b', 3]
{1: 2}
>>> a[3][1]="qiqi"
>>> pprint a
File "<stdin>", line 1
pprint a
^
SyntaxError: invalid syntax
>>> print a
(1, 2, 'a', ['b', 'qiqi'], {1: 2})
>>>
五种数据结构的增删改查随时背着能用出来。
- 字典
>>> a={1:"qq",2:"ii"}
>>> for key,value in a.items():
... print key,":",value
...
1 : qq
2 : ii
>>> del a[1]
>>> print a
{2: 'ii'}
>>> for i in a:
... print i
...
2
>>> for value in a.values():
... print value
...
Ii
- set集合
>>> a=set([1,2,3,1,2,3,5,6])
>>> print a
set([1, 2, 3, 5, 6]) #自动过滤重复值
>>> b=set([1,9])
>>> a&b #交集
set([1])
>>> a|b #并集
set([1, 2, 3, 5, 6, 9])
>>> a-b #差集
set([2, 3, 5, 6])
>>> b-a #差集
set([9])
转化为list:
>>> x=str([1,2,3])
>>> print x
[1, 2, 3]
>>> list(x)
['[', '1', ',', ' ', '2', ',', ' ', '3', ']']
>>> eval(x)
[1, 2, 3]
判断类型,转set为list:
>>> x=str([1,2,3])
>>> print x
[1, 2, 3]
>>> list(x)
['[', '1', ',', ' ', '2', ',', ' ', '3', ']']
>>> eval(x)
[1, 2, 3]
- 变量赋值
>>> a=b=c=1
>>> print a
1
>>> b
1
>>> c
1
>>> x,y,z=1,2,'yy'
>>> x
1
>>> y
2
>>> z
'yy'
>>> t,d=(2,3)
>>> t
2
>>> d
3
两个变量直等,对于可变元素而言修改一个另一个的值变;对于不可变元素另一个的值不变。
>>> a=501
>>> b=a
>>> print a
501
>>> b
501
>>> a=300
>>> print b
501
>>>
>>>
>>> a=[1,2]
>>> b=a
>>> print a
[1, 2]
>>> print b
[1, 2]
>>> a.append(3)
>>> print b
[1, 2, 3]
原理解释:
a=501 内存501
b=a b指向内存501
a=300, 内存的300
a=[] 内存【】
b=a 内存[]
a.append("a") a,b指向同一个地址,一样变化的。
a=502
b还是指向原来的内存【】
字典:
>>> a={1:2}
>>> b=a
>>> a
{1: 2}
>>> b
{1: 2}
>>> a[2]=2
>>> a
{1: 2, 2: 2}
>>> b
{1: 2, 2: 2}
不可变类型:整数、字符串、元组
可变类型: 列表, 字典