How to play several data types in python (1)

Python is a dynamically typed language, which means that in Python you can operate on many different data types, and these data types can be converted automatically. Here are some tricks to play with Python data types:

number:

Integers (1, 100, -786, etc.)
Floating point numbers (1.1, -78.4, 3.14159, etc.)
Complex numbers (3+2j, 4-5j, 5+5j, etc.)
Octal numbers (0o123, 0O123, etc.)
Hexadecimal numbers ( 0x123, 0X123, etc.)

String:

Strings can be created with single or double quotes.
Multiline strings can be created with three single quotes or three double quotes.
We can insert the values ​​of variables and expressions in strings through various methods.
Strings in Python are immutable, so you cannot change any characters in the string.

List:

Lists are one of the most versatile data structures in Python.
Lists can contain elements of different types, such as integers, floats, strings, other lists, etc.
Lists can be nested, i.e. one list can contain another list.
Lists are mutable, you can add, remove, or change elements in the list.

tuple:

Tuples are very similar to lists, but with one key difference: tuples are immutable.
This means you cannot change any element in the tuple. Tuples are often used to represent an ordered collection that does not change.

dictionary:

A dictionary is a collection of key-value pairs in Python.
Each key must be unique in the dictionary, while each value can be any Python object.
You can access values ​​in a dictionary by key, and you can add, delete, or change key-value pairs.

gather:

A set is an unordered collection of non-repeating elements.
The elements in the collection are separated by commas, with a space after each element.
Sets can be created using curly braces {} or the set() function.

Boolean value:

Python has two built-in Boolean values: True and False.
Boolean values ​​are commonly used in conditional statements and logical operations. For example, compare two values ​​to see if they are equal or unequal, check if a number is even, etc.

None:

None is a special type in Python that means "no value" or "nothing".
When a variable is declared but not assigned a value, its value is None. Similarly, some built-in functions (such as len(), type(), str(), int(), etc.) will return None when operating on undefined values.

Composite type:

More complex data structures can be created using combinations of the above data types. For example, you can contain a dictionary within a list, or a list within a tuple, etc.

Builder:

Python's generators provide a more efficient way to create iterators that do not need to store all values, but only store the current value and generate the next value. This is especially useful for large data sets.

Decorator:

Python's decorators can increase the functionality of a function or method without changing its code. A decorator is a function that accepts a function as an argument and returns a new function. It can add additional logic before and after function calls.

Context manager:

Python's context managers allow you to create specific blocks of code in which specific setup and cleanup work can be performed. For example, use the with keyword to automatically manage resources (such as file opening and closing).

Generators and iterators:

Python's generators and iterators allow you to create your own data streams that generate new values ​​as needed, rather than generating them all at once. This is especially useful for large data sets.

Guess you like

Origin blog.csdn.net/babyai996/article/details/132754286