Python Programming: Detailed Solutions for MSBTE Questions
Subject: Python Programming (MSBTE)
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, 2: 4, 3: 9, 4: 16}
>>> squares.pop(2) # remove a particular item
>>> squares
{1: 1, 3: 9, 4: 16}
popitem(): The method, popitem() can be used to remove and return an arbitrary item (key, value)
from the dictionary.
Example:
>>> squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
>>> print(squares.popitem()) # remove an arbitrary item
(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:
>>> squares = {1: 1, 4: 16}
>>> squares.clear() # removes all items
>>> squares
{}
del(): We can also use the del keyword to remove individual items or the entire dictionary itself.
Example:
>>> squares = {1: 1, 3: 9, 4: 16}
>>> del squares[3] # delete a particular item
>>> squares
{1: 1, 4: 16}
b) Explain Numpy package in detail.
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
number of elements in that direction.
A one-dimensional array has one axis, while a two-dimensional array has rows and columns, with
each row being Axis-0 and each column being Axis-1.
NumPy arrays are fixed-size, which means once created, the size cannot be changed.
Installation in Windows, Linux, and Mac OS:
python -m pip install numpy
To use NumPy, import it using:
import numpy as np
Operations available in NumPy include mathematical and logical operations on arrays, Fourier
transforms, linear algebra operations, and random number generation.
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.
Syntax: f.seek(offset, fromwhere)
Example:
>>> f = open('demofile.txt', 'r')
>>> f.seek(4)
>>> print(f.readline()) # Reads from the 4th index position
tell(): The tell() function returns the current position of the file pointer, which indicates from where the
next read or write operation will occur.
Example:
>>> f = open('demofile.txt', 'r')
>>> print(f.tell()) # Prints the current position of the file pointer
a) Explain any four set operations with examples.
1. Set Union: The union of two sets is the set of all elements from both sets without duplicates.
Syntax: first_set.union(second_set) or first_set | second_set.
Example:
>>> first_set = {1, 2, 3}
>>> second_set = {3, 4, 5}
>>> 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.
Syntax: first_set.intersection(second_set) or first_set & second_set.
Example:
>>> first_set = {1, 2, 3, 4, 5, 6}
>>> second_set = {4, 5, 6, 7, 8, 9}
>>> 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
in the second set. Syntax: first_set.difference(second_set) or first_set - second_set.
Example:
>>> first_set = {1, 2, 3, 4, 5, 6}
>>> second_set = {4, 5, 6, 7, 8, 9}
>>> first_set.difference(second_set)
{1, 2, 3}
4. Set Symmetric Difference: The symmetric difference contains elements that are in either set, but
not both. Syntax: first_set.symmetric_difference(second_set) or first_set ^ second_set.
Example:
>>> first_set = {1, 2, 3, 4, 5, 6}
>>> second_set = {4, 5, 6, 7, 8, 9}
>>> first_set.symmetric_difference(second_set)
{1, 2, 3, 7, 8, 9}
b) Explain building blocks of Python.
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
defined using the def keyword in Python.
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
organizing and distributing libraries and tools.
c) List and explain any four built-in functions on set.
1) add(): Adds an element to the set. If the element already exists, it does not add it again.
Example:
>>> s = {'a', 'b'}
>>> s.add('c')
>>> print(s)
{'a', 'b', 'c'}
2) discard(): Removes an element from the set. It does not raise an error if the element is not found.
Example:
>>> s = {'a', 'b'}
>>> 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 = {'a', 'b'}
>>> s.remove('a')
>>> print(s)
{'b'}
4) clear(): Removes all elements from the set.
Example:
>>> s = {'a', 'b'}
>>> s.clear()
>>> print(s)
set()