赞
踩
作为运维,我们是离不开Python的,所以学习Python是当务之急,那下面我们就来总结一下吧。
我演示下使用脚本的方式做网站:
#! /usr/bin/python import socket def handle_request(client): buf=client.recv(1024) print(buf) msg="HTTP/1.1200OK\r\n\r\n" client.send(('%s'%msg).encode()) msg="Hello,World!" client.send(('%s'%msg).encode()) def main(): ip_port=("自己的IP",80) sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind(ip_port) sock.listen(5) while True: conn,addr=sock.accept() handle_request(conn) conn.close() if __name__ == "__main__": main()
这样就可以在浏览器中通过访问自己的IP地址来进行访问网站啦。
Python中文会乱码
常见的那些编码格式:
unicode 2-4 位的字节
全部涵盖所有字符
windows nt 开始 就支持unicode 的编码格式
unicode 的问题:在互联网的时代下,8位,16,32 位 中间用0表达
utf -8 :英文在 uft-8 编码下 和ASCII 是一致的
GB 是3个字节
是最流行的对unicode 进行存储和传播的最好的编码范式
python3 默认的编码格式就是unicode
1: str 在内存中显示给我们看的(unicode)
2: bytes 在存储和传输的过程中使用的,(省空间 utf-8 编码)
装换的过程:
明文str 和字节 (bytes) 数据之间关系的转换,就是编码和解码的过程
str 类型 存储unicode 的数据,向我们人能理解的数据,就是明文
byte 是计算机识别的用于存储和网络间的传输
str—>byte 编码 encode()
byte----str 解码 decode()
各个编码之间的转换
读取文件编解码的过程
小问题:
写文件 使用utf-8 编码
读文件 gbk的解码方式读取看什么效果 ?
---------报错的效果如下----改为相同的编码即可
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa4 in position 4: illegal multibyte sequence
python 3 中的申请的变量称为标识符 import
not if for else import 这些
单行是#
使用help 查看帮助信息(保留原始的状态)
'''
this is get www.baidu.com values
'''
def get_value():
'''
this is get www.baidu.com values
'''
return
help(get_value)
作用域是通过对齐进行表示的,类似其它高级语句{}
缩进是代表的相同的作用域,相同的功能的代码放在一起
python 解释器 一行,一行 进行解释的
原则:
所以顶格的首先进行解释,依次继续解释(按照行进行)
然后才是作用域下面的
缩进原则上是4个空格,但是只要对齐即可 ,做好不要TAB键 ,使用空格
“\” 进行连接,但是 在[],{},() 中,不要反斜线,只要逗号隔开即可。
python 常用的变量的类型
数字:
int: 33
bool: True
float: 1.23
complex: 1+2j
引号:单引号
双引号和单引号使用是相同,当变量中有单引号,我们使用双引号,当变量当中有双引号,我们使用单引号
三个双引号和单引号,表示的多行的注释
转移符 ‘\’
字符可以使用+连接
pyhton 字符变量是不可更改
字符的截取是:变量[头下标:尾下标:步长],其中的下标是索引,左到有右是0开始,
从右到左-1 开始的,头下标:尾下标 左闭,右开
如果 【:】 前后没有值,默认 :左边表示是头(0), 默认:右边的值就尾
str=‘www.baidu.com’
他的索引值为:
0 1 2 3 4 5 6 7 8 9 10 11 12
反过来是这样的:
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
注意:
[:-4:-2](步长是-值):默认 :左边表示是尾(0), 默认:右边的值就头
import 和 from … import :
都是导入响应模块
import 是导入全部的模块
from … import 是模块中的某个函数,变量
from sys import *
import sys
print 函数()
print([输出项1,输出项2,输出项3…输出项n][sep=分割符][end]=结束符默认值是\n)
格式化的输出
print(“sum=%o” %123)
附件格式
m.n ,其中m 是数据占的宽带,n 代码小数所占的位数
-:输出的左对齐,默认是右对齐
+:正数
字符串的format 方法
{[序号]:格式说明}
“{}{}”.format(“hello”,“world”)
In [18]: def func(): ...: ''' ...: this is function help ...: ''' ...: print("ok") ...: In [19]: help(func) In [20]: s1="www"+"rrr" + \ ...: "ttt"+"hhh" + \ ...: "hhh" In [21]: s1 Out[21]: 'wwwrrrttthhhhhh' In [22]: s1="www"+"rrr" \ ...: +"ttt"+"hhh" \ ...: +"hhh" In [23]: s1 Out[23]: 'wwwrrrttthhhhhh' In [24]: t=['rr','rr', ...: 'hh','jj', ...: 'gg','33'] In [25]: t Out[25]: ['rr', 'rr', 'hh', 'jj', 'gg', '33'] In [26]: str='www.baidu.com' In [27]: str Out[27]: 'www.baidu.com' In [28]: print(str[0:-1]) www.baidu.co In [29]: print(str[:]) www.baidu.com In [30]: print(str[:4]) www. In [31]: print(str[0:4]) www. In [32]: print(str[0:4]) www. In [33]: print(str[-1:-4]) In [34]: print(str[:-4]) www.baidu In [35]: print(str[-3:-1]) co In [36]: print(str[-7:-6]) i In [37]: print(str[-3:-6]) In [38]: x,y=2,3 In [39]: print(x,y) 2 3 In [40]: print(x,y,sep=":") 2:3 In [41]: print(x,y,sep=",") 2,3 In [42]: print(x,y,sep=",",end="%") 2,3% In [43]: print("sum=%o" %123) sum=173 In [44]: print("sum=%d" %123) sum=123 In [45]: print("sum=%i" %123) sum=123 In [46]: print("sum=%x" %123) sum=7b In [47]: print("sum=%c" %123) sum={ In [48]: print("sum=%s" %123) sum=123 In [49]: print("sum=%f" %123) sum=123.000000 In [50]: value=8.123 In [51]: print("%6.2f" %value) 8.12 In [52]: print("%6.3f" %value) 8.123 In [53]: print("%06.2f" %value) 008.12 In [54]: "{}{}".format("hello","world") Out[54]: 'helloworld' In [55]: "{0}{1}".format("hello","world") Out[55]: 'helloworld' In [56]: "{1}{0}".format("hello","world") Out[56]: 'worldhello' In [57]: "{1}{0}{1}".format("hello","world") Out[57]: 'worldhelloworld' In [58]: "{0:.2f},{1},{1}".format(3.1415926,100) Out[58]: '3.14,100,100' In [59]: "{0:5.2f},{1},{1}".format(3.1415926,100) Out[59]: ' 3.14,100,100' In [60]: "{0:5.4f},{1},{1}".format(3.1415926,100) Out[60]: '3.1416,100,100' In [61]: "{0:.4f},{1},{1}".format(3.1415926,100) Out[61]: '3.1416,100,100' In [62]: "{0},pi={x}".format("圆周率",x=3.14) Out[62]: '圆周率,pi=3.14' 字符的练习: In [1]: s1="beijing xian tianjin beijing chongqing" In [2]: s1 Out[2]: 'beijing xian tianjin beijing chongqing' In [3]: s1.find("xian") Out[3]: 8 In [4]: s1.find("beijing") Out[4]: 0 In [5]: s1.replace("beijing","1111") Out[5]: '1111 xian tianjin 1111 chongqing' In [6]: s1.replace("1111","bejing",1) Out[6]: 'beijing xian tianjin beijing chongqing' In [7]: s1.replace("beijing","2222",1) Out[7]: '2222 xian tianjin beijing chongqing' In [8]: s1.split() Out[8]: ['beijing', 'xian', 'tianjin', 'beijing', 'chongqing'] In [9]: s1.split('beijing') Out[9]: ['', ' xian tianjin ', ' chongqing'] In [10]: s2="1,2,3,4,5," In [11]: s2.split(',') Out[11]: ['1', '2', '3', '4', '5', ''] In [12]: s3=s1+s2 In [13]: s3 Out[13]: 'beijing xian tianjin beijing chongqing1,2,3,4,5,' In [14]: s3=s1+", "+s2 In [15]: s3 Out[15]: 'beijing xian tianjin beijing chongqing, 1,2,3,4,5,' In [16]: s2 Out[16]: '1,2,3,4,5,' In [17]: sep='>' In [18]: s4=sep.join(s2) In [19]: s4 Out[19]: '1>,>2>,>3>,>4>,>5>,' In [20]: s5=("hello","world") In [21]: sep="" In [22]: sep.join(s5) Out[22]: 'helloworld' In [23]: s1 Out[23]: 'beijing xian tianjin beijing chongqing' In [24]: s1.upper() Out[24]: 'BEIJING XIAN TIANJIN BEIJING CHONGQING' In [25]: s1.count("BEIJING") Out[25]: 0
[] ,是通用复合数据类型 支持,字符,数字,列表(嵌套)
[头下标:尾下标],索引 从左到右 从0 开始,从右到左 -1 开始,下标是空取头或取尾
t[“a”,“b”,“c”,“d”,“e”]
0 1 2 3 4
-5 -4 -3 -2 -1
特殊:
长度和内容都是可变的
没有长度限制,类型可以不同
所有元素放在【】中,逗号隔开即可,包含0 个多个对象引用的有序序列
列表的运算
+ 将新的列表加入到尾部
t=list[] In [38]: In [38]: t=["a","b","c","d","e"] In [39]: t1=[:] File "<ipython-input-39-12177964a025>", line 1 t1=[:] ^ SyntaxError: invalid syntax In [40]: t1=t[:] In [41]: t1 Out[41]: ['a', 'b', 'c', 'd', 'e'] In [42]: t1=t[3:] In [43]: t1 Out[43]: ['d', 'e'] In [44]: t1=t[3:4] In [45]: t1 Out[45]: ['d'] In [46]: t1 Out[46]: ['d'] In [47]: t1=t1+[5,6] In [48]: t1 Out[48]: ['d', 5, 6] In [49]: t=t+[5,6] In [50]: t Out[50]: ['a', 'b', 'c', 'd', 'e', 5, 6] In [51]: t2=t1.append("python") In [52]: t2 In [53]: t2=t.append("python") In [54]: t2 In [55]: t Out[55]: ['a', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [56]: t1 Out[56]: ['d', 5, 6, 'python'] TypeError: insert() takes exactly 2 arguments (1 given) In [58]: t.insert(1,"111") In [59]: t Out[59]: ['a', '111', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [60]: t.insert(0,"222") In [61]: t Out[61]: ['222', 'a', '111', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [62]: help(t.insert) In [63]: help(t.append) In [65]: t Out[65]: ['222', 'a', '111', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [66]: t.index('222') Out[66]: 0 In [67]: t.index('111') Out[67]: 2 In [68]: t.count('222') Out[68]: 1 In [69]: del t[0] In [70]: t Out[70]: ['a', '111', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [71]: del t[1] In [72]: t Out[72]: ['a', 'b', 'c', 'd', 'e', 5, 6, 'python'] In [73]: del t[t.index("python")] In [74]: t Out[74]: ['a', 'b', 'c', 'd', 'e', 5, 6] In [75]: t1 Out[75]: ['d', 5, 6, 'python'] In [76]: del t1 In [79]: t.remove("a") In [80]: t Out[80]: ['b', 'c', 'd', 'e', 5, 6] In [81]: help(t.remove) In [82]: In [82]: t.remove(5) In [84]: t Out[84]: ['b', 'c', 'd', 'e', 6] In [85]: t1=['a','b'] In [87]: t1>t Out[87]: False In [88]: t>t1 Out[88]: True In [89]: len(t) Out[89]: 5 In [90]: t Out[90]: ['b', 'c', 'd', 'e', 6] In [91]: len(t1) Out[91]: 2 In [93]: t3=[1,45,67,34,33] In [94]: max(t3) Out[94]: 67 In [95]: min(t3) Out[95]: 1 In [96]: t5=["g","r","u","o"] In [97]: max(t5) Out[97]: 'u' In [98]: min(t5) Out[98]: 'g' In [99]: sum(t3) Out[99]: 180 In [100]: sorted(t3) Out[100]: [1, 33, 34, 45, 67] In [101]: sorted(t5) Out[101]: ['g', 'o', 'r', 'u'] In [102]: sorted(t5,reverse=True) Out[102]: ['u', 'r', 'o', 'g'] In [103]: sorted(t3,reverse=True) Out[103]: [67, 45, 34, 33, 1] In [104]: sorted(t3,reverse=False) Out[104]: [1, 33, 34, 45, 67] In [105]: t3.sort() In [106]: t3 Out[106]: [1, 33, 34, 45, 67] In [107]: t3.sort(reverse=True) In [108]: t3 Out[108]: [67, 45, 34, 33, 1] In [109]: help(sorted) In [110]: t5 Out[110]: ['g', 'r', 'u', 'o'] In [112]: reversed(t5) Out[112]: <list_reverseiterator at 0x7fd4e8942208> In [113]: t5 Out[113]: ['g', 'r', 'u', 'o'] In [114]: t7=reversed(t5) In [115]: t7 Out[115]: <list_reverseiterator at 0x7fd4e89a0908> In [116]: for i in t7: ...: print(i) ...:
类似于列表,使用()表示,内部元素逗号隔开,
元组不能二次赋值,相当于是只读列表。
tuple=(“ww”,“gg”,“hh”)
元组名[开始索引:结束索引:步长]
元组和列表的区别
列表是可变,元组是不可变的序列
元组的处理速度比列表要快
元组可以作为字典的关键字,列表是不能作为关键字,因为列表是可变的
list() 和 tuple() 实现相互的转变
是无序的对象集合,字典中的元素可以通过键值来获取,而不是通过下标,{}字典
字典由key 和value 组成,用过key-value 的键值对的一种映射类型
特性:
字典中键值是不可变类型 有字符和元组组成,键值不能重复,值是可以重复的
{101:“bj”,022:“tianj”,033:“najing”,44:“tt”}
a_dict={“101”:“bj”,“022”:“tianj”,“033”:“najing”,“44”:“tt”}
dict()
zip()
key=[‘a’,‘b’,‘c’]
v=[1,2,3]
{‘a’:1,‘b’:2,‘c’:3}
d1=dict(zip(key,v))
zip() :接受任意多个序列作为参数,返回元组
In [117]: y=("rr","hh","44","gg") In [118]: y Out[118]: ('rr', 'hh', '44', 'gg') In [119]: y[0] Out[119]: 'rr' In [120]: y[1] Out[120]: 'hh' In [121]: y.index("44") Out[121]: 2 In [122]: y.count("44") Out[122]: 1 In [123]: del File "<ipython-input-123-d8959d277f58>", line 1 del ^ SyntaxError: invalid syntax In [124]: y Out[124]: ('rr', 'hh', '44', 'gg') In [125]: t Out[125]: ['b', 'c', 'd', 'e', 6] In [126]: y1=tuple(t) In [127]: y1 Out[127]: ('b', 'c', 'd', 'e', 6) In [128]: type(y1) Out[128]: tuple In [129]: type(t) Out[129]: list In [130]: y1 Out[130]: ('b', 'c', 'd', 'e', 6) In [131]: t8=list(y1) In [132]: t8 Out[132]: ['b', 'c', 'd', 'e', 6] In [133]: a_dict={"101":"bj","022":"tianj","033":"najing","44":"tt"} File "<ipython-input-133-a2dfea2e4b06>", line 1 a_dict={"101":"bj","022":"tianj","033":"najing","44":"tt"} ^ SyntaxError: invalid character in identifier In [134]: a_dict={"101":"bj","022":"tianj","033":"najing","44":"tt"} In [135]: a_dict Out[135]: {'101': 'bj', '022': 'tianj', '033': 'najing', '44': 'tt'} In [136]: In [137]: key=['a','b','c'] In [138]: v=[1,2,3] In [139]: d1=dict(zip(key,v)) In [140]: d1 Out[140]: {'a': 1, 'b': 2, 'c': 3} In [141]: d2=dict(one=1,two=2,three=3) In [142]: d2 Out[142]: {'one': 1, 'two': 2, 'three': 3} In [144]: help(list) In [145]: t9=list("3,4,5,6") In [146]: t9 Out[146]: ['3', ',', '4', ',', '5', ',', '6'] In [147]: d3={}.fromkeys((1,2,3),'student') In [148]: d3 Out[148]: {1: 'student', 2: 'student', 3: 'student'} In [149]: d3={}.fromkeys((1,2,3)) In [150]: d3 Out[150]: {1: None, 2: None, 3: None} In [152]: d3={}.fromkeys(()) In [153]: d3 Out[153]: {} In [154]: d1 Out[154]: {'a': 1, 'b': 2, 'c': 3} In [155]: d1['a'] Out[155]: 1 In [156]: d1['b'] Out[156]: 2 In [157]: d1.get('a') Out[157]: 1 In [159]: d1['a']=10 In [160]: d1 Out[160]: {'a': 10, 'b': 2, 'c': 3} In [161]: d1.keys Out[161]: <function dict.keys> In [162]: d1.keys() Out[162]: dict_keys(['a', 'b', 'c']) In [163]: d1.values() Out[163]: dict_values([10, 2, 3]) In [164]: for i in d1.keys(): ...: print(i) ...: a b c In [165]: for i in d1.values(): ...: ...: print(i) ...: ...: ...: 10 2 3 In [166]: d2 Out[166]: {'one': 1, 'two': 2, 'three': 3} In [170]: d3 Out[170]: {} In [171]: d3.clear() In [172]: d3 Out[172]: {} In [173]: del d3 In [175]: d1.items() Out[175]: dict_items([('a', 10), ('b', 2), ('c', 3)]) In [176]:
while:
while 表达式:
语句 循环体
else:
语句
当表示式式真(1(非0)) 重复执行语句,直到 表达是假(0),执行else 下的语句
特点: 先判断,后执行
例如:
求和 200 已内的和
sum=0
i=1
while i<=200:
sum+=i
i=i+1
print("sum= %d" % sum)
判断输入的数是否小于5
count =int(input())
while count<5:
print(coount,"is less than 5")
count=count+1
else:
print(count,"is not less than 5")
下一片,咋们来用For 循环写程序 ,程序的练习(运算符),编写较为复杂的程序。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。