Week 2
Programming Languages
A programming language is a specifically structured language that developer used to write the algorithm, Programed algorithm is capable of being run by the computer, simply the way you read and do the sequence of an algorithm, now the computer will read and execute the steps of the algorithm. Some programming languages are Basic, Java, PHP, HTML, Python, and SQL.
Python
Python is a clear and powerful programming language. This language is freely available to download. Bug refers to a bad input; Python has strong features to help in debugging the codes. In this unit, we will learn how to code with Python and develop some algorithm to the python program. Remember not all the algorithm in this unit need to be programmed in Python as mentioned before, the main principle of this unit is to deliver the deliver structured thinking and problem-solving skills in the frame of the algorithm. For more information about the Python Visit Pythons website at below address:
Task:
You are required to install the Python on your own device for this course. You can download and install the Python from this link ‘ https://www.python.org/downloads/’ if you need more assistance there is a well-defined step by step structure with screenshots which could help you to install the Python on your device. Click on the following link for the structure.
http://www.howtogeek.com/197947/how-to-install-python-on-windows/
Furthermore, help with this process, check this video:
Task: Open Python and try your first line of command type:
File\ New File
Type: >>> print "Hello World!"
Save the file.
Press F5
Well done! You just wrote your first Python code
Loop
In some cases, you need to repeat a process in your algorithm for a specific time or a specific condition, let say print a name for ten times. What would you do? You may say you will copy and paste the code for ten times! There is a structure in the algorithm, which helps you to repeat a procedure for a specific time or condition.
A- Continuous Loop, these specialized structures for iterating for a specific number.
FOR iteration number
Sequence
ENDFOR
In the Python's editor type the following codes and check the output.
Activity:
For i in range (10): Print i
As you can see these loop prints its iteration value any time loop runs.
Activity:
Now we can advance our Loop code. We can add the value of loop iteration any times it runs.
Sum = 0
for in range (10)
sum = sum + i
print sum
Output:
0
1
3
6
10
15
21
28
36
45 -- > Final value of ‘Sum’
>>>
Tutorial:
Try to calculate the value of ‘i’ and ‘sum’ manually and compare your output with the code output. If you still confused and do not know how the value of ‘sum’ become to 45, try following codes which print the value of ‘i’ and ‘sum’ any time loop runs.
Sum = 0
for I in range (10)
sum = sum + i
the print I, ‘+’, ‘=’, sum
The output clearly shows the value of the ‘i’ and ‘sum’ in each calculation.
0 + 0 = 0
1 + 1 = 1
2 + 3 = 3
3 + 6 = 6
4 + 10 = 10
5 + 15 = 15
6 + 21 = 21
7 + 28 = 28
8 + 36 = 36
9 + 45 = 45
B- The WHILE loop, is used while a specific condition is valid in the begging of the LOOP
WHILE condition
Sequence
ENDWHILE
Tutorial:
Try the following code
Count = 0
While (count < 10)
print count
count = count + 1
Above code will run till amount of ‘count‘ is less than 10, so when value of ‘count’ reach the 10 conditions of the loop is not valid and loop would not run.
C- Boolean condition, if you need to check the logic or Boolean condition of a statement you can use IF-THEN-ELSE.
IF condition THEN
Sequence 1
Else
Sequence 2
ENDIF
Tutorial:
Type the following code.
i = int (input (‘Enter a number :’)
If i < 10:
print (“You have entered a number smaller than 10”)
else:
print (“You have entered a number bigger than 10”)
The above code will ask you to enter a number, then if the entered number is bigger or smaller than 10, prints a related message.
Tutorial:
Modify the code in activity 2, to be continuously repeated until the user enters number 0.
Data Types in Python
Main data types in Python:
-
Number, or integer, which is a whole number without any fraction.
E.g. Number1 = 4
-
String, a set of characters.
E.g. Name= “John”
-
List is contain a set of data, which separated with by comma.
E.g. list1 = [12, 2, 1, 0, ‘John’]
Array
The array is a list of the objects in a list and each object in an array is called an element of that array.
Define an array:
Number = [1, 2, 3, 4, 5]
Name = [‘One’, ‘Two’, ‘Tree’, ‘four’, ‘Five’]
To access array elements:
Array give an index to each element and starts from‘0’, here defined array ‘Name’ has five elements from 0, 1, 2, 3, and 4 so if you want to have access to the first element of the array ‘Name’ you have to use the index:
Print name [0], which ‘One’ will be the output of this command.
We can use the loop command to have access to the elements of an array.
name = [‘John’, ‘Peter’, ‘Mason’, ‘Paul’]
for i in range (4):
print name[i]
Output will be:
John
Peter
Mason
Paul
Find the summing amount of all the elements in the array. We have a given array:
Numbers = [2,4,5,4,8,6,100,5,3,4,4,65]
We want to design an algorithm, which finds the maximum element of the array and print it.
SET Sum to 0
FOR each element in the array
ADD element to Sum
ENDFOR
PRINT Sum
Tutorial:
Modify the above code to write a Python code to create an array and print the summing amount of all the elements.
Tutorial:
Next, write Python code to create an array and print the element with the highest value.
Nested Loop
A nested loop is a structure when a loop located in another loop, technically means, outer loops will run the inner loop as a sequence.
This is a sample code of a normal loop, as we learn previously.
for i in range (5):
print i
This is a nested loop
for i in range (5):
for j in range (3):
Tutorial:
Manually calculate the value of the variable ‘sum’ after following codes.
Sum = 0
for i in range(5):
for j in range (3):
Sum = Sum +j
print Sum
Now type the code in Python and compare the final value of the variable ‘Sum’ with your own calculation.
Tutorial:
What will be the value of the variable ‘sum’ after following nested loop?
Sum = 0
for in range (5):
for j in range (5):
Sum = Sum +1
Print Sum
Sorting
Sorting refers to arranging a series of data in a particular format. An example of real life sorting could be a telephone directory that sorted based on alphabetical order or a dictionary.
Here we will learn how to sort an array and develop our solution in an algorithm.
Bubble sort
Bubble sort is one of the simplest algorithms to sort a series of data. Watch the below video.
Consider the following list of data.
Bubble sort starts from the first pair of elements in the array, compared them to find which element is bigger.
In this case first element ‘5’ is smaller than the second element ‘11’, so already is sorted and do not need anything.
Next 11 and 6 will be compared; as 11 is bigger than 6 they location will be swapped.
Next ‘11’ and ‘10’
11 is bigger than ‘10’
After changing the location of ‘5’ with ‘1’ loop did not change any element in the next scan, this means array now must be sorted.
Bubble sort in Pseudocode
for all elements of the list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
end BubbleSort
Python script with details
List = [5,11,6,10,1] # List to be sorted
Exchange = True #Define Boolean Variable
print 'Sorting:', List
while Exchange : #If list sorted do not check it again
Exchange = False #if value of this variable no changed, list sorted
for i in range (len (List) - 1):
print '----------'
print Exchange
print 'value of i: ',i
print 'Compare', List[i], '-' , List[i+1]# Pick up a pair of element to compare
if List [i] > List[i + 1]: # If first element is bigger than second one
Temp = List [i] # Change the elements value
List [i] = List [i + 1]
List [i + 1] = Temp
Exchange = True #Still list is unsorted
print 'List changed to:'
print (List)
Python Scrip without details
List = [5, 11, 6, 10, 1]
print (List)
Exchange = True
while Exchange :
Exchange = False
for i in range (len (List) - 1):
if List [i] > List[i + 1]:
Temp = List [i]
List [i] = List [i + 1]
List [i + 1] = Temp
Exchange = True
print (List)
Tutorial:
Type the python script with details and check the output. It recommended calculating the algorithm manually, then comparing your manual output with the computer output.
Activity ‘Web search’:
Research about the bubble sort and analyze its advantage and disadvantage, then based on your research, find where in the real world bubble sort can be used.