• 笨办法学Python(一)


    习题 1: 第一个程序

        你应该在练习 0 中花了不少的时间,学会了如何安装文本编辑器、运行文本编辑器、以及如何运行命令行终端,而且你已经花时间熟悉了这些工具。请不要跳过前一个练习的内容直接进行下面的内容,这也是本书唯一的一次这样的警示。

     1 print "Hello World!"
     2 
     3 print "Hello Again"
     4 
     5 print "I like typing this."
     6 
     7 print "This is fun."
     8 
     9 print 'Yay! Printing.'
    10 
    11 print "I'd much rather you 'not'."
    12 
    13 print 'I "said" do not touch this.'
    View Code

        将上面的内容写到一个文件中,取名为 ex1.py。注意这个命名方式,Python文件最好以 .py 结尾。

    Warning

        不要把上面内容最左边的数字也输进去。这些是所谓的“行号(line numbers)”,程序员在谈论到程序中某个位置的错误时会使用到行号。Python 在程序出错时也会以行号的方式告诉你错误信息,但是你是不需要输入这些行号的。

        然后你需要在命令行终端通过输入以下内容来运行这段代码:

    python2.exe ex1.py
    或者
    python2 ex1.py

        因为我的Windows里同时安装了Python2和Python3,所以我修改了Python2安装目录里的python.exe为python2.exe。

    当然,我们完全可以在cmd里运行(按住shift右键单击,选择在此处打开命令窗口)。

         如果我们把第三行的双引号去掉,则显示如下:

    这些内容你应该学会看懂的,这是很重要的一点,因为你以后还会犯类似的错误。就是我现在也会犯这样的错误。让我们一行一行来看。

      1. 首先我们在命令行终端输入命令来运行 ex1.py 脚本。

      2. Python 告诉我们 ex1.py 文件的第 3 行有一个错误。

      3. 然后这一行的内容被打印了出来。

      4. 然后 Python 打印出一个 ^ (井号,caret) 符号,用来指示出错的位置。 注意到少了一个 " (双引号,double-quote) 符号了吗?

      5. 最后,它打印出了一个“语法错误(SyntaxError)”告诉你究竟是什么样的错误。通常这些错误信息都非常难懂,不过你可以把错误信息的内容复制到搜索引擎里,然后你就能看到别人也遇到过这样的错误, 而且你也许能找到如何解决这个问题。

    Warning

      如果你来自另外一个国家,而且你看到关于 ASCII 编码的错误,那就在你的 python 脚本的最上面加入这一行: 

    # -- coding: utf-8 --

      这样你就在脚本中使用了 unicode UTF-8 编码,这些错误就不会出现了。

    加分习题

    你还会有 加分习题 需要完成。加分习题里边的内容是供你尝试的。如果你觉得做不出来,你可以暂时跳过,过段时间再回来做。

    在这个练习中,试试这些东西:

      1. 让你的脚本再多打印一行。

      2. 让你的脚本只打印一行。

      3. 在一行的起始位置放一个 ‘#’ (octothorpe) 符号。它的作用是什么?自己研究一下。

    从现在开始,除非特别情况,我将不再解释每个习题的工作原理了。

    Note

      井号有很多的英文名字,例如:’octothorpe(八角帽)’,’pound(英镑符)’, ‘hash(电话的#键)’, ‘mesh(网)’ 等。

    习题练习

    1.

     在最后一行后面添加一行打印为空(Python中的print不区分单引号和双引号,引号里的引号也会被输出)或者在在后一句里添加“ ”。

     1 print "Hello World!"
     2 
     3 print "Hello Again"
     4 
     5 print "I like typing this."
     6 
     7 print "This is fun."
     8 
     9 print 'Yay! Printing.'
    10 
    11 print "I'd much rather you 'not'."
    12 
    13 print 'I "said" do not touch this.'
    14 print ''
    View Code
     1 print "Hello World!"
     2 
     3 print "Hello Again"
     4 
     5 print "I like typing this."
     6 
     7 print "This is fun."
     8 
     9 print 'Yay! Printing.'
    10 
    11 print "I'd much rather you 'not'."
    12 
    13 print 'I "said" do not touch this.'
    14 print " "
    View Code
     1 print "Hello World!"
     2 
     3 print "Hello Again"
     4 
     5 print "I like typing this."
     6 
     7 print "This is fun."
     8 
     9 print 'Yay! Printing.'
    10 
    11 print "I'd much rather you 'not'."
    12 
    13 print 'I "said" do not touch this.
    '
    View Code

     

    2.

    在每一行后面加逗号即可

     1 print "Hello World!",
     2 
     3 print "Hello Again",
     4 
     5 print "I like typing this.",
     6 
     7 print "This is fun.",
     8 
     9 print 'Yay! Printing.',
    10 
    11 print "I'd much rather you 'not'.",
    12 
    13 print 'I "said" do not touch this.',
    View Code

     3.

    #是注释符,#后面的内容不执行

     1 print "Hello World!"
     2 
     3 print "Hello Again"
     4 
     5 print "I like typing this."
     6 
     7 #print "This is fun."
     8 
     9 print 'Yay! Printing.'
    10 
    11 print "I'd much rather you 'not'."
    12 
    13 print 'I "said" do not touch this.
    '
    View Code

  • 相关阅读:
    CSS实现简易相册方法
    php乱码问题
    javascript 面向对象程序设计 模式 随笔
    php 关闭notice级别的提示
    javascript var散布问题
    CSS实现简易幻灯片功能
    Asp.net单点登录解决方案
    把你的电脑,变成无线路由器(图文教程)
    开发中常见问题解决
    MVC 将IList<T>导出Excel文档的泛型类(继承自ActionResult)
  • 原文地址:https://www.cnblogs.com/yllinux/p/7044037.html
Copyright © 2020-2023  润新知