python的小知识点

python中的变量的名字必须由字母、数字、下划线组成,并且不可以以数字开头。

字典的内容是键-值对,键必须是不可变的,比如字符,整数,浮点数,元组,列表不可以,因为列表可变。集合的元素不重复。字典和集合都是用大括号括起来的东西。

and, or, not是Python的逻辑运算符,分别表示逻辑与,逻辑或,逻辑非。而&, |   这两个符号标识集合运算,分表标识求取两个集合的交集和并集。

#对于and 运算符,如果第一个运算数是True,则返回第二个运算数的值
#如果第一个运算符是False, 则返回False
print(True and 77)
print(True and True)
print(False and 55)
print(False and True)
#the running result is :
# 77
#True
#False
#False
View Code
#对于 or 运算符,如果第一个运算数是True,则返回True
#如果第一个运算符是False, 则返回第二个运算数的值
print(True or 77)
print(True or True)
print(False or 55)
print(False or True)
#the running result is :
#True
#True
#55
#True
View Code

函数的名字就是指向内存的一段地址。

多行注释:ctrl+/

函数里面return语句下面的代码只会被屏蔽,不会被执行。

Python中input()函数让用户输入的东西永远都是字符串,下面的来自于json模块中的一个方法可以将用户输入的字符串(注意带有一定的格式)转化成列表和字典。——(来自于老男孩教育的武沛齐老师的总结)

import json

inp_str = "[11,22,33,44]"
inp_list = json.loads(inp_str)  # 根据字符串书写格式,将字符串自动转换成 列表类型
print(inp_list)
inp_str = '{"k1":123, "k2": "wupeiqi"}'  # 正确的输入      切记,内部必须是 双引号 !!!
# inp_str = " {'k1':123, 'k2': 'wupeiqi'}"   # 错误的输入
inp_dict = json.loads(inp_str)  # 根据字符串书写格式,将字符串自动转换成 字典类型
print(inp_dict)
#配置文件插入一行记录的作业:
import
json read = '{"backend":"test.oldboy.org","record":{"server":"100.1.7.9","weight":20,"maxconn":30}}' inp_dict = json.loads(read) # 根据字符串书写格式,将字符串自动转换成 字典类型 s1=inp_dict['backend'] d=[] s2='server 100.1.7.9999 100.1.7.9 weight 20 maxconn 3000'#等待插入的记录 with open('test.log','r') as f1: re=f1.readlines() for i in re: with open('test1.log', 'a+') as f2: if not s1 in i: re2 = f2.write(i) else: i1=re.index(i) re.insert(i1+2,s2) with open('test1.log', 'a+') as f3: f3.write(i)

 

posted on 2018-07-13 11:33  一杯明月  阅读(224)  评论(0)    收藏  举报