Python based – 2
The data type
- Number (number type)
- Bool (Boolean)
- STR (string)
- List (list)
- Tuple (tuple)
- Set
- Dict (dictionary)
Among them:
- Immutable data: number (numeric type), bool (Boolean), STR (string), tuple (tuple);
- Mutable data: list, set, dict.
In Python, you do not need to specify a type to declare a variable. You can also assign multiple variables (of different types) simultaneously.
a, b, c = 1.2."The elder brother of the retailer,"
Copy the code
Numeric types – int, float, complex
There are three types of numbers: integers, floating – point numbers and complex numbers. In addition, booleans are subtypes of integers.
- Integers have infinite precision;
- Floating-point numbers are usually used in C
double
To implement the - Complex numbers contain real and imaginary parts, each represented as a floating point number. I’m going to start with a complex numberzTo extract the two parts, you can use
z.real
和z.imag
a, b, c = 20.5.5.4+3j
# a is assigned an integer
# b is assigned to a floating point number
# c is assigned to a complex number
Copy the code
The integer | Floating point Numbers | The plural |
---|---|---|
10 | 0.0 | 3.14 j. |
100 | 15.20 | 45.j |
– 786. | 21.9 | 9.322 e-36 j |
080 | 32.3 e+18 | .876j |
– 0490. | – 90. | -.6545+0J |
-0x260 | 32.54 e100 | 3e+26J |
0x69 | 70.2 e-12 | 4.53 e-7 j |
Boolean type — bool
Boolean values are represented by the constants True and False (case sensitive)
- Comparison operators return bool
- In Python, bool is a subclass of integer, so
True == 1
False == 0
True or False determination:
-
The following will be judged False:
-
None
-
False
-
Zero of any numeric type, such as 0, 0.0, 0j.
-
Any empty sequence, for example, ‘, (), [].
-
Any empty mapping, such as {}.
-
A user-defined instance of a class that defines a bool() or len() method when the method returns the integer zero or bool False.
-
class alfalse() : def __bool__(self) : # defines the __bool__() method, which always returns False return False class alzero() : def __len__(self) : # defines the __len__() method, which always returns 0 return 0 Copy the code
-
-
-
All other expressions are judged True; It is important to note that this is quite different from other languages
String — STR
Text data is processed in Python using STR objects, also known as strings. Strings are immutable sequences of Unicode code points. String definition:
-
Enclosed in single or double quotation marks, with special backslash \ escape characters, and of course multi-line strings with triple quotation marks.
- Single quotation marks:
'Double' quotes allowed '
- Double quotation marks:
"Inclusion of 'single' quotes is allowed."
. - Triple quotation marks:
"Triple single quote" "
.""" "triple double quotation marks """
- Single quotation marks:
-
# quotes str = 'hello' # double quotation marks str = "hello" # Single quotes contain double quotes str = 'say "hello"' # Double quotes contain single quotes str = "say 'hello'" str = """ double-quoted multi-line string """ " str = Single-quoted multi-line string "" Copy the code
-
The string method will be shown in separate statistics later.
List type: list
List is the most frequently used data type in Python.
-
Lists can complete the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).
-
A list is a comma-separated list of elements written between square brackets [].
-
Use list derivations: ‘[x for x in iterable]
-
# General definition list = [] # an empty list list = [1."a"[1] and {"name": "test"}] # list derivation list = [x for x in range(10)] >>> [0.1.2.3.4.5.6.7.8.9] Copy the code
Tuple type: tuple
Tuples are similar to lists except that the elements of a tuple cannot be modified.
-
Use a pair of parentheses to represent an empty tuple: ()
-
Use a comma suffix to indicate a group of units: a, or (a,)
-
Use multiple comma-separated items: a, b, c or (a, b, c)
-
Use built-in tuple(): tuple() or tuple(iterable)
-
# General definition tuple = () # empty tuple tuple = (50.)# single element tuple = ('physics'.'chemistry'.1997.2000); tuple = (1.2.3.4.5 ); tuple = "a"."b"."c"."d"; Copy the code
The set type is set
Set is made up of one or a number of different shapes and sizes of the whole, the things or objects that constitute the set are called elements or members.
-
The basic functionality is membership testing and removing duplicate elements.
-
You can create collections using curly braces {} or the set() function.
-
Note: To create an empty collection, you must use set() instead of {}, because {} is used to create an empty dictionary.
-
# General definition set(value) set = {value, value1} set = {'Google'.'Taobao'.'Runoob'.'Facebook'.'Zhihu'.'Baidu'} # set can perform set operations a = set('abracadabra') b = set('alacazam') print(a - b) The difference between a and b print(a | b) # union of a and b print(a & b) # Intersection of A and B print(a ^ b) The elements of a and B that do not coexist Copy the code
Dictionary type – dict
Dictionaries are another very useful built-in data type in Python.
A dictionary is a data structure that references values by name, and this data structure is called a map. The values in the dictionary are stored under a specific key, which can be numbers, strings or even tuples. Dictionaries are also the only mapping type built into Python
-
A dictionary is a mapping type. A dictionary is identified by {}. It is an unordered set of keys: values.
-
Keys must be of immutable type.
-
A key must be unique in the same dictionary.
-
# General definition dict = {} # empty dictionary dict['one'] = "1 - Rookie Tutorial" dict[2] = "2 - Rookie tools" dict = {'Alice': '2341'.'Beth': '9102'.'Cecil': '3258'}; The constructor is created dict([('Runoob'.1), ('Google'.2), ('Taobao'.3)]) dict(Runoob=1, Google=2, Taobao=3) # derivated dict = {x: x**2 for x in (2.4.6)} Copy the code
-
Note:
- 1. A dictionary is a mapping type whose elements are key-value pairs.
- 2. Dictionary keywords must be immutable and cannot be repeated.