10 Amazing Advanced Python Constructs that you can use to write efficient and clean Code
A Thread 🧵👇🏻
#Python #TensorFlow #DataScientist #Programming #Coding #100DaysofCode #DataScience #AI #MachineLearning
1/ DefaultDict
In python, a dictionary is a container that holds key-value pairs. Keys must be unique, immutable objects. If you try to access or modify keys that don’t exist in the dictionary, it raise a KeyError & break up your code execution ( continued..)
2/ (Continued..)To tackle this issue,Python defaultdict type, a dictionary-like class is used.If you try to access or modify a missing key,then defaultdict will automatically create the key & generate a default value for it
A defaultdict will never raise a KeyError ( Continued..)
3/ ( Continued ..) Any key that does not exist gets the value returned by the default factory
Hence, whenever you need a dictionary, and each element’s value should start with a default value, use a defaultdict.
4/ Decorators
A decorator is any callable Python object that is used to modify a function or a class. It takes a function,adds some functionality,& returns it.Decorators are very powerful tool in Python since it allows programmers to control the behavior of function or class.
5/ Memoization using Decorators
It's a technique which allows you to optimize a Python function by caching its output based on the parameters you supply to it. Once you memoize a function,it will only compute its output once for each set of parameters you call it with.
6/ If you want to speed up the parts in your program that are expensive, memoization can be a great technique to use.
There are three approaches to Memoization —
*Using global
*Using objects
*Using default parameter
*Using a Callable Class
7/ Lambda Functions
In python, Lambda is used to create small anonymous functions using “lambda” keyword and can be used wherever function objects are needed. It can any number of arguments but only one expression
8/ lambda argument(s): expression
*It can be used inside another function
*Anonymous functions are defined using the lambda keyword
*Whenever we require a nameless function for a short period of time, we use lambda functions
9/ Magic Methods
The advantage of using Python’s magic methods is that they provide a simple way to make objects behave like built-in types. Magic methods can be used to emulate the behavior of built-in types of user-defined objects.
10/ ChainMap
ChainMap allows you to treat multiple dictionaries into one to create a single, update-able view. It helps you to search values through multiple dictionaries and is used in performance critical applications. It incorporates dict mappings by reference.
11/ The timeit Module
In python, timeit module provides a simple way to time small snippet of Python program so that you get the measurement of code execution time & runs the code approx. 1 million times which is the default value.
12/ Map
Map allows you to process and transform the items of the iterables or collections without using a for loop. In Python, map() function applies the given function to each item of a given iterable construct (i.e lists, tuples etc) and returns a map object.
13/ Handle billions of rows of data within seconds…How?

14/ Want to learn complete hands on #Python with Code Implementation? Try this ( 84% off) :
udemy.com/course/complet…
I write quality threads on Data Science, Python, Programming, Machine Learning and AI in my free time. If you like this thread, then give a follow.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Naina Chaturvedi

Naina Chaturvedi Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(