Notes
Strings: "..." #immutable
+ (concatenation), * (repeat strings), len (string length), in (check whether it is an element in the string)
Triple-quotes (""") can enclose strings that span multiple lines.
Common Escape Sequences: \\ (blackslash \), \' (single quotation '), \" (double quotation "), \t (tab), \n (newline)
Strings can be compared using relational operators such as ==, !=, >, <, >=, and <=
Conversion between a string character and an Unicode Code:
chr(Unicode) Return the string representing a character whose Unicode code is Unicode.
ord(Character) Return an integer representing the Unicode code of the Character.
String methods
upper(), lower() change to uppercase/lowercase
index(str), find(str) finds the first occurrence of the specified value. index shows exception if the value is not found while find return -1.
findall(pattern, string) return a list of strings match pattern.
split(sep = " ", max = -1) split the string by sep and return a list
join() concatenate elements of an iterable into string with the string as seperator.
strip(letters) return a string with letters remove from front and back
Lists: [...] #mutable
+ (concatenation), * (repeat list), len (list length), in (check whether it is an element in the list)
methods
append(elem)
insert(index,elem)
remove(elem)
pop([index])
count(elem)
index(elem)
zip() function
l1 = [1,2,3,4,5]
l2 = ["a","b","c","d","e"]
z = zip(l1,l2)
l3 = list(z)
print(l3)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
unzipping
a,b = zip(*z)
print(a)
(1, 2, 3, 4, 5)
print(b)
('a', 'b', 'c', 'd', 'e')
List Comprehension
l1 = [1,2,3,4,5]
l2 = []
for e in l1:
l2.append(e)
#is equivalent to
l2 = [e for e in l1]
print(l2)
[1, 2, 3, 4, 5]
l3 = [e for e in l1 if e % 2 == 0]
print(l3)
[2, 4]
t4 = [("a",e) for e in l1]
print(t4)
[('a', 1), ('a', 2), ('a', 3), ('a', 4), ('a', 5)]
enumerate() functinon
l=["a","b","c","d","e"]
em1=enumerate(l)
l1=list(em1)
print(l1)
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
em2=enumerate(l,2)
l2=list(em2)
print(l2)
[(2, 'a'), (3, 'b'), (4, 'c'), (5, 'd'), (6, 'e')]
Tuples: (...) #immutable
+ (concatenation), * (repeat list), len (tuple length), in (check whether it is an element in the tuple)
tuple with only 1 element must be end with a trailing comma. Ex. (1,)
Index positions in Strings, Lists, and Tuples
Let's take a look at the string "Hello"
Getting an Element
string1 = "Hello"
print(string1[0]) #output "H", the index of the first position is 0
print(string1[-1]) #output "o", the index of the first position count from the back is -1
list1 = [45, "ok", [1,3]] #this is a nested list, meaning there is a list inside another list
print(list1[0]) #output 45, the index of the first position is 0
print(list1[2][1]) #output 3. The first part list1[2][1] return the element of index 2 in list1, in this case it is the list [1,3]. The second part list1[2][1] return the element of index 1 in the list [1,3], which is 3
tuple1 = (45, "ok", (1,3)) #this is a nested tuple, meaning there is a tuple inside another tuple
print(tuple1 [0]) #output 45, the index of the first position is 0
print(tuple1 [2][1]) #output 3. The first part tuple1[2][1] return the element of index 2 in tuple1, in this case it is the tuple (1,3). The second part tuple1[2][1] return the element of index 1 in the tuple (1,3), which is 3
Getting a Subset Syntax
string_list_or_tuple[starting_position:ending_position(not including):step]
#Important: the subset ends before the ending position (not including the element in the ending position)
#If the starting position is omitted, that mean the subset including everything from the first element
#If the ending position is omitted, that mean the subset including everything to the last element (Important: including the last element)
Getting a Subset Example
string1 = "Hello"
print(string1[1:3]) #output "el", get the subset of elements from index 1 to index 2, not including index 3
print(string1[:3]) #output "Hel", get the subset of elements from first element (index 0) to index 2, not including index 3
print(string1[1:]) #output "ello", get the subset of elements from index 1 to the last element (index 3), including index 3
print(string1[0:4:2]) #output "Hl", get the subset of elements from index 0 to index 3, not including index 4
#same rules for lists
list1 = [1,"a",5,"b", ["h",2]] #this is a nested list, meaning there is a list inside another list
print(list1[2:4]) #output [5, "b"], get the subset of elements from index 2 to index 3, not including index 4
#same rules for tuples
tuple1 = (1,"a",5,"b", ("h",2)) #this is a nested tuple, meaning there is a tuple inside another tuple
print(tuple1[2:4]) #output (5, "b"), get the subset of elements from index 2 to index 3, not including index 4
Check Whether an Element is in a String, a List, or a Tuple Syntax
element in string_or_list
#return True or False
Check Whether an Element is in a String, a List, or a Tuple Example
string1 = "Hello"
print("e" in string1) #output True
print("a" in string1) #output False
list1 = [45, "ok", [1,3]]
print(45 in list1) #output True
print(1 in list1) #output False
print([1,3] in list1) #output True
tuple1 = (45, "ok", (1,3))
print(45 in tuple1) #output True
print(1 in tuple1) #output False
print((1,3) in tuple1) #output True
Mutable and Immutable Objects
list1 = [1, 2, 3, 4]
list1[0] = 9 #change the first element in list1 to 9.
print(list1) #output [9, 2, 3, 4]
tuple1 = (1, 2, 3, 4)
tuple1[0] = 9 #attempt to change the first element in tuple. However an error occurs because tuples are immutable objects
Adding Element(s) to a List
list1 = [1, 2, 3, 4]
#Adding an element to the end of a list
list1.append(5) #Add an element 5 at the end of list1.
print(list1) #output [1, 2, 3, 4, 5]
#Adding more than one elements to the end of a list
list1.extend([6,7,8]) #Add elements 6,7,8,9 at the end of list1.
print(list1) #output [1, 2, 3, 4, 5, 6, 7, 8]
#What happen if we use append instead of extend in this case?
list1.append([6,7,8])
print(list1) #output [1, 2, 3, 4, 5, 6, 7, 8, [6, 7, 8]]. The list [6, 7, 8] is added to the end of list1 instead of the individual elements 6, 7, 8
#Add an element at a specified index.
list1.insert(3, "a") #Add an element "a" at index 3
print(list1) #output [1, 2, 3, 'a', 4, 5, 6, 7, 8, [6, 7, 8]]
#append, extend, and insert method do not work on tuples
Delete an Element from a List
list1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
#Delete an element from a list by value
list1.remove("c") #Remove the element with value "c". An ValueError error will occur if the value does not exist in the list
print(list1) #output ['a', 'b', 'd', 'e', 'f', 'g', 'h']
#delete an element at a given index
del list1[4] #Remove the element at index 4
print(list1) #output ['a', 'b', 'c', 'd', 'f', 'g', 'h']
#delete an element at a given index and return its value
deleted_element = list1.pop(3) #Remove the element at index 3 and return its value; if no index is specified, then the last element is removed.
print("You deleted " + deleted_element) #output You deleted e
print(list1) #output ['a', 'b', 'd', 'g', 'h']