/images/avatar.png

Project Euler Problem 1

Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

sum = 3 * (1 + 999 / 3) * (999 / 3) / 2 + \
      5 * (1 + 999 / 5) * (999 / 5) / 2 - \
      15 * (1 + 999 / 15) * (999 / 15) / 2
print sum

3和5的倍数

如果我们列出所有小于10且为3或5的倍数的自然数,我们得到3,5,6和9。这些数的和是23。找出所有低于1000且为3或5倍数的和。

PyQt编程第6章 主窗口

创建一个主窗口

import os
import platform
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import helpform
import newimagedlg
import qrc_resources
__version__ = "1.0.0"

import的顺序:先Python标准模块,然后第三方模块(比如PyQt),最后自己定义的模块。 一个程序通常有一个版本字符串,按照惯例称为__version__

PyQt编程第5章 对话框

对话框以“智力”分类为:哑巴,标准和聪明,取决于对话框知道程序数据的多少。 对话框以“模式”分类为:模式对话框和无模式对话框。

应用模式对话框一旦被调用,用户只能与其交互,不能使用程序其它部分。 窗口模式对话框和应用模式对话框类似,只是它仅仅阻止与其父窗口交互。 无模式对话框允许用户与程序其它部分交互。

PyQt编程第4章 GUI编程介绍

一个25行的弹出警告

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

try:
    due = QTime.currentTime()
    message = "Alert!"
    if len(sys.argv) < 2:
        raise ValueError
    hours, mins = sys.argv[1].split(":")
    due = QTime(int(hours), int(mins))
    if not due.isValid():
        raise ValueError
    if len(sys.argv) > 2:
        message = " ".join(sys.argv[2:])
except ValueError:
    message = "Usage: alert.pyw HH:MM [optional message]" # 24hr clock

while QTime.currentTime() < due:
    time.sleep(20) # 20 seconds

label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot(60000, app.quit) # 1 minute
app.exec_()

每一个PyQt图形程序必须有一个QApplication对象,因为它能识别一些命令行的参数,接受sys.argv作为 参数。

第15章 面向对象编程

15.1 OOP概述

面向对象编程的关键思想是数据抽象,继承和动态绑定。使用数据抽象,我们可以定义接口和实现分离的类。通过继承,我们可以定义相似类之间的关系模型。通过动态绑定,我们可以使用这些类对象而忽略它们之间的差别。