Python Course
Python Course
26/08/2024
def multiplicationTable(n):
table = [0]*12
mul = 1
for i in range(0, len(table)): Multiplication
Table
table[i] = n*mul
mul = mul + 1
(Better Version)
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:
def subStringExtractor(inputString, startPosition, length):
if startPosition < 0 or startPosition > length:
return "Invalid Start Position"
Substring
Function
if length <= 0:
return "Invalid Length"
result = ""
(basic)
while startPosition < length:
result = result + inputString[startPosition]
print(result)
startPosition = startPosition + 1
return result
result = ""
while startPosition < length:
result = result + inputString[startPosition]
print(result)
startPosition = startPosition + 1
return result
Swapping Values
1. A = 10 A
2. B = 15
A=B B
3. temp = A
4. A = B temp
5. B = temp
How swapping works…
counter 0
j 0
marks[0] > marks[1]
marks 20 18
marks[1] > marks[2]
18 16
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 How the bubble while j < len(marks)-count-1:
sorting program works if marks[j] > marks[j+1]:
[16, 14, 12, 10]
temp = marks[j]
14, 12, 10, 16, 18, 20
marks[j] = marks[j+1]
[14, 12, 10] marks[j+1] = temp
12, 10, 14, 16, 18, 20
j = j + 1
[12, 10] count = count + 1
10, 12, 14, 16, 18, 20
print(marks)
[10]
10, 12, 14, 16, 18, 20
PYTHON CODE
(descending)
marks = [10, 12, 14, 16, 18, 20]
count = 0
[10, 12, 14, 16, 18, 20]
12, 14, 16, 18, 20, 10 while count < len(marks):
j = 0
[12, 14, 16, 18, 20]
How the bubble while j < len(marks)-count-1:
14, 16, 18, 20, 12, 10
sorting program works if marks[j] < marks[j+1]:
[14, 16, 18, 20] temp = marks[j]
16, 18, 20, 14, 12, 10
marks[j] = marks[j+1]
[20]
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.