Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python tips and tricks

These are some tips and tricks for anyone who is interested:
-----
1. 
In-Place Swapping Of Two Numbers.


x, y = 10, 20
print(x, y)
x, y = y, x
print(x, y)

Output:
10 20
20 10
-----
2. 
Reversing a string in Python

a = "GeeksForGeeks"
print("Reverse is", a[::-1])

Output:
Reverse is skeeGroFskeeG
-----
3. 
Create a single string from all the elements in list

a = ["Geeks", "For", "Geeks"]
print(" ".join(a))

Output:
Geeks For Geeks
-----
4. 
Chaining Of Comparison Operators.

n = 10
result = 1 < n < 20
print(result)
result = 1 > n <= 9
print(result)

Output:
True
False
-----
5. 
Print The File Path Of Imported Modules.

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'>
-----
6. 
Use Of Enums In Python.


class MyName:
    Geeks, For, Geeks = range(3)
  
print(MyName.Geeks)
print(MyName.For)
print(MyName.Geeks)

Output:
2
1
2
-----
7. 
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
------
8. 
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
-----
9. 
Check The Memory Usage Of An Object.

import sys
x = 1
print(sys.getsizeof(x))

Output:
28
-----
10. 
Print string N times.

n = 2
a = "GeeksforGeeks"
print(a * n)

Output:
GeeksforGeeksGeeksforGeeks
-----
10+1. 
Checking if two words are anagrams


from collections import Counter
def is_anagram(str1, str2):
     return Counter(str1) == Counter(str2)
  
# or without having to import anything 
def is_anagram(str1, str2): 
    return sorted(str1) == sorted(str2) 
  
print(is_anagram('geek', 'eegk'))
print(is_anagram('geek', 'peek'))

Output:
True
False
Comment

python tricks

# Python's list slice syntax can be used without indices
# for a few fun and useful things:

# You can clear all elements from a list:
list = [1, 2, 3, 4, 5]
del lst[:]

print(list)

# Output
# []

# You can replace all elements of a list
# without creating a new list object:
a = list
lst[:] = [7, 8, 9]
print(list)

# Output
# [7, 8, 9]

print(a)

# Output
# [7, 8, 9]

print(a is list)

# Output
# True

# You can also create a (shallow) copy of a list:
b = list[:]
print(b)

# Output
# [7, 8, 9]

print(b is lst)

# Output
# False
Comment

PREVIOUS NEXT
Code Example
Python :: sort a list numbers in python 
Python :: python read entire file 
Python :: convert list to generator python 
Python :: discord bot python delete messages like mee6 
Python :: change dataframe value by index 
Python :: color name to hex python 
Python :: jinja macro import 
Python :: py env 
Python :: sieve of eratosthenes python 
Python :: get hash python 
Python :: Beautifulsoup - How to open images and download them 
Python :: split a variable into multiple variables in python 
Python :: python color input 
Python :: remove keys from dict python 
Python :: convert float in datetime python 
Python :: how to reverse a list in python without using inbuilt function 
Python :: how to run linux command in python 
Python :: django clear all sessions 
Python :: python remove key from dict 
Python :: python program for swapping position of two numbers 
Python :: df rename columns 
Python :: visitor IP address django 
Python :: python list prime numbers 
Python :: lambda condition python 
Python :: # find out of the memory of the python object 
Python :: how to read a csv file in python 
Python :: python comment block 
Python :: html.unescape python 
Python :: tkinter how to remove button boder 
Python :: graph 3d python 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =