Py-Facts — 10 interesting facts about Python

Python Methods and Functions

Currently At the time, Python is one of the most popular programming languages ​​due to its readability and simplicity of code. All thanks to Guido Van Rossum, its creator.

I've put together a list of 10 interesting facts in Python. They are:

1. There is actually a poem written by Tim Peters called "The Zen of Python" that can be read simply by writing it in an interpreter.

# Try to guess the result before running it

import this

Output:

 Python Zen, Tim Peters Nice is better than ugly. Explicit is better than implicit. Simple is better than complex. Hard is better than hard. The apartment is better than the invested one. Sparse is better than tight. Readability matters. Special cases aren't special enough to break the rules. Although practicality trumps cleanliness. Errors should never be silent. If you are clearly not silent. In the face of ambiguity, resist the temptation to guess. There should be one - and preferably only one - obvious way to do this. Although this path may not be obvious at first, unless you are Dutch. Now is better than never. Though it never gets better than * right * now. If the implementation is difficult to explain, this is a bad idea. If the implementation is easy to explain, it might be a good idea. Namespaces are one great idea - let's do more! 

2. Multiple values ​​can be returned in Python. Do not trust? See the code snippet below:

# 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:

 (1, 2, 3, 4, 5) 

3. In Python, you can use an "else" clause with a "for" loop. This is a special type of syntax that is only executed if the for loop naturally terminates without any break statements.

def func (array):

for num in array:

if num % 2 = = 0 :

print (num)

break  # Case1: Break is called, so "else" will not be executed.

else : # Case 2: & # 39; else & # 39; executed because no break is called

print ( " No call for Break. Else is executed "

 

print ( " 1st Case: " )

a = [ 2 ]

func (a)

print ( "2nd Case:" )

a = [ 1 ]

func (a)

 

Exit:

 1st case: 2 2nd case: No call to break. The rest is executed 

4. In Python, everything is done by reference. It does not support pointers.

5. Function Argument Unpacking — another great feature of Python. It is possible to unpack a list or dictionary as function arguments using * and ** respectively. This is commonly known as the Splat operator. An example is here

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

Output:

 3 4 2 3 

/ strong> 6. > Want to find the index inside the for loop? Wrap the iterable with & # 39; enumerate & # 39; and it will return the element along with its index. Check out this code snippet

# Know the index faster

vowels = [ 'a' , 'e' , ' i' , 'o' , ' u' ]

for i, letter in enumerate (vowels):

print (i, letter)

Output:

 (0, "a") (1, & # 39; e & # 39;) (2, "i") (3, & # 39; o & # 39;) (4, & # 39; y & # 39;) 

7. Comparison operators can be combined in Python. Answer = 1 & lt; x & lt; 10 — executable in Python. More examples here

# String comparison operators

i = 5

 

ans = 1 & lt; i & lt;  10

print (ans)

 

ans = 10 & gt; i & lt; = 9

print (ans)

 

ans = 5 = = i

print (ans)

Output:

 True True True 

8. Can't we define infinity correctly? But wait! Not for Python. See this awesome example

# Positive Infinity

p_infinity = float ( 'Inf' )

 

if 99999999999999 & gt; p_infinity:

print ( "The number is greater than Infinity!" )

else :

print ( "Infinity is greatest" )

 
# Negetive Infinity

n_infinity = float ( '-Inf' )

if - 99999999999999 & lt; n_infinity:

print ( "The number is lesser than Negative Infinity!" )

else :

  print ( "Negative Infinity is least" )

Output:

 Infinity is the largest Negative Infinity is the minimum 

9. Instead of creating a list with a loop, you can build it more succinctly with using list comprehension. See this code for more details.

# Simple list Add

a = []

for x in range ( 0 , 10 ) :

a.append (x)

print (a)

 
# Understanding the list

print ([x for x in a])

Output:

 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

10. Finally, the Python Slice special operator. It is a way to get items from lists and also to modify them. Check out this code snippet

Slice statement

a = [ 1 , 2 , 3 , 4 , 5 ]

 

print (a [ 0 : 2 ]) # Select items [0-2), no upper border is included

 

print (a [ 0 : - 1 ]) # Select all but the last

 

print (a [:: - 1 ]) # Flip the list

 

print (a [:: 2 ]) # Skip by 2

 

print (a [:: - 2 ]) # Skip -2 behind

Exit: < / p>

 [1, 2] [1, 2, 3, 4] [5, 4, 3, 2, 1] [1, 3, 5] [5, 3, 1] 

This article is provided by Harshit Gupta . If you like Python.Engineering and would like to contribute, you can also write an article and mail it to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic under discussion