Skip to document

Revision Bootcamp Notes

Essential concepts revision notes
Course

Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Academic year: 2020/2021
Uploaded by:
0followers
3Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

BASIC PROGRAMMING

Theory/Terminology

-Program – “a set of instructions given to a computer corresponding to an

algorithm to solve a problem”

(see first lecture notes)

-Program Structure

Input

X=eval(input(“Enter a number:”))

Files

Arrays

Processing

Loops, calculations etc

Output

Print()

Files

First principles

Built-in Python function vs First principles

e Sentence is “Hi my name is Sani”

Sentence=input(“Enter a sentence”)

Reverse=Sentence[::-1]

Print(Reverse)

For k in range(len(Sentence),-1):

Character = sentence[k];

Print(Character,end=””)

Precedence of operators

()

/,//,*,%

+-

For k in range(5):

M=(((m%2)+1)*4)

Data Types

Strings

Integer

Floating Point Values

Boolean

Identifiers

X=”my name is Sani”

Literal

Escape Sequences:

\n – newline

\t – tab

Print(‘I\’m happy’)

Print(“I\”m happy”)

Print(“\”)

{pos1}{pos2}.format(“e”,”e”)

{0}{1}.format(“e.g”,”e.g”)

e.g e.

{1}{0}.format(“e.g”,”e.g”)

e.g e.

{0:<20}.format(“e.g”)

e.

{0:>20}.format(“e.g”)

e.

{0:^20}.format(“e.g”)

“ e.g “

String functions e. find(); lower() etc.

(see lecture slides)

SELECTION

x=

if (x>1):

print(x)

Boolean Values

Operators

<,>,==,!=,not,and,or

De Morgan’s Laws

Not(p or q)= not(p) and not(q)

Not(p and q)=not(p) or not(q)

e. if not((a>5) and (b<10)):

print(“something..”)

if not(a>5) or not(b<10):

print(“something..”)

ITERATION

For loop (we use it when we can pre-determine how many times we want the

code to repeat itself)

While loop ()

Answer= eval(input(“Enter your answer:”))

Correct_answer=

Counter=

While (answer != correct_answer):

Counter=counter+

Print(“your answer is incorrect. Try again”)

Answer=eval(input(“Enter your answer:”))

If (counter==10):

Break;

4

5

6

we dont want 8

7

9

10

Transitioning between the two types of loop

For k in range(5):

Print(k)

k=

While (k<5):

Print(k)

k=k+

ARRAYS

(Variable containers that store a group of related variables together)

literal “Tshepo

“Jordyn” “Sam” “Casey”

index 0 1 2 3

Length=

Indexing

For k in len(students):

Students[k]=Students[k]+”000”

List/array functions (see lecture slides)

2D arrays (an array of arrays)

2d[][]={ {“x”,”y”,”z”,”a”},{“b”,”p”,”o”,”m”} ,{“q”,”I”,”v”,”u”} ,{“r”,”t”,”b”,”c”} }

Col0 Col1 Col2 Col

Row0 x y

Row

Row

Row

for row in range(4):

for col in range(4):

2d[row][col]=......

FUNCTIONS

Help Code

BirthdayFunction

Def birthday(ID):

“””A function that returns a birthday based on a user’s ID“””

.......

.......

Return .....

If (name=”__main__”):

Print(birthday(“002938383883”))

In a separate program:

Import BirthdayFunction

Print(BirthdayFunction(“012838438384”))

Print(BirthdayFunction.doc)

Structure

TESTING

Types of Errors

Syntax

Compile-time errors

Run-time errors

Logic errors

Types of Testing

If (a>5):

Print(“a is greater”)

Else

Print(“a is smaller”)

For I in range(n):

Print(l)

Selecting Test Values

FILES

Read (r)

Rewrite(w)

Append(a)

try

F=open(“names”,r)

f([n]) n=number of bytes

f([n]) n=number of lines

f([n]) n=number of lines

f()

for k in range(10):

line=f()

charac=line[:3:]

except IOError x

print(“There is an error. Error number: ”+x)

finally:

print(“Done”)

f

SEARCHING & SORTING

Types of searching

Visualization: cs.usfca/~galles/visualization/Search

Types of sorting

• Visualization (in your own time) youtube/watch?

v=PgBzjlCcFvc

Big-O Notation

Efficiency:

Binary search is more efficient than linear search.

O(log n) (average case)

O(1) (best case)

Was this document helpful?

Revision Bootcamp Notes

Course: Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Was this document helpful?
BASIC PROGRAMMING
Theory/Terminology
-Program – “a set of instructions given to a computer corresponding to an
algorithm to solve a problem”
(see first lecture notes)
-Program Structure
Input
X=eval(input(“Enter a number:”))
Files
Arrays
Processing
Loops, calculations etc
Output
Print()
Files
First principles
Built-in Python function vs First principles
e.g Sentence is “Hi my name is Sani”
Sentence=input(“Enter a sentence”)
Reverse=Sentence[::-1]
Print(Reverse)
For k in range(len(Sentence),-1):
Character = sentence[k];
CompSci BootCamps