In this article, we will learn about what all features makes python cool and different from other languages.
>>>import this
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. The 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!
Swapping of two variables in one line
We can assign values to multiple variables at the same time in the form of single statement as shown below
Example
a = 201 b = 786 print("Before swapping value of a ="+str(a)+" and b = "+str(b)) #Before swapping value a, b = b, a print("After swapping value of a ="+str(a)+" and b = "+str(b)) #After swapping value
Output
Before swapping value of a =201 and b = 786 After swapping value of a =786 and b = 201
Enumerate type
The enumerated type is used to traverse the list and similar types without actually knowing their lengths.
Example
mylist = ['t','u','t','o','r','i','a','l'] for i, value in enumerate(mylist): print( i, ': ', value)
Output
0 : t 1 : u 2 : t 3 : o 4 : r 5 : i 6 : a 7 : l
Zip method
By using zip method we can traverse over multiple lists at the same time as shown in the code below.
Example
mylist1 = ['t','u','t','o','r','i','a','l'] mylist2 = ['p','o','i','n','t'] for i,j in zip(mylist1,mylist2): print( i, ':', j)
Output
t : p u : o t : i o : n r : t
Reversing the list
By using built-in reversed method() we can directly obtain the reversed list without any looping construct
Example
list_inp = ['t','u','t','o','r','i','a','l'] print(list(reversed(list_inp)))
Output
['l', 'a', 'i', 'r', 'o', 't', 'u', 't']
Use Interactive "_" Operator.
This operator is used on the command line to print or display the output of the previous operation performed.
>>> 12 + 12 24 >>> _ 24 >>> print(_) 24
As we all are aware that there is no need for data type declaration in Python and we can change the data type of variables multiple times in a program.
Conclusion
In this article, we learnt about what all features are present in Python which makes it cool and more attractive to programmers.