Python初学者的捷径[译]

下面列出的都是这些年总结的Python的有用的知识点和一些工具。希望对你有所帮助!

交换变量值

Python初学者的捷径[译]
x = 6
y = 5
 
x, y = y, x
 
print x
>>> 5
print y
>>> 6
Python初学者的捷径[译]

内联if语句

print "Hello" if True else "World"
>>> Hello

联接

Python初学者的捷径[译]
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
print nfc + afc
>>> ['Packers', '49ers', 'Ravens', 'Patriots']
 
print str(1) + " world"
>>> 1 world
 
print `1` + " world"
>>> 1 world
 
print 1, "world"
>>> 1 world
print nfc, 1
>>> ['Packers', '49ers'] 1
Python初学者的捷径[译]

数字运算

Python初学者的捷径[译]
#Floor Division (rounds down)
print 5.0//2
>>> 2
 
#2 raised to the 5th power
print 2**5
>> 32
Python初学者的捷径[译]

注意float数做整除运算后的出来的还是整数

print .3/.1
>>> 2.9999999999999996
 
print .3//.1
>>> 2.0

数字对比

Python初学者的捷径[译]
x = 2
 
if 3 > x > 1:
    print x
>>> 2
 
if 1 < x > 0:
    print x
>>> 2
Python初学者的捷径[译]

同时遍历两个数组

 

Python初学者的捷径[译]
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
 
for teama, teamb in zip(nfc, afc):
    print teama + " vs. " + teamb
 
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
Python初学者的捷径[译]

 

延伸阅读zip方法,请点这里

遍历List并获得index

 

Python初学者的捷径[译]
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
    print index, team
 
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
Python初学者的捷径[译]

理解List

这个:

numbers = [1,2,3,4,5,6]
even = []
for number in numbers:
    if number%2 == 0:
        even.append(number)

可以写成:

numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 == 0]

理解Dictionary

和List很相似,Dictionary的这个可以写成:

teams = ["Packers", "49ers", "Ravens", "Patriots"]
print {key: value for value, key in enumerate(teams)}
>>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}

初始化List的值

items = [0]*3
print items
>>> [0,0,0]

List转换为String

teams = ["Packers", "49ers", "Ravens", "Patriots"]
print ", ".join(teams)
>>> 'Packers, 49ers, Ravens, Patriots'

从Dictionary中取得Item

一般来说是这样的,用一个try-except块包裹起来以防要取得的key值不存在:

data = {'user': 1, 'name': 'Max', 'three': 4}
try:
    is_admin = data['admin']
except KeyError:
    is_admin = False

但是你可以这样:

data = {'user': 1, 'name': 'Max', 'three': 4}
is_admin = data.get('admin', False)

取List得子集

很简单的取子集的方法:

Python初学者的捷径[译]
x = [1,2,3,4,5,6]
 
#First 3 
print x[:3]
>>> [1,2,3]
 
#Middle 4
print x[1:5]
>>> [2,3,4,5]
 
#Last 3
print x[-3:]
>>> [4,5,6]
 
#Odd numbers
print x[::2]
>>> [1,3,5]
 
#Even numbers
print x[1::2]
>>> [2,4,6]
Python初学者的捷径[译]

60个字符搞定FizzBuzz问题

FizzBuzz问题:写一个程序打印1到100的数字。遇到3的倍数打印“Fizz”来替换这个数。5的倍数打印“Buzz“,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”代替这个数字

for x in range(1,101):print"Fizz"[x%3*4:]+"Buzz"[x%5*4:]or x

这里就是List的一个知识点了。如果方括号里,冒号左边的数字式大于字符串长度的,那么什么都不会输出!也就是在非3或者5的倍数的时候什么都不会输出。

集合

在collections模块下的Counter,有时候也是很有用的。

from collections import Counter
 
print Counter("hello")
>>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})

Itertools

Python初学者的捷径[译]
from itertools import combinations
 
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for game in combinations(teams, 2):
    print game
 
>>> ('Packers', '49ers')
>>> ('Packers', 'Ravens')
>>> ('Packers', 'Patriots')
>>> ('49ers', 'Ravens')
>>> ('49ers', 'Patriots')
>>> ('Ravens', 'Patriots')
Python初学者的捷径[译]

False == True

在python里,True和False只不过是全局变量,所以:

Python初学者的捷径[译]
False = True
if False:
    print "Hello"
else:
    print "World"
 
>>> Hello
Python初学者的捷径[译]

如果你发现什么有趣的东西欢迎在下面补齐!

 

from:http://www.maxburstein.com/blog/python-shortcuts-for-the-python-beginner/

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/4294761.html
上一篇:程序员从初级到中级10个秘诀


下一篇:第二章 时间控件(DateTime Picker)