Python basic data type


1, figures

1. Boolean (bool)

Type bool has only two values: true and false

          a = False

          b = True

2. Shaping

Int type

              n = 12

              a = “12”

Convert string type to int method: int (a) default to decimal

Decimal system in type ‚ int can also be converted to other base systems: binary: bin (n) ‚ octal: OCT (n) hex: hex (n)

Operation: + (addition) – (subtraction) * (multiplication) / (Division)% (module, equivalent to remainder) * (power)

Float type


2. String

1. String creation method

              a = “xu”

2. Common string methods:

A. capitalize(): initial capital

A. casefold(): uppercase to lowercase (“value”, n): change the string length to N, if the length is not enough, use “value to supplement” to center the original string.

A.count (sub, start = none, end = none): count the number of times a str occurs from a range

A.endswitch (STR): determines whether the string ends in str. if it returns true, it does not return false.

A.find (“X”): looks for “X” in a string and returns its index value.

‘{0}, {1}’. Format (“a”, “B”): string format

A.isdigit(): determine whether the string is a number and return the bool value.

A.isspace(): determines whether the string is a space and returns the bool value.

A. lower(): change the upper case of a string to lower case

A.upper(): change string lowercase letters to uppercase

A.strip(): remove some special characters from a string

A. replace(): replace

A.split(): split and return a list

A.swapcase(): case reversal

3. List

Create list

                li = []

2. Add, delete, modify and query the list


Li. Append(): append elements at the end of the list

You can insert elements anywhere in the list


Li. Pop(): delete the last element in the list and return it

Li. Remove(): delete the elements in the list according to the value

Li. Clear(): clear list


Li [index] = ‘value’


Len (LI): length of query list

Li. Index(): search a value according to the index




Index value of start

End: index value of the end (not included)

Step: step

Loop traversal list:

                      for i in li:


4. Tuple

1. To create a tuple:

          a = (,)


1. Multiple values can be stored.

2. Immutable

3. Value can be obtained through index.

5. Dictionary
1. Create a dictionary
          dict1 = {}
          dict2 = dict(name=”xu”, age=20)
Batch creation of {}. Fromkeys ([1,2,3,4,5], “Xu”) with the following values unchanged
2. Characteristics of dictionary
Key value structure
2. The key must be immutable and unique
3. It can store any number of values, which can be modified or not unique.
4. Disorder
3. Dictionary addition, deletion, modification and query
1. Increase
              dict[“new_key”] = value
Dict.setdefault (key, value): if the dictionary contains the key, the dictionary will not change, and the value of the key will be returned.
2. Delete
Del dict [key]: delete the specified key value
Dict.pop (key): delete the specified key value and return value
– dict.popitem(): randomly delete a pair of key value pairs
Dict.clear(): clear dictionary
Del dict: delete dictionary
3. Change
Modify the value corresponding to the specified key
Dict1.update (dict2) adds the key value of dict2 to dict1. If dict1 contains the key, modify its corresponding value.
4. Check
Query the value corresponding to the key and return its value
Divt. GetKey: query the value corresponding to the key and return it. If there is no such key, return none.

Dict.keys(): returns all keys in the dictionary in the form of a list.
Dict.values(): returns all values in the dictionary in the form of a list.
Dict.items(): returns all key value pairs in the dictionary. Each key value pair is saved as a tuple, and all key value pairs are encapsulated into a list for return.
5. Traversal list
                for i in dict:
6. Assembly
1. Create a set:
          a1 = {1,2,3,4,5,6}
          li = [1,2,3,4,5,6]
          a2 = set(li)
2. Characteristics of sets
1. The element is immutable
2. If there is no repeating element, it will be naturally de duplicated.
3. Disorder
3. Add / delete a set
1. Increase
2. Delete
Remove (value) to delete an existing value
A.pop(): randomly delete a value and return the value
Remove (value): if this value is not available, an error will be reported.
3. Check
              for i in a:
4. Set relation operation
          a = {1,2,3,4,5}
          b = {3,4,7,8,9}
1. Intersection: take out the elements in two sets that want to be = the same
              a & b
2. Union and union: merge a and B to form a new set, and delete the duplicate elements, leaving only one
              a | b
3. Difference set: take out the elements only in a (b) set and form a new set
              a – b
              b – a
4. Symmetric difference set: merge a and B, return a new set, and remove the elements in a and B.
              a ^ b
5. Include relationship
A.isdisjoint (b): judge whether a and B are disjoint
Issuperset (b): judge whether B is a subset of a
A. issubset (b): judge whether a is a subset of B

6. Common methods of collection
A. diffrence (b): take out the elements only in a to form a new set (same as -)
A. intersection (b): same as (&)
3, A. symmetric (b): same as (^)
4, a.union (b): same as (|)
5, a.differencr update (b): take out the elements only in a and copy them to a again
6. Intersection update (b): take out the elements in a and B, and assign them to a again
A. symmetric difference update (b): take out the symmetric difference set of a and B and assign it to a again