sequence - a sequence is an object that holds data, stored one after another. You can perform operations on a sequence to examine and manipulate the items stored in it.
A list is a sequence and a tuple is a sequence.
List - a list is an object that contains data items. Lists are mutable / changeable. The list can hold items of different data types. Lists serve the same purpose as Arrays and provide many more built-in capabilities.
element - each item stored in a list.
stuff = [1,"fork", 3, "spoon"]
print(spoon)
list()
function - converts certain types of objects to list like a range
numbers = list(range(5))
step-value_numbers = list(range(1, 10, 2)
the variable numbers is assigned to be the value of the iterable range passed as an argument to the list
function.
numbers will print [0,1,2,3,4]
the variable step-value_numbers is assigned to be the value of the iterable range passed as an argument to the list
function. The first value is the start value of the range, the second value is the ending limit of the range and the third value is the step value - the value it will skip in the range.
Here it skips 2 so step-value_numbers will print [1,3,5,7,9]
* - The * is the repetition operator. It makes multiple copies of a list and joins them together.
list * n
numbers = [3] * 5
print(numbers)
>>[3,3,3,3,3]
numbers = [111,110,101,102]
for n in numbers:
print(n)
index - the index is the number at which each element of the list is located starting at zero.
negative index - you can use negative indexes to identify an element's position relative to the end of the list.
The last element of the list is always -1.
IndexError
- an ~IndexError` will be raised if you call an index that is not included in the list.
len
- is a function that returns the length of a sequence such as a list.
jens_list = ["Lars", "Pikku", "Friendly", "Kathy", "David", "Jason"]
size = len(jens_list)
index = 0
while index < len(jens_list):
print(jens_list[index])
index += 1
mutable - can be changed
numbers = [1,2,3,4,5]
print(numbers)
numbers[3] = 59
print(numbers)
If you want to use indexing expressions to fill a list with values you first have to create a list.
numbers = [0] * 5
index = 0
while index < len(numbers):
numbers[index] = "fill""
index += 1
list1 =[1,2,3,4,5]
print(list1)
list2 = [6,7,8,9]
print(list2)
list3 = list1 + list2
print(list3)
# or list1 += list2
p.299 slice - selects a range or span of elements from a sequence. Like a range slice also can use a step value.
list_name[start : end]
list_name[start : end : step_value]
days = ["Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"]
mid_days = days[2:5]
print(mid_days)
print(days[4:])
print(days[1:7:2])
This prints:
['Wed', 'Thurs', 'Fri']
['Fri', 'Sat', 'Sun']
['Tues', 'Thurs', 'Sat']
in
operator - searches for items in a list
item in list
- append(item)
- index(item)
- insert(index, item)
- sort()
- remove(item)
- reverse()
p.303
append(item)
- adds items to a list. The item is attached to the end of the list.
**** -
**** -
**** -
**** -
insert - inserts an item into a list
insert(subscript, item_to_be_inserted)
names = ["Steven, "Alex", "Zare"]
names.insert(2, "Jen")
names.insert(len(names), "Sam")
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
list1 = [1,2,3,4]
listcopy = [] + list1
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
isalnum -
iaalpha -
isdigit -
islower -
isspace -
isupper -
lower() -
lstrip() -
lstrip(char) -
rstrip() -
rstrip(char) -
strp() -
strip(char) -
upper() -
p.355
substring - a String that appearswithin other strings
**** -
**** -
printing substring with a slice of 2---
names = ["Aaron", "Mary", "Lars", "Jen", "Dan", "Heather"]
for i in range(0,len(names), 2):
print(names[i:i+2])
endswith(substring) -
find(substring) - searches for a specified substring within a string. The method returns the lowest index of the substring. If it is not found the method returns -1.
replace(old, new) -
startswith(substring) -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -
**** -