Data structure
String
A string is a finite sequence of zero or more characters. In a Python program, a string can be represented if we enclose single or multiple characters in single or double inverted commas.
# Single-quoted, double-quoted strings
s1 = 'hello, world!'
s2 = "hello, world!"
# Strings that start with three double or single quotes can be line wrapped
s3 = """
hello,
world!
"""
# Use \ (backslash) in a string to indicate an escape
s4 = '\n\t\141\u9a86\u660a'
# Use r at the beginning of a string to de-escalate
s5 = r'\n\\\\hello, world!\\\\\n'
#output: \n\\\hello, world!\\\\n
print(s1, s2, s3, s4, s5 end='')
String operations
- Use the
+
operator for string concatenation - Use the
*
operator to repeat the contents of a string - Use
in
andnot in
to determine if a string contains another string (membership operations) - using the
[]
and[:]
operators to remove a character or characters from a string (slicing)
# string * operations
s1 = 'hello ' * 3
# hello hello hello hello
# string summing
s2 = 'world'
s1 += s2
# hello hello hello hello world
# String in, not in operations
print('ll' in s1) # True
print('good' in s1) # False
str2 = 'abc123456'
# Remove the character at the specified position from the string (subscript operation)
print(str2[2]) # c
# String slicing (from the specified start index to the specified end index)
print(str2[2:5]) # c12
print(str2[2:]) # c123456
print(str2[2::2]) # c246
print(str2[::2]) # ac246
print(str2[::-1]) # 654321cba
print(str2[-3:-1]) # 45
Common string functions
str1 = 'hello, world!'
# Calculate the length of a string with the built-in function len
len(str1) # 13
# Get an uppercase copy of the first letter of the string
str1.capitalize() # Hello, world!
# Get an uppercase copy of the first letter of each word in the string
str1.title() # Hello, World!
# Get an uppercase copy of the string
str1.upper() # HELLO, WORLD!
# Find the location of the substring from the string
str1.find('or') # 8
str1.find('shit') # -1
# Similar to find but raises an exception if no substring is found
# print(str1.index('or'))
# print(str1.index('shit'))
# Check if the string starts with the specified string
str1.startswith('He') # False
str1.startswith('hel') # True
# Check if the string ends with the specified string
str1.endswith('!') # True
# Center the string at the specified width and fill the sides with the specified characters
str1.center(50, '*')
# Place the string to the right with the specified width and fill the left side with the specified characters
str1.rjust(50, ' ')
str2 = 'abc123456'
# Check if the string is made up of numbers
str2.isdigit() # False
# Check if the string is made up of letters
str2.isalpha() # False
# Check if the string is made up of numbers and letters
str2.isalnum() # True
str3 = ' jackfrued@126.com '
# Get a copy of the string after trimming the spaces on the left and right sides
str3.strip()
String f
formatted output
Python 3.6 Later, there is a more concise way of writing formatted strings, by prefixing the string with the letter f
, and we can use the following syntactic sugar to simplify the above code.
a, b = 5, 10
print(f'{a} * {b} = {a * b}')
Lists (List)
Numeric types are scalar types, meaning that objects of this type have no internal structure that can be accessed; whereas a string type is a structured, non-scalar type, which is why it has a range of properties and methods. The list (list
), which we will introduce next, is also a structured, non-scalar type. It is an ordered sequence of values, each of which can be identified by an index, and can be defined by placing the elements of the list in []
, with multiple elements separated by ,
, iterating through the elements of the list using a for
loop, or using the []
or [:]
operator to take out one or more elements of the list.
Definitions and operations
list1 = [1, 3, 5, 7, 100]
print(list1) # [1, 3, 5, 7, 100]
# The multiplication sign indicates the repetition of a list element
list2 = ['hello'] * 3
print(list2) # ['hello', 'hello', 'hello']
# Calculate the length of the list (number of elements)
print(len(list1)) # 5
# Subscript (index) operations
print(list1[0]) # 1
print(list1[4]) # 100
# print(list1[5]) # IndexError: list index out of range
print(list1[-1]) # 100
print(list1[-3]) # 5
list1[2] = 300
print(list1) # [1, 3, 300, 7, 100]
# Iterate through the list elements with subscripts by looping
for index in range(len(list1)):
print(list1[index])
# Iterate through the list elements with a for loop
for elem in list1:
print(elem)
# Iterate through the enumerate function after processing the list to get both the index and the value of the element
for index, elem in enumerate(list1):
print(index, elem)
Element removal
list1 = [1, 3, 5, 7, 100]
### Add an element
list1.append(200)
list1.insert(1, 400)
# Merge two lists
# list1.extend([1000, 2000])
list1 += [1000, 2000]
print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
print(len(list1)) # 9
# First determine if the element is in the list by using the member operation, and if it is, delete the element
if 3 in list1:
list1.remove(3)
if 1234 in list1:
list1.remove(1234)
print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
# Remove the element from the specified position
list1.pop(0)
list1.pop(len(list1) - 1)
print(list1) # [400, 5, 7, 100, 200, 1000]
# Clear the list elements
list1.clear()
print(list1) # []
Slicing
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += ['pitaya', 'pear', 'mango']
# Slice the list
fruits2 = fruits[1:4]
print(fruits2) # apple strawberry waxberry
# You can copy the list by slicing it completely
fruits3 = fruits[:]
print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
fruits4 = fruits[-3:-1]
print(fruits4) # ['pitaya', 'pear']
# You can get a copy of the inverted list by slicing it backwards
fruits5 = fruits[::-1]
print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
Sort by
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
list2 = sorted(list1)
# The sorted function returns a sorted copy of the list without modifying the list passed in
# Functions should be designed like sorted functions to produce as few side effects as possible
list3 = sorted(list1, reverse=True)
# Specify sorting by string length instead of the default alphabetical order via the key keyword argument
list4 = sorted(list1, key=len)
print(list1)
print(list2)
print(list3)
print(list4)
# Send a sort message to the list object to sort directly on the list object
list1.sort(reverse=True)
print(list1)
Tuple
A tuple in Python is similar to a list in that it is a container data type that can be used to store multiple data in a single variable (object), the difference being that the elements of a tuple cannot be modified, and we've used tuples more than once in the previous code. As the name implies, we combine multiple elements together to form a tuple, so it can hold multiple pieces of data just like a list.
# Define a tuple
t = ('Luo Hao', 38, True, 'Sichuan Chengdu')
print(t)
# Get the elements of the tuple
print(t[0])
print(t[3])
# Iterate through the values in the tuple
for member in t:
print(member)
# Reassign values to the tuple
# t[0] = 'king sledgehammer' # TypeError
# Variable t is re-referenced to a new tuple The original tuple will be rubbish collected
t = ('Wang Sledgehammer', 20, True, 'Yunnan Kunming')
print(t)
# Convert the tuple to a list
person = list(t)
print(person)
# A list is able to modify its elements
person[0] = 'Bruce Lee'
person[1] = 25
print(person)
# Convert a list to a tuple
fruits_list = ['apple', 'banana', 'orange']
fruits_tuple = tuple(fruits_list)
print(fruits_tuple)
Sets
Sets in Python are identical to mathematical sets in that they do not allow duplicate elements, and they can perform operations such as intersection, union, and difference.
Creating and using
### Literal syntax for creating sets
set1 = {1, 2, 3, 3, 3, 2}
print(set1)
print('Length =', len(set1))
# Constructor syntax for creating sets (more on this in the object-oriented section)
set2 = set(range(1, 10))
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3)
# Derivative syntax for creating sets (derivatives can also be used to derive sets)
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
print(set4)
Add and delete
### Add
set1.add(4)
set1.add(5)
### Update
set2.update([11, 12])
# Delete
set2.discard(5)
if 4 in set2:
set2.remove(4)
print(set1, set2)
print(set3.pop())
print(set3)
Intersection, concatenation and difference operations
### Intersection, union, difference, and symmetric difference operations on sets
print(set1 & set2)
# print(set1.intersection(set2))
print(set1 | set2)
# print(set1.union(set2))
print(set1 - set2)
# print(set1.difference(set2))
print(set1 ^ set2)
# print(set1.symmetric_difference(set2))
# Determine subsets and supersets
print(set2 <= set1)
# print(set2.issubset(set1))
print(set3 <= set1)
# print(set3.issubset(set1))
print(set1 >= set2)
# print(set1.issuperset(set2))
print(set1 >= set3)
# print(set1.issuperset(set3))
Dictionaries
A dictionary is another mutable container model. A dictionary in Python is the same as a dictionary we use in life, it can store any type of object, unlike a list or a collection, each element of a dictionary is a "key-value pair" consisting of a key and a value, with the key and the value separated by a colon.
# The literal syntax for creating a dictionary
scores = {'Luo Hao': 95, 'Bai Yuanfang': 78, 'Di Renjie': 82}
print(scores)
# Constructor syntax for creating a dictionary
items1 = dict(one=1, two=2, three=3, four=4)
# Press two sequences into a dictionary with the zip function
items2 = dict(zip(['a', 'b', 'c'], '123'))
# The derivative syntax for creating a dictionary
items3 = {num: num ** 2 for num in range(1, 10)}
print(items1, items2, items3)
# You can get the corresponding value in the dictionary by key
print(scores['Luo Hao'])
print(scores['Dee'])
# Iterate over all key-value pairs in the dictionary
for key in scores:
print(f'{key}: {scores[key]}')
# Update the elements in the dictionary
scores['Bai Yuanfang'] = 65
scores['Zhuge Wanglang'] = 71
scores.update(冷面=67, 方启鹤=85)
print(scores)
if 'Wu Zetian' in scores:
print(scores['Wu Zetian'])
print(scores.get('Wu Zetian'))
# The get method also gets the corresponding value by key but can set the default value
print(scores.get('Wu Zetian', 60))
# Delete an element from the dictionary
print(scores.popitem())
print(scores.popitem())
print(scores.pop('Luo Hao', 100))
# Clear the dictionary
scores.clear()
print(scores)
Looping tips
When looping through a dictionary, use the items()
method to retrieve both the key and the corresponding value.
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
print(k, v)
# gallahad the pure
# robin the brave
When looping through a sequence, use the enumerate()
function to retrieve both the position index and the corresponding value.
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i, v)
# 0 tic
# 1 tac
# 2 toe
When looping two or more sequences at once, the zip()
function can match the elements within them one by one.
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
print(f'What is your {q}? It is {a}.')
# It is lancelot.
# What is your quest? It is the holy grail.
It is the holy grail. # What is your favorite color?
It is blue.
## Generative
``` python
f = [x for x in range(1, 10)]
print(f)
f = [x + y for x in 'ABCDE' for y in '1234567']
print(f)
# Create a list container using the list's generative expression syntax
# This syntax creates a list after the elements are ready so it takes more memory space
f = [x ** 2 for x in range(1, 1000)]
print(sys.getsizeof(f)) # See how many bytes of memory the object takes up
print(f)
# Note that the following code creates not a list but a generator object
# The data can be fetched through the generator but it does not take up extra space to store the data
# Get the data every time you need it by using internal operations (takes extra time)
f = (x ** 2 for x in range(1, 1000))
print(sys.getsizeof(f)) # compared to generative generators which do not take up space to store data
print(f)
for val in f:
print(val)
Generators
There is another way to define generators in Python, by transforming a normal function into a generator function using the yield
keyword.
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
yield a
def main():
for val in fib(20):
print(val)
if __name__ == '__main__':
main()