- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 260字
- 2021-07-02 21:09:30
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.
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.
- 深度實(shí)踐OpenStack:基于Python的OpenStack組件開發(fā)
- Android和PHP開發(fā)最佳實(shí)踐(第2版)
- 信息安全技術(shù)
- C++ 從入門到項(xiàng)目實(shí)踐(超值版)
- Python漫游數(shù)學(xué)王國:高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計(jì)及運(yùn)籌學(xué)
- Python算法從菜鳥到達(dá)人
- Learning ArcGIS for Desktop
- Learning Nessus for Penetration Testing
- Hadoop大數(shù)據(jù)分析技術(shù)
- Training Systems Using Python Statistical Modeling
- 深度實(shí)踐KVM:核心技術(shù)、管理運(yùn)維、性能優(yōu)化與項(xiàng)目實(shí)施
- UML基礎(chǔ)與Rose建模實(shí)用教程(第三版)
- C# 7.0本質(zhì)論
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)教程
- 輕松學(xué)Scratch 3.0 少兒編程(全彩)