Python built in data type details

  • 2020-04-02 13:54:32
  • OfStack

Generally speaking, Python is positioned as a high-level dynamic programming language in the scripting language.

Python is mainly about data. Changing the value of a variable means that the variable refers to an address.
Id(variable)- > Displays the address of the variable.
So for a specific value, there are different variable names.

Python data type:

Numbers, strings, lists, tuples, dictionaries
Numbers and strings are actually very basic data types and are not very different from other languages in Python, so I won't go into details here.

The Dictionary is introduced:

Dictionary is one of Python's built-in data types that defines one-to-one relationships between keys and values.
This is what we call a hash array.
Simple definition of Dictionary: Dic={'Key1':'Value1','Key2':'Value2'}
You can't have duplicate keys in a Dictionary; assigning the same Key overrides the original value.
You can add key-value Value pairs to a Dictionary at any time.
There is no such thing as element order in a Dictionary! Because the Key is used to find Value, there is no order.
In a Dictionary, the case of a Key is sensitive!
A Dictionary is not just for storing strings. The value of a Dictionary can be any data type, including the Dictionary itself. Also, in a single Dictionary, the values of a Dictionary need not all be of the same data type, and can be mixed and matched. The data type of the Key of a Dictionary will have relative requirements, but multiple data types can be mixed and matched.
Del can use a Key to delete the corresponding element in the Dictionary. > > > Del dic [12]
Clear() can delete all the values in a Dictionary, but the original Dictionary is still there, but becomes empty {}. > > > Dic. The clear ()

The List is introduced:

List is the most frequently used data type in Python.
The data types in the List can be arbitrary and dynamically extensible.
Simple definition of List: Lis=['a','b',' ABC ','asd']. A List is a collection of ordered elements enclosed in square brackets.
List supports both positive and negative index modes: the positive index is the general case, starting at 0.
The negative index counts from the tail of the List. The last primitive of any non-empty List is always List[-1].
A List supports sharding, which takes data from the middle of a List. It is important to note the starting position of Slice.
Add data to the List: Lis.Append('New') adds data to the end of the List;
Lis. Insert (2,'New') inserts a value at 2 of List;
Lis.extend(['New','Nwe']) links the New List in the original List (at the end).

The difference between Append() and Extend() :

The parameter of Append() can be any data type, of course, it can also be a List, but it is how to List as an element, added to the original List.
Extend() can only be a single List, and all the elements of the entire List can be added to the original List one by one.
(3) search in the List: Lis. Index ('a')
. Index looks up a value in the List and returns the index value that first appears. If it occurs more than once, only the first index value is returned. If not in the List, an exception is returned.
(). To test whether a value is In the List, use In: > > > The value 'c' in lis returns is False.

Delete the element in the List:

Lis. Remove ('a') removes the first occurrence of a value from a List (not all).
Lis. Pop () pop() does two things: removes the last element of the List and returns the deleted element.
The List can be linked with the + operator. List=List+otherList = list.extend(otherList). But the + operator returns a new list (after a link), and extend() only modifies existing lists. So for a large List, Extend() executes faster.
List supports the += operator.
The * operator in the List can act as a repeater on the List. Lis=[1,2]*3 is the same thing as Lis=[1,2] +[1,2]+[1,2]. That is to link the three lists into one.

A Tuple is introduced:

A Tuple is an immutable List. Once a Tuple is created, it cannot be changed in any way.
A simple definition of a Tuple: Tup=('a','b',' ABC ','asd') the entire set of elements is enclosed in parentheses.
Because a Tuple is immutable, it has no methods to add and remove elements. It has the same index as the List and can be sliced in the same way. When you split a List, you get a new List; When you split a Tuple, you get a new Tuple.
A Tuple can also use in to see if an element exists in a Tuple.

Tuple can be regarded as a kind of special List, whose advantages are:

A Tuple is faster than a List. Define a set of constants that you simply store in a Tuple, and the only thing you can do with it is iterate over it.
Tuple can be viewed as "write protection" for data that does not need to be changed. You can make your code more secure.
A Tuple is a Key in a Dictionary, but a List is not! Because the key in a Dictionary has to be immutable.
. A Tuple can be converted to a List. The built-in tuple function can take a List and return a Tupelo with the same element. The List function takes a Tuple and returns a List.

An in-depth understanding of the Python built-in data types described in this article will go a long way toward mastering Python programming.


Related articles: