8FC21 PP&A Key
8FC21 PP&A Key
8FC21 PP&A Key
To create objects we required some Model or Plan or Blue print, which is nothing but class.
We can write a class to represent properties (attributes) and actions (behaviour) of object.
Properties can be represented by variables
Actions can be represented by Methods.
Hence class contains both variables and methods.
Object:-
Physical existence of a class is nothing but object.
We can create any number of objects for a class.
4) List any 4 functions of Numpy.
NumPy is a Python library used for working with arrays. It also has functions for working in
domain of linear algebra, Fourier transform, and matrices.
5) Define Algorithm?
The name 'Algorithm' refers to the sequence of instruction that must be followed to clarify a
problem.
The logical description of the instructions which may be executed to perform an essential
function.
Algorithms are usually generated independent of primary languages, i.e., an algorithm can
be achieved in more than one programming language.
6) What is a Spanning Tree?
A tree is a spanning tree for a linked graph if its set of vertices is the same as the graph's set
of vertices and its edges are a subgroup of the graph's edge set.
A spanning tree is a component of every linked graph. The sum of the weights of each edge
in a spanning tree is the tree's weight, denoted by the symbol w (T). The Minimum Spanning
Tree, abbreviated MST, is a spanning tree with the least amount of weight that is practically
possible.
7) What are the advantages of Python List?
The important characteristics of Python lists are as follows:
Lists are ordered.
Lists can contain any arbitrary objects.
List elements can be accessed by index.
Lists can be nested to arbitrary depth.
Lists are mutable.
Lists are dynamic.
8) What is overloading?
Function overloading or method overloading is the ability to create multiple functions of the
same name with different implementations.
9) Why we use Omega notation?
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running
time.
10) Tell about exception handling in Python?
In general, when a Python script encounters a situation that it cannot cope with, it raises an
exception. An exception is a Python object that represents an error. When a Python script
raises an exception, it must either handle the exception immediately otherwise it terminates
and quits.
Part – B
The data stored in memory can be of many types. For example, a student roll number is
stored as a numeric value and his or her address is stored as alphanumeric characters.
Python
has various standard data types that are used to define the operations possible on them and
the storage method for each of them.
Int:
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20
>>> print(0b10)
2
>>> print(0B10)
2
>>> print(0X20)
32
>>> 20
20
>>> 0b10
2
>>> a=10
>>> print(a)
10
# To verify the type of any object in Python, use the type() function:
>>> type(10)
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>
Float:
Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Float can also be scientific numbers with an "e" to indicate the power of 10.
>>> y=2.8
>>> y
2.8
>>> y=2.8
>>> print(type(y))
<class 'float'>
>>> type(.4)
<class 'float'>
>>> 2.
2.0
Example:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Output:
<class 'float'>
<class 'float'>
<class 'float'>
Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
String:
1. Strings in Python are identified as a contiguous set of characters represented in the
quotation marks. Python allows for either pairs of single or double quotes.
• 'hello' is the same as "hello".
• Strings can be output to screen using the print function. For example: print("hello").
>>> print("hello")
hello
>>> type("hello")
<class 'str'>
>>> print('hello')
hello
>>> " "
''
11) b) What are the different types of functions used in Python?
Functions and its use: Function is a group of related statements that perform a specific task.
Functions help break our program into smaller and modular chunks. As our program grows
larger and larger, functions make it more organized and manageable. It avoids repetition and
makes code reusable.
Basically, we can divide functions into the following two types:
1. Built-in functions - Functions that are built into Python.
Ex: abs(),all().ascii(),bool()………so on….
integer = -20
print('Absolute value of -20 is:', abs(integer))
Output:
Absolute value of -20 is: 20
2. User-defined functions - Functions defined by the users themselves.
def add_numbers(x,y):
sum = x + y
return sum
print("The sum is", add_numbers(5, 20))
Output:
The sum is 25
12) a) How can we access the values using the concept of dictionaries in Python?
The dictionary is an unordered collection that contains key : value pairs separated by commas
inside curly brackets.
Dictionaries are optimized to retrieve values when the key is known.
Access Dictionary:-
Dictionary is an unordered collection, so a value cannot be accessed using an index; instead, a
key must be specified in the square brackets, as shown below.
>>> capitals = {"USA":"Washington DC", "France":"Paris", "India":"New Delhi"}
>>>capitals["USA"]
'Washington DC'
>>> capitals["France"]
'Paris'
>>> capitals = {"USA":"Washington DC", "France":"Paris", "Japan":"Tokyo", "India":"New Delhi"}
>>> capitals.get("USA")
'Washington DC'
>>> capitals.get("France")
'Paris'
>>> capitals.get("usa")
>>> capitals.get("Japan")
12) b) Give the advantages of importing and using modules in Python with a suitable example.
Any text file with the .py extension containing Python code is basically a module.
Different Python objects such as functions, classes, variables, constants, etc., defined in one module
can be made available to an interpreter session or another Python script by using the import
statement.
Functions defined in built-in modules need to be imported before use.
Creating a Module:-
We can now import this module and execute the sum() function in the Python shell.
Python provides the standard library Tkinter for creating the graphical user interface for desktop
based applications.
Developing desktop based applications with python Tkinter is not a complex task. An empty Tkinter
top-level window can be created by using the following steps.
1. import the Tkinter module.
2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's computer screen.
Example
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
A bag or sack is given capacity and n objects are given. Each object has weight
wi and profit pi .
Fraction of object is considered as xi (i.e) 0<=xi<=1 .
If fraction is 1 then entire object is put into sack.
When we place this fraction into the sack we get
wixi and pixi.
Given a set of items, each with a weight and a value, determine a subset of items to
include in a collection so that the total weight is less than or equal to a given limit and the
total value is as large as possible.
The knapsack problem is in combinatorial optimization problem. It appears as a sub
problem in many, more complex mathematical models of real-world problems. One
general approach to difficult problems is to identify the most restrictive constraint, ignore
the others, solve a knapsack problem, and somehow adjust the solution to satisfy the
ignored constraints.
Applications
In many cases of resource allocation along with some constraint, the problem can be
derived in a similar way of Knapsack problem. Following is a set of example.
Finding the least wasteful way to cut raw materials
portfolio optimization
Cutting stock problems
16) b) Mention the advantages of dynamic programming with a suitable example.
One of the main advantages of using dynamic programming is that it speeds up processing,
since references that were previously calculated are used. As it is a recursive programming
technique, it reduces the lines of code in the program.
Dynamic programming is an effective method of solving problems that might
otherwise seem extremely difficult to solve in a reasonable amount of time.
Algorithms based on the dynamic programming paradigm are used in many areas of
science, including many examples in artificial intelligence, from planning problem
solving to speech recognition.
17) a) Give an example for if, Nested – if – else conditional statements.
The if statement: -
• The if statement is used to test a particular condition and if the condition is true, it executes
a block of code known as if-block.
• The condition of if statement can be any valid logical expression which can be either
evaluated to true or false.
Example: -
Program to print the largest of the three numbers.
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
Nested if-else statements: -
if(condition):
#Statements to execute if condition is true
if(condition):
#Statements to execute if condition is true
#end of nested if
#end of if
17) b) Write about tuples in Python with example.
*****