M110-Final Exam Review

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

M110

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

for i in range(1, rows+1):


for j in range(1, i+1):
print(number, end=" ")
number += 1
print()
Enter number of rows: 4
1
23
456
7 8 9 10
What is the output of the below code?
n=5
for i in range(n,0,-1):
for j in range(i):
print('x ', end="")
print('')

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.

num= int(input('Enter a number: '))


count = 0
while num != 0:
num //= 10
count+= 1
print("Total digits are: ", count)

Enter a number: 651215


Total digits are: 6
Write a Python code to find and display the factorial of any entered number
The factorial (symbol: !) means to multiply all whole numbers from our chosen number
down to 1.
For example: to calculate the factorial of 5: 5! = 5 × 4 × 3 × 2 × 1 = 120
N.B: The program should check the following cases as well:
- if the number is less than zero, then the message "Factorial does not exist for
negative numbers" should be displayed
- if the number is zero, then the factorial is 1.
num = eval(input('Enter a number: '))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers") Enter a number: 5
elif num == 0:
The factorial of 5 is 120
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial = factorial * i
print("The factorial of", num, "is", factorial)
Write a python program using while loop(s) to print the below Table.

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]]

# iterate through rows


for i in range(len(X)): [17, 15, 4]
# iterate through columns [10, 12, 9]
for j in range(len(X[0])): [11, 13,
result[i][j] = X[i][j] + Y[i][j] 18]

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

numbers= [1, 2, 3, 4, 5, 6, 7, 8, 9] # Declaring


the list
count1,count2= countThem(numbers)
print("Number of even numbers :",count2)
print("Number of odd numbers :",count1)
Write a python program to find the factors of a number using a
function, which computes the factor of the argument passed and
prints the result in a proper message.

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)

The result of the factors function for 16


is: 1 2 4 8 16
You are requested to write a program that does the following:
- prompts the user to enter the name of the file that will be used.
- displays only the first ten lines of the file’s contents. If the file contains less than ten lines, it
should display the file’s entire contents.
- The program should raise the proper exception if the entered file does not exist.
def main():
line = ''
counter = 0
fileName = input('Enter the name of
the file: ')
try:
#infile = open(fileName, 'r')
line = infile.readline()
counter = 1
# Read in and display first ten
lines
while line != '' and counter <= 10:
# Strip '\n'
line = line.rstrip('\n')
print(line)
line = infile.readline()
# Update counter when line is
read
counter +=1
# Close file
infile.close()
except IOError:
print('An error occurred trying to
read the file')
Student class

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()

You might also like