Abstract: Python's built-in function sum() is an effective and Pythonic method for summing a list of values. Adding multiple numbers is a common intermediate step in many calculations, so sum() is a very convenient tool for Python programmers.
This article is shared from the HUAWEI cloud community " Python sum(): Pythonic sum method ", author: Yuchuan.
Python's built-in function sum() is an effective and Pythonic way to sum a list of values. Adding multiple numbers is a common intermediate step in many calculations, so sum() is a very convenient tool for Python programmers.
As an additional and interesting use case, you can concatenate lists and tuples using sum(), which can be very convenient when you need to flatten the list of lists.
In this tutorial, you will learn how to:
• Manually sum the values using common techniques and tools
• Use Pythonsum() to efficiently add multiple values
• Concatenate lists and tuples with sum()
• Use sum() to approach ordinary summation problems
• Use the appropriate value of the parameter in sum()
• Choose between sum() and alternative tools to summarize and concatenate objects
This knowledge will help you use sum() or other alternative and special tools to effectively deal with and solve the summation problem in the code.
Understand the summation problem
Adding numbers is a fairly common problem in programming. For example, suppose you have a list of numbers [1, 2, 3, 4, 5] and you want to add them together to calculate their sum. Using standard arithmetic, you will do the following:
1 + 2 + 3 + 4 + 5 = 15
In terms of mathematics, this expression is very simple. It will guide you through a series of short additions until you find the sum of all the numbers.
This particular calculation can be done manually, but imagine some other unlikely situations. If you have a particularly long list of numbers, adding them manually may be inefficient and error-prone. What happens if you don't even know how many items are in the list? Finally, imagine a scenario where the number of items you need to add changes dynamically or unpredictably.
In this case, whether your list of numbers is a long list or a short list, Python is very useful summation problem.
If you want to sum the numbers by creating your own solution from scratch, then you can try a for loop:
>>>
>>> numbers = [1, 2, 3, 4, 5]
>>> total = 0
>>> for number in numbers:
... total += number
...
>>> total
15
Here, you first create total and initialize it to 0. This variable is used as an accumulator where you can store intermediate results until you get the final result. The loop uses augmented assignment to accumulate each successive value to iterate and update the numbers. total
You can also wrap the for loop in a function. This way, you can reuse code for different lists:
>>>
>>> def sum_numbers(numbers):
... total = 0
... for number in numbers:
... total += number
... return total
...
>>> sum_numbers([1, 2, 3, 4, 5])
15
>>> sum_numbers([])
0
In sum_numbers(), you take an iterable object—especially a list of numbers—as a parameter, and return the sum of the values in the input list. If the input list is empty, the function returns 0. The for loop is the same as you saw before.
You can also use recursion instead of iteration. Recursion is a functional programming technique in which functions are called in their own definitions. In other words, the recursive function calls itself in the loop:
>>>
>>> def sum_numbers(numbers):
... if len(numbers) == 0:
... return 0
... return numbers[0] + sum_numbers(numbers[1:])
...
>>> sum_numbers([1, 2, 3, 4, 5])
15
When you define a recursive function, you risk getting into an infinite loop. To prevent this, you need to define stop recursive basic situation and call the function and start the cycle of implicit recursive situation .
In the above example, the base case means that the sum of the zero-length lists is 0. The recursive case means that the sum is the first value numbers[0], plus the sum of the remaining values numbers[1:]. Since the recursive case uses a shorter sequence in each iteration, you want to encounter the basic case when numbers is a zero-length list. As the final result, you will get the sum numbers of all items in the input list.
Note: In this example, if you don't check the empty input list (your base case), sum_numbers() will never encounter an infinite recursive loop. When the length of your numbers list reaches 0, the code will try to access the items in the empty list, which will raise an IndexError and break the loop.
With this implementation, you will never get a total from this function. You will get an IndexError every time.
Another option for summing lists of numbers in Python is to use reduce() from functools. To get the sum of a list of numbers, you can pass either operator.add or an appropriate lambda function as the first parameter to reduce():
>>>
>>> from functools import reduce
>>> from operator import add
>>> reduce(add, [1, 2, 3, 4, 5])
15
>>> reduce(add, [])
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value
>>> reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
15
You can call reduce() with reduce or fold, function together with iterable as a parameter. Then reduce() uses the input function to process the iterable and returns a single cumulative value.
In the first example, the reduction function is add(), which adds two numbers together. The final result is the sum of the numbers in the input iterable. As a disadvantage, reduce() raises a TypeError when you call it with an empty iterable.
In the second example, the reduction function is a lambda function that returns the addition of two numbers.
Since summations like this are very common in programming, writing a new function every time you need to sum some numbers is a lot of repetitive work. Also, usingreduce() is not the most readable solution available to you.
Python provides a special built-in function to solve this problem. This function is conveniently called sum(). Since it is a built-in function, you can use it directly in your code without importing anything.
Getting started with Python sum()
Readability is one of the most important principles behind the Python philosophy. When summing the list of values, visualize the actions you are asking to perform in a loop. You want it to iterate through some numbers, add them to an intermediate variable, and then return the final sum. However, you might imagine a more readable version of the sum that does not require loops. You want Python to take some numbers and add them together.
Now think about how to reduce() the sum. Using reduce() can be said to be less readable and less direct than a loop-based solution.
This is why Python 2.3 added sum() as a built-in function to provide a Pythonic solution to the summation problem. Alex Martelli contributed this function, which is now the preferred syntax for summing lists of values:
>>>
>>> sum([1, 2, 3, 4, 5])
15
>>> sum([])
0
Wow! It's neat, isn't it? It reads like plain English and clearly conveys the actions you perform on the input list. Using sum() is more readable than a for loop or reduce() call. Unlike reduce(), sum() will not raise a TypeError when you provide an empty iterable. Instead, it understandably returns 0.
You can call sum() with the following two parameters:
- iterable is a required parameter that can hold any Python iterable object. Iterable objects usually contain numeric values, but they can also contain lists or tuples.
- start is an optional parameter that can save an initial value. Then add this value to the final result. It defaults to 0.
Internally, sum() adds the value iterable in startplus from left to right. The values in the input iterable are usually numbers, but you can also use lists and tuples. The optional parameter start can accept numbers, lists or tuples, depending on the content passed to iterable. It cannot carry a string.
In the following two sections, you will understand the basics of sum() used in code.
Required parameters: iterable
Accepting any Python iterable as its first parameter makes sum() generic, reusable and polymorphic. Thanks to this feature, you can use sum() lists, tuples, sets, range objects, and dictionaries:
>>>
>>> # Use a list
>>> sum([1, 2, 3, 4, 5])
15
>>> # Use a tuple
>>> sum((1, 2, 3, 4, 5))
15
>>> # Use a set
>>> sum({1, 2, 3, 4, 5})
15
>>> # Use a range
>>> sum(range(1, 6))
15
>>> # Use a dictionary
>>> sum({1: "one", 2: "two", 3: "three"})
6
>>> sum({1: "one", 2: "two", 3: "three"}.keys())
6
In all these examples, sum() calculates the arithmetic sum of all values in the input iteration, regardless of their type. In both dictionary examples, sum() is called to return the sum of the input dictionary keys. The first example sums the keys by default, and the second example sums the keys due to the .keys() call to the input dictionary.
If your dictionary stores numbers in its values, and you want to sum these values instead of keys, then you can use .values() as in the .keys() example.
You can also use a list comprehension as a parameter in sum(). This is an example of calculating the sum of squares of a series of values:
>>>
>>> sum([x ** 2 for x in range(1, 6)])
55
Python 2.4 added generator expressions to the language. Similarly, sum() works as expected when you use a generator expression as a parameter:
>>>
>>> sum(x ** 2 for x in range(1, 6))
55
This example demonstrates one of the most Pythonic techniques for solving the summation problem. It provides an elegant, readable and efficient solution in one line of code.
Optional parameters: start
The second optional parameter start allows you to provide a value to initiate the summation process. This parameter is convenient when you need to process cumulative values in order:
>>>
>>> sum([1, 2, 3, 4, 5], 100) # Positional argument
115
>>> sum([1, 2, 3, 4, 5], start=100) # Keyword argument
115
Here, you provide the initial value 100to start. The net effect is that sum() adds this value to the cumulative sum of the values in the input iterable. Note that you can provide start as a positional parameter or a keyword parameter. The latter option is more clear and readable.
If you do not provide a value for start, it defaults to 0. The default value of 0 ensures the expected behavior of returning the sum of the input values.
Sum of log values
The main purpose of sum() is to provide a Pythonic way to add values. So far, you have learned how to use this function to sum integers. In addition, you can use sum() any other numeric Python types, such as float, complex, decimal.Decimal, and fractions.Fraction.
Here are a few examples of using sum() for different numeric types of values:
>>>
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> # Sum floating-point numbers
>>> sum([10.2, 12.5, 11.8])
34.5
>>> sum([10.2, 12.5, 11.8, float("inf")])
inf
>>> sum([10.2, 12.5, 11.8, float("nan")])
nan
>>> # Sum complex numbers
>>> sum([3 + 2j, 5 + 6j])
(8+8j)
>>> # Sum Decimal numbers
>>> sum([Decimal("10.2"), Decimal("12.5"), Decimal("11.8")])
Decimal('34.5')
>>> # Sum Fraction numbers
>>> sum([Fraction(51, 5), Fraction(25, 2), Fraction(59, 5)])
Fraction(69, 2)
Here, you are using sum() with floating-point numbers for the first time. It is worth noting that when you use the special symbols inf and nan to call float("inf") and in the function behavior float("nan"). The first symbol represents an infinite value, so sum() returns inf. The second symbol represents the NaN (not a number) value. Since you cannot add numbers to non-numbers, you will get a nan result.
Other examples sum the complex, Decimal and Fraction numbers of iterables. In all cases, sum() uses the appropriate numeric type to return the cumulative sum of the results.
Connection sequence
Although sum() is mainly used to manipulate numerical values, you can also use this function to concatenate sequences such as lists and tuples. For this, you need to provide the appropriate value for start:
>>>
>>> num_lists = [[1, 2, 3], [4, 5, 6]]
>>> sum(num_lists, start=[])
[1, 2, 3, 4, 5, 6]
>>> # Equivalent concatenation
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> num_tuples = ((1, 2, 3), (4, 5, 6))
>>> sum(num_tuples, start=())
(1, 2, 3, 4, 5, 6)
>>> # Equivalent concatenation
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
In these examples, you use sum() to concatenate lists and tuples. This is an interesting feature, you can use it to flatten a list of lists or tuples of tuples. The key requirement for the work of these examples is to select an appropriate value for start. For example, if you want to connect a list, start needs to hold a list.
In the above example, sum() performs the concatenation operation internally, so it only applies to sequence types that support concatenation, except for strings:
>>>
>>> num_strs = ["123", "456"]
>>> sum(num_strs, "0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]
When you try to use sum() to concatenate strings, you will get a TypeError. As the exception message suggests, you should use str.join() to concatenate strings in Python. Later, when you enter the section on using the alternative sum() method, you will see an example of using this method.
Use Python to practice sum()
So far, you have learned to use sum(). You have learned how to use this function to add values, and how to connect sequences such as lists and tuples.
In this section, you will see more examples of when and how sum() is used in your code. Through these practical examples, you will learn that this built-in function is very convenient when you perform calculations that require finding the sum of a series of numbers as an intermediate step.
You will also learn that this sum() can be very helpful when you use lists and tuples. A special example you will see is when you need to flatten a list of lists.
Calculate the cumulative sum
The first example you will write is about how to use the start parameter to sum a cumulative list of values.
Suppose you are developing a system to manage the sales of a given product at multiple different points of sale. Every day, you will receive sales unit reports from each point of sale. You need to systematically calculate the cumulative sum to understand how many items the entire company has sold in a week. To solve this problem, you can use sum():
>>>
>>> cumulative_sales = 0
>>> monday = [50, 27, 42]
>>> cumulative_sales = sum(monday, start=cumulative_sales)
>>> cumulative_sales
119
>>> tuesday = [12, 32, 15]
>>> cumulative_sales = sum(tuesday, start=cumulative_sales)
>>> cumulative_sales
178
>>> wednesday = [20, 24, 42]
>>> cumulative_sales = sum(wednesday, start=cumulative_sales)
>>> cumulative_sales
264
...
By using start, you can set an initial value to initialize the sum, which allows you to add continuous units to the previously calculated subtotals. At this weekend, you will get the total number of sales units of the company.
Calculate the average of the sample
Another practical use case for sum() is to use it as an intermediate calculation before performing further calculations. For example, suppose you need to calculate the arithmetic mean of a numerical sample. The arithmetic average, also known as the average, is the sum of the values in the sample divided by the number of values or data points.
If you have the sample [2, 3, 4, 2, 3, 6, 4, 2] and you want to calculate the arithmetic mean manually, then you can solve this operation:
(2 + 3 + 4 + 2 + 3 + 6 + 4 + 2) / 8 = 3.25
If you want to speed things up by using Python, you can divide it into two parts. The first part of this calculation, where you add the numbers together, is the task sum(). The next part of the calculation, dividing by 8, uses the number of counts in the sample. To calculate your divisor, you can use len():
>>>
>>> data_points = [2, 3, 4, 2, 3, 6, 4, 2]
>>> sum(data_points) / len(data_points)
3.25
Here, call sum() to calculate the sum of the data points in the sample. Next, you use len() to get the number of data points. Finally, you perform the required division to calculate the arithmetic mean of the sample.
In practice, you may want to convert this code into a function with some additional features, such as descriptive names and checking for empty samples:
>>>
>>> # Python >= 3.8
>>> def average(data_points):
... if (num_points := len(data_points)) == 0:
... raise ValueError("average requires at least one data point")
... return sum(data_points) / num_points
...
>>> average([2, 3, 4, 2, 3, 6, 4, 2])
3.25
>>> average([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in average
ValueError: average requires at least one data point
In the internal average(), you first check whether the input sample has any data points. If not, then your ValueError is raised with a descriptive message. In this example, you use the walrus operator to store the number of data points in a variable, num_points so that you don't need to call len() again. The return statement calculates the arithmetic mean of the samples and sends it back to the calling code.
Note: Calculating the average value of data samples is a common operation in statistics and data analysis. The Python standard library provides a convenient module called statistics to handle these types of calculations.
In the statistics module, you will find a function called mean():
>>>
>>> from statistics import mean
>>> mean([2, 3, 4, 2, 3, 6, 4, 2])
3.25
>>> mean([])
Traceback (most recent call last):
...
statistics.StatisticsError: mean requires at least one data point
The behavior of the statistics.mean() function is very similar to the average() function you coded earlier. When you call mean() with a numerical sample, you will get the arithmetic mean of the input data. When you pass an empty list to mean(), you will get a statistics.StatisticsError.
Note that when you call average() with an appropriate sample, you will get the desired average. If you call average() with an empty sample, you will get the expected result of ValueError.
Find the dot product of two sequences
Another problem you can use sum() is to find the dot product of two sequences of equal length values. The product of the algebraic sum of the dot product in each pair of values in the input sequence. For example, if you have the sequence (1, 2, 3) and (4, 5, 6), then you can manually calculate their dot product using addition and multiplication:
1 × 4 + 2 × 5 + 3 × 6 = 32
To extract consecutive pairs of values from the input sequence, you can use zip(). Then you can use a generator expression to multiply each pair of values. Finally, sum() can summarize the product:
>>>
>>> x_vector = (1, 2, 3)
>>> y_vector = (4, 5, 6)
>>> sum(x * y for x, y in zip(x_vector, y_vector))
32
Using zip(), you can generate a list of tuples using the values from each input sequence. The generator expression loops through each tuple while multiplying the previously arranged pairs of consecutive values by zip(). The last step is to add the products together using sum().
The code in the above example is valid. However, the dot product is defined for sequences of equal length, so what happens if you provide sequences of different lengths? In this case, zip() ignores the extra values in the longest sequence, which can lead to incorrect results.
To handle this possibility, you can wrap the call to sum() in a custom function and provide an appropriate check for the length of the input sequence:
>>>
>>> def dot_product(x_vector, y_vector):
... if len(x_vector) != len(y_vector):
... raise ValueError("Vectors must have equal sizes")
... return sum(x * y for x, y in zip(x_vector, y_vector))
...
>>> dot_product((1, 2, 3), (4, 5, 6))
32
>>> dot_product((1, 2, 3, 4), (5, 6, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in dot_product
ValueError: Vectors must have equal sizes
Here, dot_product() takes two sequences as parameters and returns their corresponding dot product. If the length of the input sequence is different, the function will raise a ValueError.
Embedding functions in custom functions allows you to reuse code. It also gives you the opportunity to name a function descriptively so that users can know what the function does just by reading the function name.
Flatten list list
Flattening lists of lists is a common task in Python. Suppose you have a list of lists and you need to flatten it into a list containing all the items in the original nested list. You can use any of a variety of methods to flatten lists in Python. For example, you can use a for loop, as shown in the following code:
>>>
>>> def flatten_list(a_list):
... flat = []
... for sublist in a_list:
... flat += sublist
... return flat
...
>>> matrix = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> flatten_list(matrix)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In flatten_list(), loop through all the nested lists contained in a_list. Then it flat connects them using the enhanced assignment operation (+=). As a result, you will get a flat list containing all the items in the original nested list.
But hold on! You have learned how to use sum() to concatenate sequences in this tutorial. Can you use this feature to flatten the list of lists as in the example above? Yes! That's it:
>>>
>>> matrix = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> sum(matrix, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
That's fast! One line of code, matrix is now a flat list. However, using sum() does not seem to be the fastest solution.
An important disadvantage of any solution that implies concatenation is that behind the scenes, each intermediate step creates a new list. This can be very wasteful in terms of memory usage. The final returned list is only the most recently created list among all lists created in each round of connection. Using list comprehensions ensures that you only create and return a list:
>>>
>>> def flatten_list(a_list):
... return [item for sublist in a_list for item in sublist]
...
>>> matrix = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> flatten_list(matrix)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
This new version of flatten_list() is more efficient in memory usage and less wasteful. However, nested comprehension can be difficult to read and understand.
Using .append() is probably the most readable and Pythonic way to flatten the list of lists:
>>>
>>> def flatten_list(a_list):
... flat = []
... for sublist in a_list:
... for item in sublist:
... flat.append(item)
... return flat
...
>>> matrix = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
>>> flatten_list(matrix)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In this version of flatten_list(), a person reading your code can see that the function traverses each sublist in a_list. In the first for loop, it traverses each itemsublist to finally flat use .append(). Just like the previous derivation, this solution only creates a list in the process. One advantage of this solution is that it is very readable.
Use alternative sum()
As you have already learned, sum() is generally helpful for handling numerical values. However, Python provides an alternative tool when dealing with floating-point numbers. In math, you will find a function called fsum(), which can help you improve the overall accuracy of floating-point calculations.
You may have a task where you want to connect or link multiple iterable objects so that you can process them as a whole. In this case, you can check the function chain() of the itertools module.
You may also have a task to concatenate a list of strings. As you learned in this tutorial, sum() cannot be used to concatenate strings. This function is not built for string concatenation. The most Pythonic alternative is to use str.join().
Sum floating-point numbers: math.fsum()
If your code continuously uses sum() for summing floating-point numbers, then you should consider using math.fsum() instead. This function performs floating-point calculations more carefully than sum(), thereby improving calculation accuracy.
According to its documentation, fsum() "avoids loss of precision by tracking multiple intermediate sums." The documentation provides the following example:
>>>
>>> from math import fsum
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
Using fsum(), you can get more accurate results. However, you should note that fsum() cannot solve the representation error in floating-point operations. The following example reveals this limitation:
>>>
>>> from math import fsum
>>> sum([0.1, 0.2])
0.30000000000000004
>>> fsum([0.1, 0.2])
0.30000000000000004
In these examples, the two functions return the same result. This is because it is impossible to accurately represent these two values 0.1 and 0.2 binary floating point numbers:
>>>
>>> f"{0.1:.28f}"
'0.1000000000000000055511151231'
>>> f"{0.2:.28f}"
'0.2000000000000000111022302463'
But, unlike sum(), fsum() can help you reduce floating-point error propagation when you add very large and very small numbers:
>>>
>>> from math import fsum
>>> sum([1e-16, 1, 1e16])
1e+16
>>> fsum([1e-16, 1, 1e16])
1.0000000000000002e+16
>>> sum([1, 1, 1e100, -1e100] * 10_000)
0.0
>>> fsum([1, 1, 1e100, -1e100] * 10_000)
20000.0
Wow! The second example is very surprising and fails sum() completely. Using sum(), you will get a result of 0.0. This is far from the correct result of 20000.0, as you get with fsum().
Connect iterable objects itertools.chain()
If you are looking for a convenient tool to connect or link a series of iterable objects, consider using chain()from itertools. This function can take multiple iterators and build an iterator that produces items from the first, second, and so on, until all input iterations are exhausted:
>>>
>>> from itertools import chain
>>> numbers = chain([1, 2, 3], [4, 5, 6], [7, 8, 9])
>>> numbers
<itertools.chain object at 0x7f0d0f160a30>
>>> next(numbers)
1
>>> next(numbers)
2
>>> list(chain([1, 2, 3], [4, 5, 6], [7, 8, 9]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
When you call chain(), you will get an iterator of items from the input iterable. In this example, you can access consecutive items using numbers using next(). If you want to use a list, then you can use list() to use an iterator and return a regular Python list.
chain() is also a good choice for flattening lists of lists in Python:
>>>
>>> from itertools import chain
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list(chain(*matrix))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
To use the chain() of a flattened list of lists, you need to use the iterable unwrapping operator (*). This operator unpacks all input iterable objects so that chain() can use them and generate corresponding iterators. The last step is to call list() to build the required flat list.
Connection string str.join()
As you have already seen, sum() does not concatenate or concatenate strings. If you need to do this, then the preferred and fastest tool available in Python is str.join(). This method takes a series of strings as parameters and returns a new connection string:
>>>
>>> greeting = ["Hello,", "welcome to", "Real Python!"]
>>> " ".join(greeting)
'Hello, welcome to Real Python!'
Using .join() is the most efficient and Pythonic way to join strings. Here, you use a list of strings as parameters and build a single string from the input. Note that .join() uses the string you called the method as the delimiter during the join. In this example, you call .join() for a string consisting of a single space character (" "), so the original string fromgreeting is separated by spaces in the final string.
in conclusion
You can now use Python's built-in function sum() to add multiple values together. This function provides an efficient, readable and Pythonic way to solve the summation problem in the code. If you are dealing with mathematical calculations that require the sum of values, then its sum() can be your savior.
In this tutorial, you learned how to:
• Use common techniques and tools to sum the values
• Use Python to effectively add multiple numerical sum()
• Use the concatenated sequence sum()
• Use sum() to approach ordinary summation problems
• Use the appropriate value of iterable and start argument in sum()
• Choose between sum() and alternative tools to summarize and concatenate objects
Armed with this knowledge, you can now add multiple values in a Pythonic, readable, and efficient manner.
Click to follow to learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。