Complete Python Questions Answers
Complete Python Questions Answers
a) Explain different functions or ways to remove key : value pair from Dictionary.
pop(): We can remove a particular item in a dictionary by using the method pop(). This method
removes an item with the provided key and returns the value.
Example:
>>> squares
{1: 1, 3: 9, 4: 16}
popitem(): The method, popitem() can be used to remove and return an arbitrary item (key, value)
Example:
(5, 25)
>>> squares
{1: 1, 2: 4, 3: 9, 4: 16}
clear(): All the items can be removed at once using the clear() method.
Example:
{}
del(): We can also use the del keyword to remove individual items or the entire dictionary itself.
Example:
>>> squares
{1: 1, 4: 16}
NumPy is the fundamental package for scientific computing with Python. It provides a
high-performance multidimensional array object, and tools for working with these arrays.
An array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive
integers. NumPy's array class is called ndarray. It is also known by the alias array.
In NumPy, dimensions are called axes. Each dimension of an array has a length, which is the total
A one-dimensional array has one axis, while a two-dimensional array has rows and columns, with
NumPy arrays are fixed-size, which means once created, the size cannot be changed.
Operations available in NumPy include mathematical and logical operations on arrays, Fourier
c) Explain seek() and tell() function for file pointer manipulation in Python with example.
seek(): The seek() function in Python is used to move the file pointer to a desired position within the
file.
Example:
>>> f.seek(4)
tell(): The tell() function returns the current position of the file pointer, which indicates from where the
Example:
1. Set Union: The union of two sets is the set of all elements from both sets without duplicates.
Example:
>>> first_set.union(second_set)
{1, 2, 3, 4, 5}
2. Set Intersection: The intersection of two sets contains the common elements between both sets.
Example:
>>> first_set.intersection(second_set)
{4, 5, 6}
3. Set Difference: The difference between two sets contains elements from the first set that are not
Example:
>>> first_set.difference(second_set)
{1, 2, 3}
4. Set Symmetric Difference: The symmetric difference contains elements that are in either set, but
Example:
>>> first_set.symmetric_difference(second_set)
{1, 2, 3, 7, 8, 9}
1) Python Identifiers: Identifiers are the names given to variables. The first character must be a letter
or underscore, and subsequent characters can be letters, numbers, or underscores.
2) Reserved Words: These are predefined keywords in Python that have specific meanings, and you
cannot use them as identifiers. Examples include if, for, and while.
3) Indentation: Python uses indentation (spaces or tabs) to define the structure of the code.
Indentation is mandatory and defines blocks of code for loops, conditionals, and functions.
4) Python Types: Python has various built-in data types like String, Integer, Float, and Boolean.
Python also includes collection types like Lists, Tuples, Sets, and Dictionaries.
5) Control structures: These are used to control the flow of execution in the program. Examples
include if-else statements, for and while loops, and exception handling using try-except.
6) Functions: Functions are reusable blocks of code that perform specific tasks. Functions are
7) Modules: Modules are Python files containing code that can be imported and reused in other
Python programs.
8) Packages: A package is a collection of related Python modules. Packages are used for
1) add(): Adds an element to the set. If the element already exists, it does not add it again.
Example:
>>> print(s)
2) discard(): Removes an element from the set. It does not raise an error if the element is not found.
Example:
>>> s.discard('b')
>>> print(s)
{'a'}
3) remove(): Removes an element from the set. If the element is not found, it raises a KeyError.
Example:
>>> s.remove('a')
>>> print(s)
{'b'}
Example:
>>> s.clear()
>>> print(s)
set()