快来看啊!原来Python里还有这些的一些有趣的东西!

简介: 快来看啊!原来Python里还有这些的一些有趣的东西!

相信大家使用python也或多或少有一段时间了,只是用它来写代码,却没有发现它里面其实有很多神奇的事情

Python 作为一门开源的语言,它的社区为其贡献了一些十分幽默的东西。之前有一篇《Python中的惊喜彩蛋》已经给大家做过介绍了,这次我们就来看看新的更好玩的东西吧~


TIPS:如果您想观察彩蛋,可以加上休眠时间。



640.jpg



来只乌龟压压惊


Python 有一个酷酷的很有趣的库,叫turtle,他是用来画图的,但是他的本质就是只小乌龟哦!没想到它真的能看出小乌龟吧!来看看吧:

import turtle
import time
t =turtle.Pen()
t.shape("turtle")
for i in range(1000):
    time.sleep(1)
    t.forward(i)


(仔细看!

640.png




数字最大是多少

在Python中,无穷大的hash是10^5×π。有趣的是, float(“-inf”)的hash在Python 3版本里是“-10^5×π”而在Python 2版本里是“-10^5×e”。(虽然Python2在20天以前已经不在维护了~)


>>> infinity = float('infinity')
>>> hash(infinity)
314159
>>> hash(float('-inf'))
-314159
3

使用re.DEBUG查看正则表达式的匹配过程

正则表达式是Python的一大特色,但是调试起来会很痛苦,很容易得出一个bug。幸运的是,Python可以打印出正则表达式的解析树,通过re.debug来显示re.compile的完整过程。一旦你理解了语法,你就可以发现你的错误。

re.compile("^\[font(?:=(?P<size>"
    "[-+][0-9]{1,2}))?\](.*?)[/font]",
    re.DEBUG)
at at_beginning
literal 91
literal 102
literal 111
literal 110
literal 116
max_repeat 0 1
  subpattern None
    literal 61
    subpattern 1
      in
        literal 45
        literal 43
      max_repeat 1 2
        in
          range (48, 57)
literal 93
subpattern 2
  min_repeat 0 65535
    any None
in
  literal 47
  literal 102
  literal 111
  literal 110
  literal 116



enumerate函数

enumerate()是python的内置函数

enumerate在字典上是枚举、列举的意思

对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值

enumerate多用于在for循环中得到计数


>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>>list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>>list(enumerate(seasons, start=1))       # 小标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]




面试官灵魂拷问:函数默认参数

面试官们经常在试中出这样的一道题目:

def hello(a=[]):
    a.append(1)
    return a

调用这个函数的结果是多少,我再调用一次,结果是多少?

很明显结果不可能都是[1]

print(hello())
[1]
print(hello())
[1, 1]
print(hello())
[1, 1, 1]



万能的eval()函数

将字符串str当成有效的表达式来求值并返回计算结果。

它可以把字符串里面的任何结构都变出来,还可以计算表达式的值。非常的灵活好用。例子如下:

str1 = "[1,2,3,4,5]"
print(eval(str1), type(eval(str1)))
[1, 2, 3, 4, 5] <class 'list'>
str2 = "1+2-3*4/(2-5)"
print(eval(str2), type(eval(str2)))
7.0 <class 'float'>
str3 = "{1:'a', 2:'c'}"
print(eval(str3), type(eval(str3)))
{1: 'a', 2: 'c'} <class 'dict'>



Python的爱情观

python也有自己的爱情观,这个大家就不知道了吧!!!哈哈。

import this
love = this

这就是爱!

this is love
True

爱是对的!不对!

love is True
False

爱是错的!不对!

love is False
False

爱不分对错!嗯!

love is not True or False
True

爱就是爱啊!

love is love
True


大家如果感觉还不错的话,就请点个'在看'吧!感谢!

相关文章
|
5月前
|
JavaScript 前端开发 Python
bddExcel for Python
本文介绍如何使用Python建立bddExcel实现BDD,注意bddExcel没有用到Cucumber。
45 0
bddExcel for Python
|
9月前
|
机器学习/深度学习 数据采集 运维
python实际应用
python实际应用
83 1
|
Python
Python|取珠宝问题
Python|取珠宝问题
82 0
|
IDE 开发工具 Python
万事开头难——正确开始使用Python
万事开头难——正确开始使用Python
97 0
万事开头难——正确开始使用Python
python
alink
100 0
|
机器学习/深度学习 Python
(Python)矩阵旋转
(Python)矩阵旋转
|
Python
python:哈么雷特
week6_2.py 请在...处补充代码 def getText():
125 0
|
Python
Python小技巧
One For All!!! 导包 取别名 from math as foobar from module1 import open as open1 from module2 import open as open2 查找Python的模块位置 >>> import sys,pprint >>> pprint.
843 0
|
存储 API Python
Python 3.7.0 来了!
Python官网静悄悄地发布了一条大消息:正式发布 Python 3.7.0!同时发布的还有Python 3.6.6稳定版。官网刚刚更新了可下载文档,还在用Python 2.7和Python3.5的你,赶紧用起来!
5212 0