Skip to content

Instantly share code, notes, and snippets.

@MalikBagwala
Last active July 24, 2020 05:07
Show Gist options
  • Save MalikBagwala/5d5586239da423b51146058c8e3fead0 to your computer and use it in GitHub Desktop.
Save MalikBagwala/5d5586239da423b51146058c8e3fead0 to your computer and use it in GitHub Desktop.

What is the difference between a module and a package in python?

A module usually corresponds to one .py file containing Python code, A Python module which can contain submodules or recursively, subpackages is called a package. Technically, a package is a Python module with an __path__ attribute

What is the default implementation of python?

The default implementation of python is CPython (interpreter is writting in C language)

Write a simple program to find reverse of a string?

myString =  "abcdedg"

#begins at the end -> ends at 0th index -> decrements index every step
print(myString[::-1]) #begin:end:step

What is the difference between a list and a tuple in python?

The main difference between lists and tuples is that lists are mutable whereas tuples are immutable

What are pure functions?

Pure functions are functions that meet the below conditions

  1. They should not produce any side effects
  2. For the same input they should produce the same output
  3. They should not modify data outside their scope

What is a model in Django?

A model in django is a blueprint to a database table where its fields are mapped to the corresponding columns in a table.

What are higher order function?

A higher order function is function which does atleast one of the below two things. It is generally used to empower a function

  1. It accepts a function as its parameter
  2. it return a function as its return value

What are decoraters in python?

Decoraters are syntactic sugar for higher order functions in python, they enhance the underlying function with a resuable feature

def pretty_print(func):
	def wrapper(*args, **kwargs):
		print("#### PRETTY ####")
		func(*args,  **kwargs)
		print("---------------")
	return wrapper

@pretty_print
def say_hello():
	print("Hello!")
	 
say_hello()

What are dunder / magic methods in python?

Dunder is a short form for Double Under (Underscore) , these methods are not to be invoked directly but they are automatically invoked whenever a certain action is performed on a class. One of the most used methods is __str__

class Animal:
	def __str__(self) -> str:
		return  "string representation"
	pass

animal1 =  Animal()
print(animal1)

__str__ is invoked whenever we try to print the class object and also when we explicitely demand a string representation (str(animal1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment