Detailed explanation of data types based on Python

Similar to other languages, Python also divides the data that needs to be represented and manipulated into different data types. Each data value in python is called an object, and this object has three attribute values (unique identifier, data type, value), which correspond to the computer memory address, data type, and data value.

Python data types are classified as follows:

The number type is similar to other programming languages, so I won't explain it in detail here. As the most important basic knowledge in Python, the following mainly sorts out the core knowledge points of strings, lists, tuples, dictionaries, and sets.

First clarify a few concepts

**Sequence: **Data in a fixed order.

**Immutable type: **If the value of the memory space referenced by the variable name cannot be modified, numbers, strings, Boolean values, and tuples are immutable types.

**Variable type: **The value of the memory space referenced by the variable name can be modified. You can add or delete objects to the container, and assign the index of an element in the container to a new object. List (List), dictionary (Dictionary), collection are variable types.

index

Index can be understood as the subscript of the element, and we can get the element in the sequence by index. Each element in the sequence has a position, marked in order, the index is an integer starting from 0, the first position is indexed at 0, the second position is indexed at 1, and so on.

Index usage: use sequence name [index value]

slice

The slice operation (slice) can obtain a substring (part of a string) from a string. We use a pair of square brackets, start offset start, end offset end, and optional step size step to define a slice. Slicing uses index values to limit the range, and cut out a small sequence from a large sequence.

Usage: String [start index: end index: step length]

note:

Examples of commonly used indexes:

str='testers'

Continuous data

The elements are stored continuously, except that there is no element before the first and no element after the last. Support index access and slicing operations.

Including: string, list, tuple

Non-continuous data

Elements are not stored continuously. There may be no elements before and after any element. Indexing and slicing operations are not supported.

Including: Dictionary, Collection

1. String

Concept: A string is an ordered set of characters

In Python, you can use a pair of single quotes, double quotes, and triple quotes to define a string.

create:

s1='hellotesters'
s2="hello world!"

operating:

print(s1[0])#String index
print(s1[0:5])#String slice
print(s1*3)#String copy
print(s1+s2)#String splicing
print(len(s1))#Output string length
# s1.strip()#Truncate the specified characters on both sides of the string
print(s1.split('o'))#Cut the string with letters, o is cut away
print(s1.replace('testers','world'))#Replace testers in s1 with world

2. List

Concept: Store multiple ordered data of any type, belonging to a variable type.

The list is represented by [].

list= ['a','b','c','d']

List creation

list1 =[]#Create an empty list`
list2 = ['a','b','c','d']#Store a list of 4 values in order`
list3 =[10,'hello',True,[1,2,3]]#Any type of data can be stored in the list

Access list object

list[0]#index
list[0:2]#slice

Common operations

list.remove('hello')#Delete specified element
list.reverse()#Reverse list
list.pop()#Delete and return the deleted element according to the index
list.append()#Add an element to the end of the list
list.insert()#Insert an element into the specified position of the list
list.clear()#Clear sequence
list.sort()#Ascending
list.sort(reverse=True)#Descending

3. Tuple

Concept: Similar to a list, but the tuple is of immutable type. After the tuple is created, the value of the tuple cannot be modified, and the elements cannot be added or modified.

Use () to create tuples

tuple1 =()  #Created an empty tuple,If the tuple is not an empty tuple, there must be at least one in it,
tuple2 =(1,2,3,4,5)  #Created a 5-element tuple

Access tuple object

tuple2[0]#index
tuple2[0:2]#slice

Usage scenarios of tuples: tuples cannot be modified, which ensures that the program will not accidentally modify the data, and ensures the integrity and security of the data. Its operation method is basically the same as the list, so when you manipulate the tuple, you treat the tuple as an immutable list.

4. dictionary

A dictionary is a data type in the form of a key-value pair. A key object is linked to a value object and can be queried in the dictionary by key.

Use {} to create a dictionary

Syntax: {key1:value1,key2:value2,key3:value3}

**Description: **

d ={'name':'Zhang San','age':18,'gender':'male'}

**Common operations: **

print(d['name']) #Get value according to key
d['name']='Li Si'#Modify dictionary value
d['address']='Gaoxin Road'#Add key to dictionary-value
del d['a'] #delete
d.clear()#Empty dictionary
d.values()#List of all values
d.keys()#List of all keys
d.items()#Return all items in the dictionary,(key,value)List of tuples

5. Collection

The collection is an unordered, non-repetitive combination of data. It can realize data deduplication and operations such as intersection, difference, and union of two sets of data.

Use {} to create a collection

Three principles of collection:

s ={10,3,5,1,2,1,2,3,1,1,1,1} #Create collection
s ={[1,2,3],[4,6,7]}#Create collection
s.add()#Add elements to the collection

This article is the first of Python basics, and we will continue to update articles from basic to advanced Python in the future, so stay tuned.

Recommended Posts

Detailed explanation of data types based on Python
Detailed explanation of python sequence types
Detailed explanation of the remaining problem based on python (%)
Detailed explanation of python backtracking template
02. Python data types
Detailed explanation of Python IO port multiplexing
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the principle of Python super() method
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Detailed explanation of how python supports concurrent methods
Diagram of using Python process based on FME
Python basic data types
Detailed explanation of building Hadoop environment on CentOS 6.5
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Implementation of business card management system based on python
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of Python web page parser usage examples
Solve the problem of convex hull based on python
Detailed explanation of the attribute access process of Python objects
Detailed implementation of Python plug-in mechanism
Detailed explanation of ubuntu using gpg2
Python error handling assert detailed explanation
Detailed tutorial on installing python3.7 for ubuntu18
Draw personal footprint map based on Python
Detailed analysis of Python garbage collection mechanism
Comprehensive summary of Python built-in exception types
Check matrix calculation results based on python
Detailed usage of Python virtual environment venv
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
How to program based on interfaces in Python
Python implements SSL sending based on QQ mailbox
Common command usage of nmcli based on RHEL8/CentOS8
Detailed tutorial of installing nginx on centos8 (graphic)
Detailed examples of using Python to calculate KS
Python learning-variable types
7 features of Python3.9
Python data model
python data structure
Python data format-CSV
Detailed explanation of Centos 7 system virtual machine bridging mode
Detailed explanation of static DNS configuration under Ubuntu system
Equal Insurance Evaluation: Detailed Explanation of Centos Timeout Exit
Detailed explanation of CentOS7 network setting tutorial in vmware