Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction COMPSCI 105 SS 2015 Principles of Computer Science.

Similar presentations


Presentation on theme: "Introduction COMPSCI 105 SS 2015 Principles of Computer Science."— Presentation transcript:

1 Introduction COMPSCI 105 SS 2015 Principles of Computer Science

2 Lecturers  Lecturers  Angela Chang (Course coordinator)  Email: angela@cs.auckland.ac.nzangela@cs.auckland.ac.nz  Phone: 3737599 ext 86620  Room: 303S.585  Office hours: Tue, Wed, Thurs 11am-12noon  Or whenever the office door is open  Dr Mike Barley  Email: barley@cs.auckland.ac.nzbarley@cs.auckland.ac.nz  Phone: 3737599 ext 86133  Room: 303S.394  Office hours: TBA Lecture 01COMPSCI 1052 Teaching Assistants Steven Hu qhu009@aucklanduni.ac.nz Monica Bian rbia002@aucklanduni.ac.nz

3 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

4 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

5 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

6 Resources  Lectures  Overheads and recordings  Forum  Question and answers – peers, tutors and lecturers  http://forums.cs.auckland.ac.nz/http://forums.cs.auckland.ac.nz/  Textbook  Problem Solving with Algorithms and Data Structures  Online, free, open source  Additional resources  Python.org  PythonTutor.com Lecture 01COMPSCI 1056

7 Calendar WkMoTuWeThFr 1 6/Jan Intro, Python lists, List Comprehension 7 Mutable objects References and Equality 8 Classes 1 9 Classes 2 2 12 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 1 4 26 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

8 Laboratories WkMoTuWeThFr 1 6 Jan789 2 12 Lab1 13 Lab1 14 15 Lab2 16 Lab2 3 19 Lab3 20 Lab3 21 22 Lab4 23 Lab4 4 26 PUBLIC HOLIDAY 27 28 29 Lab5 30 Lab5 5 2 Feb Lab6 3 Lab6 4 Lab7 5 Lab7 6 PUBLIC HOLIDAY 6 9 Lab8 10 Lab8 1112 Lab9 13 Lab9 Lecture 01COMPSCI 1058

9 Assessments WkMoTuWeThFrSat 1 6 Jan789 10 2 12 Due: prelab1 (1%) 13 14 Due: Lab1 (2%) 15 16 17 Due: Lab2 (2%) 3 19 Due: prelab2 (1.5%) 20 21 Due: Lab3 (2%) 22 23 24 Due: Lab4 (2%) 4 26 PUBLIC HOLIDAY 27 Due: prelab3 1.5%) 28 29 TERM TEST 30 31 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%) 1213 14 Due: Lab9 (2%) Lecture 01COMPSCI 1059

10 Class Representative  Must elect a class rep  Attends 2 staff student meetings  Pass on student concerns to lecturers Lecture 01COMPSCI 10510

11 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

12 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

13 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: 50 100 length_in_cms: 254.0 Lecture 01COMPSCI 10513

14 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 7 3 2 4 7 3 2 2 7 4 Lecture 01COMPSCI 10514 Example01.py

15 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

16 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 = 3 + 4 3 + 4 is an expression Lecture 01COMPSCI 10516

17 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 ** 4  10 // 4  10 % 4 14 6 40 2.5 10000 2 2 Lecture 01COMPSCI 10517 Example01.py

18 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

19 Exercise 2  Write a function that calculates the area of a circle  area = π r 2 Lecture 01COMPSCI 10519

20 Boolean values and related operators  Boolean values  True  False  Relational operators  >, >=, <, <=, ==  Boolean operators  and, or, not >>> 2 == 3 Lecture 01COMPSCI 10520 False True >>> 2 == 3 or 4 < 5

21 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 10521 nameAndrew n6 Hi Andrew Even number Positive number

22 Sequences  Sequences allow you to store values in an organized fashion.  strings, lists, tuples, dictionaries, and sets  http://en.wikibooks.org/wiki/Python_Programming/Sequences Lecture 01COMPSCI 10522

23 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 10523 nameAndrew 'A' True 6 6 >>> len(name) >>> 'd' in name >>> name + ' ' + 'Luxton-Reilly' 'Andrew Luxton-Reilly'

24 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

25 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 426 9 3 Lecture 01COMPSCI 10525

26 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 10526 3 3 [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

27 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 0 1 2 3 123 0 1 2 456 0 1 2 7 0 89 0 1 Lecture 01COMPSCI 10527

28 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

29 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 10529 '' 'A' 'ndr' >>> name[0:1] >>> name[1:4]

30 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 012345 Negative index -6-5-4-3-2 Lecture 01COMPSCI 10530 'ad' 'werdnA'

31 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 10531 2 3 5 7 11 2 3 5 7 11 AndrewAndrew AndrewAndrew Example01.py

32 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 10532 Example01.py

33 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 10533 0123401234 0123401234 Example01.py

34 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

35 Exercise 5  Write a list comprehension that generates all the odd numbers between 1 and 50 Lecture 01COMPSCI 10535

36  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 10536 >>> 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']

37  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


Download ppt "Introduction COMPSCI 105 SS 2015 Principles of Computer Science."

Similar presentations


Ads by Google