Python is the most popular programming language among all, good python developer has huge demand in job market.
Here are some commonly python interview questions and answer for beginners and experienced developers.import csv import numpy as npabove are the examples of adding namespace in python programming.
Python memory management controlled by Python private heap space. All Python data structures and objects are located in a private heap.
Python heap space allocation for Python objects is done by Python memory manager. Then core API gives access to some tools for the programmer to code.
Python has built-in garbage collector, which recycle all unused memory and frees the memory and makes it available to the heap space.
Read more about python memory management in details.
for i in range(0, 20): // code block for i in xrange(0, 20): // code block
Xrange are no more supported in python version 3 or later, to check which version of python you are using, just run the command python --version
.
Python range function has multiple overloads, default range(start, stop, step)
.
for i in range(0, 5, 2): print(i) for i in range(0, 5): print(i) for i in range(2): print(i)
Package is basically the way we organise project, the folder of Python program is considered as package. A package can have modules or subfolders.
Module is the programme file (.py)
, each Python program file (.py)
is a module, and a module can import other modules like objects and attributes.
import random random.random()
_init_()
function in Python?
To access date time in python library, we need to add following library reference in our code.
from datetime import date from datetime import datetime today=date.today() currenttime= time.localtime(time.time()) print (“Current time is”, currenttime)
Learn more about python date time.
There are various ways we can define array in python programming.
colors=["red","green","blue","black","white"];
Learn more about python array with examples.
In python, there is one library has been developed for advanced array handling -called numpy, using numpy we can create array in various ways, single dimension array, two dimension array, multi dimension array etc. Learn more about numpy array.
def functionName
, look at the example below.
def welcomeStudent(name): print("Welcome ", name)
Learn more about python basic syntax
We can implement inheritance by writing like ChildClass1(ParentClass)
, please take a look at the example below where “Student” class is inherited from “People” class.
from School.People import People #this is an example of inheritence class Student(People): def __init__(self): self.StuId = id, self.Title = "Student"; print("student class"); def welcomeMessage(obj): print ("Welcome " + obj.first_name)
ChildClass1(ParentClass1, ParentClass2)
.
If we use the reference of above example, we can add another base class like from School.People import People from School.Course import Course #this is an example of multiple inheritence class Student(People, Course): def __init__(self): self.StuId = id, self.Title = "Student"; print("student class"); def welcomeMessage(obj): print ("Welcome " + obj.first_name)
Learn more about OOPs in Python
We need to simply import the reference of configParser , then read like value = config.get('section-name', 'key')
from configparser import ConfigParser class StudentDTO(): def __init__(self): config = ConfigParser() config.read('mypy.ini') database = config.get('dbinfo', 'database') dbusername = config.get('dbinfo', 'dbusername') dbpassword = config.get('dbinfo', 'dbpassword') dbhost = config.get('dbinfo', 'dbhost')
Learn more about read information from config file