美文网首页python
5.Python_3: Comments and Quotes

5.Python_3: Comments and Quotes

作者: MissLady的学习志 | 来源:发表于2020-07-14 20:26 被阅读0次

****学 习 地 址 :****

计算机科学圈| 01000011 01010011 01000011

https://cscircles.cemc.uwaterloo.ca/2-functions/


1. Comments

Python中,任何一行从 # 号开始的代码代表注释的开始。这一行在 # 号之后的内容在运行的时候会被计算机忽略。

「注释的作用不在于表示代码的含义,而在于表示代码的功能。」

注释的常见作用:

  • 解释程序中的一部分,方便以后自己和他人阅读和理解程序;

  • 确认变量的用途或者阐明难以理解的代码段;

  • 当写一个比较长的程序时, 注释“待做”内容;

  • 暂时不运行(“注释化”)一行代码,而不是彻底删掉,这样在以后就可以很容易的把这行代码找回来。

2.Strings

字符串是字母和数字等字符构成的序列,是文本块。字符串之间用两个引号引起保护:例如"Hello, World!"是一个字符串。如果 # 号出现在字符串中,则不会被视为注释。

Python中在字符串中添加引号的两个简单方法。

  • 字符串的两边是单引号,比如'blah blah'。然后,在中间使用双引号,比如'I said "Wow!" to him.'

  • 添加右划线和引号(\" or \'。这个叫做 转义序列(`escape sequence)```,Python会自动忽略右划线,只把引号放到字符串里面。

3. Exercise & Answer

Coding Exercise:Second Guessing

Debug this program so that it prints out the number of seconds in a week.

my Answer

# goal: print out the number of seconds in a week 
secondsPerMinute = 60
secondsPerHour = secondsPerMinute * 60  # todo: check this!
secondsPerDay = secondsPerHour * 24
daysPerWeek = 5
daysPerWeek = daysPerWeek + 2 # weekends are disabled!?
print(secondsPerDay * daysPerWeek)

Coding Exercise:The Great Escape

Write a program that prints the following:

A double-quote's escaped using a backslash, e.g. "

my Answer

print("A double-quote\'s escaped using a backslash, e.g. \\\"")
print('A double-quote\'s escaped using a backslash, e.g. \\\"')
print("A double-quote\'s escaped using a backslash, e.g. ",end="\\\"")

参考:

[1]https://cscircles.cemc.uwaterloo.ca/

相关文章

网友评论

    本文标题:5.Python_3: Comments and Quotes

    本文链接:https://www.haomeiwen.com/subject/dbtqhktx.html