Lists in Python Creating lists.

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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?
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.
Files in Python Caution about readlines vs. read and split.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Lists in Python.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
Lists CS303E: Elements of Computers and Programming.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
More Python Proglan Python Session 2. Lists  Lists are like flexible arrays. They can contain as many variables as you wish, and all variable types are.
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.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
ITEC 109 Lecture 20 Arrays / Lists. Arrays Review For loops –Rationale –Syntax –Examples.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus You could use: total.
Tutorial 2 Exam Revision. Notices Assignment + project Recap string formatting Revision.
String and Lists Dr. José M. Reyes Álamo.
Catapult Python Programming Session 4
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
Lexical Reference Variables in Graphics and List Box in Forms
Algorithmic complexity: Speed of algorithms
Computer Programming Fundamentals
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings in Python Creating a string.
Exceptions and files Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Lists Part 1 Taken from notes by Dr. Neil Moore
Python I/O.
File Handling Programming Guides.
Topics Introduction to File Input and Output
List Algorithms Taken from notes by Dr. Neil Moore
Bryan Burlingame 03 October 2018
Lists in Python.
Using files Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Spot the bug!.
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Outputting lists.
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Files Handling In today’s lesson we will look at:
Programming Control Structures with JavaScript Part 2
Intro to Computer Science CS1510 Dr. Sarah Diesburg
functions: argument, return value
15-110: Principles of Computing
Loops and Arrays in JavaScript
For loops Taken from notes by Dr. Neil Moore
Algorithmic complexity: Speed of algorithms
Appending or adding to a file using python
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Computer Programming Tutorial
Presentation transcript:

Lists in Python Creating lists

Simple ways to create a list Hard code the values mylist = [3, 9, ‘a’, 7.2] Using the replication operator mylist = [0] * 100 (gives a list with 100 zeros) Use the split method on a string Use the readlines method on a file Put [] around a variable if my_num is 5, then [ my_num] is the list [5]

Append vs. Concatenate Both can be used to add to an existing list but their syntax is not the same The concatenate operator + uses two lists and creates a bigger one You cannot say mylist = mylist + “joe”, it must be mylist = mylist + [“joe”] Append is a method which adds an element to the right end of a list – any type of data mylist.append(“3”) makes mylist one element longer

List accumulation Either concatenation or append can be used with lists as accumulators Initialize the list variable as an empty list [] inside your loop either concatenate or append the new item onto your list variable