Python初学者的捷径

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

交换变量值

技术分享
x = 6
y = 5
 
x, y = y, x
 
print x
>>> 5
print y
>>> 6
技术分享

内联if语句

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

联接

技术分享
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
技术分享

数字运算

技术分享
#Floor Division (rounds down)
print 5.0//2
>>> 2
 
#2 raised to the 5th power
print 2**5
>> 32
技术分享

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

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

数字对比

技术分享
x = 2
 
if 3 > x > 1:
    print x
>>> 2
 
if 1 < x > 0:
    print x
>>> 2
技术分享

同时遍历两个数组

 

技术分享
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
 
for teama, teamb in zip(nfc, afc):
    print teama + " vs. " + teamb
 
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
技术分享

 

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

遍历List并获得index

 

技术分享
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
    print index, team
 
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
技术分享

理解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得子集

很简单的取子集的方法:

技术分享
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]
技术分享

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

技术分享
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)
技术分享

False == True

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

技术分享
False = True
if False:
    print "Hello"
else:
    print "World"
 
>>> Hello
技术分享

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

 

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。