PYTHON

QUESTIONS BASED ON PYTHON LANGUAGE

What is a dynamically typed language?

Before we understand a dynamically typed language, we should learn about what typing is. Typing refers to type-checking in programming languages. In a strongly-typed language, such as Python, "1" + 2  will result in a type error since these languages don't allow for "type-coercion".
Type-checking can be done at two stages:
• Static - Data Types are checked before execution.
• Dynamic - Data Types are checked during execution. Python is an interpreted language, executes each statement line by line and thus type-checking is done on the fly, during execution. Hence, Python is a Dynamically Typed Language.

What is an Interpreted language?

An Interpreted language executes its statements line by line. Languages such as Python, Javascript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step.

Define Pass statement in Python?

A Pass statement in Python is used when we cannot decide what to do in our code, but we must type something for making syntactically correct.

Do runtime errors exist in Python? Give an example?

Yes, runtime errors exist in Python. For example, if you are duck typing and things look like a duck, then it is considered as a duck even if that is just a flag or stamp or any other thing. The code, in this case, would be A Run-time error. For example, Print “Hackr io”, then the runtime error would be the missing parenthesis that is required by print ( ).

Does Python support an intrinsic do-while loop or not?

No Python does not support an intrinsic do-while loop.

Why do we need membership operators in Python?

We need membership operators in Python with the purpose to confirm if the value is a member in another or not.

What do you understand by the process of compilation and linking in Python?

In order to compile new extensions without any error, compiling and linking is used in Python. Linking initiates only and only when the compilation is complete. In the case of dynamic loading, the process of compilation and linking depends on the style that is provided with the concerned system. In order to provide dynamic loading of the configuration setup files and rebuilding the interpreter, the Python interpreter is used.

What is Pickling and Unpickling in Python?

The Pickle module in Python allows accepting any object and then converting it into a string representation. It then dumps the same into a file by means of the dump function. This process is known as pickling. The reverse process of pickling is known as unpickling i.e. retrieving original Python objects from a stored string representation.

Whenever Python exits, all the memory isn’t deallocated. Why is it so?

Upon exiting, Python’s built-in effective cleanup mechanism comes into play and try to deallocate or destroy every other object. However, Python modules that are having circular references to other objects or the objects that are referenced from the global namespaces aren’t always deallocated or destroyed. This is because it is not possible to deallocate those portions of the memory that are reserved by the C library.

How are arguments passed in Python? By value or by reference?

All of the Python is an object and all variables hold references to the object. The reference values are according to the functions; as a result, the value of the reference cannot be changed.

Define slicing in Python.

 Slicing refers to the mechanism to select the range of items from sequence types like lists, tuples, strings.

What are docstring?

Docstring is a Python documentation string, it is a way of documenting Python functions, classes and modules.

What are decorators in Python?

Decorators are used to add some design patterns to a function without changing its structure. Decorators generally are defined before the function they are enhancing. To apply a decorator we first define the decorator function. Then we write the function it is applied to and simply add the decorator function above the function it has to be applied to. For this, we use the @ symbol before the decorator.

What is slicing in Python?

Slicing is used to access parts of sequences like lists, tuples, and strings. The syntax of slicing is-[start:end:step]. The step can be omitted as well. When we write [start:end] this returns all the elements of the sequence from the start (inclusive) till the end-1 element. If the start or end element is negative i, it means the ith element from the end. The step indicates the jump or how many elements have to be skipped. Eg. if there is a list- [1,2,3,4,5,6,7,8]. Then [-1:2:2] will return elements starting from the last element till the third element by printing every second element.i.e. [8,6,4].

What are Literals in Python and explain about different Literals?

A literal in python source code represents a fixed value for primitive data types.
There are 4 types of literals in python-
• String literals– A string literal is created by assigning some text enclosed in single or double quotes to a variable. To create multiline literals, assign the multiline text enclosed in triple quotes. Eg.name="Tanya"
• A character literals– It is created by assigning a single character enclosed in double quotes. Eg. a='T'
• Numeric literals– They include numeric values that can be either integer, floating point value, or a complex number. Eg. a=50
• Boolean literals– These can be 2 values- either True or False.

How to combine dataframes in pandas?

The dataframes in python can be combined in the following ways-
• Concatenating them by stacking the 2 dataframes vertically.
• Concatenating them by stacking the 2 dataframes horizontally.
• Combining them on a common column. This is referred to as joining. The concat() function is used to concatenate two dataframes. Its syntax is- pd.concat([dataframe1, dataframe2]).

What are the new features added in Python 3.9.0.0 version?

The new features in Python 3.9.0.0 version are-
• New Dictionary functions Merge(|) and Update(|=)
• New String Methods to Remove Prefixes and Suffixes
• Type Hinting Generics in Standard Collections
• New Parser based on PEG rather than LL1
• New modules like zoneinfo and graphlib
• Improved Modules like ast, asyncio, etc.
• Removal of erroneous methods, functions, etc.

How is memory managed in Python?

Memory is managed in Python in the following ways:
• Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.
• The allocation of heap space for Python objects is done by Python’s memory manager. The core API gives access to some tools for the programmer to code.
• Python also has an inbuilt garbage collector, which recycles all the unused memory and so that it can be made available to the heap space.

What is namespace in Python?

A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.

What is python function?

A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.

What is __init__?

__init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the __init__ method.

Does python support multiple inheritance?

Multiple inheritance means that a class can be derived from more than one parent classes. Python does support multiple inheritance, unlike Java.

What is Polymorphism in Python?

Polymorphism means the ability to take multiple forms. So, for instance, if the parent class has a method named ABC then the child class also can have a method with the same name ABC having its own parameters and variables. Python allows polymorphism.

Define encapsulation in Python?

Encapsulation means binding the code and the data together. A Python class in an example of encapsulation.

How do you do data abstraction in Python?

Data Abstraction is providing only the required details and hiding the implementation from the world. It can be achieved in Python by using interfaces and abstract classes.

Does python make use of access specifiers?

Python does not deprive access to an instance variable or function. Python lays down the concept of prefixing the name of the variable, function or method with a single or double underscore to imitate the behavior of protected and private access specifiers.

How to create an empty class in Python?

An empty class is a class that does not have any code defined within its block. It can be created using the pass keyword. However, you can create objects of this class outside the class itself. IN PYTHON THE PASS command does nothing when its executed. it’s a null statement.

What is PYTHONPATH?

It is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.

Is indentation required in python?

Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.

Is Django better than Flask?

Django is more popular because it has plenty of functionality out of the box, making complicated applications easier to build. Django is best suited for larger projects with a lot of features. The features may be overkill for lesser applications. If you’re new to web programming, Flask is a fantastic place to start. Many websites are built with Flask and receive a lot of traffic, although not as much as Django-based websites. If you want precise control, you should use flask, whereas a Django developer relies on a large community to produce unique websites.

How to merge dataframes in pandas?

Merging depends on the type and fields of different dataframes being merged. If data is having similar fields data is merged along axis 0 else they are merged along axis 1.

How to access the last five entries of a dataframe?

By using tail(5) function we can get the top five entries of a dataframe. By default df.tail() returns the top 5 rows. To get the last n rows df.tail(n) will be used.

What are comments and how can you add comments in Python?

Comments in Python refer to a piece of text intended for information. It is especially relevant when more than one person works on a set of codes. It can be used to analyse code, leave feedback, and debug it. There are two types of comments which includes:
• Single-line comment
• Multiple-line comment

What is a classifier?

A classifier is used to predict the class of any data point. Classifiers are special hypotheses that are used to assign class labels to any particular data points. A classifier often uses training data to understand the relation between input variables and the class. Classification is a method used in supervised learning in Machine Learning.

How do you get a list of all the keys in a dictionary?

One of the ways we can get a list of keys is by using: dict.keys() This method returns all the available keys in the dictionary.
dict = {1:a, 2:b, 3:c} dict.keys() o/p: [1, 2, 3]

Explain Python List Comprehension

List comprehensions are used for transforming one list into another list. Elements can be conditionally included in the new list and each element can be transformed as needed. It consists of an expression leading a for clause, enclosed in brackets.
for ex: list = [i for i in range(1000)] print list

What is the bytes() function?

The bytes() function returns a bytes object. It is used to convert objects into bytes objects, or create empty bytes object of the specified size.

What is the ‘with statement’?

“with” statement in python is used in exception handling. A file can be opened and closed while executing a block of code, containing the “with” statement, without using the close() function. It essentially makes the code much more easy to read.

What is the difference between tuple and dictionary?

One major difference between a tuple and a dictionary is that dictionary is mutable while a tuple is not. Meaning the content of a dictionary can be changed without changing it’s identity, but in tuple that’s not possible.

Differentiate between new and override modifiers.

The new modifier is used to instruct the compiler to use the new implementation and not the base class function.
The Override modifier is useful for overriding a base class function inside the child class.

Top 5 Things to Remember in an Interview

You worked hard on your resume and job application, and now you are called in for an in-person job interview. You are one step closer to your dream job. It’s time to understand how to succeed in the job interview, so that you can actually land the job. Here are the most important things you have to remember.

1. Dress appropriately

Plan out an outfit that fits the culture of the company you are applying for. If the company does not have a dress code, it’s a good idea to wear business casual. Leave your shorts and tank top at home, and put on a shirt and a pair of long pants. It’s always better to be overdressed than under. Try on your outfit before the interview to make sure that it fits and looks smart.

2. Arrive on time

Don’t ever arrive at a job interview late! It’s best to arrive 15 minutes before the scheduled time in case you have to fill in some paperwork. This also allows you to settle down and check out the dynamics of the office. If you are not familiar with the area in which the company is located, do a test run a week or two before to make sure that you won’t get lost. If you are driving, make a note on where you can park your car.

3. Mind your manner

Be polite and greet everyone you meet, including people you meet in the elevator. When you enter the interview, offer the interviewer a warm greeting. These first few seconds can make or break your interview. At the end of the interview, don’t forget to thank the interviewer for giving you the opportunity for the meeting. When you leave the company, say goodbye to the receptionist.

4. Pay attention to your body language

Poor body language, such as playing with a pen, chewing gum, slouching, and even brushing back hair, can be a distraction. If you notice you have a tendency to do any of these, train yourself to avoid these bad habits. You can replace them with positive body language that include nodding, eye contact, smiling, and solid posture.

5. Ask insightful questions

Most interviewers end an interview by allowing the candidate to ask questions. Regardless of how well you know the company and how thorough the interviewer in telling you about the job, you must ask a few questions. The more insightful your questions are, the more you will impress your interviewer. What do you do in an interview? Let us know in the comments below!