Introduction to Python
Python is an object-oriented, literal programming language
Easy to write, quick to get started, and rich open source expansion packages
Advantages and Disadvantages of Python
advantage
- Simple and easy to learn: Python’s code is relatively concise, the syntax is relatively standardized, and it is easy to get started.
- Open source: free for users to use
- Good portability: it can run on different platforms, but you need to pay attention to the configuration of the running environment
- Extensibility: Python has a rich set of third-party libraries
shortcoming
- Python and retrograde are much slower than C, Java, etc.
- Configuring packages is cumbersome
- There are incompatibilities between different versions
Data types supported by Python
Python does not need to declare its type when declaring a variable, and determines the type when assigning a value.
In a declared container variable, the internal element type is arbitrary
Since lists, sets, and dictionaries are used very frequently
In the data mining process, data is usually represented in the form of vectors and stored in lists.
list
A list can be understood as an ordered and repeatable array, mainly used to store data in order.
The elements in a list are ordered, each element has a position mark, and elements can be repeated
The basic operations of the list mainly include adding, deleting, modifying and other functions
Python provides a series of extended functions, such as querying list length, list operations , etc.
Add element
Delete element
Modify elements, query list length, list operations and determine whether an element is in the list
set
A set is an unordered non-repeating sequence
Collections can be created using curly braces { } or the set( ) function.
Note: To create an empty collection, you must use set() instead of { }, because { } is used to create an empty dictionary.
Create a collection called abc
Add element
Delete element
dictionary
Each element stored in the dictionary is a key:value pair (key:value)
The key cannot be repeated. If the same key is stored, its value will be replaced with the latest one.
dict={} represents using braces to declare an empty dictionary
The elements in a dictionary can be of any type, such as numbers, strings, lists, or even dictionaries
Add key-value pairs to dictionary
Remove key-value pairs from dictionary
identifier
Identifiers are defined names, including class names, variable names, etc.
Identifiers are case-sensitive and the first character must be a letter of the alphabet or "_"
In python3, Chinese characters can be used as variable names
You cannot use Python's own identifiers as identifiers, such as def, true, false, etc.
Comment
For single-line comments, use "#". The content of the line that appears after "#" will become a comment and will not be run.
Multi-line comments, use ''' or """ to handle multi-line comments
Use indentation to mark code blocks
In many programming languages, such as C++ and Java, { } is used to divide code segments.
Use indentation to control code snippets in Python
Use IDE (integrated Development Environment, integrated development environment) to edit code, with indentation reminders
The code is too long and needs to be split into multiple lines. You can use "\" to connect
There is no need to use "\" in the content in brackets, just wrap it directly.
Import other modules
The main function of import is to import the entire module or import modules within the module.
from···import is to import a method (function) from a module
Commonly used statements
- Conditional statements
- loop statement
- pass statement
The pass statement means nothing and is mainly used in some special locations.
Write a code that does nothing when i<50
Python editor
- PyCharm
Pycharm is developed by JetBrains and is a Python-specific IDE
The development version is free and can meet the needs of general developers.
- Spyder
- VIM
- Sublime
- Jupyter Notebook
Jupyter Notebook is a web-based application for interactive computing
Can be applied to the entire computing process: development, documentation, running code and displaying results