Sitemap

Python it like a master!! (2022)

2 min readAug 10, 2022

--

The 8 best and most groundbreaking blows to give to your software

[Insert a boring paragraph about a programming language we all know]

Without further ado, the cool, unbeatable, and awesome 8 half-price python tricks (half of zero is nothing):

Press enter or click to view image in full size

1. In-Place Swapping Of Two Numbers:

a, b = 1, 2

print(x, y)

x, y = y, x

print(x, y)

Output:

1 2
2 1

2. Reversing a string:

a = 'Python'

print(a[::-1])

Output:

nohtyP

3. Create a single string from all the elements in list:

a = ['Python', 'it', 'like', 'a', 'master']

print(" ".join(a))

Output:

Python it like a master

4. Chaining Of Comparison Operators:

n = 12

result = 1 < n < 20

print(result)

result = 1 > n <= 9

print(result)

Output:

True
False

4. Print The File Path Of Imported Modules:

Get James Smith’s stories in your inbox

Join Medium for free to get updates from this writer.

import os

import socket

print(os)

print(socket)

Output:

<module 'os' from '/usr/lib/python3.5/os.py'>
<module 'socket' from '/usr/lib/python3.5/socket.py'>

5. Return Multiple Values From Functions:

def x():

return 1, 2, 3, 4

a, b, c, d = x()

print(a, b, c, d)

Output:

1 2 3 4

6. Find The Most Frequent Value In A List:

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]

print(max(set(test), key = test.count))

Output:

4

7. Check The Memory Usage Of An Object:

import sys

x = 1

print(sys.getsizeof(x))

Output:

28

8. Print string N times:

n = 2

a = 'Python'

print(a * n)

Output:

PythonPython

--

--