1 of 35

Sequence Types

2 of 35

Sequence Types

6.0001 LECTURE 3

2

  • Ordered 1D collections of objects
  • Common examples are strings, lists, tuples
  • Use the “…”, [1, 2, 3], or (1, 2, 3) syntax to make them.
  • We use the same set of operations to act on them.

​

3 of 35

STRINGS

6.0001 LECTURE 3

3

  • think of as a sequence of case sensitive characters
  • can compare strings with ==, >, < etc.
  • len() is a function used to retrieve the length of the string in the parentheses

s = "abc"

len(s) → evaluates to 3

​

4 of 35

In addition to accessing list elements one at a time, concise syntax to access sublists; this is known as slicing

6.0001 LECTURE 3

Slicing

20

5 of 35

Slicing Example

6.0001 LECTURE 3

  • can slice strings using [start:stop:step]
  • if give two numbers, [start:stop], step=1 by default
  • you can also omit numbers and leave just colons

s = "abcdefgh"

5

s[3:6]

→ evaluates to "def", same as

s[3:6:1]

s[3:6:2] → evaluates to "df"

s[::]

s[::-1]

→ evaluates to "abcdefgh", same as s[0:len(s):1]

→ evaluates to "hgfedbca", same as s[-1:-(len(s)+1):-1]

s[4:1:-2] → evaluates to "ec"

6 of 35

STRINGS

6.0001 LECTURE 3

6

  • square brackets used to perform indexing into a string

to get the value at a certain index/position

s = "abc"

s[0]

→

evaluates to "a"

s[1]

→

evaluates to "b"

s[2]

→

evaluates to "c"

s[3]

→

trying to index out of bounds, error

s[-1]

→

evaluates to "c"

s[-2]

s[-3]

→

→

evaluates to "b"

evaluates to "a"

index: index:

0 1 2 → indexing always starts at 0

-3 -2 -1 → last element always at index -1

7 of 35

Slicing and Combining Strings

6.0001 LECTURE 3

7

Given: A string s of length at most 200 letters and four integers a, b, c and d.

​

Return: The slice of this string from indices a through b and c through d 

(with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.

​

​

Sample Dataset

HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain. 22 27 97 102

​

Sample Output

Humpty Dumpty

​

8 of 35

EXERCISE: Reversing a sequence

6.0001 LECTURE 3

8

Write a program that starts with a word, reverse it.

Pictorial Presentation:

word = “something”�rword = “”��… do stuff��print(“reversed word: “, rword)��

9 of 35

nums = list(range(5)) # range is a built-in function

# that creates a list of integers

print(nums) # Prints "[0, 1, 2, 3, 4]"

print(nums[2:4]) # Get a slice from index 2 to 4

# (exclusive); prints "[2, 3]"

print(nums[2:]) # Get a slice from index 2 to the

# end; prints "[2, 3, 4]"

print(nums[:2]) # Get a slice from the start to

# index 2 (exclusive); prints "[0, 1]"

print(nums[:]) # Get a slice of the whole list;

# prints "[0, 1, 2, 3, 4]"

print(nums[:-1]) # Slice indices can be negative; prints

# "[0, 1, 2, 3]"

nums[2:4] = [8, 9] # Assign a new sublist to a slice

print(nums) # Prints "[0, 1, 8, 9, 4]"

SLICING

10 of 35

Strings vs Lists

6.0001 LECTURE 3

  • strings are “immutable” – cannot be modified

s = "hello"

s[0] = 'y'

s = 'y'+s[1:len(s)]

→ gives an error

→ is allowed

s

"hello"

"yello"

10

11 of 35

for LOOPS

6.0001 LECTURE 3

11

  • for loops have a loop variable that iterates over a set of values

for var in range(4):

<expressions>

→ var iterates over values 0,1,2,3

→ expressions inside loop executed with each value for var

for var in range(4,6): → var iterates over values 4,5

<expressions>

​

  • range is a way to iterate over numbers, but a for loop variable can iterate over any set of values, not just numbers!

12 of 35

STRINGS AND LOOPS

6.0001 LECTURE 3

12

  • these two code snippets do the same thing

s = "abcdefgh"

for index in range(len(s)):

if s[index] == 'i' or s[index] == 'u': print("There is an i or u")

for

char in s:

​

​

if char == 'i' or char

print("There is an

== 'u':

i or u")

13 of 35

CODE EXAMPLE:

an_letters = "aefhilmnorsxAEFHILMNORSX"

​

word = ”w!o%r?d”

i = 0

while i < len(word): char = word[i]

if char in an_letters:

print(char + “is a letter")

else:

print(char + “is not a letter”)

i += 1

for char in word:

14 of 35

EXERCISE: printing the common letters in a string

6.0001 LECTURE 3

14

s1 = "this is a string"

s2 = "xyz are letters"

for char1 in s1:

for char2 in s2:

if char1 == char2:

print("common letter: “ + char1)

else:

print(uncommon letters: “ char1 + char2)

15 of 35

animals = ['cat', 'dog', 'monkey’]

for animal in animals:

print(animal)

# Prints "cat", "dog", "monkey", each on its own line.

LOOPS

16 of 35

EXERCISE

Take two lists, say for example these two:

​

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

​

and write a program that returns a list that contains only the elements that are common between the lists (without duplicates).

​

Make sure your program works on two lists of different sizes.

​

17 of 35

TUPLES

6.0001 LECTURE 5

  • an ordered sequence of elements, can mix element types
  • cannot change element values, immutable
  • represented with parentheses

te = ()

t = (2,"foo",3) t[0]

(2,"foo",3) + (5,6)

 evaluates to 2

 evaluates to (2,"foo",3,5,6)

→ slice tuple, evaluates to ("foo",)

t[1:2]

t[1:3]

len(t)

→ slice tuple, evaluates to ("foo",3)

→ evaluates to 3

t[1] = 4 → gives error, can’t modify object

17

18 of 35

MANIPULATING TUPLES

6.0001 LECTURE 5

aTuple:(( ),( ),( ))

  • can iterate over tuples

​

nums = ()

words = ()

for t in aTuple: nums = nums +

(t[0],)

if t[1] not in words:

words = words + (t[1],)

min_n = min(nums) max_n = max(nums)

unique_words = len(words)

newTuple = (min_n, max_n, unique_words)

nums( )

words(

if not already in words

i.e. unique strings from aTuple

18

? ?

? )

19 of 35

LISTS

6.0001 LECTURE 5

19

  • ordered sequence of information, accessible by index
  • a list is denoted by square brackets, []
  • a list contains elements
    • can contain mixed types (not common)
  • list elements can be changed so a list is mutable

20 of 35

INDICES AND ORDERING

6.0001 LECTURE 5

a_list = []

L = [2, 'a', 4, [1,2]]

→ evaluates to 4

→ evaluates to 2

→ evaluates to 5

→ evaluates to [1,2], another list!

→ gives an error

len(L)

L[0]

L[2]+1

L[3]

L[4]

i = 2 L[i-1]

→ evaluates to ‘a’ since L[1]='a' above

20

21 of 35

CHANGING ELEMENTS

6.0001 LECTURE 5

  • lists are mutable!
  • assigning to an element at an index changes the value

L = [2, 1, 3]

L[1] = 5

  • L is now [2, 5, 3], note this is the same object L

21

22 of 35

ITERATING OVER A LIST

6.0001 LECTURE 5

  • compute the sum of elements of a list
  • common pattern, iterate over list elements

to len(L)-1

  • range(n) goes from 0

to n-1

total = 0

for i in range(len(L)): total += L[i]

print total

  • notice
    • list elements are indexed 0

total = 0

for i in L:

total += i print total

10

23 of 35

OPERATIONS ON LISTS - ADD

6.0001 LECTURE 5

23

  • add elements to end of list with L.append(element)
  • mutates the list!

L = [2,1,3]

L.append(5)

→ L is now [2,1,3,5]

  • what is the dot?
    • lists are Python objects, everything in Python is an object
    • objects have data
    • objects have methods and functions
    • access this information by object_name.do_something()
    • will learn more about these later

24 of 35

OPERATIONS ON LISTS - ADD

6.0001 LECTURE 5

24

  • to combine lists together use concatenation, + operator, to generate a new list
  • mutate list with L.extend(some_list)

L1

=

[2,1,3]

​

L2

=

[4,5,6]

​

​

L3

=

L1 + L2

→

L3 is [2,1,3,4,5,6]

L1, L2 unchanged

L1.extend([0,6])

→ mutated L1 to [2,1,3,0,6]

25 of 35

OPERATIONS ON LISTS -

REMOVE

6.0001 LECTURE 5

25

  • delete element at a specific index with del(L[index])
  • remove element at end of list with L.pop(), returns the removed element
  • remove a specific element with L.remove(element)
    • looks for the element and removes it
    • if element occurs multiple times, removes first occurrence
    • if element not in list, gives an error

L = [2,1,3,6,3,7,0] # do below in order L.remove(2) → mutates L = [1,3,6,3,7,0] L.remove(3) → mutates L = [1,6,3,7,0]

del(L[1])

L.pop()

→ mutates L = [1,3,7,0]

→ returns 0 and mutates L = [1,3,7]

26 of 35

CONVERT LISTS TO STRINGS

AND BACK

6.0001 LECTURE 5

26

  • convert string to list with list(s), returns a list with every

character from s an element in L

  • can use s.split(), to split a string on a character parameter, splits on spaces if called without a parameter
  • use ''.join(L) to turn a list of characters into a string, can give a character in quotes to add char between every element

s = "I<3 cs" list(s) s.split('<')

L = ['a','b','c']

''.join(L)

'_'.join(L)

→ s is a string

→ returns ['I','<','3',' ','c','s']

→ returns ['I', '3 cs']

→ L is a list

→ returns "abc"

→ returns "a_b_c"

27 of 35

OTHER LIST OPERATIONS

6.0001 LECTURE 5

27

  • sort()

L=[9,6,0,3]

L.sort() L.reverse()

→ mutates L=[0,3,6,9]

→ mutates L=[9,6,3,0]

28 of 35

LISTS IN MEMORY

6.0001 LECTURE 5

28

  • lists are mutable
  • behave differently than immutable types
  • is an object in memory
  • variable name points to object
  • any variable pointing to that object is affected
  • key phrase to keep in mind when working with lists is

side effects

29 of 35

ALIASES

6.0001 LECTURE 5

29

  • hot is an alias for warm – changing one changes the other!
  • append() has a side effect

30 of 35

CLONING A LIST

6.0001 LECTURE 5

  • create a new list and copy every element using

chill = cool[:]

20

31 of 35

SORTING LISTS

6.0001 LECTURE 5

31

  • calling sort()

mutates the list, returns nothing

32 of 35

LISTS OF LISTS OF LISTS….

6.0001 LECTURE 5

32

  • can have nested lists
  • side effects still possible after mutation

33 of 35

MUTATION AND ITERATION

6.0001 LECTURE 5

33

  • avoid mutating a list as you are iterating over it

for e in L1:

if e in L2:

L1.remove(e)

L1 = [1, 2, 3, 4]

L2 = [1, 2, 5, 6]

remove_dups(L1, L2)

  • L1 is [2,3,4] not [3,4]

Why?

  • Python uses an internal counter to keep track of index it is in the loop
  • mutating changes the list length but Python doesn’t update the counter
  • loop never sees element 2

L1_copy = L1[:]

for e in L1_copy: if e in L2:

L1.remove(e)

34 of 35

EXERCISE

Write a program that takes a list, and returns two new lists with the input list values in sorted and reverse sorted order.

​

​

35 of 35

Module Questionnaire

Funding Sources: NIH U24 EB028887, NSF 2120200, 2000281, 1720625

Previous Funding : NIH R01 GM122424

​

Please take a minute or two to let us know about your experience with this module by filling out the brief zoom survey

​

Feel free to provide additional comments and suggestions in the slack or by email to us as well (hfennel@iu.edu)