python基础篇之基本概念

python基础篇是以Google for Education上面的Google’s Python Class课程为主.这是一门非常适合初学者的课程.

英文介绍

Welcome to Google’s Python Class – this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google to introduce Python to people who have just a little programming experience. The first exercises work on basic Python concepts like strings and lists, building up to the later exercises which are full programs dealing with text files, processes, and http connections. The class is geared for people who have a little bit of programming experience in some language, enough to know what a “variable” or “if statement” is. Beyond that, you do not need to be an expert programmer to use this material.

To get started, the Python sections are linked at the left – Python Set Up to get Python installed on your machine, Python Introduction for an introduction to the language, and then Python Strings starts the coding material, leading to the first exercise. The end of each written section includes a link to the code exercise for that section’s material. The lecture videos parallel the written materials, introducing Python, then strings, then first exercises, and so on. At Google, all this material makes up an intensive 2-day class, so the videos are organized as the day-1 and day-2 sections.

This material was created by Nick Parlante working in the engEDU group at Google. Special thanks for the help from my Google colleagues John Cox, Steve Glassman, Piotr Kaminksi, and Antoine Picard. And finally thanks to Google and my director Maggie Johnson for the enlightened generosity to put these materials out on the internet for free under the under the Creative Commons Attribution 2.5 license – share and enjoy!

课程结构

该课程主要包含以下三大模块,当然最重要的莫过于课程后面的练习了.

  1. 文档
  2. 视频
  3. 练习
    课程结构

简单python样例

就像刚刚学习c语言接触第一个"Hello world!"程序一样,下面就是一个简单的python版的"Hello world!"程序.

  • 代码示例

    #!/usr/bin/python
    
    # 导入所用模块 -- sys 是常用的模块
    import sys
    
    # 代码写在main()里面
    def main():
        print 'Hello World!'
        # 命令行的参数在 sys.argv[1], sys.argv[2] ...
        # sys.argv[0] 表示脚本名称
    
    # 调用main()函数来启动程序的样板
    if __name__ == '__main__':
        main()
    

    注意:
    代码第一行#!/usr/bin/python是当使用./scrpt.py执行该段程序时指定解释器的路径. 显然这里是linux的路径,windows可以换成#!C:\Python2.7

基本概念

python里面有一些基本的概念,像模块,函数之类的,理解这些基本概念有利于对python程序的理解和编写.

模块

  • 定义

模块(类比java中包里面的类)是一个包含所有你定义的函数和变量的文件,其后缀名是.py。模块可以被别的程序引入,以使用该模块中的函数等功能。这也是使用python标准库的方法。

  • 代码示例
    • 自己定义的模块

      # coding=utf-8
      #!/usr/bin/python
      # Filename: mymodule.py
      
      # 模块中定义的函数
      def sayhi():   
          print '模块就是这样建造的.'
      
      # 模块中定义的变量
      version = '0.1'
      
    • 引用自己定义好的模块

      # coding=utf-8
      #!/usr/bin/python
      # Filename: mymodule_demo.py
      
      # 导入所写模块
      import mymodule
      
      # 代码写在main()里面
      def main():
          # 显示mymodule中定义的函数和变量
          print "mymodule中定义的函数和变量:" + "  ".join(dir(mymodule))
      
          # 显示当前main函数中定义的函数的变量
          print "main函数中定义的函数和变量:" + "  ".join(dir())
      
          # 显示__name__的值
          print "main函数中__name__变量的值:" + __name__
      
      
      # 调用main()函数来启动程序的样板
      if __name__ == '__main__':
          main()
      
          # 显示当前模块中定义的函数的变量
          print "当前模块中定义的函数和变量:" + "  ".join(dir())
      
          # 显示__name__的值
          print "当前模块中__name__变量的值:" + __name__
      

函数

  • 定义
    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数定义注意点

    1. 以def关键字开头,后面接函数名称和参数以及冒号
    2. 以return [expression]结束函数
    3. 参数类型有多个
      • 必备参数–一般定义的参数
      • 命名参数–复制语句作为参数
      • 缺省参数–参数有默认值
      • 不定长参数–用*标记,类似指针
  • 代码示例

    # coding=utf-8
    #!/usr/bin/python
    # Filename: function_demo.py
    
    
    # 导入所写模块
    import sys
    
    # 自定义函数
    # 打印输入的参数和本函数名
    def printStr( str ):
        # sys._getframe().f_code.co_name 能够获得当前函数的名称
        print sys._getframe().f_code.co_name , ":" , str
        return
    
    # 打印输入的不定长参数和本函数名
    def printChangePar( argc, *argv ):
        print sys._getframe().f_code.co_name , ":" , argc
        vars =""
        for var in argv:
            vars = vars + var + " "
            return vars
    
    
    
    # 代码写在main()里面
    def main():
        # 传入命名参数
        printStr( str = "I am here ." )
    
        # 传入不定长参数
        print printChangePar(4,"I","am","here",".")
    
    
    # 调用main()函数来启动程序的样板
    if __name__ == '__main__':
        main()
    

如果该文章对您产生了帮助,或者您对技术文章感兴趣,可以关注微信公众号: 技术茶话会, 能够第一时间收到相关的技术文章,谢谢!

技术茶话会

本篇文章由一文多发平台ArtiPub自动发布

猜你喜欢

转载自blog.csdn.net/haojunyu2012/article/details/113064337