
1.3 基本输入输出和运算
1.3.1 input()函数

input()是Python的内置函数,用于从控制台读取用户输入的内容。input()函数总是以字符串的形式来处理用户输入的内容,所以用户输入的内容可以包含任何字符。
input()函数的用法为:

其中,str表示一个字符串类型的变量,input()将读取到的字符串放入str中;tipmsg表示提示信息,它会显示在控制台上,告诉用户应该输入什么样的内容;如果不写tipmsg,就不会有任何提示信息。
还可以用Python的内置函数将输入字符串转换成想要的类型,如int(string)将字符串转换成int类型、float(string)将字符串转换成float类型、bool(string)将字符串转换成bool类型等。
【例1-13】使用input()来输入数字,并转化为int。

运算结果如下,其中type()为类型输出。

1.3.2 print()函数
1.多变量输出
print()函数既可以输出一个变量,也可以同时输出多个变量,而且它具有更多丰富的功能。

print()函数的语法格式如下:

其中,value参数可以接受任意多个变量或值,默认以空格分隔多个变量。如果希望改变默认的分隔符,可通过sep参数进行设置,比如sep='|'等。
2.格式化字符串(格式化输出)
print()函数使用以%开头的转换说明符对各种类型的数据进行格式化输出,具体见表1-4。转换说明符是一个占位符,它会被后面表达式(变量、常量、数字、字符串、加减乘除等各种形式)的值代替。
表1-4 格式化输出转换说明符

【例1-14】使用格式化输出。

运算结果:

3.指定最小输出宽度
可以使用下面的格式指定最小输出宽度(至少占用多少个字符的位置):
%10d表示输出的整数宽度至少为10;
%20s表示输出的字符串宽度至少为20。
【例1-15】使用格式化输出。

运算结果:

从本例的运行结果可以发现,对于整数和字符串,当数据的实际宽度小于指定宽度时,会在左侧以空格补齐;当数据的实际宽度大于指定宽度时,会按照数据的实际宽度输出,即num宽度为5,指定为%4d时,还是按照数据的实际宽度5进行输出。
4.指定对齐方式
默认情况下,print()输出的数据总是右对齐的。也就是说,当数据不够宽时,数据总是靠右边输出,而在左边补充空格以达到指定的宽度。Python允许在最小宽度之前增加一个标志来改变对齐方式,Python支持的标志见表1-5。
表1-5 Python支持的标志

另外需要说明的是:
1)对于整数,指定左对齐时,在右边补0是没有效果的,因为这样会改变整数的值。
2)对于小数,以上三个标志可以同时存在。
3)对于字符串,只能使用“-”标志。
【例1-16】指定对齐方式。

运算结果:

5.指定小数精度
对于小数(浮点数),print()允许指定小数点后的数字位数,即指定小数的输出精度。精度值需要放在最小宽度之后,中间用点号“.”隔开;也可以不写最小宽度,只写精度。具体格式如下:

其中,m表示最小宽度,n表示输出精度,“.”必须是存在的。
【例1-17】指定小数精度和对齐方式。

运算结果:

1.3.3 算术运算符
算术运算符即数学运算符,用来对数字或其他数据类型进行数学运算,比如加减乘除。表1-6列出了Python支持的所有基本算术运算符。
表1-6 常用算术运算符及功能说明

【例1-18】算术运算实例。


运算结果:

从本例中可以看出如下几个运算特点:
1)当“+”用于数字时表示加法,但是当“+”用于字符串时,它还有拼接字符串(将两个字符串连接为一个)的作用。
2)“-”除了可以用作减法运算之外,还可以用作求负运算(正数变负数,负数变正数)。
3)“*”除了可以用作乘法运算,还可以用来重复字符串,即将n个同样的字符串连接起来。
除此之外,Python还支持“/”和“//”两个除法运算符,但它们之间是有区别的:
“/”表示普通除法,使用它计算出来的结果和数学中的计算结果相同。
“//”表示整除,只保留结果的整数部分,舍弃小数部分,是直接丢掉小数部分,而不是四舍五入。
1.3.4 赋值运算符
赋值运算符用来把右侧的值传递给左侧的变量(或者常量),可以直接将右侧的值赋值给左侧的变量,也可以进行某些运算后再赋值给左侧的变量,比如加减乘除、函数调用、逻辑运算等。
Python中最基本的赋值运算符是等号“=”,结合其他运算符,“=”还能扩展出更强大的赋值运算符。
1.基本赋值运算符
“=”是Python中最常见、最基本的赋值运算符,用来将一个表达式的值赋给另一个变量。
2.连续赋值
Python中的赋值表达式也是有值的,它的值就是被赋的那个值,或者说是左侧变量的值。如果将赋值表达式的值再赋值给另外一个变量,这就构成了连续赋值。请看下面的例子:
a=b=c=100
“=”具有右结合性,从右到左分析这个表达式:
“c=100”表示将100赋值给c,所以c的值是100;同时,“c=100”这个子表达式的值也是100。
“b=c=100”表示将c=100的值赋给b,因此b的值也是100。
以此类推,a的值也是100。
最终结果就是,a、b、c三个变量的值都是100。
需要注意的是,“=”和“==”是两个不同的运算符,前者用来赋值,而后者用来判断两边的值是否相等,千万不要混淆。
3.扩展后的赋值运算符
“=”可与其他运算符(包括算术运算符、位运算符和逻辑运算符)相结合,扩展成为功能更加强大的赋值运算符,见表1-7。
表1-7 赋值运算符及功能说明

(续)

扩展后的赋值运算符将使得赋值表达式的书写更加优雅和方便。当然这种赋值运算符只能针对已经存在的变量赋值,因为赋值过程中需要变量本身参与运算,如果变量没有提前定义,它的值就是未知的,无法参与运算。
【例1-19】扩展后的赋值运算。

运算结果:

1.3.5 位运算符
位运算按照数据在内存中的二进制位(bit)进行操作,它一般用于算法设计、驱动、图像处理、单片机等底层开发。位运算符只能用来操作整数类型,它按照整数在内存中的二进制形式进行计算。Python支持的位运算符见表1-8。
表1-8 位运算符及功能说明

左移运算符“<<”用来把操作数的各个二进制位全部左移若干位,高位丢弃,低位补0。
例如,9<<3可以转换为如下的运算:

右移运算符“>>”用来把操作数的各个二进制位全部右移若干位,低位丢弃,高位补0或1。如果数据的最高位是0,那么就补0;如果最高位是1,那么就补1。
例如,9>>3可以转换为如下的运算:

【例1-20】位运算。

运算结果:


本例中出现的负数,其二进制的写法是“各位取反,再加一”。
1.3.6 比较运算符
比较运算符,也称关系运算符,用于对常量、变量或表达式的结果进行比较大小。如果这种比较是成立的,则返回True(真),反之则返回False(假)。True和False都是bool类型,它们专门用来表示一件事情的真假,或者一个表达式是否成立。表1-9所示是比较运算符及功能说明。
表1-9 比较运算符及功能说明

【例1-21】比较运算。

运算结果:


1.3.7 逻辑运算符
与位运算类似,逻辑运算也有与、或、非,具体见表1-10。
表1-10 逻辑运算符及功能说明

【例1-22】比较运算。

运算结果:

1.3.8 运算符优先级
运算符优先级就是当多个运算符同时出现在一个表达式中时优先执行哪个运算符。
例如,对于表达式“a+b*c”,Python会先计算乘法再计算加法;b*c的结果为8,a+8的结果为24,所以d最终的值也是24。先计算*再计算+,说明*的优先级高于+。
Python支持的几十种运算符被划分成19个优先级,有的运算符优先级不同,有的运算符优先级相同,具体见表1-11。
表1-11 运算符优先级和结合性一览表
