python快速入门

来源:互联网 发布:apt get yum rpm 编辑:程序博客网 时间:2024/06/05 18:45

1、特点

python是不用“;”作为每句代码的结尾的。
python来区别模块是使用缩进来区别的,因此在python中缩进是非常重要的。


2、打印

print 'hello world'
print type() 打印变量的类型
多行打印用三引号'''被打印内容'''


3、头文件

#! /usr/bin/env python
意思为使用本地安装的python版本。


4、python的注释

单行注释用“#”
多行注释用三引号 ”’被注释内容”’


5、定义变量

name = 'xiaoming'age = 6

第一代码是定义name为字符串,第二代码为定义整形数字age


6、if语句

以下是if语句的格式,注意每个判断后面的冒号

age = 3if age > 5:  print '5'elif age > 4:  print '4'else:  print age

7、等待输入语句

raw_input('what is your name?')

8、布尔值

注意:python的布尔值首写字母大写,TrueFalse


9、while语句

while True:  print 'hello world'

10、for语句

for i in range(3):  print ielse:  print 'the last'

以上是for循环的格式,elsefor循环顺利执行完成后再执行的。也可以使用continuebreak


11、变量置换

name = 'xiaoming'age = 6print '''nihao%sagewei%d'''%(name,age)

12、列表

a = [99,88,88]
name_list,extend(a)是将a中的元素添加到name_list列表中。
列表可以相加。
列表的切片:
name_list[3:9]
截取从第3到第九个元素
name_list[5:]
截取从第5个元素到最后一个元素
name_list[:-5]
截取从第一个到倒数第5个元素
name_list[1:2:10]
截取第1,3,5…个元素

name_list.insert(name_list.index(19),'charu')name_list.remove("shanchu")name_list.count("geshu")name_list.pop()

移除最后一个

name_list.reverse()name_list.sort()

13、元组

a = (a,b,c,d)
只有countindex方法


14、字典(键值对)

name = {    'key1' : 'value1',     #字符串    'key2' : 34,           #数字    'key3' : [23,23,'we'], #列表    'key3' : {'chrkey' : '33'},  #子字典    }

遍历字典

for k,v in name.items:    print k,v

查看所有的key
key:name.keys()
查看所有的value
value:name.values()
删除第一个
name.popitem()
contacts.get('rain')没有的话返回null
但是用contacts['rain']没有的话会报错
if contacts.has_key('rain'):print '==='

例子:

contact_dic = {}with open('contact_list.txt') as if:    for i in f.readlines():        line = i.strip().split()        contact_dic[line[0]] = line[1:]print contact_dic,keys()if contact_dic.has_key('alex'):    print ...else:    for name,value in contact_dic.items()        if 'alex' in value: print 'go'        else: print 'no'

15、函数(和js很像)

def sayHi(name):    print name    return name

调用
name = sayHi('xiaoming')


16、迭代器(处理大数据的时候有优势)

a = [2,3,4,5,6]b = iter(a)b.next()

17、lambda函数(匿名函数)

functionA = lambda i: i**23
这是一个单行函数,只是为了简洁使用。


18、zip和map

使用zip函数可以把两个列表合并起来,称为一个元素的列表。

l1 = [1,3,5,7]l2 = [2,4,6,8]print zip(li,l2)

但是当长度不一样的时候,多余的被忽略
但是map不会忽略,而会用第一个参数来填充
map(None,l1,l2)
map的其他用法

def square2(x):    return x*xa = [1,2,3,4,5]print map(square2, a)

将列表a中的元素依此放入函数square2中执行。

19、pickle和json序列化

import pickleaccount_info = {    'sdsd':['sd',34,3],    'ffff':['ranchel',999,0]}f = file('account.txt','wb')pickle.dump(account_info,f)f.close()

取出方法用pickle.load(f)

json的用法和pickle完全一样,只不过是应用场景不同。


20、使用正则表达式

import re
将正则表达式编译成pattern对象
pattern = re.compile(r'hello')

使用pattern匹配文本,获得匹配结果,无法匹配将返回none
match = pattern.match('hello world!')

if match:    print match.group()

输出结果为 hello

以上可以简写为:
m = re.match(r'hello','hello world!')

p = re.compile(r'\d+')print p.split('one1two2three3four4')

输出结果为:['one','two','three','four','']
按照能够匹配的字符串string分割后返回列表。maxsplit用于指定最大分割次数,不指定则将全部分割。

print p,findall('one1two2three3four4')
搜索string,一列表形式返回全部能匹配的字符串。

替换
re.sub('[abc]','o','Mark')
结果:Mork

方法集:
strip()方法,去掉两边空格。

0 0
原创粉丝点击