CITS1401 Final Exam Notes PDF

Title CITS1401 Final Exam Notes
Author Rohit Khanna
Course Problem Solving and Programming
Institution University of Western Australia
Pages 10
File Size 581.3 KB
File Type PDF
Total Downloads 36
Total Views 131

Summary

Final Exam Notes...


Description

CITS1401 Final Exam Notes Contents - Computers and Recipes - Intro to Python - Software Development Process - Variables and Simple Loops - Maths - If/else/elif statements - Objects in Python - Strings and Cryptography - Strings and Lists are both sequences - Strings to text files - Decisions and Exception Handling - Functions - Lists and Tuples to Dictionaries - Decision Case study - Loops (while) - Simulation and Design - Search and Recursion - Programming with Recursion

Lecture 1: Introduction to Python -

Module files are text files created in text editing software that contain function definitions. This saves a lot of retyping. - Eg import math,numpy,os,re

Lecture 2: Software Development Process` -

Understand the problem, describe what your program will do, describe input and outputs, how the outputs relate to inputs, testing and debugging is also very important. Try to break the program, external users will input anything, need to account for that Variables can start with letter of underscore (no numbers)

Lecture 3: Variables and Simple Loops

-

-

Variables change as you do something to it Definite loops are used when there is a known number of times you want to repeat a certain lines of codes - For in : - can be range, a list, string For loop alters the sequence of the program

Lecture 4: Intro to Math Module -

Floats only store an approximation of the real number itself Floats aren't exact, use int whenever possible. Operations on ints produce ints (excluding /) Operation on floats produce flots //= integer division 10//3=3 % is modulus= remainder

-

Combining int with float will return a float Can only concatenate the same types (string/lists)

Lecture 5: Decisions (if/else/elif) -

-

If converts to boolean and then goes to body Python evaluates each condition in turn looking for the first one that evaluates to True. If a true condition is found, the statements indented under that condition are executed, and control passes to the next statement after the entire if-elif-else. If none are true, the statements under else are performed. The final else is optional. If there is no else, it’s possible no indented block would be executed.

Lecture 6: Strings and Cryptography decoder!! -

Concatenation glues two strings together Repetition is str*x will be strstrstrstr

-

str() returns a string representation var.capitalize() var.title() title word of each word capitalized var.center(width) centers the variable in a field of given width var.count(x) counts the number of occurrences of x in variable string var.find(x) finds the first position where x occurs in var var.join(list) concatenate list of strings into 1 large string using var as separator var.ljust(width) like center but var is left-justified var.lower var.lstrip() copy of var with leading white spaces removed var.replace(old,new) replace occurences of old with mew var.rfind(x) like find but returns the right most position var.rjust(width) var.rstrip() copy of var with trailing white spaces removed var.split() split var into a list of substrings var.upper()

Lecture 7: Strings List Sequences -

List can hold any data value (bool, string, int, float) Lists are mutable meaning they can be changed- altered by append/remove/del/pop Strings are immutable, cannot be changed only added to make new strings >>> myList = [34, 26, 15, 10] >>> myList[2] 15 >>> myList[2] = 0 >>> myList [34, 26, 0, 10] >>> myString = "Hello World" >>> myString[2] 'l' >>> myString[2] = "p”

-

-

List also have methods - min() - max() - list() - append() Other than that they also share some functions: - len() - + (concatenate) - Slicing [:] - in (but just in letters for string)

Lecture 8: File Processing -

-

>>> print("${0:0.2f} change".format(amount)) $1.50 change The part between the {} indicated how you want to format it Multiline strings: sometimes need strings that span more than one line - Embed \n in a single string - Multiline string by (“””) and \t character A file is a sequence of data that is stored in secondary memory Usually they contain more than one line and python interprets this using \n character Remember to close the file to avoid corrupting =open(,”r” or “w”) remember extension.csv .txt .read() → returns entire contents of output as single multiline string with multiple \n after each line .readline() → returns the next line of the file, this is all the text up to and including the next newline character .readlines() → returns a list of remaining lines in the file each list is a single line including the new line characters. - infile = open(someFile, "r")

-

for line in infile: # process the line here infile.close() outfile = open("mydata.out", "w") print(, file=outfile)

Lecture 9: Exception Handling -

-

Account for possible error cases and handle them in a particular way instead of it cutting the program, most of the time usually end the program neatly explaining the error Follow this syntax: - try:

Except as e (print e) Just like having multiple elif statements, you can have more than one except block for each type of error, the type of error raised will be the one outputted A bare except acts like an else and catches any errors without a specific exception type

Lecture 10: Functions -

Variables inside the function are local, only referenced and meaningful inside the function and can be reused in another function Only way for another function to see another variable is to be passed as a parameter

Lecture 11: Dictionaries -

Use a key value pair A collection that allows us to look up information associated with arbitrary keys is called mapping. Passwd = {“guido”:”programmer”,”hello”:bye”} We can use indexation to find the value from the key NOT THE OTHER WAY ROUND

-

- Passwd[“guido”] returns “programmer” Dict are mutable same way as lists - passwd[“hello”]=”changed” passwd_dir = {} for line in open('passwords', 'r'): user, pw = line.strip().split(‘,’) passwd_dir[user] = pw

Lecture 12: Search and Recursion -

Linear Search: one by one search until you find what you are looking for Binary search: each time half the possible outcomes

Lecture 13: Recursion -

A description of something referring back to itself is called recursive definition – 4! = 4(4-1)! = 4(3!) – What is 3!? We apply the definition again 4! = 4(3!) = 4[3(3-1)!] = 4(3)(2!) – And so on… 4! = 4(3!) = 4(3)(2!) = 4(3)(2)(1!) = 4(3)(2)(1)(0!) = 4(3)(2)(1)(1) = 24. All chains of recursion end up on the base case - def fact(n): if n == 0: return 1 else: return n * fact(n-1)...


Similar Free PDFs