0% found this document useful (0 votes)
234 views

Python Theory Questions

The document contains 11 questions about Python concepts like data structures, functions, variables, modules, and more. It provides explanations for each answer. Key points covered include: - The functions globals() and locals() return a dictionary data structure. - Variables inside a function are local by default, not global even if assigned a value. - globals() returns a dictionary of the module namespace, locals() returns the current namespace. - Modules allow organizing and reusing Python code through the import statement. - *args and **kwargs are used when an unknown number of arguments or keyword arguments need to be passed to a function.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views

Python Theory Questions

The document contains 11 questions about Python concepts like data structures, functions, variables, modules, and more. It provides explanations for each answer. Key points covered include: - The functions globals() and locals() return a dictionary data structure. - Variables inside a function are local by default, not global even if assigned a value. - globals() returns a dictionary of the module namespace, locals() returns the current namespace. - Modules allow organizing and reusing Python code through the import statement. - *args and **kwargs are used when an unknown number of arguments or keyword arguments need to be passed to a function.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Which of the following data structures is returned by the functions globals() and
locals()?
a) list
b) set
c) dictionary
d) tuple
Answer: c
Explanation: Both the functions, that is, globals() and locals() return value of
the data structure dictionary
2.On assigning a value to a variable inside a function, it automatically becomes a
global variable. State whether true or false.
a) True
b) False
Answer: b
Explanation: On assigning a value to a variable inside a function, t automatically
becomes a local variable. Hence the above statement is false.
3.______________ returns a dictionary of the module namespace.
________________ returns a dictionary of the current namespace.
a) locals()
globals()
b) locals()
locals()
c) globals()
locals()
d) globals()
globals()
Answer: c
Explanation: The function globals() returns a dictionary of the module namespace,
whereas the function locals() returns a dictionary of the current namespace.
4.If a is a dictionary with some key-value pairs, what does a.popitem() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary.
Answer: a
Explanation: The method popitem() removes a random key-value pair.

5. What is a dictionary in Python?


Python dictionary is one of the supported data types in Python. It is an unordered
collection of elements. The elements in dictionaries are stored as key�value pairs.
Dictionaries are indexed by keys.
For example, we have a dictionary named �dict�. It contains some keys: Country,
Capital along with their corresponding values: India and New Delhi.
dict={�Country�:�India�,�Capital�:�New Delhi�, }

6.What is a Python module?


Modules are independent Python scripts with .py extension that can be reused in
other Python codes or scripts using the import statement. A module can consist of
functions, classes, and variables, or some runnable code. Modules not only help in
keeping Python codes organized but also in making codes less complex and more
efficient. The syntax to import modules in Python codes is as follows:
import module_name # include this code line on top of the script

7.Is indentation optional in Python?


Indentation in Python is compulsory and is part of its syntax. All programming
languages have some way of defining the scope and extent of the block of codes; in
Python, it is indentation. Indentation provides better readability to the code,
which is probably why Python has made it compulsory.
8.What is the difference between append() and extend() methods?
Both append() and extend() methods are methods used for lists. These methods are
used to add elements at the end of a list.
append(element): Adds the given element at the end of the list which called this
method
extend(another-list): Adds the elements of another-list at the end of the list
which called the extend method

9.What is namespace in Python?


Ans: A namespace is a naming system used to make sure that names are unique to
avoid naming conflicts.

10.What are local variables and global variables in Python?


Global Variables:
Variables declared outside a function or in global space are called global
variables. These variables can be accessed by any function in the program.
Local Variables:
Any variable declared inside a function is known as a local variable. This variable
is present in the local space and not in the global space.

11.What does this mean: *args, **kwargs? And why would we use it?
Ans: We use *args when we aren�t sure how many arguments are going to be passed to
a function, or if we want to pass a stored list or tuple of arguments to a
function. **kwargs is used when we don�t know how many keyword arguments will be
passed to a function, or it can be used to pass the values of a dictionary as
keyword arguments. The identifiers args and kwargs are a convention, you could also
use *bob and **billy but that would not be wise.

You might also like