0% found this document useful (0 votes)
5 views30 pages

MTA Revision v1

The document contains a series of Python programming tasks and their expected outputs, covering various topics such as list manipulation, string processing, and control flow. It includes code snippets, expected results, and explanations for each task. Additionally, it discusses error correction in code and provides pseudocode and flowchart specifications for certain problems.

Uploaded by

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

MTA Revision v1

The document contains a series of Python programming tasks and their expected outputs, covering various topics such as list manipulation, string processing, and control flow. It includes code snippets, expected results, and explanations for each task. Additionally, it discusses error correction in code and provides pseudocode and flowchart specifications for certain problems.

Uploaded by

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

MTA Revision

Spring 23-24
Fall 23-24
Spring 22-23 Makeup
Summer 22-23 Makeup
MCQ
1- c

2- c

3- c

4- a

5- c
1- d

2- b

3- c

4- a

5- b
1- b

2- b

3- d

4- c

5- c
1- b

2- c

3- c

4- a

5- a
What is the output?
What will be shown on the screen when you execute the code?

x='5'+'6'
w=float(x[1])
y=25 6.0
z=int(x+'3') 56
if x=='11' or y==25: 25
568
z=z+5
else:
y= str(y)+x
print(w)
print(x)
print(y)
print(z)
x= "I have 100 reasons to quit!"
y=5 100
z=int( x[6:9]) 155155
w="15"+"5" 155
v=w Don't quit!
print(z**2) 10
print(w*2)
if v=='20':
z=z+5
else:
x="Don't quit!"
print(v)
print(x)
print(z)
Given the Python program below:
What is the expected output?
What does the u_items list represent?

a = [10,20,30,40,10,30,50,40]
d_items = []
u_items = [] The output:
for x in a: [20, 50]
if a.count(x)<=1: [10, 30, 40]
u_items.append(x) It represents the list of unique elements
elif x not in d_items: that are not repeated in the list a.
d_items.append(x)
print(u_items)
print(d_items)
Given the following python code. Write output?

L2=[]
L57=[]
L2: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Lrest=[]
L57: [5, 7, 15]
for x in range(1, 21):
if (x%2!=1): Lrest: [1, 3, 9, 11, 13, 17, 19]
L2.append(x)
elif (x%7==0 or x%5 ==0):
L57.append(x)
else:
Lrest.append(x)
print ('L2:',L2)
print ('L57:',L57)
print ('Lrest:',Lrest)
Given the following python code. What will be the output if the user inputs “Anna”

word = input("Input a word: ")


pal=''
for char in range(len(word) - 1, -1, -1):
pal+=word[char] Input a word: Anna
print(pal) annA
word_list=[] ['a', 'n', 'n', 'A']
for char in range(len(word)): The word that you have entered is not a palindrome.
word_list.append(word[char])
word_list.reverse()
print(word_list)
if pal==word:
print('The word that you have
entered is a palindrome')
else:
print('The word that you have
entered is not a palindrome.')
Write python code?
Given a list of grades, write a Python program that calculates the average grade, finds the
highest grade, and prints the results.
grades = [25, 63, 75, 64, 72, 56, 90, 81, 76, 43, 60, 71]

Another Solution
grades = [25, 63, 75, 64, 72, 56, 90, 81, 76, 43, 60, 71]
total_grade = 0
for grade in grades: grades = [25, 63, 75, 64, 72, 56, 90, 81, 76,
total_grade += grade 43,60,71]
average_grade = round(total_grade / len(grades),2)
avgg = sum(grades)/len(grades)
highest_grade = grades[0] maximum = max(grades)
for grade in grades: print( round(avgg ,2))
if grade > highest_grade: print(format(maximum,'.2f'))
highest_grade = grade

print("Average grade:", average_grade)


print("Highest grade achieved:", highest_grade)
Write a Python program that should:
a- Accept the user's input for the number of integers that should be added to the new list,
then prompts for each integer, and adds it to this list (using a loop).
b-Categorize the numbers in this list into two separate lists: one for even numbers and
another for odd numbers.
c- Finally, display the largest even and odd numbers from their respective lists.
Hint: You can use the sort() method in the third part.
n=int(input("Enter the number of items to be in the list: "))
The expected output: list1=[]
Enter the number of items to be in the list: 5 for i in range(n):
Enter an integer: 20 num=int(input("Enter an integer: "))
Enter an integer: 25 list1.append(num)
Enter an integer: 50 even=[]
Enter an integer: 14 odd=[]
Enter an integer: 63 for i in list1:
Largest even number: 50 if(i%2==0):
Largest odd number: 63 even.append(i)
else:
odd.append(i)
even.sort()
odd.sort()
print("Largest even number:",even[len(even)-1])
print("Largest odd number:",odd[len(odd)-1])
You are given a tuple of integers. Write a Python program to create a new list. This new list
should contain the same elements in the given tuple, but with all odd numbers doubled, and
display at the end the original Tuple and the new list.
In your code, you should use one loop and one if statement.
input_tuple = (1, 2, 3, 4, 5, 6)
Expected output:
Original Tuple: (1, 2, 3, 4, 5, 6)
New List: [2, 2, 6, 4, 10, 6]
input_tuple = (1, 2, 3, 4, 5, 6)
new_list = []

for num in input_tuple:


if num % 2 == 1:
new_list.append(num * 2)
else:
new_list.append(num)

print("Original Tuple:", input_tuple)


print("New List:", new_list)
Given the list below, write a python code that adds an element (6, for example) to the list,
without using append.
my-list=[1, 2, 3, 4, 5]

my_list = [1, 2, 3, 4, 5]
new_element = 6
my_list+=[new_element]
print(my_list)
Write a program that takes in a list of names (a loop that prompts the user to enter a name, s or S to
stop) and removes all duplicates, then prints the resulting list. Hint: Use pretest check.

Another Solution
# Initialize an empty list to store the names
names_list = []
names_list = []
result_list =[]
# Prompt the user to enter names until 's' or 'S' is entered
name = input("Enter a name (or 's' to stop): ")
name = input("Enter a name (or 's' to stop): ")
while name.lower() != 's':
while name.lower() != 's':
names_list.append(name)
names_list.append(name)
name = input("Enter a name (or 's' to stop): ")
if result_list.count(name) == 0:
result_list.append(name)
# Remove duplicates from the list of names
name = input("Enter a name (or 's' to stop): ")
result_list = list(set(names_list))
print("The list without duplicates is: ", result_list)
# Print the resulting list
print("The list without duplicates is: ", result_list)
Write a program that calculates and prints the sum and the product of all the even numbers
between 1 and 12, included.
In your code, you should use one while loop and one if statement.

i=1
Sum = 0
product=1

while i <= 12:


if i % 2 == 0:
Sum+= i
product*=i
i=i+1

print("The sum is:",Sum)


print("The product is:", product)
Given below a list of numbers, write a python program to:
Find and display the first number in the list that is divisible by 5.
Find and display the largest number in the list.
Find and display the sum of the numbers in the list.
numbers = [2,4,12, 7, 8, 9, 15, 20] Note: You can use any built-in function.
The expected output:
The first number divisible by 5 is: 15
The largest number in the list is: 20
The sum of the numbers in the list is: 77

numbers = [2,4,12, 7, 8, 9, 15, 20]


found = False

for num in numbers:


if num % 5 == 0:
print(f"The first number divisible by 5 is: {num}")
found = True
break

print("The largest number in the list is:", max(numbers))


print("The sum of the numbers in the list is:", sum(numbers))
Write a Python Program that generates a list of tuples. Each tuple will include two elements:
the entered number and its calculated square. The list of tuples should be for the numbers
between 1 and 5 inclusive.
The output should look like the following:
[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
.
# Create an empty list to store the tuples
result = []

# Loop through the numbers 1 to 5 inclusive


for num in range(1, 6):
# Calculate the square of the number
square = num ** 2
# Create a tuple with the number and its square
tpl = (num, square)
# Append the tuple to the result list
result.append(tpl)

# Print the list of tuples


print(result)
Write a python program that takes in a string and forms a new string made of the first 2
characters and the last 2 characters from the given string. The program should check if the
length of the input string is less than 5 characters. If it is, the program prints an error
message. Otherwise, it proceeds to extract the first 2 characters of the input string and
extracts the last 2 characters of the input string.
Hint: Use Slicing.

# Get input string from user


input_str = input("Enter a string: ")

# Check if the input string has at least 5 characters


if len(input_str) < 5:
print("The string should have at least 5 characters.")
else:
# Get the first 2 characters of the input string
first_two = input_str[:2]
# Get the last 2 characters of the input string
last_two = input_str[-2:]
# Concatenate the first 2 characters and last 2 characters
new_str = first_two + last_two
# Print the new string
print(f"The new string is: {new_str}")
Write a program that takes in two lists of 5 integers (in one loop it should add the integers to
each of the two lists) and returns a set list containing only the elements that appear in both
lists. Another Solution
list1 = []
list1 = [] list2 = []
list2 = [] print("Enter 5 integers for List 1 and List 2:")
print("Enter 5 integers for List 1 and List 2:") for i in range(5):
for i in range(5): num1 = int(input("Enter an integer for List 1: "))
num1 = int(input("Enter an integer for List 1: ")) list1.append(num1)
list1.append(num1) num2 = int(input("Enter an integer for List 2: "))
num2 = int(input("Enter an integer for List 2: ")) list2.append(num2)
list2.append(num2)
# Find the intersection of the two lists using set intersection intersect_List =[]
intersect_set = set(list1).intersection(set(list2)) for i in list1:
if i in list1 and i in list2:
# Print the resulting set print(i)
print("The intersection of List 1 and List 2 is: ", intersect_set) intersect_List.append(i)

print(list1)
print(list2)
print(intersect_List)
Find the error
Given the following program that should count the words that start with either "a" or "A" among the three
entered words.
count = 0
for i in range(1,3):
word = input("Enter a word: ")
if word[0].upper == 'A':
count = i
print("The number of words that start with 'a' or 'A' is:" +count)

The expected output should be:


Enter a word: abir
Enter a word: Amal
Enter a word: fadi
The number of words that start with 'a' or 'A' is: 2

However, the given code is not as expected and also includes an error.
You are asked to check the errors in the above code and write the correct code underlining the corrected
ones.
count = 0
for i in range(3):
word = input("Enter a word: ")
if word[0].upper() == 'A':
count += 1
print("The number of words that start with 'a' or 'A' is:", count)
Write python code for the
given flowcharts?
A- Write a python program that implements it.
B- What is the output of this program?

base = 2
power = 4
product = base
counter = 1
while counter < power:
product *= base
counter += 1
print(product)

The output is: 16


Problem Specification: Suppose that you are asked to write a program that takes input for four Math exams (M1, M2, M3,
M4), calculates the average grade (av_grade), and then determines whether the student has passed or failed the course. If
the calculated average grade is less than 50, the program should display "FAIL"; otherwise, it should print "PASS"
a- Write a pseudocode for the above problem.
b- Draw the flowchart that depicts the needed flow of the program.

Step 1: Input M1,M2,M3,M4


Step 2: av_grade =(M1+M2+M3+M4)/4
Step 3: if (av_grade <50) then
print “FAIL”
else
print “PASS”
Write a python program that implements it.
Write a python program that implements the
flowchart.

You might also like