ICT2103 Full Book-Part-1
ICT2103 Full Book-Part-1
Algorithm Abstraction
Pattern
Recognition
Computational
Thinking and Coding
in
Computational Thinking
and Coding in Python 1
2
Table of Contents
Preface 4
Study Guide 5
Computational Thinking and Coding 10 Data Analytics in Python 52
Example of an Algorithm 14 Why do I need to learn Data Analytics? 53
An Example of Applying Computational Thinking 16 The process of Data Analytics 54
Programming Language 18 Accessing files in pandas 55
Translating an Algorithm Into Python Code 20
Getting familiar with Azure Notebook 56
The for Loop 22
Examples 23 Example 1: Accessing Excel files 60
Exercises 26 The pandas DataFrame 62
Data Types and Variables 27 Skipping unnecessary rows and columns 63
Exercise on Variables 31 Display data in one or more columns 64
Python Data Types 32 Displaying unique values in a column 65
Arithmetic Operators 35 Describing the data of a column 66
Example 36 Slice data using index 67
User Defined Functions in Python 39 Working with loc in pandas 70
Examples of User Defined Functions 41 Renaming columns 72
Exercises 43 Calculated columns 73
Making Selections: The if Statement 44
Changing the index 74
Comparison Operators in Python 46
Python’s Logical Operators 47 Sorting data 76
Examples of Selections 48 Combining two similar DataFrames 78
Nested if 49 Grouping Data 79
Example of a Nested if 50 Working with missing data 80
Exercises 51 Writing data to an external file 83
3
Preface
Software
4
How can I make the most of this book?
We recommend that you read the book chapters in detail, watch the videos
linked to each chapter, and complete the practical codes and exercises at the
end of each section. Also, you can participate in the discussions on our
dedicated Facebook page.
9
Computational
Thinking and Coding
10
Computational Thinking and Coding
Computational thinking is the thought and processes involved in
formulating a problem and expressing its solution(s) in such a way
that a computer — human or machine — can effectively carry out
that solution. In other words, it is the ability to formulate a solution
that can be understood by humans and executed by computers.
Decomposition
Algorithm Abstraction
Pattern
Recognition
b =5
a =5
width = 6
Figure 2
12
Pattern Recognition: The process of looking for and finding
similarities. Patterns may exist within the same problem or in
different problems. Pattern recognition is important to get a
deeper understanding of the problem and begin formulating a
possible solution.
One way to solve this is to keep a running total in your head and while
adding numbers in sequence. That method does work, but it will take a
long time and the possibility of making a mistake is great. But if you
look closely, you will notice a pattern in these numbers: there are five
2’s, four 10’s, and three 5’s. Solving it now becomes much easier:
If you look around, you will find many patterns in daily life, including
the daily routine of going to sleep and waking up next morning.
Think of some of the patterns that you see around you and write
them down in simple steps.
13
Algorithm: A procedure for solving a given problem. It is made up of
a set of rules and steps needed to solve a problem. It is concise and
unambiguous, with clear start and end points. The algorithm can be
expressed in plain spoken language and/or a series of calculations, or
graphically in flow charts.
Example of an Algorithm
14
More Examples
Find all even numbers in a list
1,3,5,6,2,8,9,10,11,2,4
maxNumber = 0
For every number in the list
if the number is greater than maxNumber
assign maxNumber to the new number found
Next number
Study the algorithms above and see if you can improve or change them to
achieve the same result.
• Design an algorithm that lists all odd numbers between 50 and 500
• Design an algorithm that adds all numbers between 1 and 100
• Design an algorithm that finds the sum of all even numbers between 100
and 500
15
An Example of Applying Computational Thinking
Assume that you are required to develop an
algorithm to draw the shape on the right. How
can you apply computational thinking to this
problem, develop the solution/algorithm, and
later use a computer language called Python to
draw this shape?
16
What Does “Algorithm” Mean in Computer Terms?
An algorithm is a step-by-step process for producing a solution to
a problem which can be translated into computer code. There are
four main components of an algorithm: data acquisition,
sequence, selection, and iteration. Writing an algorithm to solve a
simple problem may not require all these components. An
algorithm is usually written in plain human language, and then
translated to a high-level programming language such as Python,
Java, C, etc.
17
Programming Language
Computer language, also referred to as programming language, is a
formal language used to communicate commands to a machine,
typically a computer. It is used to create a program to implement a
specific algorithm.
There are many computer languages available today and their use
depends on what technology the finished program will run on. In
addition, each programming language has unique capabilities and a
set of functions that play a role in choosing which one to implement
a particular algorithm.
• Java: Used to create programs for mobile phones, web sites, personal
computers, and many other platforms.
• C#, Visual Basic: Used to create programs that run on Microsoft products.
• Swift: Used to create programs that run on Apple products.
• JavaScript: Used to write games and other programs that run on web browsers.
• Python: One of the latest computer languages that has unique capabilities and
can run on many devices.
In this course we will be using Python. We will use a website called https://
repl.it to implement algorithms.
18
Why Python Programming Language?
Python is one of the most popular programming languages. In
recent years, it has gained more popularity due to its readable
code, powerful libraries, wide range of applications, and growing
and active community. According to an IEEE survey, Python is the
fourth most popular language, and the most popular introductory
programming language in US universities. The ability to connect to
a wide range of data sources, integrate with many applications
including machine learning, artificial intelligence, and motion
graphics, has made it one of the main choices to develop
applications that work on all platforms and computer devices.
19
Translating an Algorithm Into Python Code
In this book, we are using Python as our high-level programming
language. We are also using a specific graphical library that will
help us draw various shapes and objects. This library is called
“turtle.” In programming, the library refers to a code that is
already written and can be used to develop another code. The
turtle library has built-in commands that make it easy to draw an
object. For example, if you want to draw a line with a length of
100 points, you can simply use the command forward(100). To
access another library in Python, you need to use the keyword
import.
The above line connects our code/program to the turtle library and
creates an object with the name t. Now we can make it perform
some actions. For example, we can instruct it to move forward by
100 points.
t.forward(100)
20
Drawing a Square in Python - turtle
You are now ready to complete the square. You need to simply
write the same instructions two more times. What you have
done so far is translated the algorithm for drawing a square
from a human language into a high-level computer language.
We used Python and one of its libraries called turtle.
Code Explained
● Line 1: This line imports turtle and creates an object with the
name t.
● Line 3: Asks the object t to move forward by 100 points (or
pixels).
● Line 4: Changes to the left by 90 degrees.
● Lines 5 to 9: Repeats the same processes of lines 3 and 4.
At this point the square is complete.
● Line 10: Changes the angle by 5 degrees to the left.
21
The for Loop
The for loop repeats a block of code for a number of times. In
this example, we will repeat drawing the square 20 times.
Ends
The structure of the for loop when
i =19
starts with the keyword for in
lower case. A variable shouldbe
used as a counter. In this case for i in range(1,20):
we use i as a counter, but you
can use any name for the
variable. The keyword in range This isthe This loop
counter startswith
is required. i =1
Code Explained
● Line 3: This line starts at the same level as line 1. The for loop
is using i as a counter and starts from 1 and runs to 19. Please
note that it stops before 20 is reached.
● Lines 4 to 11: These lines are indented to the right. They all
start at the same level as the letter r of the for loop. This
means they all belong to the for loop and will be repeated.
22
Examples
23
Examples
24
Examples
25
Exercises
Now it is time to demonstrate the creativity and competency
that you have developed so far. Please try all these exercises
and submit your code through repl.it
Exercise 2: Draw a house or a car. You are free to decide the size
of the house and the model of the car. Try to show off your best
creativity and skills.
Exercise 3: Use the for loop to produce your best art.
Exercise 4: The table below provides additional turtle commands.
Use them to enhance your code for exercises 1, 2, and 3.
fillcolor color name Changes the color the turtle will use to fill a
polygon
• Explain the need for data types using real life scenarios.
• Classify different data types which can be used to
maintain program data.
• Apply the rules for variable names.
• Use arithmetic operators to perform calculation in
Python.
• Apply the Precedence Rules of arithmetic operators.
27
Data Types and Variables
In our daily life we use numbers and text to communicate and
perform certain processes. Numbers and text are called data types.
In Python and most programming languages, text is referred to as a
string. There are different types of numbers. For example, suppose
we want to know how many students are sitting in a classroom. Of
course we will not have 15.5 students, so we will use a whole
number, or integer. An integer is represented in Python as int. But if
we are dealing with prices of items where we need to use fractions,
then we will use float.
Python is very flexible with data types. You can easily change data
types.
Variables:
• Local Variable
• Global Variable
28
Local variables are the variables that are only referenced inside
the function in which it is declared. It is not accessible outside the
function.
Global variables are explicitly declared as global and can be used
in the entire program. These variables can be referenced from
any part of the program.
It is important to declare the variable by giving a valid name to it
before using it. While declaring a variable the type of variable
based on the values that are going to be stored on it should be
mentioned.
In python, it is not necessary to mention the type of the variable
while declaring the variable. The variable will take up the type of
the value that is assigned to it.
If a variable is assigned a number without decimal then the
variable is integer variable, and if we reassign a decimal value the
variable will become a float variable.
Python variables are created when they are first assigned with a
value. It is always important that the variable must be assigned
before being referred in the code.
Rules for Naming Variables:
29