1/69
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
sequence
an object that holds multiple items of data, stored one after the other
What are the 2 ways to access the individual characters in a string?
for loops
indexing
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
What is the output for this code?
name = "Juliet"
for ch in name:
print chJ
u
l
i
e
t
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
What is the output for this code?
my_string = "Roses are red"
ch = my_string[6]
print cha
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
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
len()
returns the length of a sequence
useful in preventing loops from iterating beyond the end of a string
What does it mean for a string to be immutable?
it cannot be changed
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
slice
a span of items that are taken from a sequence
What do you get when you take a slice from a string?
a span of characters from the string
What are string slices called?
substrings
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
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
string testing methods
test a string for specific characters
isalnum()
true if the string contains only numeric digits and is at least one character long
isalpha()
true if the string contains only alphabetic letters and is at least one character long
islower()
true if the all characters in the string are lower case
isspace()
true if the string contains only whitespace characters (spaces, newlines, and tabs) and is at least on character long
isupper()
true if all the alphabetic letters are uppercase and it is at least on character long
string modification methods
return a modified version of strings
lower()
converts all alphabetic letters to lowercase
lstrip()
removes all leading whitespace characters (spaces, newlines, tabs at the beginning)
lstrip(char)
removes all instances of char that appear the beginning of the string
rstrip()
removes all trailing whitespace characters (spaces, newlines, tabs at the end)
rstrip(char)
removes all instances of char that appear at the end of the string
strip()
removes all leading and trailing whitespace characters
strip(char)
removes all instances of char that appear at the beginning and end of the string
upper()
converts all alphabetic letters to uppercase
search and replace methods
search for substrings and replace the occurrences of a substring with another string
endswith(substring)
true if the string ends with substring
find(substring)
returns the lowest index in the string where the substring is found; if substring is not found, returns -1
replace(old, new)
replaces all instances of old with new
startswith(substring)
true if the string starts with substring
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
What is the output for this code?
print("Hello"*5)HelloHelloHelloHelloHello
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
What is the output for this code?
numbers = [5, 10, 15, 20]
print numbers[5, 10, 15, 20]
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
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]
True or false: an expression in the form list[index] can appear on the left side of an assignment operator
true; lists are mutable
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]
append(item)
adds item to the end of the list
index(item)
returns index of the first element whose value is equal to item
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
sort()
sorts the items into ascending order
remove(item)
removes the first occurrence of item from the list; a ValueError exception is raised if item is not found in the list
del statement
removes an element from a specific index, regardless of the item that is stored at that index
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]
min()
accepts a sequence as an argument and returns the item that has the lowest value in the sequence
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
max()
accepts a sequence as an argument and returns the item that has the highest value in the sequence
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
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]
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
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”]
What is the output for this code?
date_string = "3/6/2008"
date_list = date_string.split("/")
print(date_list)[“3”, “6”, “2008”]
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
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
How are tuples advantageous over lists?
they are safe and faster in processing
list()
converts tuple to list
tuple()
converts list to tuple
What is the output for this code?
data = (1, 2, 3)
a, b, c = data
print(b)2
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
What is the output for this code?
nums = (3, 5)
print(add(*nums))8
list comprehensions
a concise way to create lists, offering a shorter syntax than loops
syntax: [expression for item in iterable]
What is the output for this code?
myString = "Hello"
myList = [c for c in myString]
print(myList)[‘H’, ‘e’, ‘l’, ‘l’, ‘o’]
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]