练习2--注释和#号

1 ”#“号的作用

  • 可以为程序作注解,即简单说明一下这段程序是干什么的;
  • 可以让我们不再需要的代码段不再被执行,或者帮助我们临时移除一段代码;

2 举例

  输入以下代码,并查看输出结果:

 # A comment,this is so you can read your program later.
 # Anything after the # is ignored by python.
 print ("I could have code like this.") # And the comment after is ignored.
 # You can also use a coment to "disable" or comment out a piece of code:
 # print "This won't run."
 print ("This will run.")

  输出结果:

          

3 扩展

  1. 为什么在 print("Hi # there.") 里 ,#就没有被忽略?     因为 # 在一个字符串里,计算机会打印引号之间字符串的所有内容, # 在字符串里被认为是一个字符,而不是注释符号。
  2. 我如何把很多行变成注成注释?    在每一行前面加一个 # 。
  3. 如果  #  是注解的意思,那么为什么  # -*- coding: utf-8 -*-  能起作用呢?   Python  其实还是没把这行当做代码处理,这种用法只是让字符格式被识别的一个取巧的方案,或者说是一个没办法的办法吧。在编辑器设置里你还能看到一个类似的注解。
原文地址:https://www.cnblogs.com/luoxun/p/13071152.html