Creation, Traversal, Insertion and Removal

Slides:



Advertisements
Similar presentations
Python for Informatics: Exploring Information
Advertisements

Course A201: Introduction to Programming 10/28/2010.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Container Types in Python
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Programming Seminar 2009 Night 1. Review: Last Time We covered variables, for loops, functions, modules, and how programming isn’t math – Actually, we.
Introduction to Python
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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.
Strings CS303E: Elements of Computers and Programming.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Please log on The. AN INTRODUCTION TO ‘Python is a high-level, general purpose programming language’ Python is one of the many programming languages.
Lists CS303E: Elements of Computers and Programming.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
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.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
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.
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
String and Lists Dr. José M. Reyes Álamo.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Topic: Python Lists – Part 1
Python Variable Types.
Main Points: - Working with strings. - Problem Solving.
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Formatting Output.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
An Introduction to Python
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Lists in Python Outputting lists.
Coding Concepts (Data- Types)
Grouped Data Arrays, and Array Lists.
Lists Part 1 Taken from notes by Dr. Neil Moore
Basic String Operations
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By- Anshul Kumar Class: XI “B”
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
CMSC201 Computer Science I for Majors Lecture 07 – Strings and Lists
Bryan Burlingame Halloween 2018
And now for something completely different . . .
Python Review
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
L L Line CSE 420 Computer Games Lecture #4 Working with Data.
Class code for pythonroom.com cchsp2cs
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
COMPUTER SCIENCE PRESENTATION.
Topics Basic String Operations String Slicing
Python List.
Tuple.
Introduction to Computer Science
Presentation transcript:

Creation, Traversal, Insertion and Removal Lists: Creation, Traversal, Insertion and Removal

What is a list? Very similar to a string, it is a sequence of values. These values are called elements or items. The list ‘remembers’ the order, index and name of each element. Sometimes referred to as an array

students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] To create a list Let’s begin by naming the list, just like we would a variable. We then ‘list’ all the elements, using brackets, commas and quotation marks. Single or double quotations marks work ( ‘ ‘ or “ “) students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] As the students are entered in the list, they are indexed 0th, 1th, 2th, et cetera. (we don’t use ‘1st’ because 0 is the ‘first’ position. 1 is 2nd pos.) Lists, just like variables in Python must be lowercase and not contain any ‘keywords’ such as “print”, “and”, “input”, etc.

What works for Strings works for lists Each element / item in a list is also indexed, just like in a string. We can find the length, index of a character, and a specific character or word in a list. We can concatenate and repeat just like we can with a string. Concatenate means to bring together (without adding). For example: If I were to concatenate the numbers 11 and 42, I would get the return 1142, not 53 like we would if we added.

Concatenate (con*cat*en*nate) Adding without arithmetic 4 + 5 = 9 We can add using integers But can we add words together? “Yellow” + “Blue” = “Green”? No. “Yellow” + ”Blue” = “YellowBlue” This is concatenation.

How to concatenate: We use the + using strings to combine them. This is not addition. “4”+”3” would not return 7. This would return 43. We can use this to combine lists

String functions work for lists too… Strings functions: len(str) Returns the length in characters, of a string str[ : ] ‘slices’ or returns a piece of the string from [___ to ____] str.find( ) Finds a character / word within a string

Also… We can concatenate using the + sign “string 1” + “string 2” We can repeat a string or a list using the * symbol “string 1” * 3 will result in: string1 string1 string1

To add to the list Let’s say we want to add a few more students to our current list: students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] # now we will append or add elements to this list. Typing: students.append(“Chris”) students.append(“Janet”) students.append(“James”)

To add to the list Will give us our complete list: students = [“Dion”, “Meghan”, “Joseph”, “Denise”, “Chris”, “Janet”, “James” ] Notice that the last elements (students) were added to the end of the list.

Remove an item We can use the del command to remove an item from a list Let’s look at our student list: students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] If we wanted to remove Joseph from the list, we would type: del students[2] Meaning  delete from students list, the item at index 2 remember that Python begins counting at index 0.

Now, Let’s manipulate the list 1) Find the length (in elements) of the list. 2) Find the index position of a specific student – “Janet” 3) Traverse the list so that each item in the list is printed one line at a time. * This may be tricky to do. How could we use a for loop to accomplish this? Talk out how you would do this first.