-
Here I'm discussing about most amazing and interesting facts about Python
- There is actually a poem written by Tim Peters named as THE ZEN OF PYTHON which can be read by just writing import this in the interpreter.
#Try to guess the result before you actually run it
import this
Now see the output....
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!
this is actually a amazing fact Right??
-
2.One can use an “else” clause with a “for” loop in Python.
- It’s a special type of syntax that executes only if the for loop exits naturally, without any break statements.
def func(array):
for num in array:
if num%2==0:
print(num)
break
else:
print("No call for Break. Else is executed")
print("1st Case:")
a = [2]
func(a)
print("2nd Case:")
a = [1]
func(a)
output is....
1st Case:
2
2nd Case:
No call for Break. Else is executed
-
3.One can return multiple values in Python
# 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)
- Output is... (1,2,3,4,5)
-
4.Want to find the index inside a for loop?
- Wrap an iterable with ‘enumerate’ and it will yield the item along with its index.ie
# Know the index faster
vowels=['a','e','i','o','u']
for i, letter in enumerate(vowels):
print (i, letter)
Output is...
(0, 'a')
(1, 'e')
(2, 'i')
(3, 'o')
(4, 'u')
# Positive Infinity
p_infinity = float('Inf')
if 99999999999999 > p_infinity:
print("The number is greater than Infinity!")
else:
print("Infinity is greatest")
# Negetive Infinity
n_infinity = float('-Inf')
if -99999999999999 < n_infinity:
print("The number is lesser than Negative Infinity!")
else:
print("Negative Infinity is least")
Output is
Infinity is greatest Negative Infinity is least
These are some amazing facts about python