Introduction COMPSCI 105 SS 2015 Principles of Computer Science
Lecturers Lecturers Angela Chang (Course coordinator) Phone: ext Room: 303S.585 Office hours: Tue, Wed, Thurs 11am-12noon Or whenever the office door is open Dr Mike Barley Phone: ext Room: 303S.394 Office hours: TBA Lecture 01COMPSCI 1052 Teaching Assistants Steven Hu Monica Bian
Topics Making informed choices about programming Understanding the trade-offs Focus on ways of storing and manipulating data Different ways of structuring data storage Efficiency Searching Sorting Some new programming ideas Importance of Abstraction Recursion, regular expressions, exceptions, data interchange Lecture 01COMPSCI 1053
Assessment Note: Students must obtain a pass in both the practical (assignments) and non-practical work (test + exam) in order to pass as a whole Laboratories every week (starting next week) ………. 25% Pre-lab exercises & two laboratories per week dues on every Thursday OR Saturday Note: labs to be submitted using CodeRunner 5 Pre-lab exercises (7%) 9 Laboratories (18%) Mid-semester Test …………………………………...10% Thurs 29 th Jan, 10:00am – 11:00 am Final Exam …………………………………………..65% Date to be announced Lecture 01COMPSCI 1054
Laboratories All Laboratories will be started from Monday 12 Jan. Two Laboratories per week First Lab (Lab)/ Venue: G75 Streams: Monday 12:00noon – 1:00pm Monday 1:00pm-2:00pm Tuesday 12:00noon – 1:00pm Tuesday 1:00pm-2:00pm Tuesday 2:00pm-3:00pm Second Lab (Tut)/ Venue: B75 Streams: Thursday 12:00noon – 1:00pm Thursday 1:00pm-2:00pm Thursday 2:00pm-3:00pm Friday 12:00noon – 1:00pm Friday 1:00pm-2:00pm Lecture 01COMPSCI 1055
Resources Lectures Overheads and recordings Forum Question and answers – peers, tutors and lecturers Textbook Problem Solving with Algorithms and Data Structures Online, free, open source Additional resources Python.org PythonTutor.com Lecture 01COMPSCI 1056
Calendar WkMoTuWeThFr 1 6/Jan Intro, Python lists, List Comprehension 7 Mutable objects References and Equality 8 Classes 1 9 Classes Exceptions 13 Exceptions 2 14 JSON 15 Algorithm Analysis 16 Abstract Data Types, Stack ADT 3 19 Stacks 20 Queues 21 Lists 22 Lists 2 23 Recursion PUBLIC HOLIDAY 27 Recursion 2 28 Searching 29 TERM TEST 30 Hash functions 5 2 Hash functions 3 Sorting: Simple Sorts 4 Sorting: Better Sorts 5 Introduction to Trees 6 PUBLIC HOLIDAY 6 9 Priority Queues and Binary Heaps 10 Using Trees - Expressions 11 Binary Search Trees 12 Binary Search Trees and Regular Expressions 13 More Regular Expressions Lecture 01COMPSCI 1057
Laboratories WkMoTuWeThFr 1 6 Jan Lab1 13 Lab Lab2 16 Lab Lab3 20 Lab Lab4 23 Lab PUBLIC HOLIDAY Lab5 30 Lab5 5 2 Feb Lab6 3 Lab6 4 Lab7 5 Lab7 6 PUBLIC HOLIDAY 6 9 Lab8 10 Lab Lab9 13 Lab9 Lecture 01COMPSCI 1058
Assessments WkMoTuWeThFrSat 1 6 Jan Due: prelab1 (1%) Due: Lab1 (2%) Due: Lab2 (2%) 3 19 Due: prelab2 (1.5%) Due: Lab3 (2%) Due: Lab4 (2%) 4 26 PUBLIC HOLIDAY 27 Due: prelab3 1.5%) TERM TEST Due: Lab5 (2%) 5 2 Feb Due: prelab4 1.5%) 3 4 Due: Lab6 (2%) 5 6 PUBLIC HOLIDAY 7 Due: Lab7 (2%) 6 9 Due: prelab5 1.5%) 1011 Due: Lab8 (2%) Due: Lab9 (2%) Lecture 01COMPSCI 1059
Class Representative Must elect a class rep Attends 2 staff student meetings Pass on student concerns to lecturers Lecture 01COMPSCI 10510
Revision – Python Programs When the steps in a process are well defined, we can store them in the computers memory in a way that the computer can follow them. The language we store the instructions must be formal language Reduces ambiguity Python is a programming language designed to be easy to read Each step in the program is known as a statement A program is a sequence of statements Ways of running a program Interactive execution – great for learning Creating a module (file) and executing the module Lecture 01COMPSCI 10511
Variables Variables store information Information is divided into different types Python is dynamically typed Variables do not need to be declared before they are used Basic types int, float, boolean, string x = 34 x = 34.5 x = True x = 'Hello' x = 34 x = 34.5 x = True x = 'Hello' Lecture 01COMPSCI 10512
Tracing code Keep track of the contents of variables Write down the name of each variable Change the value when (and only when) an assignment occurs When you change a value, cross out the old one and write a new one length_in_inches: length_in_cms: Lecture 01COMPSCI 10513
Example 1 What is the output of the following code? Perform a code trace. a = 7 b = 3 c = 2 d = 4 e = a a = b b = e e = c c = d d = e print(a, b, c, d, e) a = 7 b = 3 c = 2 d = 4 e = a a = b b = e e = c c = d d = e print(a, b, c, d, e) ab c de Lecture 01COMPSCI Example01.py
Exercise 1 Suppose that there are 4 variables names x0, x1, x2 and x3. Write the code to move the values stored in those variables to the left, with the leftmost value ending up in the rightmost variable, as shown in the diagram below. Lecture 01COMPSCI 10515
Expression An expression is part of the program that can be evaluated (i.e. when the computer follows the rules that define the instructions, an expression turns into a value). An expression can be used anywhere that a value is used x = is an expression Lecture 01COMPSCI 10516
Example Floor division and modulus Integer part of a division, and remainder after division What do each of the following expressions evaluate to? 10 * 4 10 / 4 10 ** 4 10 // 4 10 % Lecture 01COMPSCI Example01.py
Functions A function is a sequence of instructions designed to perform a task, and is packaged as a unit. Functions have a name Functions accept arguments Functions return values Syntax Indentation rather than braces are used to signify blocks of code Variables defined within the scope of a function are not available outside the function def rectangle_area(width, height): return width * height def rectangle_area(width, height): return width * height Lecture 01COMPSCI 10518
Exercise 2 Write a function that calculates the area of a circle area = π r 2 Lecture 01COMPSCI 10519
Boolean values and related operators Boolean values True False Relational operators >, >=, <, <=, == Boolean operators and, or, not >>> 2 == 3 Lecture 01COMPSCI False True >>> 2 == 3 or 4 < 5
Conditionals Code is executed if the condition is true if n < 0: print("Negative number") elif n > 0: print("Positive number") else: print("Zero") if n < 0: print("Negative number") elif n > 0: print("Positive number") else: print("Zero") if name == "Andrew": print("Hi Andrew") if name == "Andrew": print("Hi Andrew") if n % 2 == 0: print("Even number") else: print("Odd number") if n % 2 == 0: print("Even number") else: print("Odd number") Lecture 01COMPSCI nameAndrew n6 Hi Andrew Even number Positive number
Sequences Sequences allow you to store values in an organized fashion. strings, lists, tuples, dictionaries, and sets Lecture 01COMPSCI 10522
Strings Strings are a sequence of characters Strings also have a number of other functions that can be used split() is especially useful >>> name = 'Andrew' >>> name[0] >>> name = 'Andrew' >>> name[0] Lecture 01COMPSCI nameAndrew 'A' True 6 6 >>> len(name) >>> 'd' in name >>> name + ' ' + 'Luxton-Reilly' 'Andrew Luxton-Reilly'
Exercise 3 Write a function that determines whether a given string of text contains a vowel or not. The function should return True if the text contains a vowel. Lecture 01COMPSCI 10524
Lists Lists are a built-in type in Python Use square brackets to signify a list Lists can contain any type of data, or any mixture of data my_list1 = [1, 2, 3] my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] my_list3 = [1, 5.899, 'Hello'] my_list4 = [4, 2, 6, 9, 3] my_list1 = [1, 2, 3] my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?'] my_list3 = [1, 5.899, 'Hello'] my_list4 = [4, 2, 6, 9, 3] my_list Lecture 01COMPSCI 10525
List functions Numerous list functions are supported Use help(list) to find out the functions Examples: >>> x = [1, 2, 3] >>> len(x) >>> x = [1, 2, 3] >>> len(x) Lecture 01COMPSCI [1, 2, 3, 4] [1, 2, 3, 5] >>> x += [5] >>> x + [4] >>> 3 in x True 1 1 >>> x[0] x 123 x 123 5
Lists of lists Since a list can contain anything, it can of course contain a list In memory my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] my_list Lecture 01COMPSCI 10527
Exercise 4 Draw a diagram showing how the following list is structured in memory: my_list = [[1, 2], [[3, 4], [5, 6, 7]]] Lecture 01COMPSCI 10528
Slices of sequences A piece of a sequence can be obtained using the following syntax sequence_name[x:y] where x is the index of the first element and y is the index after the last element >>> name = 'Andrew' >>> name[0:0] >>> name = 'Andrew' >>> name[0:0] Lecture 01COMPSCI '' 'A' 'ndr' >>> name[0:1] >>> name[1:4]
Slice step value Actually, the syntax allows for a third value, used to define the step size between elements included in the slice. If a value if omitted, it defaults to [start:end:1] If the step size is negative, it starts at the end and steps backward towards the start. >>> name = 'Andrew' >>> name[:4:2] >>> name = 'Andrew' >>> name[:4:2] >>> name = 'Andrew' >>> name[::-1] >>> name = 'Andrew' >>> name[::-1] nameAndrew Positive Index Negative index Lecture 01COMPSCI 'ad' 'werdnA'
For loops Used to iterate through a sequence numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) name = "Andrew" for c in name: print(c) name = "Andrew" for c in name: print(c) Lecture 01COMPSCI AndrewAndrew AndrewAndrew Example01.py
While loops Used to execute code when the end condition is unknown name = "Andrew Luxton-Reilly" i = 0 while name[i] != ' ': i += 1 print('Space is at position:', i) name = "Andrew Luxton-Reilly" i = 0 while name[i] != ' ': i += 1 print('Space is at position:', i) Space is at position: 6 Lecture 01COMPSCI Example01.py
Range Range is a special object in Python Used to generate integer numbers within a range of values Can iterate through the range for x in range(0, 5): print(x) for x in range(0, 5): print(x) Lecture 01COMPSCI Example01.py
List comprehensions A list can be created using instructions that appear within the square brackets The general format is as follows: Examples: To convert a string to a list of characters: To generate all the even numbers between 1 and 20 my_list = [x for x in range(0, 10)] [expression for variable in sequence] my_list = [c for c in 'Andrew'] my_list = [n * 2 for n in range(1, 10)] Lecture 01COMPSCI 10534
Exercise 5 Write a list comprehension that generates all the odd numbers between 1 and 50 Lecture 01COMPSCI 10535
We can extend the syntax for a list comprehension to include a condition: The general format is as follows: Examples: Generate a list of the non-vowel letters that appear in a string: List comprehensions that use conditions my_list = [x for x in range(0, 10) if x % 2 == 0] [expression for variable in sequence if condition] Lecture 01COMPSCI >>> name = 'Andrew Luxton-Reilly' >>> vowels = 'aeiou‘ >>> my_list = [c for c in name if c not in vowels] >>> name = 'Andrew Luxton-Reilly' >>> vowels = 'aeiou‘ >>> my_list = [c for c in name if c not in vowels] ['A', 'n', 'd', 'r', 'w', ' ', 'L', 'x', 't', 'n', '-', 'R', 'l', 'l', 'y']
Information in a list is stored contiguously in memory location of the information can be calculated location = start of the list + index * size of each element Efficiency issues It takes the same time to access any of the elements Slow to move elements around (i.e. add and delete elements from within the list) Features of lists Lecture 01COMPSCI 10537