Programming Fundamental Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

Programming Fundamental Notes

Unit 1: What is program?


Summary:

Welcome to Programming Fundamentals. This course introduces fundamental


concepts of computer science by exploring the discipline of computer programming.
Throughout this course you will be challenged to think differently. We have all
learned how to speak and write in a language. Many of us have had to learn more
than one language. Regardless of whether the language is English, Russian, Arabic,
Chinese, Thai, Spanish, Portuguese, or any number of other languages, they all have
common elements. Each language has structure, grammar, and vocabulary. As we
learn a new language we must learn to combine these elements together to
accomplish our goals, to convey the message and meaning that we need to convey. 

A computer programming language is no exception to this rule. Each programming


language has structure, grammar, and vocabulary that must be combined to
accomplish the goal of the program. In the spoken or written language, if we don’t
use structure, grammar, or vocabulary properly then we risk not getting our
message across. In a computer programming language, if we don’t use these
elements properly then we run the risk that our program will not function as we
expect it to, or it will not function at all.

In each subsequent unit of this course, we will be learning about the structure,
grammar, and vocabulary of the Python programming language. Python is an
interpreted language that is object-oriented. Python is actually a relatively recent
language in that it was first developed in 1991. Other programming languages, such
as Fortran, are much older. Fortran was developed by IBM in 1958. The BASIC
programming language was developed in 1964 and Cobol was developed in 1959.

Program development and the problem with insect infestation (BUGS!)

One item that is a part of this history is origin of the concept of a computer bug. As
you will learn in this unit, a computer bug is a defect in coding of a software program
that makes the program operate incorrectly or fail. The process of eliminating
problems or errors in a program is known as ‘debugging’.

 Chapter 1 - The Way of the Program


Program = A sequence of instructions + perform computation
on it

Input (Get
data)

Repetition
Output
(perform
(Display
repeated Type of data)
action)
Basic
Instructio
n

Conditional Math (+-


(if/else) operator)

print('Hello
World!')

print
statement

Display a
result on a function
screen

Addition +

XOR,bitwise
Subtraction -
operator ^

Arithmetic
Operator

Exponentiation Multiplification
** *

Divide /
Integer
(int)

Values &
Types

Floating-
point
String (str)
number
(Default)

Natural Language
Formal Language (Differences with
Formal)

Designed for specific


applications
i.e., Programming language full of ambiguity (with
multiple meaning)

Strict syntax rule (Govern


strcuture of statement)
valid tokens & structure
A lot of redundancy

This is @ well-structured
Engli$h sentence with invalid
ful of idiom & methaphor
t*kens in it. This sentence all
( literalness)
valid tokens has, but invalid
structure with.
structure of formal laguage

Debugging
Track down
programming error

Unit 2 Learning Outcome:

1. Examine the role of arguments and parameters in Python functions. Y


2. Integrate Python variables, operators, and expressions to do calculations. Y
3. Create Python functions that take arguments. Y
4. Compose Python programs using nested functions. 》》One conditional can also
be nested within another.

Unit 2 (Variables, Expressions, Statements, and Functions)


 Chapter 2 - Variables, expressions, and statements
 Chapter 3 – Functions

Summary:

Variables are simply places to store information and to give that information a


name. As the term indicates the information stored is "variable", meaning that it can
change.

Expressions are combinations of values, variables, and operators that the Python


interpreter evaluates to compute a resulting value.

Statements are units of code that have an effect, like creating a variable or


displaying a value. The Python interpreter executes statements to produce the
effect. (When executing a statement, the interpreter evaluates any expressions
included in the statement.)

Finally, functions are named sequences of statements that perform computations.


After you define a function, you can call it any number of times to have the Python
interpreter execute the statements in the function.

A critical advantage of variables, expressions, statements, and functions is that they


allow you to take small building blocks of functionality and compose them into
larger features.

Variable = Name of a value ( a = 50, a is the variable of value 50)

Assignment statement = A line of code create new variables & give it value
State Diagram Example ( show what state/value each variable is in)

Assignment
Statement

Can't use
Python Refer a value
keyword as to a name
variable name
Variable

Variable
Variable
name can't
name can't
contain illegal
begin with
character,
number
i.e., @

Python 3 keywords (displayed as orange color in coding IDE)

Unit 3 Learning Objective:

By the end of this Unit, you will be able to:

1. Differentiate between chained conditionals and nested conditionals.


2. Simplify nested conditionals using boolean expressions. “AND, OR, NOT”
3. Write a chained conditional.
4. Write a recursive function. - Function call itself till condition met & break loop
5. Create a program that uses keyboard input.
42
(value)

Expression(va
lue+variable+
operator)

n+25 (val
n
+varia +
(variable)
operator)

Python Mode

Interactive Script
Mode Mode
Interact directly with
Code is save in script
Python interpreter

No need print
Interpreter run in
statement/return
script mode to
statement to display
execute the script
result

Need print
statement/return
statement to display
result

Order of Operation (PEMDAS)


(1) P-
Parenthese
()

(4) A & S + Orde (2) E -


Exponential
&-
r **

(3) M&D *
&/

String Operation ( Only work for + & *)

Use # as comment in script

Function takes argument[value inside the parantheses] & returns a result [value]

Import Python built-in Module to use the functions in it (radian, exponential, sin, cos,
log,sqrt…):

i.e., : Import math

To use function of the module, use dot-notation “.” After module

I.e

.
math log10(100)

Function or Method
module Dot. Notation

Assignment statement: LHS must be variable!

Function Definition: Specify its name, argument-parameter & sequences of statement in it

 1st line is header [start with def & end with :] & the rest is body [Must be indented]
 To end the function, enter an empty line at the end
 Function is an object
 Variable defined inside function is local variable & can’t be detected / used outside
the function. The local variable will be destroyed after program halted inside
function.

Fruitful Functions Void Functions

Return Don't return a value


results/value Only perform an
i.e., math function action, i.e., print
statement.
Return 'None' value

Interactive Mode Script Mode

No need print Need print statement


statement to return to display / return
resulkt of a function result of function
>>> math.sqrt(5) >>> math.sqrt(5)
Output: Output: No output
2.2360679774997898 displayed
 Benefit of Function:
Benefits of Function

Make program easier to read and debug


Fragmentized code to smaller bodies
Can be useful for many programs & reuse

Unit 3 (Conditionals and Recursion)


Chapter 5 – Conditionals and recursion

This unit covers conditional execution using if statements, along with recursive


functions, which are functions that call themselves, typically with different
arguments.

Conditionals use boolean expressions, which have the value True or False, to


decide between alternative code paths.

Conditionals are used within recursive functions to avoid infinite recursion and


perform useful computations. Recursion is a powerful mechanism for iterative
computation, but it can be confusing. Stack diagrams can help you track and
understand what a recursive function is doing as it calls itself.

Other important topics covered in this unit include keyboard input and the integer
operators for floor division (//) and modulus (%).

Floor Division // Modulus %

Divide 2 nums % round down to an integer Divide 2 nos. and return the remainder

i.e. 105//60 = 1 105 % 60 = 45

If x % y = 0, mean x is divisible by y
Boolean Expression (True / False)

Relational Operator Meaning


!= Not equal
> Greater
< Less than
>= Greater or Equal to
<= Less than or Equal to
== Equal to

and

Logical
Operator

not or
** Python is not strict, any non-zero number is interpreted as True when without the logical
operator and operands.

Compound statement:

 A header with indented body


 i.e., if statement

Condition Execution: -Ability to check conditions & change the behaviour of program.

**Use pass statement which does nothing.

If x <0:

pass #pass statement

Alternative Execution:

 When there are two possibilities and conditions


 Called as branches
 else statement

Chained Conditionals:
 For more than 2 possibilities which need more than 2 branchoes
 elif statement (else if)

Nested Conditionals:

 A conditional nested within another


 Difficult to read
 Example of nested conditional:

if x == y:

print('x and y are equal')

else:

if x < y:

print('x is less than y')

else:

print('x is greater than y')

Recursion :

 A function call itself


 Must set a base case for it to exit the loop eventually (i.e. if n<=0), if not will be
infinite recursion & cause runtime error after 1000 th loop
 Example countdown(n) calling countdown(n-1) in itself

def countdown(n):

if n <= 0:

print('Blastoff!

else:

print(n)

countdown(n-1)

keyboard input (user input):

 In-built function “input” enable program to stop & wait for user to input/type sth
before continuing
 For example: name = input('What...is your name?\n')
Recursion

Condition
Nested
Exec. (if)
Unit
3

Chained Alternative
Exec. Exec. (else)
(elseif)

Unit 4 (Functions and Return Values)


Chapter 6 – Fruitful Functions

Summary:

A function is a segment of code that performs a specific series of instructions. The


function is "called" or executed from within other programs. As part of executing the
function, data can be passed to the function as a series of arguments. Functions can
also return limited information by returning a value back to the calling program,
a return value.

This unit covers techniques for using functions with return values to deal with
complexity. Boolean functions can be used to encapsulate complex tests, where the
functions can be used in conditional statements. Composition is the technique
where a function builds up a complex return value by calling simpler functions. The
idea of composition can be applied to recursion to perform interesting
computations.

Functions are particularly helpful with incremental development, an important


strategy for dealing with complexity. By adding and testing a small amount of code
at a time, a programmer can more easily debug the new features.

Temporary variable:

 Variable created to represent an expression in a function


 Make debugging easier as code is fragmentized

Dead Code:

 Code after a return statement or any other place the flow of execution can
never reach

Absolute value:

 Python built-in function ‘abs’


Incremental Development:

 To deal with large program code and ease debugging process


 Method is adding and testing only a small amount of code at a time &
return result from each part of code

Boolean Functions:

 Eg. 1 : if is_divisible(x, y):

print('x is divisible by y')

 Eg. 2 : if is_divisible(x, y) == True: #==True no need to be written, it is same


as Eg.1

print('x is divisible by y')

Leap of Faith:

 When you use built-in function without knowing its flow of execution

Checking Types

 (n, int) -> this code

Unit 5 (Iteration and Strings)

 Chapter 7 - Iteration
 Chapter 8 - Strings

This unit introduces iteration with while loops and for loops, which are often


simpler to write and understand than recursive functions.  It may seem arbitrary
that strings would be covered in the same unit, but it turns out that operations on
strings often require iteration.

Iteration is a strategy often used in algorithms, which are general processes for
solving categories of problems. One algorithm presented in this unit, for example,
searches a string for a particular letter.

In addition to looping through strings, other string functionality covered in the unit
includes string slices, which specify parts of strings, and string methods, which are
useful functions that are associated with a string variable.

Reassignment:

 Assign a new value to an existing variable to replace its old value


 Also known as update
 Example:
1. X = 5
2. X = 10 #this is reassignment
 Python evaluate RHS of expression before it assigns variable on LHS, so..:
1. x = x + 1 > this will cause error as variable appears on RHS
2. 7 = x > cause error as integer at LHS & variable at RHS

Break statement:

 To jump out of the loop


 Example:

while True:

line = input('> ')

if line == 'done':

break #break statement

print(line

While
Statement

Iteration
Recursion For loop

While statement

 Use ‘while’ keyword


 Must have a statement to update the value of iterator
while n > 0:
print(n)
n = n – 1 # update the value of iterator

Algorithms:

 Mechanical process [automation] to solve/iterate value such as Newton’s method


below:
while True:
print(x)
y = (x + a/x) / 2
if y == x:
break
x=y

To include chap 8

Introduction to String & its Index:

 String is a sequence of characters


 Characters in the string can be accessed by expression bracket called index []

>>> fruit = 'banana'

>>> letter = fruit[1]

>>> letter

'a'

 the value of the index has to be an integer. Otherwise you get:

>>> letter = fruit[1.5]

TypeError: string indices must be integers


le n (b u ilt-in fu n cti o n )

Built-in Function len:

returns quantity of
characters in a string, i.e.,
len('banana') = 6

can used to return last


character in the string as well
, last = fruit[len(fruit)-1]

Traversal of A String with Loop (While & For Statement):

 Select each character in the string and perform action till the end of the string

While Loop For Loop


index = 0 for letter in fruit:
while index < len(fruit): print(letter)
letter = fruit[index]
print(letter)
index = index + 1
 loop condition is index <  Print out each character in the
len(fruit), so when index is equal string
to the length of the string, the
condition is false, and the body of
the loop doesn’t run.
 The last character accessed is the
one with the index len(fruit)-1,

Unit 6 (Lists)
Chapter 10 – Lists

This unit covers Python lists, which have similarities and differences with strings. Like
strings, lists are sequences, they can be traversed with for loops, they have a slice
operator (“[]”), and they have many useful methods. Unlike strings, lists can store
more than just characters, including other lists. And lists are mutable; the elements
in a list can change. 

This mutability of lists illustrates some advanced concepts in Python,


including objects, references, and aliasing. These concepts are closely related, and
they can be confusing. They all come together to explain the behavior of list
arguments passed to functions. The list parameter inside the function can be used to
modify the contents of the list, where these modifications are visible after the
function executes. Reassigning the parameter, however, keeps further list operations
from affecting the original argument. Section 10.13 from the textbook (Debugging)
deserves careful attention to understand this behavior and avoid confusion.

The textbook uses lists to illustrate even more useful concepts,


including augmented assignment operators like “+=”, reduction operations like
“sum”, map operations that modify all the elements of a list, and filter operations
that select out elements from lists. 

List:

 One of Python most useful built-ins


 Contains a sequence of values which are called elements or values
 Elements are enclosed by [] in the list
 i.e. : [10, 20, 30, 40]
 Example of nested list : ['spam', 2.0, 5, [10, 20]] where a list is nested inside another
list
 A list with no elements is empty list
 List is mutable, for example:

>>> numbers = [42, 123]

>>> numbers[1] = 5
>>> numbers

[42, 5]

 1 is the index in numbers [1], meaning 2 nd element inside the list ‘numbers’, for -1
index, it means the last element inside the list, -2 is the 2 nd last element & so forth.

Traverse a list (to iterate/read through each element in the list) – Using For Loop

 To read & print each element in the list ‘numbers’:

for number in numbers:

print (number)

 To update the element inside a list, utiliae the index:

for i in range(len(numberts)): # i starts from zero 0 th to (n-1)th

numbers[i] = numbers[i] * 2 # each of the element is multiple by two, hence all


elements in the list are updated

 Length of ['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]] is 4. [1,2,3] also
counted as 1 element as it is a nested list

List Operation:

+ operator * operator

combine 2 lists into a repeat the list by nth


single list time
>>> a = [1, 2, 3] >>> [0] * 4
>>> b = [4, 5, 6] [0, 0, 0, 0]
>>> c = a + b >>> [1, 2, 3] * 3
>>> c [1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 4, 5, 6]

List slices – Modify length / extract certain part of the list

 Similar to slicer for string slices:


 Example:

t = ['a', 'b', 'c', 'd', 'e', 'f']

>>> t[1:3] # display 2nd to before 4th element

['b', 'c']

>>> t[:4] # display from start to before 5th element

['a', 'b', 'c', 'd']


>>> t[3:] # display 4th to last element

['d', 'e', 'f']

>>> t[:] # display first to last element

['a', 'b', 'c', 'd', 'e', 'f']

 To update elements inside list using slicer

>>> t[1:3] = ['x', 'y'] #Update 2nd to before 4th elements, assign new value on RHS to
replace

>>> t

['a', 'x', 'y', 'd', 'e', 'f']

List methods – activate using dot notation “.”

append

List
method

sort extend

 Examples of list method:

(i) append – add new element at the back of list

>>> t = ['a', 'b', 'c']

>>> t.append('d')

>>> t

['a', 'b', 'c', 'd']

(ii) extend – add on elements in one list to another

>>> t1 = ['a', 'b', 'c']

>>> t2 = ['d', 'e']


>>> t1.extend(t2)

>>> t1

['a', 'b', 'c', 'd', 'e']

(iii) sort – arrange elements from low to high or alphabetical (return None if you try to
modify the list)

>>> t = ['d', 'c', 'e', 'b', 'a']

>>> t.sort()

>>> t

['a', 'b', 'c', 'd', 'e']

>>>t = t.sort() # Try to modify list ‘t’

None

List Operation:

Accumulator

Delete Reduce
List
Operation

Filter Map

(i) Accumulator: (total += x augmented statement to update/accumulate elements in a list)

total += x is equivalent to total = total + x

def add_all(t):

total = 0

for x in t:

total += x

return total

** As loop runs, total add up the sum of the elements, this is called accumulator
(ii) Reduce (reduce the elements into lesser entries)

>>> t = [1, 2, 3]

>>> sum(t)

** sum function in this case is a reduce operation as it combine the no. of elements from 3
to 1

(iii) map – map a function capitalize_all onto each of the elements in a sequence

def capitalize_all(t):

res = []

for s in t:

res.append(s.capitalize())

return res

(iv) filter – select only some elements on the list and return a sublist

i.e. take a lists of strings and only returns a list that contains only uppercase strings

def only_upper(t):

res = []

for s in t:

if s.isupper(): #isupper() is a filter as it only accept certain


elements

res.append(s)

return res

(v) Delete ( 3 method to delete/extract element in the list:

a) del operator (completely remove value, can return back like .pop() method)
 >>> t = ['a', 'b', 'c']
>>> del t[1]
>>> t
['a', 'c']
 Can remove more than 1 element as well using slice index:
i. >>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> t
['a', 'f']
b) .remove() method – if you know the element but don’t know its position/index
 >>> t = ['a', 'b', 'c']
>>> t.remove('b') #remove element, no need specify its location in the
list
>>> t
['a', 'c']
c) .pop method
 pop modifies the list and returns the element that was removed. If you
don’t provide an index, it deletes and returns the last element.
>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> t
['a', 'c']
>>> x #.pop store and return the removed value
'b'

** To convert/break a string to a list of characters, use list function:

>>> s = 'spam'

>>> t = list(s)

>>> t

['s', 'p', 'a', 'm']

Breaking / Joining list :

 Convert/break a string to a list of characters, use list function:


>>> s = 'spam'
>>> t = list(s)
>>> t
['s', 'p', 'a', 'm']
 Breaks a string into individual characters or use delimiter, .split method

>>> s = 'pining for the fjords'

>>> t = s.split() #empty argument, split at spaces

>>> t

['pining', 'for', 'the', 'fjords']

Or

>>> s = 'spam-spam-spam'

>>> delimiter = '-'

>>> t = s.split(delimiter) #if specify argument, then for this case will split at “-“ character

>>> t

['spam', 'spam', 'spam']

 Join method is inverse of split method, it concatenate list of string together:

>>> t = ['pining', 'for', 'the', 'fjords']

>>> delimiter = ' '

>>> s = delimiter.join(t)
>>> s

'pining for the fjords'

Objects and Values:

 String of different variable but with same character will always refer to same object
 Use is operator to check whether variables are referring to the same object:

>>> a = 'banana' #string 1

>>> b = 'banana' #string 2, character same as string 1

>>> a is b

True # is operator returns True, means that both string variable A & B refers to the same
object.

** We can say object ‘banana’ is aliased, as this object has more than 1 reference (variables)
connected to it, as shown in stack diagram below:

* * Avoid aliasing with mutable object like list etc. as it is error prone.

>>> a = [1, 2, 3]

>>> b = [1, 2, 3]

>>> a is b

False # List 1 and List 2 are referring to different objects although they are containing the
same element. This prove that list will always create a unique object every time a list is
created without cross-reference to other list variable available like what string variable is
doing.

List argument:

 Determine whether the list is modified or a new line list is created when a list is
passed to a function

del function List slicer append method


Original list outside New list is created. Ori Original list New list is
function got list outside function outside function created. Ori
modified not affected got modified list outside
function not
affected
def delete_head(t): def >>> t1 = [1, 2] t1=
del t[0] bad_delete_head(t): >>> t2 = [1, 2, 3]
>>> letters = ['a', 'b', t = t[1:] t1.append(3) >>> t3 = t1 +
'c'] >>> t4 = [1, 2, 3] >>> t1 [4]
>>> >>> [1, 2, 3] >>> t1
delete_head(letters) bad_delete_head(t4) >>> t2 [1, 2, 3]
>>> letters >>> t4 None >>> t3
['b', 'c'] [1, 2, 3] [1, 2, 3, 4]

len(a)~Calc
length of
string
eval(a) -
calculate a.split('-') ~
math Delimit string
expression in between "-"
string

String
Function
a.replace("-",
"*") ~
a[5] ~Extract
Replace -
4th character
with * in
string
a.find("*") ~
Find
character "*"
in string

String slice:

 Slice is a segment/a part of string


 String is immutable, can’t change the character of ori, only can produce another
copy using new variable assignment

>>> greeting = 'Hello, world!'

>>> greeting[0] = 'J'

TypeError: 'str' object does not support item assignment


fruit[:3] >
fruit[3:] >'ana'
'ban'

>>> s[0:5] >


'Monty' &
fruit[3:3] > ''
fruit = String Slice
'banana' (s =
'Monty
Python')

Generic Useful Function for String:

Search character in string Counter


def find(word, letter): word = 'banana'
index = 0 count = 0
while index < len(word): #traverse word for letter in word: #traverse word
if word[index] == letter: #find if letter == 'a': #find char. ‘a’
similarity count = count + 1 #act as counter
return index #return the position print(count) #traverse all the character &
index = index + 1 return count of the specific character
return -1 #if not found return -1
Find the position of character in word string Count how many character “a” in the string
which tally with the letter ‘banana’

String Method:

upper() – take a string & returns a new find() – find location/index of the character
string with all uppercase letters in string
>>> word = 'banana' >>> word = 'banana'
>>> new_word = word.upper() >>> index = word.find('a')
>>> new_word >>> index
'BANANA' 1
>>> name = 'bob'
>>> name.find('b', 1, 2) # 2nd argument “1”
specify where search should start & 3rd
argument “2” the index before find()
should stop
-1 (result return is -1 as character b not
found between index 1 &2 not found
in operator:

 Boolean operator – return True / False


 def in_both(word1, word2):
for letter in word1: #traverse each character in word1
if letter in word2: #traverse each character in word2
print(letter) #print all the letter from word1 which appear in word2

String Comparison:

 All the uppercase letters come before all the lowercase letters, A common way to
address this problem is to convert strings to a standard format, such as all
lowercase, before performing the comparison

Unit 7 (Data and Data Structures)

 Chapter 11 – Dictionaries
 Chapter 12 – Tuples
 Chapter 13 - Case study: Data structure selection (Supplement)

Topics:

 A dictionary is a mapping
 Looping and dictionaries
 Reverse lookup
 Dictionaries and lists
 Global variables
 Tuples are immutable
 Tuple assignment
 Tuples as return values
 Lists and tuples
 Dictionaries as tuples

In this unit, you will study two powerful data structures in Python,
the dictionary and the tuple. You will learn how to use each of these types, along
with how to use them together.

Dictionaries (which are implemented as objects) provide a simple way to store data
and retrieve it using an index key.  Dictionaries are based on key-value pairs.
The key identifies the data and can be used to store, retrieve, and update the data. It
must be immutable.

The value part of the pair is the actual data, and it can be any type, including a list or
even another dictionary. An example use of a dictionary might be to store the
number of days in a month and retrieve it using the name of the month. For January,
the key would be “January”, and the value would be 31.

Tuples are sequences of values, like lists. Unlike lists and dictionaries, tuples
are immutable. Thus tuples can be keys in a dictionary. Tuples are useful for
assigning multiple values at once, for iterating through multiple lists at once, and for
iterating through the key-value pairs in a dictionary.

Dictionary Intro:
 Like a list but its indices are known as keys & can be any value type (indices for list
must be integers)
 Contain key-value pair with {} or known as item: {'one': 'uno'} ~ {'key': 'value'}
 eng2sp = dict() #making variable eng2sp become dictionary type data using dict()
fxn
 similar to list, to call out the value in dict, use [index]
 >>> eng2sp #define dictionary
{'one': 'uno', 'three': 'tres', 'two': 'dos'}
 >>> eng2sp['two'] #extract value of key ‘two’
'dos'

Checking Key & value in Dictionary:

Check key in dict Check value in dict


Use in operator to check whether the key Use values method to check whether a
exist in the dictionary, to avoid KeyError: value is inside the dictionary
>>> 'one' in eng2sp >>> vals = eng2sp.values()
True >>> 'uno' in vals
>>> 'uno' in eng2sp #uno is value in True
dict
False
*Hashtable algorithm employed in Python,
so no matter how big the dictionary,
computation time is same for all

3 ways to count how many times a letter (A~Z) appears:

String method List method Dictionary method

create 26 variables (A~Z) & Create a list with 26 create a dictionary with
traverse the string elements, convert each characters as keys and
character to a number (using counters as the
the built-in function ord), use corresponding values. The
the number as an index into first time you see a character,
the list, and increment the you would add an item to the
appropriate counter. dictionary. After that you
would increment the value of
an existing item.
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d

** get method for dictionary, capture value corresponding to its key:

>>> h = histogram('a')
>>> h
{'a': 1}
>>> h.get('a', 0)
1
Looping key & value pairs in dictionaries:

 For statement traverse keys of dictionary # Not value


 To be able to explore value of dict, can use indices on dictionary, as shown below:
def print_hist(h):
for c in h:
print(c, h[c]) # Explore values of each key using indices notation []
>>> h = histogram('parrot')
>>> print_hist(h)
a1
p1
r2
t1
o1

Dictionary reverse look-up function (using value to look for key):


def reverse_lookup(d, v): #takes 2 arguments d (dictionary) & v (value?)
for k in d: #traverse key, k in dictionary,d
if d[k] == v: #conditional for dictionary[key] a.k.a. its corresponding value is equal to v
return k #return key of dictionary
raise LookupError() #if unable to find value of key that corresponds to v (lookup val),
then raise error
**raise statement is Python in-built function for exception, avoid function caught up in
error
** A reverse lookup is much slower than a forward lookup; if you have to do it often, or if
the dictionary gets big, the performance of your program will suffer.

Dictionaries & Lists :


 List can be stored as value in dictionary, but cannot be keys as it will cause error >
“TypeError: list objects are unhashable”
 Hash is a function that takes a key and returns an integer which is called hash value.
Dictionary use this integer to store and look up key-value pair.
 Only work if keys is immutable. If key is mutable like list, errors occur. Python will
hash the key and store it in corresponding location when Key-value pair is created.
When key is modified and hashed again, it would go to different location causing 2
entries with the same key.
 Both dictionary and list are mutable, hence they can use as value only and not key.
Only tuple can be key of dictionaries
 Invert a dictionary:
def invert_dict(d):
inverse = dict() #define data type for inverse variable as dictionary
for key in d: #traverse key in dictionary
val = d[key] #define val variable as value of dictionary using indices
if val not in inverse: #if value not part of the keys in dictionary inverse
inverse[val] = [key] #inverse value & key and add it into dictionary
else:
inverse[val].append(key) #we have seen this value before, so we append the
corresponding key to the list.
return inverse

Global variable:
 Variable created outside function belongs to the special frame called _ _main_ _ ,
hence called global variable.
 Even if global and local variable with same name has same value, value of global
function won’t be affected. Local function will be deleted once function ends.
Tuple Introduction:

 Tuples are immutable


 Enclose in ()
 Create a tuple using built-in function tuple:
 >>> t = tuple()
>>> t
()
 >>> t = tuple('lupins')
>>> t
('l', 'u', 'p', 'i', 'n', 's')
 Bracket operator indexes an element:
 >>> t = ('a', 'b', 'c', 'd', 'e')
>>> t[0]
'a'
 Elements in a tuple can’t be modified but can be replaced by another Tuple as
shown below:

>>> t[0] = 'A' #Trying to modify 1st element of tuple, error

TypeError: object doesn't support item assignment

>>> t = ('A',) + t[1:] #Replace the 1st element of tuple with another tupls (‘A’), No error

>>> t

('A', 'b', 'c', 'd', 'e')

Tuple Assignment:

 To swap values of 2 variables, use tuple assignment as follow:


 >>> a, b = b, a
 LHS = tuple of variables; RHS = tuple of expression

Tuple as return values:

 In normal condition, a function can only return one value.


 we can use tuple to return multiple values in ()

List & Tuples – Useful Built-in functions for Tuple:


Zip built in function - iterator Enumerate built in function
Takes 2 or more sequences & return a list Traverse elements of a sequence & their
of tuples indices. Return index (start at 0) for each
element.
(i) example zips a string and a list: for index, element in enumerate('abc'):
>>> s = 'abc' print(index, element)
>>> t = [0, 1, 2]
>>> zip(s, t) Output:
<zip object at 0x7f7d0a9e7c48> 0a
>>> list(zip(s, t)) 1b
[('a', 0), ('b', 1), ('c', 2)] 2c

(ii) zip + for loop: - traverse a tuple


for letter, number in t:
print(number, letter)

0a
1b
2c

(iii) power combination of zip+tuple+for


loop:
def has_match(t1, t2): #take 2 seqs. t1 & t2
for x, y in zip(t1, t2): #traverse 2 seqs
if x == y: #if item in t1 = t2, return True
return True
return False

Dictionaries & Tuples:

Items method dict + zip function Update method


Returns a seq. of tuples from Form a dictionary from two Take a list of tuples & add
a dictionary sequences them as key-value pairs to
existing dictionary
>>> d = {'a':0, 'b':1, 'c':2} >>> d = dict(zip('abc', … pending eg. From Polim
>>> t = d.items() range(3)))
>>> t >>> d
dict_items([('c', 2), ('a', 0), {'a': 0, 'c': 2, 'b': 1}
('b', 1)])
Another way round, we can
use a list of tuples to form a
new dictionary:
>>> t = [('a', 0), ('c', 2), ('b', 1)]
>>> d = dict(t)
>>> d
{'a': 0, 'c': 2, 'b': 1}
Sequences of sequences:

 List of tuples[()] , list of lists [[]] , tuples of tuples (()) , tuples of lists ([])
 Comparing tuple vs list vs dictionary

Aspect Tuple List Dictionary


Return statement Easier to create Harder --
Dictionary key Yes, immutable No, mutable No, mutable
Aliasing error Nt prone, immutable Prone, mutable Prone. Mutable
Computing speed Consistent Proportional to file -
size
**Compare and see different to create list of tuple & list of list using return statement -
Polim

Unit 8 File

 Chapter 9 – Case study: word play, Section 1


 Chapter 14 – Files, Sections 1-5
 Chapter 14 – Files, remaining sections (Supplemental Reading)

Topics:

 Reading and writing files


 Format operator
 Filenames and paths
 Catching exceptions

This unit introduces reading and writing text files in Python. In particular, you will
learn how to read the lines of a text file using a for loop and how to write lines one
at a time. To help with writing, the unit describes an important new use of the “%”
operator, to format text output.

You will also learn how to find files on a computer, along with how to put new files
where you want them, using functions from the os and os.path modules. These
functions navigate the directories (also known as “folders”) of the file system.

Finally, the unit covers how to handle file errors by catching exceptions. You will
learn about try blocks, which allow you to call functions that might raise exceptions
and “catch” those exceptions to deal with the problem.

Built-in function for file object:


fin =
open('wor
ds.txt')

search file fin.readlin


pattern e()
object

method
.strip()

1. create a file object, fin associate with a text file named words.txt:
 fin = open('words.txt')
2. readline() method to extract line content in words.txt file
 >>> fin.readline()
3. strip () method ?? to be further explore
4. Combination of for loop + file object to read & print each word in every line of
Python:
 fin = open (‘words.txt’)
for line in fin:
word = line.strip()
print (word)
 For loop can be utilise as search pattern too for searching letters & etc..

def has_no_e(word):

for letter in word:

if letter == 'e':

return False

return True

**If we find the letter “e”, we can immediately return False; otherwise we have to go to the
next letter. If we exit the loop normally, that means we didn’t find an “e”, so we return True.

Looping with indices:

-Before this we are just reading the line from text file, to get into the characters in the string.

-Example of accessing characters inside the string:

For loop:
def is_abecedarian(word):

previous = word[0]

for c in word:

if c < previous:

return False

previous = c

return True

While statement:
def is_abecedarian(word):

i=0

while i < len(word)-1:

if word[i+1] < word[i]:

return False

i = i+1

return True

Stack Diagrams
 Shows value of each variable and the function each variable belongs to (Showing
relationship)
 Similar to traceback function which trace the type and source of error in a program
code:

Overall Debugging
3 kinds of error to be debug
Syntax Error
structure & rule
violated
Appear Before
program run

Erro
r
Semantic Error Runtime Error
Program able to run Appear After
Result is not correct program run

How to debug?

 What kind of error? Syntax or runtime?


 Where it occurs? Which line of the program code?

3 type of errors for incremental development code:

Violate
Pre-
condition

Fragmentized
Code Error

Wrong
Violated
usage of
Post
return
condition
value

Ways to debug fragmentized code:

i. Add print statement


a. at beginning of function to check value & type of parameter
b. Before each return statement to display the return value via print statement

Debugging Lists & Other Mutable Objects:

Common Debug/Avoid
Mistake Method
List is diff. frm string, it will be modified instead
t = t.sort() # WRONG! of returning a new object like string. It will
return None.

There are too many ways to do similar things


with list object i.e., pop, remove, del, or even a
Pick an idoim & stick with it. Fully understand
slice assignment for removal of element in list,
what each function/method is doing
append method or the +operator for adding
element.

Make copies to avoid aliasing. Keep original


and create a new variable to copy the modified
Aliasing mutable object may cause error
list. i.e., use t2 = sorted(t) to store sorted
variable t
Debugging for big database:
Scale down input (do not Check summaries &
include evaluation of type((i.e., printing
unnecessary column/row, summary and data type)
set last row/column)
Debug Big
Database

Write self-checks (i.e., set Format the output (more


data input cant be float human interface for
type..) interaction)

Data Structure Error and Debugging:

 Shape error due to data structure of wrong type, size or structure


 To debug, use structshape module to check data type, size & structure:

s tructs ha p e -p yM
o d ule .txt

Files:

Transient vs Persistent Program:

Transient Program Persistent Program


Temporary storage Permanent storage
Example: function Example: OS, web server, file

Reading & Writing:

(i) write a file using open function with mode ‘w’ write:

>>> fout = open ('output.txt', 'w')

(ii) use write method to put data into the file, argument of write must be string type:

>>> line1 = "This here's the wattle,\n"

>>> fout.write(line1)

(iii) close the file

>>> fout.close()
Format operator – to specify the type/format of the data using %:

** can be specified anywhere

 %d – format as integer

>>> camels = 42

>>> '%d' % camels

>>> 'I have spotted %d camels.' % camels

'I have spotted 42 camels.'

'42'

 %g – format as floating-pts number


 %s – format as a string

'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')

'In 3 years I have spotted 0.1 camels.'

Filename & paths:

 Files are organized into directories a.k.a. folders


 Every running program has a current directory, i.e. when open a file for reading
through Python, it looks for it in current directory
 To get name of current directory:

>>> import os

>>> cwd = os.getcwd()

>>> cwd

'/home/dinsdale'

 absolute path – does not depends on current directory


 relative path depends on current directory, i.e. of relative path > memo.txt
os.path.abspath: - find absolute path to a file
os.path.join takes a directory and a file name & form complete
os.path.exists
os.path.isdir
os.listdir returns
-checks
check
a list
existance
whether
of the files/directory
it’s
of file/directory
a directory
os.path.isfile checks
pathwhether it’s a file.

>>> os.path.abspath('memo.txt')

'/home/dinsdale/memo.txt'

>>> os.path.exists('memo.txt')

True

>>> os.path.isdir('memo.txt')

False

>>> os.path.isdir('/home/dinsdale')

True

>>> os.listdir(cwd)

['music', 'photos', 'memo.txt']

def walk(dirname):

for name in os.listdir(dirname):

path = os.path.join(dirname, name)

if os.path.isfile(path):

print(path)

else:

walk(path

Catching Exceptions: - avoiding function caught up In error:


 use “try & except” statement

try:

fin = open('bad_file')

except:

print('Something went wrong.')

Glossary Unit 1
1. Problem Solving
 Process of formulating a problem, find solution & expressing it
2. High level language:
 Programming language (i.e., Python) which is easy for human to read &
understand
3. Low level language:
 Programming language which is design to be easy for computer to run (machine
language/assembly language)
4. Portability
 A property of a program that can run on more than one kind of computer
5. Interpreter
 A program that read another program & execute it (i.e., Python 3 read code in
PyCharm IDE & execute it)
6. Prompt
 Characters display by interpreter indicating it is ready to take input from user
(i.e., >>> in Python 3 command box)
7. Program
 A set of instructions that specifies a computation
8. Print Statement
 An instruction to display a value on screen.
9. Operator:
 Special symbol for simple computation i.e., mathematical (+ - * / ** ^)
10. Value
 Basic unit of data like number / string which program manipulates/use
11. Type
 A category of value (int/float/string)
12. Integer
 Value type representing whole number
13. Floating-point
 Value type represent numbers with fractional parts/decimals
14. String
 Value type represent sequence of character
15. Natural language
 Language that people speak that evolve naturally (i.e., human language)
16. Formal language
 Language that people design for specific purpose (i..e, programming language)
17. Token
 One of the basic elements of the syntactic structure of a program, analogous to
a word in natural language
18. Syntax
 Rule that govern structure of program (i.e., parentheses () to use print
statement)
19. Parse
 To examine a program & analyse structure of program
20. Bug
 Error in program
21. Debugging
 Process of finding & correcting bugs

Glossary Unit 2
22. Variable
 Name that refer to a value (i.e., Nick)
23. Assignment
 Statement that assign a value to a variable (i.e., Nick = 10)
24. State diagram
 Diagram showing relationship between variables and their values

25. Keyword
 Word reserved by Python (i.e., print, if, def) that is used to parse a program
26. Operand
 Value which an operator operates on i.e. 2 in 2+1
27. Expression
 A combination of variables, operators & values that represent a single result.
28. Evaluate
 Simplify an expression by performing operation to yield a single value
29. Statement
 A section of code represents a command or an action.
30. Execute
 To run a statement & do what It says
31. Interactive mode
 Mode of typing code at prompt in Python interpreter
32. Script mode
 Mode of using Python interpreter to read code from script & run it.
33. Script
 A program stored in a file
34. Order of operations
 Rules governing the priority / order in which expressions to be evaluated 1st
35. Concatenate
 To join 2 operand end to end (i.e. A+pig = Apig)
36. Comment
 Information in a program that is meant for other programmers (or anyone
reading the source code) and has no effect on the execution of the program.
37. Syntax Error:
 An error in program that makes it impossible to parse (and therefore impossible
to interpret
38. Exception:
 An error that is detected while program is running
39. Semantics
 The meaning of a program
40. Semantic error:
 An error in program to make it do something other than what the programmer
intended
41. Function:
 A named sequence of statements that perform sth useful operation. It
ma/maynot take argument/input or may/may not produce a result.
42. Function definition:
 A statement that creates a new function by specifying its name, parameters &
the statements
43. Function object:
 The name of the function defined is a variable that refers to a function object.
44. Header:
 The 1st line of a function definition
45. Body:
 Sequence of statements inside a function definition
46. Parameter:
 A name used inside a function to refer to value passed as an argument
47. Function call:
 A statement that runs a function with argument list in function’s parentheses.
48. Argument
 A value provided to function when it is called which then assigned to
corresponding parameter in the function.
49. Local variable
 A variable defined inside a function and can only be used inside function.
50. Return value
 The result of function / value of the expression
51. Module:
 A file that contains a collection of related functions and other definitions
52. Import statement:
 A statement that reads a module file and creates a module object.
53. Module object:
 A value created by an import statement that provides access to the values
defined in a module.
54. Dot notation:
 Syntax to call a function in another module by specifying the module name
followed by a dot then the function name.
55. Composition
 Using an expression as part of a larger expression or a statement as a part of a
larger statement
56. Flow execution
 The order statements run in.
57. Stack diagram
 A graphical representation of a stack of functions, their variables and the values
they refer to.
58. Frame
 A box in a stack diagram that represents a function call. It contains the local
variables and parameter of the function.
59. traceback
 A list of the functions that are executing, printed when an exception occurs

Glossary Unit 3
60. Floor division
 An operator // divides 2 numbers then round them down to an integer.
61. Modulus operator
 % sign operator return the remainder after dividing two integer
62. Boolean expression
 Expression with either True or False value
63. Relational operator
 Operator that compare its operand with value
64. Logical operator
 Operator that combines Boolean expressions (and , or , not)
65. Conditional statement:
 Statement that control flow of execution to which branches depends on the
condition set.
66. Condition:
 Boolean expression in conditional statement that determine which branches to
run
67. Compound statement
 A statement that consists of a header and a body with its header ending with
colon “:” with indented body
68. Branch
 One of the alternative sequences of statement in conditional statement
69. Chained conditional:
 Conditional statement with a series of alternative branches
70. Nested conditional:
 A conditional statement that appear in the body of another conditional
statement
71. Return statement
 A function that cause a function to end immediately and return to the caller

72. Recursion
 Process of calling the function that is currently executing
73. Base case
 A conditional branch inside recursive function that does not make recursive call
74. Infinite recursion
 A recursion without base case which will cause runtime error eventually.

Glossary Unit 4
75. Temporary variable
 A variable used to store an intermediate value in a complex calculation
76. Dead code
 Part of a program that can never run, often because it appears after a return
statement
77. Incremental development
 A program development plan intended to avoid debugging by adding and testing
only a small amount of code at a time.
78. Scaffolding
 Code that is used during program development but is not part of the final
version (will be removed during final stage when code is mature)
79. Guardian
 A programming pattern that uses a conditional statement to check for and
handle circumstances that might cause an error

Glossary Unit 5
80. Reassignment:
 Assigning new value to existing variable
81. Update
 Assignment that new value of the variable depends on the old
82. Initialization
 Assignment that gives an initial value to a variable that will be updated.
83. Increment
 An update that increases the value of a variable (often by one)
84. Decrement
 An update that decrease the value of a variable
85. Iteration
 Repeated execution of a set of statements using either recursive function call or
a loop
86. Infinite loop
 A loop which terminating condition is never satisfied
87. Algorithm
 A general process for solving a category of problems
88. Object
 Something a variable can refer to.
89. Sequence
 An ordered collection of values where each value is identified by unique integer
index.
90. Item
 One of the values in sequence
91. Index
 An integer value used to select an item in a sequence, such as a character in a
string. In Python, indices start from 0.
92. Slice
 A part of a string specified by a range of indices
93. Empty string
 A string with no characters and length 0, represented by two quotation marks
94. Immutable
 Property of a sequence whose items cannot be changed
95. Traverse
 To iterate through the items in a sequence, performing a similar operation on
each
96. Search
 A pattern of traversal that stops when it finds what it is looking for.
97. Counter
 A variable used to count something, usually initialized to zero and then
incremented
98. Invocation
 A statement that calls a method
99. Optional argument
 A function/method argument that is not required.

Glossary Unit 6
100. List
 A sequence of values
101. Element
 One of the values in the list (or other sequence), also known as items
102. Nested list
 A list contained in another list
103. Accumulator
 A variable used in a loop to add up / accumulate a result (i.e., x +=2, accumulate
2)
104. Augmented Assignment:
 A statement that update the value of a variable using an operator like +=
105. Reduce:
 A processing pattern that traverses a sequence and accumulates the elements
into a single result (i.e., [1,2,3] ~ {6}
106. Map:
 A processing pattern that traverses a sequence and performs an operation on
each element
107. Filter
 A processing pattern that traverse a sequence and select the elements that
satisfy some criterion
108. Object
 Something a variable can refer to. An object has a type (string/int/float..) and
value
109. Equivalent
 Having same value
110. Reference:
 Association between a variable and its value
111. Aliasing
 A circumstances where two or more variables refer to the same object
112. Delimiter
 A character or string used to indicate where a string should be split.

Glossary Unit 7
113. Mapping
 Relationship where element of one set corresponds to an element of another
set
114. Dictionary
 A mapping of key to its values
115. Key-value pair
 Representation of the mapping of key to its value
116. Item
 Another name for key-value pair
117. Value
 An object appears in a dictionary as the second part of a key-value pair
118. Implementation
 A way of performing computation
119. Hashable
 A type that has hash function. Immutable type like integers, floats and strings
are hashable; mutable type like dictionary and lists are not.
120. Hash function
 A function used by hashable to compute location for a key
121. Lookup
 A dictionary operation that takes a key and finds the corresponding value
122. Reverse-lookup
 A dictionary operation that takes a value and finds one or more keys that map to
it
123. Raise statement
 A statement that raises an exception
124. Singleton
 A list (or other sequence) with a single statement
125. Call graph
 A diagram that shows every frame created during the execution of a program,
with an arrow from each caller to each callee
126. Memo
 A computed value stored to avoid unnecessary future computation
127. Global variable
 A variable defined outside a function and can be accessed from any function
128. Global statement
 A statement that declares a variable name global
129. Flag
 A Boolean variable used to indicate whether a condition is true
130. Declaration
 A statement like global that tells the interpreter something about a variable
131. Tuple
 An immutable sequence of elements
132. Tuple assignment
 An assignment with a sequence on the RHS and a tuple of variables on LHS. The
RHS is evaluated and then its elements are assigned to the variables on the left.
133. Gather
 The operation of assembling a variable-length argument tuple
134. Scatter
 The operation of treating a sequence as a list of arguments
135. Zip object
 The result of calling built-in function zip, an object that iterates through a
sequence of tuples
136. Iterator
 An object can iterate through a sequence but which does not provide list
operators and methods
137. Data structure
 A collection of related values, often organized in lists, dictionaries, tuples and
etc..
138. Shape error
 An error caused by a value having the wrong shape, that is wrong type or size

You might also like