Py-Facts——关于Python的 10 个有趣的事实
由于其代码可读性和简单性, Python是当今最流行的编程语言之一。这一切都要感谢它的创造者 Guido Van Rossum。
我整理了一份Python语言中 10 个有趣的事实的列表。他们来了:
1.实际上有一首 Tim Peters 写的诗,名为 THE ZEN OF PYTHON,只需在解释器中输入 import this 即可阅读。
Python3
# Try to guess the result before you actually run it
import this
Python3
# Multiple Return Values in Python!
def func():
return 1, 2, 3, 4, 5
one, two, three, four, five = func()
print(one, two, three, four, five)
Python3
def func(array):
for num in array:
if num%2==0:
print(num)
break # Case1: Break is called, so 'else' wouldn't be executed.
else: # Case 2: 'else' executed since break is not called
print("No call for Break. Else is executed")
print("1st Case:")
a = [2]
func(a)
print("2nd Case:")
a = [1]
func(a)
Python3
def point(x, y):
print(x,y)
foo_list = (3, 4)
bar_dict = {'y': 3, 'x': 2}
point(*foo_list) # Unpacking Lists
point(**bar_dict) # Unpacking Dictionaries
Python3
# Know the index faster
vowels=['a','e','i','o','u']
for i, letter in enumerate(vowels):
print (i, letter)
Python3
# Chaining Comparison Operators
i = 5;
ans = 1 < i < 10
print(ans)
ans = 10 > i <= 9
print(ans)
ans = 5 == i
print(ans)
Python3
# Positive Infinity
p_infinity = float('Inf')
if 99999999999999 > p_infinity:
print("The number is greater than Infinity!")
else:
print("Infinity is greatest")
# Negative Infinity
n_infinity = float('-Inf')
if -99999999999999 < n_infinity:
print("The number is lesser than Negative Infinity!")
else:
print("Negative Infinity is least")
Python3
# Simple List Append
a = []
for x in range(0,10):
a.append(x)
print(a)
# List Comprehension
print([x for x in a])
Python3
# Slice Operator
a = [1,2,3,4,5]
print(a[0:2]) # Choose elements [0-2), upper-bound noninclusive
print(a[0:-1]) # Choose all but the last
print(a[::-1]) # Reverse the list
print(a[::2]) # Skip by 2
print(a[::-2]) # Skip by -2 from the back
输出:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
2. Python可以返回多个值。不相信?请参见下面的代码片段:
Python3
# Multiple Return Values in Python!
def func():
return 1, 2, 3, 4, 5
one, two, three, four, five = func()
print(one, two, three, four, five)
输出:
(1, 2, 3, 4, 5)
3.可以在Python中使用带有“for”循环的“else”子句。这是一种特殊类型的语法,只有在 for 循环自然退出时才会执行,没有任何 break 语句。
Python3
def func(array):
for num in array:
if num%2==0:
print(num)
break # Case1: Break is called, so 'else' wouldn't be executed.
else: # Case 2: 'else' executed since break is not called
print("No call for Break. Else is executed")
print("1st Case:")
a = [2]
func(a)
print("2nd Case:")
a = [1]
func(a)
输出:
1st Case:
2
2nd Case:
No call for Break. Else is executed
4.在Python中,一切都是通过引用完成的。它不支持指针。
5.函数参数解包是Python的另一个很棒的特性。可以分别使用 * 和 ** 将列表或字典解包为函数参数。这通常称为 Splat运算符。这里的例子
Python3
def point(x, y):
print(x,y)
foo_list = (3, 4)
bar_dict = {'y': 3, 'x': 2}
point(*foo_list) # Unpacking Lists
point(**bar_dict) # Unpacking Dictionaries
输出:
3 4
2 3
6.想在 for 循环中找到索引?用 'enumerate' 包装一个可迭代对象,它将产生该项目及其索引。请参阅此代码段
Python3
# Know the index faster
vowels=['a','e','i','o','u']
for i, letter in enumerate(vowels):
print (i, letter)
输出:
(0, 'a')
(1, 'e')
(2, 'i')
(3, 'o')
(4, 'u')
7.在Python中可以链接运算符answer= 1 输出: 8.我们不能定义无穷大对吧?可是等等!不适用于Python。看看这个惊人的例子 输出: 9.与其使用循环构建列表,不如使用列表推导式更简洁地构建它。请参阅此代码以获得更多理解。 输出: 10.最后,Python 的特殊切片运算符。这是一种从列表中获取项目以及更改它们的方法。请参阅此代码段 输出: Python3
# Chaining Comparison Operators
i = 5;
ans = 1 < i < 10
print(ans)
ans = 10 > i <= 9
print(ans)
ans = 5 == i
print(ans)
True
True
True
Python3
# Positive Infinity
p_infinity = float('Inf')
if 99999999999999 > p_infinity:
print("The number is greater than Infinity!")
else:
print("Infinity is greatest")
# Negative Infinity
n_infinity = float('-Inf')
if -99999999999999 < n_infinity:
print("The number is lesser than Negative Infinity!")
else:
print("Negative Infinity is least")
Infinity is greatest
Negative Infinity is least
Python3
# Simple List Append
a = []
for x in range(0,10):
a.append(x)
print(a)
# List Comprehension
print([x for x in a])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Python3
# Slice Operator
a = [1,2,3,4,5]
print(a[0:2]) # Choose elements [0-2), upper-bound noninclusive
print(a[0:-1]) # Choose all but the last
print(a[::-1]) # Reverse the list
print(a[::2]) # Skip by 2
print(a[::-2]) # Skip by -2 from the back
[1, 2]
[1, 2, 3, 4]
[5, 4, 3, 2, 1]
[1, 3, 5]
[5, 3, 1]