py文件的运行

 安装过程及配置

  1. 安装过程准备:

    下载好Python的安装程序后,开始安装,在进入安装界面后一定确保勾选将Python加入到系统环境变量的路径里。如图所示:

    python怎么运行py文件
  2. 2

    如果没有选取,那么按照下面的步骤进行操作。在桌面上用鼠标右键点击我的电脑并选择属性选项。如图所示:

    python怎么运行py文件
  3. 3

    在弹出的属性设置菜单中点击高级系统设置。如图所示:

    python怎么运行py文件
  4. 4

    在高级系统设置面板中点击环境变量。如图所示:

    python怎么运行py文件
  5. 5

    在弹出的环境变量设置中找到系统环境变量设置,并选中path选项,双击,或者点击编辑选项。如图所示:

    python怎么运行py文件
  6. 6

    在弹出的编辑窗口中,新建一个环境变量为python安装路径。添加后进行保存并退出。如图所示:

    python怎么运行py文件
     

  执行py命令方法:

    1. 打开运行栏的方法使用win+r组合快捷键。在运行栏中输入cmd打开命令行窗口
      
       
      在命令提示符窗口中首先进入py命令所在的文件夹。dos命令中切换根目录直接输入驱动器盘符即可:
      
       
      直接键入python xx.py后回车确认。得到正确的运行结果。本例是个最简单的hello,world!的程序,运行后在命令行窗口中显示该串字符

        变量

      计算机的主要作用之一:进行计算,用 Python进行数据运算非常容易,跟我们平常用计算机一样简单。

      在算总消费的时候直接用的是之前已经算好的中间结果,这样是为了避免重新再算一遍所有的数据。
      错误的写法:
      >>> print('eat',10+15+7+4+7+3)
      eat 46
      >>> print('cloth',20)
      cloth 20
      >>> print('traffic',6+6+6+6+6)
      traffic 30
      >>> print('精神',300+300+400+200)
      精神 1200
      >>> 
      >>> 
      >>> print('总消费', 46+20+30+1200)
      总消费 1296
      这么写之所以是不正确,是因为最后算总消费的时候 是人肉 把之前算出来的分类结果 填进去的, 但是我们把程序写在脚本里运行时, 计算机肯定不会预先知道吃饭、交通、买衣服3个分类的结果的,所以这个结果是我们自己动态算出来的,而不是计算机算出来的。

      正确的写法是,直接把每个分类结果先起个名字存下来,然后计算总消费的时候,只需要把之前存下来的几个名字调用一下就可以了。
      >>> eat = 10+15+7+4+7+3
      >>> cloth = 20
      >>> traffic = 6+6+6+6+6
      >>> Spirit=300+300+200+400
      >>> 
      >>> total = eat + cloth + traffic + Spirit
      >>> print('总消费',total)
      总消费 1296
      • 变量:eat、cloth、traffic、Spirit、total 这几个的作用就是把程序运算的中间结果临时存到内存里,可以让后面的代码继续调用,这几个的学名就叫做“变量”。
      • 变量的作用 :
      Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.(变量用于存储要在计算机程序中引用和操作的信息。它们还提供了一种用描述性名称标记数据的方式,这样读者和我们自己就能更清楚地理解我们的程序。将变量视为包含信息的容器是有帮助的。它们的唯一目的是在内存中标记和存储数据。然后这些数据可以在整个程序中使用)。

      • 声明变量:
      name = "Shan Ying"
      变量名(标识符):name  
      变量值:Shan Ying
      • 定义变量的规则:
                   ①变量名只能是 字母、数字或下划线的任意组合。
                   ②变量名的第一个字符不能是数字。
                   ③不能使用python中的关键字(如下)声明为变量名:
                       'and'、 'as'、 'assert'、 'break'、'class' 、 'continue'、'def'、 'del'、'elif'、'else'、'except'、 'exec'、'finally'、 'for'、 'from'、 'global'、 'if'、 'import'、 'in'、 'is'、
         'lambda'、'not'、'or'、 'pass'、 'print'、'raise'、'return'、'try'、 'while'、 'with'、'yield'
      • 变量命名习惯:
                         ①驼峰体
                                AgeOfGF = 18
                                AgeOfBF = 80
                         ②下划线
                                age_of_gf = 18 
                                number_of_bf = 80
      官方推荐使用第二种命名习惯,看起来更加的清晰。
      • 定义变量的LOW 方式:
                       ①变量名为中文、拼音。
                       ②变量名过长。
                       ③变量名词不达意。
       

       常量

       常量即指不变的量,例如,pai=3.141592653..., 或在程序运行过程中不会改变的量。在Python中没有一个专门的语法代表常量,约定俗成用变量名全部大写代表常量

                       在c语言中有专门的常量定义语法,const int count = 60;  一旦定义为常量,更改即会报错。

      用户交互

      name = input("What is your name:")
      print("Hello " + name )
      执行脚本后,发现程序会等输入姓名后才能往下继续走。

      让用户输入多个信息:

      name = input("What is your name:")
      age = input("How old are you:")
      hometown = input("Where is your hometown:")
      print("Hello ",name , "your are ", age , "years old, you came from",hometown)
      执行输出:
      What is your name:Guang'tou'qiang
      How old are you:18
      Where is your hometown:DaShenLin
      Hello  Guang'tou'qiang your are  18 years old, you come from DaShenLin

      注释

      代码注释分为单行和多行注释。 单行注释用#,多行注释可以用三对双引号"""  """

      例:
      def subclass_exception(name, parents, module, attached_to=None):
          """
          Create exception subclass. Used by ModelBase below.

          If 'attached_to' is supplied, the exception will be created in a way that
          allows it to be pickled, assuming the returned exception class will be added
          as an attribute to the 'attached_to' class.
          """
          class_dict = {'__module__': module}
          if attached_to is not None:
              def __reduce__(self):
                  # Exceptions are special - they've got state that isn't
                  # in self.__dict__. We assume it is all in self.args.
                  return (unpickle_inner_exception, (attached_to, name), self.args)

              def __setstate__(self, args):
                  self.args = args

              class_dict['__reduce__'] = __reduce__
              class_dict['__setstate__'] = __setstate__

          return type(name, parents, class_dict)
      • 代码注释的原则:

                
         ①不用全部加注释,只需要在自己觉得重要或不好理解的部分加注释即可。
         ②注释可以用中文或英文,但绝对不要用拼音。
       

猜你喜欢

转载自www.cnblogs.com/bypp/p/10316833.html