Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
×

NOTE!

Click on MENU to Browse between Subjects...

Advertisement

17CS664 - PYTHON APPLICATION PROGRAMMING

Answer Script for Module 3

Solved Previous Year Question Paper

CBCS SCHEME


PYTHON APPLICATION PROGRAMMING

[As per Choice Based Credit System (CBCS) scheme]

(Effective from the academic year 2017 - 2018)

SEMESTER - VI

Subject Code 17CS664

IA Marks 40

Number of Lecture Hours/Week 3

Exam Marks 60




Advertisement

These Questions are being framed for helping the students in the "FINAL Exams" Only (Remember for Internals the Question Paper is set by your respective teachers). Questions may be repeated, just to show students how VTU can frame Questions.

- ADMIN




× CLICK ON THE QUESTIONS TO VIEW ANSWER


ANSWER :

1.1 Lists:

A list is an ordered sequence of values. It is a data structure in Python. The values inside the lists can be of any type (like integer, float, strings, lists, tuples, dictionaries etc) and are called as elements or items. The elements of lists are enclosed within square brackets. For example,

ls1=[10,-4, 25, 13]

ls2=["Tiger", "Lion", "Cheetah"]

FVBIE LEARN IN ORDERLY FASHION: Extra: Lists are mutable : Refer 17 th Question & Answer

1.2 List Slices:

Similar to strings, the slicing can be applied on lists as well. Consider a list t given below, and a series of examples following based on this object.

Here, ls1 is a list containing four integers, and ls2 is a list containing three strings. A list need not contain data of same type. We can have mixed type of elements in list. For example,

ls3 = [3.5, 'Tiger', 10, [3,4]]

Here, ls3 contains a float, a string, an integer and a list. This illustrates that a list can be nested as well.

t=['a','b','c','d','e']

i.

Extracting full list without using any index, but only a slicing operator -

>>> print(t[:])

['a', 'b', 'c', 'd', 'e']

ii ii.

Extracting elements from 2

nd

position -

>>> print(t[1:])

['b', 'c', 'd', 'e']

iii.

Extracting first three elements -

>>> print(t[:3])

['a', 'b', 'c']

iv.

Selecting some middle elements -

>>> print(t[2:4])

['c', 'd']

v.

Using negative indexing -

>>> print(t[:-2])

['a', 'b', 'c']

vi

vi. Reversing a list

using negative value for stride -

>>> print(t[::-1])

['e', 'd', 'c', 'b', 'a']

vii. Modifying (reassignment) only required set of values -

>>> t[1:3]=['p','q']

>>> print(t)

['a', 'p', 'q', 'd', 'e']

Thus, slicing can make many tasks simple.

1.3

Traversing a List

A list can be traversed using for loop. If we need to use each element in the list, we can use the for loop and in operator as below -

Demonstration of List Traversal

Fig 1.1

Demonstration of List Traversal

Output Demonstration of List Traversal

Fig 1.2

Output Demonstration of List Traversal

List elements can be accessed with the combination of range() and len() functions as well -

Demonstration of Traversal using Range Function

Fig 1.3

Demonstration of Traversal using Range Function

Output of Demonstration of Traversal using Range Function

Fig 1.4

Output of Demonstration of Traversal using Range Function

Here, we wanted to do modification in the elements of list. Hence, referring indices is suitable than referring elements directly. The len() returns total number of elements in the list (here it is 4). Then range() function makes the loop to range from 0 to 3 (i.e. 4-1). Then, for every index, we are updating the list elements (replacing original value by its square).



Advertisement

ANSWER :

FVBIE LEARN IN ORDERLY FASHION: Extra: Definition of Tuple, Creating a Tuple, Accessing a Tuple. Refer 18th Question & Answer.

2.1

Comparing Tuples

Tuples can be compared using operators like >, <, >=, == etc. The comparison happens lexicographically. For example, when we need to check equality among two tuple objects, the first item in first tuple is compared with first item in second tuple. If they are same, 2 nd items are compared. The check continues till either a mismatch is found or items get over. Consider few examples

>>>(1,2,3)==(1,2,5)

False

>>> (3,4)==(3,4)

True

The meaning of < and > in tuples is not exactly less than and greater than

,

instead, it means comes before and comes after. Hence in such cases, we will get results different from checking equality (==).

>>> (1,2,3)<(1,2,5)

True

>>> (3,4)<(5,2)

True

When we use relational operator on tuples containing non-comparable types, then TypeError will be thrown.

>>> (1,'hi')<('hello','world')

TypeError: '<' not supported between instances of 'int' and 'str'

2.2 Sort Function:

The sort() function internally works on similar pattern - it sorts primarily by first element, in case of tie, it sorts on second element and so on. This pattern is known as

DSU

-

I.

Decorate

a sequence by building a list of tuples with one or more sort keys

preceding the elements from the sequence

II.

Sort

the list of tuples using the Python built-in

sort(), and

III.

Undecorate

by extracting the sorted elements of the sequence.

Consider a program of sorting words in a sentence from longest to shortest, which illustrates DSU property.

Demonstration of DSU Property

Fig 2.1

Demonstration of DSU Property

Output of demonstration of DSU Property

Fig 2.2

Output of demonstration of DSU Property

In the above program, we have split the sentence into a list of words. Then, a tuple containing length of the word and the word itself are created and are appended to a list. Observe the output of this list - it is a list of tuples. Then we are sorting this list in descending order. Now for sorting, length of the word is considered, because it is a first element in the tuple. At the end, we extract length and word in the list, and create another list containing only the words and print it.


ANSWER :

3.1 Program:

Program to read a file & Return Lines starting From 'F' &
		followed by 2 characters, followed by 'm'

Fig 3.1

Program to read a file & Return Lines starting From 'F' & followed by 2 characters, followed by 'm'

Output for Program to read a file & Return Lines starting From 'F'
		& followed by 2 characters, followed by 'm'

Fig 3.2

Output for Program to read a file & Return Lines starting From 'F' & followed by 2 characters, followed by 'm'

Contents of sample.txt

Fig 3.3

Contents of sample.txt

3.2 Explanation:

There are a number of other special characters that let us build even more powerful regular expressions. The most commonly used special character is the period or full stop, which matches any character.

In the following example, the regular expression F..m would match any of the strings "From", "Fxxm", "F12m", or "F!@m" since the period characters in the regular expression match any character.

This is particularly powerful when combined with the ability to indicate that a character can be repeated any number of times using the * or + characters in your regular expression. These special characters mean that instead of matching a single character in the search string, they match zero-or-more characters (in the case of the asterisk) or one-or-more of the characters (in the case of the plus sign).



Advertisement

ANSWER :

4.1 Dictionary:

Dictionaries

have a method called items() that returns a list of tuples, where each tuple is a key-value pair as shown below -

>>> d = {'a':10, 'b':1, 'c':22}

>>>t = list(d.items())

>>> print(t)

[('b', 1), ('a', 10), ('c', 22)]

4.2 Sorting in Dictionary:

As dictionary may not display the contents in an order, we can use sort() on lists and then print in required order as below -

>>> d = {'a':10, 'b':1, 'c':22}

>>> t = list(d.items())

>>> print(t)

[('b', 1), ('a', 10), ('c', 22)]

>>> t.sort()

>>> print(t)

[('a', 10), ('b', 1), ('c', 22)]

4.3 Difference between Dictionary and Lists:

LIST

DICTIONARY

List is a collection of index values pairs as that of array in c++.

Dictionary is a hashed structure of key and value pairs.

List is created by placing elements in [ ] seperated by commas ", "

Dictionary is created by placing elements in { } as "key":"value", each key value pair is seperated by commas ", "

The indices of list are integers starting from 0.

The keys of dictionary can be of any data type.

The elements are accessed via indices.

The elements are accessed via key-values.

The order of the elements entered are maintained.

There is no guarantee for maintaining order.

1.3

Program to count occurrence of characters in a string and print the count

Program to count occurrence of characters in a string and print the
		count

Fig 4.1:

Program to count occurrence of characters in a string and print the count

Output of program to count occurrence of characters in a string and
		print the count

Fig 4.2:

Output of program to count occurrence of characters in a string and print the count


ANSWER :

When you pass a list to a function, the function gets a reference to the list. If the function modifies a list parameter, the caller sees the change. For example, delete_head removes the first element from a list:

Illustration of List when passes as a Argument

Fig 5.1

Illustration of List when passes as a Argument

Output of Illustration of List when passes as a Argument

Fig 5.2

Output of Illustration of List when passes as a Argument

The parameter t and the variable letters are aliases for the same object.

It is important to distinguish between operations that modify lists and operations that create new lists. For example, the append method modifies a list, but the + operator creates a new list:

Refer

24th Question & Answer

,

Where chop function modifies the list but middle function creates a new list & returns to the calling variable.


× NOTE: Each Page Provides only 5 Questions & Answer
Below Page NAVIGATION Links are Provided...
All the Questions on Question Bank Is SOLVED

Advertisement

lIKE OUR CONTENT SUPPORT US BY FOLLOWING US ON INSTAGRAM : @futurevisionbie

For immediate Notification Join the Telegram Channel



× SUGGESTION: SHARE WITH ALL THE STUDENTS AND FRIENDS -ADMIN