SoFunction
Updated on 2024-11-19

Python Built-in Data Types Explained

Generally speaking Python is positioned among programming languages as a scripting language - scripting language High-order dynamic programming language.

Python is data-oriented, and a change in the value of a variable means that the variable goes to point to an address.
I.e., Id(variable)-> shows the address of the variable.
So a specific value will have a different variable name.

Python's data types:

Numbers, strings, lists, tuples, dictionaries
Numbers and strings are actually very basic data types, and the difference between Python and other languages is not that big, so I won't go into details here.

Dictionary Introduction:

Dictionary is one of Python's built-in data types that defines a one-to-one relationship between a key and a value.
It's actually what's commonly referred to as a hash array.
Simple definition of Dictionary: Dic={'Key1':'Value1','Key2':'Value2'}
Dictionary can not have a duplicate Key, to the same Key assignment will overwrite the original value.
Key-Value pairs can be added to a Dictionary at any time.
There is no such thing as order of elements in a Dictionary! Because it is through the Key and then find the Value, there is no order.
In Dictionary, Key is case sensitive!
Dictionary is not only used to store strings, the value of the Dictionary can be any data type, including the Dictionary itself. At the same time, in a single Dictionary, Dictionary values do not all need to be of the same data type, can be mixed and matched.Dictionary Key data type will have relative requirements, but also can be a variety of data types can be mixed and matched.
Del can use a Key to remove the corresponding element from a Dictionary. >>>del dic[12]
Clear() removes all values from a Dictionary, but the original Dictionary remains, it just becomes empty {}. >>>()

List Introduction:

List is the most frequently used data type in Python.
The data types in a List can be arbitrary and are dynamically expandable.
Simple definition of List: Lis=['a','b','abc','asd'].List is an ordered collection of elements enclosed in square brackets.
List supports both positive and negative indexing modes: positive indexing is the general case, starting from 0.
Negative indexes are counted from the end of the List. The last original of any non-empty List is always List[-1].
Lists support slicing, both by taking data from the middle of the List. One thing to note is the starting position of the Slice.
Adds data to the List: ('New') adds data to the end of the List;
(2,'New') inserts the value at position 2 of the List;
(['New','Nwe']) links the new List (at the end) within the original List.

Difference between Append() and Extend():

The argument to ①.Append() can be any datatype, and of course it can be a List, but it is adding how List as an element to the original List.
Extend() argument can only be a List, at the same time is the entire List of all the elements, one by one added to the original List.
③. Search in List: ('a')
④.index looks up a value in the List and returns the index value of its first occurrence. If it occurs more than once, only the index value of the first occurrence is returned; if it is not in the List, an exception is returned.
⑤. To test whether a value is in a List, use In: >>>'c' in lis The value returned is False.

Deletes elements in a List:

①. ('a') removes the first occurrence (not all) of a value from the List.
②. () pop() does two things: it deletes the last element of the List and it returns this deleted element.
Lists can be linked using the + operator. list=List+otherList is equivalent to (otherList). But the + operator takes a new list (after linking) as the return value, while extend() only modifies the existing List. so for large Lists, Extend() is executed faster.
④.List supports the += operator.
⑤. the * operator in a List can act as a repeater on a List. lis=[1,2]*3 is equivalent to lis=[1,2]+[1,2]+[1,2]. That is, it is linking three Lists into one.

Tuple presents:

A Tuple is an immutable List. once a Tuple is created, it cannot be changed in any way.
Simple definition of Tuple: Tup=('a','b','abc','asd') The entire set of elements is enclosed in parentheses.
Because Tuple is immutable, it has no methods to add and remove elements. There are the same indexes as a List, which can be partitioned in the same way. When splitting a List, you get a new List; when splitting a Tuple, you get a new Tuple.
Tuple can also use in to see if an element exists in Tuple.

Tuple can be viewed as a special class of List that exists for its own good:

①.Tuples are faster to operate on than Lists. Defining a set of constants is simply a matter of storing it in a Tuple, and the only operation you can perform on it is to keep traversing it.
Tuple can be thought of as "write-protecting" data that does not need to be changed. Tuple can be thought of as "write-protecting" the data that does not need to be changed.
Tuple is available as Key in Dictionary, while List is not! Because the key in a Dictionary must be immutable.
Tuples can be converted to Lists. The built-in tuple function can take a List and return a Tupelo with the same elements; while the List function takes a Tuple and returns a List.

A deeper understanding of the Python built-in data types described in this article will go a long way in becoming proficient in Python programming.