



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




● An algorithm is a set of steps to solve a problem or perform a task. ● you can use flowcharts or pseudo code ● an equal sign (=) can only be used when comparing (in the diamond) whereas everywhere else must use arrows (<—— ) [to assign to variables]
● An algorithm is more efficient if:
● Algorithms that look for a target item within a data structure (e.g. - a list). ● Needed to identify if a target item exists and which position it exists in. ● LINEAR SEARCH
● Operators that combine or invert Boolean values such as : AND, OR, NOT.
● These are ways of organising and storing data so that it can be used efficiently: [e.g - arrays, lists] ● Choice of data structure affects how easily you can store, access, search, sort data. ● List = a data structure that stores multiple pieces of related data under one variable name in an ordered sequence. ● Each item in the list is called an element, and each element has a position number known as its index (starting from 0). ● Lists can be changed as items can be added, removed, or updated and you can access any item directly using its index. ● They are useful for storing data of the same or different data types, such as integers, strings, or both, in one place. ● USING LISTS… -Access an element —-> list[index] -Example: numbers[0] gives the first item. -Add an element at the end —-> append() -Example: numbers.append(5) -Insert at a specific position —> insert(index, value) -Example: numbers.insert(2, 10) -Remove an element by value —-> remove(value) -Example: numbers.remove(3) -Remove an element by index —-> pop(index) -Example: numbers.pop(1) -Changing an element ——>list[index] = new_value -Example: numbers[0] = 7
-Find the number of elements —-> len(list) -Example: len(numbers)
● Input() gets thee data and print() outputs the data in python.
● Concatenate strings —> + -Example: "Hello " + "World" —> "Hello World" ● Get length —> len(string) -Example: len("Hello") —> 5 ● Access a character —> string[index] -Example: "Hello"[0] —> 'H' ● Slice a string —> string[start:end] -Example: "Hello"[1:4] —> 'ell' (FIRST NUMBER IS INCLUSIVE AND SECOND NUMBER AFTER THE COLON IS EXCLUSIVE) ● Change case —> upper(), lower() Example: "hello".upper() —> "HELLO" ● Find substring —> find(substring) Example: "Hello".find("e") —> 1 ● Replace substring —> replace(old, new) Example: "Hello".replace("H", "J") —> "Jello" ● Check if substring exists —> in Example: "e" in "Hello" —> True ● Strip whitespace —-> strip() Example: " hello ".strip() ——> "hello"
● Helps make code easier to read, maintain, reuse and test. ● You can call it many times in your main program rather than rewriting it each time.