PyQt(Chapter 03)

Wed 27 September 2017 by Little Captain

Sequence

  • 有序
  • 通过索引定位
  • 切片操作取出子 sequence
  • String、Tuple(至少含有两个元素, (1) 为 int 1) 不可变(创建后, 元素不可变), List 可变
  • + * [] 是所有 sequence 通用的操作符
  • + 连接
  • * 重复序列, 产生新的序列
  • [] 取出指定的元素和子 sequence
# +
[1, 1] + [1, 2] # [1, 1, 1, 2]
(1, 2) + (1, 2) # [1, 2, 1, 2]
'12' + '12' # '1212'
# *
[1, 2] * 3 # [1, 2, 1 …
read more

PyQt(Chapter 02)

Mon 25 September 2017 by Little Captain

Getting Wet in Python (深入巨蟒湿地)

算数操作符

x + y # Addition
x - y # Subtraction
x * y # Multiplication
x / y # Division
x // y # Truncating division (floor division)
x ** y # Exponentiation. Sets x to the power y
x % y # Modulo operator
x # Unary minus
+x # Unary plus

除法操作符

  • / : 结果总是不会被截断, 结果总是浮点数
  • // : 不管有没有浮点操作数, 都将结果截断为整数. 但是如果一个操作数为浮点数, 结果就为浮点数 …
read more

PyQt(Chapter 01)

Sun 24 September 2017 by Little Captain

Python and Its Features

数据类型

Integers

  • 32 bits
  • [-232 232-1]

Long Integers

  • 无限精度, 受限于计算机内存

Floating Point Numbers

  • 浮点总是双精度的, 64 bits

Boolean

  • 仅取两个值: True、False

Complex Number(复数)

  • a + b * j
  • a、b都是浮点数

Strings

  • Unicode 字符序列

Lists

  • 一组值的有序序列

Tuples

  • 不可变、一组值的有序序列

Sets

  • 无序的值的集合

Dictionaries

  • 无序的键值对集合

0b、0B: 二进制 …

read more

Lean Python(Chapter 11)

Sun 24 September 2017 by Little Captain

Databases

SQLite

数据库函数

# 打开连接数据库
conn = sqlite3.connect(filename)
# 执行 SQL
conn.executescript(sql)
# 返回游标
cursor = conn.cursor()
# 执行查询操作, 返回数据
cursor.execute(sql)
rows = cursor.fetchall()

连接和导入数据 (SQLite)

# insert
import os
import sqlite3

db_filename='mydatabase.db'

exists = os.path.exists(db_filename)
if exists:
    os.unlink(db_filename)

conn = sqlite3.connect(db_filename)

schema …
read more

Lean Python(Chapter 10)

Sat 23 September 2017 by Little Captain

字符串搜索

  • find 返回字符串出现的字符位置, 如果没有找到就返回 -1
txt="The quick brown fox jumps over the lazy dog"
txt.find('jump')
txt.find('z')
txt.find('green')

正则表达式

# 匹配邮箱
import re

regex = '\s[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}[\s]'
text = """This is some text with x@y.z embedded e-mails
that we …
read more

Lean Python(Chapter 09)

Sat 23 September 2017 by Little Captain

Accessing the Web

import requests
from urllib.parse import urlparse

url = input('Web url to fetch:')
urlparts = urlparse(url)
if urlparts[0] == '':
    url = ''.join(('http://', url))

qstring = input('Enter query string:')
if len(qstring) > 0:
    url = '?'.join((url, qstring))

save = input('Save downloaded page to disk [y/n]?')

print('Requesting …
read more

Lean Python(Chapter 08)

Sat 23 September 2017 by Little Captain

Testing Your Code

模块化编程与代码测试

  • 模块化编程是测试更为容易

测试驱动的开发(TDD)

  1. 开发代码前先写测试
  2. 运行测试, 观察失败, 然后添加和修正代码, 使测试通过
  3. 当测试通过后, 寻求改进代码设计的机会
  • 对于大型项目, TDD 能够最好的模块化你的代码

单元测试框架 : unittest

def calc(a, op, b):
    if op not in '+-/*':
        return None, 'Operator must be +-/*'
    try:
        if op == '+':
            result = a + b
        elif op == '-':
            result = a - b
        elif op == '/':
            result = a / b
        else …
read more

Lean Python(Chapter 07)

Sat 23 September 2017 by Little Captain

Exception and Error Handling

异常和错误的处理

print('Input two numbers. the first will be divided by the second')

afirst = input('first number:')
first=float(afirst)
asecond = input('second number:')
second = float(asecond)

quotient = first / second
print('Quotient first/second = ',quotient)
print('Input two numbers. the first will be divided by the …
read more

Lean Python(Chapter 06)

Sat 23 September 2017 by Little Captain

Object Orientation

创建对象

类的定义

  • 类是对象的模板
from datetime import datetime

class person(object):
    "Person Class"
    def __init__(self, name, age, parent = None):
        self.name =name
        self.age = age
        self.created = datetime.today()
        self.parent = parent
        self.children = []
        print('Created', self.name, 'age', self.age)

    def setName(self, name):
        self.name = name …
read more

Lean Python(Chapter 05)

Fri 22 September 2017 by Little Captain

Module

导入模块

  • 一个文件就是一个模块, 模块名就是文件名
# as 使用自定义的名字引用这个模块
import modulename [as name]
# 从模块中引入指定的函数、类、变量等
from module import function1, function2...
# 从模块中引入所有的内容, 这时直接使用, 而不需要使用 . 引用
from module import *
  • 注意 : 通常, 导入模块的需要内容和功能, 而要避免使用 import *

Python 自带模块

  • 查看 Python 环境 Path
import sys
sys.path
read more