Python pit refers to north

1. A few questions

  1. Python2 or Python3?
  2. Why are there all kinds of inexplicable errors when installing Python package?
  3. How to add a third party package? Many packages have multi-level dependencies. How to solve this problem better?

2. Environment related

2.1 Anaconda

What is Anaconda and what are its advantages?

  • Anaconda is a python release that includes conda, Python, and a large number of Data Science libraries. The Most Popular Python Data Science Platform;

  • Conda is a management tool that manages packages, dependencies, and environments in multiple languages. Includes Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, FORTRAN; If you need to use Python2 and Python3 in your development, Conda can help you easily switch between different versions of the environment and manage the different versions of the package, so that the installation of the package is so easy, there will not be too many strange errors;

  • Anaconda includes Conda, Python, and a large number of data science libraries. If you don’t need that many libraries, you can also install Miniconda.

    Install a package

    conda install pkg_name

    Install multiple packages at once

    conda install numpy scipy pandas

    Install the specified version of the package

    Conda install numpy = 1.10

    Remove a package

    conda remove pkg_name

    # upgrade

    conda update

    # view all packages

    conda list

  • Let’s look at using Conda to manage different versions of Python environments;

    # create a python2.7 environment and install the pandas package

    Conda create -n py2 python=2.7 pandas

    Conda will install all packages for pandas



    Activate Py2 activate py2

    source activate py2

    Deactivate = deactivate = deactivate

    source deactivate

    # delete environment

    conda env remove -n py2

    List all created environments

    conda env list

    If you want to share python code and need to share the same development environment, you can export the current environment

    conda env export > environment.yaml

    Create the environment from the YAML file

    conda env create -f environment.yaml

That’s it. With Anaconda, you basically don’t have to worry about version conflicts, package installation, and integrated management environment. You don’t have to worry about environment issues, and you can spend your energy writing code.

2.2 IPython

IPython is a Python-based interactive interpreter; IPython provides more powerful editing and interaction capabilities than the native Python Shell. Has many advantages:

  1. Help information perfect, and support fuzzy matching (print? , print?? , np. * load *?) ; Adding a question mark after an object also lists relevant information (called the object’s introspection);
  2. Support TAB completion code, using history input (%history/%hist/hist command)
  3. %run runs external Python scripts, %load loads external Python scripts, and %paste runs clipboard content;
  4. Support terminal shortcut keys of shell;
  5. Magic Command (%timeit, %debug? PWD, %, etc…). ;
  6. %matplotlib Inline inline visualization display;

2.3 Jupyter Notebook

Jupyter Notebook (formerly known as IPython Notebook) is an open source Web application that creates and shares documents containing code, equations, visual interfaces, and text with the following benefits:

  1. Available languages: supports more than 40 programming languages, including Python, R, Julia, Scala, etc.
  2. Share documents: You can share your documents by email, Dropbox, Github, and Jupyter Notebook Viewer.
  3. Interactive output: Code can produce rich and interactive output, including HTML, images, videos, LaTeX, and custom MIME formats;
  4. Big data integration: Processes the same data in Python, R, or Scala using big data tools such as Apache Spark, pandas, SciKit-learn, GGplot2, and TensorFlow.

Jupyter Notebook has many other features to explore, such as remote access.

  • Jupyter Notebook – No-browser Does not automatically start the browser
  • Jupyter notebook – generate-config Generates configurations, changes access IP addresses, and adds passwords for login

3. Python language basics

  1. Indent split (4 Spaces), unlike C++ and Java, which use semicolons to separate each execution statement

  2. Without parentheses, loops and control statements use colons (:)

  3. It is not recommended to write assignments on the same line, for example, a = 5; b = 6; c = 7

  4. Everything is an object

  5. Note: Start with #

  6. Dynamic data type, a=5, is also true without redeclaring a= ‘foo’

  7. Strongly typed, a=5; B = ‘5’; A +b is an error, and implicit conversions are performed in weakly typed languages (PHP, JS, Perl)

  8. Mutable and immutable objects

    • The variable object
      • lists
      • dicts
      • NumPy arrays
    • Immutable object
      • string
      • tuples
  9. Scalar/atomic type

    • None
    • str
      • ‘hello,’
      • “The world”
      • Use “or” More often.”
    • bytes
      • Bytes
      • Unicode
    • Float division: 3/2=1.5, 3//2=1
    • bool
      • True
      • False
    • int
    • Type conversion
  10. Dates and times

  11. The control flow

    • if, elif, and else
    • for loops
      • for value in collection:
      • for a,b,c in iterator:
    • while loops
    • pass
    • range
  12. Ternary expressions

    • value = true-expr if condition else false-expr
  13. Built-in function

    • __builtin__
    • View all built-in functions dir(__builtin__)
    • Look at the built-in methods of a class
      • dir(list)
      • dir(tuple)

4. Mind mapping

The above contents are organized into the mind map of the introduction for subsequent review:

Python
Database design stuff