DEV Community

Cover image for Python Tips & Tricks For Beginners
Raghav Mrituanjaya
Raghav Mrituanjaya

Posted on • Edited on • Originally published at thegogamicblog.xyz

6 1

Python Tips & Tricks For Beginners

In this short yet effective post, we will discuss some of the top tips and tricks in python that might help you write your code faster 🚀

Reversing a string

s = "This is a String"
print(s[::-1])
Enter fullscreen mode Exit fullscreen mode

Removing duplicates from a list

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = list(set(a))
print(b)
Enter fullscreen mode Exit fullscreen mode

Merging Two or More Dictionaries

a = {'a': 1, 'b': 2}
b = {'c': 3, 'd': 4}
c = {**a, **b}
Enter fullscreen mode Exit fullscreen mode
  • This trick might only work for Python version >= 3.5

Converting a list of strings into a string

a = ['a', 'b', 'c']
b = ''.join(a)
print(b)
Enter fullscreen mode Exit fullscreen mode

Retrieving the memory size

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(sys.getsizeof(a), "Bytes")
print(sys.getsizeof(a) / 1024, "Megabytes")
Enter fullscreen mode Exit fullscreen mode

Formatting Numbers

num1 = 100_000_000_000
num2 = 100_000_000_000
num3 = num1+num2
print(f"{num3:,}")
Enter fullscreen mode Exit fullscreen mode
  • The output will be 200,000,000,000

Final Thoughts

  • This post may get updated from time to time
  • If you would like to mention any tricks kindly mention them in the comment section below

P.S:- Vultr(Get a $100 credit by registering using this link) is a good hosting choice if you're looking for one

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
sokhavuth profile image
Sokhavuth TIN

To highlight Python code, you just write the word Python or py after the opening triple quote:

num1 = 100_000_000_000
num2 = 100_000_000_000
num3 = num1+num2
print(f"{num3:,}")
Enter fullscreen mode Exit fullscreen mode
Collapse
 
raghavmri profile image
Raghav Mrituanjaya

I have edited the post :) Thanks for letting me know!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️