Basic concepts of python basics

Python basics is based on Google 's Python Class on Google for Education . This is a course very suitable for beginners.

English introduction

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!

course structure

The course mainly contains the following three modules, of course, the most important is the exercises at the end of the course.

  1. Documentation
  2. video
  3. Exercise
    course structure

Simple python example

Just like learning the C language and contacting the first "Hello world!" program, the following is a simple Python version of the "Hello world!" program.

  • Code example

    #!/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()
    

    Note: The
    first line #!/usr/bin/pythonof the code is to specify the path of the interpreter when using ./scrpt.py to execute the program. Obviously this is the path of linux, windows can be replaced by#!C:\Python2.7

basic concept

There are some basic concepts in python, such as modules and functions. Understanding these basic concepts is conducive to the understanding and writing of python programs.

Module

  • definition

A module (analogous to a class in a package in java) is a file that contains all the functions and variables you define, and its suffix is ​​.py. Modules can be introduced by other programs to use functions and other functions in the module. This is also the way to use the python standard library.

  • Code example
    • Self-defined module

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

      # 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__
      

function

  • Defined
    functions are organized, reusable, code segments used to implement a single or related function. Note on function definition

    1. Start with the def keyword, followed by the function name and parameters, and a colon
    2. End the function with return [expression]
    3. There are multiple parameter types
      • Mandatory parameters-generally defined parameters
      • Named parameters-copy statements as parameters
      • Default parameters-parameters have default values
      • Variable length parameter-marked with *, similar to pointer
  • Code example

    # 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()
    

If this article has helped you, or if you are interested in technical articles, you can follow the WeChat public account: Technical Tea Party, you can receive related technical articles as soon as possible, thank you!

Technical tea party

This article is automatically published by ArtiPub , a multi- posting platform

Guess you like

Origin blog.csdn.net/haojunyu2012/article/details/113064337