-
算数运算
序号 表达式 含义 1 3/2 除以,结果1.5 2 3//2 整除,结果1 3 2**3 pow(2, 3),结果8 -
获取用户输入
>>>x = input("x:") x:>? 123 >>>y = input("y:") y:>? 456 >>>print(int(x)*int(y)) 56088
-
print用法
打印多个参数时,在参数中间插入了空格
>>>print('Age:',42) Age: 42 >>>name = 'Gumby' >>>salutation = 'Mr.' >>>greeting = 'Hello,' >>>print(greeting, salutation, name) Hello, Mr. Gumby
print自定义分隔符sep =
print自定义结束符end=,默认为换行符,即print变量之后会换行
>>>print("I", "wish", "to", "register", "a", "complaint", sep="_") I_wish_to_register_a_complaint >>> print('Hello,', end=' ') ...print('world!') Hello, world!
-
导包
# 导包4种方式 import somemodule from somemodule import somefunction from somemodule import somefunction, anotherfunction, yetanotherfunction from somemodule import * # 当导入的两个模块都含有函数open时 #以第一种方法导入 import module1 import module2 #调用 module1.open(...) module2.open(...) # 或者 from module1 import open as open1 from module2 import open as open2 # as用法 >>>import math as foobar >>>foobar.sqrt(4) 2.0 >>>from math import sqrt as foobar >>>foobar(4) 2.0
-
序列解包
>>>x, y, z = 1, 2, 3 >>>print(x, y, z) 1 2 3 >>>x, y = y, x >>>print(x, y, z) 2 1 3 >>>values = 1, 2, 3 >>>values (1, 2, 3) >>>x, y, z = values >>>x 1 >>>scoundrel = {'name':'Robin', 'girlfriend':'Marion'} >>>key, value = scoundrel.popitem() >>>key 'girlfriend' >>>value 'Marion' >>>x, y, z = 1, 2 Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: not enough values to unpack (expected 3, got 2) >>>x, y, z = 1, 2, 3, 4 Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: too many values to unpack (expected 3) >>>a, b, *rest = [1, 2, 3, 4] >>>rest [3, 4] >>>name = "Albus Percival Wulfric Brian Dumbledore" >>>first, *middle, last = name.split() >>>middle ['Percival', 'Wulfric', 'Brian'] >>>a, *b, c = "abc" >>>a, b, c ('a', ['b'], 'c')
-
断言
工作原理伪代码
if not condition: crash program
目的是让程序在错误条件出现时立即崩溃(这样胜过以后再崩溃)
>>>age = 10 >>>assert 0 < age < 100 >>>age = -1 >>>assert 0 < age < 100 Traceback (most recent call last): File "<input>", line 1, in <module> AssertionError >>>age = -1 >>>assert 0 < age < 100, 'The age must be realistic' Traceback (most recent call last): File "<input>", line 1, in <module> AssertionError: The age must be realistic
-
列表推导
# ==运算符表示内容相同,is表示引用相同 ta = [x * x for x in range(10)] tb = [] for x in range(10): tb.append(x * x) assert ta == tb ta = [x * x for x in range(10) if x % 3 == 0] tb = [] for x in range(10): if x % 3 == 0: tb.append(x * x) assert ta == tb ta = [(x, y) for x in range(3) for y in range(3)] tb = [] for x in range(3): for y in range(3): tb.append((x, y)) assert ta == tb girls = ['alice', 'bernice', 'clarice'] boys = ['chris', 'arnold', 'bob'] ta = [b + '+' + g for b in boys for g in girls if b[0] == g[0]] tb = [] for b in boys: for g in girls: if b[0] == g[0]: tb.append(b + '+' + g) assert ta == tb
-
占位符
if 1 == 2: pass
-
Python不同版本中 类的区别
截止到Python2.1时,只存在旧式类;Python2.2引进新式类;Python3中只有新式类;有些功能(如特性和函数super)不适用于旧式类。
如果文件开头有__metaclass__ = type,则文件中的类都是新式类;如果在类的作用域内有__metaclass__ = type,则仅当前类为新式类;
新式类隐式地继承object。