交换变量

x = 6
y = 5

x, y = y, x

print x
>>> 5
print y
>>> 6
if 句子在内行人
print "Hello" if True else "World"
>>> Hello
联接
下边的最终一种方法在关联2个不一样种类的目标时看起来很帅。

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
测算方法
#向下取整
print 5.0//2
>>> 2
# 2的5次方
print 2**5
>> 32
留意浮点数的除法
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
2个目录另外迭代更新
zip函数全部标准都考虑时才会开展循环系统 ,随意个目录解析xml完都是会跳出循环

nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
for teama, teamb in zip(nfc, afc):
print teama " vs. " teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
带数据库索引的目录迭代更新
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
目录计算
已经知道一个目录 ,刷挑选出双数目录方式:

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]
词典计算
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print {key: value for value, key in enumerate(teams)}
>>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
复位目录的值
items = [0]*3
print items
>>> [0,0,0]
将目录转化成字符串数组
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print ", ".join(teams)
>>> 'Packers, 49ers, Ravens, Patriots'
从词典中获得原素
不能用下述的方法

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)
获得子目录
x = [1,2,3,4,5,6]
#前3个
print x[:3]
>>> [1,2,3]
#正中间4个
print x[1:5]
>>> [2,3,4,5]
#最终3个
print x[-3:]
>>> [4,5,6]
#合数项
print x[::2]
>>> [1,3,5]
#双数项
print x[1::2]
>>> [2,4,6]
60字符处理FizzBuzz
前不久Jeff Atwood 营销推广了一个简易的编程练习叫FizzBuzz,难题引入以下:

写一个程序流程,复印数据1到100 ,3的倍数复印“Fizz”来更换这一数,5的倍数复印“Buzz ”,针对既是3的倍数也是5的倍数的数据复印“FizzBuzz ” 。

这儿有一个简洁明了的方式处理这个问题:

for x in range(101):print"fizz"[x%3*4::] "buzz"[x%5*4::]or x
结合
采用Counter库

from collections import Counter
print Counter("hello")
>>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
迭代更新专用工具
和collections库一样 ,还有一个库叫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

 

 

强烈推荐一下我建的python交流学习QQqun:850973621 ,群内有免费的视频实例教程,开发环境 、
电子书 、新项目源码分享 。一起学习交流,一起发展!

![QQ截屏20201205144328.png](https://upload-images.jianshu.io/upload_images/25205170-147de4930a92262d.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)

![QQ截屏20201210135017.png](https://upload-images.jianshu.io/upload_images/25205170-725ff1ae1cddc996.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)

![QQ截屏20201210135040.png](https://upload-images.jianshu.io/upload_images/25205170-4cc63e05465d38ca.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)

 

 

创作者:大明白
连接:https://www.jianshu.com/p/883db10b506a
来源于:百度百家
版权归创作者全部。商业服务转截请联络创作者得到 受权 ,非商用转截请标明出處。

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归趣营销www.SEOgUrublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ卍61910465