一.赋值即定义 1.运行以下代码会出现报错 #!/usr/bin/env python #_*_conding:utf-8_*_ x = 100 def outer(): def inner(): x += 100 #其实这里等效于"x = x + 100",我们直到这是一个赋值语句,会优先计算右边的等式,即"x + 100".而在此时由于x变量赋值即定义,即此时的x和全局作用域的x并非同一个对象. print(x) return inner foo = outer…
Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there are ${howmany} ${lang} Quotation symbols") print s.substitute(lang='Python',howmany=3) >>>there are 3 Python Quotation symbols 用法很简单,先生成一个模板…
1.print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设置的. 一个小例子 def esc(code=0): return f'\033[{code}m' print(esc('31;1;0') + 'Error:'+esc()+'important') 在控制台或者 Pycharm 运行这段代码之后你会得到结果. Error:important 其中 E…
1.判断是否是回文 def is_back(s): ]==(s if s.strip() else False) print(is_back('上海自来水来自海上')) print(is_back('山东落花生花落东山')) print(is_back('山西悬空寺空悬西山')) print(is_back('随随便便写的')) 2.看下面这段代码, 具体是什么意思呢 nums=[1,1,1,2,2,3,4,5,6,6,7,8] for n in nums: if n%2==0: nums.re…
假设文件结构: pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py python -m 你的测试/预处理文件,即为 python -m pkg.tests.core_test.py 更多参考这里:http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-in…
get方法,用于获取字典中某个键值key 对应value的值,此方法可以接收两个参数,第一个参数传入key的值,第二个参数用于传入一个自定义返回值,如果查询的key在字典中存在,就会反回对应key在字典中保存的value值,如果查询的key在字典中不存在,就会返回自定义的返回值,示例代码如下: errmessage = { '400':'(IAM) missing required headers', #缺少必须的报头 '401':'(IAM) headers lack host', #报头缺少…
res00是一张rgb图 [x for sub1 in res00 for sub2 in sub1 for x in sub2] 列出所有像素值…
import sqlite3 #导入模块 conn = sqlite3.connect('example.db') C=conn.cursor() #创建表 C.execute('''CREATE TABLE stocks(data text,trans text, symple text,qty real,price real)''') #插入一条数据 C.execute('''INSERT INTO stocks VALUES('2006-10-01,'BUY','RHA',100,35.1…
python中的这些坑,早看早避免. 说一说python中遇到的坑,躲坑看这一篇就够了 传递参数时候不要使用列表 def foo(num,age=[]): age.append(num) print("num",num) return age print(foo(1)) print(foo(2)) print(foo(3)) 上面的代码输出的结果并不是我们预期的那样,打印出三个数组[1],[2],[3]. 而是下面这样. num 1 [1] num 2 [1, 2] num 3 [1,…
python中的round函数不能直接拿来四舍五入,一种替代方式是使用Decimal.quantize()函数. 具体内容待补. >>> round(2.675, 2) 2.67 可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确. # -*- coding: utf-8 -*- from decimal import Decimal, ROUND_HALF_UP class NumberUtil(object): @staticmethod def…