Python Course
26/08/2024
def multiplicationTable(n):
table = [0]*12
mul = 1
for i in range(0, len(table)):
table[i] = n*mul
mul = mul + 1
return table
def displayMultable(array):
for i in range(0, len(array)):
print(array[i])
flag = True
while flag:
k = int(input("Please enter the multiplication table you need: "))
if k >= 1:
returnArray = multiplicationTable(k)
displayMultable(returnArray)
flag = False
else:
print("Please enter a positive number")
Multiplication
Table
(Better Version)
def subStringExtractor(inputString, startPosition, length):
if startPosition < 0 or startPosition > length:
return "Invalid Start Position"
if length <= 0:
return "Invalid Length"
result = ""
while startPosition < length:
result = result + inputString[startPosition]
print(result)
startPosition = startPosition + 1
return result
extractedString = subStringExtractor("Harry Potter", 0, 6)
print(extractedString)
Substring
Function
(basic)
def subStringExtractor(inputString, startPosition, length):
if inputString == "" or inputString.isspace():
return "Sorry your input string is empty!"
if startPosition < 0 or startPosition > length:
return "Invalid Start Position"
if length <= 0:
return "Invalid Length"
result = ""
while startPosition < length:
result = result + inputString[startPosition]
print(result)
startPosition = startPosition + 1
return result
extractedString = subStringExtractor("Harry Potter", 0, 6)
print(extractedString)
Substring
Function
(better)
Bubble Sort Algorithm
A simple algorithm used for taking a list of unordered numbered (unsorted) and
putting them into a correct order. (Ascending or descending)
Swapping Values
1. A = 10
2. B = 15
A=B
3. temp = A
4. A = B
5. B = temp
A
B
temp
How swapping works…
counter
0
j
0
marks[0] > marks[1]
20
18
marks[1] > marks[2]
18
16
marks
temp
0
PYTHON CODE
(ascending)
marks = [20, 18, 16, 14, 12, 10]
count = 0
[20, 18, 16, 14, 12, 10]
18, 16, 14, 12, 10, 20
while count < len(marks):
j = 0
[18, 16, 14, 12, 10]
16, 14, 12, 10, 18, 20
while j < len(marks)-count-1:
if marks[j] > marks[j+1]:
[16, 14, 12, 10]
14, 12, 10, 16, 18, 20
temp = marks[j]
marks[j] = marks[j+1]
[14, 12, 10]
12, 10, 14, 16, 18, 20
marks[j+1] = temp
j = j + 1
[12, 10]
10, 12, 14, 16, 18, 20
[10]
10, 12, 14, 16, 18, 20
count = count + 1
print(marks)
PYTHON CODE
(descending)
[10, 12, 14, 16, 18, 20]
12, 14, 16, 18, 20, 10
[12, 14, 16, 18, 20]
14, 16, 18, 20, 12, 10
[14, 16, 18, 20]
16, 18, 20, 14, 12, 10
marks = [10, 12, 14, 16, 18, 20]
count = 0
while count < len(marks):
j = 0
while j < len(marks)-count-1:
if marks[j] < marks[j+1]:
temp = marks[j]
marks[j] = marks[j+1]
[16, 18, 20]
18, 20, 16, 14, 12, 10
marks[j+1] = temp
j = j + 1
[18, 20]
20, 18, 16, 14, 12, 10
count = count + 1
print(marks)
[20]
20, 18, 16, 14, 12, 10
Question 1
Get the last name of a user and count how many number of times the letter “a” is
repeated.
For the above given program whether it is “A” or “a” it should do the counting.
Counting number of Spaces
How to ignore a certain code in your program?
Using ‘’’ at the start and ‘’’ at the end
• Here MAXATTEMPTS is a “constant”
• After your assign a value for a constant, it won’t be
changed.
• Always constants should be in caps when being used in
python.