First, the function
def function name (): The code encapsulated by the function ...
- DEF is an English define abbreviation
- Other Python files can be introduced into the calling
- Save two blank lines when defining and other code including comments
Pycharm when debugging
- F8 Step over stepping code, the function call will be treated as a line of code directly executed
- F7 Step into stepping code, if it's a function, it goes inside the function.
Note When shortcut keys ctrl+q View description information for a function
Second, the module
Modules are a core concept of the Python program architecture
- The module is like a toolkit, you need to import it to use it, import this module
- Every Python source code file that ends with a. PY is a module
- All the variables and functions defined in the module are tools that the module can provide to the outside world for direct use.
How to use: (Easy to reuse once written code)
- Define a variable or function in a Python file, and import the module using import in another file
- After importing, you can use the module name. variable/module name. The way the function is used, the variable or function defined in the module
(PST) PYC file--optimization of startup speed
First compile the import module into a byte-code file in the __pycache__ directory
III. Types of Advanced variables
First mark. All non-numeric variables in Python support the following features:
- is a sequence sequence, which can also be understood as a container
- Value []
- Traverse for
- Calculate length, maximum/small value, compare, delete
- Link + and repeat *
- Slice
1. List (the most Frequently used data type ("array") | The same type of data usually exists)
- Dedicated to storing a bunch of information
- Lists are defined with [], data is separated by
- Its index starts from 0
Common operations: (. Press the TAB key to view)
(1) Increase
- Append append data to the end of the list
- Insert inserts data at the specified position in the list
- Extend can append the entire contents of the other list to the end of the current list
(2) Delete
- Remove deletes the specified data by default deletes the first one element
- Pop default Deletes the last data of the list, and if you specify a parameter, you can delete the index of the element
- Clear to clear the list
del keyword (delete) delete list element (PST)
The essence is to remove a variable from memory, which is no longer possible to continue using the variable.
(3) Data statistics
- Len (list) List length
- List. Count (data) Number of occurrences of data in the list
(4) Sort
- List. Sort Ascending order
- List. Sort (reverse=true) in descending order
- List. Reverse () reverse, reverse
(5) Loop traversal
For loops can traverse all variables of non-numeric type : list, tuple, dictionary, and string
for inch name_list: Print (name)
2, tuple (tuple) (element cannot be modified | Typically used to store different types of data)
- A tuple represents a sequence of multiple elements
- Users store A string of information, separated by data,
- Define with ()
- The index also starts from 0
- Create empty tuple tuple = ()
- The value tuple name corresponding to the fetch index [0]
- A tuple contains only one element, and you need to add a comma after the element
Common operations: (. Press the TAB key to view)
- Index gets the subscript of a certain data in a tuple
- Count Statistics counts
- Len (tuple) tuple length
- Looping through the same list (typically for a few reasons that are not used for tuples)
Application Scenarios
- Parameters and return values for functions
- A function can receive any number of arguments, or return more than one data at a time
- format string
- Keep the list from being modified to keep your data safe
-
- Converting between tuples and lists
- List (tuple) tuples--Lists
- Tuple (list) List---tuples
3. Dictionary (dictionary) (the most flexible data type in Python except for the list)
- can also store multiple data
- Typically used to store information that describes an object
- and List of differences
- List is an ordered collection of objects
- A dictionary is an unordered collection of objects
- Dictionary defined with {}
- Dictionaries use key values to store data, to use between key-value pairs, and to separate
- Key is index
- Value is the data
- Use between keys and values: delimited
- The key must be unique
- Values can take any data type, but keys can only use strings, numbers, or tuples
Common operations: (. Press the TAB key to view)
Other Operations
Looping through
Application Scenarios
- Use multiple key-value pairs to describe an object's related information
- Place multiple dictionaries in a single list, and then iterate through the inside of the loop body for each dictionary operator
4. String (The data type that represents the text information)
- Use a pair of double quotes or a pair of single quotes to define a string ( most programming languages use "to define a string )
- Use the index to get the corresponding position of the character , also starting from 0
- You can use the For loop to traverse
Common operations: (. Press the TAB key to view)
- (1) Type of Judgment 9
- (2) Find and replace 7 kinds of
- (3) Capitalization conversion 5 kinds
- (4) Text alignment 3 kinds
- (5) 3 kinds of blank characters are removed
- (6) 5 kinds of splitting and connecting
Slicing of strings (for strings, lists, and tuples)
- Use index values to qualify ranges, cut out small collections from a large collection
- A dictionary is an unordered collection that uses key-value pairs to hold data
Jumping slices
You want to cut to the end of the string, the index does not write
Try to do some examples ~
Num_str = "0123456789"
- (1), Num_str[2:6]
- (2), num_str[2:]
- (3), Num_str[0:6]/num_str[:6]
- (4), num_str[:]
- (5), Num_str[::2]
- (6), Num_str[1::2]
- (7), Num_str[2:-1] ( -1 is intercepted to the bottom of the first)
- (8), Num_str[-2:]
- (9), Num_str[::-1] (step-1 i.e. to Zoche)
Basic knowledge of Python (iii)