1/29
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
string
is a sequence of characters that represents textual data like a person's name, a location, or a message to the user.
string literal
is a string defined in the source code of a program by surrounding the text value with single or double quotes, like 'MARY' or "MARY".
sequence type
a type that orders a collection of objects into a sequence from first to last.
len()
built-in function that is used to find the length of a string (and any other sequence type like a list)
Brackets
A programmer can reference a character at a specific index by appending brackets [ ] containing the desired index after the name of a string variable:
string concatenation
A program can add new characters to the end of a string in a process
aster is 5 characters long
ageratum is 8 characters long
fav_flower1 = input()
fav_flower2 = input()
print(fav_flower1, "is", len(fav_flower1), "characters long")
print(fav_flower2, "is", len(fav_flower2), "characters long")
The third character of grape is a
fav_fruit = input()
print("The third character of", fav_fruit, "is", fav_fruit[2])
States I've visited: Utah, North Carolina, Nevada
visited_list_name = input()
place1 = input()
place2 = input()
place3 = input()
places_visited_str = visited_list_name + ": " + place1 + ", " + place2 + ", " + place3
print(places_visited_str)
Your name is May
name = "May" |
Container
is a construct used to group related values together and contains references to other objects instead of data.
List
is a container created by surrounding a sequence of variables or literals with brackets [ ]. Ex: my_list = [10, "abc"] creates a new list variable my_list that contains the two items: 10 and "abc".
Element
A list item is called an element. A list is mutable, meaning that the elements in a list can be replaced, reordered, or removed.
Index
A list is also a sequence type, meaning the contained elements are ordered by position in the list, known as the element's index,
method
instructs an object to perform some action and is specified by providing the object name followed by a "." symbol and then the method name.
append()
list method is used to add new elements to the end of a list.
pop()
removes the element at the given index from the list. "bw", which is at index 1, is removed and 'abc' is now at index 1. Using pop(), a specific element can be removed according to its position in the list
remove()
removes the first element with a given value. "abc" is removed and now the list only has one element. remove() is useful for removing a specific element from a list according to its value.
[10, “bw”]
After append: [10, “bw”, “abc”]
my_list = [10, “bw”]
print(my_list)
my_list.append(“abc”)
print(f”After append: {my_list}”)
house_prices[1] = "$175,000"
Write a statement that performs the desired action. Assume the list house_prices = ["$140,000", "$550,000", "$480,000"] exists.
1)
Update the price of the second item in house_prices to "$175,000".
Sequence-type methods
are functions built into the Python language definitions of sequence types like lists and strings.
Some of the functions and methods useful to lists
| Find the length of the list. |
| Produce a new list by concatenating list2 to the end of list1. |
| Find the element in the list with the smallest value. All elements must be of the same type. |
| Find the element in the list with the largest value. All elements must be of the same type. |
| Find the sum of all elements of a list (numbers only). |
| Find the index of the first element in the list whose value matches val. |
|
List data_list is created with four integers read from input. Output the fourth element multiplied by the third element minus the first element.
data_list = [int(input()), int(input()), int(input()), int(input())]
print(data_list[3] * data_list[2] - data_list[0])
Input
9
6
10
5
Your output
['9', '10', '5']
# Reads three values from input into years_list
years_list = [input(), input(), input()]
years_list.pop(1)
new_year = input()
years_list.append(new_year)
print(years_list)
index_value with the index of the element 8 in data_list.
index_value = data_list.index(8)
tuple
stores a collection of data, like a list, but is immutable – once created, the tuple's elements cannot be changed.Typically, tuples are surrounded with parentheses, as in (5, 15, 20). Note that printing a tuple always displays surrounding parentheses.
namedtuple
allows the programmer to define a new simple data type that consists of named attributes
set
is an unordered collection of unique elements. A set can be created using the set() function, which accepts a sequence-type iterable object (list, tuple, string, etc.)
| |