python 编程之初出茅庐

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

闭关苦练-容器类型-1

list
– 元素可变 a=[] – 元素异构 a = [99, "bottles of beer", ["on", "the", "wall"]] – dir(a)
操作 输出
说明
a = range(5) a.pop() a.reverse() a.sort() a.insert(0, 42) a.append(['another', 'list']) a.extend(['another', 'list']) 5 None None None None None
python版本
print len(open("a.py").readlines())
优势:
1、1:19
2、无需编译,即写即用
高,高家庄的高!-2

简单动态特性
class Demo(object):
pass a=Demo() a.b=1 print a.b
初入茅庐-1

安装
– 从http://www.python.org/download/ 下载合适平台的版本,推荐2.7 – linux:uncompress, configure, make&&make install – 添加python安装路径的bin目录到系统环境变量path中
易学 跨平台 胶水

– cython –Jython –Ironpython –pypy

有趣语法
– a,b=b,a –'s' * 3 -> 'sss'
派森你好--谁在用?
Google Baidu Tencent Alibaba NASA RedHat 。。。

派森你好—应用范围
python 编程之初出茅庐
raymond 2014-04
大纲
派森你好 高,高家庄的高! 初入茅庐 闭关苦练 初出茅庐 武功秘籍

派森你好
历史 特点 谁在用? 应用范围

派森你好-历史

什么是python
Python是一种语法简洁优美的,面向对象的,内置高级 数据结构,支持模块和包,支持多种平台,可扩展的解释型通 用编程语言。
# [0,1,2,3,4] # [0,1,2,3,4] # [4,3,2,1,0] # [0,1,2,3,4] # [42,0,1,2,3,4] # [0,1,2,3,4,['another', 'list']] # [0,1,2,3,4,'another', 'list'] #[0,0,0]
tuple
闭关苦练-控制流-if

Python支持三种不同的控制结构:if,for和while,不支 持C语言中的switch语句。 if 语句的用法:
if EXPRESSION1: STATEMENT1 elif EXPRESSION2: STATEMENT2 else: STATEMENT3

闭关苦练-控制流-for,while
初入茅庐-2

基本语法
– 以冒号(:) 为段落的开始 – 不必使用分号(;) 为段落结尾,段落采用缩进表示,同一段落缩进相同 推荐用空格缩进,不推荐tab作为缩进 – 井字号(#) 是注释符

3大利器
– dir – help – type
闭关苦练


基本数据类型: number, str 容器类型: list, tuple,dict 变量 控制流 函数 标准库
初出茅庐-1 输出下面字符,共10行
* ** *!* **** **!** ****** ***!*** ******** ****!**** **********
打印上面的字符?
初出茅庐-2
看图,说话
jgnnq yqtnf
初出茅庐-3 下面程序的输出?
alist=['a','b','c','d'] print 'alist=' ,alist for each in alist: print each alist.remove(each)
# Get an element # Set an element # unkown # [x,y] # [3,4] # [('y', 4), ('x', 3)] #b: {'y':4}, d:{’x’: 3, ’y’: 4} #b: {'y':4,2:3}, d:{’x’: 3, ’y’: 4,2:3}

例子:
def gcd(a, b): "greatest common divisor" while a != 0: a, b = b%a, a # parallel assignment return b >>> gcd.__doc__ 'greatest common divisor' >>> gcd(12, 20) 4


python 没有函数重载,why?
下面函数有什么问题?
def func(in,out):
print in,out out = 2;
闭关苦练-标准库

核心库
–os, sys, string, getopt, StringIO, struct, pickle, ...
正则库
–re module;

文件读取例子
c++版本
#include <iostream> #include <fstream> #include <string> using namespace std; int CountLines(char *filename) { ifstream ReadFile; int n=0; string temp; ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件 while(getline(ReadFile,temp)) { n++; } ReadFile.close(); return n; } int main() { cout<<"comn.txt的行数为: "<<CountLines("a.py")<<endl; return 0; }
a= [0] * 3
– 元素不可变 b=(), dir(b) – 元素异构 – c=(0), c=(0,)
闭关苦练-容器类型-2

dict
– key不可变,value可变 a={key: value} – 元素异构 – dir(a)
a={} b = { 'x': 3, 'y': 4 } c = { ’uid’: 105, ’login’: ’beazley’, ’name’ : ’David Beazley’ } u = c[’uid’] c[’shell’] = "/bin/sh" c['login'] = 23 b.get('Perl', 'unknown') b.keys() b.values() b.items() d=b.copy(); del b['x'] b[2]=3; b.update(d) # An empty dictionary

python之父
Python的创始人为Guido van Rossum。1989年圣诞节期间, 在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个 新的脚本解释程序,做为ABC语言的一种继承。之所以选中 Python作为程序的名字,是因为他是一个Monty 大蟒蛇飞行 马戏团的爱好者。
派森你好-特点
闭关苦练-基本数据类型-1

number
–bool –int –float –long – complex –c-style shifting & masking True False 12 0 -12987 0123 0X1A2 12.03 1E1 -1.54E-21 10232323322394L 1+3j 1<<16, x&0xff, x|1, ~x, x^y
网络库
–socket, httplib, htmllib, ftplib, smtplib, lxml...
其他库
–pdb (debugger), profile+pstats –Tkinter (Tcl/Tk interface), audio, *dbm, ...

在线参考
–标准库:http://docs.python.org/library/index.html
闭关苦练-基本数据类型-2

str
– 不可变 – "hello"+"world" "helloworld" # concatenation – 'hello'*3 "hellohellohello" # repetition – "hello"[0] "h" # indexing – "hello"[-1] "o" # (end) – "hello"[::-1] "olleh" #反转 – "hello"[1:4] "ell" # slicing – len("hello") 5 # size – "hello" < "jello" 1 # comparison – "e" in "hello" 1 # search – "escapes: \n etc, \033 etc, \if etc" – 'single quotes' """triple quotes""" r"raw strings" – "The %s is %d" %('abc',12) The abc is 12 #formatting string
闭关苦练-函数-2

作用域-LGB规则:
大多数名字引用在三个作用域中查找:先局部(Local),次之 (Global),再次之内置(Built-in)。若仍然找不到这个变量名,则引发 NameError异常.

参数分类:
– – – – 普通参数,即命名参数 arg1 带默认值参数 arg2=0 元组参数 *arg3 词典参数 **arg4

网站开发 - django web.py

测试 – 自动化测试,自动化部署、运维脚本 selunium robotframework pyunit safe toast 数据挖掘- ipython、matplotlib、numpy、scipy
网络开发 – rpyc xmlrpc


高,高家庄的高!-1
武功秘籍
内功心法
– http://www.python.org --官网 – 《python 核心编程第二版》 --官方手册 – http://python.cn/ --中文论坛

break: 终止当前循环 continue: 终止本次循环 pห้องสมุดไป่ตู้ss: 什么事都不做
闭关苦练-函数-1

语法:
def funcname(arg1, arg2=0,*arg3, **arg4): """documentation""" # optional doc string
statements return xxx
闭关苦练-变量-1 不需声明 不用初始化 不用指定类型

可以是任何东东
– module – class – function – ...
闭关苦练-变量-2

变量赋值
– x=y 仅仅做了引用 – 如何区分? id(x) – 可变变量的引用
a = [1, 2, 3] a 1 2 3
b=a

hello world
– 交互运行:
python
>>>print 'hello world'
– 脚本运行: python hello.py
#!/usr/bin/python print “hello, world!”

IDE
– Windows: idle ,eclipse, netbean, ultraedit – Linux: vim emcas

for语句的用法:
mylist = "for statement" for word in mylist: print word #break else: #最终执行 print "End list"

while语句的用法:
a=1; b=5 while a < b: # Do something a=a+1
a b a
1 2 3
a.append(4)
1 2 3 4 b
闭关苦练-变量-3

变量赋值
– 常量变量的引用
a=1 a 1
a b=a b 2
old reference deleted
1
new int object created
by add operator (1+1)
a
a = a+1
b 1
by assignment (a=...)
相关文档
最新文档