Python2的安装

  • 对于Linux/Mac用户,你的电脑上应该是默认有Python2.7的.
  • 对于Windows用户:
    1. 安装Python的msi文件:到这里下载您的系统对应的Python-2.7...msi文件,如果您不知道您的系统应该下载哪一个msi,那么就点击字体加粗的那个msi文件链接.下载之后,双击下载的文件,会启动一个安装向导,在安装向导中一路下一步,直到完成安装.
    2. 告诉Windows你的Python安装在哪儿(为环境变量PATH增加一个路径,即Python的安装路径,默认为C:\Python27), 按照后面文章操作:windows10设置环境变量PATH

Python3的安装

根据您的操作系统,查看下面文章,可以忽略文章中安装Git的部分.

如何运行Python?

Linux/Mac下如何运行Python代码?
Windows下如何运行Python代码?

同时安装Python2和Python3的情况下,如何使用正确的版本?


练习题:选择

下面哪个变量名是合法的?(单选)
2hello
world-this
Hello
False
 
              

Python2和Python3的差异

  1. 在Python2中: 7 / 2 == 3 , 即整数除以整数,得到的还是整数。这和C语言是一致的,是计算机的思维方式。
  2. 在Python3中: 7 / 2 == 3.5 , 即整数除以整数,得到的是浮点数。这和人类的思维是一致的。

练习:选择题

89.3 // 5 = ?(单选)
17.86
17.0
17
 
              

Python2和Python3的差异

  1. 在Python2中: print "hello world"
  2. 在Python3中: print("hello world")

练习:填空

>>> a = [3, 4, 6, 7, 8, 9]
>>> a[:] == [4, 6, 7]
True
  
              


练习:填空

for num in [3, 4, 6, 7, 8, 9]:
    if num == :
        break
    if num == 3:
        
    print(num)

最后输出为:
4
6
  

想不明白运行过程?去可视化观看运行过程

课后练习:填空

 compute_profit(price_yuan, price_fen, cost_yuan=12, cost_fen=0):
      '''
      计算利润, 以分为单位
      '''
      diff_yuan = price_yuan - cost_yuan
      diff_fen = price_fen - cost_fen
       diff_yuan * 100 + diff_fen

compute_profit(15, 37) ==    # 正确

  
              

课后练习:填空

目录结构如下
server.py
models.py
deps---------tools.py
        |
        -----__init__.py
        |
        -----adapter--------__init__.py
                       |
                       -----xml.py
                       |
                       -----json.py (class JsonAdapter)

server.py中要使用到JsonAdapter, 那么导入的语句应该是:
from  import JsonAdapter

  

课后练习:填空

(3) = (3, )  # 表达式是正确的,值为True

  
              
a = {"a": 12, "c": 99}
a[] == 99  # 表达式值为True

  

              

Python3和Python2的区别

  • Python2: 类定义class Cat(object)
  • Python3: 类定义class Cat()
  • Python2: super语法: super(Cat, self).__init__(param)
  • Python3: super语法: super().__init__(param)

课后练习:填空题

class Dog:
    def (self):
        pass
    def bark(self, text):
        print(text)

jack = Dog()
("I Love Python")

 

Python3和Python2的区别

  • Python2: print "content", # 逗号避免了在最后增加一个回车
  • Python3: print("content", end="") # 参数end避免了在最后增加一个回车

课后练习:填空

 open("a.txt", 'w')  f:
    f.write("some text")

  
              

课后练习:填空

try:
    int("error input")
 ValueError  e:
    print(e.message)

  

进一步学习