# Python_编程特色

2017-01-13 10:56:08来源:csdn作者:Jmilk人点击

forelse语句

Python有着许多非常有意思的有别于别的高级语言的语法和处理方式，这些特别的变成技巧能够实现更少的代码量，更清晰的语法结构和更高的效率。

IPython 4.0.0

[expr for variable in listName]

Example:

In [18]: [x**2 for x in range(10)]
Out[18]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [20]: [x for x in range(100) if x %5 == 0]
Out[20]: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

In [21]: newList = []In [22]: for x in range(100):
....: if x % 5 == 0:
....: newList.append(x)
....: In [23]: newList
Out[23]: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

[x for x in range(2,100) if not [y for y in range(2,int(x/2+1)) if x % y == 0]]

primes = []
for x in range(2,100):#1不是素数
li = [] #临时的List对象，只在for语句块中有效
for y in range(2,x/2+1):#因为在range()中，所以要+1补足
if x % y == 0:
li.append(x)
if not li:#如果临时的li中为空，表示N为素数
primes.append(x)
print primes

noprimes = []
for i in range(2,8):
for j in range(i*2,100,i):
noprimes.append(j)
set(noprimes)
primes = []
for x in range(2,100):
if x not in noprimes:
primes.append(x)
print primes

noprimes = set(j for i in range(2,8) for j in range(i*2, 100,i))
primes = [x for x in range(2,100) if x not in noprimes]

In [143]: nestLi = [[1,2,3],[4,5,6],[7,8,9]]In [144]: newLi = [x for para in nestLi for x in para]In [145]: newLi
Out[145]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

newLi = []
nestLi = [[1,2,3],[4,5,6],[7,8,9]]
for para in nestLi:
for x in para:
newLi.append(x)print newLi

In [3]: from random import randintIn [4]: [randint(1, 10) for i in range(2)]
Out[4]: [10, 4]In [5]: [randint(1, 10) for i in range(2)]
Out[5]: [7, 8]In [6]: [randint(1, 10) for i in range(2)]
Out[6]: [3, 4]In [7]: [randint(1, 10) for i in range(2)]
Out[7]: [10, 8]In [8]: [randint(1, 10) for i in range(2)]
Out[8]: [1, 6]字典的默认值

In [153]: dic = {'name':'JMilk','age':23}In [154]: dic['city'] = dic.get('city','BJ')In [155]: dic
Out[155]: {'age': 23, 'city': 'BJ', 'name': 'JMilk'}

for…else语句In [173]: pycat test.py
for x in xrange(5):
if x == 5:
print 'I can find 5!'
else:
print 'I cannot find 5!'In [174]: run test.py
I cannot find 5!

Out[4]: (2, 1)

Out[30]: True

Python真值表在运算符详解篇已经有过了介绍，传送门：http://blog.csdn.net/jmilk/article/details/48666475

In [36]: name = 'Jmilk'In [37]: dic = {'age': 23, 'name': 'Jmilk'}In [38]: li = ['Hi','Jmilk']In [39]: if name and dic and li:
....: print 'All True!'
....:
All True!

Out[51]: 'Hello JMilk!'In [52]: li
Out[52]: ['Hi', 'Jmilk']In [53]: tup = ('hello','world!')In [54]: str[::-1]
Out[54]: '!kliMJ olleH'In [55]: li[::-1]
Out[55]: ['Jmilk', 'Hi']In [56]: tup[::-1]
Out[56]: ('world!', 'hello')

In [70]: str = ' 'In [71]: li
Out[71]: ['Hi', 'Jmilk']In [72]: str.join(li)
Out[72]: 'Hi Jmilk'

Python一切皆对象的思想，造就了非常多的针对数据类型的内建函数。

Python除了有大量的模块之外，还有着丰富的内置函数。 求和：

In [1]: num = [1,2,3]In [2]: sum(num)
Out[2]: 6

In [3]: max(num)
Out[3]: 3

In [4]: min(num)
Out[4]: 1

In [84]: from operator import mulIn [85]: reduce(mul,range(1,5),1)
Out[85]: 24

In [1]: a = -100In [2]: abs(a)
Out[2]: 100

In [6]: divmod(10,3)
Out[6]: (3, 1) #商==3;余数==1

In [11]: pow(2,4)
Out[11]: 16#相当于 2**4In [12]: pow(2,4,5)
Out[12]: 1 #相当于 2**4%5

In [14]: round(1)
Out[14]: 1.0利用zip()来创建键值对

zip(seq1 [, seq2 […]]) -> [(seq1[0], seq2[0] …), (…)] 将两个序列参数，转换成Tuple和List的组合。

In [175]: keys = ['Name', 'Sex', 'Age']In [176]: values = ['Tim', 'Male', 23]In [177]: zip(keys, values)
Out[177]: [('Name', 'Tim'), ('Sex', 'Male'), ('Age', 23)]In [178]: dict(zip(keys,values))
Out[178]: {'Age': 23, 'Name': 'Tim', 'Sex': 'Male'}最后

Jmilk