Must Know Python Tricks

Here are some cool python tricks to play around with.

Trick # 1 : Setting Multiple Values
#Set multi values
i =  [57, 909, 8]
x, y, z = i

print(x,y,z)
Output :

57 909 8

Trick # 2 : Swap Values
#swap numbers
x=25
y=50
y, x =x, y

print (x , y )

Output :

50 25

Trick # 3 : Print X Times
#Print X number of times
x = 5
String = "Code It "

print(String * x)
Output :

Code It Code It Code It Code It Code It

Trick # 4 : Find Most Recurring Number
#Most recurring value
List = [21, 55, 21, 34, 21, 21, 76, 76, 92, 99, 65]

print(max(set(List), key = List.count))
Output :

21

Trick # 5 : Reverse Order of a List
# Reversing Numbers
list = [5,10,15,20]
print( list [::-1])

20, 15, 10, 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s