[go: up one dir, main page]

0% found this document useful (0 votes)
2 views7 pages

python ass 1

The document contains a series of Python programming assignments that require the creation of various functions. These functions include shifting elements in a list, rearranging list halves, swapping alternate elements, replacing characters in a string, calculating grades from a dictionary, counting long place names, determining word lengths, and filtering students based on their marks. Each assignment includes sample input and output to illustrate the expected functionality.

Uploaded by

vani.gera2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

python ass 1

The document contains a series of Python programming assignments that require the creation of various functions. These functions include shifting elements in a list, rearranging list halves, swapping alternate elements, replacing characters in a string, calculating grades from a dictionary, counting long place names, determining word lengths, and filtering students based on their marks. Each assignment includes sample input and output to illustrate the expected functionality.

Uploaded by

vani.gera2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Assignment 1

Q1. WAP that creates a function LShift(Arr,n) in Python, which accepts a list Arr of numbers and n
is a numeric value by which all the elements of the list are shifted to the left.

Sample input Data of list Arr=[10,20,30,40,12,11], n=2

Output Arr= [30,40,12,11,10,20]

INPUT

def Lshift(Arr,n):

for i in range(n):

t=Arr[0]

for j in range(1,len(Arr)):

Arr[j-1]=Arr[j]

Arr[-1]=t

return Arr

a=eval(input('List: '))

x=int(input('Write the number by which you want to shift the list: '))

print(Lshift(a,x))

OUTPUT

List: [10,20,30,40,12,11]

Write the number by which you want to shift the list: 2

[30, 40, 12, 11, 10, 20]

Q2. WAP that creates a function ReArrange(Arr) in Python, which should swap the contents of the
first half locations of a list Arr with the contents of the second half locations. Note: Assuming that
the list has even number of values in it.

Sample input data: [12,5,7,23,8,10]

Output Arr: [23,8,10,12,5,7]

INPUT

def ReArrange(Arr):

mid=len(Arr)//2

Arr[0:mid],Arr[mid:]=Arr[mid:],Arr[0:mid]

return Arr

l=eval(input('List: '))

print(ReArrange(l))
OUTPUT

List: [12,5,7,23,8,10]

[23, 8, 10, 12, 5, 7]

Q3. WAP that creates a function SwapAlternate(Arr) in Python, which should swap the contents at
even locations with that of odd locations of a list Arr.

Sample input data of Arr= [10,20,30,40,50,60,70]

Output Arr= [20,10,40,30,60,50,70]

INPUT

def SwapAlternate(Arr):

for i in range(len(Arr)-1):

if i%2==0:

t=Arr[i]

Arr[i]=Arr[i+1]

else:

Arr[i]=t

return Arr

a=eval(input('Enter a list: '))

print(SwapAlternate(a))

OUTPUT

Enter a list: [10,20,30,40,50,60,70]

[20, 10, 40, 30, 60, 50, 70]

Q4. WAP that creates a function Puzzle(W, N) which takes the argument w as an English word and
n as an integer and returns the string where every nth alphabet of the word w is replaces with
underscore(‘_’).

Sample input: w is TELEVISION, n is 3

Output: TEL_VIS_ON

INPUT

def Puzzle(W,N):

s=''

for i in range(len(W)):

if i in range(N,len(W),N+1):
s+='_'

else:

s+=W[i]

return s

a=input('String: ')

n=int(input('Write the number: '))

print(Puzzle(a,n))

OUTPUT

String: Television

Write the number: 3

Tel_vis_on

Q5. WAP that creates a user defined function showGrades(S) which takes the dictionary S as an
argument. The dictionary S contains Name:[Physics,Maths,Chemistry] as key:value pairs. The
function should display the corresponding grade obtained by the students according to the
following criteria:
Average of PCM Grade
>=90 A
<90 but >=60 B
<60 C

Sample input: {‘Amit’:[92,86,64],’Nagma’:[65,42,43]}

Output:

Amit- B

Nagma-C

INPUT

def showGrades(S):

for i in S:

a=sum(S[i])/3

if a>=90:

grade='A'

elif a<90 and a>=60:

grade='B'

else:

grade='C'
print(i,'-',grade)

n=int(input('Write the number of students you want to add: '))

d={}

for i in range(n):

name=input('Name of the student: ')

p=int(input('Marks in physics: '))

m=int(input('Marks in maths: '))

c=int(input(Marks in chemistry: '))

d[name]=[p,m,c]

showGrades(d)

OUTPUT

Write the number of students you want to add: 2

Name of the student: Amit

Marks in physics: 92

Marks in maths: 84

Marks in chemistry: 64

Name of the student: Nagma

Marks in physics: 65

Marks in maths: 42

Marks in chemistry: 43

Amit - B

Nagma – C

Q6. WAP that creates a user defined function countNow(Places) that takes a dictionary, Places as
an argument and returns another dictionary with those places (in upper case) whose names are
longer than 5 characters. The output should be as shown below:

Sample input: {1:’Delhi’,2:’London’,3:’New York’}

Output: LONDON, NEW YORK

INPUT

def countNow(Places):

t=[]
for i in Places:

if len(Places[i])>5:

t.append(Places[i].upper())

return t

n=int(input('Write the number of places you want to add: '))

d={}

for i in range(n):

p=input(‘Name of the place: ')

d[i+1]=p

a=countNow(d)

print(','.join(a))

OUTPUT

Write the number of places you want to add: 3

Name of the place: Michigan

Name of the place: Paris

Name of the place: London

MICHIGAN,LONDON

Q7. WAP that creates a function lenWords(STRING) that takes string as an argument and returns a
tuple containing the length of each word of a string.

Sample input: ‘come let us have some fun’

Output: (4,3,2,4,4,3)

INPUT

def lenWords(STRING):

l=STRING.split()

t=()

for i in l:

t+=(len(i),)

return t

a=input('Write the sentence: ')

print(lenWords(a))
OUTPUT

Write the sentence: come let us have some fun

(4, 3, 2, 4, 4, 3)

Q8. WAP that creates a user defined function RetScholars(D) that accepts a dictionary D containing
Name as key and marks as value of 5 students. Then, the function should create a new dictionary
of students scoring 90 or more marks and return it.

If D contains {‘Anu’:68,’Raj’:75,’Jai’:92,’Om’:93,’Sam’:90}

Then the new dictionary returned should be {‘Jai’:92,’Om’:93,’Sam’:90}

Ans.

INPUT

def RetScholars(D):

scholars={}

for i in D:

if D[i]>=90:

scholars[i]=D[i]

return scholars

d={}

for i in range(5):

name=input('Name of the student: ')

marks=int(input('Marks: '))

d[name]=marks

print(RetScholars(d))

OUTPUT

Name of the student: Om

Marks: 68

Name of the student: Jai

Marks: 89

Name of the student: Jagdish

Marks: 98

Name of the student: Hare


Marks: 92

{'Jagdish': 98, 'Hare': 92}

You might also like