python之常用知识点

1、for/while 循环后的else用法

在Python中的while或者for循环之后还可以有else子句,形如下:

    for x in range(1,5):
        if x == 6 :
           print "found the number",x
           break;
    else:
         print "not found!"

我们先来看看python官方文档中的解释,原文在这儿

Loop statements may have an else clause; it is executed when the loop terminates through

exhaustion of the list (with for) or when the condition becomes false (with while),

but not when the loop is terminated by a break statement.


即:循环语句后可以有一个else从句,这个else从句在因为for循环中list全部遍历完或者因为while循环中条件不满足而执行,
而如果循环中有break语句执行了则else从句就不执行了
。。

 

 

2、python中is的用法

 

3、如何删除一个集合中的元素

手册地址:http://docs.python.org/2/library/sets.html

The following table lists operations available for set that do not apply to immutable instances of frozenset:

update(other, ...)
set |= other | ...

Update the set, adding elements from all others.

Changed in version 2.6: Accepts multiple input iterables.

intersection_update(other, ...)
set &= other & ...

Update the set, keeping only elements found in it and all others.

Changed in version 2.6: Accepts multiple input iterables.

difference_update(other, ...)
set -= other | ...

Update the set, removing elements found in others.

Changed in version 2.6: Accepts multiple input iterables.

symmetric_difference_update(other)
set ^= other

Update the set, keeping only elements found in either set, but not in both.

add(elem)

Add element elem to the set.

remove(elem)

Remove element elem from the set. Raises KeyError if elem is not contained in the set.

discard(elem)

Remove element elem from the set if it is present.

pop()

Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.

clear()

Remove all elements from the set.

Note, the non-operator versions of the update(), intersection_update(), difference_update(), and symmetric_difference_update() methods will accept any iterable as an argument.

Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value.

 

OperationEquivalentResult

len(s)

 

cardinality of set s

x in s

 

test x for membership in s

x not in s

 

test x for non-membership in s

s.issubset(t)

s <= t

test whether every element in s is in t

s.issuperset(t)

s >= t

test whether every element in t is in s

s.union(t)

s | t

new set with elements from both s and t

s.intersection(t)

s & t

new set with elements common to s and t

s.difference(t)

s - t

new set with elements in s but not in t

s.symmetric_difference(t)

s ^ t

new set with elements in either s or t but not both

s.copy()

 

new set with a shallow copy of s

 

OperationEquivalentResult

s.update(t)

s |= t

return set s with elements added from t

s.intersection_update(t)

s &= t

return set s keeping only elements also found in t

s.difference_update(t)

s -= t

return set s after removing elements found in t

s.symmetric_difference_update(t)

s ^= t

return set s with elements from s or t but not both

s.add(x)

 

add element x to set s

s.remove(x)

 

remove x from set s; raises KeyError if not present

s.discard(x)

 

removes x from set s if present

s.pop()

 

remove and return an arbitrary element from s; raises KeyError if empty

s.clear()

 

remove all elements from set s

 

4、python的类型换行

函数                      描述
int(x [,base ])         将x转换为一个整数
long(x [,base ])        将x转换为一个长整数
float(x )               将x转换到一个浮点数
complex(real [,imag ])  创建一个复数
str(x )                 将对象 x 转换为字符串
repr(x )                将对象 x 转换为表达式字符串
eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s )               将序列 s 转换为一个元组
list(s )                将序列 s 转换为一个列表
chr(x )                 将一个整数转换为一个字符
unichr(x )              将一个整数转换为Unicode字符
ord(x )                 将一个字符转换为它的整数值
hex(x )                 将一个整数转换为一个十六进制字符串
oct(x )                 将一个整数转换为一个八进制字符串

 

 

posted @ 2013-08-29 20:37  开心星  阅读(168)  评论(0)    收藏  举报