Most of it Flashcards
Control structures
building blocks of programming that manage the flow of a program based on conditions
Sequence structure
default control stucture, where instructions are executed one after another in order, e.g assigning a value to a variable, performing arithmetic operation
Iteration
refers to the process of repeating a block of code multiple times, used for repitition until condition is met. For loop and while loop.
Example of while loop
Initialize counter to 1
While counter is less than or equal to 10
Print counter
increment counter by 1
End while
In code =
total_sum = 0
number = -1
while number !=0:
number = int(input(“enter a number(0 to stop):”))
Total_sum =+ number
print(“total sum:”, total_sum)
Modular coding
Breaking down a complex problem into smaller, manageable parts.
Functions
Block of code designed to perform a task. It can take input process it and return it. In python the def keword is used follow by function name and parentheses.
Parameters
Functions can take any number of parameters, allowing you to pass data to the function
Charactersitics of modular coding
Cohesion - Each module should focus on 1 thing
Low coupling - interact with each other as little as possible
Encapsulation - Modules should hide their inner workings from other modules
Abstraction: Modules should provide a simple interface for others to use, hiding complex details
Local scope
Variables defined inside a function or block.
Def my_function():
local_var = “i’m local”
print(local_var)
Encapsulation
Binds together data and functions that manipulate data, keeping both safe from outside interface and misuse.
class Myclass
def_init_(self)
self.my_variable = “i’m encapsulated”
def my_method(self):
print(self.my_varaible)
Arrays
Collection of items stored at contiguous memory locations ; storing multiple items of the same type together to organize data more efficiently. Key features include fixed size, all elemnts must be same data type, elements can be accessed directly using their index number.
Properties of arrays
Properties of array:
index: array elements are accessed via indices
Size: the total number of elements that an array can hold
Fixed size: once declared, size of an array cant be changed
my_array[0] = 1
size = len(my_array)
Operations on arrays
Traversal: visiting each elemnent on the array
Insertion: addingan element to the array
Deletion: removing an element
Search: finding the location of an element
Update: changing an existing element to a new value
first_element = my_array[0] # access element
my_array[0] = 2 # update element
my_array.append(3) # insert element
def my_array[0] # delete element
One dimensional arrays
contains elements in a linear sequence, practical for storing data.
Practical
one_d_array = [1,2,3,4,5] # a one-dimensional array
print(one_d_array[0]) # accessing the first element
two dimensional arrays
organize elements in rows and columns
two_d_array = [[1,2,3],[4,5,6],[7,8,9]] # a 2 dimensional array
print(two_d_array[0][0] # accessing the first element of the first array
Advantages and disadvantages of arrays
Advantages:
- Simple and easy to use
-Efficient data storage and retireval for indexed data
-Basis for other data structures like strings
Disadvantages:
-Fixed size
-Poor perfomance for insertions and deletions compares to other data structures
- Does not offer dynamic resizing
Dictionary
A dictionary is a collection of key-value pairs, where each key is unique and used to access its corresponding value.
Charactersitics : changeable, ordered insertions, can grow
e.g student_grades = {‘Alice’: 85, ‘Bob’:92, ‘Clara’: 88}
accessing it: print(my_dict[‘name’]) # output name
adding it: my_dict[‘age’] = 30
updating: my_dict[‘name’] = ‘Doe’
delete: del my_dict[‘name’]
Dictionary iteration
for key in my_dict:
print(key)
Use cases of dictionaries
Advantages:
- Fast access: lookup of values based on key is very efficient
- Flexible: keys can be any immutable type, making dictionaries versatile
Limitations:
- Unordered: older python libaries, dictionaries did not maintain order
-Memory usage: dictionaries comusme more memory than lists
relational operators
== equals to
!= not equal to
> greater than
< less than
>= greater than or even
<= less than or even to
OOP
the concept of “objects” which contain data in forms of fields(attributes) and code in the form of procedures(methods)
Encapsulation, abstraction, inheritancem and polymorphism
Encapsulation
bundling of attribvutes and methods that operate on the data into a single unit called object to restrict direct access to an objects components.
Benefits:
- Improved security
- Simple interface while hiding complex implementation details
Abstraction
the concept of hiding the complex reality while exposing only the essential parts.
Benefits:
- reduces complexity
Inheritance
mechanism where a new class is created from an existing class
Benefits:
- Code reuse
- Establish a relationship between different classes through their objects