Python interview questions--1

Table of contents

python interview questions

1) What is Python? What are the benefits of using Python?

2) What is PEP 8?

3) What is serialization and deserialization?

4) How to interpret Python?

5) How is memory managed in Python?

6) What tools are available to help find bugs or perform static analysis?

7) What are Python decorators?

9) How are parameters passed by value or by reference?

10) What are Dict and List comprehensions?

11) What are the built-in types provided by python?

12) What are namespaces in Python?

13) What is lambda in Python?

14) Why doesn't the lambda form in python have a statement?

15) What is Python pass?

21) How to copy objects in Python?

22) What is an inverted index in Python?

23) How to convert a number to a string?

25) What are module and package in Python?

26) What are the rules for referring to local and global variables in Python?

27) How to share global variables across modules?

32) Mention the use of the // operator in Python?

33) Top five benefits of using Python?

34) Mention the use of split function in Python?

36) What is the difference between Django, Pyramid and Flask?

Django

Flask

Pyramid

40) Flask is an MVC model, if yes, give an example of the MVC pattern for your application?


python interview questions

1) What is Python? What are the benefits of using Python?

Python is a programming language that contains objects, modules, threads, exceptions, and automatic memory management . The good thing about Python is that it's easy to use, portable, extensible, has built-in data structures, and it's an open source.

2) What is PEP 8?

PEP 8 is a coding convention on how to write Python code to be more readable.

3) What is serialization and deserialization?

The Pickle module takes any Python object and converts it to a string representation and dumps it to a file using the dump function, a process called pickling.

The process of retrieving a raw Python object from a stored string representation is called unpickling.

4) How to interpret Python?

The Python language is an interpreted language. Python programs run directly from source code. It converts the source code written by the programmer into an intermediate language and again into machine language which must be executed.

5) How is memory managed in Python?

  • Python memory is managed by the Python private heap space. All Python objects and data structures are located on the private heap. Programmers do not have access to this private heap , the interpreter is responsible for handling this private heap.
  • Allocation of Python heap space for Python objects is done by the Python memory manager. The core API provides some tools for programmers to write code.
  • Python also has a built-in garbage collector which reclaims all unused memory and frees memory and makes it available for heap space.

6) What tools are available to help find bugs or perform static analysis?

PyChecker is a static analysis tool that detects errors in Python source code and warns about errors of style and complexity. Pylint is another tool for verifying that a module complies with coding standards.

7) What are Python decorators?

Python decorators are a specific change we make in the Python syntax to easily change functions.

8) What is the difference between list and tuple?

The difference between lists and tuples is that lists are mutable whereas tuples are not. Tuples can be hashed, e.g. as dictionary keys.

  • Lists are mutable. It can be modified after creation.
  • Tuples are immutable. Once a tuple is created, it cannot be changed.
  • Lists represent order. They are ordered sequences, usually objects of the same type. Say for example all usernames sorted by creation date like ["Seth", "Ema", "Eli"].
  • A tuple represents a structure. Can be used to store elements of different data types. For example, database records in memory, such as (2, "Ema", "2020–04–16") (#id, name, creation date).

9) How are parameters passed by value or by reference?

Everything in Python is an object, and all variables contain references to objects. Reference values ​​are based on functionality; therefore, you cannot change the referenced value. However, they can be changed if the objects are mutable.

10) What are Dict and List comprehensions?

They are syntactic constructs that allow easy creation of a Dictionary or List from an existing iterable.

11) What are the built-in types provided by python?

Pythons with mutable and immutable types, the built-in type is the Mutable built-in type

  • List
  • Sets
  • Dictionaries

immutable built-in types

  • Strings
  • Tuples
  • Numbers

12) What are namespaces in Python?

In Python, every name that is imported has a place where it can be concatenated. This is called a namespace. It's like a box where variable names map to dropped objects. Whenever a variable is searched, this box will be searched for the corresponding object.

13) What is lambda in Python?

It is a single expression anonymous function, usually used as an inline function .

14) Why doesn't the lambda form in python have a statement?

The lambda form in python has no statements because it is used to create new function objects and then return them at runtime.

15) What is Python pass?

pass means, a no-op Python statement , or in other words, it's a placeholder in a compound statement where whitespace should be left and nothing has to be written there.

16) What is an iterator in Python?

In Python, an iterator is used to iterate over a set of elements, such as a container such as a list.

17) What is unit testing in Python?

The unit testing framework in Python is called unittest. It supports sharing settings, automating tests, testing off code, aggregating tests into collections, and more.

18) What is slicing in Python?

The mechanism for selecting a sequence of items from a sequence type (such as a list, tuple, string, etc.) is called slicing.

19) What are generators in Python?

Methods that implement iterators are called generators. This is a normal function, except that it produces expressions within the function.

20) What is docstring in Python?

Python documentation strings are called docstrings, and it is a way of documenting Python functions, modules, and classes.

21) How to copy objects in Python?

To copy objects in Python, try copy.copy() or copy.deepcopy() for the general case. You can't copy all objects, but most objects.

22) What is an inverted index in Python?

Python sequences can be both positive and negative indices. For positive indices, 0 is the first index, 1 is the second, and so on. For negative indices, (-1) is the last index, (-2) is the second-to-last index, and so on.

23) How to convert a number to a string?

To convert a number to a string, use the built-in function str() . If octal or hexadecimal representation is required, use the built-in functions oct() or hex().

24) What is the difference between Xrange and range?

Xrange returns xrange objects, while range returns lists, and uses the same memory regardless of range size.

25) What are module and package in Python?

In Python, modules are a way of structuring programs. Every Python program file is a module that imports other modules such as objects and attributes.

A folder of Python programs is a module package. Packages can contain modules or subfolders.

26) What are the rules for referring to local and global variables in Python?

Local variables : If a variable is assigned a value anywhere in the function body, it is assumed to be local.

Global Variables : Only those variables that are referenced inside a function are implicit global variables .

27) How to share global variables across modules?

To share global variables across modules within a single program, create a special module. Import the configuration module in all modules of the application. This module will be available as a global variable across modules.

28) Explain how to create Python scripts on Unix?

To make a Python script executable on Unix, you need to do two things,

  • The mode of the script file must be executable
  • The first line must start with # (#!/usr/local/bin/python)

29) Explain how to delete a file in Python?

Use the command os.remove(filename) or os.unlink(filename)

30) Explain how to generate random numbers in Python?

To generate random numbers in Python, you need to import the command as:

import random

random.random()

This will return a random float in the range [0,1).

31) Explain how to access a module written in Python from C?

You can access modules written in Python in C via,

Module = = PyImport_ImportModule(“”);

32) Mention the use of the // operator in Python?

It is a Floor Divisionoperator that divides two operands and the result is a quotient, displaying only the digits before the decimal point. For example, 10 // 5 = 2 and 10.0 // 5.0 = 2.0.

33) Top five benefits of using Python?

  • Python includes a huge standard library for most Internet platforms like email, HTML, etc.
  • Python does not require explicit memory management as the interpreter itself allocates memory to new variables and frees them automatically
  • Easy to read due to square brackets
  • Easy to learn for beginners
  • Having built-in data types saves programming time and effort, declaring variables

34) Mention the use of split function in Python ?

Using the split function in Python is to break a string into shorter strings using a defined delimiter . It gives a list of all words present in the string.

35) Explain what is Flask and its benefits ?

Flask is a web microframework for Python based on the "Werkzeug, Jinja 2 and good intentions" BSD license. Werkzeug and jingja are two of its dependencies.

Flask is part of Microframework. This means it has few dependencies on external libraries. It makes the framework lightweight while having few updated dependencies and fewer security bugs.

36) What is the difference between Django, Pyramid and Flask?

Flask is a "micro-framework" primarily intended for smaller applications with simpler requirements. In Flask, you have to use external libraries.

Pyramid is built for larger applications. It provides flexibility and allows developers to use the right tool for their project. Developers can choose databases, URL structures, template styles, and more. Pyramid is configurable.

Like Pyramid, Django can also be used in larger applications. It includes an ORM.

Django


Django is a full-featured web development framework that provides many out-of-the-box features and tools that allow developers to build complex web applications faster. Here are some advantages and application scenarios of Django:

advantage

Complete feature set: Django provides many built-in features such as authentication system, admin backend, form processing, and database integration. This allows developers to focus more on business logic rather than low-level implementation details.

ORM support: Django's object-relational mapping (ORM) layer allows developers to use Python code instead of SQL statements to manipulate the database. This simplifies data access and management, and improves development productivity.

Strong community support: Django has a huge community, providing a lot of documentation, tutorials and open source projects. This means developers can easily find solutions and get help.

Extensibility: Django's modular design allows developers to easily add, replace, or extend various components to meet specific project needs.


Flask

is a lightweight web framework that focuses on simplicity and flexibility. Compared with Django, Flask provides fewer functions, but also gives developers more freedom and flexibility. Here are some advantages and application scenarios of Flask:

 advantage

- Easy to learn: Flask's design philosophy is "simple yet flexible", so it is concise, easy to understand and use. Developers can get started quickly and can freely choose the components to use according to their needs.

- Flexibility: Flask provides basic functions and tools, but it does not force developers to develop in a specific way. Developers can choose the plug-ins, libraries and tools they use according to their needs, thus achieving a higher degree of flexibility.

- Micro-framework features: Since Flask is a micro-framework, it does not have a built-in database abstraction layer, form validation and other functions, but these functions can be added through plug-ins, thereby reducing the complexity of the framework and redundant code.

Pyramid


Pyramid is a simple and powerful web development framework, its design goal is to provide a flexible way of development, while maintaining scalability and high performance. Here are some advantages and application scenarios of Pyramid:

37) What is Flask-WTF and what are their characteristics?

Flask-WTF provides easy integration with WTForms. Features include Flask WTF:

  • Integration with wtforms
  • Secure form with csrf token
  • Global csrf protection
  • Internationalization integration
  • Recaptcha supporting
  • File upload that works with Flask Uploads

38) What is the common way to explain the working of Flask script?

The usual way Flask scripts work is:

  • The import path of the application
  • or the path to a Python file

39) Explain how to access session in Flask?

Sessions basically allow you to remember information from one request to another. In one Flask, it uses signed cookies so that the user can view the session contents and modify them. If only the key Flask.secret_key is present, the user can modify the session.

40) Flask is an MVC model, if yes, give an example of the MVC pattern for your application?

Basically, Flask is a minimalistic framework that behaves the same as an MVC framework. So MVC is very suitable for Flask, the pattern of MVC we will consider the following examples.

 

Guess you like

Origin blog.csdn.net/qq_38998213/article/details/132509733