Python Questions
5.33K subscribers
1 photo
7 links
Tasks for Beginners, Interview Questions, Regular expressions, simple coding problems, Quiz etc.

Useful Resources — »»» @python_resources_iGnani
Projects for Practice — »»» @python_projects_repository
Discussion Forum — »»» @python_programmers_club
Download Telegram
#interviewQuestions : 0010

Q: What is a Namespace in Python?

A namespace is basically a system to make sure that all the names of each and every object in a Python program are unique and can be used without any conflict.

You might already know that everything in Python like strings, lists, functions, etc. is an object. Python implements namespaces as dictionaries. There is a name-to-object mapping, with the names as keys and the objects as values. Multiple namespaces can use the same name and map it to a different object. Here are a few examples of namespaces:

Local Namespace: This namespace includes local names inside a function. This namespace is created when a function is called, and it only lasts until the function returns.

Global Namespace: This namespace includes names from various imported modules that you are using in a project. It is created when the module is included in the project, and it lasts until the script ends.

Built-in Namespace: This namespace includes built-in functions and built-in exception names.
#interviewQuestions : 0011

Q: Explain the following assignment?

x, y, z = 5, 10, 15
a = b = c = 0

a = x - z
x -= z

b = y - y + z
y -= (y + z)
print(b, y)

15 -15

Notice
the result in the here, it should be 15 and 15, but the value of y is -15.
Do you know why?
When the statements are evaluated, the statements are evaluated from left to right. But before that, anything within the braces are executed first.

So, in the first statement, since there is no curly braces, it first executes y - y and the result + z, this becomes 15
In the second statement, the expression within curly braces is executed, which is y + z, which comes to 25 and then now the expression becomes y -= 25, which is same as y = y- 25, hence the result -15.
#interviewQuestions : 0012

What is PYTHONPATH?

PYTHONPATH is an environment variable can be set, to add additional directories where python will look for modules and packages.

Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.

For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.

The only reason to set PYTHONPATH is to maintain directories of custom Python libraries that you do not want to install in the global default location (i.e., the site-packages directory).
#interviewQuestions : 0013

What are python modules?

Python modules are files containing Python code.

A module can define functions, classes and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use.