Chapter 8 - Working with Sequences: Strings & Lists

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/69

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:56 PM on 8/2/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

70 Terms

1
New cards

sequence

an object that holds multiple items of data, stored one after the other

2
New cards

What are the 2 ways to access the individual characters in a string?

  • for loops

  • indexing

3
New cards

How can you use for loops to access individual characters in a string?

each time the loop iterates, the variable will reference a copy of a character in string, beginning with the first character

4
New cards

What is the output for this code?

name = "Juliet"
for ch in name:
     print ch

J

u

l

i

e

t

5
New cards

How can you use indexing to access individual characters in a string?

each character in a string has an index that specifies its position in the string; indexing starts at 0, and the index of the last character in a string is 1 less than the number of characters in the string; index -1 is the last character

6
New cards

What is the output for this code?

my_string = "Roses are red"
ch = my_string[6]
print ch

a

7
New cards

What is the output for this code?

my_string = "Roses are red"
print (my_string[0], my_string[6], my_string[10])

R a r

8
New cards

When does an Index Error exception occur?

when you try to use an index that is out of range for a particular string

  • most likely to happen when a loop incorrectly iterates beyond the end of a string

9
New cards

len()

returns the length of a sequence

  • useful in preventing loops from iterating beyond the end of a string

10
New cards

What does it mean for a string to be immutable?

it cannot be changed

11
New cards

What is a consequence of strings being immutable?

you cannot use an expression in the form string[index] on the left side of an assignment operator

12
New cards

slice

a span of items that are taken from a sequence

13
New cards

What do you get when you take a slice from a string?

a span of characters from the string

14
New cards

What are string slices called?

substrings

15
New cards

string[start:end]

gives a slice of a string

  • start is the index of the 1st character in the slice, and end is the index marking the end of the slice

  • the expression will return a string containing a copy of the characters from start up to but not including the end

16
New cards

What is the output for this code?

text = "Four score and seven years ago"
if "seven" in text:
     print("The string "seven" was found")
else:
     print("The string "seven" was not found")

The string “seven” was found

17
New cards

string testing methods

test a string for specific characters

18
New cards

isalnum()

true if the string contains only numeric digits and is at least one character long

19
New cards

isalpha()

true if the string contains only alphabetic letters and is at least one character long

20
New cards

islower()

true if the all characters in the string are lower case

21
New cards

isspace()

true if the string contains only whitespace characters (spaces, newlines, and tabs) and is at least on character long

22
New cards

isupper()

true if all the alphabetic letters are uppercase and it is at least on character long

23
New cards

string modification methods

return a modified version of strings

24
New cards

lower()

converts all alphabetic letters to lowercase

25
New cards

lstrip()

removes all leading whitespace characters (spaces, newlines, tabs at the beginning)

26
New cards

lstrip(char)

removes all instances of char that appear the beginning of the string

27
New cards

rstrip()

removes all trailing whitespace characters (spaces, newlines, tabs at the end)

28
New cards

rstrip(char)

removes all instances of char that appear at the end of the string

29
New cards

strip()

removes all leading and trailing whitespace characters

30
New cards

strip(char)

removes all instances of char that appear at the beginning and end of the string

31
New cards

upper()

converts all alphabetic letters to uppercase

32
New cards

search and replace methods

search for substrings and replace the occurrences of a substring with another string

33
New cards

endswith(substring)

true if the string ends with substring

34
New cards

find(substring)

returns the lowest index in the string where the substring is found; if substring is not found, returns -1

35
New cards

replace(old, new)

replaces all instances of old with new

36
New cards

startswith(substring)

true if the string starts with substring

37
New cards

repetition operator

when the operand on the left side of the * symbol is a string and the operand on the right side is an integer

string_to_copy * n

38
New cards

What is the output for this code?

print("Hello"*5)

HelloHelloHelloHelloHello

39
New cards

list

an object that contains multiple data items

  • mutable - contents can be changed during a program’s execution

  • dynamic data structures - items may be added to or removed from them

  • indexing, slicing and various methods can be used on them

40
New cards

What is the output for this code?

numbers = [5, 10, 15, 20]
print numbers

[5, 10, 15, 20]

41
New cards

What is the output for this code?

my_list = [10, 20, 30, 40]
print(my_list[0], my_list[1], my_list[2], my_list[3])

10 20 30 40

42
New cards

What is the output for this code?

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
mid_days = days[2:5]
print(mid_days]

[Tuesday, Wednesday, Thursday]

43
New cards

True or false: an expression in the form list[index] can appear on the left side of an assignment operator

true; lists are mutable

44
New cards

What is the output for this code?

numbers = [1, 2, 3, 4, 5]
print numbers
numbers[0] = 99
print numbers

[1, 2, 3, 4, 5]

[99, 2, 3, 4, 5]

45
New cards

append(item)

adds item to the end of the list

46
New cards

index(item)

returns index of the first element whose value is equal to item

47
New cards

insert(index, item)

inserts item into the list at the specific index, shifting every item after it by one position toward the end of the list

  • if the specified index is beyond the list’s end, the item is added to the end of it

  • if the specified index is negative, the item is inserted at the beginning of list

48
New cards

sort()

sorts the items into ascending order

49
New cards

remove(item)

removes the first occurrence of item from the list; a ValueError exception is raised if item is not found in the list

50
New cards

del statement

removes an element from a specific index, regardless of the item that is stored at that index

51
New cards

What is the output for this code?

my_list = [1, 2, 3, 4, 5]
print("Before deletion:", my_list)
del my_list[2]
print("After deletion:", my_list)

Before deletion: [1, 2, 3, 4, 5]
After deletion: [1, 2, 4, 5]

52
New cards

min()

accepts a sequence as an argument and returns the item that has the lowest value in the sequence

53
New cards

What is the output for this code?

my_list = [5, 4, 3, 2, 50, 40, 30]
print("The lowest value is", min(my_list))

The lowest value is 2

54
New cards

max()

accepts a sequence as an argument and returns the item that has the highest value in the sequence

55
New cards

What is the output for this code?

my_list = [5, 4, 3, 2, 50, 40, 30]
print("The highest value is", max(my_list))

The highest value is 50

56
New cards

What is the output for this code?

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = list1 + list2
print(list3)

[1, 2, 3, 4, 5, 6, 7, 8]

57
New cards

What is the output for this code?

def main():
     numbers = [2, 4, 6, 8, 10]
     total = 0
     for value in numbers:
        total += value
     print("The total of the elements is", total)
main()

The total of the elements is 30

58
New cards

What is the output for this code?

def main():
     my_string = "One two three four"
     word_list = my_string.split()
     print(word_list)
main()

[“One”, “two”, “three”, “four”]

59
New cards

What is the output for this code?

date_string = "3/6/2008"
date_list = date_string.split("/")
print(date_list)

[“3”, “6”, “2008”]

60
New cards

two dimensional lists

contain other lists as their elements

  • a.k.a. nested lists

  • useful for working with multiple data sets

  • two indexes are needed to process data

  • typically use nested loops to process

61
New cards

tuples

immutable sequences that are very similar to lists

  • format: tuple_name = (item1, item2)

  • support operations as lists

  • do not support append, remove, sort, reverse, or insert

62
New cards

How are tuples advantageous over lists?

they are safe and faster in processing

63
New cards

list()

converts tuple to list

64
New cards

tuple()

converts list to tuple

65
New cards

What is the output for this code?

data = (1, 2, 3)
a, b, c = data
print(b)

2

66
New cards

What is the output for this code?

numbers = [1, 2, 3, 4, 5]
a, *middle, c = numbers
print(a, middle, b)

1 [2, 3, 4] 5

67
New cards

What is the output for this code?

nums = (3, 5)
print(add(*nums))

8

68
New cards

list comprehensions

a concise way to create lists, offering a shorter syntax than loops

syntax: [expression for item in iterable]

69
New cards

What is the output for this code?

myString = "Hello"
myList = [c for c in myString]
print(myList)

[‘H’, ‘e’, ‘l’, ‘l’, ‘o’]

70
New cards

What is the output for this code?

squares = [x*x for x in range (0,10)]
print(squares)

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]