Basic Python knowledge (3): Basic python knowledge

Source: Internet
Author: User

Basic Python knowledge (3): Basic python knowledge
I. Functions

Def function name (): Code encapsulated by the function...
  • Def is short for define
  • Other Python files can be introduced and called.
  • DefinitionAnd other code including comments keep two blank lines

PycharmDebuggingHour

    NotePress Ctrl + Q to view the function description.

Ii. Modules

A module is a core concept of Python program architecture.

  • The module is like a toolkit and needs to be imported. import this module.
  • Each Python source code file ending with. py is a module.
  • The complete set variables and functions defined in the module are tools that can be directly used by the module.

Usage: (easy to reuse code that has been written)

(Popular Science) pyc file-optimization of startup speed

First, compile the import module into a bytecode file and put it in the _ pycache _ directory.

Iii. Advanced variable types

First, Mark that all non-numeric variables in Python support the following features:

1. List (the most frequently used data type ("array") | data of the same type is usually stored)
  • Used to store a string of information
  • The list is defined by [], and data is separated
  • Its Index starts from 0.

Common Operations: (. Press the tab key to view details)

(1) Increase

(2) Delete

Del keyword (delete) delete list elements (Popular Science)

In essence, a variable is deleted from the memory, that is, it cannot be used any more.

(3) Data Statistics (4) sorting (5) loop Traversal

A for loop can traverse all non-numeric variables: List, tuples, dictionaries, and strings.

for name in name_list:  print(name)

 

2. Tuple (elements cannot be modified | used to store different types of data)
  • Tuples represent a sequence composed of multiple elements.
  • Users store a string of Information separated by commas (,).
  • Defined ()
  • The index also starts from 0.
    • Create null tuple = ()
    • Name of the value tuples corresponding to the index [0]
    • The tuples only contain one element. You must add a comma (,) to the end of the element.
Common Operations: (. Press the tab key to view details)

Application scenarios
    • Conversion between tuples and lists
      • List (tuples) tuples --> list
      • Tuple (list) list --> tuples
3. dictionary (the most flexible data types in Python except the list)
  • You can also store multiple data.
    • It is usually used to store information about an object.
  • Differences from List
    • A list is a set of ordered objects.
    • A dictionary is a collection of unordered objects.
  • Dictionary defined {}
  • The dictionary uses key-value pairs to store data and key-value pairs for separation.
    • The key is the index.
    • Value is Data
    • Use: To separate keys and values
    • The key must be unique.
    • Values can be of any data type, but keys can only use strings, numbers, or tuples.
Common Operations: (. Press the tab key to view details)

 

Other operations

Loop Traversal  Application scenarios4. String (the data type of text information)
  • One pair of double quotes or one pair of single quotes can be used to define a string ("to define a string in most programming languages)
    • Flexible Use
  • Use the index to obtain the characters at the corresponding position, also starting from 0
  • You can use for loop Traversal

Common Operations: (. Press the tab key to view details)

  • (1) 9 types of judgment
  • (2) search and replace 7 types
  • (3) case-insensitive Conversion
  • (4) three types of text alignment
  • (5) Remove three types of white space characters
  • (6) splitting and connection 5 types
String slices (applicable to strings, lists, and meta groups)
  • Use the index value to define the range and cut a small set from a large set.
  • A dictionary is an unordered set that uses key-value pairs to store data.
String [start index: End index: Step Size]

 

Skip slice

You just want to end the index at the end of the string without writing it.

Try 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 truncated to the last one)
  • (8), num_str [-2:]
  • (9), num_str [:-1] (step-1 is Left tangent)

Related Article

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.