Python AAU Final Exam Answer
Python AAU Final Exam Answer
Part one
Error finding (10 pts.)
1. number = input(“please enter any number: ”) # the number can not be
interpreted as an integer.
2. For i in range(1,number):
3. if (number % i ==0):
4. sum += 1 # unsupported operand types(s)
5. if (sum == number):
6. print(number , ”is a perfect number”)
7. else:
8. print(number ,”is not a perfect number”)
by Neway.M 1 06/07/2024
Answer
by Neway.M 2 06/07/2024
Question 2
for (i in range(11.5)):
print(i, end=‘ ’)
by Neway.M 3 06/07/2024
Line Error type solution
error
1 Syntax error for i in range float(11.5)
by Neway.M 4 06/07/2024
Question 3
i +=2
while(i <== 9): # assignment operator syntax error
print(‘the value of i is:’)
Print(i+=1)
by Neway.M 5 06/07/2024
Answer
by Neway.M 6 06/07/2024
Error type Description
by Neway.M 7 06/07/2024
Common error finding method in python
1) Forgetting to put a :
by Neway.M 9 06/07/2024
3) Using the wrong amount of indentation.
by Neway.M 10 06/07/2024
4) Forgetting the len() call in a for loop statement.
(Causes “TypeError: 'list' object cannot be interpreted
as an integer”)
Commonly you want to iterate over the indexes of
items in a list or string, which requires calling
the range() function.
Just remember to pass the return value of Len(some
List), instead of passing just some List.
by Neway.M 11 06/07/2024
5) Trying to modify a string value.
by Neway.M 12 06/07/2024
6) Trying to concatenate a non-string value to a string
value.
(Causes “Type Error: Can't convert 'int' object to str
implicitly”)
by Neway.M 13 06/07/2024
7) Forgetting a quote to begin or end a string value.
by Neway.M 15 06/07/2024
9) A typo for a method name.
by Neway.M 16 06/07/2024
10) Going past the last index of a list.
by Neway.M 17 06/07/2024
11) Using a non-existent dictionary key.
by Neway.M 18 06/07/2024
12) Trying to use a Python keyword for a variable name.
(Causes “Syntax Error: invalid syntax”)
by Neway.M 19 06/07/2024
13) Using an augmented assignment operator on a new
variable.
(Causes “Name Error: name 'foobar' is not defined”)
Do not assume that variables start off with a value such
as 0 or the blank string.
A statement with an augmented operator like spam +=
1 is equivalent to spam = spam + 1.
This means that there must be a value in spam to begin
with.
by Neway.M 20 06/07/2024
14) Using a local variable (with the same name as a global variable)
in a function before assigning the local variable.
(Causes “UnboundLocal Error: local variable 'foobar' referenced
before assignment”)
Using a local variable in a function that has the same name as a
global variable is tricky. The rule is: if a variable in a function is
ever assigned something, it is always a local variable when used
inside that function. Otherwise, it is the global variable inside that
function.
This means you cannot use it as a global variable in the function
before assigning it.
by Neway.M 21 06/07/2024
15) Trying to use range() to create a list of integers.
(Causes “Type Error: 'range' object does not support
item assignment”)
Sometimes you want a list of integer values in order,
so range() seems like a good way to generate this list.
However, you must remember that range() returns a
"range object", and not an actual list value.
by Neway.M 22 06/07/2024
16) There is no ++ increment or –- decrement operator.
(Causes “SyntaxError: invalid syntax”)
by Neway.M 23 06/07/2024
by Neway.M 24 06/07/2024
Part two
Code output(10pt.)
4. row = 6
String = “python”
for i in range(row,0,-1):
j= 0
for k in string:
if(j == i):
break
print(j,end=k)
j += 1
print(“ ”)
by Neway.M 25 06/07/2024
Question 5
def output(X(i)):
i += 1
print(i)
i = 19
for i in range(10):
output(X(i))
i+=5
output(X(i))
by Neway.M 26 06/07/2024
Part3
Completion(14pt.)
Fibonacci series
The first number start with 0 and the second number start with1
First,second=0,1
n= t-in---------------
Print(“Fibonacci series are: ”)
For i in---range(n):-------------------
if i <= 1:
result = I
else:
result =first + second----------------------
first =---second---------------------
second=-result-----------------
print(result)
by Neway.M 27 06/07/2024
Fibonacci Series In Python
Fibonacci series in python is a sequence of numbers in
which the current term is the sum of the previous two
terms.
The first two terms of the Fibonacci sequence are 0 and 1.
So what is the logic behind this?
Let’s understand it in detail.
by Neway.M 28 06/07/2024
The logic behind Fibonacci sequence in python
by Neway.M 29 06/07/2024
Python Five method to
Fibonacci series
Table of Contents
1. Fibonacci series in python using for loop
2. Fibonacci series in python using while loop
3. Fibonacci series using recursion in python
4. Printing Nth term of Fibonacci series using recursion
5. Printing Fibonacci series dynamically
by Neway.M 30 06/07/2024
1. Fibonacci series in python using for loop
Accept the number of terms from the user and pass that number in the loop and
implement the Fibonacci number’s logic in the loop.
n = int(input("Enter number of terms: "))
n1, n2 = 0, 1
i=0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to",n,":")
print(n1)
else:
print("Fibonacci sequence:")
for i in range(n):
print(n1)
sum = n1 + n2
n1 = n2
n2 = sum
i += 1
by Neway.M 31 06/07/2024
Fibonacci series in python using while loop
Take a number of terms of the Fibonacci series as input from the user and iterate while loop with the
logic of the Fibonacci series.
n = int(input("Enter number of terms: "))
n1, n2 = 0, 1
i=0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while (i <n):
print(n1)
sum = n1 + n2
n1 = n2
n2 = sum
i += 1
by Neway.M 32 06/07/2024
3.Fibonacci series using recursion in python
Same logic as above, accept a number from the user and pass it to
the function.
The function will return the value and outside we print it.
def Fibonacci(n): #fibinacci function
if n < 0:
print("Incorrect input")
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n - 1) + Fibonacci(n - 2) #Logic for the fibonacci series
n = int(input("Enter number of terms: "))
i=0
for i in range(n):
print(Fibonacci(i))
by Neway.M 33 06/07/2024
4.Printing Nth term of Fibonacci series using recursion
digit = 0
result = 0
number = 370
number1 = number
temp = number
#digit counting code
while number != 0:
number =--number//10--------
#the sum of each digit powered to the digit count
While-number1!=0------------:
number=----------------------
result=--------------------------
number1 = number1//10
If------------:
print(“number is Armstrong”)
else:
Print(“number is not Armstrong”)
by Neway.M 36 06/07/2024
Python Program to Check Armstrong Number
by Neway.M 37 06/07/2024
Concept of Armstrong number
# initialize sum
sum = 0
num = 1634
# initialize sum
sum = 0
by Neway.M 42 06/07/2024
Answer
python digits counting code
by Neway.M 43 06/07/2024
Sum of each digit powered to number
of digit
by Neway.M 44 06/07/2024
Question8
calculate number of occurrence of a given character from given string
by Neway.M 45 06/07/2024
Python Program to Count Occurrence of a Character in a String
by Neway.M 46 06/07/2024
Cont.…
by Neway.M 47 06/07/2024
Python Program to Count Occurrence of a Character method 2
by Neway.M 48 06/07/2024
Python Program to Count Total Occurrence of a Character method
3
by Neway.M 52 06/07/2024
steps
by Neway.M 53 06/07/2024
2.Use the Python Reversed Function to Check if a String is a Palindrome
by Neway.M 54 06/07/2024
Cont..
by Neway.M 55 06/07/2024
3.Using a For Loop to Check if a Python String is a Palindrome
by Neway.M 56 06/07/2024
Cont..
by Neway.M 57 06/07/2024
4.Using a Python While Loop to Check if a String is a Palindrome
In this section, let’s explore how to use a Python while loop to see if a
string is a palindrome or not.
One of the benefits of this approach is that we don’t actually need to
reassign a reversed string, which, if your strings are large, won’t
consume much memory.
a_string = 'Was it a car or a cat I saw'
def palindrome(string):
string = string.lower().replace(' ', '')
first, last = 0, len(string) - 1
while(first < last):
if(string[first] == string[last]):
first += 1
last -= 1
else:
return False
return True
print(palindrome(a_string))
by Neway.M 58 06/07/2024
Cont..
by Neway.M 59 06/07/2024
5.Check if a Number is a Python Palindrome