python note 18 (review)

Today's Content

  • Review

Detailed Content

1. Getting started with Python

1.1 Setting up environment

Set up python environment on mac system.

The role of environment variables: to facilitate the execution of executable programs on the command line (terminal), add the directory where the executable programs are located to the environment variables, and then do not need to enter a path in the future.

1.2 Variable Naming

  • variable

    name = 'alex'
  • global variable

    NAME = "oldboy"
  • function

    def func_list():
        pass 
  • constant

    Modified values are not allowed, and conventions are enforced in Python.

1.3 Operator

v = 1 or 2 
v = 0 or 2 
v = 1 and 2 
v = 0 and 2 
val =  v  if v else 666
val = v or 666 # Source code

The difference between is and ==

1.4 Data Types

  • int

    • integer
    • Other decimal conversion decimal int('0b11011',base=2)
    • int / long
    • division
  • bool

    • 0 None Empty
  • str, string type, commonly used for data operations in memory.

    v = 'alex' # unicode encoding is stored in memory.
  • bytes, byte type, commonly used for data storage and network transmission.

    v = 'alex'.encode('utf-8')  # Convert strings to bytes (from unicode encoding to utf-8 encoding)
    v = 'alex'.encode('gbk')    # Convert strings to bytes (from unicode encoding to gbk encoding)
  • list

  • tuple

  • dict

  • set

  • None

1.5 Encoding

  • py default interpreter encoding
    • py2
    • py3
  • Storage in what encoding opens in what encoding (it is recommended that Pycharm be set to UTF-8 encoding).

  • Common encoding
    • ascii
    • unicode
    • utf-8
    • gbk
    • gb2312

1.6 Shallow and Deep Copies

Recreate a copy of the variable type.

  • Shallow copy: first layer
  • Deep copy: all layers.
import copy

v = [11,22,33,[11,22,3]]
v1 = copy.copy(v1)
v2 = copy.deepcopy(v)

1.7 py2 and py3 differences

  • String types are different
    • py3: str bytes
    • py2: unicode str
  • Default interpreter encoding
  • Input and Output
  • int
    • int long
    • division
  • range and xrang
  • Modules and packages
  • Dictionaries
    • keys
      • py2: list
      • py3: an iterator that can be looped but not indexed
    • values
      • py2: list
      • py3: an iterator that can be looped but not indexed
    • items
      • py2: list
      • py3: an iterator that can be looped but not indexed
  • map/filter
    • py2: return to list
    • py3: returns an iterator that can be looped but not indexed

2. Functions

2.1 Built-in Functions

  • Common built-in functions: open / id / type / len / range...
  • The difference between is and ==Check by id

2.2 Custom Functions

  • Functional programming: Increase code readability and reuse.

  • Function basic format:

    def show(name,age):
        """
        What is a function?
        :param name: 
        :param age: 
        :return: 
        """
        return None
  • Function as parameter

  • Function as variable

  • Function as return value

    • closure
    • Decorator
  • generator

    def func():
        print(123)
        yield 1
        yield 3
    
    # generator
    v = func()
    # Loop V or v. u next_().
    def base():
        yield 88
        yield 99
    
    def func():
        yield 1
        yield 2
        yield from base()
        yield 3
    
    result = func()
    
    for item in result:
        print(item)

    Generator Derivation

    # def func():
    #     result = []
    #     for i in range(10):
    #         result.append(i)
    #     return result
    # v1 = func()
    v1 = [i for i in range(10)] # A list derivation that immediately loops through all elements.
    print(v1)
    
    # def func():
    #     for i in range(10):
    #         yield i
    # v2 = func()
    v2 = (i for i in range(10)) # Generator derivation creates a generator whose internal loop is execution.
    
    # Interview Question: Compare the differences between [i for i in range(10)] and (i for i in range(10)))?
    # Example 1
    # def func():
    #     result = []
    #     for i in range(10):
    #         result.append(i)
    #     return result
    # v1 = func()
    # for item in v1:
    #    print(item)
    
    # Example 2
    # def func():
    #     for i in range(10):
    #         def f():
    #             return i
    #         yield f
    #
    # v1 = func()
    # for item in v1:
    #     print(item())
    
    # Example three:
    v1 = [i for i in range(10)] # A list derivation that immediately loops through all elements.
    v2 = (lambda :i for i in range(10))
    for item in v2:
        print(item())
  • iterator

3. Modules

3.1 Built-in Modules

  • Common built-in modules: json / datetime / time / os / sys

3.2 Third Party Modules

  • requests
  • xlrd,xlwd

3.3 Custom Modules

  • file
  • Folder/Package

3.4 Using modules

  • Import

    • import module

    • from module. Module import module

    • from modules.Modules.Module import functions

    • Relative import [not recommended]

      from . import xxx
      from .. import xxx

    Note: Files and folders cannot be named the same as imported modules, otherwise they will be found directly in the current directory.

  • Invoke module internal elements

    • Function ()
    • Modules. Functions ()

Tags: Python encoding Mac network

Posted on Fri, 14 Feb 2020 08:53:33 -0800 by vidyashankara