COMPSCI 107 Computer Science Fundamentals

Slides:



Advertisements
Similar presentations
Lecture 07 – Iterating through a list of numbers.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Lists Introduction to Computing Science and Programming I.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Introduction COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
COMPSCI 101 Principles of Programming Lecture 4 –The type() function, the string type, the len() function, string slices.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
PYTHON LISTS. What are lists? An ordered set of elements A variable with 0 or more things inside of it Examples myList = [8, 6, 7, 5, 3, 0, 9] strList.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Lists COMPSCI 105 S Principles of Computer Science.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
String Manipulation Part 1
Computer Programming Fundamentals
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 09 – For Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Introduction to Strings
Bryan Burlingame Halloween 2018
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Fundamentals of Python: First Programs
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Python Data Structures: Lists
Python Data Structures: Lists
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Tuple.
Introduction to Computer Science
Presentation transcript:

COMPSCI 107 Computer Science Fundamentals Lecture 05 – Sequences of data

Recap Previously, we looked at: how our programs can have unexpected consequences for society the importance of writing tests before code documenting code after it is written using doctest to integrate testing and documentation COMPSCI 107 - Computer Science Fundamentals

Learning outcomes At the end of this lecture, students should be able to: Use index values to refer to elements of a sequence Use the “slice” syntax to refer to a subsequence Use operations that apply to sequences Use the for loop syntax to iterate over a sequence Use list comprehensions to create a list Visualise how lists are stored in memory Create multi-dimensional lists COMPSCI 107 - Computer Science Fundamentals

Sequences Sequences allow you to store values in an organized fashion. Built-in types: strings, lists, tuples http://en.wikibooks.org/wiki/Python_Programming/Sequences COMPSCI 107 - Computer Science Fundamentals

Strings Strings are a sequence of characters Strings have a number of methods that are useful split() is especially useful help(str) >>> name = 'Andrew' >>> name[0] 'A' >>> 'd' in name True >>> len(name) 6 >>> name + '_' + 'Luxton-Reilly' 'Andrew_Luxton-Reilly' COMPSCI 107 - Computer Science Fundamentals

Exercise 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. COMPSCI 107 - Computer Science Fundamentals

Python lists my_list = [1, 2, 3] Lists are a built-in type in Python Use square brackets to signify a list Can contain any type of data, or any mixture of types How do you visualise the following list? my_list = [1, 2, 3] Show the Python visualiser online my_list

Python lists my_list = [1, 2, 3] Lists are a built-in type in Python Use square brackets to signify a list Can contain any type of data, or any mixture of types How do you visualise the following list? my_list = [1, 2, 3] 2 1 3 Show the Python visualiser online my_list 1 2 3 4

Python lists 3 my_list = [1, 2, 3] len(my_list) Numerous list functions are supported Use help(list) to see a complete list of functions Examples: my_list = [1, 2, 3] len(my_list) 3 2 1 3 Show the Python visualiser online my_list 1 2 3 4

Python lists my_list = [1, 2, 3] my_list = my_list + [4] Numerous list functions are supported Use help(list) to see a complete list of functions Examples: my_list = [1, 2, 3] my_list = my_list + [4] 4 2 1 3 Note – the + operator here creates a new list my_list 1 2 3 4

Python lists my_list.append(7) Numerous list functions are supported 7 Use help(list) to see a complete list of functions Examples: my_list.append(7) 7 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

Python lists True 3 in my_list Numerous list functions are supported 4 Use help(list) to see a complete list of functions Examples: 3 in my_list True 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

Python lists 1 my_list[0] Numerous list functions are supported 4 2 1 Use help(list) to see a complete list of functions Examples: my_list[0] 1 4 2 1 3 Show the Python visualiser online my_list 1 2 3 4

Python lists Numerous list functions are supported append(item) Use help(list) to see a complete list of functions append(item) insert(i, item) pop() pop(i) sort() reverse() del( my_list[i] ) index(item) count(item) remove(item) Make sure you are comfortable with these functions (i.e. you can use the Python docs to call them and understand how they work) Show the Python visualiser online

Exercises Write a function that sums the elements of a list that contains numbers. Write a function that accepts a list and returns a new list with the same contents, but in reverse order. COMPSCI 107 - Computer Science Fundamentals

Lists of lists Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] Show the Python visualiser online

Lists of lists Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] Show the Python visualiser online

Lists of lists Since a list element can be of any type, it can actually be a list my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] The online Python Tutor has a useful visualisation engine: http://www.pythontutor.com/visualize.html Show the Python visualiser online

Slices of sequences A piece of a sequence can be obtained using the following syntax: sequence_name[start : stop] Where start is the index of the first element, and stop is the index after the last element >>> name = "Andrew" >>> values = [5,8,2,3,7,1] >>> print(name[1:2]) n >>> print(values[2:5]) [2, 3, 7] Show the Python visualiser online

Slices 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 is omitted, it defaults to : [ start : stop : step] length 1 Using a step size of -1, and not providing start and stop means that Python will step through the word right to left, from end to beginning >>> word = 'Computer' >>> print(word[:4:2]) Cm >>> print(word[::-1]) retupmoC Sure, the [::] is the extended slice operator. It allows you to take substrings. Basically, it works by specifying which elements you want as [begin:end:step], and it works for all sequences. Two neat things about it: You can omit one or more of the elements and it does "the right thing" Negative numbers for begin, end, and step have meaning For begin and end, if you give a negative number, it means to count from the end of the sequence. For instance, if I have a list: l = [1,2,3] Then l[-1] is 3, l[-2] is 2, and l[-3] is 1. For the step argument, a negative number means to work backwards through the sequence. So for a list:: l = [1,2,3,4,5,6,7,8,9,10] You could write l[::-1] which basically means to use a step size of -1 while reading through the list. Python will "do the right thing" when filling in the start and stop so it iterates through the list backwards and gives you [10,9,8,7,6,5,4,3,2,1]. I've given the examples with lists, but strings are just another sequence and work the same way. So a[::-1] means to build a string by joining the characters you get by walking backwards through the string. word C o m p u t e r Positive index 1 2 3 4 5 6 7 Negative index -8 -7 -6 -5 -4 -3 -2 -1

Exercise

For loops Used to iterate through a sequence 2 3 5 7 11 numbers = [2, 3, 5, 7, 11] for i in numbers: print(i) A n d r e w name = 'Andrew' for c in name: print(c) Show the Python visualiser online

List comprehensions A list can be created using instructions that appear within the square brackets The general format of a list comprehension is: my_list = [x for x in range(90, 100)] print(my_list) [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] Show the Python visualiser online [expression for variable in sequence]

List comprehensions Examples Converting a string into a list of characters: my_list = [c for c in 'COMPSCI107'] print(my_list) ['C', 'O', 'M', 'P', 'S', 'C', 'I', '1', '0', ‘7'] Generating all even numbers between 1 and 20 my_list = [n * 2 for n in range(1, 10)] print(my_list) Show the Python visualiser online [2, 4, 6, 8, 10, 12, 14, 16, 18]

List comprehensions using conditions We can extend the syntax for a list comprehension to include a condition: The general format of a list comprehension using a condition is: my_list = [x for x in range(0, 10) if x % 2 == 0] print(my_list) [0, 2, 4, 6, 8] Show the Python visualiser online [expression for variable in sequence if condition]

Exercises What is the output? (a) Result = [2, 4] (b) Result = [3, 4] values = [x + x for x in range(5) if x + x < x * x] print('Result =', values) (a) Result = [2, 4] (b) Result = [3, 4] (c) Result = [0, 2, 4, 6, 8] (d) Result = [6, 8] (e) Result = [0, 1, 2, 3, 4] Answer = D

Exercise What is the output? numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] primes = [2, 3, 5, 7] my_list = [x+x for x in numbers if x not in primes] print(my_list) Numbers are immutable, lists are mutable – more tomorrow!

List comprehensions using conditions Example Generate a list of the non-vowel letters that appear in a string: phrase = 'The quick brown fox jumps over the lazy dog' vowels = 'aeiou' my_list = [c for c in phrase if c not in vowels] for c in my_list: print(c, end = '') Show the Python visualiser online Th qck brwn fx jmps vr th lzy dg

Summary Information in a list is stored contiguously in memory (i.e. in consecutive memory locations) Location (memory address) of the information can be calculated as: location = address of start of list + index * size of each element Efficiency issues It takes the same amount of time to access any of the elements It is slow to move elements around (i.e. add and delete elements from within the list) COMPSCI 107 - Computer Science Fundamentals