随笔分类 - python
1
摘要:def upper_everything(elements: list[str] -> list[str]): #指定傳入參數的類型,及返回參數的類型 return [element.upper() for element in elements] if __name__ == '__main__'
阅读全文
摘要:判斷某個數是不是素數 number = 12 i = 2 isPrime = True while i < number: if number % i == 0: isPrime = False break i += 1 if isPrime: print(number, "是素數") else:
阅读全文
摘要:計算某個數的階乘 number = 5 # 計算3的階乘 re = 1 # 存儲最後算出來的結果,因為最初的數是1,1*任何數都是1 i = 1 # 臨時存儲當前階乘計算到哪個數了 while i <= number: re *= i i += 1 print(re) 打印1!-2!+3!-4!..
阅读全文
摘要:原因: 因为安装numpy用的是 pip来安装的 pypi官方对于numpy的库已经升级了,但是升级后的版本与其他的库不匹配 所以报错 解决: 先把已经安装的numpy卸载: pip uninstall numpy 再安装低版本的numpy: pip install -U numpy==1.14.5
阅读全文
摘要:【背景】 环境: RHEL 7.3 版本: python2.7 【错误情况】 写了一个简单的python脚本 将dict转换为json 脚本如下: 执行后报错: 【排查】 1. 进入python解释器,查看是否存在json模块 ==> 没有报错,证明存在此模块 2. 测试是否存在dumps模块并且可
阅读全文
摘要:【背景】 需求: 打印word文件 模块: python-docx 【问题】 传递xxx.doc文件给python脚本,执行后,控制台没有内容输出 经查询后了解到,大致理由: doc是早一代的word文件,封闭格式,office或wps是采用反破译去解析出来,但也不保证完美解析 python没有做解
阅读全文
摘要:【情况】 在写Python代码的时候,需要用到psutil模块,需要安装。 但是在安装时,报错:error: command 'gcc' failed with exit status 1 【解决步骤】 按照经验,觉得应该是缺少了gcc模块, 所以操作 [ yum install -y gcc ]
阅读全文
摘要:【前提】 今日是20171207,目前Linux发行版默认安装的Python版本都是2.6.x,但是这个版本Python已经不再进行维护了。 所以需要将Python做一个升级,到2.7.x 【注意】 Python官方已经说明,2020年之后都不会再进行维护Python2.7.x。 【升级准备】 1.
阅读全文
摘要:[root@localhost test1]# vim 19.py //add #!/usr/bin/python macaddr = '00:0C:29:D1:6F:E9' prefix_mac = macaddr[:-3] last_two = macaddr[-2:] plus_one = int(last_two, 16) + 1 new_last_two = hex(plus_one...
阅读全文
摘要:[root@localhost test1]# vim 18.py //add #!/usr/bin/python with open('/proc/meminfo') as ll: for line in ll: if line.startswith('MemTotal'): total = line.split()[1] co...
阅读全文
摘要:/* 使用while循环遍历文件*/ [root@localhost test1]# vim 17.py //add #!/usr/bin/python ll = open('/tmp/1.txt') while True: line = ll.readline() if not line: break print line, [root@localh...
阅读全文
摘要:/* while 是在有条件控制的情况下 进行的循环 */ [root@localhost test1]# vim 13.py //ADD #!/usr/bin/python n = 0 while True: if n == 10: break print n, 'hello' n += 1 [root@localhost test1]# pytho...
阅读全文
摘要:/* for...else 语句,当for循环正常结束后,才会执行else语句。 */ eg1: [root@localhost test1]# vim 11.py //ADD #!/usr/bin/python for i in xrange(10): print i else: print 'main end' [root@localhost test1]# pytho...
阅读全文
摘要:In [2]: list1 = [1,2,3,4] In [3]: for i in list1: ...: print i ...: 1 2 3 4 In [4]: for i in list1: print i, ...: 1 2 3 4 //range(头,尾,走多少步取值) In [7]: range(0,10) Out[7]: [0, 1, 2...
阅读全文
摘要:/* if ....else .... */ [root@localhost test1]# vim 5.py //ADD #!/usr/bin/python if 1>2: print 'hello python' print 'TRUE' else: print 'FALSE' [root@localhost test1]# python 5.py FALSE ...
阅读全文
摘要:[root@localhost test1]# vim 4.py //ADD #!/usr/bin/python info = {} name = raw_input("Please input a name: ") age = raw_input("Please input your age: ") gender = raw_input("Please input (M/F): ") inf...
阅读全文
摘要:Python数据类型 1。数值 --类型: 1/整型 2/长整型 3/浮点型 -- 0.0, 12.0, -18.8, 3e+7等 4/复数型 -- complex 2. 字符串 --是一个序列 -- 对于序列的操作 可以通过索引和切片 3. 列表 4. 元组 5. 字典
阅读全文
摘要:id() -- 可以查看变量指向内存的地址 赋值为字符串,需要用 单引号 '' type() --可以查看此变量是什么类型的 Tranning: 写一个四则运算器 1.要求从键盘读取数字 2. 分清楚input() and raw_input()的区别
阅读全文
摘要:/* 1. python 源码文件以"py"为扩展名 */ [root@localhost ~]# mkdir test1 [root@localhost ~]# cd test1/ [root@localhost test1]# vi 1.py //ADD #!/usr/bin/python print 'hello world' [root@localhost test1]# pytho...
阅读全文
摘要:1. python安装 //先查看是否存在python的包,如果没有,那可以用yum或去python的官网安装 [root@localhost ~]# rpm -qa|grep python python-2.6.6-51.el6.i686 python-iniparse-0.3.1-2.1.el6.noarch python-urlgrabber-3.9.1-9.el6.noarch 2....
阅读全文
1

浙公网安备 33010602011771号