Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Python

Uploaded by

Shweta Saini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python

Uploaded by

Shweta Saini
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is the difference between a Mutable datatype and an Immutable data type?

Mutable data types can be edited i.e., they can change at runtime. Eg – List,
Dictionary, etc.Immutable data types can not be edited i.e., they can not change at
runtime. Eg – String, Tuple, etc.

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


Everything in Python is an object and all variables hold references to the objects.
The reference values are according to the functions; as a result, you cannot change
the value of the references. However, you can change the objects if it is mutable.
-- What is the difference between a Set and Dictionary?
The set is an unordered collection of data types that is iterable, mutable and has
no duplicate elements.A dictionary in Python is an ordered collection of data
values, used to store data values like a map.
-- What is List Comprehension? Give an Example.
List comprehension is a syntax construction to ease the creation of a list based on
existing iterable.
For Example:- my_list = [i for i in range(1, 10)]
-- What is a lambda function?
A lambda function is an anonymous function. This function can have any number of
parameters but, can have just one statement. For Example:
a = lambda x, y : x*y
print(a(7, 19))
-- What is a pass in Python?
Pass means performing no operation or in other words, it is a placeholder in the
compound statement, where there should be a blank left and nothing has to be
written there.
-- What is the difference between / and // in Python?
/ represents precise division (result is a floating point number) whereas //
represents floor division (result is an integer).
-- How is Exceptional handling done in Python?
There are 3 main keywords i.e. try, except, and finally which are used to catch
exceptions and handle the recovering mechanism accordingly. Try is the block of a
code that is monitored for errors. Except block gets executed when an error occurs.
The beauty of the final block is to execute the code after trying for an error.
This block gets executed irrespective of whether an error occurred or not. Finally,
block is used to do the required cleanup activities of objects/variables.
1-- What is swapcase function in Python?
It is a string’s function that converts all uppercase characters into lowercase and
vice versa.

-- Difference between for loop and while loop in Python


The “for” Loop is generally used to iterate through the elements of various
collection types such as List, Tuple, Set, and Dictionary. Developers use a “for”
loop where they have both the conditions start and the end. Whereas, the “while”
loop is the actual looping feature that is used in any other programming language.
Programmers use a Python while loop where they just have the end conditions.
-- Can we Pass a function as an argument in Python?
Yes, Several arguments can be passed to a function, including objects, variables
(of the same or distinct data types), and functions. Functions can be passed as
parameters to other functions because they are objects. Higher-order functions are
functions that can take other functions as arguments.

-- What are *args and *kwargs?


To pass a variable number of arguments to a function in Python, use the special
syntax *args and **kwargs in the function specification. It is used to pass a
variable-length, keyword-free argument list. By using the *, the variable we
associate with the * becomes iterable, allowing you to do operations on it such as
iterating over it and using higher-order operations like map and filter.
-- What is Scope in Python?
The location where we can find a variable and also access it if required is called
the scope of a variable.
• Local variable: Local variables are those that are initialized within a function
and are unique to that function. It cannot be accessed outside of the function.
• Global variables: Global variables are the ones that are defined and declared
outside any function and are not specified to any function.
• Module-level scope: It refers to the global objects of the current module
accessible in the program.
• Outermost scope: It refers to any built-in names that the program can call. The
name referenced is located last among the objects in this scope.
-- What is a dynamically typed language?
Typed languages are the languages in which we define the type of data type and it
will be known by the machine at the compile-time or at runtime. Typed languages can
be classified into two categories:
• Statically typed languages: In this type of language, the data type of a variable
is known at the compile time which means the programmer has to specify the data
type of a variable at the time of its declaration.
• Dynamically typed languages: These are the languages that do not require any pre-
defined data type for any variable as it is interpreted at runtime by the machine
itself. In these languages, interpreters assign the data type to a variable at
runtime depending on its value.
-- What is a break, continue, and pass in Python?
The break statement is used to terminate the loop or statement in which it is
present. After that, the control will pass to the statements that are present after
the break statement, if available.
Continue is also a loop control statement just like the break statement. continue
statement is opposite to that of the break statement, instead of terminating the
loop, it forces to execute the next iteration of the loop.
Pass means performing no operation or in other words, it is a placeholder in the
compound statement, where there should be a blank left and nothing has to be
written there.

-- How do you floor a number in Python?


The Python math module includes a method that can be used to calculate the floor of
a number.
• floor() method in Python returns the floor of x i.e., the largest integer not
greater than x.
• Also, The method ceil(x) in Python returns a ceiling value of x i.e., the
smallest integer greater than or equal to x.

-- Differentiate between List and Tuple?


Let’s analyze the differences between List and Tuple:
List
• Lists are Mutable datatype.
• Lists consume more memory
• The list is better for performing operations, such as insertion and deletion.
• The implication of iterations is Time-consuming
Tuple
• Tuples are Immutable datatype.
• Tuple consumes less memory as compared to the list
• A Tuple data type is appropriate for accessing the elements
• The implication of iterations is comparatively Faster

30. What are Decorators?


Decorators are a very powerful and useful tool in Python as they are the specific
change that we make in Python syntax to alter functions easily.
32. What are Iterators in Python?
In Python, iterators are used to iterate a group of elements, containers like a
list. Iterators are collections of items, and they can be a list, tuples, or a
dictionary. Python iterator implements __itr__ and the next() method to iterate the
stored elements. We generally use loops to iterate over the collections (list,
tuple) in Python.
33. What are Generators in Python?
In Python, the generator is a way that specifies how to implement iterators. It is
a normal function except that it yields expression in the function. It does not
implement __itr__ and next() method and reduces other overheads as well.

If a function contains at least a yield statement, it becomes a generator. The


yield keyword pauses the current execution by saving its states and then resumes
from the same when required.
34. Does Python supports multiple Inheritance?
Python does support multiple inheritances, unlike Java. Multiple inheritances mean
that a class can be derived from more than one parent class.
35. 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.
36. Define encapsulation in Python?
Encapsulation means binding the code and the data together. A Python class is an
example of encapsulation.
37. How do you do data abstraction in Python?
Data Abstraction is providing only the required details and hides the
implementation from the world. It can be achieved in Python by using interfaces and
abstract classes.

40. What is slicing in Python?


Python Slicing is a string operation for extracting a part of the string, or some
part of a list. With this operator, one can specify where to start the slicing,
where to end, and specify the step. List slicing returns a new list from the
existing list.
Syntax: Lst[ Initial : End : IndexJump ]

42. What is PIP?


PIP is an acronym for Python Installer Package which provides a seamless interface
to install various Python modules. It is a command-line tool that can search for
packages over the internet and install them without any user interaction.

43. What is a zip function?


Python zip() function returns a zip object, which maps a similar index of multiple
containers. It takes an iterable, converts it into an iterator and aggregates the
elements based on iterables passed. It returns an iterator of tuples.
44. What are Pickling and Unpickling?
The Pickle module accepts any Python object and converts it into a string
representation and dumps it into a file by using the dump function, this process is
called pickling. While the process of retrieving original Python objects from the
stored string representation is called unpickling.
46. What is __init__() in Python?
Equivalent to constructors in OOP terminology, __init__ is a reserved method in
Python classes. The __init__ method is called automatically whenever a new object
is initiated. This method allocates memory to the new object as soon as it is
created. This method can also be used to initialize variables.

You might also like