- int (integer type)
- float (floating point number)
- complex
- bool (Boolean value)
- string
- list
- tuple
- set
- dict (dictionary)
int integer type:
- Number number type;
- Integer, there is no limit to the length, it can be as large as it takes up the available memory;
- You can add "_" to separate the numbers, which does not affect the numerical value. It is just for the convenience of reading.
num = 123_456_789
print(num) # 输出123456789
# 默认十进制
print(123) # 123
# 二进制0b开头
print(0b1010) # 10
# 八进制0o开头
print(0o157) # 111
# 十六进制0x开头
print(0x19A) # 410
float floating point number:
- Number number type;
- You can also add "_" to separate the numbers, which does not affect the numerical value, just for the convenience of reading.
a = 1.2 # 1.2
b = .12 # 0.12
c = 12. # 12.0
d = 1_2.3_4 # 12.34
complex plural:
- Number number type;
- Writing method: x + yj, x is the real part and y is the imaginary part.
c = 1+2j
print(c) # (1+2j)
bool Boolean value:
- Number numeric type, bool is a subclass of int;
- True==1, False==0, can be used as an integer to directly participate in calculations.
b1 = True
b2 = False
print(b1+b2, b1+5) # 1 6
string string:
- Single-line string representation: single quotation mark ('') or double quotation mark ("");
- Multi-line string representation: add three single quotes/double quotes at the beginning and end;
- There is no difference between single quotes and double quotes, the string values expressed are equal;
- Python has no concept of characters, only strings of length 1, 'a' == "a", both are strings.
str1 = '单行字符串'
str2 = "单行字符串"
str3 = '''
多行字符串
多行字符串
'''
str4 = """
多行字符串
多行字符串
"""
print(str1 == str2) # True
print(str3 == str4) # True
String addition: The plus sign (+) can concatenate two strings into a new string;
String multiplication: The asterisk (*) can copy the specified number of strings and concatenate them into a new string;
print('hello ' + 'world') # hello world
print('hello ' * 3) # hello hello hello
String index: string[subscript], forward order starts from 0, reverse order starts from -1 (-1 is the last character, -2 is the second to last character at the end);
st = 'hello'
print(st[0], st[1], st[-2], st[-1]) # h e l o
String interception: string [start subscript, end subscript], the subscript is taken from the left but not the right, and does not include the cut-off subscript itself. Zero and positive numbers indicate positive order, and negative numbers indicate reverse order;
st = 'hello'
print(st[0:3], st[0:-2]) # hel hel
list list:
An ordered sequence. The types of elements in the sequence can be different. They are marked with square brackets [*,*,*] and separated by commas (,).
statement:
lst = ['0', 1, 2.3, True, 5]
lst = [] # 空列表
lst = list() # 空列表
Indexing and interception:
In the same way as a string string, the positive order of subscripts starts from 0, and the reverse order starts from -1. When intercepting, the subscripts are taken from the left and not from the right.
(For general use, it is recommended that a list only stores elements of the same type)
lst = ['0', 1, 2.3, True, 5]
print(lst[0]) # 0
print(lst[-1]) # 5
print(lst[0:2]) # ['0', 1]
print(lst[1:-2]) # [1, 2.3]
Addition and multiplication:
In the same way as string strings, addition can connect two lists into a new list; multiplication can copy a specified number of copies of a list and connect them into a new list.
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst3 = lst1 + lst2
lst4 = lst1 * 2
print(lst3) # [1, 2, 3, 4, 5, 6]
print(lst4) # [1, 2, 3, 1, 2, 3]
Traverse:
for item in list:
print(item)
Derivation: (a structure that constructs another new data sequence from one data sequence)
[表达式 for 变量 in 列表]
[out_exp_res for out_exp in input_list]
[表达式 for 变量 in 列表 if 条件]
[out_exp_res for out_exp in input_list if condition]
dict dictionary:
Unordered sequence, the elements are key-value pairs (key, value), the key must be unique and cannot be repeated, and an immutable type must be used . The value can be quickly located through the key;
The key types can be different, and the value types can also be different. Identified by square brackets [*,*,*], commas (,) separate elements, and colons (:) separate key and value within elements.
statement:
dic = {'name': 'xiaoming', 'age': 18}
dic = {} # 空字典
dic = dict() # 空字典
Index to value by key:
str1 = dic['name'] # 如果输入的key不存在,dic[]会报错
str2 = dic.get('name') # dic.get()不会报错
print(str1, str2, str1 == str2) # xiaoming xiaoming True
Dictionary does not support addition and multiplication
Derivation:
{ key_expr: value_expr for value in collection }
{ key_expr: value_expr for value in collection if condition }
tuple:
Similar to the list sequence, in an ordered sequence, the types of elements in the sequence can be different, marked with parentheses (*,*,*), and separated by commas (,) between elements. Unlike list, tuple is an immutable type and cannot be changed after it is declared. It is faster than list.
statement:
tup = ('str', 1, 2, 3.4, 5.6)
tup = () # 空元组
tup = tuple() # 空元组
tup = (1,) # 一个元素的元组,后面要加逗号,不然会识别为int
Indexing and interception:
In the same way as the list list, the forward subscript starts from 0, and the reverse order starts from -1. When intercepting, the subscript is taken from the left instead of the right.
tup = ('str', 1, 2, 3.4, 5.6)
print(tup[0]) # str
print(tup[-1]) # 5.6
print(tup[0:2]) # ('str', 1)
print(tup[1:-2]) # (1, 2)
Addition and multiplication:
In the same way as a list, addition can connect two tuples into a new tuple; multiplication can copy a specified number of tuples and connect them into a new tuple.
tup1 = (1, 2, 3)
tup2 = (4, 5, 6)
tup3 = tup1 + tup2
tup4 = tup1 * 2
print(tup3) # (1, 2, 3, 4, 5, 6)
print(tup4) # (1, 2, 3, 1, 2, 3)
Derivation:
(expression for item in Sequence )
(expression for item in Sequence if conditional )
set collection:
An unordered and non-repeating sequence. The types of elements in the sequence can be different. They are marked with curly braces {*,*,*} and separated by commas (,).
statement:
sett = {'0', 1, 2.3}
sett = set() # 空集合
# sett = {} 不能使用{},已被声明空字典占用
Because the collection is unordered, duplication will be automatically removed after declaration; however, indexing and interception are supported, and addition and multiplication are not supported.
sett = {1, 1, 2, 2, 3, 3}
print(sett) # {1, 2, 3}
Derivation:
{ expression for item in Sequence }
{ expression for item in Sequence if conditional }
Appendix 1 (identifier and type):
id(): Get the unique identifier of the object (memory address of the object), which is an integer;
type(): Get the type of the object.
test = 123
print(id(test)) # 4417458224
print(type(test)) # <class 'int'>
Appendix 2 (variable types and immutable types):
definition:
Immutable type: data cannot be modified in the original memory, and the memory address changes after modification (Number, String, Tuple)
Variable type: data can be modified on the original memory, and the memory address remains unchanged after modification (List list, Dict dictionary, Set collection)
illustrate:
Example of immutable type: data can only be modified by reassignment, and memory will be changed after assignment
num = 1
print(id(num)) # 4354294000
num = 2
print(id(num)) # 4354294032
str1 = 'hello'
print(id(str1)) # 4355664944
str1 = 'world'
print(id(str1)) # 4356996976
tup1 = (1, 2, 3)
# tup1[0] = 4 # 报错,元组不允许修改
Variable type example: elements can be modified individually, and the memory of the sequence does not change after modifying the element; the memory will also change after reassignment
lst1 = [1, 2, 3]
print(id(lst1)) # 4355635392
lst1[0] = 4
lst1[1] = 5
lst1[2] = 6
print(id(lst1)) # 4355635392
lst1 = [4, 5, 6]
print(id(lst1)) # 4355658880
Appendix 3 (Number and String conversion):
Convert between int, float and string:
a = 12
stra = str(a)
floa = float(a)
b = 3.4
strb = str(b)
intb = int(b) # 只取整数部分,舍弃小数部分
c = '5'
d = '6.7'
intc = int(c) # 字符串本身只有数字,没有其他字符
flod = float(d) # 字符串本身只有数字和小数点,没有其他字符
Appendix 4 (convert various types to Bool):
The value of the object is used in calculations as a Boolean value. If the value is False:
- int, float, complex, value == 0
- str string, list list, tuple tuple, set collection, dict dictionary, value == None or empty sequence
num1 = 0 num2 = 0.0 str1 = None str2 = '' lst1 = None lst2 = [] dic1 = None dic2 = {} tup1 = None tup2 = () set1 = None set2 = set()
num = 0
if num:
print(True)
else:
print(False)
# False
str1 = ''
if str1:
print(True)
else:
print(False)
# False
lst = []
if lst:
print(True)
else:
print(False)
# False