练习1--第一个程序

一 注意事项:

1 .py文件的保存路径起名的时候名字里面不能带有“空格”,否则无法切换到该目录

2 python2和python3里面的print函数格式不一样,用python2对应的语法编辑的程序用python3解释器运行会报错

3 print()函数括号前空格个数多少不会报错,例如:print     ("Hello Again")

4 print()函数括号后加分号会报错,例如:print ("Hello World!");

二 代码:

1 python2语法下的第一个程序:

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.'

2 python3语法下的第一个程序:

 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.')

三 代码的执行

1  先用Set-location(默认使用powershell命令行程序)切换到程序所在目录

  • 注意:还可以用cd命令切换到指定路径,例如:cd  E:3_work4_python2_code2_LearnPythonTheHardWay
  • Set-location将当前PowerShell的工作位置设置为指定位置。 该位置可以是目录,注册表位置,子目录或任何提供程序路径。 slcdchdirSet-Location的别名。

2  然后输入python ex1.py回车即可

3  在运行 ex1.py 时遇到了遇到了 SyntaxError:invalid syntax 提示。

  • 提示: 很可能是已经运行了Python,然后又输了一次 python 。
  • 解决办法:关闭 Terminal,重新打开,然后只输入python3.6 ex1.py 。

四 执行结果

    

五 扩展

1 “#“:在一行代码最前面加入该符号可以注释该行,脚本不会输出该行的内容

2 python2里面如果想输出中文的话,需要在文件开头加一行:# -*- coding: utf-8 -*-  ,python3不需要,可以直接运行

原文地址:https://www.cnblogs.com/luoxun/p/13065932.html