
上QQ阅读APP看书,第一时间看更新
4.1 什么是运算符和表达式

↑扫码看视频
运算符是具有运算功能的符号,而表达式则是由值、变量和运算符组成的式子。表达式的作用是将运算符的运算作用表现出来。在本节中,将简要讲解Python运算符和表达式的基础知识。
先看一道小学数学课上的题目,如图4-1所示。

图4-1 小学数学题目
在图4-1中用到了多个四则运算符号,其中+、-、×、÷就是运算符,而算式“75+360 ÷20-5”就是一个表达式。除了+、-、×、÷运算符外,和数学有关的运算符还有>、≥、≤、<、∫、%等。例如下面的数学运算式就是一个表达式。

在Python编辑器中的表现形式如下所示。

单一的值或变量也可以当作是表达式,具体演示如下所示。

当Python显示表达式的值时,显示的格式与输入时的格式是相同的。如果输入的是字符串,则意味着包含引号。而打印语句输出的结果不包括引号,只有字符串的内容。例如下面演示了有引号和没有引号的区别。
