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.
**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'
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
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
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
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
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
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