分類彙整: Python範例

Sorted 範例

dic = {'Tom': 90, 'Mary':56, 'Bob':77, 'Alan': 80, 'Sunny':100}
lis = [90,56,77,80,100]
students = [('Tom', 90), ('Mary', 56), ('Bob', 77), ('Alan', 80), ('Sunny', 100)]

def temp_function(list):
    print(list)
    return list[0]

'''
print(dic)
print(dic.items())
print(dic.keys())
print(dic.values())
'''
print('dic sorted:')
print(sorted(dic.items(), key = temp_function))

print('lis sorted:')
print(sorted(lis))

print('students sorted:')
print(sorted(students, key= temp_function))

#lambda
print('lambda')
print(sorted(dic.items(), key = lambda x : x[1]))

PyQT 按鈕事件範例

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sat Jun  8 21:02:00 2013
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(329, 414)
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(30, 30, 251, 31))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 100, 251, 31))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(120, 250, 75, 41))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(30, 160, 251, 31))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.label.setText)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.btnTest)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "TextLabel", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.label_2.setText(_translate("Dialog", "TextLabel", None))
    def btnTest(self):
        self.label_2.setText(self.lineEdit.text())

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

sqlite 連線範例

# -*- coding: utf-8 -*-
import sqlite3
conn = sqlite3.connect('starbucks.sqlite')
cur=conn.cursor()

cur.execute("insert into 'stores' ('longitude','latitude','name','address','tel','wifi','notes') VALUES(20,100,'測試店家','台北市羅斯福路1段1號','0222223333','Y','')")
cur.execute('select * from stores')
conn.commit()

for store in cur.fetchall():
	print(store)

#print(cur.fetchall()[99])
cur.close()
conn.close()