0% found this document useful (0 votes)
46 views

Material Coding

The document provides information about computational thinking and its key components. It defines computational thinking as formulating problems and solutions in a way that humans and computers can understand. The four core components of computational thinking are decomposition, abstraction, algorithmic thinking, and pattern recognition. An example is provided to demonstrate applying these components to break down a complex problem. Real-life examples are also given to illustrate algorithms and their use of sequence, selection, and iteration to solve problems step-by-step.

Uploaded by

Anchal Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Material Coding

The document provides information about computational thinking and its key components. It defines computational thinking as formulating problems and solutions in a way that humans and computers can understand. The four core components of computational thinking are decomposition, abstraction, algorithmic thinking, and pattern recognition. An example is provided to demonstrate applying these components to break down a complex problem. Real-life examples are also given to illustrate algorithms and their use of sequence, selection, and iteration to solve problems step-by-step.

Uploaded by

Anchal Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 177

Week 1 – Computational Thinking and its Components.

(CLO1)
Learning Outcomes
• What is Computational Thinking?
• What are the key components of Computational Thinking?
• An example of applying computational thinking

2
What is Computational Thinking?
• 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.

3
Components of Computational Thinking
• There are four (4) core components of computational thinking:

Decomposition

Algorithm Abstraction

Pattern
Recognition

4
1. Decomposition
• Decomposition: is the process of breaking down a
complex problem or system into smaller and more
manageable parts.
• This could not be achieved without a proper and detailed
understanding of the problem that needs to be solved.

5
2. Abstraction
• Abstraction : The process of identifying important information and
ignoring irrelevant details.
• When you are dealing with a complex problem, there is the
possibility that many components or data will have no relation to
the problem you are trying to solve.
• Identifying and eliminating these components is an important task.
• A key to this process is to look for the information or facts that are
necessary to solve the problem.

6
Abstraction: example
• Find the area of the following shape with the facts provided:

a=5 b=5

Color = blue l=5

width = 6
7

7
Abstraction: exercise
• When you want to know the time, you only look at the hour, minutes
and seconds. Ignore the details like clock shape, colour, make, type
(digital / analog) which are not important.

• A tourist visit Dubai list down the import information he need in dubai
city map?

8
3. Pattern Recognition
• Pattern Recognition: is the process of looking and finding
similarities.
• Patterns may exist amongst 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.

9
Pattern Recognition: example

10

10
4. Algorithm
• Algorithm: is the step by step solution for the given problem.
• The algorithm can be expressed in plain spoken language and/or series of
calculations, or charts such flow charts.
• Algorithm must be
• Have clear and specific instructions.
• Do not miss any steps.
• Give steps in correct order.

11

11
Algorithm: example
• Make a cup of black tea 1. Get an empty cup of tea
2. Determine the type of tea
3. Add the selected tea bag in the cup
4. Determine if sugar is needed
5. Add the amount of sugar needed
6. Add water to a kettle
7. Turn on heat under the kettle
8. Bring water to a boil
9. Add boiling water into the cup
10. Stir the cup with the tea bag
11. Remove tea bag after a minute
12. Serve the tea
13. Done
12

12
Week 2 – Computational Thinking and its Components.
(CLO1)
Learning Outcomes
• What is an algorithm?
• Real life example of an algorithm

2
Lecture 2

Computer Algorithm

3
Learning Outcome
• Apply appropriate sequence and iteration to develop an algorithm
• Develop algorithms to draw various objects

4
Algorithm: reminder
• Algorithm: is the step by step solution to the given problem.
• It is clear and specific, include all steps and has correct order.
• The algorithm can be expressed in plain spoken language and/or
series of calculations, or charts such flow charts.

5
Algorithm
• Sometimes there are different solutions or algorithms to solve the
same problems.
• Some algorithms are faster and more efficient than others.
• There are many ways to solve the same problem.

6
Algorithm: example 1
• List all even numbers from 1 to 1000
• 2, 4, 6, 8, 10, 12....998, 1000
assign 0 to x
for number i = 1 to 500
increase x by 2
Display x
next i

7
Algorithm: example 2
• Find the maximum number in the following list:
• 1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
maxNumber = The first number
for every number in the list
if the number is greater than maxNumber
assign number to the maxNumber
next number
print maxNumber

8
Algorithm: example 3
• Find all even numbers in the following list:
• 1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
for every number in the list
divide the number by 2
get the remainder of the division
if the remainder = 0
the number is even, print it
else
the number is odd, do nothing
next number

9
Algorithm: exercise
Look around you for real life
problem.
• Use the 4 key components of
the Computational Thinking to Decomposition
produce a solution.
• Write the solution as an
algorithm
Algorithm Abstraction
• Present your solution to your
class mate and seek feedback.
• Summarize the feedback and Pattern
modify your algorithm Recognition

10

10
Algorithm in computer terms
• An algorithm is a step-by-step process producing a solution for 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 (simple) human language, and
then translated to a high-level programming language such as Python.

11

11
Algorithm in computer terms
• How to draw a square (100 points on each side)?
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points

12

12
Algorithm: example 4
• 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.

13

13
Algorithm: example 4 solution
• If you divide the shape into four parts,
examining each part carefully, you will
notice that the shape is made up of
squares.
• Each square is rotated slightly by a
small angle.
• This process of discovery is called
decomposition.

14

14
Algorithm: example 4 solution
• If you draw one square, then change
the angle slightly, draw the next square,
and repeat this multiple times, you will
get the shape.
• Seeing the shape is pattern recognition.

15

15
Algorithm: example 4 solution
• There are other lines in this shape that
are created by having many squares
drawn at different angles.
• If you spend too much time at this stage
trying to understand how they have
been created, you might get confused.
• Focusing on the big picture and finding
the most important part of the problem
is called abstraction.

16

16
Algorithm: example 4 solution
• The next step is writing the solution
step-by-step using simple language.
This is called algorithm.

17

17
Algorithm: example 4 solution
from 1 to the number of boxes
draw a box
change drawing angle by 5
degrees
next box

18

18
What to do next?
• Write algorithms to draw the following objects:

Car Computer

An object of your choice


19

19
Week 3 – Translating Algorithm into a computer code
(CLO1)
Learning Outcomes
• List four of the mostly used high level programming languages
• Recognize four widely used Python packages or libraries
• Convert algorithms from human natural language to computer code
in Python
• Use loop to repeat a block of code

2
High Level Programming Languages
• 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.

3
Example of programming languages
• 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.

4
How to implement?
• In this course we will be using Python.
• We will use a website called https://repl.it to implement algorithms

5
Why Python Programming Language?
The ability to connect to a wide range of data sources,
integrate with many applications including machine learning,
artificial intelligence, motion graphics, etc.
Package for scientific computing in Python.

SciPy (pronounced “Sigh Pie”) mathematics, science, and engineering

High-performance, easy-to-use data structures and data analysis


tools.
6

6 Matplotlib is a Python 2D plotting library


Translating algorithm to Python Code
• The turtle library has built-in commands that make it easy to draw an object.
• To access a library in Python, you need to use the keyword import.
• For example, if you want to draw a line with length 100 points, you can simply use
the command forward(100).
import turtle as t

t.forward(100)

t.left(90)

7
t.forward(100)
7
8

Drawing a square in Python - turtle


• Size of each side = 100 dots
Algorithm: • Python code:
• Move pen forward by 100 dots
• Change the angle to the left by 90
degrees
• Move pen forward by 100 dots
• Change the angle to the left by 90
degrees
• Move pen forward by 100 dots
• Change the angle to the left by 90
degrees
• Move pen forward by 100 dots
• Change the angle to the left by 5 degrees

8
Repetition :

9
The for loop
• The for loop repeats a block of code for number of times.
• In this example, we will repeat 4 times

stop after 4
loops

for i in range(4):

This is the
counter of
repetitions 10

10
The for loop
• Syntax:

for counter in range(start, end):


[list of instructions]
This loop, repeats [list of instructions] from counter = start until end - 1

11

11
for loop example

: :

Equivalent code
12

12
Some turtle commands
Method or Required
Description
Command Parameters
forward amount Moves the turtle forward by the specified amount
backward amount Moves the turtle backward by the specified amount
right angle Turns the turtle clockwise
left angle Turns the turtle counter clockwise
penup None Picks up the turtle’s pen
pendown None Puts down the turtle’s pen to start drawing
color color name Changes the color of the turtle’s pen
Changes the color of the turtle will use to fill a
fillcolor color name
polygon
position None Returns the current position
goto x,y Move the turtle to position x,y

shape shape name Should be ‘arrow’, ‘classic’, ‘turtle’, or ‘circle’


13

13 https://trinket.io/docs/colors
Drawing a circle
• Syntax: turtle.circle(radius, extent = None, steps = None)
• radius is the radius of the circle
• extent – an angle – determines which part of the circle is
drawn. If extent is not given, draw the entire circle. If extent
is not a full circle, one endpoint of the arc is the current pen
position.
• steps determines the number of steps to use
• Draw the arc in counterclockwise direction if radius is
positive, otherwise in clockwise direction.
14

14
Drawing a circle
• import turtle as t
• t.circle(50)  draw a circle
with a radius of 50 dots.
• t.circle(50, 180)  draw a ½
circle with a radius of 50 dots.
• t.circle(50, 180, 4)  draw a
½ circle with a radius of 50
dots, 4 steps.

15

15
fillcolor(colorName)
• Fills a closed shape with a color
• Should be used with begin_fill( ) and end_fill( )
• Example:
t.fillcolor("red")
t.begin_fill( )
t.circle(50)
t.end_fill( )

16

16
Exercise
• Draw the following shapes:

17

17
Week 4 – Data Types, Variables, Operators and
Expressions.
(CLO2)
Learning Outcomes
Distinguish between different data types

Apply the rules for naming variables

Use arithmetic operators to perform calculation in Python.

Apply the Precedence Rules of arithmetic operators

2
1. Data Types
• In our daily life we use numbers and text to communicate
and perform certain processes.

• The primitive data types are numbers and text.

• Objects such as images and files are also part of data


types.

3
Data Types
long (not
decimal binary octal hex
int available in
26 0b11010 0o32 0x1A
Python 3)
Examples: 26L
Numeric float 10.5
Types 0.105e2

complex Examples: 1 + 3.14j


1 – real, 3.14 - imaginary

' hello world '


string " hello world "
''’ use this for
Non – numeric multi line string '''
Types 4
True
4 bool
False
Exercise: Data Types
Identify the data types for the following

1. Total students present in the class


2. Course code of a subject
3. Room temperature
4. HCT student ID
5. Home address
6. Day of the week (Sunday, Monday, etc)
7. Price of a book
5
8. CGPA of a student
5
2. Variables
Variables are used to store data from one data type.
For example, the variable fname is used to store the first name of a person.
The first name of a person is of type text or string.

fname age
6

6
2. Rules for naming a Variable
Technical definition of a variable: A variable is a memory location where a programmer
can store a value.

● Must begin with a letter (a - z, A - B) or (_)


● Other characters can be letters, numbers or _
● Case Sensitive. Upper case is different from lower case
● Can be any (reasonable) length
● There are some reserved words which you cannot use as a variable name
because Python uses them for other things.
7

7
Exercise: Variables
Identify valid variable names from the following

1. max_price
2. Total students
3. US$
4. student_id
5. as
6. discount20
7. 1st_student
8. ICT-2013 8

8
Using Variable
• What if you want to change the size of the square from 100 to 50 dots?

• Solution 1:
• Replace all 100 by 50

• Solution 2:
• Use a variable instead of a value!
• How?

9
Using Variable

• Let us go back to our square


example.
• You noticed that we used 100 as
the length of the square.
• Now, we will use a variable L
instead of 100.

10

10
3. Arithmetic Operators
Operator Description Example

+ Addition Adds values on either side of the operator. Z=a+b

- Subtraction Subtracts right hand operand from left hand Z=a–b


operand.

* Multiplication Multiplies values on either side of the operator Z=a*b

/ Division Divides left hand operand by right hand operand Z=b/a

% Modulus Divides left hand operand by right hand operand Z=b%a


and returns remainder

** Exponent Performs exponential (power) calculation on Z = a**b


operators a to the power b

// Floor Returns the integral part of the quotient. Z = a // b

11

11
Exercise: Operators and Expressions.
Convert the following mathematical expression to Python
expression

1 2

4
3
12

12
4. Precedence Rules

1. ( ) x = 18 / 2 * 3 + 2
2. ** 9 * 3
3. *, /, %, // 27 + 2
4. +, -
29

13

13
Exercise: Precedence Rules.
Evaluate the following expressions using the operator
precedence.

1. 12 + 8 / 4 * 3
2. (12 + 8) / 4 * 3

3. 11 // 3 + 3 ** (4 - 2) * 2

14

14
Summary
Primitive Data types: int, float, string

Variables are used to store data from one data type.

Variables naming must follow the rules.

Arithmetic operators used to do arithmetic operations.

Arithmetic operators evaluates based on operator precedence.

15

15
Week 5 – Input, Process, Output.
(CLO2)
Learning Outcomes

Use assignment operator and expressions

Display information using print() statement

Read input from the user

Write simple program for real life problems

2
1. Assignment
variable = expression
The = operator assign the expression on the right side to the variable in left side.

a=5 a is 5

x = y = 10 x and y is 10
a, b = 10,5 a is 10, b is 5

s = 10 ** 3 s is 1000

tax = amount * 0.05 tax is 5% of 3

3
amount
Interaction with user
• To read/get a value from the user:
• use the command: input
• Read from the keyboard

• To display a value to the user:


• Use the command: print
• On-screen display

4
2. Display statement
print(expression[,expression2, expression3,…])
The print() function prints the given expression on the console screen.

print("hello world") hello world

print("hello ","world") hello world


print("hello ",2," you") hello 2 you

print(5 + 2) 7
5

5
print("5 + 20 = ",5 + 20) 5 + 20 = 25
3. Read user input
variable = input(string)
• Any entry is considered as a string
• Must Convert the entry to the desired data type

Example: ask the user for his name and his age
name = input(“Enter your name:”)
age = input(“Enter your age:”) Returns string

age = int(age) Converts from string to int


6

6
Input convertion
x = input(“Enter anything:”)

• The data type of x is string

• To convert the x into int:


• x = int(x)

• To Convert the x into float:


• x = float(x)

7
Example 1
Program to read name and age from user and display it.

name = input("Enter a name”) Input


age = input("Enter a age ”)
print("Hello ",name," my age is also ",age) Output

8
Example 2 – with simple calculation.
Program to read name and age from user and calculate and display his/her age on next year.

name = input("Enter a name”)


age = input("Enter a age ”) Input
age = int(age)

ageNextYear = age + 1 Process

print("Hello ",name," you will be ", ageNextYear, "next year" ) Output

9
Example 3
Write a Python program that read price of an item and quantity bought, calculate and display the total.

price = input("Enter the price of item ")


price = float(price) Input
qty = input("Enter the quantity ")
qty = int(qty)
total = price * qty Process

print("Total price ",total," dhs") Output

10

10
Exercise - 1.

Task1: Modify the previous example to read price of


two items, calculate and display the total

Task2: Modify the above example to calculate the VAT


5% and display the total price with VAT.

11

11
Exercise - 2.
Write a python program that calculate and display
area of the given shape.

Note: Formula to calculate area of triangle is

height

base
Note: Formula to calculate area of rectangle is

12

12 width
Exercise - 3.
Write a python program that read weight (kg) and
height (m) of a person, calculate and display his BMI
[Body mass index].

Sample output

13
13

13
Exercise - 4.
A basket can hold 5 apples. Write a python program that read total
number of apples, calculate and display the number baskets need and
number of apples that will be without a basket.
(Note: you need to use integer division and modulus operators)

Sample output

14

14
Summary
print() function used to display in standard output [screen]
Input() function used to read input from keyboard and return
as string
= used to assign value / expression on right to a variable in
left.
Program using input  process  output.
15

15
Week 6 – Selections
(CLO3)
Learning Outcomes
Apply if statement to single or multiple conditions.

Structure conditions in Python using the correct logical


operators.

Write Python codes to solve various computational


problems that require the use of the if statement.

2
Selection or Decision
Selections: if statement

• In life there are moments when we need to decide on something using


conditions.

• Example: “If it is raining outside, then I carry umbrella.”

• Another example, if you would like to buy an item and you found two similar
products. You will then put some conditions such as a price limit, rating of the
product by experts, etc.

3
Selection or Decision
Selections: if statement (Cont)

In programming such logic is written using if statement.

If the answer to the if statement is True, then a certain process


will be executed, else other processes will be executed.
Python programming language provides following types of decision making
statements.

• If..else
• nested if
• nested else if

4
Selections: if statement
Structure of if statement in Python:

Rules:
• A lower case if keyword must be used
• The if statement must end with a colon :
• The keyword else must be lower case and start at
the same level as the keyword if
• The keyword else must end with a colon :
5

5
Operator Description Example
Equal == If the values of two operands are equal, then the 2 == 2 [True]
condition becomes True, otherwise False. 3 == 2 [False]
"sum" == "sum” [True]
"ABC" == "Abc” [False]
Python Not Equal != If values of two operands are not equal, then 2 != 2 [False]
Comparison Not Equal <> condition becomes True, otherwise False. 3 != 2 [True]
"sum" != "sum” [False]
Operators "ABC" != "Abc” [True]

Greater than > If the value of left operand is greater than the 3 > 2 [True]
value of right operand, then condition becomes 3 > 3 [False]
True.
Greater than equal If the value of left operand is greater than or 3 >= 2 [True]
>= equal to the value of right operand, then 3 >= 3 [True]
condition becomes True. 3 >= 4 [False]
Less than < If the value of left operand is less than the value 2 < 3 [True]
of right operand, then condition becomes True. 3 < 3 [False]
Less than equal <= If the value of left operand is less than or equal 3 <= 4 [True]
to the value of right operand, then condition 3 <= 3 [True]
becomes True. 6
4 <= 3 [False]
6
Example – 1 (Condition with numeric)

emirates = input("How many emirates are in UAE ")


emirates = int(emirates ) Input

if(emirates == 7): Condition

print("Yes you are correct ") When True

else: Otherwise

print("No, there are 7 emirates ") When False

Sample output

7
Example – 2 (Condition with non numeric)

capital = input("What is capital of UAE ") Input

if(capital == "Abu Dhabi"): Condition

print("Yes you are correct ") When True


else: Otherwise
print("No, it is Abu Dhabi ") When False

Sample output

8
Note: String comparision is case sensitive.
Example - 3

Write a Python
program to calculate
the cost of insurance
based on the age. If
the age of the
subscriber is more
than or equal 40 years,
the cost will be 500
each month, otherwise
the cost is 300 a
month.

9
Exercise – Simple condition

Task 1:

Write a Python program that read mark of a student and display Pass
when given mark is more than or equal to 60, otherwise display Fail.

Task 2:

Write a Python program that read absent percentage of a student and


display Drop from the course when the absent percentage more than
equal to 15, otherwise display Continue the course.

10
Logical Operators

When you have more than one condition in the same if statement [compound
condition], then you need to use a logical operator. These logical operators simply
allow you to request that both conditions must be met or only one of them.
• If both are conditions must be True then use and.
• If Any one of the conditions is True then use or.

Operator Description Example

and If both the operands are 3>7 and 2<3


true then condition
becomes true.

or If any of the two operands 7 > 7 or 2 < 3


are non-z ero then
condition becomes true
11
2
Python Logical Operators

When you have more than one condition, it is


recommended to group them using brackets.
Think of a situation when condition 1 to be met and one of
the other two conditions

12
Example - 4

A child is eligible to age= input("What is your age") Input


enter a ride if its age is age= int(age)
between 4 to 10. Write if(age > 3 and age < 11): Condition
a Python program to
read child age, decide print("Yes you are eligible to ride ") True
and display if the child else: Otherwise
is eligible for ride or print("Sorry you not eligible to ride") False
not.

13
Task - 1

Modify the above Python program to read height and


age of a child and check if the child is eligible for ride,
age should be between 5 to 12, or height is more than
48 inch.

14
Exercise

Identify the output of the following code.

15
Week 7 – Nested Selections
(CLO3)
Learning Outcomes

Formulate the required logic to build a nested if statement to solve many


computational problems.

2
3

1. Nested if

There are other situations when you need to apply another


condition even in case the first condition was True. In this case
you need to use a normal if instead of process 1.

3
4

Example - 2

Write a Python program to ask users to enter a number. The


program determines and display given number is less than 1, single
digit (1 – 9) or positive number that is more than one digit.
Sample Outputs

4
5

2. Nested else if

There are situations when you need to apply another


condition in case the answer for the first condition is false.
In this case we replace else with elif.

5
6

Example 3

Write a Python
program to ask users
to enter an age. The
program determines
and display a
message as shown in
bellow table.

age Message Sample outputs


Upto 12 Child
13 to 19 Teenager
20 to 59 Adult
660 and above Senior
7

3. Nested if with nested else if

There are situations when you need to apply both nested


if with nested else if.

7
Example 4

Write a Python program to ask users to enter a three numbers. The


program determines and display a the biggest number of the three.
Sample outputs

8
9

More Example
Grade
Write a Python Grade Range Points
A 90 – 100 4
program to ask users
A- 87 – 89 3.7
to enter a mark out of
B+ 84 – 86 3.3
100. The program
B 80 – 83 3
finds the equivalent
B- 77 – 79 2.7
letter grade using the
C+ 74 – 76 2.3
below grading
C 70 – 73 2
system.
C- 67 - 69 1.7
D+ 64 - 66 1.3
D 60 – 63 1
F 0 – 59 0

9
Example 5

10

10
Exercises

Identify which of the following is a valid condition.

1. if (total = 500)
2. if (rate =>”FIVE” )
3. if (age = 2.5)
4. if (mark =< 40)
5. if (2.85 <= price)
6. if (5+8)
7. if ((1 - 1) <= 0)
8. if (ticketType == "Golden Class")

11
Exercises

Exercise 1: Convert the previous marks-to-grade example (example-5) to


read the grade letter and display the associated mark range.

Exercise 2: Modify the above example to display the Grade Point Average
(GPA) based on the mark.

Exercise 3: Modify the above program to allow the student to enter the marks
for 5 courses and calculate the semester average and GPA.

12
Exercises

Exercise 4: Write a python program to accept the input of a number and


check whether the number is positive, negative or zero.

Exercise 5: Write a python program to accept two numbers as input and find
out which is the larger number. If both are equal then print the result as “Both
are Equal”.

13

13
Week 8 – Repetition (for loop)
(CLO3)
Learning Outcomes
Apply for statement to repeat set of statements.

Understand and apply different types of for loop.

Write Python codes to solve various computational


problems that require the use of the for statement.

2
The for loop
The for loop repeats a block of code for number of times. In
this example, we will repeat
counter

for i in range(4):

for loop
number of
timers to
repeat

3
Key points to remember
The for loop is one kind of loop. It uses a counter which
takes on a value every time the loop iterates.

The range() function indicates how many times the


statement will repeat. For example range(4) means repeat
4 times and range(10) means repeat 10 times

4
Version 1: For loop with only end value

for i in range(endValue):
Statements

The starting value of loop is 0. In actual fact, range(4) generates four


numbers 0,1,2 and 3 and the counter takes on each value one at a time.
Example Output

5
Version 2: For loop with start and end value

for i in range(startValue, endValue):


Statements

The starting value of loop can be changed to any given number.


Note: Start value must be less than the end value, otherwise loop will not
be executed.

Output
Example

6
Version 3: For loop with increment value
for i in range(startValue, endValue, stepValue):
Statements
The starting value of loop can be changed to any given number. Step
value can be change from 1 to any value.
Note: Step value must be negative if start value is greater than end value.
Example Output

7
Example using loop
Exercise 1: Write a program that read 5 marks of a student, calculate and display the
average price.

# program to read 5 marks and display its average


total_marks = 0

for i in range(5):
mark = int(input("Enter a mark "))
total_marks = total_marks + mark

avg_mark = total_marks / 5

print("Average mark ", avg_mark)

8
Task

1. Identify and fix the errors in the bellow program.

2. Identify the output of the bellow program.

9
Exercises

Exercise 1: Write a program that read price of 5 products, calculate and


display the total price.

Exercise 2: Write a program that read 5 numbers display given number of


negative numbers.

Exercise 3: Write a program that display all the number from 1 to 25, that is
divisible by 5.

10

10
Week 9 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes
Understand the process of Data Analysis

Introduction to Python Pandas Library.

Read data from different data sources.

Applying data cleaning.

2
Why do I need to learn Data Analytics?

• The ability to work with data and


produce meaningful analysis that
help with taking the right
decision is becoming one of the
most important skills for today
work environment.
• Python and its data science
library named Pandas make this
task simple and fun.

3
The process of Data Analytics

• The process of data analysis is made of


7 key steps from deciding the objectives Report

of your analysis to writing the final


Visualize Results
report and conclusion.
Modelling&
Validation

• The time you spend at each spend Exploratory


Analysis

depends on the data that you working


Cleaning Data
with.
Data Acquisition

• Well organized and clean data require What are your objectives?

less time to be spent of cleaning and


exploring the data.

4
1. What are your objectives:
The first step in data analytics is deciding your objectives. What are you trying to
achieve?

2. Data Acquisition 3. Cleaning Data


Reading data from various sources The raw data may contain unwanted data or
including spreadsheet, comma separate missing data for some columns. In this
file, web pages, database, etc. Then you process you remove unwanted data and fill
store the data in variables. missing data if possible.

4. Exploratory Analysis 5. Modeling and validation:


The objective of this process is to get In this step you start building models of your
familiar with your data and data for the purpose of duplicating data
summarize their main characteristics. analytics techniques. Advance tools such
machine learning are used in this step, which
will not be covered in this book.
6. Visualizing Results
Using various charts to visualize the
7. Report
data to help with decision making
Writing the final report and key conclusions.
process.

5
What is Pandas?
• Pandas is an open source library
providing high-performance, easy-to-
use data structures and data analysis
tools for the Python programming
language.
• It provides fast, flexible, and
expressive data structures designed
to make working with data easy and
intuitive.
• Pandas allows data users to work
with high-level building blocks for
doing practical, real world data
analysis in Python.
6
Importing Pandas to Python

Similar to importing turtle library, the following line imports


pandas and create a pandas object with the name pd.

From now and on, you can use the object pd to perform
pandas operations.

7
Data Acquisition: 1. Read data from Excel file.

Pandas allows you to access various sources of data including Excel, Comma
Separated Values (CSV), tables, text, database, web and more.

The general syntax to read data from excel file is as follows:


The targeted
Pandas sheet.

Varaible = pd.read_excel(”Filename”, sheet_name=”Sheet1”)

Any variable The The file


name that function name
you create to that including
store the reads extensio
DataFrame. data n.
8
Data Acquisition: 1. Read data from Excel file.

Note: filename and work sheetname are case sensitive.

9
Data Acquisition: 2. Read data from CSV file.

Read data from a flat file - Comma Separated Values (CSV).

Varaible = pd.read_csv(”Filename.csv”, delimiter=‘,')


Example

10
Data Acquisition: 3. Read data from Web page (HTML).

Read data from a web page.

Varaible = pd.read_html(”url”) Note: read_html returns a list of DataFrame objects

11
Pandas Data Frame
DataFrame: DataFrame is a two (or more) dimensional data structure – basically a table with
rows and columns. The columns have names and the rows have indexes.

These are the Columns

index

Rows
12
Pandas Data Frame
Let us examine the pandas data frame in more detail.

Index: The index is column zero in the data frame. The default
index is integer number from 0 to the length-1 of the data
frame. Index is used to speed up searching large data.
Later we will learn how to change the index of a data
frame.
Columns: These are the names of the columns as in the original
excel data. My looking carefully at these columns, we will
note that they are not the appropriate columns for the data
set. The correct column names are in the third row. To get
the correct columns we need to skip the first 4 rows. We
will learn later how to rename columns and create new
columns.
Rows: Rows are the actual data excluding columns and index.
You may noticed that there are number of cells with data
NaN. These are the missing or not available data from our
data source. Later we will learn how to deal which such
missing data.
13
Data Cleaning: 1. Skipping unnecessary rows and columns
Pandas allows you to skip unwanted rows when reading the data.

We basically added comma then skiprows=4 to the original line that reads data from
the excel file. Store the result in a new data frame as data2.

14
Without skiprows After skiprows
Data Cleaning: 2. Working with missing data

In many cases you will find your self dealing with missing data. This
is indicated by NaN in the cells or just empty cell. How to deal with
such situation.

1. Drop missing observations 3. Drop column if they only contain missing values
This will delete all rows that contain any missing data. To force dropna to work on columns you need to add axis=1.

2. Drop rows where all cells in that row is NA 4. Fill in missing data with zeros
This will delete rows that are missing all its data. This will replace NaN with 0.

Note:
• keyword how=“all” to indicate all cells have missing data
15 • keyword how=“any” to indicate any cell have missing data
Example: Filling in missing data

In this example we have a simple data set that contains students score pre and post test. The data set
contains number of missing data of students who did not attend the exam. These missing data are
appearing as NaN. Our simple task is to replace NaN with average (mean) score of the column..

The data set is included in the excel file ”Grades.xlsx” that we used in the previous example. It
located in the sheet “PrePost”.

Here is the code needed to read the sheet.

16
Example: Filling in missing data

Fill all the missing values with 0 permanently using the below example.

To fill in the missing data with 0, we will use the following line:

17 Note: use inplace = True to make the changes permenent.


Data Cleaning: 3. Renaming columns
When reading data from a data source you may get some columns with long or unappropriated names.
Here you will learn how to rename a column.

Brace The old New


type of column column
bracket name Colon name
Comma

The
function
Brace
rename
type of
bracket
Please note that inplace=True is needed to ensure that the changes
you may are permanent. Otherwise, this line we display the new
column name and if you try to access the same DataFrame, then you
18 will get the old name.
Example: Renaming columns

Following command will rename column Question1, Question2, Question3, and Question4
to Q1, Q2, Q3, Q4.

19
Command summary.

20
Week 10 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes

Explore the data and Slicing.

Using statistical / aggregation commands .

Adding a calculated column.

Join two data frames.

Write DataFrame to an Excel file.

2
Exploring the data: Show information about the data frame.

dataFrame: Display all the rows and columns in the data frame.

dataFrame.columns: Display all the column name in the data frame.

len(dataFrame) : Display number of rows in the data frame.

dataFrame.shape: Display number of rows and columns in the data frame.

dataFrame.shape[0]: Display number of rows in the data frame.

dataFrame.shape[1]: Display number of columns in the data frame.

3
Example: Show information about the data frame.
1. Display all the rows and columns

2. Display all column names


4. Display number of rows

5. Display number of columns


3. Display number of rows and columns

4
Slicing the data: Show specific rows / columns of a data frame.
Slicing data is a technique which is used to create small sets of your large data.

dataFrame.head(): Display first 5 rows of the data frame.

dataFrame.head(n): Display first n rows of the data frame. [ n is an int value]

dataFrame.tail(): Display last 5 rows of the data frame.

dataFrame.tail(n): Display last n rows of the data frame. [ n is an int value]

dataFrame[start:end+1]: Display all rows start from index start to index end

dataFrame[start:end+1:step]: Display all rows start from index start to index end in the
intervals of step
dataFrame[" Column "]: Display specific column

dataFrame [[" Column1 ", " Column2 ",…]]: : Display specific columns
5
Example: Show specific rows of the data frame.

1. Display top rows

2. Display bottom rows

2. Display Specific rows

6
Example: Show specific columns of the data frame.
data

Show only one column Show more than one columns

7
Slice data using loc

The pandas loc function allows us to search and slice


data based on both index and columns. It is a powerful
tool to allow us to focus on the important rows and
columns for our data analytics.

8
Slice data using loc

Represents the The colon This represent This


first row in your is the last row in comma
targeted data. separates your targeted separate
If you want start and data. If you s rows
data starting end of the want all data and
from row zero, rows. It is to the end of columns.
then leave it must the set, then It is must
empty, have. leave it empty. have.

The name of Please note Here you specify The colon Here you specify
your data the use of the first column is the last column
frame object. square name. Please separates name. Please
In our example bracket. note that you start and note that you
this is data2. Normal should use column end of the should use column
bracket will name and not columns. It name and not
not work. numbers. is must numbers.
have.
9
Example: Slice data using loc

Display rows 2 to 5 and columns "Higher Education Institution” to "Enrolled_Post Graduate"

Note that you need to use the index of the rows and the name of the column.

In this example the index is 2:5

The column
"Higher Education Institution” :"Enrolled_Post Graduate"
10
Slice data using loc

You can display columns that are not in sequence, you need
to add then inside a square bracket [ ].
Example: Display rows 3, 5, and 5 and Columns "Higher Education Institution” and
"Enrolled _ UnderGraduate"

11
Slice data using iloc

The pandas iloc function similar to loc to slice rows and


columns, it use index for columns instead of column
names.
Index.  0 1 2 3 4 5

Note that you need to use the index of


the rows and the columns.

In this example the index for row is 2:5


and index for columns is 0:2
12
Changing index

The default index in a DataFrame is integer values


starting from zero. To change the default index to any
other column, you need to use .set_index as follows:

Example: Change the the index of our test example to StudentID

Note: Higher Education Institution is


now the index and presented
different on the DataFrame. The
column is appearing in bold.

13
Resetting the index
When you need to reset the index back to its original values. There are different ways to
do this. On common method is to run the line that reads the data from your source.
However, you can use the function .reset_index()

Reset the index of our test example to its original values

14
Statistics / Aggregation Commands
When you need to summaries the data in data frame Pandas makes the calculation of
different statistics very simple.

Syntax of using statistic command on


specific column
dataFrame["Column"].statistics_method()

Syntax of using statistic command on


all column
dataFrame.statistics_method()

15 15
Displaying unique values in a column
Finding unique (nonrepeating) values in a column is needed to perform analysis on your
data.

dataFrame["Column"].unique()

For example, to know the unique values in the column "Specialisation" use the function
.unique() that helps you with perform this task.

16
Calculated Columns

Pandas allows you to easily add new columns to the DataFrame. This is usually used to
create a new calculated column.

Syntax to create a new column: DataFrame[”New Column"] = expression


Example:
Bellow example create a new column Total Enrolled which is sum of Enrolled Graduates and Enrolled Post Graduate

17
Appending Data : Join two Dataframe

newDataFrame = dataFrame1.append(dataFrame2)

Example: You have two data frames as bellow. Both sheet have the same structure. They contains students
from Education and Foundation Specialisation. We need to combine both in one DataFrame.

dataFrame1 dataFrame2

18
Combained dataFrame
Writing data to external file

Example: Write the data you cleaned in the previous example to an external file.

The above lines stores the DataFrame data in the


an excel file ‘NewData.xlsx’ in a sheet with the
name ’Sheet1’.

19 19
Summary of Pandas Commands
Selection Statistics
• df[col] | Returns specific column • df.describe() | Summary statistics for
• df[[col1, col2]] | Returns columns numerical columns
• df[start:end+1,step] Return specific rows • df.mean() | Returns the mean / average
• df.loc[start:end,”col1”:”col2|] Returns specific rows and columns • df.count() | Returns the number of non-
• df.iloc[start:end,col1Index:col2Index] null values
• df.sum() | Returns the sum of values.
Viewing/Inspecting Data • df.max() | Returns the highest value
• df.min() | Returns the lowest value
• df.head(n) | First n rows of the DataFrame
• df.tail(n) | Last n rows of the DataFrame Change index
• df.shape() | Number of rows and columns
• df.unique() | Return unique values • df.set_index(col,inplace=True)
• df.reset_index(inplace=True)

Exporting/Writing Data Join Dataframes


• writer = pd.ExcelWriter(filename) • df1.append(df2)
• df.to_excel(writer,'Sheet1')
• writer.save() Calculated Columns
• df[newCol] = expression
20
Week 11 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes

Filter data using conditions.

Sort data

Group data

2
Slice data using simple conditions
You can also slice data using conditions based on the value of one or more
columns.
The structure of the command line is: DataFrame[Condition]
Example 1 Display all the rows where "Specialisation" is "Health and Medicine"

Condition data["Specialisation"] == "Health and Medicine"

3
Slice data using simple conditions

Example 2 Display all the rows where " Enrolled _ UnderGraduate" is 1000 and above

Condition data["Enrolled _ UnderGraduate"] >= 1000

Example 3 Display total number of rows where "Enrolled _ UnderGraduate" is above


the average "Enrolled _ UnderGraduate"
Condition data["Enrolled _ UnderGraduate"] >= data["Enrolled _ UnderGraduate"].mean()

4
Slice data using multiple conditions
To slice data using more than one condition, you need to put each condition inside a bracket ( ).
You also need to use a logical operator. The symbol & is used for and. The symbol | is used for or.

The structure of the command line is: DataFrame[(Condition) & (Condition)]


DataFrame[(Condition) | (Condition)]

Example 1 Display all the rows where "Specialisation" is ”Law” or "Humanities"

Condition (data["Specialisation"] == "Law") | (data["Specialisation"] == "Humanities")

5
Slice data using multiple conditions

Example 2 Display all the rows where "Enrolled _ UnderGraduate" value is from 500 to 800

Condition (data["Enrolled _ UnderGraduate"] >= 500) & (data["Enrolled _ UnderGraduate"] <= 800)

6
Sorting data with one column

Sorting data is a technique that display data in an ascending or descending order.


Syntax
By default, the data will be sorted in ascending order.
Example: Sort the Enrolled _ UnderGraduate column

Ascending Order
7 Descending Order
Sorting data with more than one column

To sort data based on more than one column, you need to include all
columns inside the square bracket.
Example: Sort Specialisation in ascending order, Enrolled undergraduate in descending order

8
Grouping Data
Grouping data by columns is used when you have duplicated data in a particular column. For example
you, the same student is doing more than one course and has grades in each course. The function
needed for this operation is .groupby.

Syntax
Instead of mean(), you can use sum(), min() or any of the aggregation commands.

Example: Total. Number of students enrolled in each specialisation

9
Grouping Data
Example: Total. Number of students enrolled in each specialisation, by each Institution

10
Summary of Pandas Commands

Filter
• DataFrame[Condition]
• DataFrame[(Condition1) & (Condition2) ]
• DataFrame[(Condition1) | (Condition2) ]

Sort

• DataFrame.sort_values([Columns], ascending = [True / False])

Group

• DataFrame.groupby ([Columns]).aggregation command()

11
Week 12 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes

Data Visualization.

Understand the different types of charts.

Plotting 3 different types of charts

Enhancing the chart

2
Data Visualization
Using charts to visualize data helps with improving understanding of a data set,
interpretation of the data and communications among people who work with it.

Pandas offers many chart that are suitable for use within
management or corporate presentations.

A chart can create a clearer picture of a set of data values than a


DataFrame with rows and columns, allowing managers to
incorporate this understanding into analysis and future planning.

It is important that x-axis will be the index of your DataFrame. If you want to have a
specific column to be your x-axis, then you need to set the index to that column.

3
Comparison of different charts.
Graph Type Advantage Disadvantage
Line graph  Can compare multiple  Use only with continuous data
A line graph plots continuous data as continuous data sets easily
points and then joins them with a  Interim data can be inferred
line. Multiple data sets can be from graph line
graphed together, but a key must be
used.

Bar graph  Visually strong  Graph categories can be


A bar graph displays discrete data in  Can easily compare two or reordered to emphasize certain
separate columns. A double bar three data sets effects
graph can be used to compare two  Use only with discrete data
data sets. Categories are considered
unordered and can be rearranged
alphabetically, by size, etc

Pie chart  Visually appealing  No exact numerical data


A pie chart displays data as a  Shows percent of total for  Hard to compare 2 data sets
percentage of the whole. Each pie each category  "Other" category can be a
section should have a label and problem
percentage. A total data number  Total unknown unless specified
should be included.  Best for 3 to 7 categories
 Use only with discrete data

4
Comparison of different charts.
Graph Type Advantage Disadvantage
Histogram  Visually strong  Cannot read exact values
A histogram displays continuous  Can compare to normal because data is grouped into
data in ordered columns. Categories curve categories
are of continuous measure such as  Usually vertical axis is a  More difficult to compare two data
time, inches, temperature, etc. frequency count of items sets
falling into each category  Use only with continuous data

Pictograph  Easy to read  Hard to quantify partial icons


A pictograph uses an icon to  Visually appealing  Icons must be of consistent size
represent a quantity of data values in  Handles large data sets  Best for only 2-6 categories
order to decrease the size of the easily using keyed icons  Very simplistic
graph. A key must be used to explain
the icon.

Map chart  Good visual appeal  Needs limited categories


A map chart displays data by  Overall trends show well  No exact numerical values
shading sections of a map, and must  Color key can skew visual
include a key. A total data number interpretation
should be included.

5 Source:
https://www.whaqualitycenter.org/Portals/0/Tools%20to%20Use/Making%20Sense%20of%20Data/Using%20Graphs%20to%20Display%20Data%20R%202-12.pdf
Plotting a Line Chart
A line graph plots continuous data as points and then joins them with a line. Multiple data sets
can be graphed together, but a key must be used.

The structure of the command line is: DataFrame[Column].plot() DataFrame[Column].plot.line()

Example: A line chart to display number of students enrolled in Undergraduate and Post Graduate

df[['Graduate _ UnderGraduate','Graduated_Post Graduate']].plot()

6
Plotting a Bar Chart
A bar graph displays discrete data in separate columns. A double bar graph can be used to
compare two data sets.

The structure of the command line is: DataFrame[Column].plot.bar()


Example: A bar chart to display number of students enrolled in Undergraduate and Post Graduate in each Specialisation

df[['Graduate _ UnderGraduate','Graduated_Post Graduate']].plot.bar()

7
Plotting a Pie Chart
A pie chart displays data as a percentage of the whole. Each pie section should have a label
and percentage. A total data number should be included.

The structure of the command line is: DataFrame[Column].plot.pie()


Example: A pie chart to display total number of students enrolled in each specialisation

df['Total Enrolled'].plot.pie()

8
Enhancing the Chart - Example
1. Create a bar chart with title and change colours

chart = df[['Graduate _ UnderGraduate','Graduated_Post


Graduate']].plot.bar(title='Enrolled Students',color=["Blue","Green"])
Title
2. Change the x and y label
chart.set_xlabel('Specialisation’)
chart.set_ylabel('Enrolled')
Colours

Y-label

X-label
9
Summary of Pandas Commands

Display chart

• DataFrame[Column].plot.line()
• DataFrame[Column].plot.bar()
• DataFrame[Column].plot.pie()

Enhance

• title = “your title”


• color = [‘colour1’,’colour2’]
• set_xlabel(‘x_label’)
• set_ylabel(‘y_label’)

10

You might also like