Python pit refers to north
1. A few questions
- Python2 or Python3?
- Why are there all kinds of inexplicable errors when installing Python package?
- 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:
- 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);
- Support TAB completion code, using history input (%history/%hist/hist command)
- %run runs external Python scripts, %load loads external Python scripts, and %paste runs clipboard content;
- Support terminal shortcut keys of shell;
- Magic Command (%timeit, %debug? PWD, %, etc…). ;
- %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:
- Available languages: supports more than 40 programming languages, including Python, R, Julia, Scala, etc.
- Share documents: You can share your documents by email, Dropbox, Github, and Jupyter Notebook Viewer.
- Interactive output: Code can produce rich and interactive output, including HTML, images, videos, LaTeX, and custom MIME formats;
- 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
-
Indent split (4 Spaces), unlike C++ and Java, which use semicolons to separate each execution statement
-
Without parentheses, loops and control statements use colons (:)
-
It is not recommended to write assignments on the same line, for example, a = 5; b = 6; c = 7
-
Everything is an object
-
Note: Start with #
-
Dynamic data type, a=5, is also true without redeclaring a= ‘foo’
-
Strongly typed, a=5; B = ‘5’; A +b is an error, and implicit conversions are performed in weakly typed languages (PHP, JS, Perl)
-
Mutable and immutable objects
- The variable object
- lists
- dicts
- NumPy arrays
- Immutable object
- string
- tuples
- The variable object
-
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
-
Dates and times
-
The control flow
- if, elif, and else
- for loops
- for value in collection:
- for a,b,c in iterator:
- while loops
- pass
- range
-
Ternary expressions
- value = true-expr if condition else false-expr
-
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: