Interview logo

python interview questions and answers (part-2)

Important python interview questions and answers

By Sedhu SPublished 3 years ago โ€ข 4 min read

What is a module in Python?

A module in Python is a file containing Python definitions and statements. The file name is the module name with the suffix .py added. For example, if we have a file named mymodule.py, it can be imported using the import statement with the name mymodule. The module can contain functions, classes, and variables.

What is the difference between range() and xrange() functions in Python?

Both range() and xrange() functions are used to generate a sequence of numbers, but the difference is in their implementation. The range() function returns a list of numbers, whereas xrange() returns an xrange object, which is an iterator. xrange() is more memory efficient, especially when working with large ranges of numbers. However, the range() function is available in both Python 2 and Python 3, whereas xrange() is only available in Python 2.

What is a list comprehension in Python and how is it used?

A list comprehension is a compact and efficient way to create a new list in Python by applying an operation to each element in an existing list or other iterable. It is a syntactic construct that consists of an expression followed by at least one "for" clause and zero or more "if" clauses. The resulting list comprehension is more readable and often more efficient than a traditional for loop.

Explain the difference between a local variable and a global variable in Python?

Local variables are only accessible within the scope of the function or block of code where they are defined. They are only visible within the function, and are destroyed when the function exits. Global variables are defined outside of any function or block of code, and are accessible from any part of the program. They have a global scope and exist for the lifetime of the program.

What is the difference between a tuple and a set in Python?

A tuple is an ordered collection of items, which can be of any type, and it is immutable. A set is an unordered collection of unique items, which can be of any type, and it is mutable. Tuples are enclosed in parentheses, while sets are enclosed in curly braces.

What is a lambda function in Python?

A lambda function is a small, anonymous function that can be created with the lambda keyword. They are often used as a shorthand way to create small functions with only one expression. Lambda functions can take any number of arguments, but can only have one expression. They are commonly used with built-in functions such as filter(), map() and reduce().

What is the difference between a module and a package in Python?

A module is a single file containing Python definitions and statements. A package is a collection of modules grouped together in a single directory. Packages are used to organize related modules and provide a namespace for the modules within the package. The package is defined by a file named init.py which can be empty or can contain initialization code.

What is the use of the "pass" statement in Python?

The "pass" statement in Python is used as a placeholder when a statement is required but no action needs to be taken. It is often used as a placeholder in control structures such as loops, functions, and classes when you plan to implement them later. It also serves as a marker to indicate that the code block is intentional and not an error.

How do you check if a key exists in a Python dictionary?

In Python, you can use the in operator to check if a key exists in a dictionary. The in operator returns True if the key is found in the dictionary, and False otherwise. You can also use the get() method to retrieve the value of a key from a dictionary and check if the returned value is None.

What is the difference between shallow copy and deep copy in Python?

A shallow copy creates a new object with a reference to the original object, while a deep copy creates a new object with a completely independent copy of the original object. In the case of shallow copy, if we modify the original object, the copy will also be modified, whereas in the case of deep copy, the copy remains unchanged if the original object is modified.

How do you handle file I/O in Python?

In Python, you can handle file I/O using built-in functions such as open(), read(), write(), and close(). The open() function is used to open a file, the read() function is used to read the contents of a file, the write() function is used to write to a file, and the close() function is used to close a file. You can also use the with statement to automatically close the file after reading or writing is done.

What is the difference between the "is" and "==" operators in Python?

The "is" operator is used to compare the identity of two objects, while the "==" operator is used to compare the values of two objects. The "is" operator returns True if both variables point to the same object in memory, whereas "==" operator returns True if the values of the variables are equal.

Documentary

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    ยฉ 2026 Creatd, Inc. All Rights Reserved.