Change language

10 essential Python tips and tricks for programmers

| |

1. Replacing two numbers in place.

x, y = 10 , 20

print (x, y)

x, y = y, x

print (x, y)

Output:

 10 20 20 10 

2. Line breaking in Python

a = " GeeksForGeeks "

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

Output:

 Reverse is skeeGroFskeeG 

3. Create one row from all the elements in the list

a = [ "Geeks" , "For" , "Geeks" ]

print ( "" .join (a))

Exit:

 Geeks For Geeks 

4. Concatenation of comparison operators.

n = 10

result = 1 & lt; n & lt;  20

print (result)

result = 1 " n " = 9

print (result)

Exit:

 True False 

4. Type the path to the imported modules file.

import os; 

import socket; 

 

print (os)

print (socket)

Exit:

 "module ’os’ from’ / usr / lib / python3.5 / os.py’" "module ’socket’ from’ /usr/lib/python3.5/socket.py’> 

5. Using Enums in Python.

class MyName:

Geeks, For, Geeks = range ( 3 )

 

print (MyName.Geeks )

print (MyName.For)

print (MyName.Geeks)

Exit :

 2 1 2 

6. Return multiple values ​​from functions.

def x ():

return 1 , 2 , 3 , 4

a, b, c, d = x ()

 

print (a, b, c, d)

Exit :

 1 2 3 4 

7. Find the most common value in the list.

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

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

Exit:

 4 

8. Check object memory usage.

import sys

x = 1

print (sys.getsizeof (x))

Exit :

 28 

9. Print a line N times.

n = 2

a = "GeeksforGeeks"

print (a * n); 

Exit:

 GeeksforGeeksPython.Engineering 

10. Checking if two words are anagrams

from collections import Counter

def is_anagram (str1, str2):

return Counter (str1) = = Counter (str2)

print (is_anagram ( ’geek’ , ’eegk’ ))

 

print (is_anagram ( ’geek’ , ’peek’ )) 

Exit :

 True False 

Links:
1. 10 neat Python tricks beginners should know
2. 30 Essential Python Tips and Tricks for Programmers

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically