Basic knowledge of Python (iii)

Source: Internet
Author: User

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

    1. F8 Step over stepping code, the function call will be treated as a line of code directly executed
    2. 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)

    1. Define a variable or function in a Python file, and import the module using import in another file
    2. 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:

    1. is a sequence sequence, which can also be understood as a container
    2. Value []
    3. Traverse for
    4. Calculate length, maximum/small value, compare, delete
    5. Link + and repeat *
    6. 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
    1. Append append data to the end of the list
    2. Insert inserts data at the specified position in the list
    3. Extend can append the entire contents of the other list to the end of the current list

(2) Delete
    1. Remove deletes the specified data by default deletes the first one element
    2. Pop default Deletes the last data of the list, and if you specify a parameter, you can delete the index of the element
    3. 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
    1. Len (list) List length
    2. List. Count (data) Number of occurrences of data in the list
(4) Sort
    1. List. Sort Ascending order
    2. List. Sort (reverse=true) in descending order
    3. 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)

    1. Index gets the subscript of a certain data in a tuple
    2. Count Statistics counts
    3. Len (tuple) tuple length
    4. Looping through the same list (typically for a few reasons that are not used for tuples)
Application Scenarios
    1. Parameters and return values for functions
      • A function can receive any number of arguments, or return more than one data at a time
    2. format string
    3. 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
    1. Use multiple key-value pairs to describe an object's related information
    2. 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 )
      • Flexible use
    • 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.