Test your skills through the online practice test: Python Quiz Online Practice Test

Related differences

Python vs JavaPython 2 vs Python 3

Ques 96. What is Python Closures?

As we have already discussed, closure is a nested function that helps us access the outer function's variables even after the outer function is closed. For example,

def greet():
    # variable defined outside the inner function
    name = "John"
    
    # return a nested anonymous function
    return lambda: "Hi " + name

# call the outer function
message = greet()

# call the inner function
print(message())

Output:

Hi John

In the above example, we have created a function named greet() that returns a nested anonymous function.

Is it helpful? Add Comment View Comments
 

Ques 97. When to use closures? So what are closures good for?

Closures can be used to avoid global values and provide data hiding, and can be an elegant solution for simple cases with one or few methods.

However, for larger cases with multiple attributes and methods, a class implementation may be more appropriate.

Is it helpful? Add Comment View Comments
 

Ques 98. What is the difference between append() and extend() methods?

Both append() and extend() methods are methods used to add elements at the end of a list.

  • append(element): Adds the given element at the end of the list that called this append() method
  • extend(another-list): Adds the elements of another list at the end of the list that called this extend() method

Is it helpful? Add Comment View Comments
 

Ques 99. How is Multithreading achieved in Python?

Python has a multi-threading package ,but commonly not considered as good practice to use it as it will result in increased code execution time.

  • Python has a constructor called the Global Interpreter Lock (GIL). The GIL ensures that only one of your ‘threads’ can execute at one time.The process makes sure that a thread acquires the GIL, does a little work, then passes the GIL onto the next thread.
  • This happens at a very Quick instance of time and that’s why to the human eye it seems like your threads are executing parallely, but in reality they are executing one by one by just taking turns using the same CPU core.

Is it helpful? Add Comment View Comments
 

Ques 100. What is functional programming? Does Python follow a functional programming style? If yes, list a few methods to implement functionally oriented programming in Python.

Functional programming is a coding style where the main source of logic in a program comes from functions.

Incorporating functional programming in our codes means writing pure functions.

Pure functions are functions that cause little or no changes outside the scope of the function. These changes are referred to as side effects. To reduce side effects, pure functions are used, which makes the code easy-to-follow, test, or debug.

Python does follow a functional programming style.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: