0% found this document useful (0 votes)
61 views

Lab Task 7: Programming Exercises

The document contains 5 programming questions related to Python exercises involving lists, tuples, strings, and basic operations. Question 1 asks to use min and max functions to find minimum and maximum values from tuples in a list based on element position. Question 2 checks if dart coordinates hit within a dartboard radius. Question 3 provides Python expressions for comparing word lengths and checking dictionary order and letter presence. Question 4 asks to take user input to fill a tuple with province names. Question 5 demonstrates list and tuple operations like insertion, appending, popping, removal and reversing while completing the same tasks on both a list and tuple.

Uploaded by

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

Lab Task 7: Programming Exercises

The document contains 5 programming questions related to Python exercises involving lists, tuples, strings, and basic operations. Question 1 asks to use min and max functions to find minimum and maximum values from tuples in a list based on element position. Question 2 checks if dart coordinates hit within a dartboard radius. Question 3 provides Python expressions for comparing word lengths and checking dictionary order and letter presence. Question 4 asks to take user input to fill a tuple with province names. Question 5 demonstrates list and tuple operations like insertion, appending, popping, removal and reversing while completing the same tasks on both a list and tuple.

Uploaded by

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

LAB TASK 7

PROGRAMMING EXERCISES:
Question 1.
Use inbuilt min and max functions to perform the task of getting the minimum and maximum value of in a
list of tuples for a particular element position in a tuple.
Sample = [(2, 3), (4, 7), (8, 11), (3, 6)]

Program:
lst = [(2, 3), (4, 7), (8, 11), (3, 6)]
a=[]
b=[]
for i in lst:
a.append(min(i))
b.append(max(i))
for x in range(len(a)):
print ("Tuple #",x+1)
print("MINIMUN =",a[x],"\nMAXIMUM =",b[x])
print()

Result:

Question 2
A dartboard of radius 10 and the wall it is hanging on are represented using the two dimensional
coordinate system, with the board’s center at coordinate (0, 0). Variables x and y store the x- and
y-coordinate of a dart hit. Write an expression using variables x and y that evaluates to True if the
dart hits (is within) the dartboard, and evaluate the expression for these dart coordinates:
(a) (0, 0)
(b) (10, 10)
(c) (6, 6)
(d) (7, 8)
Program:
x=[]
y=[]
for i in range(4):
print("For HIT #",i+1)
x.append(int(input("Enter x-coordinate: ")))
y.append(int(input("Enter y-coordinate: ")))
if (x[i])**2 + (y[i])**2 <= 100:
print("\nYOU HIT IT")
else:
print("\nYOU MISSED")
print()

Result:

Question 3.
Write Python expressions corresponding to these statements:
(a)The number of characters in the word "anachronistically" is 1 more than the number of characters
in the word "counterintuitive".
(b)The word "misinterpretation" appears earlier in the dictionary than the word "misrepresentation".
(c)The letter "e" does not appear in the word "floccinaucinihilipilification".
(d)The number of characters in the word "counterrevolution" is equal to the sum of the number of
characters in words "counter" and "resolution."
Program:
a) wrd1="anachronistically"
wrd2="counterintuitive"
a=len(wrd1)
b=len(wrd2)
print("The number of characters in the word",wrd1,"is",a-b,"more than the number
of characters in the word",wrd2)
b) a="misinterpretation"
b="misrepresentation"
if (a<b):
print(a,"appears earlier in dictionary than",b)

c) wrd="floccinaucinihilipilification"
if "e" in wrd:
print("Yes! e is present")
else:
print("No! e is not present")

d) wrd="counterrevolution"
wrd1="counter"
wrd2="resolution"
a=len(wrd)
b=len(wrd1)+len(wrd2)
if a==b:
print("The number of characters in the word ",wrd," is equal to the sum of
the number of characters in words ",wrd1," and ",wrd2)

Result:
a) The number of characters in the word anachronistically is 1 more than the number
of characters in the word counterintuitive.
b) misinterpretation appears earlier in dictionary than misrepresentation.
c) No! e is not present.
d) The number of characters in the word counterrevolution is equal to the sum of
the number of characters in words counter and resolution.

Question 4.
Write a program in Python that holds an empty tuple and fill that tuple after taking user
input for names of provinces of Pakistan n fill an empty tuple and print.

Program:
sample=()
x=list(sample)
for i in range(5):
x.append(input("Enter province name: "))
sample=tuple(x)
print(f"The tuple consisting of provinces is {sample}")
Result:

Question 5. Start by assigning to variables monthsL and monthsT a list and a tuple,
respectively, both containing strings 'Jan', 'Feb', 'Mar', and 'May', in that order. Then attempt
the following with both containers:
(a)Insert string 'Apr' between 'Mar' and 'May'.
(b)Append string 'Jun'.
(c)Pop the container.
(d)Remove the second item in the container.
(e)Reverse the order of items in the container.
Note: when attempting these on tuple monthsT you.

Program:
monthL=["Jan","Feb","Mar","May"]
monthT=("Jan","Feb","Mar","May")

monthL.insert(3,"Apr")
print(monthL)
monthT=monthT[:3]+("Apr",)+monthT[3:4]
print(monthT)

monthL.append("Jun")
print(monthL)
monthT=monthT.__add__(("Jun",))
print(monthT)

monthL.pop()
print(monthL)
monthT=monthT[:5]
print(monthT)

monthL.pop(1)
print(monthL)
monthT=monthT[0:1]+monthT[2:6]
print(monthT)

monthL.reverse()
print(monthL)
monthT=monthT[::-1]
print(monthT)
Result:
['Jan', 'Feb', 'Mar', 'Apr', 'May']
('Jan', 'Feb', 'Mar', 'Apr', 'May')
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun')
['Jan', 'Feb', 'Mar', 'Apr', 'May']
('Jan', 'Feb', 'Mar', 'Apr', 'May')
['Jan', 'Mar', 'Apr', 'May']
('Jan', 'Mar', 'Apr', 'May')
['May', 'Apr', 'Mar', 'Jan']
('May', 'Apr', 'Mar', 'Jan')

You might also like