Posts

Create a Large file for Test Data Processing using Python

  Code Example # Create a Large file for Test Data Processing This below code will create 1.1GB .txt file import string from random import choice with open("large_data.txt", "w") as fp:     i = 1     while i <= 1000000:         word = (''.join(                 choice(string.ascii_uppercase + string.digits) for _ in range(10)) + "|"                ) * 100         fp.write(word[:] + "\n")         i += 1

Write a Callback Function in Python

  Code Example #callback function def callback ( a, b ): print ( 'Sum = {0}' . format (a+b)) def main ( a,b,f= None ): print ( 'Add any two digits.' ) if f is not None : f(a,b) main( 1 , 2 , callback)  

Multiprocessing

  Code Example # Multiprocessing # Way 1 from multiprocessing import Pool import time def f(x, y): for i in range(20): print(i, end="#") time.sleep(2) pool = Pool(processes=10) for i in range(10): pool.apply_async(f, [10, i])

Write a Logic to Validate only this partten (()), ((), )(

  Code Example #Write a Logic to Validate only this partten  (()), ((), )(   user_input = input("enter here :") stack = [i for i in user_input] for j,i in enumerate(user_input):     if i == ')':         if stack.index(')') > stack.index('('):             stack.remove('(')             stack.remove(')') if ')' in stack or '(' in stack:     print("no") else:     print("yes")    

Print Execution Time Using Custom Decorator

  Code Example # Print Execution Time Using Custom Decorator  from datetime import datetime def print_time(fun):     def wrapper_method(*args, **kwargs):         start_time = datetime.now()         res = fun(*args, **kwargs)         end_time = datetime.now()         print("execution time : ", end_time-start_time)         print(res)     return wrapper_method @print_time def my_function1(goto=10):    # with params     for i in range(goto):         i = i**1000     return "200" @print_time def my_function2():           # without params     for i in range(10):         i = i**1000 my_function1(1000) my_function2()

How to solve the Liner Equation using Python

  Code Example # How to solve the Liner Equation using Python Example 1: [with Multiple variable] import numpy as np '''     8x+3y−2z=9     −4x+7y+5z=15     3x+4y−12z=35 ''' A = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]]) b = np.array([9, 15, 35]) answer = np.linalg.solve(A, b) print(answer)  

Frequently Asked Python Interview Question

  Practice Set 1 01)  How Python code Execute? 02)  Why Python is interpreted language? 03)  Why Python is known as a scripting language? 04)  What is PVM? 05)  How Python manages its memory? 06)  What is namespace in Python? 07)  What are built-in data types in Python? 08)  What is literal? 09)  What is docstring? 10)  How to create a custom datatype in Python? 11)  What is the difference between List, Tuple, and Set? 12)  What is faster between List and Tuple? Explain Why? 13)  What is the difference between Python List and Array? In both cases how memory allocates? 14)  If I want to update a Tuple how can I do it? 15)  How many types of dictionaries present in Python? 16)   What is the difference between Dict and OrderDict? 17)   What is the advantage of using Dict? 18)  You have a list how can you make sure all the elements in the list should be unique? 19)  What is  monkey patching in Python? 20)  What is a  Decorator in Python? Why we use it? 21)  What is filter method in Python?