文章内容是根据笨办法学python课程中的代码修改为适合python3的代码
书中练习一和练习二 py2和py3的区别主要在于print函数
在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异
主要体现在以下几个方面:
1.python3中print是一个内置函数,有多个参数,而python2中print是一个语法结构;
2.Python2打印时可以不加括号:print ‘hello world’, Python3则需要加括号 print(“hello world”)
L1:第一个程序
书中代码改为
print("Hello World !")
print("Hello Again ")
print("I like typing this.")
print("This is fun.")
print("Yay! Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this. ')
输出部分的结果为:
Hello World !
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I’d much rather you ‘not’.
I “said” do not touch this.
L2:注释和井号"#"
# A comment, this is so you can your program later
# Anything after the # is ignored by python
print("I could have code like this .") # and the comment atfer is ignored
# You cant also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print("This will run.")
输出部分的结果为:
I could have code like this .
This will run.