官术网_书友最值得收藏!

Expressions

The third major building block of Python programs is expressions. We've seen expressions in every example so far because it's nearly impossible to do anything in Python without using expressions.

Expressions consist of data values and operations to perform on those data values. The very simple expressions are a single data value and with no operations, for example, a single number. More complex expressions involve at least one operation and probably more data values as well, for example, adding two numbers or calculating the area, as shown in the following code example:

import math 
 
def example_function(name: str, radius: float) -> str: 
  area = math.pi * radius ** 2 
  return "The area of {} is {}" .format(name, area)  
 
print(example_function('Bob', 5)) 

All expressions produce a resulting data value of some sort; for example, adding two numbers produces the sum as another number, while concatenating two text strings produces the concatenation as another text string. Using a name variable to look up the stored value is an expression, so is running a function.

If the function doesn't explicitly return a value, the result is a special value called none.

Anywhere we need a value, we can use any expression that produces the needed value. It doesn't matter whether the expression is a simple number, such as 55, a variable name, a complex combination of values and operators, a function call, or any other expression. At least, it doesn't matter as far as the final result is concerned. Some expressions take less time to execute than others, so speed can be a factor.

主站蜘蛛池模板: 拉萨市| 女性| 开远市| 开鲁县| 手游| 乳源| 城步| 濮阳县| 湘潭市| 宜宾市| 微山县| 望奎县| 正镶白旗| 阿拉善左旗| 平湖市| 东乌珠穆沁旗| 教育| 花垣县| 化德县| 仙游县| 安岳县| 鹿泉市| 永定县| 沭阳县| 遵义市| 临夏市| 池州市| 昌吉市| 榆树市| 朝阳县| 通化市| 邢台县| 乐东| 丰顺县| 精河县| 开阳县| 砚山县| 新昌县| 南投县| 车致| 苍溪县|