Lab Task 7: Programming Exercises
Lab Task 7: Programming Exercises
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')