随笔分类 - python学习历程
自学python记录
摘要:创建一个生成器 >>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g = (x * x for x in range(10)) >>> g <generator object <gene
阅读全文
摘要:异常分别位除零错误(ZeroDivisionError)、命名错误(NameError))、类型错误(TypeError) 1、异常处理 >>> while True: ... try: ... x = int(input("Please enter a number: ")) ... break
阅读全文
摘要:模块是包括Python定义和声明的文件。文件名就是模块名加上.py后缀。模块的模块名可以由全局变量__name__得到。 每个模块都有自己私有的符号表,被模块内所有的函数定义作为全局符号表使用。因此,模块的作者可以在模块内部使用全局变量,而无需担心它与某个用户的全局变量意外冲突 1、作为脚本来执行模
阅读全文
摘要:1、列表方法 list.append(x) list.extend(L) list.insert(i, x) list.remove(x) list.pop([i]) list.index(x) list.count(x) list.sort() list.reverse() 1.2、列表推导式 列
阅读全文
摘要:1、默认参数 def power(x, n = 2): s = 1 while n > 0: n = n - 1 s = s * x return s 调用 >>> power(5) 25 >>> power(5, 2) 25 设置默认参数的注意点: 必选参数在前,默认参数在后,否则Python会报
阅读全文
摘要:定义函数 >>> def fib(n): # write Fibonacci series up to n ... """Print a Fibonacci series up to n.""" ... a, b = 0, 1 ... while a < n: ... print(a, end=’
阅读全文
摘要:1、if语句 >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print(’Negative changed to zero’) ... e
阅读全文
摘要:Python基本运算 1、复数运算 变量在使用前必须“定义”(赋值),否则会出错 带有后缀 j 或 J 就被视为虚数,带有非零实部的复数写为(real + imag * j),或者可以用complex(real, imag)函数创建。 >>> 1j * 1J (-1+0j) >>> 1j * com
阅读全文
摘要:Python3多行输入 1、input().split()用法 host, port, username, passwd, dbname = input("请输入服务器地址,端口号,用户名,密码及数据库名,空格隔开:").split() # 注意input()的返回类型是str print(host
阅读全文
摘要:python3的输出 print('{0},{1}'.format('zhangk', 32)) print('{},{},{}'.format('zhangk','boy',32)) print('{name},{sex},{age}'.format(age=32,sex='male',name
阅读全文

浙公网安备 33010602011771号