python学习2——变量和命名。

命名变量

1、= 是赋值的符号,== 才表示等于。

2、命名时用 _ 代替空格。

3、命名是用引号表示字符串。

4、介绍几个格式化字符,类似的还有很多

      %a     浮点数、十六进制数字

      %c     字符

      %d     有符号十进制整数

      %f      浮点数

      %s     字符串

如:

>>> my_height = "160"

>>>print("She is %d cm tall." %my_height)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
print("She is %d cm tall." %my_height)
TypeError: %d format: a number is required, not str

由于变量命名时用了双引号,所以不能用%d,需要去掉双引号或者使用%s 引用。

>>> my_height = 160
>>> print("She is %d cm tall." %my_height)
She is 160 cm tall.

  >>> my_height = "160"
  >>> print("She is %s cm tall." %my_height)
  She is 160 cm tall.

猜你喜欢

转载自www.cnblogs.com/shannon-V/p/9493889.html
今日推荐