Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Programming with Collections Collections in Java Using Arrays Week 9.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Lists in Python.
Chapter 8: Arrays.
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.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Built-in Data Structures in Python An Introduction.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Week 6 - Friday.  What did we talk about last time?  Loop 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.
String and Lists Dr. José M. Reyes Álamo.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Excel IF Function.
Python Variable Types.
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Pamela Moore & Zenia Bahorski
CS 100: Roadmap to Computing
Multiple variables can be created in one declaration
CS 115 Lecture 8 Structured Programming; for loops
Containers.
Primitives Many languages have primitives: core data types built into the language with a definite space set aside for each type in memory. These are usually.
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Type Conversion, Constants, and the String Object
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
String Objects & its Methods
Type Conversion, Constants, and the String Object
Lists in Python.
Bryan Burlingame Halloween 2018
CISC101 Reminders Slides have changed from those posted last night…
Guide to Programming with Python
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Java Programming Arrays
Linked Lists.
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Coding Concepts (Data Structures)
Programming for Geographical Information Analysis: Core Skills
CMSC 202 Java Primer 2.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Arrays .
String and Lists Dr. José M. Reyes Álamo.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
References.
Python Primer 1: Types and Operators
Intro to Computer Science CS1510 Dr. Sarah Diesburg
For loops Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Java Programming Language
Data Structures & Algorithms
Bryan Burlingame Halloween 2018
Python Review
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Arrays & Loops.
Class code for pythonroom.com cchsp2cs
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Topics Basic String Operations String Slicing
Introduction to Computer Science
String Objects & its Methods
Presentation transcript:

Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes called curly brackets elsewhere. [] brackets # Sometimes called square brackets elsewhere. () parentheses # Sometimes called curved brackets elsewhere. We'll try and use the Python terms.

Holding more than one thing What happens if we want to store more than one thing? What happens if we want to store 10000 data points? We don’t want to have to call each by a new name. Languages generally have variables that are arrays: one variable that can store multiple things with one name and a numerical index. Here’s one in Java: Assignment: array_name[3] = 21; Subscription: System.out.print (array_name[3]);

Out of range Depending on the language and data stored, the array will either refer to a space in memory where all the literals are stored, or will contain pointers (memory addresses) or references (linked labels) to their locations in memory (in Python it is generally the later). Attempts to read a cell that doesn't exist will usually generate some kind of error message (usually "Index out of range/bounds" or similar) and end the program.

Arrays As arrays can be very large, they usually require manifest typing – you have to say what they are going to hold (and sometimes how large they will be) to build them. Python arrays need manifest typing. Python arrays are in a special module. You’d make one like this: import array a = array.array('i',[0,0,0,0]) # Signed int type 'i' a.insert(3, 21) print(a[3]) Unlike many arrays, you don't need to say how big it will be. https://docs.python.org/3/library/array.html

Arrays Arrays are very efficient: they are memory optimised for space and often for searching. However, in general in most languages they aren't very easy to use: You can only put pre-defined data types in them (usually just one) In most languages (though not Python) they have a fixed size (in Python, stuff just gets added to the end whatever the index, though attempts to read non-existent cells still generates errors). Because of this, most languages have wrappers for arrays that make them more flexible. In Python, these objects are called Containers, and much more used than arrays. Wrappers are code that goes around other code like an ectoskeleton to add functionality. Indeed, arrays in Python are actually regarded as an addon, rather than a core datatype, and the wrappers are core, in reverse of the usual situation.

Containers The container depends on how data is to be accessed, and whether it can be altered or not. Data changeable: "Mutables" Data fixed "Immutables" Access by position: "Sequences" List Tuple String Bytes Access by name: "Mappings" Dictionary Named tuple Access by checking existence: "Sets" Set Frozen set We will mainly be dealing with those in bold.

Mutability Mutability is whether data can be changed after it is stored in an object. Immutable objects are more efficient, as the computer can optimise around them guaranteed they won't change. Obviously, though, not being able to change them is limiting. Numbers and strings are immutable, though they may not seem it: if you assign a new value to a pre-existing label, the value is created anew. Of the containers, strings and tuples are immutable. We'll see that a) strings are containers, and b) that tuples can be immutable, yet contain mutable objects. Dictionaries, sets, and lists are mutable.

Sequences Have the advantage of being a sequence (usually the sequence as added, but potentially ordered). Their length is found with a builtin function, thus: len(sequence_name) a = len(seq) #e.g Just like arrays, we refer to the values in them using a name and position index: name[i] # Where i is an int. print(seq[2]) # e.g. All indices start with zero and go to len(sequence_name) - 1 i = 0 i = 1 i = 2 i = 3 … i = len(x) - 1

Sequences Tuples: immutable sequence of objects, either literal-style or more complicated objects. Lists: mutable sequence of objects, either literal-style or more complicated objects. Both can contain other sequences as their objects, and both can contain mixed types of object, so, for example ints and strings.

Tuples Tuples are produced by the comma operator if used without [] {} enclosers. For example: a = 2,3 Frequently they are inside (), but this is only necessary for making the empty tuple: a = (2,3) a = () The following is a tuple with one element (the documentation calls this a singleton, though beware this has other, more general meanings): a = 2, You can also use a constructor function: a = tuple(some_other_container) Like int(x) and float(x) there is a constructor function for tuples.

Tuples You can also add and multiply tuples (but not subtract from them or divide): >>> a = 1,2,3 >>> a = a + (4,) # or a += 4, >>> a (1, 2, 3, 4) >>> a = a*2 # or a *= 2 (1,2,3,1,2,3) a *= 2 only works with single values, obviously - that is, you can't multiple a tuple by a tuple of multiple values.

Subscription You can get values out, thus: a[0] # First a[len(a) - 1] # Last But also: a[-1] # Last a[-len(a)] # First Negatives count back from the end (essentially negatives get len()added to them).

Packing Forcing multiple answers into a sequence is known as 'packing'. Example: the divmod() builtin function returns a tuple (a // b, a % b) >>> c = divmod (9,2) >>> type(c) <class 'tuple'> # c is automatically made tuple type >>> c[0] # subscription, cell 0 4 # 9//2 >>> c[1] # subscription, cell 1 1 # 9%2

Unpacking Unpacking is splitting a sequence across variables: >>> a = (1,2) >>> b, c = a >>> b 1 >>> c 2 There must be the right number of variables to unpack into. Relatively rare in other languages, so feels Pythonic.

Packing/Unpacking This packing/unpacking means you can assign multiple variables at once: a, b = 1, 2 But also means you can do this: a, b = divmod(9,2) If you see two variable names with a comma between, they are usually being assigned some unpacked sequence. You can find out what type with: >>> c = divmod(9,2) >>> type(c) or >>> type(divmod(9,2)) Which returns the sequence (here a tuple) and passes it straight to the type function.

Complicated variable assignment example >>> # Fibonacci series >>> # the sum of two elements defines the next >>> a, b = 0, 1 >>> while b < 10: ... print(b) ... a, b = b, a+b Here's an example of a complicated packing/unpacking sequence, taken from the Python tutorial. https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming Can you work out what is going on here?

Ranges Ranges are a special type of immutable sequence. range(start, stop, step) # Elements in italics optional Start; stop; step should be ints. Default for start = 0 Default for step = 1 Generates numbers up to but not including stop. range(4) generates 0,1,2,3 Note how this is the same as sequence indices - this will come in useful. range(2,8,2) generates 2,4,6 range(0,-5,-1) generates 0,-1,-2,-3,-4 Ranges generate an immutable sequence of numbers. You can use these for making synthetic data, but we'll see other uses for them when we look at processing containers in the next lecture.

Tuple constructor a = tuple(x) # Where x is a container or producer of a sequence One way to make a tuple is thus: >>> a = tuple(range(5)) >>> a (0,1,2,3,4) Note you can't just assign a label: >>> a = range(5) 'range(0,5)' >>> type(a) <class 'range'>

Sequence functions min(a) Smallest item in a. max(a) Largest item in a. a.index(x,i,j) Index of the first occurrence of x in a (at or after optional index i and before index j). a.count(x) Counts of x in a. any(a) For a sequence of Booleans, checks whether any are true Returns as soon as it finds one (likewise, because of conversion, whether any numbers are != 0) One useful element of having data in sequences is that there are functions that work specifically with sequences; for example, the max() function will find the maximum value in the sequence.