Some coolest things in python may be you don't know [Python part 3]
Some coolest things in python may be you don't know
Displaying Hello world in Java!
Displaying Hello world in python
1.Swapping of numbers
In other language
a=a+b;
b=a-b;
a=a-b;
In python language:
a,b=.b,a #swap two number without additional varible
2.Number System Conversion
print(bin(2))
print(hex(10))
Output->
0b10
0xa
3.Concept in 2 lines
a=int(input())
print(("Not Multiple of 5", "Multiple of 5")[a%5==0])
Input->
10
Output->
multiple of 5
4.Display Calender of any year
import calendar
cal = calendar.month(2017,11)
print(cal)
Output->
5. interpreter will store your last value. get it with the help of _
>>> 2**2
4
>>> _
4
>>>
6.Boolean takes interger value
a=[2,3,4,5]
print a[True]
print a[false]
output->
3
2
7. Reverse the list itself:
mylist = [1,2,3]
mylist.reverse()
print mylist
-> [3,2,1]
# Iterate in reverse
for element in reversed([1,2,3]): print element
-> 3
-> 2
-> 1
8.Splat call
def foo(a, b, c):
print a, b, c
mydict = {'a':1, 'b':2, 'c':3}
mylist = [10, 20, 30]
foo(*mydict)
-> a, b, c
foo(**mydict)
-> 1, 2, 3
foo(*mylist)
-> 10 20 30
9.Chaining comparisons: Rather than write something like this:
if mark> 50 and mark <= 100:
print("You passed the exam.")
elif mark >= 0 and mark <= 50:
print("You did NOT pass the exam.")
else:
print("Error: Invalid mark")
you can just write:
if 50 < mark <= 100: # Yup, this is valid Python syntax
print("You passed the exam.")
elif 0 <= mark <= 50:
print("You did NOT pass the exam.")
else:
print("Error: Invalid mark")
Just makes the code more shorter/readable.
10. type import this in python and run
Comments
Post a Comment