This is the third day of my participation in Gwen Challenge

Write a series of articles documenting your learning process as you work with Python data recently. This series is presented primarily as a summary of the outline and is intended for application purposes.

Characteristics of 1.

Beginner friendly with a gentle learning curve

  1. Introduction of grammar
  2. Cross-platform support
  3. Support extension to other languages
  4. Open source
  5. Rich class library
  6. Interpreted, object-oriented, dynamic data type high-level programming language

2. Grammar

2.1. Writing rules

  1. #Character as comment
  2. importorfrom... importTo import the corresponding module
  3. Use indentation to represent code blocks without curly braces {}
  4. Single-line statements are too long, so we can use backslashes instead\To implement multi-line statements

2.2. Data types

2.2.1. Numeric Types (Number)

Python supports four major numeric types, including int, float, Boolean bool, and complex

Bool optional values are True and False. Non-0 is True and 0 is False

Numeric type Types support the following operations:

  • +add
  • -subtraction
  • *The multiplication
  • /Divide and get a floating point number
  • //Divide and get integers
  • %Take more than
  • 六四运动chengfang

Note: Python converts integers to floating-point numbers when mixing calculations.

2.2.2. Sequence Types (Sequence)

A sequence means that its members are ordered and one or more of its members can be accessed by subscript offsets,

There are three types: String, List, and tuple List

Concept:

  1. Strings are enclosed in single or double quotation marks, and special characters are escaped using a backslash **

     "123"
    Copy the code

    String elements cannot be modified

  2. A list is a comma-separated list of elements written between square brackets []

    [0, "123"]Copy the code

    The types of the elements in the list can be different, and the elements in the list can be modified

  3. Tuples are similar to lists except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), with elements separated by commas

    (" 123 ", "456")Copy the code

    Element types in tuples can also be different

The basic operations for sequence types include:

  • Member operator[not] in
  • Join operator+
  • Repeat operator*
  • Slice operator[:]
2.2.3. Set Types (Set)

Set is made up of one or a number of different shapes and sizes of the whole, the things or objects that constitute the set are called elements or members.

Creation method:

You can create collections using curly braces {} or the set() function

coll = {value01,value02,... } coll = set(value)Copy the code

Note: To create an empty collection, you must use set() instead of {}, because {} is used to create an empty dictionary.

Basic functions:

  1. Membership testing

    . in set

  2. Set operations

    • Difference set-
    • And set|
    • intersection&
    • Exclusive or^
2.2.3 Dictionary Types

A dictionary is an unordered collection of objects whose elements are accessed by keys

Concept:

  1. A dictionary is a mapping type. A dictionary is identified by {}. It is an unordered set of keys: values
  2. Keys must be of immutable type. A key must be unique in the same dictionary

Creation method:

Dict = {'key': 'value'} dict = {'key': 'value'}Copy the code
2.2.4. Type summary

Of the six standard data types for Python3:

  • ** Immutable data (3) : **Number (Number), String (String), Tuple (Tuple);
  • ** Mutable data (3) : **List (Dictionary), Set (Set).
2.2.5. Type judgment

The built-in type() function can be used to query the type of object to which the variable refers, as follows

<class 'STR '> >>>type("123")Copy the code

You can also use isinstance()

Isinstance (123, int)Copy the code

Isinstance differs from type in that:

  • type()A subclass is not considered a superclass type.
  • isinstance()A subclass is considered a superclass type.
2.2.6. Type conversion

Python is a weakly typed language that supports conversion from one type to another. Python has several built-in conversion functions, which are used as follows

>>>int("123")Copy the code