Python 基础

1. pydev, Emacs PthonWin

2. 缩进分层

3. # ''' 注释'''

4. input("somthing" rawinput("something")

5. int(yearstr)+1

6. print("something")

7. ** 乘方运算 // 整除运算

8. 操作字符串

string.capitalize() 将字符串的第一个字母大写
string.count(参数)
string.find(参数)
string.isalnum() 是否包含0-9A-Za-z
string.join
string.swapcase()
string.split()
string.title()
string.upper()
len(string)

9. 字符串索引和分片

str = 'abcdef'
str[2]
str[2:4:1] 分片最后一个为步长

str(数字)

10. 原始字符串

path = R'sds\sd'

11. 列表


列表 [], 逗号分割

list = ['a', 'b', 'c']

list.append()
list.count(x)
list.extend(L), 向列表中追加列表L
list.index(x),
list.insert()
list.pop()
list.remove()
list.reverse()
list.sort()

12. 元组, 元组内容不能改变,因此只能索引和分片

('a', 'b', 'c')

13. 字典

{}

dic.clear
dic.copy
dic.get(k)
dic.has_key(k)
dic.items()
dic.keys()
dic.pop
dic.update
dic.values

14. 文件

file = open ("c/sss", 'w')
file.write('pthon\n')

for i in range(10)
a.append(str(i));

file.writelines(a)

file.readlines();

15. if

if a==b:
。。。。
else:
.....

16. for

for <> in 对象集合

for i in range(1,5+1);
print


17. while

while <条件>:

18. 函数

def <函数名>(参数列表)

return <返回值>

参数可以有默认值

18. import and from

import 模块名

math.seqr

from 模块名 import 函数名


19. 编写一个模块

mymodule.py

def show()
print('sss')

name = 'my name'

import mymodule
mymodule.show()

print(mymodule.name)

20. 导入模块

import os
import sys
modulepath = os.getcwd()+\\'module'
sys.path.append(modulepath)

将mymodule的路径加入到系统路径下

21 编译module

file compile.py, py, pyc

import py_compile
py_compile.compile('mymodule.py', 'mymodule.pyc')


优化
-o
-oo

22 查找模块提供的函数名

import sys
dir(sys)

23 类

class human :
age = 0;
sex = ''
__name = 'mike'
def check(item, self)

类的私有属性以双下滑线表示


多重继承

class C(A,B):

24 异常处理

try:
....
except <异常名1>:
...
except <异常名2>:

else
...


raise 抛出异常


自定义异常类

class MyError(Exception)


25. pdb 调试

python -m pdb prime.py

step
break
where
contine
condition

26. 注册表

import win32api
import win32con

win32api.RegOpenKey

27. 获取当前路径

os.getCwd()

获得目录的内容
os.listdir(path)

os.mkdir(path)

os.rmdir(path)

os.path.isdir(path) 判断是否为目录

os.path.isfile(path)

os.rename

28. 生成可执行文件 py2exe

29. os.system

os.system(command par1 par2 par3 ...)

win32api.ShellExecute(0, 'open', 'notepad.exe', '','', 0)

30. 连接数据库

a import odbc
b.import win32com.client
c. ADO

import MySQLdb

db = MySQLdb.connect(host='localhost',use='root',passwd='python',db='python')

cur = db.cursor()
cur.execute(..)

con.commit

r=cur.execute('select * from people')
r=cur.fetchall()
print(r)
cur.close()

31 web

zope Plone

页面连接数据库
显示内容

reg.form['name']
reg.form[name']


32 urllib.request 访问网站

ftplib
FTP(host, user, passwd, acct)

33. poplib, smtplib 收发邮件


db.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值