Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

This article summarizes six common data-type operations in Python:

  • Numbers
  • String (String)
  • List (List)
  • Tuple (Tuple)
  • The Dictionary
  • Set

Numbers, Numbers

Numbers are common, such as 1,2,100,999, etc., and two common conversion functions: int and float. Common operations on numeric data:

1.1 Arithmetic Operation

The arithmetic operation returns a specific value:

  • Add: +
  • Cut: –
  • Passenger: *
  • In addition: /
  • Power: * *
  • Find the remainder: %
  • Dealer: / /

1.2 Comparison Operation

The comparison returns a Boolean value: True or False

  • More than: >
  • Less than:”
  • Is equal to: = =
  • Less than or equal to: <=
  • Greater than or equal to: >=
  • Does not equal: <> or! =

1.3 Common Functions

  1. Take the absolute value: ABS
  2. Round: round
  3. Integer: int
  4. To float: float

String

Strings are one of the common data types in Python, and you can use the STR function to force other types of data into character types

2.1 Keyboard Input

Anything entered in the terminal through the input function is string data

2.2 Three methods of generation

String data is generated in three ways:

  • Single quotes: ‘python’
  • Double quotes: “python”
  • “””I am learning Python…” “”

2.3 Index and Slice

1. Index:

The index function allows you to view the index value

2. About slices:

  • Standard form:start:stop:step
  • Start without tail: contains the start part, but does not contain the stop part
  • When slicing, the left side of the index starts at 0 and the right side starts at -1
  • Step size can be positive or negative

2.4 Common Functions

  1. Find the length: len
  2. Return the maximum value: Max, min
  3. View the encoding of the character: ord
  4. Look at the encoding (numeric) corresponding character: CHR
  5. Member judge: In
  6. The string repeats: *
  7. Join: + or join

2.5 Common Operations

  1. Check whether all letters are: isalpha
  2. Cutting: the split
  3. Remove Spaces:
    • Strip () : Spaces at both ends
    • Lstrip () : left space
    • Rstrip () : space on the right
  4. Case conversion:
    • Upper () : uppercase letters
    • Lower () : Lower all letters to lowercase
    • Isupper () : checks whether all letters are uppercase
    • Islower () : checks whether all letters are converted to lower case
    • Capitalize () : Capitalize all the first letters
    • Title () : uppercase the first letter of all words in the string and lowercase the rest
    • Istitle () : checks whether it is in title mode. That is, all words in the string are capitalized and other words are lowercase
  5. String format
    • The placeholder %
    • The format function
    • f-string

This is a List of books

Lists are arguably the most active data type in Python, with extremely frequent use and many operations:

3.1 create

  • Through the list function
  • Square brackets[]To create a

3.2 Common Functions

  1. Find the length: len
  2. Merge multiple lists: +
  3. Member judge: In
  4. The list element repeats: *
  5. Returns the maximum value in the list (comparing ASCII codes) : Max, min

3.3 Common Operations

  1. Indexing and slicing operations (analogous to strings)
  2. Append: Appends the whole to the end of the list
  3. Extend: Each element in the list is merged to form a large list
  4. Index: Displays the index of an element
  5. Insert: Inserts elements at the specified location
  6. Pop: Remove top element (pop top element on the stack)
  7. Remove: removes the first occurrence of the element; An error is reported if the element does not exist
  8. Reverse: Reverses the order of the elements in a list; Analogy reversed
  9. Sort: Sort of list elements; Analogy sorted

4. Tuple

A tuple can be thought of as a special “list” that cannot be modified.

4.1 create

It is created by parentheses () or the tuple function. Even if there is only one element, the element in a tuple must have a comma at the end

t1 = (1.2.3)
t2 = (4.)# single element
t3 = tuple(("python"."go"."html")
Copy the code

4.2 Common Operations

  1. Find the length: len
  2. Tuple element repetition: *
  3. Tuple stitching: +
  4. View the maximum value: Max, min
  5. Member judge: In
  6. Iterate over tuple elements: for loop
  7. Index and slice

The Dictionary is a Dictionary

Strings, lists, and tuples are all ordered data types, while dictionaries are unordered data types, which are mainly used to store data with certain mapping relationships.

In dictionaries, keys are different, repetitive, immutable data types, and values can be of any data type

5.1 create

There are two ways to create a dictionary:

  • through{}To create a
  • Create it using the dict function
d1 = {"name":"Wang"."sex":"male"}
d2 = dict(["name"."Wang"], ["sex"."male"])
Copy the code

5.2 Common Operations

  • Value: df1[“name”]
  • Df1 [“address”] = “Beijing”
  • Delete key-value pairs by key: del df1[“sex”]
  • Df1 [“name”] = “red”
  • Determines whether the specified key-value pair exists: “birth” in df1

5.3 Common Methods

  1. Clear: clear
  2. Get value: get by key
  3. Update the value of the key-value pair: update
  4. Delete a key value pair: pop
  5. Pick a random key-value pair: popItem
  6. Get value by key (default can be set) : setdefault
  7. Create a default dictionary (with a value of None, which can be specified) : fromKeys
  8. Dictionary traversal, the default traversal keys: keys, values, items

A Set of

Collections cannot have repeating elements and can be considered a “hybrid” of lists and dictionaries.

6.1 create

  • Create by set (empty collection only)
  • through{}create
  • Create frozenset: frozenset

6.2 Common Methods

  1. Add element: add
  2. Update collection: update
  3. Randomly remove element: POP
  4. To specify element removal (error if element does not exist) : remove
  5. Specifies the element to be removed (nonexistent, no error reported) : discard
  6. Clear the collection: clear

6.3 Set Operation

  1. Member judge: In

  2. Subset and superset: ISSubset, Issuperset

  3. Intersection: intersection computes

  4. And set: the union

  5. Difference set: differencee

  6. Symmetric difference set: symmetric_difference