Elemental Questions of Python

Is Python interpreted or compiled ? Are there ways to compile the code ?

Which is faster in python — Searching in a list or a dictionary. And Why ?

Dictionaries are Python’s built in a mapping type and so have also been highly optimized, list requires walking through the list until it finds the result from beginning to the result each time .However, if the data sets are really small (<1000 elements) list perform well.

How can we get the code of a python object ?

What are the ways to make python code work faster ?

Use list comprehensions : They are concise and speedy way to create new lists.

Use “in” if possible : To check if membership of a list, it’s generally faster to use the “in” keyword.

Use Sets and unions : Too much looping makes your code unhealthy, if you wanted to get the overlapping values in two lists. You could do this using nested for loops.

Use multiple assignments : Python has good way to assign the values of multiple variables.

Use join() to concatenate strings : In python, you can concatenate strings using “+” . However, strings in Python are immutable, and the “+” operation involves creating a new string and copying the old content at each step . A more efficient approach would be to use the array module to modify and then use the join() function to re-create your final string.

Use builtin functions and libraries : Python comes with a lot of batteries included. You can write quality and efficient code but it’s hard to beat the underlying libraries. These have been optimized and are tested.

What is exec function and how we can use it ?


exec(object[, globals[, locals]])

It can take three parameters:

  • object : As already said this can be a string or object code
  • globals : This can be a dictionary and the parameter is optional
  • locals: This can be a mapping object and is also optional


prog = 'print("The sum of 5 and 10 is", (5+10))'


The sum of 5 and 10 is 15

What are metaclasses and dataclasses in python ?

Detaclasses module is introduced in Python 3.7 . A dataclass is a class typically containing mainly data, although there aren’t really any restrictions. It is created using the new @dataclass decorator. Dataclasses automatically create __init__, __repr__, __eq__, __gt__, __lt__ methods without you having to add them explicitly. This is handy when we need to create custom containers for your data.

What is __call__ method in python ?

Thanks for reading. I hope that this was helpful!👍





The price of “anything” is the amount of “time”, U xchange for it. Education | Technology | Data Science | Statistics | History

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store