0% found this document useful (0 votes)
6 views13 pages

Python Course

The document includes Python code snippets for creating a multiplication table, extracting substrings, and implementing the bubble sort algorithm. It also discusses handling user input, counting occurrences of a character in a string, and the concept of constants in Python. The code examples demonstrate both ascending and descending sorting techniques, along with error handling for invalid inputs.

Uploaded by

gayuumaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Python Course

The document includes Python code snippets for creating a multiplication table, extracting substrings, and implementing the bubble sort algorithm. It also discusses handling user input, counting occurrences of a character in a string, and the concept of constants in Python. The code examples demonstrate both ascending and descending sorting techniques, along with error handling for invalid inputs.

Uploaded by

gayuumaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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

extractedString = subStringExtractor("Harry Potter", 0, 6)


print(extractedString)
def subStringExtractor(inputString, startPosition, length):
if inputString == "" or inputString.isspace():
return "Sorry your input string is empty!" Substring
if startPosition < 0 or startPosition > length:
Function
return "Invalid Start Position" (better)
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)
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 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]

[16, 18, 20] marks[j+1] = temp


18, 20, 16, 14, 12, 10 j = j + 1
count = count + 1
[18, 20]
20, 18, 16, 14, 12, 10 print(marks)

[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.

You might also like