Computer Program - a structured sequence of instructions written in a specific programming language that directs a computer or digital system to perform a series of tasks or calculations
Typically, a program has:
Source code
Control flow
Data structures
Input/Output (I/O)
Error handling
Python Indentation - refers to the spaces at the beginning of a code line. Python uses it to indicate a block of code.
Where in other programming languages the indentation in code is for readability only, the indentation in Python is not that important. False
Variables are used to store information to be referenced and manipulated in a computer program.
Variables also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.
Data Types are the classification or categorization of data items.
Data Types represents the kind of value that tells what operations can be performed on a particular data.
Data Types are categorized into Primitive and Non-primitive Data Types
Examples of variables with common primitive data types:
my_birthday = “September 25, 2000” → String Data Type
my_age = 100 —> Int Data type
my_weight = 65.4 —> Float Data type
I_am_old = True —> Boolean Data type
Wala_lang = None
Identifying variable data types
A variable's type determines the values that the variable can have and the operations that can be performed on it
x = “hello”
print(type(x)) →outputs <class 'str'>
print(isinstance(x, str)) —>outputs true
Data type conversion refers to the process of changing the representation of data from one data type to another.this is to ensure that data types will adhere to the logic of our program.
Data Structures - Also called “collections”, they are built-in types that allow you to store and manipulate collections of data. They can contain different types of data
Data Structures - Elements can be accessed by index if the collection is indexed
Lists
Are created using square brackets []
my_???? = [1, 2, 3, “four”, 3, 4.0]
Ordered, mutable (can be changed), indexed, and allows duplicate elements.
Tuples
Created using round brackets ()
my_???? = (1, 2, 3, 4, True)
Ordered, immutable (cannot be changed after creation), indexed, and allows duplicate elements.
Sets
Created using curly braces {}
my_???? = {1, 2, 3.5, “hello”}
unordered, unchangeable* (but you can remove or add items in it), and unindexed.
Dictionary
Created using curly braces {} and contains keys and values by pair
my_???? = {'name': 'Bentong', 'age': 99}
ordered*,unindexed, changeable and do not allow duplicates.
Conditionals allow a program to execute certain pieces of code based on whether specific conditions are met.
Conditionals enable decision-making within the code, allowing
different outcomes depending on varying inputs or states.
Programming languages evaluates conditions to be either true or false
Complete the image:
A) True
B) False
C) Condition
For conditions to be evaluated, we use comparison operators
● == is equal to
● != not equal to
● > greater than
● < less than
● >= greater than or equal to
● <= less than or equal to
Alongside comparison operators we also use logical operators to combine conditional statements
● and (both conditions must evaluate to true)
● or (only one of the conditions must evaluate to true)
● not (reverses the truth value of a condition)
Conditionals
● if statement
● ifelse statement
● ifelseif/elifelse statement
● switch statement (available as “ match case ” in python 3.10 and up)
● Ternary operator
A loop is a programming construct that repeats a block of code as long as a condition is true.
Loops help automate repetitive tasks, making code more efficient and readable.
Types of loops in Python
● for loop: Used for iterating over a sequence (like a list, tuple, dictionary, string,
or range).
● while loop: Repeats as long as a specified condition is true.
Loop Controls
The flow of the loop can also be controlled to our preference.
● break: Exits the loop prematurely.
● continue: Skips the rest of the code inside the loop for the current iteration
● pass: Does nothing; it’s a placeholder
Looping best practices
● Avoid infinite loops.
● Ensure loop conditions will eventually be false.
● Use appropriate loop types for different scenarios.
Lists
Ordered
mutable (can be changed)
indexed
allows duplicate elements.
Tuples
Ordered
immutable (cannot be changed after creation)
indexed
allows duplicate elements.
Sets
unordered
unchangeable* (but you can remove or add items in it)
unindexed
(does not allow duplicate values)
Dictionary
ordered*
changeable
unindexed
do not allow duplicates
Source code: Human-readable instructions written in a language like Python, Java, or C++.
Controlflow: Logical constructs like loops, conditionals, and functions that manage the sequence of operations.
Datastructures: Ways to store, organize, and retrieve data efficiently (e.g., arrays,
lists, trees, variables).
Input/Output (I/O): Mechanisms for interacting with the outside world, such as reading input from users or files and producing output.
Error handling: Techniques to manage and recover from unexpected conditions or
mistakes (e.g., exceptions, validation checks). (real-time debugging vs compile-time