Computational Thinking
(Programming
Logic)
By: Mr. GYENI Q. STEHEN (PRIMO)
0541982749/0262883344
evetsnas@[Link]
PRESEC-LEGON
DATA TYPES
A data type is a label or an element that tells a computer how to
understand and handle a piece of data. We can define data as an
elementary value or a collection of values.
Common examples of data type in programming include integers,
floating-point numbers, booleans, and strings. These types are
often abbreviated to int, float, bool, and str. They are pre-defined
data types that are built into most programming languages and
have a fixed size and format.
Note: data types can vary from one programming language to
another.
Common Data Types
1. Numeric Data Types
Used to store numbers.
• Integer (int): Whole numbers (e.g., 10, –45, 200).
• Float/Real (decimal): Numbers with decimal points (e.g., 3.14, –
0.75).
• Double: Larger range of decimal values with higher precision.
Common Data Types
2. Character Data Types
• Character (char): Stores a single letter, digit, or symbol (e.g., 'A', '7',
'$’).
3. String Data Types
• Stores a sequence of characters (letters, words, sentences).
• Example: "Hello", "OpenAI", "123 Main Street".
Common Data Types
4. Boolean Data Types
• Stores two logical values only: True or False.
• Example: Used in conditions like isLoggedIn = True.
5. Date/Time Data Types
• Stores date and time values.
• Example: 25-08-2025, 14:35:20.
Why Are Data Types Important?
• Ensure the correct kind of data is stored in variables.
• Help the computer save memory efficiently.
• Prevent errors by restricting invalid operations (e.g., you can’t
multiply "Hello" by 5 in most languages).
Variables
A variable was explained as a named location in memory that
stores a value of a specific data type.
On the other hand, the data type defines which operations can
safely be performed to create, transform and use the variable in
another computation. A variable can have a short name (such as
x and y) or a more descriptive name (such as age, carName or
total_volume).
Revise notes on Rules for Variable Naming.
Data Types in Python
A variable was explained as a named location in memory that
stores a value of a specific data type.
On the other hand, the data type defines which operations can
safely be performed to create, transform and use the variable in
another computation. A variable can have a short name (such as
x and y) or a more descriptive name (such as age, carName or
total_volume).
Revise notes on Rules for Variable Naming.
Data Types in Python
In some programming languages, a variable has to be declared,
indicating its name and data type, before it can be used. In
Python, you do not need to declare variables before using
them. The data item is set to a type when you assign a value to a
variable.
Some examples are given in Table 4.5, using a variable called ‘x’.
Data Types in Python
If you want to set a variable to a specific data type, you can do so
using type conversion functions using Python. This means that
you can change the type of a variable’s value to another type by
using specific functions k
nown as type conversion functions.
The point here is that “conversion functions” allow you to convert
data from one type to another. For example, you can convert a
number stored as a string to an integer or change a floating-point
number to an integer. Example 3
int(“123”) converts the string “123” to the integer 123.
Predictions (Expected Output)
• w = False → This is a Boolean value.
Output: <class 'bool'>
• x = 1 → This is an integer value.
Output: <class 'int'>
• y = 2.8 → This is a floating-point number.
Output: <class 'float'>
• z = "hello" → This is a string.
Output: <class 'str'>
Data Structures
The term “structure” refers to the collection of two or more
systems working together as a whole. For example, the body
system such as digestive system, reproductive system, excretory
system, circulatory system and the rest put together is called the
body structure.
Though a single variable can be considered a data structure in
the most fundamental sense, data structures are more commonly
understood to be more complex arrangements that can hold
multiple values or collections of data. Data structures are a
fundamental concept in computer science
Data Structures
They are used to organise and store collections of data in a way
that facilitates efficient access and modification. Examples
include arrays, linked lists, stacks, queues, trees, and graphs.
These structures can hold multiple items of certain data type(s),
and provide ways to efficiently access, manage, and manipulate
these data items.
They are like the building blocks that allow programmers to
efficiently store, retrieve, and manipulate data in computer
programs. It defines the relationship between data items and the
operations that can be performed on them.
Types of Data Structures
1. Primitive Data Structures
These are the basic data types provided by programming languages.
Examples: Integer, Float, Character, Boolean, String.
2. Non-Primitive Data Structures
These are more complex and built using primitive data types.
Data Structures are often classified into two main types:
1. Linear data structures
2. Non-linear data structures
Types of Data Structures
• Linear data structure
The arrangement of the data is made on a straight path, where
each element consists of the successors and predecessors,
except for the first and the last data elements. The term traversing
refers to the iterating over a collection of data. Data elements in a
linear data structure are traversed one after the other and only
one element can be directly reached while traversing. All the data
items in linear data structures can be traversed in a single run.
Types of Data Structures
(a) Linear Data Structures
Data is arranged in a sequential (one after another) manner.
• Array: Collection of elements of the same type stored in contiguous
memory (e.g., [10, 20, 30]).
• List: Ordered collection that can store different types (e.g., ["apple",
10, 3.5]).
• Stack: Follows LIFO (Last In, First Out) principle (e.g., undo
operations).
• Queue: Follows FIFO (First In, First Out) principle (e.g., printer job
queue).
Types of Data Structures
Non-linear data structure
Non-linear simply means that the items or elements are not
arranged or organised in an end-to-end structure.
In computing, non-linear data structures are those where the data
elements are not organised sequentially, but rather, in an
interconnected manner.
All the data elements in a non-linear data structure cannot be
traversed in single run.
Types of Data Structures
(b) Non-Linear Data Structures
Data is arranged in a hierarchical or interconnected manner.
• Tree: Hierarchical structure with a root and branches (e.g., family
tree, file system).
• Graph: Collection of nodes connected by edges (e.g., social networks,
maps).
Types of Data Structures
Static Linear Data Structure.
Definition:
A static linear data structure has a fixed size, determined at the time of
creation.
Once created, its size cannot change during program execution.
Examples:
Array in C, Java, or Python (list with fixed size).
Types of Data Structures
Features:
[Link] is allocated at compile time.
[Link] to implement, fast access (indexing).
[Link] memory if not fully used.
[Link] grow or shrink in size.
Example in Python (conceptual):
arr = [10, 20, 30, 40, 50] # fixed size array of 5 elements
Types of Data Structures
Dynamic Linear Data Structure
• Definition:
A dynamic linear data structure can grow or shrink in size during
program execution.
Memory is allocated as needed, making it flexible.
Examples:
Linked List
Dynamic Lists in Python (resizable).
Types of Data Structures
• Features:
[Link] is allocated at runtime.
[Link] use of memory (no wastage).
[Link] complex to implement than static.
[Link] access than arrays (no direct indexing in linked lists).
• Example (Linked List idea):
# A simple dynamic list in Python
my_list = []
my_list.append(10)
my_list.append(20) # grows at runtime
Difference Between Linear and Non-
Linear Data Structures
Why Are Data Structures Important?
• Improve efficiency in storing and retrieving data.
• Enable faster searching, sorting, and processing.
• Provide the foundation for algorithms.
• Used in almost all software systems (databases, operating systems,
compilers, AI, etc.).
Common Data Structures Operations
Searching: Looking for an element in a data structure.
Sorting: Putting the elements of a data structure either in
ascending or descending order.
Insertion: Adding a new element to a data structure.
Updating: Replacing a data structure element with another
element.
Deletion: Removing the element from the data structure
Traversal: Visiting each element of the data structure exactly once.
Merging: Combining two similar data structures into one.
Lets Brainstorm
Group Work