Dive to Python

最近在学习爬虫,摘抄一些python的基础知识,一天就可以入门语法了,哈哈哈

变量、基础数据类型

变量

Python中变量不需要声明,你可以通过直接赋值的方式使用变量:

a = 1
b = True
c = 1.0
a = 3+2j

在Python中,你可以在给一个变量赋值为一种类型之后重新为其赋值为新的类型(如上面代码第1、4行)。

基础数据类型

Python中的数字有:整型、长整型、浮点型、布尔型、复数。

整形:int,常见的整数(正数/负数/0),通过sys.maxint即可查看当前平台上最大的整型的值。
长整型:long,比整型最大的值还大或者比整型最小的值还小的整数。注意:Python中长整型没有大小限制,你的内存有多大,它就能表示多大。
浮点型:float,即小数。
布尔型:bool,常见形式为True, False,表示逻辑真和逻辑假,其实背后的实现是数字10.
复数:complex,Python是为数不多语法层面上支持复数的语言,表现为形如1+2j的形式。可以直接支持两个复数的运算。
Python中的所有数据类型都是对象。你可以通过type()函数查看一个变量的类型。
Python中字符串有4种表现形式:单引号、双引号、三引号:

string1 = 'this is a str'
string2 = "this is a str"
string3 = '''this is a str
这里还是string3的范围'''
string4 = """this is a str"""

其中,第3/4种字符串表现形式都是三引号引起来的。如果在字符串的引号之前加上一个u,则表示这是一个unicode字符串,如果加上r,则表示这是一个原始字符串 ,这两种类型,对于初学者暂不讨论。值得注意的是,Python中没有C-Like语言中的字符类型。

运算符、控制语句

运算符

Python中没有类似于C/C++/Java的自增自减运算符,因为在Python中,数字是一种不可变类型(immutable),对数字的操作都会产生新的对象而不是修改原来的对象。但是i = i + 1一样可以写为i += 1
注意:Python2.5以后的除法为地板除,即5/2=2而不是5/2 = 2.5
Python常见的逻辑运算符为:

!=, <>:不等于,表示两边的值不相等,推荐使用前一种。1 != 2
==:等于,表示两边的值相等。2 == 2
and:逻辑与,如果两边的值都为True则整个值为True,反之为False。1==1 and 2==2
or:逻辑或,如果两边有一个值True则整个值为True,反之为False。1==2 or 2==2
not:逻辑非,如果值为True,则结果为False。not 2==1
控制语句

条件语句:

Python中的条件语句只有if-elif-else,没有switch-case,但是有经验的开发者通常使用map这一结构来达到比switch-case更加优雅,更加Pythonic的实现,这个后续再说。

if 条件1:
    语句块1
elif 条件2:
    语句块2
else:
    语句块3

elif类似于其它语言中的else if

循环语句:

Python中有两种方式表达循环语句:

while 循环:

while 条件:
    语句块

for循环:

for 变量 in 集合:
    语句块

值得注意的是Python中没有C-Like语言里的for(int i=0; i<10; i++)的语法,不过可以使用xrange或者range函数实现同样的效果。前一个函数返回一个生成器,后一个函数返回list,一般推荐使用前一个,至于这两个函数的异同,后面有时间细聊。

集合

Python中常见的集合类型有:list,tuple,set,dict

list

list表达的是一列数据的集合,列表中的元素可以增加、删除、查找等操作:

In [1]: l = ['this', 'is', 'a', 'list']  # 初始化一个list

In [2]: type(l)
Out[2]: list

In [3]: l.index('is')                    # 查找元素
Out[3]: 1

In [4]: l.append('new item')             # 新增元素

In [5]: l
Out[5]: ['this', 'is', 'a', 'list', 'new item']

In [6]: l.reverse()                      # 反转列表

In [7]: l
Out[7]: ['new item', 'list', 'a', 'is', 'this']

In [8]: l.remove('is')                   # 删除元素

In [9]: l
Out[9]: ['new item', 'list', 'a', 'this']

tuple

tuple类似于list,同样表示一组数据的集合,不同的是,tuple是写保护的,即:tuple不可以对其元素做任何修改操作(新增、删除等):

In [1]: t = (1,2,3)    # 初始化

In [2]: t
Out[2]: (1, 2, 3)

In [3]: type(t)
Out[3]: tuple

set

set表示一个不重复元素的集合,值得注意的是:set是无序的。

In [1]: s = {'this', 'is', 'a', 'a', 'set'}  # 初始化

In [2]: s
Out[2]: {'a', 'is', 'set', 'this'}

In [3]: type(s)
Out[3]: set

In [4]: s.add('new item')               # 新增元素

In [5]: s
Out[5]: {'a', 'is', 'new item', 'set', 'this'}

In [6]: s.remove('a')                  # 删除元素

In [7]: s
Out[7]: {'is', 'new item', 'set', 'this'}

dict

dict表示一组数据的对应关系,与set一样,dict是无序的(你可以通过collections中的OrderDict来达到有序)。同时,对于初学者,dictset的声明方式容易混淆:

In [1]: d = {'a': 'A', 'b': 'B'}  # 初始化

In [2]: d
Out[2]: {'a': 'A', 'b': 'B'}

In [3]: d['a']  
Out[3]: 'A'

In [4]: d['c'] = 'C'            # 新增元素

In [5]: d
Out[5]: {'a': 'A', 'b': 'B', 'c': 'C'}

In [6]: del d['a']             # 删除元素

In [7]: d
Out[7]: {'b': 'B', 'c': 'C'}

函数(初步)

在Python中声明函数的方式如下:

def add(num1, num2):
    return num1 + num2

通其它语言一样,你可以在函数中通过return返回值。不同的是,Python中函数可以有多个返回值,比如下面的函数,返回了两个数的和以及差:

def calc(n1, n2):
    return n1 + n2, n1 * n2

add, sub = calc(5, 1)
print add, sub  # 6 4

其实这里并不是真的返回了两个值,而是将返回值组装成一个tuple再返回。

Pythonic Code Styles

交换两个数

常规写法:

t = a
a = b
b = t

二逼写法:

a = a ^ b
b = a ^ b
a = b ^ a

推荐写法:

a, b = b, a

迭代的时候带上序号

常规写法:

index = 0
for item in iterable:
    print index, item
    index += 1

推荐写法:

for index, item in enumerate(iterable):
    print index, item

其中,enumerate还可以接收第二个参数,指定开始的位置。

同时迭代两个可迭代对象

常规写法:

f

or pos in xrange(len(iterable1)):
    item1 = iterable1[pos]
    item2 = iterable2[pos]
    print item1, item2

推荐写法:

for item1, item2 in zip(iterable1, iterable2):
    print item1, item2

获取对象的方法列表

使用dir(obj)即可得到obj对象的所有属性(字段)、方法。比如:

In [26]: l = ['a', 'b', 'c']

In [27]: dir(l)
Out[27]:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
 '__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
 '__getattribute__', '__getitem__', '__getslice__', '__gt__',
 '__hash__', '__iadd__', '__imul__', '__init__', '__iter__',
 '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
 '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
 '__rmul__', '__setattr__', '__setitem__', '__setslice__',
 '__sizeof__', '__str__', '__subclasshook__', 'append',
 'count', 'extend', 'index', 'insert', 'pop', 'remove',
 'reverse', 'sort']

三元表达式

Python里没有C-Like语言中condition ? value1 : value2的语法,但是我们可以有很多中方法实现同样的功能:

value1 if condition else value2
(value2, value1)[condition]
condition and value1 or value2

以上语法都可以实现同样的功能。

if

# 如果要判断一个变量是否等于某几个数字, this is bad
if num == 2 or num ==4 or num ==6:
    pass
# 如果要判断一个变量是否等于某几个数字, this is good
if num in (2, 4, 6):
    pass

# 如果判断一个数字是否在一个区间里,this is bad
if num >= 10 and num <=100:
    pass
# 如果判断一个数字是否在一个区间里,this is good
if 10 <= num <= 100:
    pass

字符串格式化

普通程序员:

name = 'xlzd'
age = 21
string = 'My name is %s and I am %d years old' % (name, age)

二逼程序员:

name = 'xlzd'
age = 21
string = 'My name is ' + name + ' and I am ' + str(age) + ' years old.'

推荐写法:

string = 'My name is {name} and I am {age} years old.'.format(name='xlzd', age=21)

列表推倒

要实现从一个list中取出满足某个条件(在100-1000开区间中)的元素组成一个新的list
普通写法:

l = [1, 2, 4, 34, 76, 123, 560, 590, 777, 1200]
new_list = []
for item in l:
    if 100 < item < 1000:
        new_list.append(item)

推荐写法:

l = [1, 2, 4, 34, 76, 123, 560, 590, 777, 1200]
new_list = [item for item in l if 100 < item < 1000]  # 生成一个listor
new_list = (item for item in l if 100 < item < 1000)  # 生成一个generator

关于上面推荐的两种写法,后一种相对前一种的好处是生成了一个generator,它会延迟计算而不是立刻得出结果,所以在内存使用上会更优。
这样的推导语句同样适用于dictset

序列切片

liststr等对象的切片操作,可以优雅而高效的实现很多功能:

In [1]: l = range(10)  # 初始化一个list

In [2]: l
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: l[::-1]      # 逆序
Out[3]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [4]: l[5:]        # 切片,取第5个以后
Out[4]: [5, 6, 7, 8, 9]

In [5]: l[:5]        # 切片,取第五个及以前
Out[5]: [0, 1, 2, 3, 4]

In [6]: l[::2]       # 从第0位开始,每隔一位取一个数   
Out[6]: [0, 2, 4, 6, 8]

In [7]: l[1::2]      # 从第1位开始,每隔一位取一个数   
Out[7]: [1, 3, 5, 7, 9]

switch-case

Python没有switch-case语法,一般程序员会使用if-elif-else来模拟这样的操作,但是有经验的逼格高的程序员一般通过映射一个dict

# Java code
String int2en(int num) {
    switch(num) {
        case 1: return "one";
        case 2: return "two"; 
        case 3: return "there"; 
        case 4: return "four";
        case 5: return "five";
        default: return "out";
    }
}

普通程序员的Python实现:

def int2en(num):
    if num == 1:
        return "one"
    if num == 2:
        return "two"
    if num == 3:
        return "there"
    if num == 4:
        return "four"
    if num == 5:
        return "five"
    return "out"

推荐:

INT_EN_MAP = {
    1: 'one',
    2: 'two',
    3: 'there',
    4: 'four',
    5: 'five'
}

def int2en(num):
    return INT_EN_MAP.get(num, 'out')

拼接文件路径

一般比较常见的写法是连接字符串,但是这样的写法会导致另外的平台可能不可用,也容易出现拼接字符串的错误。比如OS X和Linux上使用”/”而Windows却使用”\”来区分文件路径。通过os.path.join函数来完成这样的操作:

import os
base = '/User/xlzd'

# this is bad
target = base + '/workspace'

# this is good
target = os.path.join(base, 'workspace')

参考这个专栏~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值