Home >Backend Development >Python Tutorial >What are the basic data types in python
What are the basic data types of python? Let me introduce them to you one by one:
1. Numbers ---> int class
Of course, for numbers, Python’s number types are Int integer type, long long integer type, float floating point number, complex complex number, and Boolean value (0 and 1). Here we only introduce the integer type.
In Python2, the size of integers is limited, that is, when the number exceeds a certain range, it is no longer of int type, but of long type. In Python3, regardless of the size of the integer The length is collectively called an integer.
The main methods are as follows:
int -->Convert the string data type to int type, Note: The content in the string must be numbers
bit_length() -->Convert the number to binary and return the minimum number of binary digits
2, Boolean value --->bool class
For Boolean values, there are only two results, True and False, which correspond to 0 and 1 in binary respectively. There are too many values of True, we only need to know what the values of False are---》None, empty (i.e. [ ]/( ) /" "/{ }), 0;
Related recommendations: "python video tutorial"
3. String --->str class
About string is Python The most commonly used data type in , it has many uses. We can use single quotes '' or double quotes "" to create strings.
Strings cannot be modified. All about characters, we can introduce strings from aspects such as indexing, slicing, length, traversal, deletion, splitting, clearing whitespace, case conversion, determining what starts with, etc.
Create string
Slice
Index--> index(), find()
The difference between index() and find() The point is: if the character or sequence of the index is not in the string, for index--》ValueError: substring not found, and for find--> returns -1.
Length-->len()
Note: The len() method--> can also be used for other data types, such as checking the number of elements in lists, tuples, and dictionaries .
Delete--> del
Judge string content--> isalnum(), isalpha(), isdigit()
Case conversion--> capitalize(), lower(), upper(), title(), casefold()
Determine what starts and ends--> startswith(), endswith()
Extension-- >expandtabs()
Formatted output-->format(), format_map()
Join method
Split--> split(), partition( )
Replacement-->replace
Replacement-->makestran 、translate
4、List --->list class
A list is composed of a series of elements arranged in a specific order. Its elements can be any data type, such as numbers, strings, lists, tuples, dictionaries, Boolean values, etc., and its elements can also be Modified.
The form is:
names = ['little-five","James","Alex"]2 #or 3 names = list(['little-five" ,"James","Alex"])
Index, slice
Append-->append()
Expand-->extend()
Note: The difference between extend and append: --> The former adds elements as a whole, while the latter decomposes elements of data types and adds them to the list. Example:
insert() -->insert
pop() -->remove
remove()-->remove, del --> ;Delete
sorted()-->Sort, the default is forward order, add reverse =True, it means reverse order
5, Tuple --->tuple class
A tuple is an unmodifiable list. Its characteristics are similar to those of list. It is identified using parentheses instead of square brackets.
#Tuple name = ("little-five","xiaowu")print(name[0])
6. Dictionary --->dict class
The dictionary is a series of key-value pairs, each key-value pair is separated by commas, each key corresponds to a value, and the corresponding value can be accessed by using the key. disorderly.
The definition of the key must be immutable, that is, it can be a number, a string, a tuple, a Boolean value, etc.
The definition of value can be any data type.
Traverse -->items, keys, values
7, collection -->set class
About the definition of set: In my opinion, a set is like a basket. You can store things in it and take things out of it. However, these things are unordered and it is difficult for you to specify which one to take separately. things; at the same time, it can be filtered through certain methods to get the part of the things you need. Therefore, sets can be created, added, deleted, and relationally operated.
Characteristics of collections:
1. Deduplication
2. Unordered
3. Each element must be unique The variable type is (hashable type, which can be used as the key of the dictionary).
Create: set, frozenset
Add: add, update
Delete: pop, remove, discard
Relational operations: intersection &, union | , difference set - , intersection complement set^ , issubset , isupperset
Determine whether the relationship between two sets is a subset or parent set--> issubset , isupperset
The above is the detailed content of What are the basic data types in python. For more information, please follow other related articles on the PHP Chinese website!