Python 基础学习
来源:互联网 发布:18u网络机柜 编辑:程序博客网 时间:2024/06/06 01:12
# -*- coding: UTF-8 -*-
str = 'Hello World!'
print str # 输出完整字符串
print str[0] # 输出字符串中的第一个字符
print str[2:5] # 输出字符串中第三个至第五个之间的字符串
print str[2:] # 输出从第三个字符开始的字符串
print str * 2 # 输出字符串两次
print str + "TEST" # 输出连接的字符串
# -*- coding: UTF-8 -*-
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # 输出完整列表
print list[0] # 输出列表的第一个元素
print list[1:3] # 输出第二个至第三个的元素
print list[2:] # 输出从第三个开始至列表末尾的所有元素
print tinylist * 2 # 输出列表两次
print list + tinylist # 打印组合的列表
# -*- coding: UTF-8 -*-
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # 输出完整元组
print tuple[0] # 输出元组的第一个元素
print tuple[1:3] # 输出第二个至第三个的元素
print tuple[2:] # 输出从第三个开始至列表末尾的所有元素
print tinytuple * 2 # 输出元组两次
print tuple + tinytuple # 打印组合的元组
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # 元组中是非法应用
list[2] = 1000 # 列表中是合法应用
# -*- coding: UTF-8 -*-
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # 输出键为'one' 的值
print dict[2] # 输出键为 2 的值
print tinydict # 输出完整的字典
print tinydict.keys() # 输出所有键
print tinydict.values() # 输出所有值
Python pass是空语句,是为了保持程序结构的完整性。
passass 不做任何事情,一般用做占位语句
21.Unicode 字符
u'Hello World !'
22.时间
#!/usr/bin/pythonimport time; # This is required to include time module.
ticks = time.time()print"Number of ticks since 12:00am, January 1, 1970:", ticks
获取当前时间
localtime = time.localtime(time.time());
获取格式化的时间
localtime = time.asctime(time.time());
获取某月日历
import calendar
cal = calendar.month(2008, 1);
23. lambda 函数
sum = lambda arg1, arg2: arg1 + arg2;
调用: sum(10, 20)
reduce(lambda x,y : x + y,Sn)
python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果
24.dir 函数
用来获取一个模块中所有的定义的模块
import math
content = dir(math)
25.文件 I/O
input([prompt]) 函数和 raw_input([prompt]) 函数基本类似,但是 input 可以接收一个Python表达式作为输入,并将运算结果返回。
str = input("请输入:");
请输入:[x*5 for x in range(2,10,2)]你输入的内容是: [10, 20, 30, 40]
File对象的属性
fo = open("foo.txt", "wb")fo.write( "www.runoob.com!\nVery good site!\n");# 关闭打开的文件fo.close()
# 打开一个文件fo = open("foo.txt", "r+")str = fo.read(10);print "读取的字符串是 : ", str# 关闭打开的文件fo.close()
文件定位:
tell()方法告诉你文件内的当前位置;换句话说,下一次的读写会发生在文件开头这么多字节之后。
seek(offset [,from])方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。
fo = open("foo.txt", "r+")str = fo.read(10);print "读取的字符串是 : ", str # 查找当前位置position = fo.tell();print "当前文件位置 : ", position # 把指针再次重新定位到文件开头position = fo.seek(0, 0);str = fo.read(10);print "重新读取字符串 : ", str# 关闭打开的文件fo.close()
import os # 重命名文件test1.txt到test2.txt。os.rename( "test1.txt", "test2.txt" )
import os # 删除一个已经存在的文件test2.txtos.remove("test2.txt")
import os # 创建目录testos.mkdir("test")
import os # 将当前目录改为"/home/newdir"os.chdir("/home/newdir")
import os # 给出当前的目录os.getcwd()
import os # 删除”/tmp/test”目录os.rmdir( "/tmp/test" )
#!/usr/bin/python# Define a function here.def temp_convert(var):try:return int(var)except ValueError, Argument:print "The argument does not contain numbers\n", Argument# Call above function here.temp_convert("xyz");
def functionName( level ):if level < 1:raise "Invalid level!", level
用户自定义异常
class Networkerror(RuntimeError):def __init__(self, arg):self.args = arg
try:raise Networkerror("Bad hostname")except Networkerror,e:print e.args
#!/usr/bin/python# -*- coding: UTF-8 -*-class Employee:'所有员工的基类' empCount = 0def __init__(self, name, salary):self.name = name self.salary = salary Employee.empCount += 1def displayCount(self):print "Total Employee %d" % Employee.empCount def displayEmployee(self):print "Name : ", self.name, ", Salary: ", self.salary"创建 Employee 类的第一个对象"emp1 = Employee("Zara", 2000)"创建 Employee 类的第二个对象"emp2 = Employee("Manni", 5000)emp1.displayEmployee()emp2.displayEmployee()print "Total Employee %d" % Employee.empCount
你也可以使用以下函数的方式来访问属性:
- getattr(obj, name[, default]) : 访问对象的属性。
- hasattr(obj,name) : 检查是否存在一个属性。
- setattr(obj,name,value) : 设置一个属性。如果属性不存在,会创建一个新属性。
- delattr(obj, name) : 删除属性。
hasattr(emp1, 'age') # 如果存在 'age' 属性返回 True。getattr(emp1, 'age') # 返回 'age' 属性的值setattr(emp1, 'age', 8) # 添加属性 'age' 值为 8delattr(empl, 'age') # 删除属性 'age'
Python内置类属性
- __dict__ : 类的属性(包含一个字典,由类的数据属性组成)
- __doc__ :类的文档字符串
- __name__: 类名
- __module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod)
- __bases__ : 类的所有父类构成元素(包含了以个由所有父类组成的元组)
python对象销毁(垃圾回收)
class Point:def __init__( self, x=0, y=0):self.x = x self.y = y def __del__(self): class_name = self.__class__.__name__ print class_name, "销毁"pt1 = Point()pt2 = pt1pt3 = pt1print id(pt1), id(pt2), id(pt3) # 打印对象的iddel pt1del pt2del pt3
类的继承
class Parent: # 定义父类 parentAttr = 100def __init__(self):print "调用父类构造函数"def parentMethod(self):print '调用父类方法'def setAttr(self, attr):Parent.parentAttr = attr def getAttr(self):print "父类属性 :", Parent.parentAttrclass Child(Parent): # 定义子类def __init__(self):print "调用子类构造方法"def childMethod(self):print '调用子类方法 child method'c = Child() # 实例化子类c.childMethod() # 调用子类的方法c.parentMethod() # 调用父类方法c.setAttr(200) # 再次调用父类的方法c.getAttr() # 再次调用父类的方法
你可以使用issubclass()或者isinstance()方法来检测。
- issubclass() - 布尔函数判断一个类是另一个类的子类或者子孙类,语法:issubclass(sub,sup)
- isinstance(obj, Class) 布尔函数如果obj是Class类的实例对象或者是一个Class子类的实例对象则返回true。
方法重写
class Parent: # 定义父类def myMethod(self):print '调用父类方法'class Child(Parent): # 定义子类def myMethod(self):print '调用子类方法'c = Child() # 子类实例c.myMethod() # 子类调用重写方法
基础重载方法
下表列出了一些通用的功能,你可以在自己的类重写:
构造函数
简单的调用方法: obj = className(args)2__del__( self )
析构方法, 删除一个对象
简单的调用方法 : dell obj3__repr__( self )
转化为供解释器读取的形式
简单的调用方法 : repr(obj)4__str__( self )
用于将值转化为适于人阅读的形式
简单的调用方法 : str(obj)5__cmp__ ( self, x )
对象比较
简单的调用方法 : cmp(obj, x)
运算符重载
Python同样支持运算符重载,实例如下:
#!/usr/bin/pythonclass Vector:def __init__(self, a, b):self.a = a self.b = b def __str__(self):return 'Vector (%d, %d)' % (self.a, self.b)def __add__(self,other):return Vector(self.a + other.a, self.b + other.b)v1 = Vector(2,10)v2 = Vector(5,-2)print v1 + v2
class JustCounter:__secretCount = 0 # 私有变量publicCount = 0 # 公开变量def count(self):self.__secretCount += 1self.publicCount += 1print self.__secretCount
re.match函数
re.match(pattern, string, flags=0)
import reprint(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
line = "Cats are smarter than dogs"matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)if matchObj:print "matchObj.group() : ", matchObj.group()print "matchObj.group(1) : ", matchObj.group(1)print "matchObj.group(2) : ", matchObj.group(2)else:print "No match!!"
re.match与re.search的区别
matchObj = re.search( r'dogs', line, re.M|re.I)if matchObj:print "search --> matchObj.group() : ", matchObj.group()else:print "No match!!"
检索和替换
re.sub(pattern, repl, string, max=0)
返回的字符串是在字符串中用 RE 最左边不重复的匹配来替换。如果模式没有发现,字符将被没有改变地返回。
可选参数 count 是模式匹配后替换的最大次数;count 必须是非负整数。缺省值是 0 表示替换所有的匹配。
正则表达式修饰符 - 可选标志
正则表达式可以包含一些可选标志修饰符来控制匹配的模式。修饰符被指定为一个可选的标志。多个标志可以通过按位 OR(|) 它们来指定。如 re.I | re.M 被设置成 I 和 M 标志:
$ gunzip MySQL-python-1.2.2.tar.gz$ tar -xvf MySQL-python-1.2.2.tar$ cd MySQL-python-1.2.2$ python setup.py build$ python setup.py install
#!/usr/bin/python# -*- coding: UTF-8 -*-import MySQLdb# 打开数据库连接db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# 使用execute方法执行SQL语句cursor.execute("SELECT VERSION()")# 使用 fetchone() 方法获取一条数据库。data = cursor.fetchone()print "Database version : %s " % data# 关闭数据库连接db.close()
# 打开数据库连接db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# 如果数据表已经存在使用 execute() 方法删除表。cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")# 创建数据表SQL语句sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )"""cursor.execute(sql)
# 打开数据库连接db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# SQL 插入语句sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""try:# 执行sql语句 cursor.execute(sql)# 提交到数据库执行 db.commit()except:# Rollback in case there is any error db.rollback()
# SQL 插入语句sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ ('Mac', 'Mohan', 20, 'M', 2000)
Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。
- fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
- fetchall():接收全部的返回结果行.
- rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
# 打开数据库连接db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# SQL 查询语句sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000)try:# 执行SQL语句 cursor.execute(sql)# 获取所有记录列表 results = cursor.fetchall()for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4]# 打印结果print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income )except:print "Error: unable to fecth data"
# 打开数据库连接db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# 使用cursor()方法获取操作游标 cursor = db.cursor()# SQL 更新语句sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')try:# 执行SQL语句 cursor.execute(sql)# 提交到数据库执行 db.commit()except:# 发生错误时回滚 db.rollback()
执行事务
事务机制可以确保数据一致性。
事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。
- 原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
- 一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
- 隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
- 持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
# SQL删除记录语句sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)try:# 执行SQL语句 cursor.execute(sql)# 向数据库提交 db.commit()except:# 发生错误时回滚 db.rollback()
DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常:
import smtplibsmtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
参数说明:
- host: SMTP 服务器主机。 你可以指定主机的ip地址或者域名如:w3cschool.cc,这个是可选参数。
- port: 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下SMTP端口号为25。
- local_hostname: 如果SMTP在你的本机上,你只需要指定服务器地址为 localhost 即可。
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
参数说明:
- from_addr: 邮件发送者地址。
- to_addrs: 字符串列表,邮件发送地址。
- msg: 发送消息
这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。
thread.start_new_thread ( function, args[, kwargs] )
参数说明:
- function - 线程函数。
- args - 传递给线程函数的参数,他必须是个tuple类型。
- kwargs - 可选参数。
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadimport time# 为线程定义一个函数def print_time( threadName, delay): count = 0while count < 5: time.sleep(delay) count += 1print "%s: %s" % ( threadName, time.ctime(time.time()) )# 创建两个线程try: thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) )except:print "Error: unable to start thread"while 1:pass
thread 模块提供的其他方法:
- threading.currentThread(): 返回当前的线程变量。
- threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
- threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:
- run(): 用以表示线程活动的方法。
- start():启动线程活动。
- join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
- isAlive(): 返回线程是否活动的。
- getName(): 返回线程名。
- setName(): 设置线程名。
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadingimport timeexitFlag = 0class myThread (threading.Thread): #继承父类threading.Threaddef __init__(self, threadID, name, counter): threading.Thread.__init__(self)self.threadID = threadID self.name = name self.counter = counter def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 print "Starting " + self.name print_time(self.name, self.counter, 5)print "Exiting " + self.namedef print_time(threadName, delay, counter):while counter:if exitFlag: thread.exit() time.sleep(delay)print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1# 创建新线程thread1 = myThread(1, "Thread-1", 1)thread2 = myThread(2, "Thread-2", 2)# 开启线程thread1.start()thread2.start()print "Exiting Main Thread"
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadingimport timeclass myThread (threading.Thread):def __init__(self, threadID, name, counter): threading.Thread.__init__(self)self.threadID = threadID self.name = name self.counter = counter def run(self):print "Starting " + self.name # 获得锁,成功获得锁定后返回True# 可选的timeout参数不填时将一直阻塞直到获得锁定# 否则超时后将返回False threadLock.acquire() print_time(self.name, self.counter, 3)# 释放锁 threadLock.release()def print_time(threadName, delay, counter):while counter: time.sleep(delay)print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1threadLock = threading.Lock()threads = []# 创建新线程thread1 = myThread(1, "Thread-1", 1)thread2 = myThread(2, "Thread-2", 2)# 开启新线程thread1.start()thread2.start()# 添加线程到线程列表threads.append(thread1)threads.append(thread2)# 等待所有线程完成for t in threads: t.join()print "Exiting Main Thread"
Queue模块中的常用方法:
- Queue.qsize() 返回队列的大小
- Queue.empty() 如果队列为空,返回True,反之False
- Queue.full() 如果队列满了,返回True,反之False
- Queue.full 与 maxsize 大小对应
- Queue.get([block[, timeout]])获取队列,timeout等待时间
- Queue.get_nowait() 相当Queue.get(False)
- Queue.put(item) 写入队列,timeout等待时间
- Queue.put_nowait(item) 相当Queue.put(item, False)
- Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
- Queue.join() 实际上意味着等到队列为空,再执行别的操作
#!/usr/bin/python# -*- coding: UTF-8 -*-import Queueimport threadingimport timeexitFlag = 0
class myThread (threading.Thread):def __init__(self, threadID, name, q): threading.Thread.__init__(self)
self.threadID = threadID
self.name = name self.q = q def run(self):print "Starting " + self.name process_data(self.name, self.q)print "Exiting " + self.namedef process_data(threadName, q):
while not exitFlag: queueLock.acquire()
if not workQueue.empty():
data = q.get() queueLock.release()print "%s processing %s" % (threadName, data)
else:
queueLock.release() time.sleep(1)threadList = ["Thread-1", "Thread-2", "Thread-3"]nameList = ["One", "Two", "Three", "Four", "Five"]queueLock = threading.Lock()workQueue = Queue.Queue(10)threads = []threadID = 1
# 创建新线程
for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1
# 填充队列queueLock.acquire()
for word in nameList: workQueue.put(word)queueLock.release()
# 等待队列清空
while not workQueue.empty():
pass
# 通知线程是时候退出exitFlag = 1
# 等待所有线程完成
for t in threads: t.join()
print "Exiting Main Thread"
- Python学习----Python基础
- Python基础学习-Django基础
- Python学习笔记-Python基础
- Python学习笔记--Python基础
- 【Python】python基础学习笔记
- 学习Python基础
- 学习Python基础
- python基础学习
- python基础学习
- Python基础学习笔记
- Python基础语法学习
- Python基础学习
- Python学习----基础篇
- Python基础学习_1
- python基础学习
- python学习--基础篇
- python基础学习
- python基础学习
- linux文件描述符和套接字的问题
- 下面介绍几种查看linux版本信息的方法和GCC版本
- Python 通过调用接口获取公交信息
- win7系统右键没有新建文件夹选项
- 关于Myeclipse2015的破解的详细介绍
- Python 基础学习
- PAT1010 一元多项式求导 (25)
- Zookeeper的作用
- BufferString
- 文本颜色
- [译]深入浅出Redux中间件
- smtp 邮件发送
- url 特殊符号转义
- OpenCV Mat数据类型及位数总结