[Seven data types of Python study notes]

Python data types: Number, Boolean, String, list, tuple, set, dictionary

int integer

a=1
print(a,type(a))

float floating point number

b=1.1
print(b,type(b))

complex plural

c=10+0.5j
print(c,type(c))

bool Boolean value: True, False, true and false are not boolean values ​​in Python (case sensitive)

flag=False
print(flag,type(flag))

str string: strings are represented by double quotes or single quotes

d="Hello"
e='world'
print(d,type(d))
print(e,type(e))

Use d[i] to represent the i+1th character, which is similar to the index in the array, and the string can be regarded as an array

print(d[1],type(d[1]))

Use d[i:j] to intercept the string, for example, d[0:2] means to intercept the first character to the second character of the string d, and the character at index 2 cannot be intercepted

When using d[i:], it means that the interception is from the i+1th character to the last character

print(d[0:2],d[2:])

Use + to concatenate strings

print(d+e)

Use multiplexing strings, for example, d 3 means repeating the string d three times

print(d*3)

List list: equivalent to an array, the elements in the list are accessed through the index, the index value starts from 0, and the maximum value is the length of the list -1 (-1 can be used to represent the last element)

Elements of different data types can be stored in the list, which can be numbers, strings, Boolean values...

list1=[] means to define an empty list len(list1) means the length of the list

# list1= [0]*10 表示创建一个长度为10的列表
# print(list1, type(list1), len(list1))

define a list and assign values ​​to it

list2=[1,2,"A","B","C",True]
print(list2,list2[3],list2[-1])

You can use iteration to access the elements in the list, assign values ​​​​to it, or perform other operations

list1=[]
for i in range(10):
    list1.append(i) #append()方法为添加列表元素到列表末尾
    print(list1[i])

Multidimensional list: that is, list nesting, same as multidimensional array

list3=[['A','B','C','D'],['a','b','c','d'],[1,2,3,4]]
for i in range(len(list3)):
    print(list3[i])

Tuples: Tuples in Python are similar to list lists, identified by (), and support all types of characters, numbers, lists, tuples, etc.

tuple1 = (1,2,3)
print(tuple1,type(tuple1))
tuple2 = (1,"A",True,list1,tuple1)
print(tuple2,type(tuple2))

Tuples cannot be assigned twice, and this error will be reported when the following code is run:tuple1[0]="A" TypeError: 'tuple' object does not support item assignment

Therefore, a tuple is equivalent to an immutable list of lists

# tuple1[0]="A"
# print("tuple1",tuple1,type(tuple1))

Set set: The set in Python is the same as the set concept in mathematics, and it is used to store unique elements, that is, the elements in the set are unique and different from each other.

Use {} to represent a collection, and use "," to separate each two elements

set1={
    
    1,2,3,4,"A"}
print(set1,type(set1))

When using the following code, the output result is: {1, 2, 3, 4} <class 'set'> Length: 4 Because the set collection cannot store repeated elements, when elements with the same value appear, they will be overwritten (that is, only save a value)

set2={
    
    1,2,2,3,4}#定义时存放5个元素,即长度为5
print(set2,type(set2),"长度:",len(set2))

In a set, only immutable data types can be stored, including integers, floating-point types, strings, and tuples. Variable data types such as lists, dictionaries, and sets cannot be stored, otherwise the Python interpreter will throw a TypeError error.

set3={
    
    1,"A",True,tuple1}
print("set3:",set3)

The following code will prompt the following error:TypeError: unhashable type: 'list'

# set4={list1,set1}
# print("set4:",set4)

Note that the set() function is used when creating an empty set collection, and a dictionary is created when using {}

set5=set()
set6={
    
    }
print(type(set5),type(set6)) #<class 'set'> <class 'dict'>

Dictionary: Like a list, a dictionary in Python is a variable sequence that stores data in key-value pairs. Create a dictionary with {key:value}

dict1={
    
    }
print(dict1,type(dict1))
dict2={
    
    "张三":22,"李四":21,"王五":23}
print(dict2)

Access the elements in the dictionary: use [key], use the get(key) method

print(dict2["张三"])
print(dict2.get("李四"))

Modify dictionary element value

dict2["张三"]=25
print(dict2) #{'张三': 25, '李四': 21, '王五': 23}
dict2["李四"]=20
print(dict2) #{'张三': 25, '李四': 20, '王五': 23}

Use in and not in to judge whether an element exists in the dictionary (judgment based on key value, not value)

print("王五" in dict2) #True
print("李四" not in dict2) #False
print(23 in dict2) #False

Add and delete dictionary elements

Add elements: just assign values ​​directly

dict2["刘明"]=22
print(dict2) #{'张三': 25, '李四': 20, '王五': 23, '刘明': 22}

Delete elements: use del

del dict2["刘明"]
print(dict2) #{'张三': 25, '李四': 20, '王五': 23}

Use the keys() and values() methods to get all the keys and values ​​in the dictionary

keys=dict2.keys()
print(keys,type(keys)) #dict_keys(['张三', '李四', '王五']) <class 'dict_keys'>
values=dict2.values()
print(values,type(values)) #dict_values([25, 20, 23]) <class 'dict_values'>

Guess you like

Origin blog.csdn.net/qq_43884946/article/details/129001167