1) What is Python?
Ans: Python is a high-level, interpreted, general-purpose programming language. Being a general-purpose language, it can be used to build almost any type of application with the right tools/libraries.
2) What are the
benefits of using Python?
- Python is a general-purpose
programming language that has a simple, easy-to-learn syntax that
emphasizes readability and therefore reduces the cost of program
maintenance. Moreover, the language is capable of scripting, is completely
open-source, and supports third-party packages encouraging modularity and
code reuse.
- Its high-level data structures,
combined with dynamic typing and dynamic binding, attract a huge community
of developers for Rapid Application Development and deployment.
3) What is an Interpreted
language?
Ans: 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.
4) What is PEP 8
and why is it important?
Ans: PEP stands for Python Enhancement Proposal. A PEP
is an official design document providing information to the Python community,
or describing a new feature for Python or its processes. PEP 8 is especially
important since it documents the style guidelines for Python Code. Apparently
contributing to the Python open-source community requires you to follow these
style guidelines sincerely and strictly.
5) What is Scope
in Python?
Ans: Every object in Python functions within a scope. A
scope is a block of code where an object in Python remains relevant. Namespaces
uniquely identify all the objects inside a program. However, these namespaces
also have a scope defined for them where you could use their objects without
any prefix. A few examples of scope created during code execution in Python are
as follows:
- A local scope refers to the local
objects available in the current function.
- A global scope refers to the
objects available throughout the code execution since their inception.
- A module-level scope refers to
the global objects of the current module accessible in the program.
- An outermost scope refers to all
the built-in names callable in the program. The objects in this scope are
searched last to find the name referenced.
Note: Local scope objects can be synced with global
scope objects using keywords such as global.
6) What are
global, protected and private attributes in Python?
·
Global variables
are public variables that are defined in the global scope. To use the variable
in the global scope inside a function, we use the global keyword.
·
Protected
attributes are attributes defined with an underscore prefixed to their
identifier eg. _sara. They can still be accessed and modified from outside the
class they are defined in but a responsible developer should refrain from doing
so.
·
Private attributes
are attributes with double underscore prefixed to their identifier eg. __ansh.
They cannot be accessed or modified from the outside directly and will result
in an AttributeError if such an attempt is made.
7) What is self in
Python?
Ans: Self is
a keyword in Python used to define an instance of an object of a class. In
Python, it is explicitly used as the first parameter, unlike in Java where it
is optional. It helps in distinguishing between the methods and attributes of a
class from its local variables.
8) What is
__init__?
Ans: __init__ is a contructor method in Python and is
automatically called to allocate memory when a new object/instance is created.
All classes have a __init__ method associated with them. It helps in
distinguishing methods and
attributes of a
class from local variables.
# class definition
class Student:
def __init__(self, fname, lname, age, section):
self.firstname = fname
self.lastname = lname
self.age = age
self.section = section
# creating a new
object
stu1 = Student("Sara", "Ansh", 22, "A2")
9) What are
decorators in Python?
Ans: A Python decorator
is a specific change made in the Python syntax for the easy alteration of
functions.
10) Difference
between generators and iterators?
Ans: In Python,
iterators are used to iterate over a group of elements (in a list, for
example). The way of implementing these iterators is known as generators. It
yields an expression in the function, but otherwise behaves like a normal
function.
11) How to convert
a number into a string?
Ans: One of the most
common python interview questions. We can use the inbuilt str() function. For
an octal or hexadecimal representation, we can use the other inbuilt functions
like oct() or hex().
12) What is the
use of the // operator in Python?
Ans: Using the //
operator between 2 numbers gives the quotient when the numerator is divided
from the denominator. It is called the Floor Division operator. It is one of
the general questions from the Python interview questions and answers guide.
13) Does Python
have a Switch or Case statement like in C?
Ans: No, it does not.
However, we can make our own Switch function and use it.
14) What is the
range() function and what are its parameters?
Ans: The range()
function is used to generate a list of numbers. Only integer numbers are
allowed, and hence, parameters can be both negative and positive. The following
parameters are acceptable:
range(stop)
Where ‘stop’ is
the no. of integers to generate, starting from 0. Example: range(5) ==
[0,1,2,3,4]
range([start],
stop[, step])
Start: gives the
starting no. of the sequence
Stop: specifies
the upper limit for the sequence
Step: is the
incrementing factor in the sequence
15) What is the
use of %s?
Ans: %s is a format
specifier which transmutes any value into a string.
16) Is it
mandatory for a Python function to return a value?
Ans: No
17) Does Python
have a main() function?
Ans: Yes, it does. It
is executed automatically whenever we run a Python script. To override this
natural flow of things, we can also use the if statement.
18) What is GIL?
Ans: GIL or the Global
Interpreter Lock is a mutex, used to limit access to Python objects. It
synchronizes threads and prevents them from running at the same time.
19) Before the use
of the ‘in’ operator, which method was used to check the presence of a key in a
dictionary?
Ans: The has_key()
method
20) How do you
change the data type of a list?
Ans: To change a list
into a tuple, we use the tuple() function
To change it into
a set, we use the set() function
To change it into
a dictionary, we use the dict() function
To change it into a string, we use the .join() method
21) How can you
capitalize the first letter of a string in Python?
Ans: In Python, you can
use the capitalize() method to capitalize the first letter of a string.
However, if a string already consists of a capital letter at the beginning, it
will return the original string.
22) What is an
Expression?
Ans: An expression Can be defined as a combination of
variables, values operators a call to functions. It is a sequence of operands
or operators like a + B – 5 is called an expression. Python supports many such
operators for combining data object into an express.
23)What is a
statement in Python?
Ans: It is an instruction that Python can interpret and
execute when you type the statement in the command line Python execute and
displays the result if there is one.
24) What is ==
in Python?
Ans: It is an operator which is used to check or compare
the values of two objects
25) How do you do
data abstraction in Python?
Ans: An abstraction means hiding away information or
showing only information that’s necessary.
Example
print(len((1,2,3,1)))
#in this example we dont want to learn how len was introduced in python
26) What is a
dictionary in pthon?
Ans: Dictionary is a data structure as well as a data
type in python.It is enclosed in curly brackets{}.
Dictionary contains 2 elements – key and value
key is a string for us to grab a value.
Example
dictionary = {
‘a’: 1,
‘b’: 2
}
print(dictionary[‘b’])
0 Comments