M110-Final Exam Review
M110-Final Exam Review
M110-Final Exam Review
Python
Programming
Final Exam Review
Prepared by: Dr. Ahmad Mikati
Write a python program that prompts the user to enter the GPA and
displays a message showing the letter GPA and whether this student
is accepted in the MSc. Program based on the following criteria.
If the student has a GPA >=3.0, then he/she will be admitted.
If the student has a GPA>2.5 , then he/she will be admitted with
probation.
Otherwise, he/she will not be allowed to join the program.
Write a python program that accepts a string from the user
and prints it in reverse order with an asterisk after each
character.
Example: if the entered string is STAR, then the output will
be R*A*T*S*
word =input("Enter a string: ")
for char in range(len(word) - 1, -1, -1):
print(word[char], end="*")
Enter a string:
STAR
R*A*T*S*
What is the output of the below code if the entered
number of rows is 4?
rows = int(input("Enter number of
rows: "))
number = 1
x xxxx
x xxx
x xx
x x
x
What is the output of the below code?
print("Printing certain values sum in a given
range")
num=6
previousNum=0
for i in range(num):
Sum = previousNum + i
print(Sum, end="\t")
previousNum = i
print('\nThe value of i is now:',i)
Printing certain values sum in a given
range
0 1 3 5 7 9
The value of i is now: 5
Write a Python program that prompts the user to input a number.
The program should count and display the total number of digits
in that number.
1 2 3 4 5
6 7 8 9 10
i=1
2 4 6 8 10
12 14 16 18 20 while i<=10:
3 6 9 12 15 j=1
18 21 24 27 30 while j<=10:
4 8 12 16 20 print (i*j, end='\t')
24 28 32 36 40 j+=1
5 10 15 20 25 i+=1
30 35 40 45 50 print('')
6 12 18 24 30
36 42 48 54 60
7 14 21 28 35
42 49 56 63 70
8 16 24 32 40
48 56 64 72 80
9 18 27 36 45
54 63 72 81 90
10 20 30 40 50
What is the output of each of the following
programs?
i=0 i=0
while i<=5: while i<=5:
i+=1 i+=1
if i==2: if i==4:
continue break
else: else:
print(i, print(i, end="
end=" ") ")
else: else:
print("\ print("\nDone!")
nDone!")
1235 123
6
Done!
Write Python programs that display the following
shapes:
* size=5
** for line in range(1,size+1):
*** for asterisk in range(1,
**** line+1):
***** print('*', end='')
print()
* size=5
** for line in
*** range(1,size+1):
**** for asterisk in range(1,
***** line+1):
**** print('*', end='')
*** print()
** for j in range(size-1,0,-1):
* for k in range(1,j+1):
print('*', end='')
print()
Write a Python program to convert a list of multiple integers into a
single number. Check your result by adding 2 to the final expected
number.
l=[12,13,14]
x=''
for i in range(3):
x=x+str(l[i])
x=int(x)
print(x)
print(x+2)
121314
121316
Write a python program to add the below two matrices using nested
loop and display the result matrix.
X= Y = [[5,8,1],
[[12,7,3], [6,7,3],
[4,5,9]]
[4 ,5,6],
X = [[12,7,3],[7[4,8,9]]
,5,6], [7 ,8,9]]
Y = [[5,8,1], [6,7,3], [4,5,9]]
for r in result:
print(r)
Write Python programs that generates integers from 1 to 30 included
and places the integers which are multiples of 2 in a list, integers which
are multiples of 7 but not multiples of 2 at the same time (e.g.: 14) in
another list, and the remaining integers should be placed in the third
list.
TheL2=[]
program should print at the end these three lists
L7=[]
Lrest=[]
for x in range(1, 31):
if (x%2==0): The multiples of 2 are: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
L2.append(x) The multiples of 7 [but not multiples of 2] are: [7, 21]
elif (x%7==0): The remaining elements are: [1, 3, 5, 9, 11, 13, 15, 17, 19, 23, 25, 27, 29]
L7.append(x)
else:
Lrest.append(x)
print ('The multiples of 2 are:',L2)
print ('The multiples of 7 [but not multiples of 2] are:',L7)
print ('The remaining elements are:',Lrest)
What is the output of each of the following
program?
var1=5
def yess():
global var1
var1=3 5
print(var1) 3
30
print(var1)
yess()
var1*=10
print(var1)
Write a complete python program that includes a function that
takes a list as an argument and returns the counts of odd and even
numbers. The program should print the counts in a proper
message. You may use the below list to test your code.
numbers= [1, 2, 3, 4, 5, 6, 7, 8, 9]
def countThem(list1):
count_odd = 0
count_even = 0
for x in list1:
if x % 2==0:
count_even+=1 Number of even
else: numbers : 4
count_odd+=1 Number of odd
return count_odd, count_even numbers : 5
def factors(x):
print("The result of the factors function
for",x,"is:",end=" ")
for i in range(1, x + 1):
if x % i == 0:
print(i,end=" ")
num = 16
factors(num)
1. Write a Student class in Python with name, grade1, and grade2 attributes.
2. Create a constructor with parameters: name, grade1, and grade2
3. Create a Sum() method to calculate and returns the sum of the grades.
4. Create an Average() method uses the Sum() method to calculate and return the
average of the grades.
5. Create a method displayResults() that displays the sum, average, and whether
the student has passed or failed of an object created using an instantiation on
Student class.
N.B: No need to validate the entered grades. The passing grade is 50.
class Student:
def __init__(self,name, grade1, grade2):
self.name=name
self.g1=grade1
self.g2=grade2
def Sum(self):
s=self.g1+self.g2
return s
def average(self):
avrg=self.Sum()/2
return avrg
def displayResults(self):
print("The name and sum of grade1 and grade2 are:
",self.name,",",self.Sum())
print("The name and average of grade1 and grade2 are:
",self.name,",",self.average())
if avrg>=50:
print("Pass")
else:
print("Fail")
s1=Student("Taha", 63,36)
s1.displayResults()
You are requested to write a python program that does the following:
a. Create Computation class with a default constructor (without parameters) allowing to perform
various calculations on integers numbers.
b. Create a method called Factorial() in the above class which allows to calculate the factorial of an
integer, n.
c. Create a method called Sum() in the above class allowing to calculate the sum of the first n
integers 1 + 2 + 3 + .. + n.
d. Instantiate the class, prompt the user to enter an integer, and write the necessary statements to
test the above methods.
class Computation:
def __init__ (self):
self.n=0
# --- Factorial ------------
def factorial(self, n):
j=1
for i in range (1, n + 1):
j=j*i
return j
Enter an integer: 5
# --- Sum of the first n numbers ---- The factorial of the number 5
def sum (self, n): is: 120
j=0 The sum from the number 5
for i in range (1, n + 1): to 1 is: 15
j=j+i
return j
x= Computation()
n=int(input("Enter an integer: "))
print("The factorial of the number",n,
"is:",x.factorial(n))
print("The sum from the number",n, " to 1
is:",x.sum(n))
Write a python program that displays a title and a label with text as
shown in the below figure.
import tkinter
class MyGUI:
def __init__(self):
# Create the main window widget.
self.main_window = tkinter.Tk()
# Display a title.
self.main_window.title('Testing GUI')
# Create a Label widget containing the text.
self.label =
tkinter.Label(self.main_window,text='Hello M110
Students!')
# Call the Label widget's pack method.
self.label.pack()
# Enter the tkinter main loop.
tkinter.mainloop()
# Create an instance of the MyGUI class.
my_gui = MyGUI()